Vendor OpenClaw source as Adolf fork baseline
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
This commit is contained in:
449
extensions/kimi-coding/stream.ts
Normal file
449
extensions/kimi-coding/stream.ts
Normal file
@@ -0,0 +1,449 @@
|
||||
// Kimi Coding plugin module implements stream behavior.
|
||||
import type { StreamFn } from "openclaw/plugin-sdk/agent-core";
|
||||
import {
|
||||
streamSimple,
|
||||
type AssistantMessage,
|
||||
type AssistantMessageEvent,
|
||||
} from "openclaw/plugin-sdk/llm";
|
||||
import type { ProviderWrapStreamFnContext } from "openclaw/plugin-sdk/plugin-entry";
|
||||
import { streamWithPayloadPatch } from "openclaw/plugin-sdk/provider-stream-shared";
|
||||
import { normalizeOptionalLowercaseString } from "openclaw/plugin-sdk/string-coerce-runtime";
|
||||
|
||||
const TOOL_CALLS_SECTION_BEGIN = "<|tool_calls_section_begin|>";
|
||||
const TOOL_CALLS_SECTION_END = "<|tool_calls_section_end|>";
|
||||
const TOOL_CALL_BEGIN = "<|tool_call_begin|>";
|
||||
const TOOL_CALL_ARGUMENT_BEGIN = "<|tool_call_argument_begin|>";
|
||||
const TOOL_CALL_END = "<|tool_call_end|>";
|
||||
|
||||
type KimiToolCallBlock = {
|
||||
type: "toolCall";
|
||||
id: string;
|
||||
name: string;
|
||||
arguments: Record<string, unknown>;
|
||||
};
|
||||
|
||||
type KimiThinkingType = "enabled" | "disabled";
|
||||
interface MutableAssistantMessageEventStream extends AsyncIterable<AssistantMessageEvent> {
|
||||
result: () => Promise<AssistantMessage>;
|
||||
}
|
||||
type KimiThinkingConfig = {
|
||||
type: KimiThinkingType;
|
||||
budget_tokens?: number;
|
||||
};
|
||||
type KimiThinkingLevel =
|
||||
| "off"
|
||||
| "minimal"
|
||||
| "low"
|
||||
| "medium"
|
||||
| "high"
|
||||
| "xhigh"
|
||||
| "adaptive"
|
||||
| "max";
|
||||
|
||||
const KIMI_ANTHROPIC_THINKING_BUDGETS: Record<Exclude<KimiThinkingLevel, "off">, number> = {
|
||||
minimal: 1024,
|
||||
low: 1024,
|
||||
medium: 4096,
|
||||
high: 8192,
|
||||
adaptive: 8192,
|
||||
xhigh: 8192,
|
||||
max: 8192,
|
||||
};
|
||||
const KIMI_ANTHROPIC_VISIBLE_OUTPUT_RESERVE_TOKENS = 1024;
|
||||
const KIMI_ANTHROPIC_MIN_OUTPUT_TOKENS = 16000;
|
||||
|
||||
function normalizeKimiThinkingBudgetTokens(value: unknown): number | undefined {
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) {
|
||||
return undefined;
|
||||
}
|
||||
const normalized = Math.floor(value);
|
||||
return normalized >= 1024 ? normalized : undefined;
|
||||
}
|
||||
|
||||
function normalizeKimiAnthropicMaxTokens(value: unknown): number | undefined {
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) {
|
||||
return undefined;
|
||||
}
|
||||
const normalized = Math.floor(value);
|
||||
return normalized > 0 ? normalized : undefined;
|
||||
}
|
||||
|
||||
function ensureKimiAnthropicMaxTokens(
|
||||
payloadObj: Record<string, unknown>,
|
||||
thinkingConfig: KimiThinkingConfig,
|
||||
): void {
|
||||
if (thinkingConfig.type !== "enabled" || thinkingConfig.budget_tokens === undefined) {
|
||||
return;
|
||||
}
|
||||
const required = Math.max(
|
||||
KIMI_ANTHROPIC_MIN_OUTPUT_TOKENS,
|
||||
thinkingConfig.budget_tokens + KIMI_ANTHROPIC_VISIBLE_OUTPUT_RESERVE_TOKENS,
|
||||
);
|
||||
const current = normalizeKimiAnthropicMaxTokens(payloadObj.max_tokens);
|
||||
payloadObj.max_tokens = current === undefined ? required : Math.max(current, required);
|
||||
}
|
||||
|
||||
function messageHasOpenAIToolCalls(message: Record<string, unknown>): boolean {
|
||||
return Array.isArray(message.tool_calls) && message.tool_calls.length > 0;
|
||||
}
|
||||
|
||||
function ensureKimiOpenAIReasoningContent(payloadObj: Record<string, unknown>): void {
|
||||
if (!Array.isArray(payloadObj.messages)) {
|
||||
return;
|
||||
}
|
||||
for (const message of payloadObj.messages) {
|
||||
if (!message || typeof message !== "object") {
|
||||
continue;
|
||||
}
|
||||
const record = message as Record<string, unknown>;
|
||||
if (record.role !== "assistant" || !messageHasOpenAIToolCalls(record)) {
|
||||
continue;
|
||||
}
|
||||
if (!("reasoning_content" in record)) {
|
||||
record.reasoning_content = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function stripKimiOpenAIReasoningContent(payloadObj: Record<string, unknown>): void {
|
||||
if (!Array.isArray(payloadObj.messages)) {
|
||||
return;
|
||||
}
|
||||
for (const message of payloadObj.messages) {
|
||||
if (message && typeof message === "object") {
|
||||
delete (message as Record<string, unknown>).reasoning_content;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeKimiThinkingType(value: unknown): KimiThinkingType | undefined {
|
||||
if (typeof value === "boolean") {
|
||||
return value ? "enabled" : "disabled";
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
const normalized = normalizeOptionalLowercaseString(value);
|
||||
if (!normalized) {
|
||||
return undefined;
|
||||
}
|
||||
if (["enabled", "enable", "on", "true"].includes(normalized)) {
|
||||
return "enabled";
|
||||
}
|
||||
if (["disabled", "disable", "off", "false"].includes(normalized)) {
|
||||
return "disabled";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
if (value && typeof value === "object" && !Array.isArray(value)) {
|
||||
return normalizeKimiThinkingType((value as Record<string, unknown>).type);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function normalizeKimiThinkingConfig(value: unknown): KimiThinkingConfig | undefined {
|
||||
const type = normalizeKimiThinkingType(value);
|
||||
if (!type) {
|
||||
return undefined;
|
||||
}
|
||||
if (type === "disabled") {
|
||||
return { type: "disabled" };
|
||||
}
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return { type: "enabled" };
|
||||
}
|
||||
const record = value as Record<string, unknown>;
|
||||
const budgetTokens = normalizeKimiThinkingBudgetTokens(
|
||||
record.budget_tokens ?? record.budgetTokens,
|
||||
);
|
||||
return budgetTokens === undefined
|
||||
? { type: "enabled" }
|
||||
: { type: "enabled", budget_tokens: budgetTokens };
|
||||
}
|
||||
|
||||
function resolveKimiAnthropicThinkingBudgetTokens(
|
||||
thinkingLevel: KimiThinkingLevel | undefined,
|
||||
): number | undefined {
|
||||
if (!thinkingLevel || thinkingLevel === "off") {
|
||||
return undefined;
|
||||
}
|
||||
return KIMI_ANTHROPIC_THINKING_BUDGETS[thinkingLevel];
|
||||
}
|
||||
|
||||
export function resolveKimiThinkingConfig(params: {
|
||||
configuredThinking: unknown;
|
||||
thinkingLevel?: KimiThinkingLevel;
|
||||
}): KimiThinkingConfig {
|
||||
const configured = normalizeKimiThinkingConfig(params.configuredThinking);
|
||||
const levelBudgetTokens = resolveKimiAnthropicThinkingBudgetTokens(params.thinkingLevel);
|
||||
if (configured) {
|
||||
return configured.type === "enabled" && configured.budget_tokens === undefined
|
||||
? { type: "enabled", budget_tokens: levelBudgetTokens ?? 1024 }
|
||||
: configured;
|
||||
}
|
||||
if (!params.thinkingLevel || params.thinkingLevel === "off") {
|
||||
return { type: "disabled" };
|
||||
}
|
||||
return levelBudgetTokens === undefined
|
||||
? { type: "enabled" }
|
||||
: { type: "enabled", budget_tokens: levelBudgetTokens };
|
||||
}
|
||||
|
||||
export function resolveKimiThinkingType(params: {
|
||||
configuredThinking: unknown;
|
||||
thinkingLevel?: KimiThinkingLevel;
|
||||
}): KimiThinkingType {
|
||||
return resolveKimiThinkingConfig(params).type;
|
||||
}
|
||||
|
||||
function stripTaggedToolCallCounter(value: string): string {
|
||||
return value.trim().replace(/:\d+$/, "");
|
||||
}
|
||||
|
||||
function parseKimiTaggedToolCalls(text: string): KimiToolCallBlock[] | null {
|
||||
const trimmed = text.trim();
|
||||
// Kimi emits tagged tool-call sections as standalone text blocks on this path.
|
||||
if (!trimmed.startsWith(TOOL_CALLS_SECTION_BEGIN) || !trimmed.endsWith(TOOL_CALLS_SECTION_END)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let cursor = TOOL_CALLS_SECTION_BEGIN.length;
|
||||
const sectionEndIndex = trimmed.length - TOOL_CALLS_SECTION_END.length;
|
||||
const toolCalls: KimiToolCallBlock[] = [];
|
||||
|
||||
while (cursor < sectionEndIndex) {
|
||||
while (cursor < sectionEndIndex && /\s/.test(trimmed[cursor] ?? "")) {
|
||||
cursor += 1;
|
||||
}
|
||||
if (cursor >= sectionEndIndex) {
|
||||
break;
|
||||
}
|
||||
if (!trimmed.startsWith(TOOL_CALL_BEGIN, cursor)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const nameStart = cursor + TOOL_CALL_BEGIN.length;
|
||||
const argMarkerIndex = trimmed.indexOf(TOOL_CALL_ARGUMENT_BEGIN, nameStart);
|
||||
if (argMarkerIndex < 0 || argMarkerIndex >= sectionEndIndex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rawId = trimmed.slice(nameStart, argMarkerIndex).trim();
|
||||
if (!rawId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const argsStart = argMarkerIndex + TOOL_CALL_ARGUMENT_BEGIN.length;
|
||||
const callEndIndex = trimmed.indexOf(TOOL_CALL_END, argsStart);
|
||||
if (callEndIndex < 0 || callEndIndex > sectionEndIndex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rawArgs = trimmed.slice(argsStart, callEndIndex).trim();
|
||||
let parsedArgs: unknown;
|
||||
try {
|
||||
parsedArgs = JSON.parse(rawArgs);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (!parsedArgs || typeof parsedArgs !== "object" || Array.isArray(parsedArgs)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const name = stripTaggedToolCallCounter(rawId);
|
||||
if (!name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
toolCalls.push({
|
||||
type: "toolCall",
|
||||
id: rawId,
|
||||
name,
|
||||
arguments: parsedArgs as Record<string, unknown>,
|
||||
});
|
||||
|
||||
cursor = callEndIndex + TOOL_CALL_END.length;
|
||||
}
|
||||
|
||||
return toolCalls.length > 0 ? toolCalls : null;
|
||||
}
|
||||
|
||||
function rewriteKimiTaggedToolCallsInMessage(message: unknown): void {
|
||||
if (!message || typeof message !== "object") {
|
||||
return;
|
||||
}
|
||||
|
||||
const content = (message as { content?: unknown }).content;
|
||||
if (!Array.isArray(content)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let changed = false;
|
||||
const nextContent: unknown[] = [];
|
||||
for (const block of content) {
|
||||
if (!block || typeof block !== "object") {
|
||||
nextContent.push(block);
|
||||
continue;
|
||||
}
|
||||
const typedBlock = block as { type?: unknown; text?: unknown };
|
||||
if (typedBlock.type !== "text" || typeof typedBlock.text !== "string") {
|
||||
nextContent.push(block);
|
||||
continue;
|
||||
}
|
||||
|
||||
const parsed = parseKimiTaggedToolCalls(typedBlock.text);
|
||||
if (!parsed) {
|
||||
nextContent.push(block);
|
||||
continue;
|
||||
}
|
||||
|
||||
nextContent.push(...parsed);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (!changed) {
|
||||
return;
|
||||
}
|
||||
|
||||
(message as { content: unknown[] }).content = nextContent;
|
||||
const typedMessage = message as { stopReason?: unknown };
|
||||
if (typedMessage.stopReason === "stop") {
|
||||
typedMessage.stopReason = "toolUse";
|
||||
}
|
||||
}
|
||||
|
||||
function transformKimiStreamEvent(
|
||||
value: unknown,
|
||||
transformMessage: (message: unknown) => void,
|
||||
): void {
|
||||
const event =
|
||||
value && typeof value === "object"
|
||||
? (value as { partial?: unknown; message?: unknown })
|
||||
: undefined;
|
||||
if (!event) {
|
||||
return;
|
||||
}
|
||||
for (const message of [event.partial, event.message]) {
|
||||
transformMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
function wrapStreamMessageObjects(
|
||||
stream: MutableAssistantMessageEventStream,
|
||||
transformMessage: (message: unknown) => void,
|
||||
): MutableAssistantMessageEventStream {
|
||||
const readFinalMessage = stream.result.bind(stream);
|
||||
Object.assign(stream, {
|
||||
async result() {
|
||||
const message = await readFinalMessage();
|
||||
transformMessage(message);
|
||||
return message;
|
||||
},
|
||||
});
|
||||
|
||||
const createIterator = stream[Symbol.asyncIterator].bind(stream);
|
||||
stream[Symbol.asyncIterator] = () => {
|
||||
const iterator = createIterator();
|
||||
return {
|
||||
async next() {
|
||||
const step = await iterator.next();
|
||||
if (!step.done) {
|
||||
transformKimiStreamEvent(step.value, transformMessage);
|
||||
}
|
||||
return step;
|
||||
},
|
||||
async return(value?: unknown) {
|
||||
return iterator.return?.(value) ?? { done: true as const, value: undefined };
|
||||
},
|
||||
async throw(error?: unknown) {
|
||||
return iterator.throw?.(error) ?? { done: true as const, value: undefined };
|
||||
},
|
||||
};
|
||||
};
|
||||
return stream;
|
||||
}
|
||||
|
||||
export function createKimiToolCallMarkupWrapper(baseStreamFn: StreamFn | undefined): StreamFn {
|
||||
const underlying = baseStreamFn ?? streamSimple;
|
||||
return (model, context, options) => {
|
||||
const maybeStream = underlying(model, context, options);
|
||||
if (maybeStream && typeof maybeStream === "object" && "then" in maybeStream) {
|
||||
return Promise.resolve(maybeStream).then((stream) =>
|
||||
wrapStreamMessageObjects(stream, rewriteKimiTaggedToolCallsInMessage),
|
||||
);
|
||||
}
|
||||
return wrapStreamMessageObjects(maybeStream, rewriteKimiTaggedToolCallsInMessage);
|
||||
};
|
||||
}
|
||||
|
||||
export function createKimiThinkingWrapper(
|
||||
baseStreamFn: StreamFn | undefined,
|
||||
thinkingConfig: KimiThinkingConfig | KimiThinkingType,
|
||||
): StreamFn {
|
||||
const underlying = baseStreamFn ?? streamSimple;
|
||||
return (model, context, options) =>
|
||||
streamWithPayloadPatch(underlying, model, context, options, (payloadObj) => {
|
||||
const normalized =
|
||||
typeof thinkingConfig === "string" ? { type: thinkingConfig } : thinkingConfig;
|
||||
payloadObj.thinking =
|
||||
model.api === "anthropic-messages" ? { ...normalized } : { type: normalized.type };
|
||||
if (model.api === "anthropic-messages") {
|
||||
ensureKimiAnthropicMaxTokens(payloadObj, normalized);
|
||||
} else if (normalized.type === "enabled") {
|
||||
ensureKimiOpenAIReasoningContent(payloadObj);
|
||||
} else {
|
||||
stripKimiOpenAIReasoningContent(payloadObj);
|
||||
}
|
||||
delete payloadObj.reasoning;
|
||||
delete payloadObj.reasoning_effort;
|
||||
delete payloadObj.reasoningEffort;
|
||||
stripAnthropicCacheControlMarkers(payloadObj);
|
||||
});
|
||||
}
|
||||
|
||||
function stripContentBlockCacheControl(block: unknown): void {
|
||||
if (!block || typeof block !== "object") {
|
||||
return;
|
||||
}
|
||||
|
||||
const record = block as Record<string, unknown>;
|
||||
delete record.cache_control;
|
||||
|
||||
if (record.type === "tool_result" && Array.isArray(record.content)) {
|
||||
for (const nestedBlock of record.content) {
|
||||
stripContentBlockCacheControl(nestedBlock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function stripContentArrayCacheControl(value: unknown): void {
|
||||
if (!Array.isArray(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const block of value) {
|
||||
stripContentBlockCacheControl(block);
|
||||
}
|
||||
}
|
||||
|
||||
function stripAnthropicCacheControlMarkers(payloadObj: Record<string, unknown>): void {
|
||||
stripContentArrayCacheControl(payloadObj.system);
|
||||
|
||||
if (!Array.isArray(payloadObj.messages)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const message of payloadObj.messages) {
|
||||
if (!message || typeof message !== "object") {
|
||||
continue;
|
||||
}
|
||||
|
||||
stripContentArrayCacheControl((message as Record<string, unknown>).content);
|
||||
}
|
||||
}
|
||||
|
||||
export function wrapKimiProviderStream(ctx: ProviderWrapStreamFnContext): StreamFn {
|
||||
const thinkingConfig = resolveKimiThinkingConfig({
|
||||
configuredThinking: ctx.extraParams?.thinking,
|
||||
thinkingLevel: ctx.thinkingLevel,
|
||||
});
|
||||
return createKimiToolCallMarkupWrapper(createKimiThinkingWrapper(ctx.streamFn, thinkingConfig));
|
||||
}
|
||||
Reference in New Issue
Block a user