26 lines
597 B
TypeScript
26 lines
597 B
TypeScript
import { ToolCallDelta } from "..";
|
|
|
|
function safeParseArgs(
|
|
args: string | undefined,
|
|
errorId?: string,
|
|
): Record<string, any> {
|
|
try {
|
|
return JSON.parse(args ?? "{}");
|
|
} catch (e) {
|
|
const identifier = errorId ? `Call: ${errorId}\nArgs:${args}\n` : "";
|
|
console.error(
|
|
`Failed to parse tool call arguments\n${identifier}Error:`,
|
|
e,
|
|
);
|
|
return {};
|
|
}
|
|
}
|
|
|
|
export function safeParseToolCallArgs(
|
|
toolCall: ToolCallDelta,
|
|
): Record<string, any> {
|
|
return safeParseArgs(
|
|
toolCall.function?.arguments,
|
|
`${toolCall.function?.name} ${toolCall.id}`,
|
|
);
|
|
}
|