Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(openai-chat): quote rejected tool arguments back in the failed call's result
AdityaUmale667e33a8cc73
diff --git a/server/drivers/openai-chat-protocol.test.ts b/server/drivers/openai-chat-protocol.test.tsindex c0a8461e1b..3b72b158cd 100644--- a/server/drivers/openai-chat-protocol.test.ts+++ b/server/drivers/openai-chat-protocol.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vitest";-import { ChatReasoningDetails, ChatToolCalls, MAX_REASONING_DETAIL_CHARS } from "./openai-chat-protocol.ts";+import { ChatReasoningDetails, ChatToolCalls, MAX_REASONING_DETAIL_CHARS, toolArguments } from "./openai-chat-protocol.ts";const completeCall = {id: "call_echo",@@ -229,3 +229,14 @@ describe("streamed reasoning details", () => {expect(() => details.add(delta("x".repeat(MAX_REASONING_DETAIL_CHARS)))).toThrow(/exceeded the size limit/);});});++describe("rejected tool arguments", () => {+ it("quotes a long cut-off call by its start and end, so the model sees the path and where it broke off", () => {+ const text = `{"path":"notes/long.md","content":"${"x".repeat(5_000)}END`;+ let message = "";+ try { toolArguments(text); } catch (error) { message = (error as Error).message; }+ expect(message.startsWith(`tool arguments are not complete JSON; you sent ${text.length} characters: {"path":"notes/long.md"`)).toBe(true);+ expect(message.endsWith("xxEND")).toBe(true);+ expect(message.length).toBeLessThan(1_200);+ });+});diff --git a/server/drivers/openai-chat-protocol.ts b/server/drivers/openai-chat-protocol.tsindex 0fae8ad30c..b8e7448142 100644--- a/server/drivers/openai-chat-protocol.ts+++ b/server/drivers/openai-chat-protocol.ts@@ -28,12 +28,17 @@ export function object(value: unknown): Record<string, unknown> | undefined {export function toolArguments(text: string): Record<string, unknown> {let args: unknown;try { args = JSON.parse(text.trim() || "{}"); }- catch { throw new ChatProtocolError("tool arguments are not complete JSON"); }+ catch { throw new ChatProtocolError(`tool arguments are not complete JSON; you sent ${text.length} characters: ${shownArguments(text)}`); }const found = object(args);if (!found) throw new ChatProtocolError("tool arguments must be a JSON object");return found;}+/** Rejected arguments as the failed call's result quotes them back, so the model sees where they broke off. */+function shownArguments(text: string): string {+ return text.length <= 1_000 ? text : `${text.slice(0, 500)} … ${text.slice(-500)}`;+}+/** 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 `{}`, and ones that ran only once trimmed (a straydiff --git a/server/drivers/openai-chat.test.ts b/server/drivers/openai-chat.test.tsindex 4da5960203..71694825be 100644--- a/server/drivers/openai-chat.test.ts+++ b/server/drivers/openai-chat.test.ts@@ -436,7 +436,7 @@ describe("createOpenAIChatRuntime tool approvals", () => {});it.each([- ["not JSON", '{"path":"a.md","content":"cut off', "{}", "tool arguments are not complete JSON"],+ ["not JSON", '{"path":"a.md","content":"cut off', "{}", 'tool arguments are not complete JSON; you sent 33 characters: {"path":"a.md","content":"cut off'],["empty", "", "{}", undefined],["blank", " ", "{}", undefined],["led by a byte-order mark", '\uFEFF{"n":1}', '{"n":1}', undefined],@@ -472,8 +472,8 @@ describe("createOpenAIChatRuntime tool approvals", () => {expect(bodies).toHaveLength(3);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);+ expect(JSON.parse(bodies[1].messages.find((message: any) => message.role === "tool" && message.tool_call_id === "c1")?.content).result)+ .toBe(toolError);}expect((events.find((event) => event.type === "turn.completed") as any).ok).toBe(true);} finally {