@agent-inspect/langchain 6.7.3 → 6.7.5

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
@@ -1,9 +1,14 @@
1
1
  'use strict';
2
2
 
3
+ var path = require('path');
3
4
  var base = require('@langchain/core/callbacks/base');
4
5
  var advanced = require('agent-inspect/advanced');
5
6
  var logs = require('agent-inspect/logs');
6
7
 
8
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
+
10
+ var path__default = /*#__PURE__*/_interopDefault(path);
11
+
7
12
  // packages/langchain/src/agent-inspect-callback.ts
8
13
 
9
14
  // packages/langchain/src/metadata.ts
@@ -351,6 +356,11 @@ var LangChainTracePersistence = class {
351
356
  #runCompleted = false;
352
357
  #runStartTime;
353
358
  #rootLcRunId;
359
+ /** Live LangChain callback runIds for standalone envelope finalization. */
360
+ #activeLcRunIds = /* @__PURE__ */ new Set();
361
+ #sawError = false;
362
+ #lastErrorMessage;
363
+ #finalizationToken = 0;
354
364
  #lcToStepId = /* @__PURE__ */ new Map();
355
365
  constructor(options = {}) {
356
366
  const inContext = advanced.hasActiveContext();
@@ -371,10 +381,14 @@ var LangChainTracePersistence = class {
371
381
  return this.#traceDir;
372
382
  }
373
383
  reset() {
384
+ this.#finalizationToken += 1;
374
385
  this.#runStarted = false;
375
386
  this.#runCompleted = false;
376
387
  this.#runStartTime = void 0;
377
388
  this.#rootLcRunId = void 0;
389
+ this.#activeLcRunIds.clear();
390
+ this.#sawError = false;
391
+ this.#lastErrorMessage = void 0;
378
392
  this.#lcToStepId.clear();
379
393
  }
380
394
  noteRoot(lcRunId, parentRunId) {
@@ -388,7 +402,9 @@ var LangChainTracePersistence = class {
388
402
  }
389
403
  async onStepStart(params) {
390
404
  try {
405
+ this.#finalizationToken += 1;
391
406
  this.noteRoot(params.lcRunId, params.lcParentRunId);
407
+ this.#activeLcRunIds.add(params.lcRunId);
392
408
  if (this.#standalone && !this.#runStarted) {
393
409
  await this.#ensureRunStarted(params.startTime, params.attributes);
394
410
  }
@@ -460,13 +476,12 @@ var LangChainTracePersistence = class {
460
476
  ...params.status === "error" && params.errorMessage ? { error: { message: params.errorMessage } } : {}
461
477
  };
462
478
  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
- );
479
+ if (params.status === "error") {
480
+ this.#sawError = true;
481
+ if (params.errorMessage) this.#lastErrorMessage = params.errorMessage;
469
482
  }
483
+ this.#activeLcRunIds.delete(params.lcRunId);
484
+ await this.#scheduleStandaloneFinalization(params.endTime);
470
485
  } catch (err) {
471
486
  this.#warn(err);
472
487
  }
@@ -474,7 +489,9 @@ var LangChainTracePersistence = class {
474
489
  /** Point-in-time adapter events (e.g. agent action) — writes start + completed pair. */
475
490
  async onInstantStep(params) {
476
491
  try {
492
+ this.#finalizationToken += 1;
477
493
  this.noteRoot(params.lcRunId, params.lcParentRunId);
494
+ this.#activeLcRunIds.add(params.lcRunId);
478
495
  if (this.#standalone && !this.#runStarted) {
479
496
  await this.#ensureRunStarted(params.timestamp, params.attributes);
480
497
  }
@@ -511,10 +528,37 @@ var LangChainTracePersistence = class {
511
528
  ...params.status === "error" && params.errorMessage ? { error: { message: params.errorMessage } } : {}
512
529
  };
513
530
  await this.#write(completed);
531
+ if (params.status === "error") {
532
+ this.#sawError = true;
533
+ if (params.errorMessage) this.#lastErrorMessage = params.errorMessage;
534
+ }
535
+ this.#activeLcRunIds.delete(params.lcRunId);
536
+ await this.#scheduleStandaloneFinalization(params.timestamp);
514
537
  } catch (err) {
515
538
  this.#warn(err);
516
539
  }
517
540
  }
541
+ /**
542
+ * When the last active LangChain callback ends, yield one microtask so a
543
+ * same-turn sibling start can cancel finalization, then write run_completed
544
+ * before the callback promise settles. Unresolved external parents do not
545
+ * block the envelope.
546
+ */
547
+ async #scheduleStandaloneFinalization(endTime) {
548
+ if (!this.#standalone || this.#runCompleted || !this.#runStarted) return;
549
+ if (this.#activeLcRunIds.size > 0) return;
550
+ const token = ++this.#finalizationToken;
551
+ await Promise.resolve();
552
+ if (token !== this.#finalizationToken) return;
553
+ if (!this.#standalone || this.#runCompleted || !this.#runStarted) return;
554
+ if (this.#activeLcRunIds.size > 0) return;
555
+ const status = this.#sawError ? "error" : "success";
556
+ await this.#ensureRunCompleted(
557
+ endTime,
558
+ status,
559
+ status === "error" ? this.#lastErrorMessage : void 0
560
+ );
561
+ }
518
562
  async #ensureRunStarted(startTime, attrs) {
519
563
  if (this.#runStarted) return;
520
564
  this.#runStarted = true;
@@ -575,6 +619,15 @@ function serializedLabel(s) {
575
619
  if (Array.isArray(s.id) && s.id.length > 0) return s.id[s.id.length - 1];
576
620
  return s.type;
577
621
  }
622
+ function resolveToolDisplayName(tool, runName, metadata) {
623
+ const fromRun = typeof runName === "string" ? runName.trim() : "";
624
+ if (fromRun) return fromRun;
625
+ const metaName = metadata?.toolName;
626
+ if (typeof metaName === "string" && metaName.trim()) return metaName.trim();
627
+ const metaTool = metadata?.tool;
628
+ if (typeof metaTool === "string" && metaTool.trim()) return metaTool.trim();
629
+ return serializedLabel(tool) ?? "tool";
630
+ }
578
631
  function errorShape(err) {
579
632
  if (err instanceof Error) {
580
633
  return { errorName: err.name, errorMessage: err.message };
@@ -722,7 +775,11 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
722
775
  if (parentRunId) out.parentRunId = parentRunId;
723
776
  if (this.#opts.runName) out.adapterRunName = this.#opts.runName;
724
777
  if (runNameArg) out.runName = runNameArg;
725
- if (this.#opts.traceDir) out.traceDir = this.#opts.traceDir;
778
+ if (this.#opts.persist) out.traceStorage = "local";
779
+ const configuredTraceDir = this.#opts.traceDir?.trim();
780
+ if (configuredTraceDir && !path__default.default.isAbsolute(configuredTraceDir) && !/^[A-Za-z]:[\\/]/.test(configuredTraceDir)) {
781
+ out.workspaceRelativeTraceDir = configuredTraceDir;
782
+ }
726
783
  const cap = this.#opts.capture;
727
784
  if (cap !== "none" && tags?.length) out.tags = [...tags];
728
785
  return out;
@@ -1019,7 +1076,7 @@ var AgentInspectCallback = class extends base.BaseCallbackHandler {
1019
1076
  async handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName, _toolCallId) {
1020
1077
  this.#ensureRoot(runId, parentRunId);
1021
1078
  this.#rememberStart(runId, "TOOL");
1022
- const toolName = serializedLabel(tool) ?? "tool";
1079
+ const toolName = resolveToolDisplayName(tool, runName, metadata);
1023
1080
  const previews = {};
1024
1081
  if (this.#opts.capture === "preview") previews.inputPreview = input;
1025
1082
  const attrs = {