Skip to main content

Copilot SDK でのローカル CLI の使用

          GitHub Copilot SDKは、お使いのコンピューターに既にサインインしている CLI で使用します。最も簡単な構成で、認証コードやインフラストラクチャは必要ありません。

この機能を使用できるユーザーについて

GitHub Copilot SDK は、すべての Copilot プランで使用できます。

メモ

          Copilot SDK は現在 テクニカル プレビューです。 機能と可用性は変更される場合があります。
          GitHub Copilot SDKをローカルにサインインした CLI に接続することが、最も早く開始する方法です。 

          **次の場合に最適です。** 個人プロジェクト、プロトタイプ作成、ローカル開発、SDK の学習。

どのように機能するのか

          Copilot CLIをインストールしてサインインすると、資格情報はシステム キーチェーンに格納されます。 SDK は、子プロセスとして CLI を自動的に開始し、保存されている資格情報を使用します。 主な特性:
  • CLI は SDK によって自動的に生成されます。セットアップは必要ありません。
  • 認証では、システム キーチェーンからサインインしているユーザーの資格情報が使用されます。
  • 通信は stdio (stdin/stdout) 経由で行われます。ネットワーク ポートは開かっていません。
  • セッションはコンピューターに対してローカルです。

簡単スタート

既定の構成では、オプションは必要ありません。

Node.js/ 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, 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({"prompt": "Hello!"})
print(response.data.content)

await client.stop()

Go

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!"})
fmt.Println(*response.Data.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);

SDK は、CLI の開始、認証、セッションの管理など、すべてを処理します。

これはどのように内部的に機能しますか?

コンポーネント間の相互作用の順序の詳細については、 github/copilot-sdkrepository のシーケンス図を参照してください。

構成オプション

ほとんどの場合、既定値は機能しますが、ローカルセットアップをカスタマイズできます。

const client = new CopilotClient({
    // Override CLI location (default: bundled with @github/copilot)
    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="YOUR-GITHUB-TOKEN"   # Recommended
export GH_TOKEN="YOUR-GITHUB-TOKEN"               # GitHub CLI compatible
export GITHUB_TOKEN="YOUR-GITHUB-TOKEN"           # GitHub Actions compatible
          `YOUR-GITHUB-TOKEN`を有効なGitHubpersonal access tokenまたは OAuth トークンに置き換えます。 SDK はこれらを自動的に選択します。コードの変更は必要ありません。

セッションの管理

ローカル CLI では、セッションは既定で一時的です。 再開可能なセッションを作成するには、セッション ID を指定します。

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

// Resume it in a later run
const resumed = await client.resumeSession("my-project-analysis");

セッションの状態は、 ~/.copilot/session-state/SESSION-ID/にローカルに格納されます。ここで、 SESSION-ID は指定したセッション ID です。

制限事項

制限事項詳細情報
          **単一ユーザー** | 資格情報は、CLI にサインインしたユーザーに関連付けられます。 |

| ローカルのみ | CLI は、アプリと同じコンピューター上で実行されます。 | | マルチテナントなし | 1 つの CLI インスタンスから複数のユーザーにサービスを提供することはできません。 | | CLI サインインが必要 | ユーザーは copilot を実行し、最初に認証する必要があります。 |

次のステップ