Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions kernel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kernel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acp-kernel",
"version": "0.0.201",
"version": "0.0.202",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
27 changes: 24 additions & 3 deletions kernel/src/wire/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export type OpenAIMessage = {
role: "system" | "developer" | "user" | "assistant" | "tool";
content?: string | null | OpenAIContentPart[];
reasoning_content?: string | null;
// #7432: some OpenAI-wire providers (Ollama Cloud, ...) echo the assistant
// thinking under `reasoning` instead of `reasoning_content`. Read side
// accepts the alias; the wire echo stays `reasoning_content` (coreToOpenai
// never emits `reasoning` — an upstream that drops a `reasoning` field would
// silently lose the CoT reuse).
reasoning?: string | null;
tool_calls?: OpenAIToolCall[];
tool_call_id?: string;
name?: string;
Expand Down Expand Up @@ -242,9 +248,24 @@ export function openaiToCore(body: OpenAIRequestBody): Flat {
break;
}
case "assistant": {
const rawReasoning = m.reasoning_content;
const fieldPresent = typeof rawReasoning === "string";
const fieldReasoning = fieldPresent ? rawReasoning : "";
// #7432: accept the reasoning alias (reasoning_content → reasoning),
// first non-empty wins. `fieldPresent` marks that the host sent the
// reasoning key (under either name, even empty) so coreToOpenai can
// re-emit it for strict-echo models. The write side stays
// reasoning_content — an upstream that silently drops a `reasoning`
// field would lose the CoT reuse.
let fieldPresent = false;
let fieldReasoning = "";
for (const f of ["reasoning_content", "reasoning"] as const) {
const v = m[f];
if (typeof v === "string") {
fieldPresent = true;
if (v.length > 0) {
fieldReasoning = v;
break;
}
}
}
let reasoning = fieldReasoning;
let text = stringContent(m.content);
if (!reasoning) {
Expand Down
38 changes: 38 additions & 0 deletions kernel/tests/wire-reasoning-echo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,44 @@ test("an absent reasoning_content field is not invented", () => {
assert.ok(!("reasoning_content" in wire[0]!), "no field, no echo");
});

// #7432: a provider that echoes the assistant thinking under `reasoning`
// (Ollama Cloud) instead of `reasoning_content` — the read side accepts the
// alias; the write side stays `reasoning_content` (an upstream that silently
// drops a `reasoning` field would lose the CoT reuse).
test("a `reasoning` alias is read and echoed back as reasoning_content", () => {
const wire = roundTrip([
{ role: "assistant", content: "the answer", reasoning: "step 1... step 2..." },
]);
const assistant = wire[0]!;
assert.equal(assistant.role, "assistant");
assert.strictEqual(
assistant.reasoning_content,
"step 1... step 2...",
"alias read, echoed as reasoning_content",
);
assert.ok(!("reasoning" in assistant), "no `reasoning` key on output");
});

test("a blank `reasoning` alias keeps the blank-echo contract", () => {
const wire = roundTrip([
{ role: "assistant", content: ".", reasoning: "" },
]);
const assistant = wire[0]!;
assert.ok("reasoning_content" in assistant, "blank alias re-emits the key");
assert.strictEqual(assistant.reasoning_content, "");
});

test("reasoning_content wins over the reasoning alias when both are present", () => {
const wire = roundTrip([
{ role: "assistant", content: "a", reasoning_content: "from rc", reasoning: "from r" },
]);
assert.strictEqual(
wire[0]!.reasoning_content,
"from rc",
"first non-empty in the chain wins",
);
});

test("an inline thinking block still wins over a blank field", () => {
const wire = roundTrip([
{
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 74 additions & 38 deletions src/loop/adapter-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,51 @@ const PROXY_TOOL_SET = new Set([
"bili_compress", "bili_decompress", "bili_search_context", "bili_status",
]);

// #7432: OpenAI-wire upstreams disagree on the streaming reasoning field name —
// the standard is `reasoning_content`, but Ollama Cloud (and some others) emit
// `delta.reasoning`. Read side accepts the alias (order mirrors the client's
// reasoningFields: reasoning_content → reasoning → reasoning_text); the frame
// forwarded to the client is normalized back to `reasoning_content` so the
// client-visible field name stays stable and the history echo never trips an
// upstream that silently drops a `reasoning` field (it accepts the echo only
// as `reasoning_content`).
const REASONING_FIELDS = ["reasoning_content", "reasoning", "reasoning_text"] as const;

function readReasoningField(delta: Record<string, unknown>): { value: string | null; source: string | null } {
for (const f of REASONING_FIELDS) {
const v = delta[f];
if (typeof v === "string" && v.length > 0) return { value: v, source: f };
}
return { value: null, source: null };
}

// #7432: rebuild the delta with the reasoning field normalized to
// `reasoning_content` (any alias field is deleted) and, when given, the content
// field set. `reasoning`/`content` of null leave that field untouched so a
// frame carrying only one of the two is not reshaped.
function normalizeProseFields(
parsed: Record<string, unknown>,
reasoning: string | null,
content: string | null,
): Record<string, unknown> {
const clone = { ...parsed } as { choices?: Array<Record<string, unknown>> };
if (clone.choices && clone.choices.length > 0) {
const choice = { ...(clone.choices[0] as Record<string, unknown>) };
const delta = { ...(choice.delta as Record<string, unknown>) };
if (reasoning !== null) {
for (const f of REASONING_FIELDS) delete delta[f];
if (reasoning.length > 0) delta.reasoning_content = reasoning;
}
if (content !== null) {
if (content.length > 0) delta.content = content;
else delete delta.content;
}
choice.delta = delta;
clone.choices = [choice, ...clone.choices.slice(1)];
}
return clone;
}

// Given a raw SSE chunk that carries tool_call fragments, decide how to
// replay it for a real-tool round: "keep" = every fragment belongs to a real
// call (forward verbatim), null = nothing real in it (drop), or a rewritten
Expand Down Expand Up @@ -67,10 +112,16 @@ function withheldProseFrame(parsed: Record<string, unknown>): Buffer | null {
const delta = choice?.delta as Record<string, unknown> | undefined;
if (!choice || !delta) return null;
const ct = typeof delta.content === "string" ? delta.content : "";
const rc = typeof delta.reasoning_content === "string" ? delta.reasoning_content : "";
if (ct.length === 0 && rc.length === 0) return null;
const { value: rc } = readReasoningField(delta);
if (ct.length === 0 && (rc === null || rc.length === 0)) return null;
const cleanDelta = { ...delta };
delete cleanDelta.tool_calls;
// #7432: normalize the reasoning field to reasoning_content so the client
// sees a stable field name (the upstream may have used an alias).
if (rc !== null && rc.length > 0) {
for (const f of REASONING_FIELDS) delete cleanDelta[f];
cleanDelta.reasoning_content = rc;
}
return Buffer.from("data: " + JSON.stringify({ ...parsed, choices: [{ ...choice, delta: cleanDelta }, ...(choices ?? []).slice(1)] }) + "\n\n", "utf8");
}

Expand Down Expand Up @@ -110,28 +161,6 @@ async function* iterSseChunks(stream: ReadableStream<Uint8Array>): AsyncGenerato
}
}

// Rebuild one SSE frame's choices[0].delta with cleaned field values (#1881):
// a filtered frame must reach the client with the CLEANED bytes — forwarding
// the original raw frame leaks the echo the filter just removed. Fields that
// filtered down to empty are dropped from the delta rather than sent as "".
function applyDeltaFields(parsed: Record<string, unknown>, fields: Record<string, string>): Record<string, unknown> {
const clone = { ...parsed } as { choices?: Array<Record<string, unknown>> };
if (clone.choices && clone.choices.length > 0) {
const choice = { ...(clone.choices[0] as Record<string, unknown>) };
const delta = { ...(choice.delta as Record<string, unknown>) };
for (const [k, v] of Object.entries(fields)) {
if (v === "") delete delta[k];
else delta[k] = v;
}
choice.delta = delta;
clone.choices = [choice, ...clone.choices.slice(1)];
}
return clone;
}
function rewriteDeltaFields(parsed: Record<string, unknown>, fields: Record<string, string>): Buffer {
return Buffer.from(`data: ${JSON.stringify(applyDeltaFields(parsed, fields))}\n\n`, "utf8");
}

// A chunk forwarded verbatim must never carry finish_reason: the ACP loop may run
// another upstream round after this one (core.ts reRequest), and the completion is
// emitted once, at the end, by emitCompletion. Leaking an early finish makes every
Expand Down Expand Up @@ -600,7 +629,10 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
// was filtered — a dual-field frame leaked unfiltered content
// to the client while the filtered text event suppressed its
// raw to avoid double-sending.
const rcIn = typeof delta.reasoning_content === "string" && delta.reasoning_content.length > 0 ? delta.reasoning_content : null;
// #7432: accept the reasoning alias (reasoning_content →
// reasoning → reasoning_text); rcSource drives the forward
// normalization below.
const { value: rcIn, source: rcSource } = readReasoningField(delta);
const ctIn = typeof delta.content === "string" && delta.content.length > 0 ? delta.content : null;
let rcOut = rcIn;
let ctOut = ctIn;
Expand All @@ -621,11 +653,12 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
// echo (#1881). Structured events stay internal here.
let json = jsonStr;
let obj = parsed;
if (rcChanged || ctChanged) {
const fields: Record<string, string> = {};
if (ctIn !== null) fields["content"] = ctOut ?? "";
if (rcIn !== null) fields["reasoning_content"] = rcOut ?? "";
obj = applyDeltaFields(parsed, fields);
// #7432: rebuild when a prose field was filtered OR the
// reasoning field arrived under a non-standard alias (the
// alias must be normalized to reasoning_content on the
// client-visible bytes).
if (rcChanged || ctChanged || (rcIn !== null && rcSource !== "reasoning_content")) {
obj = normalizeProseFields(parsed, rcIn !== null ? rcOut : null, ctIn !== null ? ctOut : null);
json = JSON.stringify(obj);
}
rawToolChunks.push({ json, parsed: obj });
Expand All @@ -639,11 +672,16 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
}

const fin = (b: Buffer): Buffer => (finishReason ? stripFinishReasonChunk(b) : b);
// #7432: when a prose field was filtered OR the reasoning field
// arrived under a non-standard alias, rebuild the client-visible
// frame with the reasoning field normalized to
// reasoning_content; otherwise forward the original bytes.
const needRebuild = rcChanged || ctChanged || (rcIn !== null && rcSource !== "reasoning_content");
const rawFwd = needRebuild
? Buffer.from(`data: ${JSON.stringify(normalizeProseFields(parsed, rcIn !== null ? rcOut : null, ctIn !== null ? ctOut : null))}\n\n`, "utf8")
: rawBuf;
if (rcIn !== null && ctIn !== null) {
const raw = rcChanged || ctChanged
? rewriteDeltaFields(parsed, { content: ctOut ?? "", reasoning_content: rcOut ?? "" })
: rawBuf;
const fwd = fin(raw);
const fwd = fin(rawFwd);
const hasText = ctOut !== null && ctOut.length > 0;
const hasReason = rcOut !== null && rcOut.length > 0;
// Both structured events feed the loop's internal state;
Expand All @@ -660,11 +698,9 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
yield { kind: "text", delta: ctOut!, raw: fwd } as ParsedStreamEvent;
}
} else if (rcIn !== null) {
const raw = rcChanged ? rewriteDeltaFields(parsed, { reasoning_content: rcOut ?? "" }) : rawBuf;
yield { kind: "reasoning", delta: rcOut ?? "", raw: fin(raw) } as ParsedStreamEvent;
yield { kind: "reasoning", delta: rcOut ?? "", raw: fin(rawFwd) } as ParsedStreamEvent;
} else if (ctIn !== null) {
const raw = ctChanged ? rewriteDeltaFields(parsed, { content: ctOut ?? "" }) : rawBuf;
yield { kind: "text", delta: ctOut ?? "", raw: fin(raw) } as ParsedStreamEvent;
yield { kind: "text", delta: ctOut ?? "", raw: fin(rawFwd) } as ParsedStreamEvent;
} else if (delta.role || (Object.keys(delta).length === 0 && !finishReason)) {
yield { kind: "meta", chunk: fin(rawBuf), firstRoundOnly: true } as ParsedStreamEvent;
}
Expand Down
Loading
Loading