> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# send() 與 on()

> 說明如何組合 Laravel Copilot SDK 的 send() / sendAndWait() 與 on(),處理中間事件與最終回應。

## Session `send()` 與 `on()`

`sendAndWait()` 因為回應會立即傳回而易於理解,但 **只能接收最後一則 assistant 訊息**。

```php theme={null}
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;

Copilot::start(function (CopilotSession $session) {
    $response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
    dump($response->content());
});
```

若也想接收中間訊息,可透過 `on()` 註冊事件監聽器。

```php theme={null}
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\SessionEvent;

Copilot::start(function (CopilotSession $session) {
    $session->on(function (SessionEvent $event): void {
        if ($event->isAssistantMessage()) {
            dump($event->content());
        } else {
            dump($event);
        }
    });

    $message_id = $session->send(prompt: 'Tell me something about Laravel.');

    // 以迴圈等待訊息接收
    $session->wait(timeout: 60.0);
});
```

在 PHP 中這樣寫可能不太好理解,因此建議組合使用 `on()` 與 `sendAndWait()`。

```php theme={null}
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\SessionEvent;

Copilot::start(function (CopilotSession $session) {
    $session->on(function (SessionEvent $event): void {
        if ($event->isAssistantMessage()) {
            dump($event->content());
        } else {
            dump($event);
        }
    });

    $response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');

    // 用迴圈等待的部分已在 sendAndWait() 內處理,因此此時已經收到最後一則訊息。
    // 中間訊息則透過上方的 on() 接收。

    // 不需要從 sendAndWait 取得的最終訊息。
    // dump($response->content());
});
```

## 在 `on()` 指定特定事件類型

可以用 `SessionEventType` enum 或字串指定事件類型,只訂閱該事件。

```php theme={null}
use Revolution\Copilot\Enums\SessionEventType;
use Revolution\Copilot\Types\SessionEvent;

$session->on(SessionEventType::ASSISTANT_MESSAGE, function (SessionEvent $event): void {
    dump($event->content());
});

$session->on('assistant.message', function (SessionEvent $event): void {
    dump($event->content());
});
```

## 訂閱所有事件類型

若不指定事件類型即會訂閱所有事件。

```php theme={null}
use Revolution\Copilot\Enums\SessionEventType;
use Revolution\Copilot\Types\SessionEvent;

$session->on(function (SessionEvent $event): void {
});

// 也可以使用具名參數指定
$session->on(handler: function (SessionEvent $event): void {
});

// 也可以允許 null 的動態類型指定。
$type = null;
$session->on(type: $type, handler: function (SessionEvent $event): void {
});
```

<Info>
  最新資訊請參閱 [GitHub 儲存庫](https://github.com/invokable/laravel-copilot-sdk)。
</Info>


## Related topics

- [Steering 與 Queueing](/zh-TW/packages/laravel-copilot-sdk/steering.md)
- [Queue 與 Job](/zh-TW/queues.md)
- [BlueskyManager 與 HasShortHand](/zh-TW/packages/laravel-bluesky/bluesky-manager.md)
- [教學 - Laravel Console Starter](/zh-TW/packages/laravel-console-starter/tutorial.md)
- [Socialite - Laravel Bluesky](/zh-TW/packages/laravel-bluesky/socialite.md)
