@agent-inspect/langchain 6.7.2 → 6.7.4

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
@@ -349,6 +349,11 @@ var LangChainTracePersistence = class {
349
349
  #runCompleted = false;
350
350
  #runStartTime;
351
351
  #rootLcRunId;
352
+ /** Live LangChain callback runIds for standalone envelope finalization. */
353
+ #activeLcRunIds = /* @__PURE__ */ new Set();
354
+ #sawError = false;
355
+ #lastErrorMessage;
356
+ #finalizationToken = 0;
352
357
  #lcToStepId = /* @__PURE__ */ new Map();
353
358
  constructor(options = {}) {
354
359
  const inContext = hasActiveContext();
@@ -369,10 +374,14 @@ var LangChainTracePersistence = class {
369
374
  return this.#traceDir;
370
375
  }
371
376
  reset() {
377
+ this.#finalizationToken += 1;
372
378
  this.#runStarted = false;
373
379
  this.#runCompleted = false;
374
380
  this.#runStartTime = void 0;
375
381
  this.#rootLcRunId = void 0;
382
+ this.#activeLcRunIds.clear();
383
+ this.#sawError = false;
384
+ this.#lastErrorMessage = void 0;
376
385
  this.#lcToStepId.clear();
377
386
  }
378
387
  noteRoot(lcRunId, parentRunId) {
@@ -386,7 +395,9 @@ var LangChainTracePersistence = class {
386
395
  }
387
396
  async onStepStart(params) {
388
397
  try {
398
+ this.#finalizationToken += 1;
389
399
  this.noteRoot(params.lcRunId, params.lcParentRunId);
400
+ this.#activeLcRunIds.add(params.lcRunId);
390
401
  if (this.#standalone && !this.#runStarted) {
391
402
  await this.#ensureRunStarted(params.startTime, params.attributes);
392
403
  }
@@ -458,13 +469,12 @@ var LangChainTracePersistence = class {
458
469
  ...params.status === "error" && params.errorMessage ? { error: { message: params.errorMessage } } : {}
459
470
  };
460
471
  await this.#write(event);
461
- if (this.#standalone && !this.#runCompleted && this.#rootLcRunId === params.lcRunId && !params.lcParentRunId) {
462
- await this.#ensureRunCompleted(
463
- params.endTime,
464
- params.status,
465
- params.errorMessage
466
- );
472
+ if (params.status === "error") {
473
+ this.#sawError = true;
474
+ if (params.errorMessage) this.#lastErrorMessage = params.errorMessage;
467
475
  }
476
+ this.#activeLcRunIds.delete(params.lcRunId);
477
+ await this.#scheduleStandaloneFinalization(params.endTime);
468
478
  } catch (err) {
469
479
  this.#warn(err);
470
480
  }
@@ -472,7 +482,9 @@ var LangChainTracePersistence = class {
472
482
  /** Point-in-time adapter events (e.g. agent action) — writes start + completed pair. */
473
483
  async onInstantStep(params) {
474
484
  try {
485
+ this.#finalizationToken += 1;
475
486
  this.noteRoot(params.lcRunId, params.lcParentRunId);
487
+ this.#activeLcRunIds.add(params.lcRunId);
476
488
  if (this.#standalone && !this.#runStarted) {
477
489
  await this.#ensureRunStarted(params.timestamp, params.attributes);
478
490
  }
@@ -509,10 +521,37 @@ var LangChainTracePersistence = class {
509
521
  ...params.status === "error" && params.errorMessage ? { error: { message: params.errorMessage } } : {}
510
522
  };
511
523
  await this.#write(completed);
524
+ if (params.status === "error") {
525
+ this.#sawError = true;
526
+ if (params.errorMessage) this.#lastErrorMessage = params.errorMessage;
527
+ }
528
+ this.#activeLcRunIds.delete(params.lcRunId);
529
+ await this.#scheduleStandaloneFinalization(params.timestamp);
512
530
  } catch (err) {
513
531
  this.#warn(err);
514
532
  }
515
533
  }
534
+ /**
535
+ * When the last active LangChain callback ends, yield one microtask so a
536
+ * same-turn sibling start can cancel finalization, then write run_completed
537
+ * before the callback promise settles. Unresolved external parents do not
538
+ * block the envelope.
539
+ */
540
+ async #scheduleStandaloneFinalization(endTime) {
541
+ if (!this.#standalone || this.#runCompleted || !this.#runStarted) return;
542
+ if (this.#activeLcRunIds.size > 0) return;
543
+ const token = ++this.#finalizationToken;
544
+ await Promise.resolve();
545
+ if (token !== this.#finalizationToken) return;
546
+ if (!this.#standalone || this.#runCompleted || !this.#runStarted) return;
547
+ if (this.#activeLcRunIds.size > 0) return;
548
+ const status = this.#sawError ? "error" : "success";
549
+ await this.#ensureRunCompleted(
550
+ endTime,
551
+ status,
552
+ status === "error" ? this.#lastErrorMessage : void 0
553
+ );
554
+ }
516
555
  async #ensureRunStarted(startTime, attrs) {
517
556
  if (this.#runStarted) return;
518
557
  this.#runStarted = true;
@@ -573,6 +612,15 @@ function serializedLabel(s) {
573
612
  if (Array.isArray(s.id) && s.id.length > 0) return s.id[s.id.length - 1];
574
613
  return s.type;
575
614
  }
615
+ function resolveToolDisplayName(tool, runName, metadata) {
616
+ const fromRun = typeof runName === "string" ? runName.trim() : "";
617
+ if (fromRun) return fromRun;
618
+ const metaName = metadata?.toolName;
619
+ if (typeof metaName === "string" && metaName.trim()) return metaName.trim();
620
+ const metaTool = metadata?.tool;
621
+ if (typeof metaTool === "string" && metaTool.trim()) return metaTool.trim();
622
+ return serializedLabel(tool) ?? "tool";
623
+ }
576
624
  function errorShape(err) {
577
625
  if (err instanceof Error) {
578
626
  return { errorName: err.name, errorMessage: err.message };
@@ -1017,7 +1065,7 @@ var AgentInspectCallback = class extends BaseCallbackHandler {
1017
1065
  async handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName, _toolCallId) {
1018
1066
  this.#ensureRoot(runId, parentRunId);
1019
1067
  this.#rememberStart(runId, "TOOL");
1020
- const toolName = serializedLabel(tool) ?? "tool";
1068
+ const toolName = resolveToolDisplayName(tool, runName, metadata);
1021
1069
  const previews = {};
1022
1070
  if (this.#opts.capture === "preview") previews.inputPreview = input;
1023
1071
  const attrs = {