Skip to main content

ローカル CLI のセットアップ

SDK のバンドルされた CLI の代わりに、特定の CLI バイナリを使用します。 これは高度なオプションです。CLI パスを明示的に指定し、SDK とのバージョンの互換性を確保する必要があります。

次の場合に使用します。 特定の CLI バージョンをピン留めするか、Go SDK (CLI をバンドルしない) を使用する必要があります。

どのように機能するのか

既定では、Node.js、Python、および.NET SDK には、独自の CLI 依存関係が含まれます (既定のセットアップ (バンドルされた CLI) を参照)。 これをオーバーライドする必要がある場合 (たとえば、システムにインストールされた CLI を使用する場合)、 Connection オプションを使用できます。

図: 説明されたプロセスを示すフローチャート。

主な特性:

  • CLI バイナリ パスを明示的に指定します
  • SDK との CLI バージョンの互換性については、お客様が責任を負います
  • 認証では、システム キーチェーン (または env vars) からサインインしているユーザーの資格情報を使用します
  • 通信は stdio 経由で行われます

Configuration

ローカル CLI バイナリの使用

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

const client = new CopilotClient({
    cliPath: "/usr/local/bin/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
from copilot.generated.session_events import AssistantMessageData
from copilot.session import PermissionHandler

client = CopilotClient({
    "cli_path": "/usr/local/bin/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("Hello!")
if response:
    match response.data:
        case AssistantMessageData() as data:
            print(data.content)

await client.stop()
Go

メモ

Go SDK は CLI をバンドルしないため、常に Connectionを提供する必要があります。

package main

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

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

    client := copilot.NewClient(&copilot.ClientOptions{
        Connection: copilot.StdioConnection{Path: "/usr/local/bin/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!"})
    if response != nil {
        if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
            fmt.Println(d.Content)
        }
    }
}
client := copilot.NewClient(&copilot.ClientOptions{
    Connection: copilot.StdioConnection{Path: "/usr/local/bin/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!"})
if response != nil {
    if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
        fmt.Println(d.Content)
    }
}
.NET
var client = new CopilotClient(new CopilotClientOptions
{
    Connection = RuntimeConnection.ForStdio(path: "/usr/local/bin/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);

追加のオプション

const client = new CopilotClient({
    cliPath: "/usr/local/bin/copilot",

    // Set log level for debugging
    logLevel: "debug",

    // Pass extra CLI arguments
    cliArgs: ["--log-dir=/tmp/copilot-logs"],

    // Set working directory
    cwd: "/path/to/project",
});

環境変数の使用

キーチェーンの代わりに、環境変数を使用して認証できます。 これは、CI や対話型ログインが不要な場合に便利です。

# Set one of these (in priority order):
export COPILOT_GITHUB_TOKEN="gho_xxxx"   # Recommended
export GH_TOKEN="gho_xxxx"               # GitHub CLI compatible
export GITHUB_TOKEN="gho_xxxx"           # GitHub Actions compatible

SDK はこれらを自動的に選択します。コードの変更は必要ありません。

セッションの管理

セッションの既定値はエフェメラルです。 再開可能なセッションを作成するには、独自のセッション ID を指定します。

// Create a named session
const session = await client.createSession({
    sessionId: "my-project-analysis",
    model: "gpt-4.1",
});

// Later, resume it
const resumed = await client.resumeSession("my-project-analysis");

セッションの状態は、 ~/.copilot/session-state/{sessionId}/にローカルに格納されます。

制限事項

制限事項詳細情報
バージョンの互換性CLI のバージョンが SDK と互換性があることを確認する必要があります
単一ユーザー資格情報は、CLI にサインインしたユーザーに関連付けられます
ローカルのみCLI は、アプリと同じコンピューター上で実行されます
マルチテナントなし1 つの CLI インスタンスから複数のユーザーにサービスを提供できない

次のステップ