Files
yetone ff2d815249 commandcode 0.1.11: a streamed Go reply Command Code stops before its finish line ends cut short, with a note, instead of failing the turn (#91)
This is the sibling of zen-free 0.1.4 (6a5dd01). /alpha/generate's reply
is turned into a Chat Completions stream, which magpie's gateway can't go
on with, so the error frame threw away what the client was already shown.

Now a streamed reply that said something (text, reasoning or a tool call)
and then lost its finish line ends with the note, a finish (`stop`, or
`tool_calls` after a call) and [DONE]. Tool calls arrive whole here, so
none can be half written. A reply that said nothing, or one that wasn't
streamed, is still a 502, so magpie retries it.

Run: HOME=$(mktemp -d) bun test packages/commandcode gave 37 pass. The
whole repo's bun test gave 1129 pass, 0 fail. bun scripts/check.mjs passed.
On the old index.mjs, 3 of the 4 new tests fail. With the cut branch
disabled by hand they fail again.

Not tried: a real Go account. The owner's two Command Code accounts aren't
on Go, and the real upstream can't be made to cut a stream. A streamed
request through the patched loader on those accounts still takes the
Provider API path, which this commit doesn't touch.
2026-10-12 20:31:33 +08:00

62 lines
3.0 KiB
JavaScript

// A Go reply Command Code stops sending before its finish line (#91): a
// streamed one that said something ends cut short, with a note, instead of
// failing the turn; one that said nothing, or a non-streamed one, is a 502.
import { afterEach, expect, test } from "bun:test"
import { CommandCodePlugin, _internal } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => {
globalThis.fetch = real
_internal.subsSeen.clear()
})
const NOTE = "(The reply was cut short: Command Code closed the stream before it finished. Ask again to go on.)"
async function ask(lines, stream) {
globalThis.fetch = async (url) => {
if (new URL(String(url)).pathname === "/alpha/generate") return new Response(lines.map((l) => JSON.stringify(l)).join("\n") + "\n")
return new Response("", { status: 404 })
}
const hooks = await CommandCodePlugin()
const opts = await hooks.auth.loader(async () => ({ type: "api", key: "go-key", metadata: { plan: "Go" } }))
const res = await opts.fetch("https://api.commandcode.ai/provider/v1/chat/completions", {
method: "POST",
body: JSON.stringify({ model: "deepseek/deepseek-v4-pro", stream, messages: [{ role: "user", content: "hi" }] }),
})
return { status: res.status, text: await res.text() }
}
const frames = (text) =>
text.split("\n\n").filter((f) => f.startsWith("data: ")).map((f) => (f === "data: [DONE]" ? "[DONE]" : JSON.parse(f.slice(6))))
test("a streamed reply cut short after its text ends with the note, a stop and [DONE]", async () => {
const r = await ask([{ type: "reasoning-delta", text: "thinking" }, { type: "text-delta", text: "Half an ans" }], true)
expect(r.status).toBe(200)
const f = frames(r.text)
expect(f.at(-1)).toBe("[DONE]")
expect(f.some((x) => x.error)).toBe(false)
expect(f.map((x) => x.choices?.[0]?.delta?.content ?? "").join("")).toBe("Half an ans\n\n" + NOTE)
expect(f.at(-2).choices[0].finish_reason).toBe("stop")
})
test("a streamed reply cut short after a whole tool call finishes as tool_calls", async () => {
const r = await ask([{ type: "tool-call", toolCallId: "c1", toolName: "read", input: { path: "a" } }], true)
const f = frames(r.text)
expect(f.at(-1)).toBe("[DONE]")
expect(f.at(-2).choices[0].finish_reason).toBe("tool_calls")
expect(f.flatMap((x) => x.choices?.[0]?.delta?.tool_calls ?? []).map((c) => c.function.name)).toEqual(["read"])
})
test("a reply cut short with only reasoning says the note without a blank line before it", async () => {
const f = frames((await ask([{ type: "reasoning-delta", text: "hm" }], true)).text)
expect(f.map((x) => x.choices?.[0]?.delta?.content ?? "").join("")).toBe(NOTE)
})
test("a reply cut short before saying anything, or not streamed, is still a 502", async () => {
const none = await ask([{ type: "start" }], true)
expect(none.status).toBe(502)
const whole = await ask([{ type: "text-delta", text: "Half" }], false)
expect(whole.status).toBe(502)
expect(JSON.parse(whole.text).error.message).toBe("the reply ended before it was complete")
})