|
1 | 1 | import type { ProviderWrapStreamFnContext } from "openclaw/plugin-sdk/plugin-entry"; |
2 | | -import { createDeepSeekV4OpenAICompatibleThinkingWrapper } from "openclaw/plugin-sdk/provider-stream-shared"; |
| 2 | +import { streamWithPayloadPatch } from "openclaw/plugin-sdk/provider-stream-shared"; |
3 | 3 |
|
4 | 4 | function isOpencodeGoDeepSeekV4ModelId(modelId: unknown): boolean { |
5 | 5 | return modelId === "deepseek-v4-flash" || modelId === "deepseek-v4-pro"; |
6 | 6 | } |
7 | 7 |
|
| 8 | +function isDisabledDeepSeekV4ThinkingLevel(thinkingLevel: unknown): boolean { |
| 9 | + const normalized = typeof thinkingLevel === "string" ? thinkingLevel.toLowerCase() : ""; |
| 10 | + return normalized === "off" || normalized === "none"; |
| 11 | +} |
| 12 | + |
8 | 13 | export function createOpencodeGoDeepSeekV4Wrapper( |
9 | 14 | baseStreamFn: ProviderWrapStreamFnContext["streamFn"], |
10 | 15 | thinkingLevel: ProviderWrapStreamFnContext["thinkingLevel"], |
11 | 16 | ): ProviderWrapStreamFnContext["streamFn"] { |
12 | | - return createDeepSeekV4OpenAICompatibleThinkingWrapper({ |
13 | | - baseStreamFn, |
14 | | - thinkingLevel, |
15 | | - shouldPatchModel: (model) => |
16 | | - model.provider === "opencode-go" && isOpencodeGoDeepSeekV4ModelId(model.id), |
17 | | - }); |
| 17 | + if (!baseStreamFn) return undefined; |
| 18 | + const underlying = baseStreamFn; |
| 19 | + return (model, context, options) => { |
| 20 | + if (!(model.provider === "opencode-go" && isOpencodeGoDeepSeekV4ModelId(model.id))) { |
| 21 | + return underlying(model, context, options); |
| 22 | + } |
| 23 | + return streamWithPayloadPatch(underlying, model, context, options, (payload) => { |
| 24 | + // Handle disabled thinking |
| 25 | + if (isDisabledDeepSeekV4ThinkingLevel(thinkingLevel)) { |
| 26 | + payload.thinking = { type: "disabled" }; |
| 27 | + delete payload.reasoning_effort; |
| 28 | + delete payload.reasoning; |
| 29 | + if (Array.isArray(payload.messages)) { |
| 30 | + for (const msg of payload.messages) { |
| 31 | + if (msg && typeof msg === "object") { |
| 32 | + delete (msg as Record<string, unknown>).reasoning_content; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Use model.thinkingLevelMap if available, otherwise fall back to hardcoded mapping |
| 40 | + const map = (model as unknown as Record<string, unknown>).thinkingLevelMap as |
| 41 | + | Record<string, string | null> |
| 42 | + | undefined; |
| 43 | + const mappedEffort = |
| 44 | + typeof map === "object" && map !== null ? map[String(thinkingLevel)] : undefined; |
| 45 | + const reasoningEffort = |
| 46 | + typeof mappedEffort === "string" |
| 47 | + ? mappedEffort |
| 48 | + : thinkingLevel === "xhigh" || thinkingLevel === "max" |
| 49 | + ? "max" |
| 50 | + : "high"; |
| 51 | + |
| 52 | + payload.thinking = { type: "enabled" }; |
| 53 | + payload.reasoning_effort = reasoningEffort; |
| 54 | + |
| 55 | + // Backfill reasoning_content on assistant messages |
| 56 | + if (Array.isArray(payload.messages)) { |
| 57 | + for (const message of payload.messages) { |
| 58 | + if ( |
| 59 | + message && |
| 60 | + typeof message === "object" && |
| 61 | + (message as Record<string, unknown>).role === "assistant" |
| 62 | + ) { |
| 63 | + const record = message as Record<string, unknown>; |
| 64 | + if (!("reasoning_content" in record)) { |
| 65 | + record.reasoning_content = ""; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + }; |
18 | 72 | } |
0 commit comments