orcaterm 0.1.7 reads the tool call a model writes as text (DSML, <tool_call>, <function_calls>, <function=, <invoke name=) on both the streamed and non-streamed paths. The answer stops where the call begins, and the turn ends as a text_tool_call error (an error frame when streamed, a 502 when not), so magpie can retry or try another account instead of passing the call on as prose. A call shown in a code fence or in backticks is ordinary text, as in trae. A call carried only in the thinking goes through the same guard: the call is read from the extractor that cut it (answerAndCall), so prose before it no longer hides it. An `ask` envelope reaches the client as its question with its options under it, one per line, whether it is complete JSON, hand-written with a bare line break, cut short by the stream, or appended after prose. Reviewed at 39b4ef653e89e2b3e8c2d1b803563089ba4e3e2b, every line changed since 7a49ed7c. Merge-tested with origin/main2f8e65a1(clean, no conflicts in packages/orcaterm). Ran, with Bun 1.4.2: - WSL magpie-desktop (wsl-run, HOME=$(mktemp -d)): bun test packages/orcaterm 160 pass, 0 fail; bun scripts/check.mjs exit 0. The same after merging origin/main2f8e65a1: 160 pass, 0 fail, check exit 0. - Windows xiaomi-gpu (win-run, fresh HOME and USERPROFILE): bun test packages/orcaterm 160 pass, 0 fail; bun scripts/check.mjs exit 0. - Review probes (local, not pushed): 40, all green. That covers the 21 from round 2, including the three thinking-only calls (prose + call streamed gives the prose plus an error frame, not streamed gives a 502, a call inside a thinking envelope gives the error frame), the loose ask, the cut-short ask and prose + ask. It also covers 19 new ones: streamed and non-streamed give the same result for a loose ask in 1-, 3- and 4096-character frames, prose + loose ask, prose + fenced ask, an appended completion still dropped, a fenced or backticked call in the thinking shown, content winning over a call in the thinking, a call split over 1-character thinking frames, a cut-short opener in the thinking, a complete ask followed by prose kept verbatim, a backticked ask example kept, and an appended ask whose question carries a call (502 / error frame). Broken by hand, one at a time (cp-restored and touched each time), the whole orcaterm suite: 13 of 17 breaks go red. Red: trusting a match at the end of the text (3), the quoted check in answerCut (6), the complete-opener hold (18), the i flag (2), the streamed thinking guard (3), the non-streamed thinking guard (1), answerAndCall returning no call (4), answerField always taskCompletion (2), the appended ask dropped (1), the loose reader's options strip (1), the loose ask's options (1), the cut-short field (1), the ask unwrap (2). Not red in the suite (each one red in the review probes): object options read for their label, a blank question treated as none, and the fence taken off an appended ask (break it and the question of "Two options.\n```json\n{ask}\n```" is dropped). textCallIn without quoted() has no effect any more, because nothing calls textCallIn now apart from its export. Correction to round 2: the "duplicate" <function_calls> in TEXT_CALL_OPEN and TEXT_CALL_MARKS is <function_calls> and its antml:-prefixed form. Our tooling had shown both lines the same. Not a duplicate. Not tried: a real OrcaTerm account (none used from this PR), and an end-to-end magpie gateway run. Edges noted, not blocking: an ask carrying both taskCompletion and question answers with taskCompletion when it is valid JSON, and with question when it is hand-written. A hand-written ask whose option string contains "]" still goes out raw.
218 lines
10 KiB
JavaScript
218 lines
10 KiB
JavaScript
// What magpie hands the plugin is a Chat Completions body; OrcaTerm's
|
|
// assistant takes one user turn plus a history list and has no system
|
|
// role. This checks that the translation keeps the conversation's meaning:
|
|
// system text survives, turns stay in order, and tool traffic degrades to
|
|
// text instead of being dropped.
|
|
import { expect, test } from "bun:test"
|
|
import { _internal } from "./index.mjs"
|
|
|
|
// The suite fires far more than the account's ten-a-minute budget; lift the
|
|
// pacer so these tests measure the plugin, not the limiter.
|
|
process.env.ORCATERM_NO_PACE = "1"
|
|
|
|
|
|
const turn = (messages) => _internal.toUpstreamBody({ messages }, { conversationId: "cid-1", userId: "u-1" })
|
|
|
|
test("system text is carried as a prefix, the last user turn as the input", () => {
|
|
const up = turn([
|
|
{ role: "system", content: "you are a terminal assistant" },
|
|
{ role: "user", content: "first" },
|
|
{ role: "assistant", content: "first answer" },
|
|
{ role: "user", content: "second" },
|
|
])
|
|
expect(up.conversationId).toBe("cid-1")
|
|
expect(up.input.input.startsWith("[system instructions")).toBe(true)
|
|
expect(up.input.input.endsWith("second")).toBe(true)
|
|
// The earlier turns travel as a transcript in the text: magpie gives every
|
|
// request a fresh sessionID, so nothing on the wire can rely on server-side
|
|
// conversation state (measured 2026-10-27).
|
|
expect(up.input.input).toContain("[earlier in this conversation]")
|
|
expect(up.input.input).toContain("user: first")
|
|
expect(up.input.input).toContain("assistant: first answer")
|
|
// and nothing goes up as historyMessages
|
|
expect(up.input.historyMessages).toEqual([])
|
|
expect(up.input.type).toBe("start")
|
|
expect(up.stream).toBe(true)
|
|
})
|
|
|
|
// OrcaTerm runs its own tools server-side; the client's tool calls can only
|
|
// be described, so they become text the model can read.
|
|
test("tool calls and their results become text, nothing is lost", () => {
|
|
const up = turn([
|
|
{ role: "user", content: "what's the weather" },
|
|
{ role: "assistant", content: "", tool_calls: [{ id: "c1", function: { name: "get_weather", arguments: '{"city":"SZ"}' } }] },
|
|
{ role: "tool", tool_call_id: "c1", content: "sunny 30C" },
|
|
])
|
|
// no history is sent, so the tool traffic has to ride along in the text
|
|
const all = up.input.input
|
|
expect(all).toContain("get_weather")
|
|
expect(all).toContain("sunny 30C")
|
|
// the tool result is the last turn, so it is what gets asked
|
|
expect(up.input.input).toContain("tool result")
|
|
})
|
|
|
|
// An empty input is answered with a 400 by the assistant, so an empty
|
|
// conversation has to be given something to reply to.
|
|
test("an empty conversation still sends a non-empty input", () => {
|
|
expect(turn([]).input.input.trim().length).toBeGreaterThan(0)
|
|
expect(turn([{ role: "system", content: "be brief" }]).input.input.trim().length).toBeGreaterThan(0)
|
|
})
|
|
|
|
test("image parts are noted, text parts kept", () => {
|
|
const up = turn([
|
|
{
|
|
role: "user",
|
|
content: [
|
|
{ type: "text", text: "look" },
|
|
{ type: "image_url", image_url: { url: "http://x/y.png" } },
|
|
],
|
|
},
|
|
])
|
|
expect(up.input.input).toContain("look")
|
|
expect(up.input.input).toContain("[image]")
|
|
})
|
|
|
|
// A model id magpie may pass with its provider prefix, in any case, with
|
|
// padding — and one it doesn't know, which must reach the endpoint as is
|
|
// rather than be swallowed.
|
|
test("model ids resolve through aliases, prefixes and case", () => {
|
|
expect(_internal.resolveModel("deepseek")).toBe("TokenHub/deepseek-v4-pro")
|
|
expect(_internal.resolveModel("orcaterm/kimi")).toBe("TokenHub/kimi-k3")
|
|
expect(_internal.resolveModel(" GLM-4.7-THINKING ")).toBe("glm-4.7-thinking")
|
|
expect(_internal.resolveModel("some-new-model")).toBe("some-new-model")
|
|
// a bare prefix must fall back to a real model, not send an empty id
|
|
expect(_internal.resolveModel("orcaterm/")).toBe("TokenHub/deepseek-v4-pro")
|
|
expect(_internal.resolveModel(undefined)).toBe("TokenHub/deepseek-v4-pro")
|
|
})
|
|
|
|
// OrcaTerm's picker shows a label, and the id sent upstream is a different
|
|
// string (`newModelId` vs `modelName` in its own table). Sending the label is
|
|
// what made upstream answer "LLM(x) is not loaded", and `hy3-preview` is a
|
|
// label, not an id — it answers 401. The label must resolve to the working id.
|
|
test("labels resolve to the ids upstream actually serves", () => {
|
|
expect(_internal.resolveModel("Hy3-preview")).toBe("Hunyuan3/hy3")
|
|
expect(_internal.resolveModel("hy3-preview")).toBe("Hunyuan3/hy3")
|
|
expect(_internal.resolveModel("Deepseek-V4-Pro")).toBe("TokenHub/deepseek-v4-pro")
|
|
expect(_internal.resolveModel("GLM-5.2")).toBe("TokenHub/glm-5.2")
|
|
expect(_internal.resolveModel("Kimi-k2.6")).toBe("TokenHub/kimi-k3")
|
|
})
|
|
|
|
// Upstream answered `Invalid inputId: in-…` to the ids this plugin used to
|
|
// invent, so the shape matters: the web client sends a plain uuid v4 in
|
|
// `input.id` (module 73383's Ub) and the same value in `x-seqid`. A prefix of
|
|
// our own, however unique, is not that.
|
|
test("the input id is the web client's uuid v4, and x-seqid is that same id", async () => {
|
|
const up = turn([{ role: "user", content: "hi" }])
|
|
expect(up.input.id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)
|
|
// and a fresh one per request, never a fixed value
|
|
expect(turn([{ role: "user", content: "hi" }]).input.id).not.toBe(up.input.id)
|
|
})
|
|
|
|
// Upstream does not answer in prose: the whole reply is the assistant's own
|
|
// message object, and the text is its taskCompletion. Passed on raw, an agent
|
|
// showed `{"action": "completion", …}` as the answer.
|
|
test("the answer is unwrapped from the action envelope, alone or after prose", () => {
|
|
const one = JSON.stringify({ action: "completion", ph_1: "--", taskCompletion: "答案" })
|
|
expect(_internal.unwrapAnswer(one)).toBe("答案")
|
|
// fenced the way the assistant fences code
|
|
expect(_internal.unwrapAnswer("```json\n" + one + "\n```")).toBe("答案")
|
|
// prose first, envelope after it: the prose is the answer
|
|
expect(_internal.stripTrailingEnvelope("已记住。\n" + one)).toBe("已记住。\n")
|
|
// and a reply that is not an envelope is left alone
|
|
expect(_internal.unwrapAnswer("用 {花括号} 表示集合")).toBe("用 {花括号} 表示集合")
|
|
})
|
|
|
|
// An `ask` keeps its answer in `question`, not `taskCompletion`, so the whole
|
|
// object used to go out as the reply — braces, `ph_1` and all (seen live
|
|
// 2026-11-11, the model asking which of five options to take).
|
|
test("an ask is unwrapped to its question and choices", () => {
|
|
const ask = JSON.stringify({
|
|
action: "ask",
|
|
ph_1: "--",
|
|
question: "要处理哪个目标?",
|
|
options: ["检查运行状态", "排查服务异常", ""],
|
|
})
|
|
expect(_internal.unwrapAnswer(ask)).toBe("要处理哪个目标?\n\n- 检查运行状态\n- 排查服务异常")
|
|
// no choices to show: the question alone
|
|
expect(_internal.unwrapAnswer(JSON.stringify({ action: "ask", question: "继续吗?", options: [] }))).toBe("继续吗?")
|
|
// an ask with no question is not an answer to unwrap
|
|
const empty = JSON.stringify({ action: "ask", ph_1: "--" })
|
|
expect(_internal.unwrapAnswer(empty)).toBe(empty)
|
|
// another action's `question` is not one: only an ask is read this way
|
|
const review = JSON.stringify({ action: "review", question: "这是审查的备注", tool: {} })
|
|
expect(_internal.unwrapAnswer(review)).toBe(review)
|
|
})
|
|
|
|
// And it holds while the ask streams in, one character at a time: the answer
|
|
// the client builds out of the deltas is the same one, and no brace of it goes
|
|
// out.
|
|
test("a streamed ask reaches the client as its answer, not as its JSON", async () => {
|
|
const ask = JSON.stringify({
|
|
action: "ask",
|
|
ph_1: "--",
|
|
question: "要处理哪个目标?",
|
|
options: ["检查运行状态", "排查服务异常"],
|
|
})
|
|
const resp = _internal.upstreamToOpenAI(
|
|
new ReadableStream({
|
|
start(c) {
|
|
const all = `data: ${JSON.stringify({ content: ask, reasoningContent: "", type: "ai", code: 0 })}\n\ndata: [DONE]\n\n`
|
|
for (const ch of all) c.enqueue(new TextEncoder().encode(ch))
|
|
c.close()
|
|
},
|
|
}),
|
|
{ model: "m" },
|
|
)
|
|
let content = ""
|
|
for (const line of (await resp.text()).split("\n")) {
|
|
if (!line.startsWith("data: ") || line.trim() === "data: [DONE]") continue
|
|
content += JSON.parse(line.slice(6)).choices?.[0]?.delta?.content ?? ""
|
|
}
|
|
expect(content).toBe("要处理哪个目标?\n\n- 检查运行状态\n- 排查服务异常")
|
|
expect(content).not.toContain("{")
|
|
})
|
|
|
|
// The three shapes the reviewer listed as still going out raw.
|
|
const streamedAsk = async (raw, size = 3) => {
|
|
const all = `data: ${JSON.stringify({ content: raw, reasoningContent: "", type: "ai", code: 0 })}\n\ndata: [DONE]\n\n`
|
|
const pieces = []
|
|
for (let i = 0; i < all.length; i += size) pieces.push(all.slice(i, i + size))
|
|
const resp = _internal.upstreamToOpenAI(
|
|
new ReadableStream({
|
|
start(c) {
|
|
for (const p of pieces) c.enqueue(new TextEncoder().encode(p))
|
|
c.close()
|
|
},
|
|
}),
|
|
{ model: "m" },
|
|
)
|
|
let content = ""
|
|
for (const line of (await resp.text()).split("\n")) {
|
|
if (!line.startsWith("data: ") || line.trim() === "data: [DONE]") continue
|
|
content += JSON.parse(line.slice(6)).choices?.[0]?.delta?.content ?? ""
|
|
}
|
|
return content
|
|
}
|
|
|
|
// 1. Written by hand, with a bare line break in the question, the way a long
|
|
// answer comes (#564). Only `taskCompletion` was read this loosely.
|
|
test("a hand-written ask with a bare line break still comes out as its answer", async () => {
|
|
const raw = '{"action": "ask", "ph_1": "--", "question": "Which one?\nPick:", "options": ["a", "b"]}'
|
|
expect(await streamedAsk(raw)).toBe("Which one?\nPick:\n\n- a\n- b")
|
|
})
|
|
|
|
// 2. The stream ends inside the question: a completion keeps its decoded prefix,
|
|
// and the ask must do the same.
|
|
test("an ask the stream ended inside keeps its question", async () => {
|
|
const raw = '{"action":"ask","ph_1":"--","question":"Which target?","options":["a"]}'
|
|
const cut = raw.slice(0, raw.indexOf("target?") + "target?".length)
|
|
expect(await streamedAsk(cut)).toBe("Which target?")
|
|
})
|
|
|
|
// 3. Prose, then the ask appended: dropping the envelope is right for a
|
|
// completion, and wrong for an ask — the question is what the user answers.
|
|
test("an ask appended after prose keeps the question", async () => {
|
|
const raw = 'I found two targets.\n{"action":"ask","ph_1":"--","question":"Which target?","options":["a","b"]}'
|
|
expect(await streamedAsk(raw)).toBe("I found two targets.\nWhich target?\n\n- a\n- b")
|
|
})
|