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