Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

fix(computer-use): accept keys / ui_action calls padded with placeholder fields

leen.zhou8f6a7e49de0e
diff --git a/packages/agent/src/extensions/pibot-tools.ts b/packages/agent/src/extensions/pibot-tools.ts
index ad7a3b7..6673c77 100644
--- a/packages/agent/src/extensions/pibot-tools.ts
+++ b/packages/agent/src/extensions/pibot-tools.ts
@@ -1,5 +1,5 @@
import type { ExtensionFactory } from "@earendil-works/pi-coding-agent";
-import { normalizeTodoPauseInput } from "@pibot/protocol-base";
+import { normalizeComputerInput, normalizeTodoPauseInput } from "@pibot/protocol-base";
import { Type } from "typebox";
import { PARALLEL_READS } from "../core/tool-effects.ts";
import type { LocalToolPorts } from "../core/types.ts";
@@ -54,12 +54,19 @@ export function toolsExtension(
prepareArguments: (args: unknown) =>
normalizeTodoPauseInput(args) as Record<string, unknown>,
}
- : tool.jsonTextArguments?.length
+ : tool.name === "keys" || tool.name === "ui_action"
? {
+ // Placeholder fields from strict-mode providers; authorization and the permit
+ // below both see the normalised call.
prepareArguments: (args: unknown) =>
- parseJsonTextArguments(args, tool.jsonTextArguments ?? []),
+ normalizeComputerInput(tool.name, args) as Record<string, unknown>,
}
- : {}),
+ : tool.jsonTextArguments?.length
+ ? {
+ prepareArguments: (args: unknown) =>
+ parseJsonTextArguments(args, tool.jsonTextArguments ?? []),
+ }
+ : {}),
async execute(id, input, sdkSignal, update, ctx) {
const signal = sdkSignal ? AbortSignal.any([sdkSignal, ports.signal]) : ports.signal;
const permit = permits.get(id);
diff --git a/packages/brain/src/runtime/tools/tool-input-issues.ts b/packages/brain/src/runtime/tools/tool-input-issues.ts
index b52acba..25ff4bd 100644
--- a/packages/brain/src/runtime/tools/tool-input-issues.ts
+++ b/packages/brain/src/runtime/tools/tool-input-issues.ts
@@ -141,7 +141,7 @@ function baseIssueText(issue: z.core.$ZodIssue): string {
"Choose ref or screenshotId/x/y":
"只用一种方式:要么 ref(来自最新的 browser_snapshot),要么 screenshotId 加 x、y(来自最新的 browser_screenshot),不要同时给 / Use one target: either ref from the latest browser_snapshot, or screenshotId with x and y from the latest browser_screenshot, not both",
keys_text_and_combo:
- "text 和 combo 只能给一个:text 输入文字,combo 按快捷键;先输入再按键就分两次调用 keys / Give text or combo, not both: text types characters, combo presses a shortcut; to type and then press keys, make two keys calls",
+ 'text 和 combo 只能给一个:text 输入文字,combo 按快捷键;快捷键里的字母也放进 combo(如 ["cmd","shift","g"]);先输入再按键就分两次调用 keys / Give text or combo, not both: text types characters, combo presses a shortcut, and a shortcut\'s letter goes in combo too (e.g. ["cmd","shift","g"]); to type and then press keys, make two keys calls',
keys_text_or_combo_required:
'需要 text(要输入的文字)或 combo(快捷键,如 ["cmd","s"])其中一个 / Provide exactly one of text (characters to type) or combo (a shortcut, e.g. ["cmd","s"])',
ui_action_batch_actions_required:
diff --git a/packages/brain/src/runtime/tools/tools.ts b/packages/brain/src/runtime/tools/tools.ts
index 031880a..f75d8fc 100644
--- a/packages/brain/src/runtime/tools/tools.ts
+++ b/packages/brain/src/runtime/tools/tools.ts
@@ -18,6 +18,7 @@ import {
MemoryNoteSchema,
MemorySchema,
MemoryScopeSchema,
+ normalizeComputerInput,
normalizeTodoPauseInput,
PersonalTodoToolDeclarationSchema,
PersonalTodoToolSchema,
@@ -513,7 +514,10 @@ export function parseTool(call: LocalToolCall, registry: CapabilityToolRegistry)
)
throw new Error("Office input exceeds bounded content limit");
const tool = call.tool as keyof typeof schemas;
- const input = tool === "todo_pause" ? normalizeTodoPauseInput(call.input) : call.input;
+ const input =
+ tool === "todo_pause"
+ ? normalizeTodoPauseInput(call.input)
+ : normalizeComputerInput(tool, call.input);
return { tool, args: kernelSchema(tool, registry)?.parse(input) } as ToolInput;
}
/**
@@ -566,7 +570,11 @@ export function toolInputFailure(
};
const parsed = validationError
? { success: false as const, error: validationError }
- : schema.safeParse(tool === "todo_pause" ? normalizeTodoPauseInput(input) : input);
+ : schema.safeParse(
+ tool === "todo_pause"
+ ? normalizeTodoPauseInput(input)
+ : normalizeComputerInput(tool, input),
+ );
if (parsed.success) return undefined;
// Arguments that arrive as {} for a tool with required fields usually mean the reply hit the
// output-token limit mid-call. Size the recovery chunk from the actual model budget instead of
diff --git a/packages/protocol-base/src/computer/computer-input.ts b/packages/protocol-base/src/computer/computer-input.ts
new file mode 100644
index 0000000..4f4bbb1
--- /dev/null
+++ b/packages/protocol-base/src/computer/computer-input.ts
@@ -0,0 +1,76 @@
+// Strict-mode providers make models fill every optional field (2026-11-21, gpt-6.1-sol: keys with
+// text " " next to combo; ui_action "select" carrying a made-up path and a copy of itself in
+// actions). Every such call failed validation, so computer use was unusable. Before validation,
+// fields that carry nothing are dropped: null, blank strings, empty arrays, and for ui_action the
+// fields the named `action` never reads. `action` is required and says which form the call takes,
+// so nothing is guessed; the strict schema still checks the result, policy sees the normalised
+// call, and two meaningful values for one choice (keys text + combo) still fail.
+
+type Args = Record<string, unknown>;
+
+const empty = (value: unknown) =>
+ value === null ||
+ (typeof value === "string" && !value.trim()) ||
+ (Array.isArray(value) && value.length === 0);
+
+const absent = (value: unknown) => typeof value === "string" && !value.trim();
+
+function without(args: Args, drop: (key: string, value: unknown) => boolean): Args {
+ let changed = false;
+ const kept: Args = {};
+ for (const [key, value] of Object.entries(args)) {
+ if (drop(key, value)) changed = true;
+ else kept[key] = value;
+ }
+ return changed ? kept : args;
+}
+
+/** A blank value means nothing except to setValue, where "" clears the field. */
+function elementStep(step: unknown): unknown {
+ if (!step || typeof step !== "object" || Array.isArray(step)) return step;
+ const action = (step as Args).action;
+ return without(
+ step as Args,
+ (key, value) => value === null || (key === "value" && action !== "setValue" && absent(value)),
+ );
+}
+
+const UI_ACTION_FIELDS: Record<"batch" | "menu" | "element", ReadonlySet<string>> = {
+ batch: new Set(["elementId", "path", "value"]),
+ menu: new Set(["elementId", "value", "actions"]),
+ element: new Set(["path", "actions"]),
+};
+
+function normalizeUiAction(args: Args): Args {
+ const action = args.action;
+ if (typeof action !== "string") return args;
+ const form = action === "batch" ? "batch" : action === "menu" ? "menu" : "element";
+ const unused = UI_ACTION_FIELDS[form];
+ const trimmed = without(args, (key, value) => {
+ if (unused.has(key)) return true;
+ // Only element actions get this far with a value (the other forms never read it).
+ if (key === "value") return value === null || (action !== "setValue" && absent(value));
+ return key !== "app" && key !== "action" && empty(value);
+ });
+ return form === "batch" && Array.isArray(trimmed.actions)
+ ? { ...trimmed, actions: trimmed.actions.map(elementStep) }
+ : trimmed;
+}
+
+function normalizeKeys(args: Args): Args {
+ const combo = Array.isArray(args.combo) && args.combo.length > 0;
+ return without(args, (key, value) => {
+ if (key === "app") return false;
+ // Whitespace typed next to a shortcut is filler; on its own it is a real space to type.
+ if (key === "text") return value === null || value === "" || (combo && !String(value).trim());
+ if (key === "combo") return value === null || (Array.isArray(value) && !value.length);
+ return empty(value);
+ });
+}
+
+/** Drops placeholder fields from keys / ui_action arguments; other tools pass through untouched. */
+export function normalizeComputerInput(tool: string, input: unknown): unknown {
+ if (tool !== "keys" && tool !== "ui_action") return input;
+ if (!input || typeof input !== "object" || Array.isArray(input)) return input;
+ return tool === "keys" ? normalizeKeys(input as Args) : normalizeUiAction(input as Args);
+}
diff --git a/packages/protocol-base/src/index.ts b/packages/protocol-base/src/index.ts
index 4191339..94ff713 100644
--- a/packages/protocol-base/src/index.ts
+++ b/packages/protocol-base/src/index.ts
@@ -43,6 +43,7 @@ export * from "./chat/topics.ts";
export * from "./chat/user-question.ts";
export * from "./computer/apps.ts";
export * from "./computer/computer.ts";
+export * from "./computer/computer-input.ts";
export * from "./computer/host.ts";
export * from "./computer/host-guard.ts";
export * from "./connectors/apps.ts";