Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(assistant): trace rejected tool calls (#14676)

Nimarcddcb56dd50f
diff --git a/web/src/__tests__/server/in-app-agent-stream.servertest.ts b/web/src/__tests__/server/in-app-agent-stream.servertest.ts
index 6002c5f20e..05ad6daba9 100644
--- a/web/src/__tests__/server/in-app-agent-stream.servertest.ts
+++ b/web/src/__tests__/server/in-app-agent-stream.servertest.ts
@@ -32,6 +32,7 @@ const instrumentationMocks = vi.hoisted(() => {
const instrumentation = {
recordEvents: vi.fn(),
recordAvailableTools: vi.fn(),
+ recordToolCallApproval: vi.fn(),
end: vi.fn(),
endWithError: vi.fn(),
flush: vi.fn(),
@@ -833,6 +834,7 @@ describe("createAgUiStream", () => {
},
langfuseClient,
useLocalPrompt: false,
+ langfuseTracing: createTestTracingConfig(),
},
});
await readStream(stream);
@@ -942,6 +944,12 @@ describe("createAgUiStream", () => {
role: "tool",
},
]);
+ expect(
+ instrumentationMocks.instrumentation.recordToolCallApproval,
+ ).toHaveBeenCalledWith({
+ toolCallId: "tool-call-1",
+ status: "approved",
+ });
const agentConfig = vi.mocked(Agent).mock.calls[0]?.[0];
const createScoreConfigTool = agentConfig?.tools
@@ -1212,6 +1220,7 @@ describe("createAgUiStream", () => {
},
langfuseClient,
useLocalPrompt: false,
+ langfuseTracing: createTestTracingConfig(),
},
});
await readStream(stream, (event) => {
@@ -1257,6 +1266,12 @@ describe("createAgUiStream", () => {
},
]);
expect(streamedEvents).toEqual(persistedEvents);
+ expect(
+ instrumentationMocks.instrumentation.recordToolCallApproval,
+ ).toHaveBeenCalledWith({
+ toolCallId: "tool-call-1",
+ status: "rejected",
+ });
expect(onComplete).toHaveBeenCalledOnce();
});
@@ -1497,6 +1512,16 @@ function createToolApprovalResumeInput(approved: boolean) {
};
}
+function createTestTracingConfig() {
+ return {
+ environment: "langfuse-in-app-agent",
+ metadata: { langfuse_project_id: "project-1" },
+ user: { id: "user-1" },
+ traceId: "0123456789abcdef0123456789abcdef",
+ targetProjectId: "project-1",
+ };
+}
+
function parseEvents(chunk: string) {
return chunk
.split("\n\n")
diff --git a/web/src/__tests__/server/unit/in-app-agent-instrumentation.servertest.ts b/web/src/__tests__/server/unit/in-app-agent-instrumentation.servertest.ts
index 04dc52ea38..d4c5e14521 100644
--- a/web/src/__tests__/server/unit/in-app-agent-instrumentation.servertest.ts
+++ b/web/src/__tests__/server/unit/in-app-agent-instrumentation.servertest.ts
@@ -171,6 +171,9 @@ describe("InAppAgentInstrumentation", () => {
}),
}),
);
+ expect(
+ mocks.handler.langfuse.enqueue.mock.calls[0]?.[1].metadata,
+ ).not.toHaveProperty("toolCallApproval");
expect(mocks.agentGeneration.update).toHaveBeenCalledWith(
expect.objectContaining({
name: "agent-run",
@@ -202,6 +205,51 @@ describe("InAppAgentInstrumentation", () => {
expect(mocks.trace.update.mock.calls[0][0]).not.toHaveProperty("output");
});
+ it.each(["approved", "rejected"] as const)(
+ "records manual tool approval metadata as %s",
+ (status) => {
+ const instrumentation = createInstrumentation();
+
+ instrumentation.recordToolCallApproval({
+ toolCallId: "tool-1",
+ status,
+ });
+ instrumentation.recordEvents([
+ {
+ type: EventType.TOOL_CALL_START,
+ toolCallId: "tool-1",
+ toolCallName: "createScoreConfig",
+ parentMessageId: "tool-1-approval-tool-call",
+ },
+ {
+ type: EventType.TOOL_CALL_ARGS,
+ toolCallId: "tool-1",
+ delta: '{"name":"readiness"}',
+ },
+ {
+ type: EventType.TOOL_CALL_END,
+ toolCallId: "tool-1",
+ },
+ {
+ type: EventType.TOOL_CALL_RESULT,
+ toolCallId: "tool-1",
+ content: "ok",
+ },
+ ]);
+
+ expect(mocks.handler.langfuse.enqueue).toHaveBeenCalledWith(
+ "tool-create",
+ expect.objectContaining({
+ metadata: expect.objectContaining({
+ toolCallId: "tool-1",
+ parentMessageId: "tool-1-approval-tool-call",
+ toolCallApproval: status,
+ }),
+ }),
+ );
+ },
+ );
+
it("records run failures on the agent generation", () => {
const instrumentation = createInstrumentation();
const toolCall = {
diff --git a/web/src/components/nav/in-app-ai-agent-button.tsx b/web/src/components/nav/in-app-ai-agent-button.tsx
index b9fe2d4b2d..94900df821 100644
--- a/web/src/components/nav/in-app-ai-agent-button.tsx
+++ b/web/src/components/nav/in-app-ai-agent-button.tsx
@@ -134,15 +134,13 @@ export const InAppAiAgentButton = () => {
}
setSupportDrawerOpen(false);
- setOpen((currentOpen) => {
- const nextOpen = !currentOpen;
+ const willOpen = !open;
- if (nextOpen) {
- floatingPanelHandle.resetGeometry();
- }
+ if (willOpen) {
+ floatingPanelHandle.resetGeometry();
+ }
- return nextOpen;
- });
+ setOpen((currentOpen) => !currentOpen);
};
return (
diff --git a/web/src/ee/features/in-app-agent/server/agent.ts b/web/src/ee/features/in-app-agent/server/agent.ts
index f586bfd78e..8d443321ae 100644
--- a/web/src/ee/features/in-app-agent/server/agent.ts
+++ b/web/src/ee/features/in-app-agent/server/agent.ts
@@ -315,6 +315,7 @@ export async function createAgUiStream(params: {
...runInput.syntheticEvents,
];
+ instrumentation?.recordToolCallApproval(runInput.toolCallApproval);
instrumentation?.recordEvents(terminalEvents);
for (const syntheticEvent of terminalEvents) {
enqueueEvent(syntheticEvent);
@@ -554,6 +555,9 @@ export async function createAgUiStream(params: {
agUiEvent.type === EventType.RUN_STARTED &&
pendingSyntheticEvents.length > 0
) {
+ instrumentation?.recordToolCallApproval(
+ runInput.toolCallApproval,
+ );
instrumentation?.recordEvents(pendingSyntheticEvents);
for (const syntheticEvent of pendingSyntheticEvents) {
enqueueEvent(syntheticEvent);
diff --git a/web/src/ee/features/in-app-agent/server/human-in-the-loop.ts b/web/src/ee/features/in-app-agent/server/human-in-the-loop.ts
index 09989b2fd0..093af24aa6 100644
--- a/web/src/ee/features/in-app-agent/server/human-in-the-loop.ts
+++ b/web/src/ee/features/in-app-agent/server/human-in-the-loop.ts
@@ -156,6 +156,10 @@ export type ManualToolApprovalRunInput = {
input: AgUiRunAgentInput;
syntheticEvents: AgUiEvent[];
shouldContinue: boolean;
+ toolCallApproval?: {
+ toolCallId: string;
+ status: "approved" | "rejected";
+ };
};
export async function createManualToolApprovalRunInput(params: {
@@ -184,6 +188,10 @@ export async function createManualToolApprovalRunInput(params: {
toolError: MANUAL_TOOL_APPROVAL_REJECTION_MESSAGE,
}),
shouldContinue: false,
+ toolCallApproval: {
+ toolCallId: approvalRequest.toolCallId,
+ status: "rejected",
+ },
};
}
@@ -228,6 +236,10 @@ export async function createManualToolApprovalRunInput(params: {
},
syntheticEvents,
shouldContinue: true,
+ toolCallApproval: {
+ toolCallId: approvalRequest.toolCallId,
+ status: "approved",
+ },
};
}
diff --git a/web/src/ee/features/in-app-agent/server/instrumentation.ts b/web/src/ee/features/in-app-agent/server/instrumentation.ts
index be77b383f8..4a293a4340 100644
--- a/web/src/ee/features/in-app-agent/server/instrumentation.ts
+++ b/web/src/ee/features/in-app-agent/server/instrumentation.ts
@@ -77,6 +77,7 @@ type ToolObservationBody = {
statusMessage?: string;
metadata?: Record<string, unknown>;
};
+type ToolCallApprovalStatus = "approved" | "rejected";
export function createInAppAgentInstrumentation({
input,
@@ -123,6 +124,10 @@ export class InAppAgentInstrumentation {
parentMessageId?: string;
}
>();
+ private readonly toolCallApprovals = new Map<
+ string,
+ ToolCallApprovalStatus
+ >();
private readonly metadata: Record<string, unknown>;
private readonly agentRunOutputMessages: AgentRunChatMessage[] = [];
private readonly agentRunToolCalls: AgentRunToolCall[] = [];
@@ -225,6 +230,17 @@ export class InAppAgentInstrumentation {
);
}
+ recordToolCallApproval(approval?: {
+ toolCallId: string;
+ status: ToolCallApprovalStatus;
+ }) {
+ if (this.ended || !approval) {
+ return;
+ }
+
+ this.toolCallApprovals.set(approval.toolCallId, approval.status);
+ }
+
endWithError(error: unknown) {
if (this.ended) {
return;
@@ -453,6 +469,7 @@ export class InAppAgentInstrumentation {
const output =
tool.output === undefined ? undefined : normalizeToolOutput(tool.output);
const isError = options?.statusMessage !== undefined || isToolError(output);
+ const toolCallApproval = this.toolCallApprovals.get(toolCallId);
const body: ToolObservationBody = {
id: toolCallId,
traceId: this.agentRun.traceId,
@@ -470,6 +487,7 @@ export class InAppAgentInstrumentation {
metadata: {
...(options?.metadata ?? {}),
toolCallId,
+ ...(toolCallApproval ? { toolCallApproval } : {}),
...(tool.argsComplete ? {} : { argsComplete: false }),
...(tool.parentMessageId
? { parentMessageId: tool.parentMessageId }
@@ -478,6 +496,7 @@ export class InAppAgentInstrumentation {
};
this.recordToolCall(toolCallId, tool, output);
+ this.toolCallApprovals.delete(toolCallId);
(
this.langfuse as unknown as {