|
| 1 | +import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime"; |
| 2 | +import type { |
| 3 | +OllamaVisibleContentSanitizer, |
| 4 | +OllamaVisibleContentStreamResolution, |
| 5 | +} from "./visible-content-contract.js"; |
| 6 | + |
| 7 | +const INLINE_REASONING_MIN_PREFIX_CHARS = 80; |
| 8 | +const INLINE_REASONING_MAX_PENDING_CHARS = 512; |
| 9 | +const INLINE_REASONING_BOUNDARY_RE = /(^|\s)\uFE0F\s*/u; |
| 10 | + |
| 11 | +type InlineReasoningVisibleTextResolution = |
| 12 | +| { kind: "visible"; text: string; bypassInlineReasoning?: boolean } |
| 13 | +| { kind: "pending" }; |
| 14 | + |
| 15 | +export function isOllamaCloudKimiModelRef(modelId: string): boolean { |
| 16 | +const normalizedModelId = normalizeLowercaseStringOrEmpty(modelId); |
| 17 | +const slashIndex = normalizedModelId.indexOf("/"); |
| 18 | +const normalizedWireModelId = |
| 19 | +slashIndex === -1 ? normalizedModelId : normalizedModelId.slice(slashIndex + 1); |
| 20 | +return normalizedWireModelId.startsWith("kimi-k") && normalizedWireModelId.includes(":cloud"); |
| 21 | +} |
| 22 | + |
| 23 | +function resolveInlineReasoningVisibleText(params: { |
| 24 | +text: string; |
| 25 | +final: boolean; |
| 26 | +}): InlineReasoningVisibleTextResolution { |
| 27 | +const match = INLINE_REASONING_BOUNDARY_RE.exec(params.text); |
| 28 | +if (!match) { |
| 29 | +if (!params.final && params.text.length <= INLINE_REASONING_MAX_PENDING_CHARS) { |
| 30 | +return { kind: "pending" }; |
| 31 | +} |
| 32 | +return { |
| 33 | +kind: "visible", |
| 34 | +text: params.text, |
| 35 | +bypassInlineReasoning: |
| 36 | +!params.final && params.text.length > INLINE_REASONING_MAX_PENDING_CHARS, |
| 37 | +}; |
| 38 | +} |
| 39 | + |
| 40 | +const boundaryStartIndex = match.index + match[1].length; |
| 41 | +const boundaryEndIndex = match.index + match[0].length; |
| 42 | +const prefix = params.text.slice(0, boundaryStartIndex).trim(); |
| 43 | +const answer = params.text.slice(boundaryEndIndex).trim(); |
| 44 | +if (prefix.length >= INLINE_REASONING_MIN_PREFIX_CHARS) { |
| 45 | +return { kind: "visible", text: answer }; |
| 46 | +} |
| 47 | + |
| 48 | +return params.final ? { kind: "visible", text: params.text } : { kind: "pending" }; |
| 49 | +} |
| 50 | + |
| 51 | +export function createKimiInlineReasoningSanitizer(): OllamaVisibleContentSanitizer { |
| 52 | +let bypassInlineReasoning = false; |
| 53 | + |
| 54 | +return { |
| 55 | +resolveStreamText(params): OllamaVisibleContentStreamResolution { |
| 56 | +if (bypassInlineReasoning) { |
| 57 | +return { kind: "visible", text: params.text }; |
| 58 | +} |
| 59 | + |
| 60 | +const resolution = resolveInlineReasoningVisibleText(params); |
| 61 | +if (resolution.kind === "pending") { |
| 62 | +return resolution; |
| 63 | +} |
| 64 | +if (resolution.bypassInlineReasoning) { |
| 65 | +bypassInlineReasoning = true; |
| 66 | +} |
| 67 | +return { kind: "visible", text: resolution.text }; |
| 68 | +}, |
| 69 | +sanitizeFinalText(text) { |
| 70 | +const resolution = resolveInlineReasoningVisibleText({ text, final: true }); |
| 71 | +return resolution.kind === "visible" ? resolution.text : text; |
| 72 | +}, |
| 73 | +}; |
| 74 | +} |