Skip to content

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

fix(openai-chat): count a repeated thought signature once toward the size limit

AdityaUmale3f660d1d2959
diff --git a/server/drivers/openai-chat-protocol.test.ts b/server/drivers/openai-chat-protocol.test.ts
index 0263147d5f..dba7f272af 100644
--- a/server/drivers/openai-chat-protocol.test.ts
+++ b/server/drivers/openai-chat-protocol.test.ts
@@ -81,6 +81,15 @@ describe("streamed tool calls without an index (Gemini's OpenAI-compatible endpo
expect(calls.finish("stop", false)).toEqual([completeCall]);
});
+ it("counts a signature repeated on every chunk once toward the size limit", () => {
+ const calls = new ChatToolCalls();
+ const extra_content = { google: { thought_signature: "s".repeat(100_000) } };
+ calls.add([{ index: 0, ...completeCall, extra_content }], true);
+ calls.add([{ index: 0, extra_content }], true);
+ calls.add([{ index: 0, extra_content }], true);
+ expect(calls.finish("stop", false)).toEqual([{ ...completeCall, extra_content }]);
+ });
+
const whole = (id: string, name: string, args = "{}") => ({ id, type: "function", function: { name, arguments: args } });
it("keeps two whole calls sent in one chunk", () => {
diff --git a/server/drivers/openai-chat-protocol.ts b/server/drivers/openai-chat-protocol.ts
index beb2eb058a..c3bee0245c 100644
--- a/server/drivers/openai-chat-protocol.ts
+++ b/server/drivers/openai-chat-protocol.ts
@@ -71,8 +71,10 @@ export class ChatToolCalls {
}
const extra = object(delta.extra_content);
if (extra) {
+ // A later chunk replaces the earlier value, so count only the size change.
+ const previous = call.extra_content ? JSON.stringify(call.extra_content).length : 0;
call.extra_content = extra;
- this.argumentChars += JSON.stringify(extra).length;
+ this.argumentChars += JSON.stringify(extra).length - previous;
}
if (call.function.name.length > 64 || this.argumentChars > MAX_ARGUMENT_CHARS) {
throw new ChatProtocolError("provider tool calls exceeded the size limit");