167 lines
4.3 KiB
TypeScript
167 lines
4.3 KiB
TypeScript
import { withToolSpan } from "@obs/spans";
|
|
import { getRequestLogger } from "@obs/context";
|
|
import type { LlmClient } from "@llm/llm-client";
|
|
import type { ChatCompletionOptions, ChatMessage } from "@llm/types";
|
|
import { parseToolCalls } from "@tools/parser";
|
|
import type { Tool, ToolContext } from "@tools/tool";
|
|
import { getActiveProcessTrace } from "./trace.js";
|
|
|
|
export interface ToolLoopOptions {
|
|
deadlineMs?: number | undefined;
|
|
llmClient: LlmClient;
|
|
maxHops?: number | undefined;
|
|
maxTokens?: number | undefined;
|
|
messages: ChatMessage[];
|
|
modelOverride?: string | undefined;
|
|
role?: ChatCompletionOptions["role"];
|
|
signal?: AbortSignal | undefined;
|
|
temperature?: number | undefined;
|
|
tier?: ChatCompletionOptions["tier"];
|
|
toolContext: ToolContext;
|
|
tools: readonly Tool<unknown, unknown>[];
|
|
}
|
|
|
|
export async function runWithTools(
|
|
options: ToolLoopOptions,
|
|
): Promise<string | null> {
|
|
const logger = getRequestLogger();
|
|
const maxHops = options.maxHops ?? 2;
|
|
const messages = options.messages.map((message) => ({ ...message }));
|
|
let allowNativeTools = true;
|
|
|
|
for (let hop = 0; hop <= maxHops; hop++) {
|
|
const response = await options.llmClient.chatCompletion(messages, {
|
|
deadlineMs: options.deadlineMs,
|
|
maxTokens: options.maxTokens,
|
|
modelOverride: options.modelOverride,
|
|
role: options.role,
|
|
signal: options.signal,
|
|
temperature: options.temperature,
|
|
tier: options.tier,
|
|
tools: allowNativeTools ? options.tools : undefined,
|
|
});
|
|
|
|
if (!response) {
|
|
logger.debug(
|
|
{
|
|
hop,
|
|
role: options.role,
|
|
tier: options.tier,
|
|
},
|
|
"[ToolLoop] LLM returned no response",
|
|
);
|
|
return null;
|
|
}
|
|
|
|
const toolCalls = parseToolCalls(response.message);
|
|
if (toolCalls.length === 0) {
|
|
logger.debug(
|
|
{
|
|
content: response.content,
|
|
hop,
|
|
role: options.role,
|
|
tier: options.tier,
|
|
},
|
|
"[ToolLoop] No tool call in model response",
|
|
);
|
|
return response.content;
|
|
}
|
|
|
|
logger.debug(
|
|
{
|
|
hop,
|
|
role: options.role,
|
|
tier: options.tier,
|
|
toolCalls: toolCalls.map((call) => ({
|
|
id: call.id,
|
|
name: call.name,
|
|
})),
|
|
},
|
|
"[ToolLoop] Parsed tool calls",
|
|
);
|
|
|
|
if (hop === maxHops) {
|
|
messages.push(response.message);
|
|
messages.push({
|
|
role: "user",
|
|
content:
|
|
"도구 호출은 여기까지 하고, 지금까지 얻은 정보만으로 한국어 문장으로 최종 답변해줘.",
|
|
});
|
|
allowNativeTools = false;
|
|
continue;
|
|
}
|
|
|
|
messages.push(response.message);
|
|
|
|
for (const call of toolCalls) {
|
|
const tool = options.tools.find((candidate) => candidate.name === call.name);
|
|
if (!tool) {
|
|
throw new Error(`Tool not registered for call: ${call.name}`);
|
|
}
|
|
|
|
logger.debug(
|
|
{
|
|
arguments: call.arguments,
|
|
callId: call.id,
|
|
tool: tool.name,
|
|
},
|
|
"[ToolLoop] Executing tool call",
|
|
);
|
|
|
|
const traceStep = getActiveProcessTrace()?.startStep("tool-call");
|
|
let result: unknown;
|
|
try {
|
|
const validated = tool.argsSchema.parse(call.arguments);
|
|
result = await withToolSpan(
|
|
tool.name,
|
|
{
|
|
"tool.call.id": call.id,
|
|
"tool.name": tool.name,
|
|
},
|
|
() => tool.execute(validated, options.toolContext),
|
|
);
|
|
traceStep?.finish({
|
|
args: call.arguments,
|
|
name: tool.name,
|
|
ok: true,
|
|
resultSummary: summarizeToolResult(result),
|
|
});
|
|
} catch (error) {
|
|
traceStep?.finish({
|
|
args: call.arguments,
|
|
name: tool.name,
|
|
ok: false,
|
|
resultSummary: error instanceof Error ? error.message : String(error),
|
|
});
|
|
throw error;
|
|
}
|
|
|
|
logger.debug(
|
|
{
|
|
callId: call.id,
|
|
result,
|
|
tool: tool.name,
|
|
},
|
|
"[ToolLoop] Tool call completed",
|
|
);
|
|
|
|
messages.push({
|
|
role: "tool",
|
|
name: tool.name,
|
|
tool_call_id: call.id,
|
|
content: JSON.stringify(result),
|
|
});
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function summarizeToolResult(result: unknown): string {
|
|
try {
|
|
return JSON.stringify(result).slice(0, 200);
|
|
} catch {
|
|
return String(result).slice(0, 200);
|
|
}
|
|
}
|