Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(openai-chat): replay trimmed tool-call arguments when only the trimmed text parses

AdityaUmale2b38bbbf4018
diff --git a/server/drivers/openai-chat-protocol.ts b/server/drivers/openai-chat-protocol.ts
index c87cd9eaca..0fae8ad30c 100644
--- a/server/drivers/openai-chat-protocol.ts
+++ b/server/drivers/openai-chat-protocol.ts
@@ -36,15 +36,19 @@ export function toolArguments(text: string): Record<string, unknown> {
/** The calls as the next request replays them. A strict server refuses the whole request over arguments that don't
* parse ("function.arguments must be valid JSON"), so blank ones (a tool with no arguments) and ones `toolArguments`
- * refuses (they never ran; the call's result says so) go back as `{}`. */
+ * refuses (they never ran; the call's result says so) go back as `{}`, and ones that ran only once trimmed (a stray
+ * byte-order mark, which isn't JSON whitespace) go back trimmed. */
export function replayableToolCalls(calls: ChatToolCall[]): ChatToolCall[] {
- return calls.map((call) => replaysAsSent(call.function.arguments) ? call
- : { ...call, function: { ...call.function, arguments: "{}" } });
+ return calls.map((call) => {
+ const replayed = replayArguments(call.function.arguments);
+ return replayed === call.function.arguments ? call : { ...call, function: { ...call.function, arguments: replayed } };
+ });
}
-function replaysAsSent(text: string): boolean {
- if (!text.trim()) return false;
- try { toolArguments(text); return true; } catch { return false; }
+function replayArguments(text: string): string {
+ const trimmed = text.trim();
+ if (!trimmed) return "{}";
+ try { toolArguments(trimmed); return trimmed; } catch { return "{}"; }
}
/** Opaque provider reasoning blocks must keep their original order/signatures.
diff --git a/server/drivers/openai-chat.test.ts b/server/drivers/openai-chat.test.ts
index 7c36c8a174..4da5960203 100644
--- a/server/drivers/openai-chat.test.ts
+++ b/server/drivers/openai-chat.test.ts
@@ -436,10 +436,11 @@ describe("createOpenAIChatRuntime tool approvals", () => {
});
it.each([
- ["not JSON", '{"path":"a.md","content":"cut off', "tool arguments are not complete JSON"],
- ["empty", "", undefined],
- ["blank", " ", undefined],
- ])("replays a call whose arguments are %s as an empty object, so a strict provider takes the next request", async (_kind, badArguments, toolError) => {
+ ["not JSON", '{"path":"a.md","content":"cut off', "{}", "tool arguments are not complete JSON"],
+ ["empty", "", "{}", undefined],
+ ["blank", " ", "{}", undefined],
+ ["led by a byte-order mark", '\uFEFF{"n":1}', '{"n":1}', undefined],
+ ])("replays a call whose arguments are %s as valid JSON, so a strict provider takes the next request", async (_kind, badArguments, replayedArguments, toolError) => {
const call = (id: string, argumentsText: string) =>
`data: ${JSON.stringify({ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id, type: "function", function: { name: "fx_write", arguments: argumentsText } }] } }] })}\n\n`
+ 'data: {"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}\n\n';
@@ -469,7 +470,7 @@ describe("createOpenAIChatRuntime tool approvals", () => {
});
await vi.waitFor(() => expect(events.some((event) => event.type === "turn.completed")).toBe(true), { timeout: 10_000 });
expect(bodies).toHaveLength(3);
- expect(bodies[1].messages.find((message: any) => message.tool_calls)?.tool_calls[0].function.arguments).toBe("{}");
+ expect(bodies[1].messages.find((message: any) => message.tool_calls)?.tool_calls[0].function.arguments).toBe(replayedArguments);
if (toolError) {
expect(bodies[1].messages.find((message: any) => message.role === "tool" && message.tool_call_id === "c1")?.content)
.toContain(toolError);