Skip to content

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

fix(openai-chat): replay blank tool-call arguments as {}

AdityaUmale83429990db12
diff --git a/server/drivers/openai-chat-protocol.ts b/server/drivers/openai-chat-protocol.ts
index 070a2d916e..c87cd9eaca 100644
--- a/server/drivers/openai-chat-protocol.ts
+++ b/server/drivers/openai-chat-protocol.ts
@@ -34,13 +34,17 @@ export function toolArguments(text: string): Record<string, unknown> {
return found;
}
-/** The calls as the next request replays them. Arguments `toolArguments` refuses never ran (the call's result says
- * so) and go back as `{}`: a strict server refuses the whole request over them ("function.arguments must be valid JSON"). */
+/** 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 `{}`. */
export function replayableToolCalls(calls: ChatToolCall[]): ChatToolCall[] {
- return calls.map((call) => {
- try { toolArguments(call.function.arguments); return call; }
- catch { return { ...call, function: { ...call.function, arguments: "{}" } }; }
- });
+ return calls.map((call) => replaysAsSent(call.function.arguments) ? call
+ : { ...call, function: { ...call.function, arguments: "{}" } });
+}
+
+function replaysAsSent(text: string): boolean {
+ if (!text.trim()) return false;
+ try { toolArguments(text); return true; } catch { return false; }
}
/** 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 be98b5fc01..7c36c8a174 100644
--- a/server/drivers/openai-chat.test.ts
+++ b/server/drivers/openai-chat.test.ts
@@ -435,11 +435,15 @@ describe("createOpenAIChatRuntime tool approvals", () => {
}, 20_000);
});
- it("replays a call whose arguments are not JSON as an empty object, so a strict provider takes the next request", async () => {
+ 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) => {
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';
- const replies = [call("c1", '{"path":"a.md","content":"cut off'), call("c2", "{}"),
+ const replies = [call("c1", badArguments), call("c2", "{}"),
'data: {"choices":[{"index":0,"delta":{"content":"Written."}}]}\n\ndata: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n'];
const bodies: any[] = [];
vi.stubGlobal("fetch", vi.fn(async (input: unknown, init?: RequestInit) => {
@@ -466,8 +470,10 @@ 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.role === "tool" && message.tool_call_id === "c1")?.content)
- .toContain("tool arguments are not complete JSON");
+ if (toolError) {
+ expect(bodies[1].messages.find((message: any) => message.role === "tool" && message.tool_call_id === "c1")?.content)
+ .toContain(toolError);
+ }
expect((events.find((event) => event.type === "turn.completed") as any).ok).toBe(true);
} finally {
await instance.dispose();