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