Skip to content

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

Show a released prefix in the item exactly as the deltas showed it

erywim7a9e1e9f0aa2
diff --git a/server/drivers/cut-reply-text.ts b/server/drivers/cut-reply-text.ts
index 05a6a82a51..8f51a01b22 100644
--- a/server/drivers/cut-reply-text.ts
+++ b/server/drivers/cut-reply-text.ts
@@ -23,7 +23,6 @@ export class MaskedDeltas {
this.emit = emit;
}
seed(kind: StreamKind, text: string) { this.held[kind] = text; }
- heldText(kind: StreamKind) { return this.held[kind]; }
feed(text: string, kind: StreamKind, flush = false) {
const masked = maskSecretHalves(this.held[kind] + text, this.secrets, this.safe, flush);
this.held[kind] = masked.held;
diff --git a/server/drivers/openai-chat.test.ts b/server/drivers/openai-chat.test.ts
index 7215201baa..49c3282a1b 100644
--- a/server/drivers/openai-chat.test.ts
+++ b/server/drivers/openai-chat.test.ts
@@ -1261,6 +1261,22 @@ describe("createOpenAIChatRuntime output-limit continuation", () => {
expect(shown).toContain("before");
});
+ it("keeps a released prefix in both the deltas and the item when a tool call follows", async () => {
+ let requests = 0;
+ const events = await runTurn(
+ () => ++requests === 1
+ ? sse(text("before abcdefgh", "length"))
+ : requests === 2
+ ? sse(two({ content: "Let me check." }, { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "no_such_tool", arguments: "{}" } }] }, "tool_calls"))
+ : new Response(JSON.stringify({ error: { message: "bad request" } }), { status: 400, headers: { "content-type": "application/json" } }),
+ "openai-compat",
+ { MINIMAX_API_KEY: "abcdefgh12345678" },
+ );
+ const deltas = events.flatMap((event) => event.type === "content.delta" && event.streamKind === "assistant_text" ? [event.delta] : []).join("");
+ expect(textOf(events)).toEqual(["before abcdefghLet me check."]);
+ expect(deltas).toBe("before abcdefghLet me check.");
+ });
+
it("keeps the partial answer when the continuation request fails", async () => {
let requests = 0;
const events = await runTurn(() => ++requests === 1
diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.ts
index 4d1e1aec7a..411823d0cd 100644
--- a/server/drivers/openai-chat.ts
+++ b/server/drivers/openai-chat.ts
@@ -670,10 +670,10 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)
}
willContinue = !completion.toolCalls.length
&& cutReplyWillContinue(cutReplies, completion, Boolean(options.localModel && tools.definitions.length));
- // A held prefix that no continuation will complete is dropped, not shown as half a credential.
- if (prefix && (!completion.text.trim() || completion.toolCalls.length > 0)) { prefix = ""; deltas.seed("assistant_text", ""); }
- delta("", "assistant_text", !willContinue);
- delta("", "reasoning_text", !willContinue);
+ // Held text is flushed only when the turn's answer ends, so a credential prefix is never shown in halves.
+ const flushHeld = !willContinue && !completion.toolCalls.length;
+ delta("", "assistant_text", flushHeld);
+ delta("", "reasoning_text", flushHeld);
carried = willContinue ? maskSecrets(prefix + completion.text, false).held : ""; carriedReasoning = willContinue ? maskSecrets(carriedReasoning + completion.reasoning, false).held : "";
break;
} catch (value) {
@@ -742,7 +742,7 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)
}
// Reasoning on a tool-call round belongs to the protocol, not a final reply.
const reply = completion.text.trim() ? completion.text : completion.toolCalls.length || unreadable ? "" : completion.reasoning;
- const shownReply = prefix || willContinue ? maskSecrets(prefix + completion.text, !willContinue).visible : safeText(reply);
+ const shownReply = prefix || willContinue || completion.text.trim() ? maskSecrets(prefix + completion.text, !willContinue && !completion.toolCalls.length).visible : safeText(reply);
if (reply.trim()) { spoke = true; answer.add(shownReply); }
if (!willContinue) answer.end();
abort.signal.throwIfAborted();