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)
  • 会话在其计算机上按用户进行管理

快速入门

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();
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-4.1")
response = await session.send_and_wait("Hello!")
print(response.data.content)

await client.stop()
Go

注意

Go SDK 不会捆绑 CLI。 您必须单独安装 CLI,或将 Connection 设置为指向现有二进制文件。 有关详细信息,请参阅“本地 CLI 设置”。

package main

import (
    "context"
    "fmt"
    "log"
    copilot "github.com/github/copilot-sdk/go"
)

func main() {
    ctx := context.Background()

    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-4.1"})
    response, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
    if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
        fmt.Println(d.Content)
    }
}
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-4.1"})
response, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
    fmt.Println(d.Content)
}
.NET
await using var client = new CopilotClient();
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);
Java

注意

Java SDK 不会捆绑或嵌入Copilot CLI。 您必须单独安装 CLI,并通过 ConnectionCOPILOT_CLI_PATH 环境变量配置其路径。

import com.github.copilot.sdk.CopilotClient;
import com.github.copilot.sdk.events.*;
import com.github.copilot.sdk.json.*;

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-4.1")
    .setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();

var response = session.sendAndWait(new MessageOptions()
    .setPrompt("Hello!")).get();
System.out.println(response.getData().content());

client.stop().get();

身份验证策略

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

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

选项 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 (bring your own key)

会话管理

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

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 (bring your own key)

后续步骤