diff --git a/scripts/live-bug-test.ts b/scripts/live-bug-test.ts new file mode 100644 index 0000000..e09410a --- /dev/null +++ b/scripts/live-bug-test.ts @@ -0,0 +1,237 @@ +/** + * Live end-to-end test: reproduce the provider_error 400 bug against the real + * Qoder gateway using the local machine's stored OAuth credentials. + * + * Scenario that triggered the bug (from historical session analysis): + * user → assistant(ONLY tool_calls, no text) → toolResult → new request + * + * Before the fix: transform produced { content: null, tool_calls: [...] } + * → Qoder gateway dropped the assistant message + * → toolResult orphaned → upstream 400 provider_error + * + * After the fix: transform produces { content: " ", tool_calls: [...] } + * → gateway keeps the message → tool pairs up → success + * + * Usage: npx tsx scripts/live-bug-test.ts + */ +import { readFileSync } from "node:fs"; +import { homedir } from "node:os"; +import { join } from "node:path"; +import { buildAuthHeaders, getQoderChatURL, getQoderMode } from "../src/cosy.js"; +import { qoderEncodeBody } from "../src/qoder-encoding.js"; +import { transformMessagesForQoder, transformTools } from "../src/transform.js"; + +const AUTH_FILE = join(homedir(), ".pi", "agent", "auth.json"); +const MODEL = process.argv[2] || "lite"; + +interface QoderCreds { + access: string; + userID: string; + name: string; + email: string; + machineID?: string; +} + +function loadCreds(): QoderCreds { + const auth = JSON.parse(readFileSync(AUTH_FILE, "utf-8")); + const c = auth.qoder; + if (!c?.access) throw new Error("No qoder credentials in ~/.pi/agent/auth.json — run `pi login` first"); + return c; +} + +function buildRequestBody( + model: string, + messages: ReturnType, + tools?: ReturnType, +) { + const uuid = () => globalThis.crypto.randomUUID(); + return { + request_id: uuid(), + request_set_id: uuid(), + chat_record_id: uuid(), + session_id: `qoder-session-${model}-${uuid()}`, + stream: true, + chat_task: "FREE_INPUT", + is_reply: true, + is_retry: false, + source: 1, + version: "3", + session_type: "qodercli", + agent_id: "agent_common", + task_id: "common", + code_language: "", + chat_prompt: "", + image_urls: null, + aliyun_user_type: "", + system: "", + messages, + tools: tools || [], + parameters: { max_tokens: 1024 }, + chat_context: { + chatPrompt: "", + imageUrls: null, + extra: { context: [], modelConfig: { key: model, is_reasoning: false }, originalContent: "" }, + features: [], + text: "", + }, + model_config: { key: model, is_reasoning: false, source: "system" }, + business: { product: "cli", version: "1.0.0", type: "agent", stage: "start", id: uuid(), name: "", begin_at: Date.now() }, + }; +} + +interface TestResult { + ok: boolean; + httpStatus: string; + upstreamStatus: number | null; + errorDetail: string | null; + firstText: string | null; +} + +/** Send a request, decode the SSE envelope stream, return a verdict. */ +async function sendRequest(label: string, body: Record): Promise { + const mode = getQoderMode(); + const url = getQoderChatURL(mode); + const creds = loadCreds(); + + // CRITICAL: body must be encoded with qoderEncodeBody, and auth headers + // computed on the ENCODED bytes (see stream.ts). + const encodedBody = qoderEncodeBody(Buffer.from(JSON.stringify(body))); + const encodedBytes = Buffer.from(encodedBody, "utf8"); + const headers = buildAuthHeaders(encodedBytes, url, { + userID: creds.userID, + authToken: creds.access, + name: creds.name, + email: creds.email, + machineID: creds.machineID, + }); + + console.log(`\n[${label}] POST ${url} model=${body.model_config?.key}`); + const res = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "text/event-stream", + "Cache-Control": "no-cache", + "Accept-Encoding": "identity", + "X-Model-Key": body.model_config?.key, + "X-Model-Source": body.model_config?.source || "system", + ...headers, + }, + body: encodedBytes, + }); + + const text = await res.text(); + let upstreamStatus: number | null = null; + let errorDetail: string | null = null; + let firstText: string | null = null; + + if (!res.ok) { + errorDetail = `HTTP ${res.status}: ${text.slice(0, 500)}`; + } else { + // SSE envelope: data:{"statusCodeValue":200,"body":""} + for (const line of text.split("\n")) { + if (!line.startsWith("data:")) continue; + const payload = line.slice(5).trim(); + if (!payload || payload === "[DONE]") continue; + try { + const envelope = JSON.parse(payload); + if (envelope.statusCodeValue && envelope.statusCodeValue !== 200) { + upstreamStatus = envelope.statusCodeValue; + errorDetail = `Upstream ${envelope.statusCodeValue}: ${String(envelope.body).slice(0, 300)}`; + break; + } + const innerStr = envelope.body; + if (!innerStr || innerStr === "[DONE]") continue; + const inner = JSON.parse(innerStr); + const delta = inner.choices?.[0]?.delta?.content || inner.choices?.[0]?.message?.content; + if (delta && !firstText) firstText = String(delta).slice(0, 80); + } catch { + // non-JSON line, skip + } + } + if (!firstText && !errorDetail) errorDetail = `no content parsed; raw head: ${text.slice(0, 400)}`; + } + + return { + ok: res.ok && !errorDetail, + httpStatus: `HTTP ${res.status} ${res.statusText}`, + upstreamStatus, + errorDetail, + firstText, + }; +} + +function fmt(r: TestResult) { + const verdict = r.ok ? "✅ OK" : "❌ FAIL"; + const parts = [verdict, r.httpStatus]; + if (r.upstreamStatus) parts.push(`upstream=${r.upstreamStatus}`); + if (r.firstText) parts.push(`text="${r.firstText}…"`); + if (r.errorDetail) parts.push(`err=${r.errorDetail}`); + return parts.join(" | "); +} + +async function main() { + const creds = loadCreds(); + console.log(`Loaded credentials: userID=${creds.userID} email=${creds.email} mode=${getQoderMode()}`); + + const piTools = [ + { + name: "calculate", + description: "Evaluate a math expression and return the result.", + parameters: { type: "object", properties: { expr: { type: "string" } }, required: ["expr"] }, + }, + ]; + const transformedTools = transformTools(piTools); + + // ── Test 1: basic connectivity (plain user message, no tools) ── + { + const messages = transformMessagesForQoder([{ role: "user", content: "Reply with exactly: pong" } as never]); + const body = buildRequestBody(MODEL, messages); + const r = await sendRequest("1-basic-connectivity", body); + console.log(fmt(r)); + } + + // ── Test 2: the bug scenario, FIXED transform (content: " ") ── + { + const messages = transformMessagesForQoder([ + { role: "user", content: "What is 2 + 3? Use the calculate tool." } as never, + { + role: "assistant", + content: [{ type: "toolCall", id: "call_test_1", name: "calculate", arguments: { expr: "2+3" } }], + } as never, + { role: "toolResult", toolCallId: "call_test_1", content: "5" } as never, + ]); + const asst = messages[1] as { content: unknown; tool_calls?: unknown[] }; + console.log(`\n[2-bug-fixed] transformed assistant content=${JSON.stringify(asst.content)} (expect " ") tool_calls=${asst.tool_calls?.length}`); + const body = buildRequestBody(MODEL, messages, transformedTools); + const r = await sendRequest("2-bug-fixed", body); + console.log(fmt(r)); + } + + // ── Test 3: the bug scenario, OLD buggy transform (content: null) ── + { + const messages = transformMessagesForQoder([ + { role: "user", content: "What is 2 + 3? Use the calculate tool." } as never, + { + role: "assistant", + content: [{ type: "toolCall", id: "call_test_2", name: "calculate", arguments: { expr: "2+3" } }], + } as never, + { role: "toolResult", toolCallId: "call_test_2", content: "5" } as never, + ]); + (messages[1] as { content: unknown }).content = null; // force OLD buggy behavior + console.log(`\n[3-bug-old] transformed assistant content=null (forced old buggy behavior)`); + const body = buildRequestBody(MODEL, messages, transformedTools); + const r = await sendRequest("3-bug-old-null", body); + console.log(fmt(r)); + console.log( + r.ok + ? " → lite upstream TOLERATES content:null (like GLM) — bug does NOT reproduce on this model" + : " → lite upstream REJECTS content:null — bug reproduces here too, fix is validated", + ); + } +} + +main().catch((e) => { + console.error("FATAL:", e); + process.exit(1); +}); diff --git a/scripts/live-thinking-leak-test.ts b/scripts/live-thinking-leak-test.ts new file mode 100644 index 0000000..9911995 --- /dev/null +++ b/scripts/live-thinking-leak-test.ts @@ -0,0 +1,378 @@ +/** + * Live reproduction of the `` leak against the real Qoder gateway. + * + * What this verifies: + * 1. Whether the server actually splits a `...` pair + * across `delta.reasoning_content` (opener) and `delta.content` (closer) + * — the M1 condition that caused `` to leak into visible text. + * 2. That the NEW ThinkingTagParser + stripThinkingTags produce clean output + * (no literal tags) on the real captured stream. + * 3. For comparison, that the OLD parser logic leaks `` into text + * on the same stream. + * + * Usage: npx tsx scripts/live-thinking-leak-test.ts [model] [prompt] + * model defaults to "efficient" (the model that leaked historically) + */ +import { readFileSync } from "node:fs"; +import { homedir } from "node:os"; +import { join } from "node:path"; +import type { AssistantMessage, AssistantMessageEvent, AssistantMessageEventStream } from "@earendil-works/pi-ai"; +import { buildAuthHeaders, getQoderChatURL, getQoderMode } from "../src/cosy.js"; +import { qoderEncodeBody } from "../src/qoder-encoding.js"; +import { transformMessagesForQoder } from "../src/transform.js"; +import { + THINKING_TAG_VARIANTS, + ThinkingTagParser, + stripThinkingTags, +} from "../src/thinking-parser.js"; + +const AUTH_FILE = join(homedir(), ".pi", "agent", "auth.json"); +const MODEL = process.argv[2] || "efficient"; +const PROMPT = + process.argv[3] || + "A bat and a ball cost $1.10 in total. The bat costs $1.00 more than the ball. How much does the ball cost? Reason carefully step by step before answering."; + +interface QoderCreds { + access: string; + userID: string; + name: string; + email: string; + machineID?: string; +} + +function loadCreds(): QoderCreds { + const auth = JSON.parse(readFileSync(AUTH_FILE, "utf-8")); + const c = auth.qoder; + if (!c?.access) throw new Error("No qoder credentials in ~/.pi/agent/auth.json — run `pi login` first"); + return c; +} + +function uuid() { + return globalThis.crypto.randomUUID(); +} + +function buildRequestBody(model: string, messages: ReturnType, isReasoning: boolean) { + return { + request_id: uuid(), + request_set_id: uuid(), + chat_record_id: uuid(), + session_id: `qoder-session-${model}-${uuid()}`, + stream: true, + chat_task: "FREE_INPUT", + is_reply: true, + is_retry: false, + source: 1, + version: "3", + session_type: "qodercli", + agent_id: "agent_common", + task_id: "common", + code_language: "", + chat_prompt: "", + image_urls: null, + aliyun_user_type: "", + system: "", + messages, + tools: [], + parameters: { max_tokens: 1024 }, + chat_context: { + chatPrompt: "", + imageUrls: null, + extra: { context: [], modelConfig: { key: model, is_reasoning: isReasoning }, originalContent: PROMPT }, + features: [], + text: PROMPT, + }, + model_config: { key: model, is_reasoning: isReasoning, source: "system" }, + business: { product: "cli", version: "1.0.0", type: "agent", stage: "start", id: uuid(), name: PROMPT.slice(0, 30), begin_at: Date.now() }, + }; +} + +interface RawStream { + reasoningChunks: string[]; + contentChunks: string[]; + finishReason: string | null; + rawHead: string; +} + +/** POST and collect every raw delta.reasoning_content / delta.content chunk. */ +async function captureRawStream(model: string, isReasoning: boolean): Promise { + const mode = getQoderMode(); + const url = getQoderChatURL(mode); + const creds = loadCreds(); + const messages = transformMessagesForQoder([{ role: "user", content: PROMPT } as never]); + const body = buildRequestBody(model, messages, isReasoning); + const encodedBody = qoderEncodeBody(Buffer.from(JSON.stringify(body))); + const encodedBytes = Buffer.from(encodedBody, "utf8"); + const headers = buildAuthHeaders(encodedBytes, url, { + userID: creds.userID, + authToken: creds.access, + name: creds.name, + email: creds.email, + machineID: creds.machineID, + }); + + console.log(`\nPOST ${url} model=${model} is_reasoning=${isReasoning}`); + const res = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "text/event-stream", + "Cache-Control": "no-cache", + "Accept-Encoding": "identity", + "X-Model-Key": model, + "X-Model-Source": "system", + ...headers, + }, + body: encodedBytes, + }); + + const out: RawStream = { reasoningChunks: [], contentChunks: [], finishReason: null, rawHead: "" }; + if (!res.ok || !res.body) { + const text = await res.text().catch(() => ""); + out.rawHead = `HTTP ${res.status} ${res.statusText}: ${text.slice(0, 400)}`; + return out; + } + + const reader = res.body.getReader(); + const decoder = new TextDecoder(); + let buf = ""; + let envelopeBuffer = ""; + let headWritten = false; + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + buf += decoder.decode(value, { stream: true }); + let nl: number; + while ((nl = buf.indexOf("\n")) !== -1) { + const line = buf.slice(0, nl); + buf = buf.slice(nl + 1); + envelopeBuffer += line + "\n"; + // SSE lines look like: data:{"statusCodeValue":200,"body":""} + const m = line.match(/^data:(.*)$/); + if (!m) continue; + const payload = m[1].trim(); + if (!payload || payload === "[DONE]") continue; + try { + const envelope = JSON.parse(payload); + if (!headWritten) { + out.rawHead = `envelope statusCodeValue=${envelope.statusCodeValue} (first line ok)`; + headWritten = true; + } + if (envelope.statusCodeValue && envelope.statusCodeValue !== 200) { + out.rawHead = `Upstream ${envelope.statusCodeValue}: ${String(envelope.body).slice(0, 300)}`; + return out; + } + const innerStr = envelope.body; + if (!innerStr || innerStr === "[DONE]") continue; + const inner = JSON.parse(innerStr); + const choice = inner.choices?.[0]; + const delta = choice?.delta; + if (delta?.reasoning_content) out.reasoningChunks.push(delta.reasoning_content); + if (delta?.content) out.contentChunks.push(delta.content); + if (choice?.finish_reason) out.finishReason = choice.finish_reason; + } catch { + // non-JSON line, skip + } + } + } + return out; +} + +// ── OLD parser logic (faithful reimplementation of the pre-fix behavior) ── + +function getTrailingPrefixLength(text: string, tag: string): number { + const max = Math.min(text.length, tag.length - 1); + for (let len = max; len > 0; len--) { + if (text.endsWith(tag.slice(0, len))) return len; + } + return 0; +} +function getMaxTrailingPrefix(text: string, tags: string[]): number { + let max = 0; + for (const t of tags) max = Math.max(max, getTrailingPrefixLength(text, t)); + return max; +} + +/** Old processBeforeThinking: openers only, NO orphan-closer handling. */ +function oldParseContent(chunks: string[]): { thinking: string; text: string } { + let buffer = ""; + let inThinking = false; + let activeClose = THINKING_TAG_VARIANTS[0].close; + let thinking = ""; + let text = ""; + for (const chunk of chunks) { + buffer += chunk; + let progress = true; + while (buffer.length > 0 && progress) { + progress = false; + if (!inThinking) { + let bestPos = -1; + let bestV: (typeof THINKING_TAG_VARIANTS)[number] | null = null; + for (const v of THINKING_TAG_VARIANTS) { + const p = buffer.indexOf(v.open); + if (p !== -1 && (bestPos === -1 || p < bestPos)) { + bestPos = p; + bestV = v; + } + } + if (bestV) { + if (bestPos > 0) text += buffer.slice(0, bestPos); + buffer = buffer.slice(bestPos + bestV.open.length); + activeClose = bestV.close; + inThinking = true; + progress = true; + continue; + } + // no opener → emit safe text holding back open-tag prefixes only + const trailing = getMaxTrailingPrefix(buffer, THINKING_TAG_VARIANTS.map((v) => v.open)); + const safe = buffer.length - trailing; + if (safe > 0) { + text += buffer.slice(0, safe); + buffer = buffer.slice(safe); + progress = true; + } + } else { + const ep = buffer.indexOf(activeClose); + if (ep !== -1) { + if (ep > 0) thinking += buffer.slice(0, ep); + buffer = buffer.slice(ep + activeClose.length); + inThinking = false; + if (buffer.startsWith("\n\n")) buffer = buffer.slice(2); + progress = true; + continue; + } + const trailing = getTrailingPrefixLength(buffer, activeClose); + const safe = buffer.length - trailing; + if (safe > 0) { + thinking += buffer.slice(0, safe); + buffer = buffer.slice(safe); + progress = true; + } + } + } + } + if (inThinking) thinking += buffer; + else text += buffer; + return { thinking, text }; +} + +// ── NEW parser (the actual shipped class) ── + +function newParseContent(chunks: string[]): { thinking: string; text: string } { + const output: AssistantMessage = { + role: "assistant", + content: [], + api: "qoder-api" as never, + provider: "qoder", + model: "efficient", + usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } }, + stopReason: "stop", + timestamp: Date.now(), + } as AssistantMessage; + const events: AssistantMessageEvent[] = []; + const stream = { + push: (e: AssistantMessageEvent) => events.push(e), + end: () => {}, + [Symbol.iterator]: function* () {}, + [Symbol.asyncIterator]: function* () {}, + events, + } as unknown as AssistantMessageEventStream; + const parser = new ThinkingTagParser(output, stream); + for (const c of chunks) parser.processChunk(c); + parser.finalize(); + let thinking = ""; + let text = ""; + for (const block of output.content) { + if (block.type === "thinking") thinking += (block as { thinking: string }).thinking; + else if (block.type === "text") text += (block as { text: string }).text; + } + return { thinking, text }; +} + +function containsAnyTag(s: string, which: "open" | "close"): string[] { + const found: string[] = []; + for (const v of THINKING_TAG_VARIANTS) { + const tag = which === "open" ? v.open : v.close; + if (tag.length > 0 && s.includes(tag)) found.push(tag); + } + return found; +} + +function banner(s: string) { + console.log("\n" + "─".repeat(70)); + console.log(s); + console.log("─".repeat(70)); +} + +async function main() { + banner(`Live repro: leak | model=${MODEL}`); + console.log(`prompt: ${PROMPT.slice(0, 100)}${PROMPT.length > 100 ? "…" : ""}`); + + // Try is_reasoning=false first (matches the historical leak config), then true. + for (const isReasoning of [false, true]) { + const raw = await captureRawStream(MODEL, isReasoning); + if (raw.rawHead && raw.reasoningChunks.length === 0 && raw.contentChunks.length === 0) { + console.log(`\n!! no deltas captured: ${raw.rawHead}`); + continue; + } + const reasoning = raw.reasoningChunks.join(""); + const content = raw.contentChunks.join(""); + console.log( + `\ncaptured: ${raw.reasoningChunks.length} reasoning chunks (${reasoning.length} chars), ` + + `${raw.contentChunks.length} content chunks (${content.length} chars), finish=${raw.finishReason}`, + ); + + if (reasoning.length === 0 && content.length === 0) { + console.log("(empty stream — nothing to analyze)"); + continue; + } + + // ── Detect M1: server split a tag pair across the two channels ── + const openInReasoning = containsAnyTag(reasoning, "open"); + const closeInContent = containsAnyTag(content, "close"); + const openInContent = containsAnyTag(content, "open"); + const closeInReasoning = containsAnyTag(reasoning, "close"); + const m1Split = openInReasoning.length > 0 && closeInContent.length > 0; + + banner("M1 detection (tag split across reasoning_content / content)"); + console.log(`reasoning_content has openers : ${openInReasoning.length ? openInReasoning.join(" ") : "(none)"}`); + console.log(`content has closers : ${closeInContent.length ? closeInContent.join(" ") : "(none)"}`); + console.log(`content has openers : ${openInContent.length ? openInContent.join(" ") : "(none)"}`); + console.log(`reasoning_content has closers: ${closeInReasoning.length ? closeInReasoning.join(" ") : "(none)"}`); + console.log(`\n>>> M1 split reproduced: ${m1Split ? "YES — server splits the tag pair across channels" : "NO (model did not emit split tags this run)"}`); + + if (content.length > 0) { + banner("CONTENT channel — OLD vs NEW parser"); + const oldC = oldParseContent(raw.contentChunks); + const newC = newParseContent(raw.contentChunks); + const oldLeakTags = [...containsAnyTag(oldC.text, "close"), ...containsAnyTag(oldC.text, "open")]; + const newLeakTags = [...containsAnyTag(newC.text, "close"), ...containsAnyTag(newC.text, "open")]; + console.log(`OLD text output : ${JSON.stringify(oldC.text.slice(0, 120))}${oldC.text.length > 120 ? "…" : ""}`); + console.log(`OLD leaks tags : ${oldLeakTags.length ? oldLeakTags.join(" ") : "(none) ← clean"}`); + console.log(`NEW text output : ${JSON.stringify(newC.text.slice(0, 120))}${newC.text.length > 120 ? "…" : ""}`); + console.log(`NEW leaks tags : ${newLeakTags.length ? newLeakTags.join(" ") : "(none) ← clean"}`); + } + + if (reasoning.length > 0) { + banner("REASONING_CONTENT channel — OLD (verbatim) vs NEW (stripThinkingTags)"); + const oldR = reasoning; // old stream.ts appended verbatim + const newR = stripThinkingTags(reasoning); + const oldRLeaks = containsAnyTag(oldR, "open").concat(containsAnyTag(oldR, "close")); + const newRLeaks = containsAnyTag(newR, "open").concat(containsAnyTag(newR, "close")); + console.log(`OLD reasoning head: ${JSON.stringify(oldR.slice(0, 80))}…`); + console.log(`OLD leaks tags : ${oldRLeaks.length ? oldRLeaks.join(" ") : "(none) ← clean"}`); + console.log(`NEW reasoning head: ${JSON.stringify(newR.slice(0, 80))}…`); + console.log(`NEW leaks tags : ${newRLeaks.length ? newRLeaks.join(" ") : "(none) ← clean"}`); + } + + // If we got a real stream, don't bother retrying the other is_reasoning flag. + if (reasoning.length > 0 || content.length > 0) break; + } + + banner("done"); +} + +main().catch((e) => { + console.error("FATAL:", e); + process.exit(1); +}); diff --git a/src/__tests__/thinking-parser.test.ts b/src/__tests__/thinking-parser.test.ts index 145f1c7..bce343d 100644 --- a/src/__tests__/thinking-parser.test.ts +++ b/src/__tests__/thinking-parser.test.ts @@ -1,6 +1,6 @@ import type { Api, AssistantMessage, AssistantMessageEvent, AssistantMessageEventStream } from "@earendil-works/pi-ai"; import { beforeEach, describe, expect, it, vi } from "vitest"; -import { ThinkingTagParser } from "../thinking-parser.js"; +import { stripThinkingTags, ThinkingTagParser } from "../thinking-parser.js"; function createMockStream() { const events: AssistantMessageEvent[] = []; @@ -240,4 +240,68 @@ describe("ThinkingTagParser", () => { expect(output.content).toHaveLength(1); expect(output.content[0]).toMatchObject({ type: "thinking", thinking: "unfinished" }); }); + + // ── Orphan closing tags (opener arrived via reasoning_content) ───────── + // Regression: Qoder's backend splits one `...` pair + // across two SSE fields — opener+reasoning into `reasoning_content`, closer + // +answer into `content`. The closer has no opener in the content stream, so + // it must be dropped instead of leaking into visible text. + + it("drops an orphan closing tag at the start of content", () => { + const parser = new ThinkingTagParser(output, stream); + parser.processChunk("\n\n让我查一下这个选项"); + parser.finalize(); + + const textBlocks = output.content.filter((c) => c.type === "text"); + expect(textBlocks).toHaveLength(1); + const text = (textBlocks[0] as { type: string; text: string }).text; + expect(text).toBe("让我查一下这个选项"); + expect(text).not.toContain(""); + expect(output.content.some((c) => c.type === "thinking")).toBe(false); + }); + + it("drops an orphan closer split across stream chunks", () => { + const parser = new ThinkingTagParser(output, stream); + parser.processChunk("\n\nanswer"); + parser.finalize(); + + const textBlocks = output.content.filter((c) => c.type === "text"); + expect(textBlocks).toHaveLength(1); + const text = (textBlocks[0] as { type: string; text: string }).text; + expect(text).toBe("answer"); + expect(text).not.toContain(""); + expect(text).not.toContain(" variant too", () => { + const parser = new ThinkingTagParser(output, stream); + parser.processChunk("\n\nresult"); + parser.finalize(); + + const text = (output.content.find((c) => c.type === "text") as { type: string; text: string })?.text ?? ""; + expect(text).toBe("result"); + expect(text).not.toContain(""); + }); + + it("emits text before an orphan closer, then drops the closer", () => { + const parser = new ThinkingTagParser(output, stream); + parser.processChunk("intro\n\noutro"); + parser.finalize(); + + const text = (output.content.find((c) => c.type === "text") as { type: string; text: string })?.text ?? ""; + expect(text).not.toContain(""); + expect(text).toContain("intro"); + expect(text).toContain("outro"); + }); + + // ── stripThinkingTags helper ─────────────────────────────────────────── + + it("stripThinkingTags removes opening and closing tag variants", () => { + expect(stripThinkingTags("hello")).toBe("hello"); + expect(stripThinkingTags("deep")).toBe("deep"); + expect(stripThinkingTags("plain text")).toBe("plain text"); + expect(stripThinkingTags("")).toBe(""); + expect(stripThinkingTags("")).toBe(""); + }); }); diff --git a/src/stream.ts b/src/stream.ts index ede0cc8..9c42e5e 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -23,7 +23,7 @@ import { import { getCachedModelConfig } from "./models.js"; import { getCachedCredentials } from "./oauth.js"; import { qoderEncodeBody } from "./qoder-encoding.js"; -import { ThinkingTagParser } from "./thinking-parser.js"; +import { stripThinkingTags, ThinkingTagParser } from "./thinking-parser.js"; import { transformMessagesForQoder, transformTools } from "./transform.js"; interface ToolCallState { @@ -326,19 +326,27 @@ export function streamQoder( if (delta) { // 1. Process reasoning/thinking content (API reasoning) if (delta.reasoning_content) { - if (thinkingBlockIndex === -1) { - thinkingBlockIndex = output.content.length; - output.content.push({ type: "thinking", thinking: "" }); - stream.push({ type: "thinking_start", contentIndex: thinkingBlockIndex, partial: output }); + // Qoder's backend sometimes routes a literal `` + // opener into reasoning_content (with the matching + // `` closer landing in the content stream). Strip + // tag artifacts so the thinking block stays clean, matching + // the SDK's ContentBlock model. + const reasoningChunk = stripThinkingTags(delta.reasoning_content); + if (reasoningChunk) { + if (thinkingBlockIndex === -1) { + thinkingBlockIndex = output.content.length; + output.content.push({ type: "thinking", thinking: "" }); + stream.push({ type: "thinking_start", contentIndex: thinkingBlockIndex, partial: output }); + } + const block = output.content[thinkingBlockIndex] as ThinkingContent; + block.thinking += reasoningChunk; + stream.push({ + type: "thinking_delta", + contentIndex: thinkingBlockIndex, + delta: reasoningChunk, + partial: output, + }); } - const block = output.content[thinkingBlockIndex] as ThinkingContent; - block.thinking += delta.reasoning_content; - stream.push({ - type: "thinking_delta", - contentIndex: thinkingBlockIndex, - delta: delta.reasoning_content, - partial: output, - }); } // 2. Process text content diff --git a/src/thinking-parser.ts b/src/thinking-parser.ts index 560dba7..a939d01 100644 --- a/src/thinking-parser.ts +++ b/src/thinking-parser.ts @@ -5,7 +5,7 @@ import type { ThinkingContent, } from "@earendil-works/pi-ai"; -const THINKING_TAG_VARIANTS: Array<{ open: string; close: string }> = [ +export const THINKING_TAG_VARIANTS: Array<{ open: string; close: string }> = [ { open: "", close: "" }, { open: "", close: "" }, { open: "", close: "" }, @@ -28,6 +28,25 @@ function getMaxTrailingPossibleTagPrefixLength(text: string, tags: string[]): nu return maxLength; } +/** + * Remove every thinking/reasoning tag variant (open and close) from `text`. + * + * Qoder's backend sometimes routes a literal `` opener into the + * `reasoning_content` channel (and the matching `` closer into the + * `content` channel). Stripping these artifacts keeps the thinking block clean, + * matching the SDK's `ContentBlock` model. Best-effort per chunk: a tag split + * across stream deltas is not caught here (the ThinkingTagParser handles the + * content-channel side with cross-delta buffering). + */ +export function stripThinkingTags(text: string): string { + let out = text; + for (const { open, close } of THINKING_TAG_VARIANTS) { + if (open.length > 0 && out.includes(open)) out = out.split(open).join(""); + if (close.length > 0 && out.includes(close)) out = out.split(close).join(""); + } + return out; +} + export class ThinkingTagParser { private textBuffer = ""; private inThinking = false; @@ -90,27 +109,53 @@ export class ThinkingTagParser { } private processBeforeThinking(): void { - let bestPos = -1; - let bestVariant: (typeof THINKING_TAG_VARIANTS)[number] | null = null; + // Find the first opener and first closer in the buffer. + let bestOpenPos = -1; + let bestOpenVariant: (typeof THINKING_TAG_VARIANTS)[number] | null = null; + let bestClosePos = -1; + let bestCloseVariant: (typeof THINKING_TAG_VARIANTS)[number] | null = null; for (const variant of THINKING_TAG_VARIANTS) { - const pos = this.textBuffer.indexOf(variant.open); - if (pos !== -1 && (bestPos === -1 || pos < bestPos)) { - bestPos = pos; - bestVariant = variant; + const openPos = this.textBuffer.indexOf(variant.open); + if (openPos !== -1 && (bestOpenPos === -1 || openPos < bestOpenPos)) { + bestOpenPos = openPos; + bestOpenVariant = variant; + } + const closePos = this.textBuffer.indexOf(variant.close); + if (closePos !== -1 && (bestClosePos === -1 || closePos < bestClosePos)) { + bestClosePos = closePos; + bestCloseVariant = variant; } } - if (bestPos !== -1 && bestVariant) { - if (bestPos > 0) this.emitText(this.textBuffer.slice(0, bestPos)); - this.textBuffer = this.textBuffer.slice(bestPos + bestVariant.open.length); - this.activeEndTag = bestVariant.close; + + // Opener comes first (or is the only tag): a real thinking block carried + // in the content stream. Enter thinking mode; processInsideThinking will + // handle its closer. + if (bestOpenVariant !== null && (bestCloseVariant === null || bestOpenPos < bestClosePos)) { + if (bestOpenPos > 0) this.emitText(this.textBuffer.slice(0, bestOpenPos)); + this.textBuffer = this.textBuffer.slice(bestOpenPos + bestOpenVariant.open.length); + this.activeEndTag = bestOpenVariant.close; this.inThinking = true; return; } - const trailingPrefixLength = getMaxTrailingPossibleTagPrefixLength( - this.textBuffer, - THINKING_TAG_VARIANTS.map((variant) => variant.open), - ); + // Closer with no preceding opener: an orphan close tag. Its matching + // opener was delivered via the separate `reasoning_content` channel (see + // stream.ts), so there is no thinking block to close here. Drop it — and + // the separator whitespace the model emits right after `` — so + // it does not leak into visible text. + if (bestCloseVariant !== null) { + if (bestClosePos > 0) this.emitText(this.textBuffer.slice(0, bestClosePos)); + this.textBuffer = this.textBuffer.slice(bestClosePos + bestCloseVariant.close.length); + if (this.textBuffer.startsWith("\n\n")) this.textBuffer = this.textBuffer.slice(2); + else if (this.textBuffer.startsWith("\n")) this.textBuffer = this.textBuffer.slice(1); + return; + } + + // No complete tag yet. Hold back any trailing prefix that could be the + // start of an opener OR a closer, so a tag split across stream deltas is + // not partially emitted as text. + const allTags = THINKING_TAG_VARIANTS.flatMap((variant) => [variant.open, variant.close]); + const trailingPrefixLength = getMaxTrailingPossibleTagPrefixLength(this.textBuffer, allTags); const safeLen = this.textBuffer.length - trailingPrefixLength; if (safeLen > 0) { this.emitText(this.textBuffer.slice(0, safeLen));