feat(thinking): return Claude thinking text to OpenAI-format clients

This commit is contained in:
MrBeanDev
2026-09-26 11:57:41 +07:00
parent fe347e4ea5
commit 90b0693423
3 changed files with 108 additions and 4 deletions

View File

@@ -102,9 +102,28 @@ export function extractThinking(body) {
return null;
}
// Capture thinking intent from a body. Alias of extractThinking, named for clarity
// at the call-site where intent is snapshotted before format translation.
export const captureThinking = extractThinking;
// Capture thinking intent from a body before format translation strips it.
// Besides the effort, records whether an OpenAI-shaped client wants the thinking
// text itself: Claude returns it only with thinking.display "summarized", a field
// OpenAI has no equivalent for, so the intent cannot survive translation on its own.
export function captureThinking(body) {
const cfg = extractThinking(body);
if (!cfg || cfg.mode === "none") return cfg;
const display = openAIThinkingDisplay(body);
return display ? { ...cfg, display } : cfg;
}
function openAIThinkingDisplay(body) {
// Responses API: reasoning.summary is the explicit request for reasoning text.
if (body.reasoning && typeof body.reasoning === "object") {
const summary = body.reasoning.summary;
return typeof summary === "string" && summary && summary !== "none" ? "summarized" : undefined;
}
// Chat Completions has no summary knob. A client setting reasoning_effort is
// asking for reasoning, and reasoning_content is how it would receive it.
if (typeof body.reasoning_effort === "string") return "summarized";
return undefined;
}
const NATIVE_ONLY_FORMATS = new Set(["gemini-level", "gemini-budget", "claude-budget", "claude-adaptive", "kiro"]);
@@ -383,7 +402,8 @@ export function applyThinking(targetFormat, model, body, provider = null, intent
const supportedLevels = getThinkingLevels(provider, cleanModel);
// Anthropic's `display` (summarized | omitted) decides whether thinking text
// comes back at all; keep what the client asked for instead of resetting it.
const display = typeof body.thinking?.display === "string" ? body.thinking.display : undefined;
// An OpenAI-shaped client's ask arrives via the captured intent instead.
const display = typeof body.thinking?.display === "string" ? body.thinking.display : intent?.display;
stripAll(body);
applyFormat(fmt, body, cfg, caps, supportedLevels, display);
return body;

View File

@@ -119,6 +119,7 @@ exports[`GOLDEN request: OpenAI → Claude > reasoning_effort → adaptive outpu
},
],
"thinking": {
"display": "summarized",
"type": "adaptive",
},
}

View File

@@ -0,0 +1,83 @@
// OpenAI-format clients asking for reasoning get Claude's thinking text back.
//
// Claude only returns thinking text when the request sets thinking.display to
// "summarized" — otherwise the redact-thinking beta is sent and every thinking
// block comes back signature-only. That field has no OpenAI equivalent, so an
// OpenAI-format client (opencode, DeepSeek Harness, Cherry Studio, ...) could never
// see its reasoning: reasoning_content stayed empty however high the effort was.
//
// The client's intent is read from the pre-translation body:
// - Chat Completions: setting reasoning_effort is the request for reasoning.
// - Responses API: reasoning.summary is OpenAI's explicit ask for summaries.
// Claude-format clients are untouched — they set display themselves.
import { describe, it, expect } from "vitest";
import "./registerAll.js";
import { translateRequest } from "../../open-sse/translator/index.js";
import { FORMATS } from "../../open-sse/translator/formats.js";
import { selectAnthropicBeta } from "../../open-sse/providers/shared.js";
const REDACT = "redact-thinking-2026-02-12";
const MODEL = "claude-opus-5";
function toClaude(sourceFormat, body) {
return translateRequest(sourceFormat, FORMATS.CLAUDE, MODEL, body, true, null, "claude");
}
const chat = (extra = {}) => ({ model: MODEL, messages: [{ role: "user", content: "Is 391 prime?" }], ...extra });
const responses = (extra = {}) => ({ model: MODEL, input: [{ role: "user", content: "Is 391 prime?" }], ...extra });
describe("Chat Completions clients", () => {
it("reasoning_effort asks Claude for summarized thinking and drops redact-thinking", () => {
const out = toClaude(FORMATS.OPENAI, chat({ reasoning_effort: "high" }));
expect(out.thinking.display).toBe("summarized");
expect(selectAnthropicBeta(MODEL, out)).not.toContain(REDACT);
});
it("reasoning_effort none leaves thinking off and keeps redact-thinking", () => {
const out = toClaude(FORMATS.OPENAI, chat({ reasoning_effort: "none" }));
expect(out.thinking?.display).toBeUndefined();
expect(selectAnthropicBeta(MODEL, out)).toContain(REDACT);
});
it("no reasoning_effort changes nothing", () => {
const out = toClaude(FORMATS.OPENAI, chat());
expect(out.thinking?.display).toBeUndefined();
expect(selectAnthropicBeta(MODEL, out)).toContain(REDACT);
});
});
describe("Responses API clients", () => {
it("reasoning.summary asks Claude for summarized thinking", () => {
const out = toClaude(FORMATS.OPENAI_RESPONSES, responses({ reasoning: { effort: "high", summary: "auto" } }));
expect(out.thinking.display).toBe("summarized");
expect(selectAnthropicBeta(MODEL, out)).not.toContain(REDACT);
});
it("effort without summary keeps thinking text redacted, as OpenAI would", () => {
const out = toClaude(FORMATS.OPENAI_RESPONSES, responses({ reasoning: { effort: "high" } }));
expect(out.thinking?.display).toBeUndefined();
expect(selectAnthropicBeta(MODEL, out)).toContain(REDACT);
});
});
describe("Claude-format clients are untouched", () => {
it("thinking without display stays redacted", () => {
const out = toClaude(FORMATS.CLAUDE, {
model: MODEL, max_tokens: 4096,
thinking: { type: "enabled", budget_tokens: 2048 },
messages: [{ role: "user", content: "Is 391 prime?" }],
});
expect(out.thinking?.display).toBeUndefined();
expect(selectAnthropicBeta(MODEL, out)).toContain(REDACT);
});
it("an explicit display is kept as sent", () => {
const out = toClaude(FORMATS.CLAUDE, {
model: MODEL, max_tokens: 4096,
thinking: { type: "enabled", budget_tokens: 2048, display: "omitted" },
messages: [{ role: "user", content: "Is 391 prime?" }],
});
expect(out.thinking.display).toBe("omitted");
expect(selectAnthropicBeta(MODEL, out)).toContain(REDACT);
});
});