注意
Copilot SDK 当前处于 技术预览版. 功能和可用性可能会发生更改。
作为应用程序的一部分交付 Copilot 命令行界面(CLI) ,以便用户无需进行其他设置即可开始使用。
**最适合:** 桌面应用、独立工具、电子应用和可分发 CLI 实用工具。
工作原理
在应用程序捆绑包中包含 CLI 二进制文件,而不是依赖全局安装的 CLI。 SDK通过cliPath选项指向你的捆绑复制件。 关键特征包括:
- CLI 二进制文件随应用一起提供 -- 无需单独安装。
- 你可以控制应用使用的确切 CLI 版本。
- 用户通过应用、环境变量或 BYOK 进行身份验证。
- 会话在他们的机器上按用户进行管理。
Setup
步骤 1:在项目中包括 CLI
CLI 作为 npm 包的 @github/copilot 一部分分发。
npm install @github/copilot
步骤 2:将 SDK 指向捆绑 CLI
Node.js/TypeScript
import { CopilotClient } from "@github/copilot-sdk";
import path from "path";
const client = new CopilotClient({
// Point to the CLI binary in your app bundle
cliPath: path.join(__dirname, "vendor", "copilot"),
});
const session = await client.createSession({ model: "gpt-4.1" });
const response = await session.sendAndWait({ prompt: "Hello!" });
console.log(response?.data.content);
await client.stop();
Python
from copilot import CopilotClient, PermissionHandler
from pathlib import Path
client = CopilotClient({
"cli_path": str(Path(__file__).parent / "vendor" / "copilot"),
})
await client.start()
session = await client.create_session(on_permission_request=PermissionHandler.approve_all, model="gpt-4.1")
response = await session.send_and_wait({"prompt": "Hello!"})
print(response.data.content)
await client.stop()
Go
client := copilot.NewClient(&copilot.ClientOptions{
CLIPath: "./vendor/copilot",
})
if err := client.Start(ctx); err != nil {
log.Fatal(err)
}
defer client.Stop()
session, _ := client.CreateSession(ctx, &copilot.SessionConfig{Model: "gpt-4.1"})
response, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
fmt.Println(*response.Data.Content)
.NET
var client = new CopilotClient(new CopilotClientOptions
{
CliPath = Path.Combine(AppContext.BaseDirectory, "vendor", "copilot"),
});
await using var session = await client.CreateSessionAsync(
new SessionConfig { Model = "gpt-4.1" });
var response = await session.SendAndWaitAsync(
new MessageOptions { Prompt = "Hello!" });
Console.WriteLine(response?.Data.Content);
身份验证策略
捆绑 CLI 时,需要确定用户如何进行身份验证。 下图演示了常见模式。

选项 A:用户的登录凭据(最简单的)
用户登录到 CLI 一次,捆绑应用使用这些凭据。 不需要额外的代码 - 这是默认行为。
const client = new CopilotClient({
cliPath: path.join(__dirname, "vendor", "copilot"),
// Default: uses signed-in user credentials
});
选项 B:通过环境变量获取令牌
以编程方式设置令牌或指示用户在启动应用之前设置令牌:
const client = new CopilotClient({
cliPath: path.join(__dirname, "vendor", "copilot"),
env: {
COPILOT_GITHUB_TOKEN: getUserToken(),
},
});
请将 getUserToken() 替换为应用中用于检索用户 OAuth 令牌的 GitHub 逻辑。
选项 C:BYOK (无需 GitHub 身份验证)
如果您管理自己的模型提供商密钥,用户就不需要 GitHub 帐户。
const client = new CopilotClient({
cliPath: path.join(__dirname, "vendor", "copilot"),
});
const session = await client.createSession({
model: "gpt-4.1",
provider: {
type: "openai",
baseUrl: "https://api.openai.com/v1",
apiKey: process.env.OPENAI_API_KEY,
},
});
会话管理
捆绑应用通常需要命名会话,以便用户可以恢复对话:
const client = new CopilotClient({
cliPath: path.join(__dirname, "vendor", "copilot"),
});
// Create a session tied to the user's project
const sessionId = `project-${projectName}`;
const session = await client.createSession({
sessionId,
model: "gpt-4.1",
});
// Resume the session in a later run
const resumed = await client.resumeSession(sessionId);
会话状态存储在 ~/.copilot/session-state/SESSION-ID/,其中 SESSION-ID 是您提供的会话 ID。
分布模式
桌面应用 (Electron, Tauri)
在应用的资源目录中包括 CLI 二进制文件:
import { app } from "electron";
import path from "path";
const cliPath = path.join(
app.isPackaged ? process.resourcesPath : __dirname,
"copilot"
);
const client = new CopilotClient({ cliPath });
CLI 工具
对于可分发的 CLI 工具,请解析相对于您的二进制文件的路径:
import { fileURLToPath } from "url";
import path from "path";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const cliPath = path.join(__dirname, "..", "vendor", "copilot");
const client = new CopilotClient({ cliPath });
特定于平台的二进制文件
针对多个平台进行分发时,请为每个目标包括正确的二进制文件:
my-app/
├── vendor/
│ ├── copilot-darwin-arm64 # macOS Apple Silicon
│ ├── copilot-darwin-x64 # macOS Intel
│ ├── copilot-linux-x64 # Linux x64
│ └── copilot-win-x64.exe # Windows x64
└── src/
└── index.ts
import os from "os";
function getCLIPath(): string {
const platform = process.platform; // "darwin", "linux", "win32"
const arch = os.arch(); // "arm64", "x64"
const ext = platform === "win32" ? ".exe" : "";
const name = `copilot-${platform}-${arch}${ext}`;
return path.join(__dirname, "vendor", name);
}
const client = new CopilotClient({
cliPath: getCLIPath(),
});
局限性
| 限度 | 详细信息 |
|---|
**包大小** | CLI 二进制文件会增加应用的分发大小。 |
| Updates | 在发布周期中管理 CLI 版本更新。 | | 平台构建 | 每个 OS/体系结构都需要单独的二进制文件。 | | 单个用户 | 每个捆绑的 CLI 实例都为一个用户提供服务。 |
后续步骤
- 有关使用 GitHub 帐户登录的用户,请参阅 将 GitHub OAuth 与 Copilot SDK 配合使用。
- 若要在服务器而不是用户计算机上运行,请参阅 为后端服务设置 Copilot SDK。
- 有关安装和第一条消息,请参阅 开始使用 Copilot SDK。