Main client for interacting with the Copilot CLI. The CopilotClient manages the connection to the Copilot CLI server and provides methods to create and manage conversation sessions. It can either spawn a CLI server process or connect to an existing server. The client supports
| 1060 | |
| 1061 | |
| 1062 | class CopilotClient: |
| 1063 | """ |
| 1064 | Main client for interacting with the Copilot CLI. |
| 1065 | |
| 1066 | The CopilotClient manages the connection to the Copilot CLI server and provides |
| 1067 | methods to create and manage conversation sessions. It can either spawn a CLI |
| 1068 | server process or connect to an existing server. |
| 1069 | |
| 1070 | The client supports both stdio (default) and TCP transport modes for |
| 1071 | communication with the CLI server. |
| 1072 | |
| 1073 | Example: |
| 1074 | >>> # Create a client with default options (spawns CLI server) |
| 1075 | >>> client = CopilotClient() |
| 1076 | >>> await client.start() |
| 1077 | >>> |
| 1078 | >>> # Create a session and send a message |
| 1079 | >>> session = await client.create_session( |
| 1080 | ... on_permission_request=PermissionHandler.approve_all, |
| 1081 | ... model="gpt-4", |
| 1082 | ... ) |
| 1083 | >>> session.on(lambda event: print(event.type)) |
| 1084 | >>> await session.send("Hello!") |
| 1085 | >>> |
| 1086 | >>> # Clean up |
| 1087 | >>> await session.disconnect() |
| 1088 | >>> await client.stop() |
| 1089 | |
| 1090 | >>> # Or connect to an existing server |
| 1091 | >>> client = CopilotClient( |
| 1092 | ... connection=RuntimeConnection.for_uri("localhost:3000"), |
| 1093 | ... ) |
| 1094 | """ |
| 1095 | |
| 1096 | def __init__( |
| 1097 | self, |
| 1098 | *, |
| 1099 | connection: RuntimeConnection | None = None, |
| 1100 | working_directory: str | None = None, |
| 1101 | log_level: LogLevel = "info", |
| 1102 | env: dict[str, str] | None = None, |
| 1103 | github_token: str | None = None, |
| 1104 | base_directory: str | None = None, |
| 1105 | use_logged_in_user: bool | None = None, |
| 1106 | telemetry: TelemetryConfig | None = None, |
| 1107 | session_fs: SessionFsConfig | None = None, |
| 1108 | request_handler: CopilotRequestHandler | None = None, |
| 1109 | session_idle_timeout_seconds: int | None = None, |
| 1110 | enable_remote_sessions: bool = False, |
| 1111 | on_list_models: Callable[[], list[ModelInfo] | Awaitable[list[ModelInfo]]] | None = None, |
| 1112 | mode: CopilotClientMode = "copilot-cli", |
| 1113 | ): |
| 1114 | """ |
| 1115 | Initialize a new CopilotClient. |
| 1116 | |
| 1117 | All process-management options (``working_directory``, ``log_level``, |
| 1118 | ``env``, ``github_token``, …) apply only when the SDK spawns the runtime |
| 1119 | (stdio / tcp connections). They are ignored when connecting to an |
no outgoing calls
searching dependent graphs…