Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions packages/core/lib/v3/llm/aisdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,13 @@ You must respond in JSON format. respond WITH JSON. Do not include any other tex
model: this.model.modelId,
output:
textResponse.text ||
(transformedToolCalls.length > 0
? `[${transformedToolCalls.length} tool calls]`
((textResponse.toolCalls?.length ?? 0) > 0
? summarizeToolCalls(
textResponse.toolCalls!.map((toolCall) => ({
name: toolCall.toolName,
input: toolCall.input,
})),
)
: ""),
inputTokens: textResponse.usage.inputTokens,
outputTokens: textResponse.usage.outputTokens,
Expand Down Expand Up @@ -407,3 +412,33 @@ You must respond in JSON format. respond WITH JSON. Do not include any other tex
return result;
}
}

function summarizeToolCalls(
toolCalls: Array<{ name?: string; input?: unknown }>,
): string {
if (toolCalls.length === 0) {
return "";
}

const summaries = toolCalls.map((toolCall) => {
const name = toolCall.name || "unknown";
return `${name}(${truncateToolArguments(toolCall.input)})`;
});

return toolCalls.length === 1
? `tool call: ${summaries[0]}`
: `tool calls: ${summaries.join("; ")}`;
}

function truncateToolArguments(toolInput: unknown, maxLength = 160): string {
if (toolInput === undefined) {
return "";
}

const serializedArguments =
typeof toolInput === "string" ? toolInput : JSON.stringify(toolInput);

return serializedArguments.length > maxLength
? `${serializedArguments.slice(0, maxLength - 3)}...`
: serializedArguments;
}
12 changes: 12 additions & 0 deletions packages/core/tests/integration/flowLogger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@ test.describe("flow logger integration", () => {
expect(
directChildrenOfType(events, actRoot, "LlmResponseEvent"),
).toHaveLength(1);
const agentRootLlmResponses = directChildrenOfType(
events,
agentRoot,
"LlmResponseEvent",
);
expect(agentRootLlmResponses[0].data.output).toContain("tool call: act(");
expect(agentRootLlmResponses[1].data.output).toContain(
"tool call: done(",
);
assertNoFloatingLlmEvents(events);
assertNoFloatingCdpEvents(events);
} finally {
Expand Down Expand Up @@ -822,6 +831,9 @@ test.describe("flow logger integration", () => {
expectDirectParent(pageScreenshotCompleted, pageScreenshot);
expect(llmRequests).toHaveLength(3);
expect(llmResponses).toHaveLength(3);
expect(llmResponses[0].data.output).toContain("tool call: goto(");
expect(llmResponses[1].data.output).toContain("tool call: screenshot(");
expect(llmResponses[2].data.output).toContain("tool call: done(");

for (const event of [...llmRequests, ...llmResponses]) {
expect(event.eventParentIds).toEqual([root.eventId]);
Expand Down
Loading