> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# はじめに - GitHub Copilot SDK for Laravel

> Copilot を使う最初の Laravel コマンドを作り、Laravel Copilot SDK の基本ワークフローを学びます。

## Copilot 対応の Laravel アプリを最初に作る

このガイドでは、Laravel Copilot SDK を使ってコマンドラインアシスタントを作ります。
1 つのプロンプト送信から始めて、セッション、イベント、ツールへ進みます。

## 前提条件

開始前に次を確認してください。

* GitHub Copilot CLI をインストールし、認証済みであること
* PHP `8.4+`
* Laravel `13.x`

CLI が利用可能か確認します。

```bash theme={null}
copilot --version
```

## SDK をインストールする

Composer でパッケージをインストールします。

```bash theme={null}
composer require revolution/laravel-copilot-sdk
```

必要に応じて設定を公開します。

```bash theme={null}
php artisan vendor:publish --tag=copilot-config
```

必要なら `.env` で CLI のパスを設定します。

```dotenv theme={null}
COPILOT_CLI_PATH=copilot
```

## 最初のメッセージを送る

コマンドを作成し、`Copilot::run()` を呼び出します。

```php theme={null}
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Revolution\Copilot\Facades\Copilot;

class CopilotDemo extends Command
{
    protected $signature = 'copilot:demo';
    protected $description = 'Demo Copilot SDK';

    public function handle()
    {
        $response = Copilot::run(prompt: 'What is 2 + 2?');

        $this->info($response->content());
    }
}
```

コマンドを実行します。

```bash theme={null}
php artisan copilot:demo
```

## セッションでコンテキストを維持する

1 つの会話で複数のプロンプトを使う場合は `Copilot::start()` を使います。

```php theme={null}
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;

Copilot::start(function (CopilotSession $session) {
    $response = $session->sendAndWait(prompt: 'What is 2 + 2?');
    $this->info('Answer: '.$response->content());

    $response = $session->sendAndWait(prompt: 'Now multiply that by 3');
    $this->info('Answer: '.$response->content());
});
```

## セッションイベントを処理する

`on()` でイベントハンドラーを登録し、アシスタントのメッセージや失敗を確認します。

```php theme={null}
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\SessionEvent;

Copilot::start(function (CopilotSession $session) {
    $session->on(function (SessionEvent $event): void {
        if ($event->isAssistantMessage()) {
            $this->info($event->content());
        } elseif ($event->failed()) {
            $this->error($event->errorMessage() ?? 'Unknown error');
        }
    });

    $session->sendAndWait(prompt: 'Tell me a short Laravel joke');
});
```

## カスタムツールを追加する

JSON スキーマとハンドラーを使ってツールを定義します。

```php theme={null}
use Illuminate\JsonSchema\JsonSchema;
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\SessionConfig;
use Revolution\Copilot\Types\Tool;

$parameters = JsonSchema::object([
    'topic' => JsonSchema::string()
        ->description('Topic to look up')
        ->required(),
])->toArray();

$config = new SessionConfig(
    tools: [
        Tool::define(
            name: 'lookup_fact',
            description: 'Returns a fact for a topic.',
            parameters: $parameters,
            handler: function (array $params): array {
                $topic = $params['topic'] ?? '';

                return [
                    'textResultForLlm' => "Fact for {$topic}",
                    'resultType' => 'success',
                    'sessionLog' => "lookup_fact: {$topic}",
                    'toolTelemetry' => [],
                ];
            },
        ),
    ],
);

Copilot::start(function (CopilotSession $session) {
    $response = $session->sendAndWait(
        prompt: 'Use lookup_fact to tell me something about Laravel.'
    );

    $this->info($response->content());
}, config: $config);
```

## 対話型アシスタントを作る

セッション、イベント、ツールをまとめると、対話型の Artisan コマンドを作成できます。

```php theme={null}
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\JsonSchema\JsonSchema;
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\SessionConfig;
use Revolution\Copilot\Types\SessionEvent;
use Revolution\Copilot\Types\Tool;
use Revolution\Copilot\Types\ToolResultObject;

use function Laravel\Prompts\error;
use function Laravel\Prompts\info;
use function Laravel\Prompts\note;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\text;

class CopilotAssistant extends Command
{
    protected $signature = 'copilot:assistant';
    protected $description = 'Interactive Copilot assistant';

    public function handle()
    {
        $facts = [
            'PHP' => 'A popular general-purpose scripting language for web development.',
            'Laravel' => 'A web application framework with expressive, elegant syntax.',
            'Composer' => 'Dependency manager for PHP.',
        ];

        $parameters = JsonSchema::object([
            'topic' => JsonSchema::string()
                ->description('Topic to look up')
                ->required(),
        ])->toArray();

        $config = new SessionConfig(
            tools: [
                Tool::define(
                    name: 'lookup_fact',
                    description: 'Returns a fun fact about a given topic.',
                    parameters: $parameters,
                    handler: function (array $params) use ($facts) {
                        $topic = $params['topic'] ?? '';
                        $fact = $facts[$topic] ?? "No fact available for {$topic}.";

                        return new ToolResultObject(
                            textResultForLlm: $fact,
                            resultType: 'success',
                            sessionLog: "lookup_fact: {$topic}",
                            toolTelemetry: [],
                        );
                    },
                ),
            ],
        );

        Copilot::start(function (CopilotSession $session) {
            info('Copilot assistant');
            info("Session: {$session->id()}");
            info("Try: Use lookup_fact to tell me about Laravel");

            $session->on(function (SessionEvent $event): void {
                if ($event->isAssistantMessage()) {
                    note($event->content());
                } elseif ($event->failed()) {
                    error($event->errorMessage() ?? 'Unknown error');
                }
            });

            while (true) {
                $prompt = text(
                    label: 'You',
                    placeholder: 'Ask me anything...',
                    required: true,
                    hint: 'Ctrl+C to exit',
                );

                spin(
                    callback: fn () => $session->sendAndWait($prompt),
                    message: 'Thinking...',
                );

                echo "\n";
            }
        }, config: $config);
    }
}
```

コマンドを実行します。

```bash theme={null}
php artisan copilot:assistant
```

## ツールの仕組み

ツールを定義するときは次の 3 点を指定します。

1. ツールが何をするか。
2. どのパラメーターを受け取るか。
3. どのハンドラーコードを実行するか。

Copilot はユーザー入力に応じてツール呼び出しを判断し、SDK がハンドラーを実行して結果を返します。

## 次に試す機能

### MCP サーバーを接続する

```php theme={null}
use Revolution\Copilot\Types\SessionConfig;

$config = new SessionConfig(
    mcpServers: [
        'github' => [
            'type' => 'http',
            'url' => 'https://api.githubcopilot.com/mcp/',
        ],
    ],
);
```

### カスタムエージェントを作る

```php theme={null}
use Revolution\Copilot\Types\SessionConfig;

$config = new SessionConfig(
    customAgents: [
        [
            'name' => 'pr-reviewer',
            'displayName' => 'PR Reviewer',
            'description' => 'Reviews pull requests for best practices',
            'prompt' => 'You are an expert code reviewer. Focus on security, performance, and maintainability.',
        ],
    ],
);
```

### システムメッセージをカスタマイズする

```php theme={null}
use Revolution\Copilot\Types\SessionConfig;
use Revolution\Copilot\Types\SystemMessageConfig;

$config = new SessionConfig(
    systemMessage: new SystemMessageConfig(
        content: 'You are a helpful assistant for our engineering team. Always be concise.',
    ),
);
```

## 外部 CLI サーバーに接続する

Copilot CLI をサーバーモードで起動します。

```bash theme={null}
copilot --headless --port 4321
```

次に `.env` で接続先 URL を設定します。

```dotenv theme={null}
COPILOT_URL=tcp://127.0.0.1:4321
```

`COPILOT_URL` を設定すると、SDK は新しい CLI プロセスを起動せず、既存のサーバーに接続します。

## テレメトリーと可観測性

`config/copilot.php` でテレメトリーを設定します。

```php theme={null}
'telemetry' => [
    'otlpEndpoint' => 'http://localhost:4318',
],
```

または直接設定します。

```php theme={null}
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\TelemetryConfig;

Copilot::useStdio([
    'telemetry' => new TelemetryConfig(
        otlpEndpoint: 'http://localhost:4318',
    ),
]);
```

## さらに学ぶ

* [認証](/jp/packages/laravel-copilot-sdk/auth)
* [MCP](/jp/packages/laravel-copilot-sdk/mcp)
* [カスタムプロバイダー](/en/packages/laravel-copilot-sdk/custom-providers)
* [テレメトリー](/en/packages/laravel-copilot-sdk/telemetry)
* [SessionConfig](/jp/packages/laravel-copilot-sdk/session-config)
* [SessionEvent](/en/packages/laravel-copilot-sdk/session-event)
* [Laravel Copilot SDK 概要](/jp/packages/laravel-copilot-sdk)
* [公式 SDK リポジトリ](https://github.com/github/copilot-sdk)

## 次のステップ

<Columns cols={2}>
  <Card title="認証" href="/jp/packages/laravel-copilot-sdk/auth">
    ローカル環境と CI に適した認証方式を選びます。
  </Card>

  <Card title="SessionConfig" href="/jp/packages/laravel-copilot-sdk/session-config">
    モデル、フック、MCP サーバー、実行時の挙動を設定します。
  </Card>

  <Card title="SessionEvent" href="/jp/packages/laravel-copilot-sdk/session-event">
    Laravel 固有のイベントヘルパーとライフサイクル処理を理解します。
  </Card>

  <Card title="権限リクエスト" href="/jp/packages/laravel-copilot-sdk/permission-request">
    明示的な承認でツールを実行する権限フローを確認します。
  </Card>

  <Card title="ツール" href="/jp/packages/laravel-copilot-sdk/tools">
    Copilot からアプリケーションコードを呼び出します。
  </Card>

  <Card title="MCP" href="/jp/packages/laravel-copilot-sdk/mcp">
    MCP サーバーの既成ツールを接続します。
  </Card>
</Columns>

ここまでで、単発プロンプトからツール連携セッションまでの Laravel Copilot SDK の中核フローを一通り確認できました。

<Info>
  参考: [Laravel Copilot SDK README](https://github.com/invokable/laravel-copilot-sdk/blob/main/README.md)、[Laravel Copilot SDK Getting Started](https://github.com/invokable/laravel-copilot-sdk/blob/main/docs/getting-started.md)、[GitHub Copilot SDK](https://github.com/github/copilot-sdk)
</Info>
