Skip to content
Closed
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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ The four numbered files above are the spine. These carry the rest of the behavio

**Response out — parsing**

- `proxy/toolCallNormalizer.mjs` — the largest module in the bridge: turns model prose back into OpenAI `tool_calls`. It parses only _unfenced_ XML so that code fences inside a `content` parameter survive intact, and `chooseAssistantResponse` falls back through recorder tool-call parts and alternate assistant texts. Anyone touching model output lands here.
- `proxy/toolCallNormalizer.mjs` — the largest module in the bridge: turns model prose back into OpenAI `tool_calls`. Only two transport shapes count: XML `function_calls`/`invoke` markup and JSON objects whose `type` is `tool_call`. Calls inside Markdown code fences or inline code are examples and never run, while fences inside a parameter value survive intact. Callers pass the request's `tools`: XML values are typed by each tool's schema, so string parameters keep their exact bytes, and JSON calls must name a listed tool. A truncated XML batch throws `o1_code_incomplete_tool_call` instead of running its prefix. `chooseAssistantResponse` falls back through recorder tool-call parts and alternate assistant texts, parsing each one separately. Anyone touching model output lands here.
- `proxy/anthropicToolFormat.mjs` / `jsonToolFormat.mjs` — render prior tool calls back out in the format the agent itself emits, so a continued thread reads its own history. Selected by `toolFormat`.
- `proxy/streamGate.mjs` — decides from cumulative text whether a turn is prose (stream it live) or a tool call (suppress until normalized). Buffers the first `DECISION_THRESHOLD` (24) non-whitespace characters, holds back a trailing fragment that could be a marker split across frames, and flips to suppressed on a mid-stream marker. This is why prose starts a beat late; do not "fix" it by streaming raw deltas, or a tool-calling turn will spray XML into the UI and then be retroactively replaced.
- `proxy/streamAdapter.mjs` — SSE chunk shaping for the streamed OpenAI response.
Expand Down
7 changes: 4 additions & 3 deletions packages/desktop/src/main/wpp-bridge/proxy/jsonToolFormat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { parseToolArguments } from "./toolArguments.mjs";
// Render an OpenAI tool_call back into the JSON protocol the GPT/"o1-code-builder" agent emits,
// so prior tool calls replayed into the transcript match the format the model is asked to produce
// (see TOOL_CALL_SYSTEM_REMINDER_JSON). The proxy parses this same shape back out in
// src/toolCallNormalizer.mjs (parseTrailingToolCallJsons / parseLeadingToolCallJson).
// toolCallNormalizer.mjs, keeping the call id so a replayed call keeps its identity.
//
// Mirrors src/anthropicToolFormat.mjs's formatAnthropicToolCall, but for JSON instead of XML.
// Mirrors anthropicToolFormat.mjs's formatAnthropicToolCall, but for JSON instead of XML.
export function formatJsonToolCall(toolCall) {
const fn = toolCall?.function || toolCall;
const name = fn?.name || "unknown";
const id = toolCall?.id || fn?.id || "";
const args = parseToolArguments(fn?.arguments);

return JSON.stringify({ type: "tool_call", tool: name, args });
return JSON.stringify(id ? { type: "tool_call", id, tool: name, args } : { type: "tool_call", tool: name, args });
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe("CookieMonster request envelope", () => {
test("keeps the established Opus envelope byte-identical", () => {
const raw = serializeChatCompletionRequest(framed())
expect(new Bun.CryptoHasher("sha256").update(raw).digest("hex")).toBe(
"87f727b61b18d52b89403822ca36d1bc8dbe199c97ca91b32780c20cb66ba771",
"6e2449b2652abfb891eba2713a61d0be7a98318b02a6f2aac4ec2581de2468a9",
)
})

Expand Down
6 changes: 3 additions & 3 deletions packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ export async function handleChatCompletions(request, response, body, { bridge =
tool_calls: undefined,
finish_reason: "stop"
}
: stripTaskCompleteMarker(chooseAssistantResponse(finalText, toolCallParts, o1CodeRun.response.alternateAssistantTexts));
: stripTaskCompleteMarker(chooseAssistantResponse(finalText, toolCallParts, o1CodeRun.response.alternateAssistantTexts, { tools: body.tools }));
if (!isCompaction) {
normalizeToolCallArguments(normalized.tool_calls, body.tools);
}
Expand Down Expand Up @@ -643,7 +643,7 @@ export function shouldRecoverMissingRequiredToolCall(body, run, { isCompaction =

const finalText = run?.response?.finalText || run?.finalText || "";
const toolCallParts = run?.response?.toolCallParts || run?.toolCallParts || {};
const normalized = chooseAssistantResponse(finalText, toolCallParts, alternateAssistantTexts(run));
const normalized = chooseAssistantResponse(finalText, toolCallParts, alternateAssistantTexts(run), { tools: body.tools });
if (normalized?.tool_calls?.length) return false;
if (!activeTurnHasToolProgress(body)) return true;
if (!commentaryPhaseEnabled(body)) return false;
Expand All @@ -662,7 +662,7 @@ export function shouldRecoverIncompleteTask(body, run, { isCompaction = false }
if (isCompaction || !Array.isArray(body?.tools) || body.tools.length === 0) return false;
const finalText = run?.response?.finalText || run?.finalText || "";
const toolCallParts = run?.response?.toolCallParts || run?.toolCallParts || {};
const normalized = chooseAssistantResponse(finalText, toolCallParts, alternateAssistantTexts(run));
const normalized = chooseAssistantResponse(finalText, toolCallParts, alternateAssistantTexts(run), { tools: body.tools });
if (normalized?.tool_calls?.length) return false;
const content = normalized?.content || "";
return !hasTaskCompleteMarker(content)
Expand Down
11 changes: 5 additions & 6 deletions packages/desktop/src/main/wpp-bridge/proxy/streamGate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,11 @@ function trailingMarkerPrefixLength(text) {
return longest;
}

// True when the LEADING non-whitespace content looks like the start of a tool call. Mirrors the
// signals toolCallNormalizer.mjs keys on so the live decision matches the final normalization:
// Anthropic XML (optionally wrapped in a leading code fence) or a leading JSON object (which may
// be a {"type":"tool_call"} or delegated-agent call). A leading "{" is treated conservatively as
// a potential tool call — prose almost never starts with a brace, and if it turns out to be
// content the finalize path still emits it as a trailing diff (just not streamed live).
// True when the LEADING non-whitespace content might start a tool call. This is deliberately broader
// than toolCallNormalizer.mjs, which treats fenced markup as an example and runs only typed JSON for
// a listed tool. Here, XML markup (even inside a leading code fence) or any leading JSON object
// suppresses live output. Prose almost never starts that way, and over-suppressing only delays it:
// when the final normalization finds no call, finalizeStreamedCompletion still emits the content.
export function leadingLooksLikeToolCall(text) {
const trimmed = String(text || "").trimStart();

Expand Down
Loading
Loading