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

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

<!-- markdownlint-disable GHD046 GHD005 -->

<!-- Suppressed: GHD046 (outdated release terminology), GHD005 (hardcoded data variable) -->

**次の場合に使用します。** 特定の CLI バージョンをピン留めするか、Go SDK を使用する必要があります (CLI は自動的に含まれません)。

## どのように機能するのか

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

![図: 説明されたプロセスを示すフローチャート。](/assets/images/help/copilot/copilot-sdk/setup-local-cli-diagram-0.png)

**主な特性:**

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

## Configuration

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

<div class="ghd-codetabs">
<div class="ghd-codetab" data-lang="typescript" data-label="TypeScript"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">TypeScript</div>

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

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

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

await client.stop();
```

</div>

<div class="ghd-codetab" data-lang="python" data-label="Python"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">Python</div>

```python
from copilot import CopilotClient
from copilot.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-5.4")
response = await session.send_and_wait("Hello!")
if response:
    match response.data:
        case AssistantMessageData() as data:
            print(data.content)

await client.stop()
```

</div>

<div class="ghd-codetab" data-lang="go" data-label="Go"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">Go</div>

> \[!NOTE]
> Go SDK は CLI を自動的に出荷しません。
> `PATH`に`copilot`をインストールし、`COPILOT_CLI_PATH`環境変数を設定するか、[バンドル ツール](https://github.com/github/copilot-sdk/tree/main/go/README.md#distributing-your-application-with-an-embedded-github-copilot-cli)を使用して CLI を埋め込むか、インストールされているバイナリで`StdioConnection.Path`をポイントします。

```golang
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-5.4"})
response, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
if response != nil {
    if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
        fmt.Println(d.Content)
    }
}
```

</div>

<div class="ghd-codetab" data-lang="dotnet" data-label=".NET"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">.NET</div>

```csharp
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-5.4" });

var response = await session.SendAndWaitAsync(
    new MessageOptions { Prompt = "Hello!" });
Console.WriteLine(response?.Data.Content);
```

</div>

</div>

## 追加のオプション

```typescript
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 や対話型ログインが不要な場合に便利です。

```bash
# 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 を指定します。

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

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

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

## 制限事項

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

## 次のステップ

* **[既定のセットアップ (バンドルされた CLI)](/ja/copilot/how-tos/copilot-sdk/setup/bundled-cli)**: SDK の組み込み CLI を使用する (ほとんどのユース ケースに推奨)
* **[初めてのCopilot搭載アプリを構築する](/ja/copilot/how-tos/copilot-sdk/getting-started)**: 完全な対話型アプリを構築する
* **[認証](/ja/copilot/how-tos/copilot-sdk/auth/authenticate)**: すべての認証方法の詳細