Skip to content

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

Carry a JSON reply's held suffix and replay a cut Responses message

erywim918aa2c750ed
diff --git a/server/drivers/openai-chat.test.ts b/server/drivers/openai-chat.test.ts
index 7e34ff0312..6efc693092 100644
--- a/server/drivers/openai-chat.test.ts
+++ b/server/drivers/openai-chat.test.ts
@@ -1166,6 +1166,10 @@ describe("createOpenAIChatRuntime output-limit continuation", () => {
`data: ${JSON.stringify({ choices: [{ index: 0, delta }] })}\n\n`
+ `data: ${JSON.stringify({ choices: [{ index: 0, delta: {}, finish_reason: finishReason }], usage: { prompt_tokens: 10, completion_tokens: 4 } })}\n\n`;
const text = (value: string, finishReason: string) => stream({ content: value }, finishReason);
+ const json = (value: string, finishReason: string) => new Response(JSON.stringify({
+ choices: [{ index: 0, message: { role: "assistant", content: value }, finish_reason: finishReason }],
+ usage: { prompt_tokens: 10, completion_tokens: 4 },
+ }), { headers: { "content-type": "application/json" } });
const textOf = (events: RuntimeEvent[]) =>
events.filter((event) => event.type === "item.completed" && event.itemType === "assistant_text").map((event) => event.text);
@@ -1202,6 +1206,24 @@ describe("createOpenAIChatRuntime output-limit continuation", () => {
expect(shown.join("")).not.toContain("12345678");
});
+ it("carries the held suffix of a reply that arrived as JSON, not a stream", async () => {
+ let requests = 0;
+ const events = await runTurn(
+ () => ++requests === 1 ? json("This is", "length") : json(" complete.", "stop"),
+ "openai-compat",
+ { MINIMAX_API_KEY: "secret12345678" },
+ );
+ expect(requests).toBe(2);
+ expect(textOf(events)).toEqual(["This is complete."]);
+ const bodies = vi.mocked(fetch).mock.calls
+ .filter(([url]) => String(url).endsWith("/chat/completions"))
+ .map(([, init]) => JSON.parse(String(init?.body)) as { messages: Array<{ role: string; content: unknown }> });
+ expect(bodies[1]!.messages.slice(-2)).toEqual([
+ { role: "assistant", content: "This is" },
+ { role: "user", content: LENGTH_CONTINUATION },
+ ]);
+ });
+
it("still fails a length finish with no text", async () => {
let requests = 0;
const events = await runTurn(() => (requests++, sse(text("", "length"))));
diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.ts
index b3153d2d57..097956505d 100644
--- a/server/drivers/openai-chat.ts
+++ b/server/drivers/openai-chat.ts
@@ -675,7 +675,7 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)
// A continuation resumes the same text, so a held credential prefix crosses over instead of being shown.
delta("", "assistant_text", !willContinue);
delta("", "reasoning_text", true);
- carried = willContinue ? pending.assistant_text : "";
+ carried = willContinue ? maskSecrets(prefix + completion.text, false).held : "";
break;
} catch (value) {
const error = asError(value);
diff --git a/server/drivers/openai-responses.test.ts b/server/drivers/openai-responses.test.ts
index dc2c01b481..45399443fb 100644
--- a/server/drivers/openai-responses.test.ts
+++ b/server/drivers/openai-responses.test.ts
@@ -473,6 +473,30 @@ describe("what Responses can answer", () => {
expect(textOf(seen, "assistant_text")).toBe("Half and half.");
});
+ it("replays a cut message when only its reasoning item finished", async () => {
+ let responses = 0;
+ const f = await fixture(onResponses((response) => {
+ responses += 1;
+ if (responses === 1) {
+ const reasoning = reasoningItem();
+ events(response, [
+ created,
+ { type: "response.reasoning_summary_text.delta", item_id: "rs_1", output_index: 0, summary_index: 0, delta: "Thinking it over." },
+ done(reasoning, 0),
+ textDelta("Half"),
+ { type: "response.incomplete", response: { status: "incomplete", incomplete_details: { reason: "max_output_tokens" }, output: [reasoning, messageItem("Half")], usage } },
+ ]);
+ return;
+ }
+ answer(response, " and half.");
+ }));
+ const { end, events: seen } = await f.turn();
+ expect(end).toMatchObject({ ok: true });
+ const continuation = f.calls.at(-1)!;
+ expect(continuation.body.input.filter((item: any) => item.type === "message" && item.content?.[0]?.text === "Half")).toHaveLength(1);
+ expect(textOf(seen, "assistant_text")).toBe("Half and half.");
+ });
+
it("refuses a tool call cut off by an incomplete response without running it", async () => {
const f = await fixture(onResponses((response) => {
events(response, [done(callItem()), { type: "response.incomplete", response: { status: "incomplete", incomplete_details: { reason: "max_output_tokens" }, output: [callItem()] } }]);
diff --git a/server/drivers/openai-responses.ts b/server/drivers/openai-responses.ts
index ac6cc56107..8e5f1049d0 100644
--- a/server/drivers/openai-responses.ts
+++ b/server/drivers/openai-responses.ts
@@ -197,8 +197,8 @@ export async function readResponsesStream(body: ReadableStream<Uint8Array> | nul
}
};
const finish = (response: Record<string, unknown> | undefined) => {
- if (!done.size && Array.isArray(response?.output)) {
- for (const [position, item] of response.output.entries()) if (object(item)) finishItem(item as ResponseItem, position);
+ if (Array.isArray(response?.output)) {
+ for (const [position, item] of response.output.entries()) if (object(item) && !done.has(position)) finishItem(item as ResponseItem, position);
}
const used = object(response?.usage);
const cachedInput = object(used?.input_tokens_details)?.cached_tokens;