mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-10 13:00:45 +08:00
重构主控编排与运行时预热链路,统一工作区提示词/专家调度协议并补齐 wiki 记忆注入与写回闭环。
同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。 Made-with: Cursor
This commit is contained in:
parent
4a23b715a2
commit
dbbe3add6a
14438 changed files with 2693620 additions and 2546 deletions
246
openclaw/extensions/kimi-coding/stream.ts
Normal file
246
openclaw/extensions/kimi-coding/stream.ts
Normal file
|
|
@ -0,0 +1,246 @@
|
|||
import type { StreamFn } from "@mariozechner/pi-agent-core";
|
||||
import { streamSimple } from "@mariozechner/pi-ai";
|
||||
import type { ProviderWrapStreamFnContext } from "openclaw/plugin-sdk/plugin-entry";
|
||||
import { streamWithPayloadPatch } from "openclaw/plugin-sdk/provider-stream-shared";
|
||||
import { normalizeOptionalLowercaseString } from "openclaw/plugin-sdk/text-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";
|
||||
type KimiThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "adaptive";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
export function resolveKimiThinkingType(params: {
|
||||
configuredThinking: unknown;
|
||||
thinkingLevel?: KimiThinkingLevel;
|
||||
}): KimiThinkingType {
|
||||
const configured = normalizeKimiThinkingType(params.configuredThinking);
|
||||
if (configured) {
|
||||
return configured;
|
||||
}
|
||||
if (!params.thinkingLevel || params.thinkingLevel === "off") {
|
||||
return "disabled";
|
||||
}
|
||||
return "enabled";
|
||||
}
|
||||
|
||||
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 wrapKimiTaggedToolCalls(
|
||||
stream: ReturnType<typeof streamSimple>,
|
||||
): ReturnType<typeof streamSimple> {
|
||||
const originalResult = stream.result.bind(stream);
|
||||
stream.result = async () => {
|
||||
const message = await originalResult();
|
||||
rewriteKimiTaggedToolCallsInMessage(message);
|
||||
return message;
|
||||
};
|
||||
|
||||
const originalAsyncIterator = stream[Symbol.asyncIterator].bind(stream);
|
||||
(stream as { [Symbol.asyncIterator]: typeof originalAsyncIterator })[Symbol.asyncIterator] =
|
||||
function () {
|
||||
const iterator = originalAsyncIterator();
|
||||
return {
|
||||
async next() {
|
||||
const result = await iterator.next();
|
||||
if (!result.done && result.value && typeof result.value === "object") {
|
||||
const event = result.value as {
|
||||
partial?: unknown;
|
||||
message?: unknown;
|
||||
};
|
||||
rewriteKimiTaggedToolCallsInMessage(event.partial);
|
||||
rewriteKimiTaggedToolCallsInMessage(event.message);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
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) => wrapKimiTaggedToolCalls(stream));
|
||||
}
|
||||
return wrapKimiTaggedToolCalls(maybeStream);
|
||||
};
|
||||
}
|
||||
|
||||
export function createKimiThinkingWrapper(
|
||||
baseStreamFn: StreamFn | undefined,
|
||||
thinkingType: KimiThinkingType,
|
||||
): StreamFn {
|
||||
const underlying = baseStreamFn ?? streamSimple;
|
||||
return (model, context, options) =>
|
||||
streamWithPayloadPatch(underlying, model, context, options, (payloadObj) => {
|
||||
payloadObj.thinking = { type: thinkingType };
|
||||
delete payloadObj.reasoning;
|
||||
delete payloadObj.reasoning_effort;
|
||||
delete payloadObj.reasoningEffort;
|
||||
});
|
||||
}
|
||||
|
||||
export function wrapKimiProviderStream(ctx: ProviderWrapStreamFnContext): StreamFn {
|
||||
const thinkingType = resolveKimiThinkingType({
|
||||
configuredThinking: ctx.extraParams?.thinking,
|
||||
thinkingLevel: ctx.thinkingLevel,
|
||||
});
|
||||
return createKimiToolCallMarkupWrapper(createKimiThinkingWrapper(ctx.streamFn, thinkingType));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue