@agent-inspect/langchain 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +623 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +70 -0
- package/dist/index.d.ts +70 -0
- package/dist/index.mjs +617 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AgentInspect contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var base = require('@langchain/core/callbacks/base');
|
|
4
|
+
var agentInspect = require('agent-inspect');
|
|
5
|
+
|
|
6
|
+
// packages/langchain/src/agent-inspect-callback.ts
|
|
7
|
+
|
|
8
|
+
// packages/langchain/src/metadata.ts
|
|
9
|
+
function isRecord(v) {
|
|
10
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
11
|
+
}
|
|
12
|
+
function num(v) {
|
|
13
|
+
if (typeof v === "number" && Number.isFinite(v)) return v;
|
|
14
|
+
if (typeof v === "string" && v.trim() !== "") {
|
|
15
|
+
const n = Number(v);
|
|
16
|
+
if (Number.isFinite(n)) return n;
|
|
17
|
+
}
|
|
18
|
+
return void 0;
|
|
19
|
+
}
|
|
20
|
+
function normalizeTokenShape(raw) {
|
|
21
|
+
const input = num(raw.promptTokens) ?? num(raw.inputTokens) ?? num(raw.input_tokens) ?? num(raw.prompt_tokens);
|
|
22
|
+
const output = num(raw.completionTokens) ?? num(raw.outputTokens) ?? num(raw.output_tokens) ?? num(raw.completion_tokens);
|
|
23
|
+
let total = num(raw.totalTokens) ?? num(raw.total_tokens);
|
|
24
|
+
if (total === void 0 && input !== void 0 && output !== void 0) {
|
|
25
|
+
total = input + output;
|
|
26
|
+
}
|
|
27
|
+
if (input === void 0 && output === void 0 && total === void 0) {
|
|
28
|
+
return void 0;
|
|
29
|
+
}
|
|
30
|
+
const out = {};
|
|
31
|
+
if (input !== void 0) out.input = input;
|
|
32
|
+
if (output !== void 0) out.output = output;
|
|
33
|
+
if (total !== void 0) out.total = total;
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
function extractTokenUsage(output) {
|
|
37
|
+
try {
|
|
38
|
+
if (!isRecord(output)) return void 0;
|
|
39
|
+
const candidates = [
|
|
40
|
+
isRecord(output.llmOutput) ? output.llmOutput.tokenUsage : void 0,
|
|
41
|
+
isRecord(output.llmOutput) ? output.llmOutput.estimatedTokenUsage : void 0,
|
|
42
|
+
output.usage_metadata,
|
|
43
|
+
isRecord(output.response_metadata) ? output.response_metadata.tokenUsage : void 0,
|
|
44
|
+
isRecord(output.response_metadata) ? output.response_metadata.token_usage : void 0
|
|
45
|
+
];
|
|
46
|
+
for (const c of candidates) {
|
|
47
|
+
if (!isRecord(c)) continue;
|
|
48
|
+
const norm = normalizeTokenShape(c);
|
|
49
|
+
if (norm) return norm;
|
|
50
|
+
}
|
|
51
|
+
return void 0;
|
|
52
|
+
} catch {
|
|
53
|
+
return void 0;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function readLcKwargs(serializedOrOutput) {
|
|
57
|
+
if (!isRecord(serializedOrOutput)) return void 0;
|
|
58
|
+
const lc = serializedOrOutput.lc_kwargs ?? serializedOrOutput.kwargs;
|
|
59
|
+
return isRecord(lc) ? lc : void 0;
|
|
60
|
+
}
|
|
61
|
+
function extractModelName(serializedOrOutput) {
|
|
62
|
+
try {
|
|
63
|
+
const lc = readLcKwargs(serializedOrOutput);
|
|
64
|
+
const fromLc = lc?.model ?? lc?.modelName ?? lc?.model_name;
|
|
65
|
+
if (typeof fromLc === "string" && fromLc.trim()) return fromLc;
|
|
66
|
+
if (isRecord(serializedOrOutput)) {
|
|
67
|
+
for (const k of ["model", "modelName", "model_name"]) {
|
|
68
|
+
const v = serializedOrOutput[k];
|
|
69
|
+
if (typeof v === "string" && v.trim()) return v;
|
|
70
|
+
}
|
|
71
|
+
const rm = serializedOrOutput.response_metadata;
|
|
72
|
+
if (isRecord(rm)) {
|
|
73
|
+
const m = rm.model_name ?? rm.model;
|
|
74
|
+
if (typeof m === "string" && m.trim()) return m;
|
|
75
|
+
}
|
|
76
|
+
const lo = serializedOrOutput.llmOutput;
|
|
77
|
+
if (isRecord(lo)) {
|
|
78
|
+
const m = lo.model_name;
|
|
79
|
+
if (typeof m === "string" && m.trim()) return m;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return void 0;
|
|
83
|
+
} catch {
|
|
84
|
+
return void 0;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function safePreview(value, maxChars) {
|
|
88
|
+
try {
|
|
89
|
+
if (maxChars <= 0) return void 0;
|
|
90
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
91
|
+
const json = JSON.stringify(value, (_k, v) => {
|
|
92
|
+
if (typeof v === "bigint") return v.toString();
|
|
93
|
+
if (typeof v === "function" || typeof v === "symbol") return void 0;
|
|
94
|
+
if (typeof v === "object" && v !== null) {
|
|
95
|
+
if (seen.has(v)) return "[Circular]";
|
|
96
|
+
seen.add(v);
|
|
97
|
+
}
|
|
98
|
+
return v;
|
|
99
|
+
});
|
|
100
|
+
if (json === void 0) return void 0;
|
|
101
|
+
if (json.length <= maxChars) return json;
|
|
102
|
+
return `${json.slice(0, maxChars)}\u2026`;
|
|
103
|
+
} catch {
|
|
104
|
+
try {
|
|
105
|
+
const s = String(value);
|
|
106
|
+
if (s.length <= maxChars) return s;
|
|
107
|
+
return `${s.slice(0, maxChars)}\u2026`;
|
|
108
|
+
} catch {
|
|
109
|
+
return void 0;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
var MAX_METADATA_KEYS = 40;
|
|
114
|
+
function toPlainMetadata(value) {
|
|
115
|
+
try {
|
|
116
|
+
if (!isRecord(value)) return {};
|
|
117
|
+
const out = {};
|
|
118
|
+
let n = 0;
|
|
119
|
+
for (const [k, v] of Object.entries(value)) {
|
|
120
|
+
if (n >= MAX_METADATA_KEYS) break;
|
|
121
|
+
if (typeof v === "function" || typeof v === "symbol") continue;
|
|
122
|
+
if (v === null || typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
|
|
123
|
+
out[k] = v;
|
|
124
|
+
n++;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (typeof v === "bigint") {
|
|
128
|
+
out[k] = v.toString();
|
|
129
|
+
n++;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (Array.isArray(v)) {
|
|
133
|
+
out[k] = `array(${v.length})`;
|
|
134
|
+
n++;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (isRecord(v)) {
|
|
138
|
+
out[k] = "[object]";
|
|
139
|
+
n++;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
out[k] = String(v);
|
|
143
|
+
n++;
|
|
144
|
+
}
|
|
145
|
+
return out;
|
|
146
|
+
} catch {
|
|
147
|
+
return {};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// packages/langchain/src/agent-inspect-callback.ts
|
|
152
|
+
function serializedLabel(s) {
|
|
153
|
+
if (typeof s.name === "string" && s.name.trim()) return s.name;
|
|
154
|
+
if (Array.isArray(s.id) && s.id.length > 0) return s.id[s.id.length - 1];
|
|
155
|
+
return s.type;
|
|
156
|
+
}
|
|
157
|
+
function errorShape(err) {
|
|
158
|
+
if (err instanceof Error) {
|
|
159
|
+
return { errorName: err.name, errorMessage: err.message };
|
|
160
|
+
}
|
|
161
|
+
return { errorMessage: String(err) };
|
|
162
|
+
}
|
|
163
|
+
var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
164
|
+
name = "agent-inspect";
|
|
165
|
+
#opts;
|
|
166
|
+
#redactor;
|
|
167
|
+
#events = [];
|
|
168
|
+
#starts = /* @__PURE__ */ new Map();
|
|
169
|
+
#rootRunId;
|
|
170
|
+
constructor(options = {}) {
|
|
171
|
+
super({});
|
|
172
|
+
this.#opts = {
|
|
173
|
+
capture: options.capture ?? "metadata-only",
|
|
174
|
+
silent: options.silent ?? false,
|
|
175
|
+
maxPreviewChars: options.maxPreviewChars ?? 200,
|
|
176
|
+
...options
|
|
177
|
+
};
|
|
178
|
+
this.#redactor = new agentInspect.Redactor({ rules: this.#opts.redact });
|
|
179
|
+
}
|
|
180
|
+
getEvents() {
|
|
181
|
+
return this.#events.map((e) => ({
|
|
182
|
+
...e,
|
|
183
|
+
attributes: e.attributes ? { ...e.attributes } : void 0,
|
|
184
|
+
source: { ...e.source }
|
|
185
|
+
}));
|
|
186
|
+
}
|
|
187
|
+
clear() {
|
|
188
|
+
this.#events = [];
|
|
189
|
+
this.#starts.clear();
|
|
190
|
+
this.#rootRunId = void 0;
|
|
191
|
+
}
|
|
192
|
+
#ensureRoot(lcRunId, parentRunId) {
|
|
193
|
+
if (parentRunId) return;
|
|
194
|
+
if (!this.#rootRunId) this.#rootRunId = lcRunId;
|
|
195
|
+
}
|
|
196
|
+
#traceRunId(lcRunId) {
|
|
197
|
+
return this.#rootRunId ?? lcRunId;
|
|
198
|
+
}
|
|
199
|
+
#durationFor(lcRunId) {
|
|
200
|
+
const s = this.#starts.get(lcRunId);
|
|
201
|
+
if (!s) return void 0;
|
|
202
|
+
return Date.now() - s.ts;
|
|
203
|
+
}
|
|
204
|
+
#rememberStart(lcRunId, kind) {
|
|
205
|
+
this.#starts.set(lcRunId, { ts: Date.now(), kind });
|
|
206
|
+
}
|
|
207
|
+
#clearStart(lcRunId) {
|
|
208
|
+
this.#starts.delete(lcRunId);
|
|
209
|
+
}
|
|
210
|
+
#baseAttrs(lcRunId, parentRunId, tags, runNameArg) {
|
|
211
|
+
const out = {
|
|
212
|
+
langchainRunId: lcRunId
|
|
213
|
+
};
|
|
214
|
+
if (parentRunId) out.parentRunId = parentRunId;
|
|
215
|
+
if (this.#opts.runName) out.adapterRunName = this.#opts.runName;
|
|
216
|
+
if (runNameArg) out.runName = runNameArg;
|
|
217
|
+
if (this.#opts.traceDir) out.traceDir = this.#opts.traceDir;
|
|
218
|
+
const cap = this.#opts.capture;
|
|
219
|
+
if (cap !== "none" && tags?.length) out.tags = [...tags];
|
|
220
|
+
return out;
|
|
221
|
+
}
|
|
222
|
+
#mergeMetadata(attrs, metadata) {
|
|
223
|
+
if (this.#opts.capture === "none" || !metadata) return;
|
|
224
|
+
attrs.metadata = this.#redactor.redactRecord(toPlainMetadata(metadata));
|
|
225
|
+
}
|
|
226
|
+
#applyPreview(attrs, previews) {
|
|
227
|
+
if (this.#opts.capture !== "preview") return;
|
|
228
|
+
const max = this.#opts.maxPreviewChars;
|
|
229
|
+
for (const [k, v] of Object.entries(previews)) {
|
|
230
|
+
const p = safePreview(v, max);
|
|
231
|
+
if (p !== void 0) attrs[k] = p;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
#pushEvent(ev) {
|
|
235
|
+
try {
|
|
236
|
+
const attributes = ev.attributes ? this.#redactor.redactRecord({ ...ev.attributes }) : void 0;
|
|
237
|
+
this.#events.push({ ...ev, attributes });
|
|
238
|
+
} catch (err) {
|
|
239
|
+
if (!this.#opts.silent) {
|
|
240
|
+
console.error("[agent-inspect:langchain]", err);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
async handleChainStart(chain, inputs, runId, runType, tags, metadata, runName, parentRunId, _extra) {
|
|
245
|
+
this.#ensureRoot(runId, parentRunId);
|
|
246
|
+
this.#rememberStart(runId, "CHAIN");
|
|
247
|
+
const label = serializedLabel(chain) ?? "chain";
|
|
248
|
+
const previews = {};
|
|
249
|
+
if (this.#opts.capture === "preview") previews.inputPreview = inputs;
|
|
250
|
+
const attrs = {
|
|
251
|
+
...this.#baseAttrs(runId, parentRunId, tags, runName)
|
|
252
|
+
};
|
|
253
|
+
this.#mergeMetadata(attrs, metadata);
|
|
254
|
+
this.#applyPreview(attrs, previews);
|
|
255
|
+
this.#pushEvent({
|
|
256
|
+
eventId: `${runId}:CHAIN:start`,
|
|
257
|
+
runId: this.#traceRunId(runId),
|
|
258
|
+
parentId: parentRunId,
|
|
259
|
+
name: `chain:${runName ?? label}`,
|
|
260
|
+
kind: "CHAIN",
|
|
261
|
+
timestamp: Date.now(),
|
|
262
|
+
status: "running",
|
|
263
|
+
attributes: attrs,
|
|
264
|
+
confidence: "explicit",
|
|
265
|
+
source: { type: "adapter" }
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
async handleChainEnd(outputs, runId, parentRunId, tags, _kwargs) {
|
|
269
|
+
this.#ensureRoot(runId, parentRunId);
|
|
270
|
+
const durationMs = this.#durationFor(runId);
|
|
271
|
+
this.#clearStart(runId);
|
|
272
|
+
const previews = {};
|
|
273
|
+
if (this.#opts.capture === "preview") previews.outputPreview = outputs;
|
|
274
|
+
const attrs = {
|
|
275
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0)
|
|
276
|
+
};
|
|
277
|
+
this.#applyPreview(attrs, previews);
|
|
278
|
+
this.#pushEvent({
|
|
279
|
+
eventId: `${runId}:CHAIN:end`,
|
|
280
|
+
runId: this.#traceRunId(runId),
|
|
281
|
+
parentId: parentRunId,
|
|
282
|
+
name: "chain:end",
|
|
283
|
+
kind: "CHAIN",
|
|
284
|
+
timestamp: Date.now(),
|
|
285
|
+
status: "ok",
|
|
286
|
+
durationMs,
|
|
287
|
+
attributes: attrs,
|
|
288
|
+
confidence: "explicit",
|
|
289
|
+
source: { type: "adapter" }
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
async handleChainError(err, runId, parentRunId, tags, _kwargs) {
|
|
293
|
+
this.#ensureRoot(runId, parentRunId);
|
|
294
|
+
const durationMs = this.#durationFor(runId);
|
|
295
|
+
this.#clearStart(runId);
|
|
296
|
+
const { errorName, errorMessage } = errorShape(err);
|
|
297
|
+
const attrs = {
|
|
298
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0),
|
|
299
|
+
errorName,
|
|
300
|
+
errorMessage
|
|
301
|
+
};
|
|
302
|
+
this.#pushEvent({
|
|
303
|
+
eventId: `${runId}:CHAIN:error`,
|
|
304
|
+
runId: this.#traceRunId(runId),
|
|
305
|
+
parentId: parentRunId,
|
|
306
|
+
name: "chain:error",
|
|
307
|
+
kind: "CHAIN",
|
|
308
|
+
timestamp: Date.now(),
|
|
309
|
+
status: "error",
|
|
310
|
+
durationMs,
|
|
311
|
+
attributes: attrs,
|
|
312
|
+
confidence: "explicit",
|
|
313
|
+
source: { type: "adapter" }
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
async handleLLMStart(llm, prompts, runId, parentRunId, _extraParams, tags, metadata, runName) {
|
|
317
|
+
this.#ensureRoot(runId, parentRunId);
|
|
318
|
+
this.#rememberStart(runId, "LLM");
|
|
319
|
+
const model = extractModelName(llm);
|
|
320
|
+
const previews = {};
|
|
321
|
+
if (this.#opts.capture === "preview") {
|
|
322
|
+
previews.promptPreview = prompts.length === 1 ? prompts[0] : prompts;
|
|
323
|
+
}
|
|
324
|
+
const attrs = {
|
|
325
|
+
...this.#baseAttrs(runId, parentRunId, tags, runName)
|
|
326
|
+
};
|
|
327
|
+
if (model && this.#opts.capture !== "none") attrs.model = model;
|
|
328
|
+
this.#mergeMetadata(attrs, metadata);
|
|
329
|
+
this.#applyPreview(attrs, previews);
|
|
330
|
+
this.#pushEvent({
|
|
331
|
+
eventId: `${runId}:LLM:start`,
|
|
332
|
+
runId: this.#traceRunId(runId),
|
|
333
|
+
parentId: parentRunId,
|
|
334
|
+
name: `llm:${model ?? "llm"}`,
|
|
335
|
+
kind: "LLM",
|
|
336
|
+
timestamp: Date.now(),
|
|
337
|
+
status: "running",
|
|
338
|
+
attributes: attrs,
|
|
339
|
+
confidence: "explicit",
|
|
340
|
+
source: { type: "adapter" }
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
async handleChatModelStart(llm, messages, runId, parentRunId, _extraParams, tags, metadata, runName) {
|
|
344
|
+
this.#ensureRoot(runId, parentRunId);
|
|
345
|
+
this.#rememberStart(runId, "LLM");
|
|
346
|
+
const model = extractModelName(llm);
|
|
347
|
+
const previews = {};
|
|
348
|
+
if (this.#opts.capture === "preview") previews.inputPreview = messages;
|
|
349
|
+
const attrs = {
|
|
350
|
+
...this.#baseAttrs(runId, parentRunId, tags, runName)
|
|
351
|
+
};
|
|
352
|
+
if (model && this.#opts.capture !== "none") attrs.model = model;
|
|
353
|
+
this.#mergeMetadata(attrs, metadata);
|
|
354
|
+
this.#applyPreview(attrs, previews);
|
|
355
|
+
this.#pushEvent({
|
|
356
|
+
eventId: `${runId}:CHAT:start`,
|
|
357
|
+
runId: this.#traceRunId(runId),
|
|
358
|
+
parentId: parentRunId,
|
|
359
|
+
name: `llm:${model ?? "llm"}`,
|
|
360
|
+
kind: "LLM",
|
|
361
|
+
timestamp: Date.now(),
|
|
362
|
+
status: "running",
|
|
363
|
+
attributes: attrs,
|
|
364
|
+
confidence: "explicit",
|
|
365
|
+
source: { type: "adapter" }
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
async handleLLMEnd(output, runId, parentRunId, tags, _extraParams) {
|
|
369
|
+
this.#ensureRoot(runId, parentRunId);
|
|
370
|
+
const durationMs = this.#durationFor(runId);
|
|
371
|
+
this.#clearStart(runId);
|
|
372
|
+
const tokens = extractTokenUsage(output);
|
|
373
|
+
const model = extractModelName(output);
|
|
374
|
+
const previews = {};
|
|
375
|
+
if (this.#opts.capture === "preview") previews.outputPreview = output;
|
|
376
|
+
const attrs = {
|
|
377
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0)
|
|
378
|
+
};
|
|
379
|
+
if (model && this.#opts.capture !== "none") attrs.model = model;
|
|
380
|
+
if (tokens && this.#opts.capture !== "none") attrs.tokens = tokens;
|
|
381
|
+
this.#applyPreview(attrs, previews);
|
|
382
|
+
this.#pushEvent({
|
|
383
|
+
eventId: `${runId}:LLM:end`,
|
|
384
|
+
runId: this.#traceRunId(runId),
|
|
385
|
+
parentId: parentRunId,
|
|
386
|
+
name: `llm:${model ?? "llm"}`,
|
|
387
|
+
kind: "LLM",
|
|
388
|
+
timestamp: Date.now(),
|
|
389
|
+
status: "ok",
|
|
390
|
+
durationMs,
|
|
391
|
+
attributes: attrs,
|
|
392
|
+
confidence: "explicit",
|
|
393
|
+
source: { type: "adapter" }
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
async handleLLMError(err, runId, parentRunId, tags, _extraParams) {
|
|
397
|
+
this.#ensureRoot(runId, parentRunId);
|
|
398
|
+
const durationMs = this.#durationFor(runId);
|
|
399
|
+
this.#clearStart(runId);
|
|
400
|
+
const { errorName, errorMessage } = errorShape(err);
|
|
401
|
+
const attrs = {
|
|
402
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0),
|
|
403
|
+
errorName,
|
|
404
|
+
errorMessage
|
|
405
|
+
};
|
|
406
|
+
this.#pushEvent({
|
|
407
|
+
eventId: `${runId}:LLM:error`,
|
|
408
|
+
runId: this.#traceRunId(runId),
|
|
409
|
+
parentId: parentRunId,
|
|
410
|
+
name: "llm:error",
|
|
411
|
+
kind: "LLM",
|
|
412
|
+
timestamp: Date.now(),
|
|
413
|
+
status: "error",
|
|
414
|
+
durationMs,
|
|
415
|
+
attributes: attrs,
|
|
416
|
+
confidence: "explicit",
|
|
417
|
+
source: { type: "adapter" }
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
async handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName, _toolCallId) {
|
|
421
|
+
this.#ensureRoot(runId, parentRunId);
|
|
422
|
+
this.#rememberStart(runId, "TOOL");
|
|
423
|
+
const toolName = serializedLabel(tool) ?? "tool";
|
|
424
|
+
const previews = {};
|
|
425
|
+
if (this.#opts.capture === "preview") previews.inputPreview = input;
|
|
426
|
+
const attrs = {
|
|
427
|
+
...this.#baseAttrs(runId, parentRunId, tags, runName),
|
|
428
|
+
tool: toolName
|
|
429
|
+
};
|
|
430
|
+
this.#mergeMetadata(attrs, metadata);
|
|
431
|
+
this.#applyPreview(attrs, previews);
|
|
432
|
+
this.#pushEvent({
|
|
433
|
+
eventId: `${runId}:TOOL:start`,
|
|
434
|
+
runId: this.#traceRunId(runId),
|
|
435
|
+
parentId: parentRunId,
|
|
436
|
+
name: `tool:${toolName}`,
|
|
437
|
+
kind: "TOOL",
|
|
438
|
+
timestamp: Date.now(),
|
|
439
|
+
status: "running",
|
|
440
|
+
attributes: attrs,
|
|
441
|
+
confidence: "explicit",
|
|
442
|
+
source: { type: "adapter" }
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
async handleToolEnd(output, runId, parentRunId, tags) {
|
|
446
|
+
this.#ensureRoot(runId, parentRunId);
|
|
447
|
+
const durationMs = this.#durationFor(runId);
|
|
448
|
+
this.#clearStart(runId);
|
|
449
|
+
const previews = {};
|
|
450
|
+
if (this.#opts.capture === "preview") previews.outputPreview = output;
|
|
451
|
+
const attrs = {
|
|
452
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0)
|
|
453
|
+
};
|
|
454
|
+
this.#applyPreview(attrs, previews);
|
|
455
|
+
this.#pushEvent({
|
|
456
|
+
eventId: `${runId}:TOOL:end`,
|
|
457
|
+
runId: this.#traceRunId(runId),
|
|
458
|
+
parentId: parentRunId,
|
|
459
|
+
name: "tool:end",
|
|
460
|
+
kind: "TOOL",
|
|
461
|
+
timestamp: Date.now(),
|
|
462
|
+
status: "ok",
|
|
463
|
+
durationMs,
|
|
464
|
+
attributes: attrs,
|
|
465
|
+
confidence: "explicit",
|
|
466
|
+
source: { type: "adapter" }
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
async handleToolError(err, runId, parentRunId, tags) {
|
|
470
|
+
this.#ensureRoot(runId, parentRunId);
|
|
471
|
+
const durationMs = this.#durationFor(runId);
|
|
472
|
+
this.#clearStart(runId);
|
|
473
|
+
const { errorName, errorMessage } = errorShape(err);
|
|
474
|
+
const attrs = {
|
|
475
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0),
|
|
476
|
+
errorName,
|
|
477
|
+
errorMessage
|
|
478
|
+
};
|
|
479
|
+
this.#pushEvent({
|
|
480
|
+
eventId: `${runId}:TOOL:error`,
|
|
481
|
+
runId: this.#traceRunId(runId),
|
|
482
|
+
parentId: parentRunId,
|
|
483
|
+
name: "tool:error",
|
|
484
|
+
kind: "TOOL",
|
|
485
|
+
timestamp: Date.now(),
|
|
486
|
+
status: "error",
|
|
487
|
+
durationMs,
|
|
488
|
+
attributes: attrs,
|
|
489
|
+
confidence: "explicit",
|
|
490
|
+
source: { type: "adapter" }
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
async handleRetrieverStart(retriever, _query, runId, parentRunId, tags, metadata, name) {
|
|
494
|
+
this.#ensureRoot(runId, parentRunId);
|
|
495
|
+
this.#rememberStart(runId, "RETRIEVER");
|
|
496
|
+
const rname = name ?? serializedLabel(retriever) ?? "retriever";
|
|
497
|
+
const attrs = {
|
|
498
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0),
|
|
499
|
+
retriever: rname
|
|
500
|
+
};
|
|
501
|
+
this.#mergeMetadata(attrs, metadata);
|
|
502
|
+
this.#pushEvent({
|
|
503
|
+
eventId: `${runId}:RETRIEVER:start`,
|
|
504
|
+
runId: this.#traceRunId(runId),
|
|
505
|
+
parentId: parentRunId,
|
|
506
|
+
name: `retriever:${rname}`,
|
|
507
|
+
kind: "RETRIEVER",
|
|
508
|
+
timestamp: Date.now(),
|
|
509
|
+
status: "running",
|
|
510
|
+
attributes: attrs,
|
|
511
|
+
confidence: "explicit",
|
|
512
|
+
source: { type: "adapter" }
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
async handleRetrieverEnd(documents, runId, parentRunId, tags) {
|
|
516
|
+
this.#ensureRoot(runId, parentRunId);
|
|
517
|
+
const durationMs = this.#durationFor(runId);
|
|
518
|
+
this.#clearStart(runId);
|
|
519
|
+
const previews = {};
|
|
520
|
+
if (this.#opts.capture === "preview" && documents.length > 0) {
|
|
521
|
+
previews.documentPreview = documents.slice(0, 3);
|
|
522
|
+
}
|
|
523
|
+
const attrs = {
|
|
524
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0),
|
|
525
|
+
documentCount: documents.length
|
|
526
|
+
};
|
|
527
|
+
this.#applyPreview(attrs, previews);
|
|
528
|
+
this.#pushEvent({
|
|
529
|
+
eventId: `${runId}:RETRIEVER:end`,
|
|
530
|
+
runId: this.#traceRunId(runId),
|
|
531
|
+
parentId: parentRunId,
|
|
532
|
+
name: "retriever:end",
|
|
533
|
+
kind: "RETRIEVER",
|
|
534
|
+
timestamp: Date.now(),
|
|
535
|
+
status: "ok",
|
|
536
|
+
durationMs,
|
|
537
|
+
attributes: attrs,
|
|
538
|
+
confidence: "explicit",
|
|
539
|
+
source: { type: "adapter" }
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
async handleRetrieverError(err, runId, parentRunId, tags) {
|
|
543
|
+
this.#ensureRoot(runId, parentRunId);
|
|
544
|
+
const durationMs = this.#durationFor(runId);
|
|
545
|
+
this.#clearStart(runId);
|
|
546
|
+
const { errorName, errorMessage } = errorShape(err);
|
|
547
|
+
const attrs = {
|
|
548
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0),
|
|
549
|
+
errorName,
|
|
550
|
+
errorMessage
|
|
551
|
+
};
|
|
552
|
+
this.#pushEvent({
|
|
553
|
+
eventId: `${runId}:RETRIEVER:error`,
|
|
554
|
+
runId: this.#traceRunId(runId),
|
|
555
|
+
parentId: parentRunId,
|
|
556
|
+
name: "retriever:error",
|
|
557
|
+
kind: "RETRIEVER",
|
|
558
|
+
timestamp: Date.now(),
|
|
559
|
+
status: "error",
|
|
560
|
+
durationMs,
|
|
561
|
+
attributes: attrs,
|
|
562
|
+
confidence: "explicit",
|
|
563
|
+
source: { type: "adapter" }
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
async handleAgentAction(action, runId, parentRunId, tags) {
|
|
567
|
+
this.#ensureRoot(runId, parentRunId);
|
|
568
|
+
const attrs = {
|
|
569
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0),
|
|
570
|
+
tool: action.tool
|
|
571
|
+
};
|
|
572
|
+
if (this.#opts.capture === "preview") {
|
|
573
|
+
this.#applyPreview(attrs, {
|
|
574
|
+
toolInputPreview: action.toolInput,
|
|
575
|
+
logPreview: action.log
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
this.#pushEvent({
|
|
579
|
+
eventId: `${runId}:AGENT:action:${Date.now()}`,
|
|
580
|
+
runId: this.#traceRunId(runId),
|
|
581
|
+
parentId: parentRunId,
|
|
582
|
+
name: "agent:action",
|
|
583
|
+
kind: "DECISION",
|
|
584
|
+
timestamp: Date.now(),
|
|
585
|
+
status: "ok",
|
|
586
|
+
attributes: attrs,
|
|
587
|
+
confidence: "explicit",
|
|
588
|
+
source: { type: "adapter" }
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
async handleAgentEnd(finish, runId, parentRunId, tags) {
|
|
592
|
+
this.#ensureRoot(runId, parentRunId);
|
|
593
|
+
const attrs = {
|
|
594
|
+
...this.#baseAttrs(runId, parentRunId, tags, void 0)
|
|
595
|
+
};
|
|
596
|
+
if (this.#opts.capture === "preview") {
|
|
597
|
+
this.#applyPreview(attrs, {
|
|
598
|
+
outputPreview: finish.returnValues,
|
|
599
|
+
logPreview: finish.log
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
this.#pushEvent({
|
|
603
|
+
eventId: `${runId}:AGENT:end:${Date.now()}`,
|
|
604
|
+
runId: this.#traceRunId(runId),
|
|
605
|
+
parentId: parentRunId,
|
|
606
|
+
name: "agent:end",
|
|
607
|
+
kind: "AGENT",
|
|
608
|
+
timestamp: Date.now(),
|
|
609
|
+
status: "ok",
|
|
610
|
+
attributes: attrs,
|
|
611
|
+
confidence: "explicit",
|
|
612
|
+
source: { type: "adapter" }
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
exports.AgentInspectCallback = AgentInspectCallback;
|
|
618
|
+
exports.extractModelName = extractModelName;
|
|
619
|
+
exports.extractTokenUsage = extractTokenUsage;
|
|
620
|
+
exports.safePreview = safePreview;
|
|
621
|
+
exports.toPlainMetadata = toPlainMetadata;
|
|
622
|
+
//# sourceMappingURL=index.cjs.map
|
|
623
|
+
//# sourceMappingURL=index.cjs.map
|