RPC 扩展 UI
扩展可以通过 $ctx->ui 请求用户交互。在 RPC 模式下,支持的调用会变成一个与常规 RPC 命令和会话事件并行的请求/响应子协议。
扩展 UI 方法分为两类:
- 对话框方法(
select、confirm、input、editor):在 stdout 上发出extension_ui_request,并阻塞等待客户端在 stdin 上发回带有匹配id的extension_ui_response。 - 即发即弃方法(
notify、setStatus、setWidget、setTitle、set_editor_text):在 stdout 上发出extension_ui_request,但不期望收到响应。客户端可以显示该信息或将其忽略。
每个请求都以一个新的 UUID 作为其 id。如果对话框方法包含 timeout 字段,当超时时间到达时,agent 端将使用默认值自动解析。客户端无需跟踪超时。pig 撤回对话框时(例如超时后)不会发送取消请求;迟到的响应会被忽略。
限制
#某些 HookUi 方法(RPC 模式下为 Pig\CodingAgent\Rpc\RpcUi)不受支持或会降级,因为它们需要直接访问终端 UI:
custom()返回null。onTerminalInput()返回一个空操作(no-op)的退订闭包。setWorkingMessage()、setWorkingVisible()、setWorkingIndicator()、setHiddenThinkingLabel()、setFooter()、setHeader()、addAutocompleteProvider()、setEditorComponent()和setToolsExpanded()为空操作(no-op)。getEditorText()返回'',getEditorComponent()返回null。getToolsExpanded()返回false。pasteToEditor()委托给setEditorText(),不包含终端粘贴处理。getAllThemes()返回[],getTheme()返回null(宿主未声明主题时)。当宿主通过set_theme声明主题时(如 Web UI),返回可用主题文件列表。setTheme()默认返回['success' => false, 'error' => 'Theme switching not supported in RPC mode']。当宿主通过set_theme声明主题时,支持按名称切换主题,并发送setTheme请求通知宿主同步更新。
注意:在 RPC 模式下,$ctx->mode() 为 'rpc' 且 $ctx->hasUi 为 true,因为对话框和即发即弃方法通过扩展 UI 子协议保持可用。在使用需要真实终端的 TUI 专用功能(例如 custom())之前,请先检查 $ctx->mode()。
来自 pig 的请求
#所有请求均包含 type: "extension_ui_request"、唯一的 id 以及 method 字段。
select
#提示用户从列表中进行选择。带有 timeout 字段的对话框方法包含以毫秒为单位的超时时间;如果客户端未及时响应,agent 将自动解析为 null。
{
"type": "extension_ui_request",
"id": "uuid-1",
"method": "select",
"title": "Allow dangerous command?",
"options": ["Allow", "Block"],
"timeout": 10000
}
预期响应:包含 value(所选选项字符串)或 cancelled: true 的 extension_ui_response。
confirm
#提示用户进行是/否确认。
{
"type": "extension_ui_request",
"id": "uuid-2",
"method": "confirm",
"title": "Clear session?",
"message": "All messages will be lost.",
"timeout": 5000
}
预期响应:包含 confirmed: true/false 或 cancelled: true 的 extension_ui_response。
input
#提示用户输入自由格式文本。
{
"type": "extension_ui_request",
"id": "uuid-3",
"method": "input",
"title": "Enter a value",
"placeholder": "type something..."
}
预期响应:包含 value(输入的文本)或 cancelled: true 的 extension_ui_response。
editor
#打开多行文本编辑器,可带有可选的预填充内容。
{
"type": "extension_ui_request",
"id": "uuid-4",
"method": "editor",
"title": "Edit some text",
"prefill": "Line 1\nLine 2\nLine 3"
}
预期响应:包含 value(编辑后的文本)或 cancelled: true 的 extension_ui_response。
notify
#显示通知。即发即弃,不期望收到响应。
{
"type": "extension_ui_request",
"id": "uuid-5",
"method": "notify",
"message": "Command blocked by user",
"notifyType": "warning"
}
notifyType 字段为 "info"、"warning" 或 "error"。如果省略,则默认为 "info"。
setStatus
#在页脚/状态栏中设置或清除状态条目。即发即弃。
{
"type": "extension_ui_request",
"id": "uuid-6",
"method": "setStatus",
"statusKey": "my-ext",
"statusText": "Turn 3 running..."
}
不带 statusText 的请求会清除该键对应的状态条目。
setWidget
#设置或清除显示在编辑器上方或下方的小部件(多行文本块)。即发即弃。
{
"type": "extension_ui_request",
"id": "uuid-7",
"method": "setWidget",
"widgetKey": "my-ext",
"widgetLines": ["--- My Widget ---", "Line 1", "Line 2"],
"widgetPlacement": "aboveEditor"
}
不带 widgetLines 的请求会清除小部件。widgetPlacement 字段为 "aboveEditor"(默认值)或 "belowEditor";扩展中的 ['placement' => 'above'] 或 'below' 会映射为这两个值。在 RPC 模式下仅支持字符串数组;组件工厂函数将被忽略。
setTitle
#设置终端窗口/标签页标题。即发即弃。
{
"type": "extension_ui_request",
"id": "uuid-8",
"method": "setTitle",
"title": "pig - my project"
}
set_editor_text
#设置输入编辑器中的文本。即发即弃。setEditorText() 和 pasteToEditor() 都会发送它。
{
"type": "extension_ui_request",
"id": "uuid-9",
"method": "set_editor_text",
"text": "prefilled text for the user"
}
setTheme
#通知宿主切换渲染主题(当扩展调用 $ctx->ui->setTheme() 时发出)。即发即弃。
{
"type": "extension_ui_request",
"id": "uuid-10",
"method": "setTheme",
"theme": "light"
}
发送给 pig 的响应
#响应仅针对对话框方法(select、confirm、input、editor)发送。id 必须与请求相匹配。
值响应(select、input、editor)
#{"type": "extension_ui_response", "id": "uuid-1", "value": "Allow"}
确认响应(confirm)
#{"type": "extension_ui_response", "id": "uuid-2", "confirmed": true}
取消响应(任意对话框)
#关闭任意对话框方法。扩展将收到 null(对于 select/input/editor)或 false(对于 confirm)。
{"type": "extension_ui_response", "id": "uuid-3", "cancelled": true}
示例
#一个在执行破坏性命令前进行询问的扩展,在终端和 RPC 中的行为完全相同:
$pig->on('tool_call', function ($event, $ctx) {
if ($event->toolName === 'bash' && str_contains($event->input['command'] ?? '', 'rm -rf')
&& !$ctx->ui->confirm('Allow?', $event->input['command'], timeout: 30000)) {
return new ToolCallEventResult(block: true, reason: 'Blocked by user');
}
return null;
});
在 RPC 中,confirm() 调用会变成一个 confirm 请求;客户端以 confirmed 作答,超时或 cancelled: true 都会拦截该命令。
请求在 RpcUi.php 中构建,响应在 RpcMode.php 中分发。有关与模式无关的扩展指南,请参见扩展。