@agent-inspect/langchain 1.0.3 → 1.1.0
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/dist/index.cjs +306 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.mjs +307 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -147,6 +147,217 @@ function toPlainMetadata(value) {
|
|
|
147
147
|
return {};
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
|
+
function kindToStepType(kind) {
|
|
151
|
+
switch (kind) {
|
|
152
|
+
case "LLM":
|
|
153
|
+
return "llm";
|
|
154
|
+
case "TOOL":
|
|
155
|
+
return "tool";
|
|
156
|
+
case "DECISION":
|
|
157
|
+
return "decision";
|
|
158
|
+
default:
|
|
159
|
+
return "logic";
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function toStepMetadata(attrs) {
|
|
163
|
+
const out = {
|
|
164
|
+
adapter: "langchain",
|
|
165
|
+
confidence: "explicit"
|
|
166
|
+
};
|
|
167
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
168
|
+
out[k] = v;
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
var LangChainTracePersistence = class {
|
|
173
|
+
#traceDir;
|
|
174
|
+
#runId;
|
|
175
|
+
#runName;
|
|
176
|
+
#standalone;
|
|
177
|
+
#silent;
|
|
178
|
+
#safety;
|
|
179
|
+
#runStarted = false;
|
|
180
|
+
#runCompleted = false;
|
|
181
|
+
#runStartTime;
|
|
182
|
+
#rootLcRunId;
|
|
183
|
+
#lcToStepId = /* @__PURE__ */ new Map();
|
|
184
|
+
constructor(options = {}) {
|
|
185
|
+
const inContext = agentInspect.hasActiveContext();
|
|
186
|
+
this.#standalone = !inContext;
|
|
187
|
+
this.#silent = options.silent ?? false;
|
|
188
|
+
this.#traceDir = inContext ? agentInspect.getTraceDirFromContext() ?? agentInspect.resolveTraceDir({ dir: options.traceDir }) : agentInspect.resolveTraceDir({ dir: options.traceDir });
|
|
189
|
+
this.#runId = (inContext ? agentInspect.getCurrentRunId() : void 0) ?? options.runId ?? agentInspect.createRunId();
|
|
190
|
+
this.#runName = options.runName ?? "langchain-agent";
|
|
191
|
+
this.#safety = agentInspect.resolveTraceSafetyOptions({
|
|
192
|
+
redact: options.redact ? { rules: options.redact } : true,
|
|
193
|
+
maxPreviewLength: options.maxPreviewChars
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
get runId() {
|
|
197
|
+
return this.#runId;
|
|
198
|
+
}
|
|
199
|
+
get traceDir() {
|
|
200
|
+
return this.#traceDir;
|
|
201
|
+
}
|
|
202
|
+
reset() {
|
|
203
|
+
this.#runStarted = false;
|
|
204
|
+
this.#runCompleted = false;
|
|
205
|
+
this.#runStartTime = void 0;
|
|
206
|
+
this.#rootLcRunId = void 0;
|
|
207
|
+
this.#lcToStepId.clear();
|
|
208
|
+
}
|
|
209
|
+
noteRoot(lcRunId, parentRunId) {
|
|
210
|
+
if (!parentRunId && !this.#rootLcRunId) {
|
|
211
|
+
this.#rootLcRunId = lcRunId;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
resolveParentId(lcParentRunId) {
|
|
215
|
+
if (!lcParentRunId) return void 0;
|
|
216
|
+
return this.#lcToStepId.get(lcParentRunId);
|
|
217
|
+
}
|
|
218
|
+
async onStepStart(params) {
|
|
219
|
+
try {
|
|
220
|
+
this.noteRoot(params.lcRunId, params.lcParentRunId);
|
|
221
|
+
if (this.#standalone && !this.#runStarted) {
|
|
222
|
+
await this.#ensureRunStarted(params.startTime, params.attributes);
|
|
223
|
+
}
|
|
224
|
+
const stepId = agentInspect.createStepId();
|
|
225
|
+
this.#lcToStepId.set(params.lcRunId, stepId);
|
|
226
|
+
const parentId = this.resolveParentId(params.lcParentRunId);
|
|
227
|
+
const event = {
|
|
228
|
+
schemaVersion: "0.1",
|
|
229
|
+
event: "step_started",
|
|
230
|
+
timestamp: params.startTime,
|
|
231
|
+
runId: this.#runId,
|
|
232
|
+
stepId,
|
|
233
|
+
...parentId ? { parentId } : {},
|
|
234
|
+
name: params.name,
|
|
235
|
+
type: kindToStepType(params.kind),
|
|
236
|
+
startTime: params.startTime,
|
|
237
|
+
metadata: toStepMetadata(params.attributes)
|
|
238
|
+
};
|
|
239
|
+
await this.#write(event);
|
|
240
|
+
} catch (err) {
|
|
241
|
+
this.#warn(err);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
async onStepEnd(params) {
|
|
245
|
+
try {
|
|
246
|
+
const stepId = this.#lcToStepId.get(params.lcRunId);
|
|
247
|
+
if (!stepId) return;
|
|
248
|
+
const durationMs = typeof params.durationMs === "number" && Number.isFinite(params.durationMs) ? Math.max(0, Math.floor(params.durationMs)) : Math.max(0, params.endTime - (this.#runStartTime ?? params.endTime));
|
|
249
|
+
const event = {
|
|
250
|
+
schemaVersion: "0.1",
|
|
251
|
+
event: "step_completed",
|
|
252
|
+
timestamp: params.endTime,
|
|
253
|
+
runId: this.#runId,
|
|
254
|
+
stepId,
|
|
255
|
+
status: params.status,
|
|
256
|
+
endTime: params.endTime,
|
|
257
|
+
durationMs,
|
|
258
|
+
...params.status === "error" && params.errorMessage ? { error: { message: params.errorMessage } } : {}
|
|
259
|
+
};
|
|
260
|
+
await this.#write(event);
|
|
261
|
+
if (this.#standalone && !this.#runCompleted && this.#rootLcRunId === params.lcRunId && !params.lcParentRunId) {
|
|
262
|
+
await this.#ensureRunCompleted(
|
|
263
|
+
params.endTime,
|
|
264
|
+
params.status,
|
|
265
|
+
params.errorMessage
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
} catch (err) {
|
|
269
|
+
this.#warn(err);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/** Point-in-time adapter events (e.g. agent action) — writes start + completed pair. */
|
|
273
|
+
async onInstantStep(params) {
|
|
274
|
+
try {
|
|
275
|
+
this.noteRoot(params.lcRunId, params.lcParentRunId);
|
|
276
|
+
if (this.#standalone && !this.#runStarted) {
|
|
277
|
+
await this.#ensureRunStarted(params.timestamp, params.attributes);
|
|
278
|
+
}
|
|
279
|
+
const stepId = agentInspect.createStepId();
|
|
280
|
+
this.#lcToStepId.set(params.lcRunId, stepId);
|
|
281
|
+
const parentId = this.resolveParentId(params.lcParentRunId);
|
|
282
|
+
const started = {
|
|
283
|
+
schemaVersion: "0.1",
|
|
284
|
+
event: "step_started",
|
|
285
|
+
timestamp: params.timestamp,
|
|
286
|
+
runId: this.#runId,
|
|
287
|
+
stepId,
|
|
288
|
+
...parentId ? { parentId } : {},
|
|
289
|
+
name: params.name,
|
|
290
|
+
type: kindToStepType(params.kind),
|
|
291
|
+
startTime: params.timestamp,
|
|
292
|
+
metadata: toStepMetadata(params.attributes)
|
|
293
|
+
};
|
|
294
|
+
await this.#write(started);
|
|
295
|
+
const completed = {
|
|
296
|
+
schemaVersion: "0.1",
|
|
297
|
+
event: "step_completed",
|
|
298
|
+
timestamp: params.timestamp,
|
|
299
|
+
runId: this.#runId,
|
|
300
|
+
stepId,
|
|
301
|
+
status: params.status,
|
|
302
|
+
endTime: params.timestamp,
|
|
303
|
+
durationMs: 0,
|
|
304
|
+
...params.status === "error" && params.errorMessage ? { error: { message: params.errorMessage } } : {}
|
|
305
|
+
};
|
|
306
|
+
await this.#write(completed);
|
|
307
|
+
} catch (err) {
|
|
308
|
+
this.#warn(err);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
async #ensureRunStarted(startTime, attrs) {
|
|
312
|
+
if (this.#runStarted) return;
|
|
313
|
+
this.#runStarted = true;
|
|
314
|
+
this.#runStartTime = startTime;
|
|
315
|
+
await agentInspect.initializeTraceFile(this.#runId, this.#traceDir);
|
|
316
|
+
const metadata = {
|
|
317
|
+
adapter: "langchain",
|
|
318
|
+
confidence: "explicit"
|
|
319
|
+
};
|
|
320
|
+
if (attrs.langchainRunId) metadata.langchainRunId = attrs.langchainRunId;
|
|
321
|
+
if (attrs.adapterRunName) metadata.adapterRunName = attrs.adapterRunName;
|
|
322
|
+
const event = {
|
|
323
|
+
schemaVersion: "0.1",
|
|
324
|
+
event: "run_started",
|
|
325
|
+
timestamp: startTime,
|
|
326
|
+
runId: this.#runId,
|
|
327
|
+
name: this.#runName,
|
|
328
|
+
startTime,
|
|
329
|
+
metadata
|
|
330
|
+
};
|
|
331
|
+
await this.#write(event);
|
|
332
|
+
}
|
|
333
|
+
async #ensureRunCompleted(endTime, stepStatus, errorMessage) {
|
|
334
|
+
if (this.#runCompleted || !this.#runStarted) return;
|
|
335
|
+
this.#runCompleted = true;
|
|
336
|
+
const startTime = this.#runStartTime ?? endTime;
|
|
337
|
+
const durationMs = Math.max(0, endTime - startTime);
|
|
338
|
+
const runStatus = stepStatus === "error" ? "error" : "success";
|
|
339
|
+
const event = {
|
|
340
|
+
schemaVersion: "0.1",
|
|
341
|
+
event: "run_completed",
|
|
342
|
+
timestamp: endTime,
|
|
343
|
+
runId: this.#runId,
|
|
344
|
+
status: runStatus,
|
|
345
|
+
endTime,
|
|
346
|
+
durationMs,
|
|
347
|
+
...runStatus === "error" && errorMessage ? { error: { message: errorMessage } } : {}
|
|
348
|
+
};
|
|
349
|
+
await this.#write(event);
|
|
350
|
+
}
|
|
351
|
+
async #write(event) {
|
|
352
|
+
const safe = agentInspect.prepareTraceEventForDisk(event, this.#safety);
|
|
353
|
+
await agentInspect.writeTraceEvent(safe, this.#traceDir);
|
|
354
|
+
}
|
|
355
|
+
#warn(err) {
|
|
356
|
+
if (!this.#silent) {
|
|
357
|
+
console.error("[agent-inspect:langchain]", err);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
};
|
|
150
361
|
|
|
151
362
|
// packages/langchain/src/agent-inspect-callback.ts
|
|
152
363
|
function serializedLabel(s) {
|
|
@@ -164,6 +375,7 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
164
375
|
name = "agent-inspect";
|
|
165
376
|
#opts;
|
|
166
377
|
#redactor;
|
|
378
|
+
#persistence;
|
|
167
379
|
#events = [];
|
|
168
380
|
#starts = /* @__PURE__ */ new Map();
|
|
169
381
|
#rootRunId;
|
|
@@ -173,9 +385,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
173
385
|
capture: options.capture ?? "metadata-only",
|
|
174
386
|
silent: options.silent ?? false,
|
|
175
387
|
maxPreviewChars: options.maxPreviewChars ?? 200,
|
|
388
|
+
persist: options.persist ?? false,
|
|
389
|
+
runName: options.runName ?? "langchain-agent",
|
|
176
390
|
...options
|
|
177
391
|
};
|
|
178
392
|
this.#redactor = new agentInspect.Redactor({ rules: this.#opts.redact });
|
|
393
|
+
if (this.#opts.persist) {
|
|
394
|
+
this.#persistence = new LangChainTracePersistence({
|
|
395
|
+
runName: this.#opts.runName,
|
|
396
|
+
traceDir: this.#opts.traceDir,
|
|
397
|
+
runId: this.#opts.runId,
|
|
398
|
+
redact: this.#opts.redact,
|
|
399
|
+
silent: this.#opts.silent,
|
|
400
|
+
maxPreviewChars: this.#opts.maxPreviewChars
|
|
401
|
+
});
|
|
402
|
+
}
|
|
179
403
|
}
|
|
180
404
|
getEvents() {
|
|
181
405
|
return this.#events.map((e) => ({
|
|
@@ -188,6 +412,7 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
188
412
|
this.#events = [];
|
|
189
413
|
this.#starts.clear();
|
|
190
414
|
this.#rootRunId = void 0;
|
|
415
|
+
this.#persistence?.reset();
|
|
191
416
|
}
|
|
192
417
|
#ensureRoot(lcRunId, parentRunId) {
|
|
193
418
|
if (parentRunId) return;
|
|
@@ -241,6 +466,38 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
241
466
|
}
|
|
242
467
|
}
|
|
243
468
|
}
|
|
469
|
+
async #persistStepStart(lcRunId, lcParentRunId, name, kind, attrs, startTime) {
|
|
470
|
+
await this.#persistence?.onStepStart({
|
|
471
|
+
lcRunId,
|
|
472
|
+
lcParentRunId,
|
|
473
|
+
name,
|
|
474
|
+
kind,
|
|
475
|
+
startTime,
|
|
476
|
+
attributes: attrs
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
async #persistStepEnd(lcRunId, lcParentRunId, status, endTime, durationMs, errorMessage) {
|
|
480
|
+
await this.#persistence?.onStepEnd({
|
|
481
|
+
lcRunId,
|
|
482
|
+
lcParentRunId,
|
|
483
|
+
endTime,
|
|
484
|
+
durationMs,
|
|
485
|
+
status,
|
|
486
|
+
errorMessage
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
async #persistInstant(lcRunId, lcParentRunId, name, kind, attrs, status, errorMessage) {
|
|
490
|
+
await this.#persistence?.onInstantStep({
|
|
491
|
+
lcRunId,
|
|
492
|
+
lcParentRunId,
|
|
493
|
+
name,
|
|
494
|
+
kind,
|
|
495
|
+
timestamp: Date.now(),
|
|
496
|
+
attributes: attrs,
|
|
497
|
+
status,
|
|
498
|
+
errorMessage
|
|
499
|
+
});
|
|
500
|
+
}
|
|
244
501
|
async handleChainStart(chain, inputs, runId, runType, tags, metadata, runName, parentRunId, _extra) {
|
|
245
502
|
this.#ensureRoot(runId, parentRunId);
|
|
246
503
|
this.#rememberStart(runId, "CHAIN");
|
|
@@ -252,18 +509,20 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
252
509
|
};
|
|
253
510
|
this.#mergeMetadata(attrs, metadata);
|
|
254
511
|
this.#applyPreview(attrs, previews);
|
|
512
|
+
const ts = Date.now();
|
|
255
513
|
this.#pushEvent({
|
|
256
514
|
eventId: `${runId}:CHAIN:start`,
|
|
257
515
|
runId: this.#traceRunId(runId),
|
|
258
516
|
parentId: parentRunId,
|
|
259
517
|
name: `chain:${runName ?? label}`,
|
|
260
518
|
kind: "CHAIN",
|
|
261
|
-
timestamp:
|
|
519
|
+
timestamp: ts,
|
|
262
520
|
status: "running",
|
|
263
521
|
attributes: attrs,
|
|
264
522
|
confidence: "explicit",
|
|
265
523
|
source: { type: "adapter" }
|
|
266
524
|
});
|
|
525
|
+
await this.#persistStepStart(runId, parentRunId, `chain:${runName ?? label}`, "CHAIN", attrs, ts);
|
|
267
526
|
}
|
|
268
527
|
async handleChainEnd(outputs, runId, parentRunId, tags, _kwargs) {
|
|
269
528
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -275,19 +534,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
275
534
|
...this.#baseAttrs(runId, parentRunId, tags, void 0)
|
|
276
535
|
};
|
|
277
536
|
this.#applyPreview(attrs, previews);
|
|
537
|
+
const ts = Date.now();
|
|
278
538
|
this.#pushEvent({
|
|
279
539
|
eventId: `${runId}:CHAIN:end`,
|
|
280
540
|
runId: this.#traceRunId(runId),
|
|
281
541
|
parentId: parentRunId,
|
|
282
542
|
name: "chain:end",
|
|
283
543
|
kind: "CHAIN",
|
|
284
|
-
timestamp:
|
|
544
|
+
timestamp: ts,
|
|
285
545
|
status: "ok",
|
|
286
546
|
durationMs,
|
|
287
547
|
attributes: attrs,
|
|
288
548
|
confidence: "explicit",
|
|
289
549
|
source: { type: "adapter" }
|
|
290
550
|
});
|
|
551
|
+
await this.#persistStepEnd(runId, parentRunId, "success", ts, durationMs);
|
|
291
552
|
}
|
|
292
553
|
async handleChainError(err, runId, parentRunId, tags, _kwargs) {
|
|
293
554
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -299,19 +560,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
299
560
|
errorName,
|
|
300
561
|
errorMessage
|
|
301
562
|
};
|
|
563
|
+
const ts = Date.now();
|
|
302
564
|
this.#pushEvent({
|
|
303
565
|
eventId: `${runId}:CHAIN:error`,
|
|
304
566
|
runId: this.#traceRunId(runId),
|
|
305
567
|
parentId: parentRunId,
|
|
306
568
|
name: "chain:error",
|
|
307
569
|
kind: "CHAIN",
|
|
308
|
-
timestamp:
|
|
570
|
+
timestamp: ts,
|
|
309
571
|
status: "error",
|
|
310
572
|
durationMs,
|
|
311
573
|
attributes: attrs,
|
|
312
574
|
confidence: "explicit",
|
|
313
575
|
source: { type: "adapter" }
|
|
314
576
|
});
|
|
577
|
+
await this.#persistStepEnd(runId, parentRunId, "error", ts, durationMs, errorMessage);
|
|
315
578
|
}
|
|
316
579
|
async handleLLMStart(llm, prompts, runId, parentRunId, _extraParams, tags, metadata, runName) {
|
|
317
580
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -327,18 +590,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
327
590
|
if (model && this.#opts.capture !== "none") attrs.model = model;
|
|
328
591
|
this.#mergeMetadata(attrs, metadata);
|
|
329
592
|
this.#applyPreview(attrs, previews);
|
|
593
|
+
const ts = Date.now();
|
|
594
|
+
const stepName = `llm:${model ?? "llm"}`;
|
|
330
595
|
this.#pushEvent({
|
|
331
596
|
eventId: `${runId}:LLM:start`,
|
|
332
597
|
runId: this.#traceRunId(runId),
|
|
333
598
|
parentId: parentRunId,
|
|
334
|
-
name:
|
|
599
|
+
name: stepName,
|
|
335
600
|
kind: "LLM",
|
|
336
|
-
timestamp:
|
|
601
|
+
timestamp: ts,
|
|
337
602
|
status: "running",
|
|
338
603
|
attributes: attrs,
|
|
339
604
|
confidence: "explicit",
|
|
340
605
|
source: { type: "adapter" }
|
|
341
606
|
});
|
|
607
|
+
await this.#persistStepStart(runId, parentRunId, stepName, "LLM", attrs, ts);
|
|
342
608
|
}
|
|
343
609
|
async handleChatModelStart(llm, messages, runId, parentRunId, _extraParams, tags, metadata, runName) {
|
|
344
610
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -352,18 +618,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
352
618
|
if (model && this.#opts.capture !== "none") attrs.model = model;
|
|
353
619
|
this.#mergeMetadata(attrs, metadata);
|
|
354
620
|
this.#applyPreview(attrs, previews);
|
|
621
|
+
const ts = Date.now();
|
|
622
|
+
const stepName = `llm:${model ?? "llm"}`;
|
|
355
623
|
this.#pushEvent({
|
|
356
624
|
eventId: `${runId}:CHAT:start`,
|
|
357
625
|
runId: this.#traceRunId(runId),
|
|
358
626
|
parentId: parentRunId,
|
|
359
|
-
name:
|
|
627
|
+
name: stepName,
|
|
360
628
|
kind: "LLM",
|
|
361
|
-
timestamp:
|
|
629
|
+
timestamp: ts,
|
|
362
630
|
status: "running",
|
|
363
631
|
attributes: attrs,
|
|
364
632
|
confidence: "explicit",
|
|
365
633
|
source: { type: "adapter" }
|
|
366
634
|
});
|
|
635
|
+
await this.#persistStepStart(runId, parentRunId, stepName, "LLM", attrs, ts);
|
|
367
636
|
}
|
|
368
637
|
async handleLLMEnd(output, runId, parentRunId, tags, _extraParams) {
|
|
369
638
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -379,19 +648,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
379
648
|
if (model && this.#opts.capture !== "none") attrs.model = model;
|
|
380
649
|
if (tokens && this.#opts.capture !== "none") attrs.tokens = tokens;
|
|
381
650
|
this.#applyPreview(attrs, previews);
|
|
651
|
+
const ts = Date.now();
|
|
382
652
|
this.#pushEvent({
|
|
383
653
|
eventId: `${runId}:LLM:end`,
|
|
384
654
|
runId: this.#traceRunId(runId),
|
|
385
655
|
parentId: parentRunId,
|
|
386
656
|
name: `llm:${model ?? "llm"}`,
|
|
387
657
|
kind: "LLM",
|
|
388
|
-
timestamp:
|
|
658
|
+
timestamp: ts,
|
|
389
659
|
status: "ok",
|
|
390
660
|
durationMs,
|
|
391
661
|
attributes: attrs,
|
|
392
662
|
confidence: "explicit",
|
|
393
663
|
source: { type: "adapter" }
|
|
394
664
|
});
|
|
665
|
+
await this.#persistStepEnd(runId, parentRunId, "success", ts, durationMs);
|
|
395
666
|
}
|
|
396
667
|
async handleLLMError(err, runId, parentRunId, tags, _extraParams) {
|
|
397
668
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -403,19 +674,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
403
674
|
errorName,
|
|
404
675
|
errorMessage
|
|
405
676
|
};
|
|
677
|
+
const ts = Date.now();
|
|
406
678
|
this.#pushEvent({
|
|
407
679
|
eventId: `${runId}:LLM:error`,
|
|
408
680
|
runId: this.#traceRunId(runId),
|
|
409
681
|
parentId: parentRunId,
|
|
410
682
|
name: "llm:error",
|
|
411
683
|
kind: "LLM",
|
|
412
|
-
timestamp:
|
|
684
|
+
timestamp: ts,
|
|
413
685
|
status: "error",
|
|
414
686
|
durationMs,
|
|
415
687
|
attributes: attrs,
|
|
416
688
|
confidence: "explicit",
|
|
417
689
|
source: { type: "adapter" }
|
|
418
690
|
});
|
|
691
|
+
await this.#persistStepEnd(runId, parentRunId, "error", ts, durationMs, errorMessage);
|
|
419
692
|
}
|
|
420
693
|
async handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName, _toolCallId) {
|
|
421
694
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -429,18 +702,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
429
702
|
};
|
|
430
703
|
this.#mergeMetadata(attrs, metadata);
|
|
431
704
|
this.#applyPreview(attrs, previews);
|
|
705
|
+
const ts = Date.now();
|
|
706
|
+
const stepName = `tool:${toolName}`;
|
|
432
707
|
this.#pushEvent({
|
|
433
708
|
eventId: `${runId}:TOOL:start`,
|
|
434
709
|
runId: this.#traceRunId(runId),
|
|
435
710
|
parentId: parentRunId,
|
|
436
|
-
name:
|
|
711
|
+
name: stepName,
|
|
437
712
|
kind: "TOOL",
|
|
438
|
-
timestamp:
|
|
713
|
+
timestamp: ts,
|
|
439
714
|
status: "running",
|
|
440
715
|
attributes: attrs,
|
|
441
716
|
confidence: "explicit",
|
|
442
717
|
source: { type: "adapter" }
|
|
443
718
|
});
|
|
719
|
+
await this.#persistStepStart(runId, parentRunId, stepName, "TOOL", attrs, ts);
|
|
444
720
|
}
|
|
445
721
|
async handleToolEnd(output, runId, parentRunId, tags) {
|
|
446
722
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -452,19 +728,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
452
728
|
...this.#baseAttrs(runId, parentRunId, tags, void 0)
|
|
453
729
|
};
|
|
454
730
|
this.#applyPreview(attrs, previews);
|
|
731
|
+
const ts = Date.now();
|
|
455
732
|
this.#pushEvent({
|
|
456
733
|
eventId: `${runId}:TOOL:end`,
|
|
457
734
|
runId: this.#traceRunId(runId),
|
|
458
735
|
parentId: parentRunId,
|
|
459
736
|
name: "tool:end",
|
|
460
737
|
kind: "TOOL",
|
|
461
|
-
timestamp:
|
|
738
|
+
timestamp: ts,
|
|
462
739
|
status: "ok",
|
|
463
740
|
durationMs,
|
|
464
741
|
attributes: attrs,
|
|
465
742
|
confidence: "explicit",
|
|
466
743
|
source: { type: "adapter" }
|
|
467
744
|
});
|
|
745
|
+
await this.#persistStepEnd(runId, parentRunId, "success", ts, durationMs);
|
|
468
746
|
}
|
|
469
747
|
async handleToolError(err, runId, parentRunId, tags) {
|
|
470
748
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -476,19 +754,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
476
754
|
errorName,
|
|
477
755
|
errorMessage
|
|
478
756
|
};
|
|
757
|
+
const ts = Date.now();
|
|
479
758
|
this.#pushEvent({
|
|
480
759
|
eventId: `${runId}:TOOL:error`,
|
|
481
760
|
runId: this.#traceRunId(runId),
|
|
482
761
|
parentId: parentRunId,
|
|
483
762
|
name: "tool:error",
|
|
484
763
|
kind: "TOOL",
|
|
485
|
-
timestamp:
|
|
764
|
+
timestamp: ts,
|
|
486
765
|
status: "error",
|
|
487
766
|
durationMs,
|
|
488
767
|
attributes: attrs,
|
|
489
768
|
confidence: "explicit",
|
|
490
769
|
source: { type: "adapter" }
|
|
491
770
|
});
|
|
771
|
+
await this.#persistStepEnd(runId, parentRunId, "error", ts, durationMs, errorMessage);
|
|
492
772
|
}
|
|
493
773
|
async handleRetrieverStart(retriever, _query, runId, parentRunId, tags, metadata, name) {
|
|
494
774
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -499,18 +779,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
499
779
|
retriever: rname
|
|
500
780
|
};
|
|
501
781
|
this.#mergeMetadata(attrs, metadata);
|
|
782
|
+
const ts = Date.now();
|
|
783
|
+
const stepName = `retriever:${rname}`;
|
|
502
784
|
this.#pushEvent({
|
|
503
785
|
eventId: `${runId}:RETRIEVER:start`,
|
|
504
786
|
runId: this.#traceRunId(runId),
|
|
505
787
|
parentId: parentRunId,
|
|
506
|
-
name:
|
|
788
|
+
name: stepName,
|
|
507
789
|
kind: "RETRIEVER",
|
|
508
|
-
timestamp:
|
|
790
|
+
timestamp: ts,
|
|
509
791
|
status: "running",
|
|
510
792
|
attributes: attrs,
|
|
511
793
|
confidence: "explicit",
|
|
512
794
|
source: { type: "adapter" }
|
|
513
795
|
});
|
|
796
|
+
await this.#persistStepStart(runId, parentRunId, stepName, "RETRIEVER", attrs, ts);
|
|
514
797
|
}
|
|
515
798
|
async handleRetrieverEnd(documents, runId, parentRunId, tags) {
|
|
516
799
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -525,19 +808,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
525
808
|
documentCount: documents.length
|
|
526
809
|
};
|
|
527
810
|
this.#applyPreview(attrs, previews);
|
|
811
|
+
const ts = Date.now();
|
|
528
812
|
this.#pushEvent({
|
|
529
813
|
eventId: `${runId}:RETRIEVER:end`,
|
|
530
814
|
runId: this.#traceRunId(runId),
|
|
531
815
|
parentId: parentRunId,
|
|
532
816
|
name: "retriever:end",
|
|
533
817
|
kind: "RETRIEVER",
|
|
534
|
-
timestamp:
|
|
818
|
+
timestamp: ts,
|
|
535
819
|
status: "ok",
|
|
536
820
|
durationMs,
|
|
537
821
|
attributes: attrs,
|
|
538
822
|
confidence: "explicit",
|
|
539
823
|
source: { type: "adapter" }
|
|
540
824
|
});
|
|
825
|
+
await this.#persistStepEnd(runId, parentRunId, "success", ts, durationMs);
|
|
541
826
|
}
|
|
542
827
|
async handleRetrieverError(err, runId, parentRunId, tags) {
|
|
543
828
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -549,19 +834,21 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
549
834
|
errorName,
|
|
550
835
|
errorMessage
|
|
551
836
|
};
|
|
837
|
+
const ts = Date.now();
|
|
552
838
|
this.#pushEvent({
|
|
553
839
|
eventId: `${runId}:RETRIEVER:error`,
|
|
554
840
|
runId: this.#traceRunId(runId),
|
|
555
841
|
parentId: parentRunId,
|
|
556
842
|
name: "retriever:error",
|
|
557
843
|
kind: "RETRIEVER",
|
|
558
|
-
timestamp:
|
|
844
|
+
timestamp: ts,
|
|
559
845
|
status: "error",
|
|
560
846
|
durationMs,
|
|
561
847
|
attributes: attrs,
|
|
562
848
|
confidence: "explicit",
|
|
563
849
|
source: { type: "adapter" }
|
|
564
850
|
});
|
|
851
|
+
await this.#persistStepEnd(runId, parentRunId, "error", ts, durationMs, errorMessage);
|
|
565
852
|
}
|
|
566
853
|
async handleAgentAction(action, runId, parentRunId, tags) {
|
|
567
854
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -587,6 +874,7 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
587
874
|
confidence: "explicit",
|
|
588
875
|
source: { type: "adapter" }
|
|
589
876
|
});
|
|
877
|
+
await this.#persistInstant(runId, parentRunId, "agent:action", "DECISION", attrs, "success");
|
|
590
878
|
}
|
|
591
879
|
async handleAgentEnd(finish, runId, parentRunId, tags) {
|
|
592
880
|
this.#ensureRoot(runId, parentRunId);
|
|
@@ -611,6 +899,7 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
|
|
|
611
899
|
confidence: "explicit",
|
|
612
900
|
source: { type: "adapter" }
|
|
613
901
|
});
|
|
902
|
+
await this.#persistInstant(runId, parentRunId, "agent:end", "AGENT", attrs, "success");
|
|
614
903
|
}
|
|
615
904
|
};
|
|
616
905
|
|