0.5 缩减重构
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user