0.5 缩减重构

This commit is contained in:
lix-2026
2026-04-13 19:21:42 +08:00
parent af92c4b149
commit 71fb1aee7e
2023 changed files with 21113 additions and 394493 deletions
@@ -2,9 +2,15 @@ import type { OpenAiCompatibleChatMessage, OpenAiCompatibleChatOptions } from "@
import { openAiCompatibleChat } from "@/lib/ai/openaiCompatibleChat";
import { parseToolTagCalls, formatToolResultTag } from "../protocol/toolTagProtocol";
export type AgentChatFn = (
messages: OpenAiCompatibleChatMessage[],
cfg: OpenAiCompatibleChatOptions,
) => Promise<{ text: string; raw: unknown }>;
export type RunAiAgentArgs = {
userMessages: Array<{ role: "user" | "assistant"; content: string }>;
cfg: OpenAiCompatibleChatOptions;
chat?: AgentChatFn;
allowedToolIds: Set<string>;
runTool: (toolId: string, toolArgs: Record<string, unknown>) => Promise<unknown>;
maxSteps?: number;
@@ -246,6 +252,7 @@ export const runAiAgent = async (args: RunAiAgentArgs): Promise<{ ok: true; text
const maxSteps = Math.max(1, Math.min(24, Math.floor(args.maxSteps ?? DEFAULT_MAX_STEPS)));
const allowedToolIds = args.allowedToolIds;
const allowedToolNames = new Set<string>(Array.from(allowedToolIds));
const chat = args.chat ?? openAiCompatibleChat;
const messages: OpenAiCompatibleChatMessage[] = [
{ role: "system", content: buildSystemPrompt(allowedToolIds, args.systemContextText) },
@@ -288,7 +295,7 @@ export const runAiAgent = async (args: RunAiAgentArgs): Promise<{ ok: true; text
};
for (; steps < maxSteps; steps += 1) {
const { text } = await openAiCompatibleChat(messages, args.cfg);
const { text } = await chat(messages, args.cfg);
const parsed = parseToolTagCalls(text, allowedToolNames);
if (parsed.calls.length === 0) {