Repository navigation
Expand file tree
/
Copy pathtoolCallEvents.ts
More file actions
135 lines (115 loc) · 3.13 KB
/
Copy pathtoolCallEvents.ts
File metadata and controls
135 lines (115 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { randomUUID } from "crypto";
import YAML from "yaml";
export type ToolCallEvent =
| {
toolCallId: string;
toolName: string;
toolInfo?: string;
phase: "start";
input: string;
}
| {
toolCallId: string;
toolName: string;
phase: "end";
durationMs: number;
output: string | null;
error: string | null;
};
export type ToolCallEventSink = (event: ToolCallEvent) => void;
const TOOL_MESSAGE_DEBUG_KEYS = new Set([
"metadata",
"additional_kwargs",
"response_metadata",
]);
function getToolCallDebugPayload(outputRecord: Record<string, unknown>) {
const lcKwargs =
typeof outputRecord.lc_kwargs === "object" && outputRecord.lc_kwargs !== null
? outputRecord.lc_kwargs as Record<string, unknown>
: null;
if (lcKwargs && "tool_call_id" in lcKwargs) {
return lcKwargs;
}
if ("tool_call_id" in outputRecord) {
return outputRecord;
}
return null;
}
function sanitizeToolCallOutputForDebug(output: unknown) {
if (typeof output !== "object" || output === null) {
return output;
}
const outputRecord = output as Record<string, unknown>;
const debugPayload = getToolCallDebugPayload(outputRecord);
if (!debugPayload) {
return output;
}
return Object.fromEntries(
Object.entries(debugPayload).filter(([key]) => !TOOL_MESSAGE_DEBUG_KEYS.has(key)),
);
}
function serializeErrorForDebug(error: unknown): unknown {
if (!(error instanceof Error)) {
return error;
}
const errorWithCause = error as Error & { cause?: unknown };
const serialized: Record<string, unknown> = {
name: error.name,
message: error.message,
};
if (error.stack) {
serialized.stack = error.stack;
}
if (errorWithCause.cause !== undefined) {
serialized.cause = serializeErrorForDebug(errorWithCause.cause);
}
for (const key of Object.getOwnPropertyNames(error)) {
if (key in serialized) {
continue;
}
serialized[key] = (error as unknown as Record<string, unknown>)[key];
}
return serialized;
}
export function createToolCallTracker(params: {
emit: ToolCallEventSink;
toolCallId?: string;
toolName: string;
toolInfo?: string;
input?: Record<string, unknown>;
startedAt?: number;
}) {
const toolCallId = params.toolCallId ?? randomUUID();
const startedAt = params.startedAt ?? Date.now();
return {
start() {
params.emit({
toolCallId,
toolName: params.toolName,
toolInfo: params.toolInfo,
phase: "start",
input: YAML.stringify(params.input ?? {}),
});
},
finishSuccess(output: unknown) {
params.emit({
toolCallId,
toolName: params.toolName,
phase: "end",
durationMs: Date.now() - startedAt,
output: YAML.stringify(sanitizeToolCallOutputForDebug(output)).trimEnd(),
error: null,
});
},
finishError(error: unknown) {
params.emit({
toolCallId,
toolName: params.toolName,
phase: "end",
durationMs: Date.now() - startedAt,
output: null,
error: YAML.stringify(serializeErrorForDebug(error)).trimEnd(),
});
},
};
}