fix(opencode-go): route DeepSeek V4 through OpenAI transport

This commit is contained in:
Peter Steinberger
2026-04-25 18:56:52 +01:00
parent 275c128e99
commit afd6b5d6fc
11 changed files with 161 additions and 77 deletions

View File

@@ -141,6 +141,77 @@ export function createPayloadPatchStreamWrapper(
);
}
export type DeepSeekV4ThinkingLevel = ProviderWrapStreamFnContext["thinkingLevel"];
function isDisabledDeepSeekV4ThinkingLevel(thinkingLevel: DeepSeekV4ThinkingLevel): boolean {
const normalized = typeof thinkingLevel === "string" ? thinkingLevel.toLowerCase() : "";
return normalized === "off" || normalized === "none";
}
function resolveDeepSeekV4ReasoningEffort(thinkingLevel: DeepSeekV4ThinkingLevel): "high" | "max" {
return thinkingLevel === "xhigh" || thinkingLevel === "max" ? "max" : "high";
}
function stripDeepSeekV4ReasoningContent(payload: Record<string, unknown>): void {
if (!Array.isArray(payload.messages)) {
return;
}
for (const message of payload.messages) {
if (!message || typeof message !== "object") {
continue;
}
delete (message as Record<string, unknown>).reasoning_content;
}
}
function ensureDeepSeekV4ToolCallReasoningContent(payload: Record<string, unknown>): void {
if (!Array.isArray(payload.messages)) {
return;
}
for (const message of payload.messages) {
if (!message || typeof message !== "object") {
continue;
}
const record = message as Record<string, unknown>;
if (record.role !== "assistant" || !Array.isArray(record.tool_calls)) {
continue;
}
if (!("reasoning_content" in record)) {
record.reasoning_content = "";
}
}
}
export function createDeepSeekV4OpenAICompatibleThinkingWrapper(params: {
baseStreamFn: StreamFn | undefined;
thinkingLevel: DeepSeekV4ThinkingLevel;
shouldPatchModel: (model: Parameters<StreamFn>[0]) => boolean;
}): StreamFn | undefined {
if (!params.baseStreamFn) {
return undefined;
}
const underlying = params.baseStreamFn;
return (model, context, options) => {
if (!params.shouldPatchModel(model)) {
return underlying(model, context, options);
}
return streamWithPayloadPatch(underlying, model, context, options, (payload) => {
if (isDisabledDeepSeekV4ThinkingLevel(params.thinkingLevel)) {
payload.thinking = { type: "disabled" };
delete payload.reasoning_effort;
delete payload.reasoning;
stripDeepSeekV4ReasoningContent(payload);
return;
}
payload.thinking = { type: "enabled" };
payload.reasoning_effort = resolveDeepSeekV4ReasoningEffort(params.thinkingLevel);
ensureDeepSeekV4ToolCallReasoningContent(payload);
});
};
}
export type GoogleThinkingLevel = "MINIMAL" | "LOW" | "MEDIUM" | "HIGH";
export type GoogleThinkingInputLevel =
| "off"