openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
130 lines (129 loc) • 4.62 kB
JavaScript
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs";
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { r as createLazyRuntimeModule } from "./lazy-runtime-zGjszCtt.mjs";
//#region src/llm/providers/stream-wrappers/moonshot-thinking.ts
const loadDefaultStream = createLazyRuntimeModule(() => import("./stream-CKfKzN0I.mjs"));
const MOONSHOT_ALWAYS_THINKING = {
"kimi-k2.7-code": "low",
"kimi-k2.7-code-highspeed": "low",
"kimi-k3": "max",
"kimi-k3.1-pro": "max",
"kimi-k3.1-pro-highspeed": "max"
};
const FIXED_SAMPLING_FIELDS = "temperature top_p n presence_penalty frequency_penalty".split(" ");
function normalizeMoonshotThinkingType(e) {
const t = asOptionalRecord(e)?.type ?? e;
if (typeof t === "boolean") return t ? "enabled" : "disabled";
const n = normalizeOptionalLowercaseString(t);
if ([
"enabled",
"enable",
"on",
"true"
].includes(n ?? "")) return "enabled";
if ([
"disabled",
"disable",
"off",
"false"
].includes(n ?? "")) return "disabled";
}
function isMoonshotToolChoiceCompatible(e) {
const t = asOptionalRecord(e)?.type ?? e;
return t == null || t === "auto" || t === "none";
}
function ensureMoonshotToolCallReasoningContent(e) {
const t = Array.isArray(e.messages) ? e.messages : [];
for (const e of t) {
const t = asOptionalRecord(e);
if (t?.role !== "assistant" || !Array.isArray(t.tool_calls)) continue;
if (t.tool_calls.length > 0 && !("reasoning_content" in t)) t.reasoning_content = "";
}
}
function resolveAlwaysThinkingEffort(e, t) {
const n = MOONSHOT_ALWAYS_THINKING[e];
return n && (n !== "max" || t) ? n : void 0;
}
function sanitizeAlwaysThinkingPayload(e, t) {
delete e.thinking;
delete e.reasoningEffort;
FIXED_SAMPLING_FIELDS.forEach((t) => Reflect.deleteProperty(e, t));
if (t === "max") e.reasoning_effort = t;
else {
delete e.reasoning_effort;
if (!isMoonshotToolChoiceCompatible(e.tool_choice)) e.tool_choice = "auto";
}
}
function prepareThinkingPayload(e, t, n, r, i) {
const a = typeof e.model === "string" ? e.model.trim().toLowerCase() : t;
let o = normalizeMoonshotThinkingType(e.thinking);
if (r) {
e.thinking = { type: r };
o = r;
}
const s = resolveAlwaysThinkingEffort(a, n);
if (s) {
sanitizeAlwaysThinkingPayload(e, s);
return (e) => {
sanitizeAlwaysThinkingPayload(e, s);
ensureMoonshotToolCallReasoningContent(e);
};
}
if (o === "enabled" && !isMoonshotToolChoiceCompatible(e.tool_choice)) {
const t = asOptionalRecord(e.tool_choice)?.type;
if (e.tool_choice === "required") e.tool_choice = "auto";
else if (t === "tool" || t === "function") {
e.thinking = { type: "disabled" };
o = "disabled";
}
}
const c = asOptionalRecord(e.thinking);
const l = a === "kimi-k2.6" && o === "enabled" && i === "all";
if (c) {
delete c.keep;
Object.assign(c, l ? { keep: "all" } : {});
}
return o === "enabled" ? ensureMoonshotToolCallReasoningContent : () => void 0;
}
/** @deprecated Moonshot provider-owned stream helper; do not use from third-party plugins. */
function resolveMoonshotThinkingType(e) {
return normalizeMoonshotThinkingType(e.configuredThinking) ?? (e.thinkingLevel ? e.thinkingLevel === "off" ? "disabled" : "enabled" : void 0);
}
/** @deprecated Moonshot provider-owned stream helper; do not use from third-party plugins. */
function resolveMoonshotThinkingKeep(e) {
return normalizeOptionalLowercaseString(asOptionalRecord(e.configuredThinking)?.keep) === "all" ? "all" : void 0;
}
/** @deprecated Moonshot provider-owned stream helper; do not use from third-party plugins. */
function createMoonshotThinkingWrapper(e, t, n, r) {
const i = e ?? (async (...e) => (await loadDefaultStream()).streamSimple(...e));
return function moonshotThinkingStream(e, a, o) {
const s = e.id.trim().toLowerCase();
const c = normalizeOptionalLowercaseString(e.provider) === "moonshot";
const l = resolveAlwaysThinkingEffort(s, c);
const u = l ? {
...e,
reasoning: true
} : e;
const d = l ? {
...o,
reasoning: l
} : o;
return i(u, a, {
...d,
onPayload(e, i) {
const a = asOptionalRecord(e);
if (!a) return d?.onPayload?.(e, i);
const o = prepareThinkingPayload(a, s, c, t, n);
const finish = (e) => {
const t = asOptionalRecord(e) ?? a;
o(t);
return r ? r(e, t) : e;
};
const l = d?.onPayload?.(e, i);
return l && typeof l.then === "function" ? Promise.resolve(l).then(finish) : finish(l);
}
});
};
}
//#endregion
export { resolveMoonshotThinkingKeep as n, resolveMoonshotThinkingType as r, createMoonshotThinkingWrapper as t };