Copilot SDK 内置运行时与会话管理要点

Copilot SDK 安装后自带运行时,Python 需额外下载;SDK 以子进程方式启动运行时并通过 stdio 通信。文档还给出认证方式与会话管理的常见做法,便于开发者搭建可恢复对话的应用。

Python SDK建议安装后一次性下载步骤: ```bash python -m copilot download-runtime ``` 这将下载匹配的运行时间并在当地缓存它。 如果您跳过此步骤, SDK 将尝试在首次使用时自动下载它作为倒置 。 **最佳服务:** 大多数应用程序——台式电脑应用程序、独立工具、CLI公用事业、原型等等。 ## [如何运作](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/bundled-cli#how-it-works>) 当您安装 SDK 时, 将自动包含 Copilot 运行时间( Node.js,.NET) , 或者通过 `python -m copilot download-runtime` (Python). SDK 以儿童进程开始,并在 stdio 上进行交流,没有额外的配置. ![图表:显示描述过程的流程图.](<https://docs.github.com/assets/cb-38745/images/help/copilot/copilot-sdk/setup-bundled-cli-diagram-0.png>) **关键特性:** - CLI二进制包含在 SDK 中——不需要单独的安装 - SDK 管理 CLI 版本以确保兼容性 - 用户通过您的app认证(或使用env vars / BYOK) . - 机上每个用户管理会话 ## [快速启动](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/bundled-cli#quick-start>) 说明 每个SDK都有最低限度的语言运行时间要求——参见该词的Prerequists部分. [节点.js](<https://github.com/github/copilot-sdk/tree/main/nodejs/README.md#prerequisites>), [Python 键](<https://github.com/github/copilot-sdk/tree/main/python/README.md#prerequisites>), [滚开](<https://github.com/github/copilot-sdk/tree/main/go/README.md#prerequisites>), [锈](<https://github.com/github/copilot-sdk/tree/main/rust/README.md#prerequisites>), [爪哇语](<https://github.com/github/copilot-sdk/tree/main/java/README.md#prerequisites>),或 [净值](<https://github.com/github/copilot-sdk/tree/main/dotnet/README.md#prerequisites>) README——由于一个不支持的运行时间(例如,在所述地板下方的Python)可以导致 `pip`/package管理器静悄悄地解决过时的SDK发布,而不是报告版本冲突. TypeScript ```typescript import { CopilotClient } from "@github/copilot-sdk"; const client = new CopilotClient(); const session = await client.createSession({ model: "gpt-5.4" }); const response = await session.sendAndWait({ prompt: "Hello!" }); console.log(response?.data.content); await client.stop(); ``` Python 键 ```python from copilot import CopilotClient from copilot.session import PermissionHandler client = CopilotClient() await client.start() session = await client.create_session(on_permission_request=PermissionHandler.approve_all, model="gpt-5.4") response = await session.send_and_wait("Hello!") print(response.data.content) await client.stop() ``` 滚开 说明 与Node.js,Python和.NET不同,Go SDK没有将CLI列为自动依赖. `NewClient(nil)` 可用时使用嵌入式CLI,然后返回到 `copilot` 打开 `PATH`。要嵌入一个 CLI,运行 [捆绑工具](<https://github.com/github/copilot-sdk/tree/main/go/README.md#distributing-your-application-with-an-embedded-github-copilot-cli>) 在构建时间。您也可以设定 `COPILOT_CLI_PATH` 或点 a `Connection` 。在已存在的二进制中。见 [本地 CLI 设置](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/local-cli>) 详细情况。 ```golang client := copilot.NewClient(nil) if err := client.Start(ctx); err != nil { log.Fatal(err) } defer client.Stop() session, _ := client.CreateSession(ctx, &copilot.SessionConfig{Model: "gpt-5.4"}) response, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"}) if d, ok := response.Data.(*copilot.AssistantMessageData); ok { fmt.Println(d.Content) } ``` 净值 ```csharp await using var client = new CopilotClient(); await using var session = await client.CreateSessionAsync( new SessionConfig { Model = "gpt-5.4" }); var response = await session.SendAndWaitAsync( new MessageOptions { Prompt = "Hello!" }); Console.WriteLine(response?.Data.Content); ``` 爪哇语 说明 Java SDK 不捆绑或嵌入副驾驶 CLI 。 单独安装 CLI 并同时制作 `copilot` 可用时间在您的 `PATH` 或设置其位置为 `setCliPath(...)` (或连接到运行中的 CLI 服务器 ) `setCliUrl(...)`). ```java import com.github.copilot.CopilotClient; import com.github.copilot.rpc.*; var client = new CopilotClient(new CopilotClientOptions() // Point to the CLI binary installed on the system .setCliPath("/path/to/vendor/copilot") ); client.start().get(); var session = client.createSession(new SessionConfig() .setModel("gpt-5.4") .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) ).get(); var response = session.sendAndWait(new MessageOptions() .setPrompt("Hello!")).get(); System.out.println(response.getData().content()); client.stop().get(); ``` ## [认证策略](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/bundled-cli#authentication-strategies>) 您需要决定您的用户将如何认证 。 这里有常见的图案 : ![图表:显示描述过程的流程图.](<https://docs.github.com/assets/cb-70956/images/help/copilot/copilot-sdk/setup-bundled-cli-diagram-1.png>) ### [备选案文A:用户的注册证书(简单)](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/bundled-cli#option-a-users-signed-in-credentials-simplest>) 用户在 CLI 上签名一次, 您的应用程序使用这些证书。 不需要额外的代码- 这是默认行为 。 ```typescript const client = new CopilotClient(); // Default: uses signed-in user credentials ``` ### [备选案文B:通过环境变量获得的奖牌](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/bundled-cli#option-b-token-via-environment-variable>) 将您的应用软件装上指示设置一个符号,或按程序设置: ```typescript const client = new CopilotClient({ env: { COPILOT_GITHUB_TOKEN: getUserToken(), // Your app provides the token }, }); ``` ### [备选案文C:BYOK(不需要GitHub 授权)](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/bundled-cli#option-c-byok-no-github-auth-needed>) 如果你管理自己的模型提供者密钥,用户就根本不需要GitHub账户: ```typescript const client = new CopilotClient(); const session = await client.createSession({ model: "gpt-5.4", provider: { type: "openai", baseUrl: "https://api.openai.com/v1", apiKey: process.env.OPENAI_API_KEY, }, }); ``` 见 **[BYOK( 打开自己的密钥)](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/byok>)** 详细情况。 ## [会话管理](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/bundled-cli#session-management>) Apps通常想要命名的会话,这样用户就可以恢复对话: ```typescript const client = new CopilotClient(); // Create a session tied to the user's project const sessionId = `project-${projectName}`; const session = await client.createSession({ sessionId, model: "gpt-5.4", }); // User closes app... // Later, resume where they left off const resumed = await client.resumeSession(sessionId); ``` 会话状态持续到 `~/.copilot/session-state/{sessionId}/`. ## [何时继续前进](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/bundled-cli#when-to-move-on>) | 需求 | 下一个指南 | | --- | --- | | 用户与 GitHub 账户签约 | [GitHub OAuth 设置](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/github-oauth>) | | 运行在服务器上而不是用户机上 | [后端服务设置](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/backend-services>) | | 使用自己的模型密钥 | [BYOK( 打开自己的密钥)](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/byok>) | ## [今后的步骤](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/bundled-cli#next-steps>) - **[BYOK( 打开自己的密钥)](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/byok>)**: 使用自己的模式提供者密钥 - **[届会续会和持续时间](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/session-persistence>)**: 高级会议管理 - **[构建您的第一个副驾驶驱动应用程序](<https://docs.github.com/en/copilot/get-started/sdk-quickstart>)**: 构建一个完整的应用程序

查看原始来源