Represents a single conversation session with the Copilot CLI. A session maintains conversation state, handles events, and manages tool execution. Sessions are created via :meth:`CopilotClient.create_session` or resumed via :meth:`CopilotClient.resume_session`. The session pro
| 1365 | |
| 1366 | |
| 1367 | class CopilotSession: |
| 1368 | """ |
| 1369 | Represents a single conversation session with the Copilot CLI. |
| 1370 | |
| 1371 | A session maintains conversation state, handles events, and manages tool execution. |
| 1372 | Sessions are created via :meth:`CopilotClient.create_session` or resumed via |
| 1373 | :meth:`CopilotClient.resume_session`. |
| 1374 | |
| 1375 | The session provides methods to send messages, subscribe to events, retrieve |
| 1376 | conversation history, and manage the session lifecycle. |
| 1377 | |
| 1378 | Attributes: |
| 1379 | session_id: The unique identifier for this session. |
| 1380 | |
| 1381 | Example: |
| 1382 | >>> async with await client.create_session( |
| 1383 | ... on_permission_request=PermissionHandler.approve_all, |
| 1384 | ... ) as session: |
| 1385 | ... # Subscribe to events |
| 1386 | ... unsubscribe = session.on(lambda event: print(event.type)) |
| 1387 | ... |
| 1388 | ... # Send a message |
| 1389 | ... await session.send("Hello, world!") |
| 1390 | ... |
| 1391 | ... # Clean up |
| 1392 | ... unsubscribe() |
| 1393 | """ |
| 1394 | |
| 1395 | def __init__( |
| 1396 | self, session_id: str, client: Any, workspace_path: os.PathLike[str] | str | None = None |
| 1397 | ): |
| 1398 | """ |
| 1399 | Initialize a new CopilotSession. |
| 1400 | |
| 1401 | Note: |
| 1402 | This constructor is internal. Use :meth:`CopilotClient.create_session` |
| 1403 | to create sessions. |
| 1404 | |
| 1405 | Args: |
| 1406 | session_id: The unique identifier for this session. |
| 1407 | client: The internal client connection to the Copilot CLI. |
| 1408 | workspace_path: Path to the session workspace directory |
| 1409 | (when infinite sessions enabled). |
| 1410 | """ |
| 1411 | self.session_id = session_id |
| 1412 | self._client = client |
| 1413 | self._workspace_path = os.fsdecode(workspace_path) if workspace_path is not None else None |
| 1414 | self._event_handlers: set[Callable[[SessionEvent], None]] = set() |
| 1415 | self._event_handlers_lock = threading.Lock() |
| 1416 | self._tool_handlers: dict[str, ToolHandler] = {} |
| 1417 | self._tool_handlers_lock = threading.Lock() |
| 1418 | self._permission_handler: _PermissionHandlerFn | None = None |
| 1419 | self._permission_handler_lock = threading.Lock() |
| 1420 | self._mcp_auth_handler: McpAuthHandler | None = None |
| 1421 | self._mcp_auth_handler_lock = threading.Lock() |
| 1422 | self._user_input_handler: UserInputHandler | None = None |
| 1423 | self._user_input_handler_lock = threading.Lock() |
| 1424 | self._exit_plan_mode_handler: ExitPlanModeHandler | None = None |
no outgoing calls
searching dependent graphs…