mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 22:30:44 +00:00
77 lines
2.6 KiB
TypeScript
77 lines
2.6 KiB
TypeScript
import type { ProviderWrapStreamFnContext } from "openclaw/plugin-sdk/plugin-entry";
|
|
import { streamWithPayloadPatch } from "openclaw/plugin-sdk/provider-stream-shared";
|
|
|
|
type DeepSeekThinkingLevel = ProviderWrapStreamFnContext["thinkingLevel"];
|
|
|
|
function isDeepSeekV4ModelId(modelId: unknown): boolean {
|
|
return modelId === "deepseek-v4-flash" || modelId === "deepseek-v4-pro";
|
|
}
|
|
|
|
function isDisabledThinkingLevel(thinkingLevel: DeepSeekThinkingLevel): boolean {
|
|
const normalized = typeof thinkingLevel === "string" ? thinkingLevel.toLowerCase() : "";
|
|
return normalized === "off" || normalized === "none";
|
|
}
|
|
|
|
function resolveDeepSeekReasoningEffort(thinkingLevel: DeepSeekThinkingLevel): "high" | "max" {
|
|
return thinkingLevel === "xhigh" || thinkingLevel === "max" ? "max" : "high";
|
|
}
|
|
|
|
function stripDeepSeekReasoningContent(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 ensureDeepSeekToolCallReasoningContent(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 createDeepSeekV4ThinkingWrapper(
|
|
baseStreamFn: ProviderWrapStreamFnContext["streamFn"],
|
|
thinkingLevel: DeepSeekThinkingLevel,
|
|
): ProviderWrapStreamFnContext["streamFn"] {
|
|
if (!baseStreamFn) {
|
|
return undefined;
|
|
}
|
|
const underlying = baseStreamFn;
|
|
return (model, context, options) => {
|
|
if (model.provider !== "deepseek" || !isDeepSeekV4ModelId(model.id)) {
|
|
return underlying(model, context, options);
|
|
}
|
|
|
|
return streamWithPayloadPatch(underlying, model, context, options, (payload) => {
|
|
if (isDisabledThinkingLevel(thinkingLevel)) {
|
|
payload.thinking = { type: "disabled" };
|
|
delete payload.reasoning_effort;
|
|
delete payload.reasoning;
|
|
stripDeepSeekReasoningContent(payload);
|
|
return;
|
|
}
|
|
|
|
payload.thinking = { type: "enabled" };
|
|
payload.reasoning_effort = resolveDeepSeekReasoningEffort(thinkingLevel);
|
|
ensureDeepSeekToolCallReasoningContent(payload);
|
|
});
|
|
};
|
|
}
|