Skip to main content

默认设置(随附的 CLI)

Node.js、Python和.NET SDK 包含 Copilot CLI 作为依赖项, 你的应用附带了它所需的一切,无需额外的安装或配置。

最适合: 大多数应用程序 - 桌面应用、独立工具、CLI 实用工具、原型等。

工作原理

安装 SDK 时,会自动包含 Copilot CLI 二进制文件。 SDK 将其作为子进程启动,并通过 stdio 进行通信。 无需额外配置。

图示:显示所述过程的流程图。

主要特征:

  • SDK 附带 CLI 二进制文件 - 无需单独安装
  • SDK 管理 CLI 版本以确保兼容性
  • 用户通过应用进行身份验证(或使用 env vars /BYOK)
  • 会话在用户本机上按用户进行管理

快速入门

代码语言 navigation

TypeScript
import { CopilotClient } from "@github/copilot-sdk";

const client = new CopilotClient();

const session = await client.createSession({ model: "gpt-4.1" });
const response = await session.sendAndWait({ prompt: "Hello!" });
console.log(response?.data.content);

await client.stop();

身份验证策略

你需要确定用户如何进行身份验证。 下面是常见模式:

图示:显示所述过程的流程图。

选项 A:用户的登录凭据(最简单的)

用户只需登录 CLI 一次,之后你的应用就会使用这些凭据。 不需要额外的代码 - 这是默认行为。

const client = new CopilotClient();
// Default: uses signed-in user credentials

选项 B:通过环境变量传递令牌

发布应用时附带设置令牌的说明,或通过编程方式设置令牌:

const client = new CopilotClient({
    env: {
        COPILOT_GITHUB_TOKEN: getUserToken(),  // Your app provides the token
    },
});

选项 C:BYOK (无需GitHub身份验证)

如果管理自己的模型提供程序密钥,用户根本不需要GitHub帐户:

const client = new CopilotClient();

const session = await client.createSession({
    model: "gpt-4.1",
    provider: {
        type: "openai",
        baseUrl: "https://api.openai.com/v1",
        apiKey: process.env.OPENAI_API_KEY,
    },
});

有关完整详细信息,请参阅 BYOK (自带密钥)

会话管理

应用通常需要命名会话,以便用户可以恢复对话:

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-4.1",
});

// User closes app...
// Later, resume where they left off
const resumed = await client.resumeSession(sessionId);

会话状态保留在 ~/.copilot/session-state/{sessionId}/

何时继续

需要下一篇指南
使用 GitHub 帐户登录的用户
GitHub OAuth 设置
在服务器上运行,而不是用户计算机
后端服务设置
使用自己的模型密钥
BYOK (自带密钥)

后续步骤