解析工具调用参数,优化事件处理逻辑

This commit is contained in:
2026-04-30 13:38:53 +08:00
parent 36d1a8d6ea
commit 85b4f45d4a
2 changed files with 57 additions and 2 deletions
+29
View File
@@ -97,6 +97,35 @@ describe("streamAgentChat", () => {
});
});
it("parses legacy tool_call arguments when params is empty", async () => {
apiFetch.mockResolvedValue({
ok: true,
body: makeStream([
'event: tool_call\ndata: {"conversationId":"agent-1e75dd01-29e","tool":"locate_features","params":{},"arguments":"{\\"ids\\":[\\"142902\\"],\\"feature_type\\":\\"junction\\"}"}\n\n',
'event: done\ndata: {"session_id":"agent-1e75dd01-29e"}\n\n',
]),
});
const events: Array<{
type: string;
sessionId?: string;
tool?: string;
params?: Record<string, unknown>;
}> = [];
await streamAgentChat({
message: "hi",
onEvent: (event) => events.push(event),
});
expect(events[0]).toEqual({
type: "tool_call",
sessionId: "agent-1e75dd01-29e",
tool: "locate_features",
params: { ids: ["142902"], feature_type: "junction" },
});
});
it("emits error when response is not ok", async () => {
apiFetch.mockResolvedValue({
ok: false,
+28 -2
View File
@@ -52,6 +52,30 @@ const parseEventBlock = (block: string): { event?: string; data?: string } => {
};
};
const isObjectRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);
const resolveToolParams = (
params: unknown,
argumentsPayload: unknown,
): Record<string, unknown> => {
if (isObjectRecord(params) && Object.keys(params).length > 0) {
return params;
}
if (isObjectRecord(argumentsPayload)) {
return argumentsPayload;
}
if (typeof argumentsPayload === "string") {
try {
const parsed = JSON.parse(argumentsPayload) as unknown;
return isObjectRecord(parsed) ? parsed : {};
} catch {
return {};
}
}
return isObjectRecord(params) ? params : {};
};
export const streamAgentChat = async ({
message,
sessionId,
@@ -125,11 +149,13 @@ export const streamAgentChat = async ({
try {
const parsed = JSON.parse(data) as {
session_id?: string;
conversationId?: string;
content?: string;
message?: string;
detail?: string;
tool?: string;
params?: Record<string, unknown>;
arguments?: unknown;
id?: string;
phase?: string;
status?: "running" | "completed" | "error";
@@ -166,9 +192,9 @@ export const streamAgentChat = async ({
} else if (event === "tool_call") {
onEvent({
type: "tool_call",
sessionId: parsed.session_id ?? "",
sessionId: parsed.session_id ?? parsed.conversationId ?? "",
tool: parsed.tool ?? "",
params: parsed.params ?? {},
params: resolveToolParams(parsed.params, parsed.arguments),
});
}
} catch {