Send a message to this session and wait until the session becomes idle. This is a convenience method that combines :meth:`send` with waiting for the session.idle event. Use this when you want to block until the assistant has finished processing the message.
(
self,
prompt: str,
*,
attachments: list[Attachment] | None = None,
mode: Literal["enqueue", "immediate"] | None = None,
agent_mode: Literal["interactive", "plan", "autopilot", "shell"] | None = None,
request_headers: dict[str, str] | None = None,
display_prompt: str | None = None,
timeout: float = 60.0,
)
| 1557 | return message_id |
| 1558 | |
| 1559 | async def send_and_wait( |
| 1560 | self, |
| 1561 | prompt: str, |
| 1562 | *, |
| 1563 | attachments: list[Attachment] | None = None, |
| 1564 | mode: Literal["enqueue", "immediate"] | None = None, |
| 1565 | agent_mode: Literal["interactive", "plan", "autopilot", "shell"] | None = None, |
| 1566 | request_headers: dict[str, str] | None = None, |
| 1567 | display_prompt: str | None = None, |
| 1568 | timeout: float = 60.0, |
| 1569 | ) -> SessionEvent | None: |
| 1570 | """ |
| 1571 | Send a message to this session and wait until the session becomes idle. |
| 1572 | |
| 1573 | This is a convenience method that combines :meth:`send` with waiting for |
| 1574 | the session.idle event. Use this when you want to block until the assistant |
| 1575 | has finished processing the message. |
| 1576 | |
| 1577 | Events are still delivered to handlers registered via :meth:`on` while waiting. |
| 1578 | |
| 1579 | Args: |
| 1580 | prompt: The message text to send. |
| 1581 | attachments: Optional file, directory, or selection attachments. |
| 1582 | mode: Message delivery mode (``"enqueue"`` or ``"immediate"``). |
| 1583 | agent_mode: The UI mode the agent was in when this message was sent |
| 1584 | (for example ``"plan"`` or ``"autopilot"``). Defaults to the |
| 1585 | session's current mode when unset. |
| 1586 | request_headers: Optional per-turn HTTP headers for outbound model requests. |
| 1587 | display_prompt: If provided, this is shown in the timeline instead of |
| 1588 | ``prompt``. |
| 1589 | timeout: Timeout in seconds (default: 60). Controls how long to wait; |
| 1590 | does not abort in-flight agent work. |
| 1591 | |
| 1592 | Returns: |
| 1593 | The final assistant message event, or None if none was received. |
| 1594 | |
| 1595 | Raises: |
| 1596 | TimeoutError: If the timeout is reached before session becomes idle. |
| 1597 | Exception: If the session has been disconnected or the connection fails. |
| 1598 | |
| 1599 | Example: |
| 1600 | >>> from copilot.session_events import AssistantMessageData |
| 1601 | >>> response = await session.send_and_wait("What is 2+2?") |
| 1602 | >>> if response: |
| 1603 | ... match response.data: |
| 1604 | ... case AssistantMessageData() as data: |
| 1605 | ... print(data.content) |
| 1606 | """ |
| 1607 | total_start = time.perf_counter() |
| 1608 | idle_event = asyncio.Event() |
| 1609 | error_event: Exception | None = None |
| 1610 | last_assistant_message: SessionEvent | None = None |
| 1611 | first_assistant_message_logged = False |
| 1612 | |
| 1613 | def handler(event: SessionEventTypeAlias) -> None: |
| 1614 | nonlocal first_assistant_message_logged, last_assistant_message, error_event |
| 1615 | match event.data: |
| 1616 | case AssistantMessageData(): |