Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions docs/jp/permission-request.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
# Permission Request

## 基本的な使い方
## Deny by Default

SessionConfigの `onPermissionRequest` にクロージャを指定するとCopilotから権限を要求された時に呼び出される。`$request`と`$invocation`は下記のような内容の配列。
ツールの操作(ファイル書き込み、シェルコマンド、URL取得、MCP呼び出しなど)はデフォルトで**拒否**される。許可するには `onPermissionRequest` ハンドラを指定する必要がある。

## PermissionHandler::approveAll()

すべてのリクエストを自動的に許可する場合は `PermissionHandler::approveAll()` を使う。

```php
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Support\PermissionHandler;
use Revolution\Copilot\Types\SessionConfig;

$config = new SessionConfig(
onPermissionRequest: PermissionHandler::approveAll(),
);

$response = Copilot::run(prompt: 'Hello', config: $config);
```

## カスタムハンドラ

リクエストの種類に応じて個別に許可・拒否を制御する場合は、クロージャを指定する。`$request`と`$invocation`は下記のような内容の配列。

```php
use Illuminate\Support\Facades\Artisan;
Expand Down
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function createSession(SessionConfig|array $config = []): CopilotSession
'availableTools' => $config['availableTools'] ?? null,
'excludedTools' => $config['excludedTools'] ?? null,
'provider' => $config['provider'] ?? null,
'requestPermission' => isset($config['onPermissionRequest']),
'requestPermission' => true,
'requestUserInput' => isset($config['onUserInputRequest']),
'hooks' => $hasHooks,
'workingDirectory' => $config['workingDirectory'] ?? null,
Expand Down Expand Up @@ -328,7 +328,7 @@ public function resumeSession(string $sessionId, ResumeSessionConfig|array $conf
'reasoningEffort' => $config['reasoningEffort'] ?? null,
'tools' => $toolsForRequest ?: null,
'provider' => $config['provider'] ?? null,
'requestPermission' => isset($config['onPermissionRequest']),
'requestPermission' => true,
'requestUserInput' => isset($config['onUserInputRequest']),
'hooks' => $hasHooks,
'workingDirectory' => $config['workingDirectory'] ?? null,
Expand Down
23 changes: 23 additions & 0 deletions src/Support/PermissionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Revolution\Copilot\Support;

use Closure;

/**
* Provides pre-built permission request handlers.
*/
final readonly class PermissionHandler
{
/**
* A handler that approves all permission requests.
*
* @return Closure(array, array): array
*/
public static function approveAll(): Closure
{
return fn (array $request, array $invocation): array => PermissionRequestKind::approved();
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Support/PermissionHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Revolution\Copilot\Support\PermissionHandler;

describe('PermissionHandler', function () {
it('approveAll returns a closure', function () {
$handler = PermissionHandler::approveAll();

expect($handler)->toBeInstanceOf(Closure::class);
});

it('approveAll closure returns approved kind', function () {
$handler = PermissionHandler::approveAll();
$result = $handler(['kind' => 'shell'], ['sessionId' => 'test-session']);

expect($result)->toBe(['kind' => 'approved']);
});
});