@posthog/ai 8.8.1 → 8.9.1

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
@@ -415,7 +415,7 @@ function sanitizeValues(obj) {
415
415
  return jsonSafe;
416
416
  }
417
417
 
418
- var version = "8.8.1";
418
+ var version = "8.9.1";
419
419
 
420
420
  const DEFAULT_MAX_DEPTH = 3;
421
421
  const MAX_STACK_LINES = 20;
package/dist/index.mjs CHANGED
@@ -413,7 +413,7 @@ function sanitizeValues(obj) {
413
413
  return jsonSafe;
414
414
  }
415
415
 
416
- var version = "8.8.1";
416
+ var version = "8.9.1";
417
417
 
418
418
  const DEFAULT_MAX_DEPTH = 3;
419
419
  const MAX_STACK_LINES = 20;
@@ -247,7 +247,7 @@ function sanitizeValues(obj) {
247
247
  return jsonSafe;
248
248
  }
249
249
 
250
- var version = "8.8.1";
250
+ var version = "8.9.1";
251
251
 
252
252
  const DEFAULT_MAX_DEPTH = 3;
253
253
  const MAX_STACK_LINES = 20;
@@ -369,13 +369,16 @@ class LangChainCallbackHandler extends base.BaseCallbackHandler {
369
369
 
370
370
  // ===== CALLBACK METHODS =====
371
371
 
372
- handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, _runType, runName) {
372
+ handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, _runType, runName, extra) {
373
373
  this._logDebugEvent('on_chain_start', runId, parentRunId, {
374
374
  inputs,
375
375
  tags
376
376
  });
377
377
  this._setParentOfRun(runId, parentRunId);
378
378
  this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName);
379
+ if (typeof extra?.posthogStartTime === 'number' && Number.isFinite(extra.posthogStartTime)) {
380
+ this.runs[runId].startTime = extra.posthogStartTime;
381
+ }
379
382
  }
380
383
  handleChainEnd(outputs, runId, parentRunId, tags, _kwargs) {
381
384
  this._logAndPopTraceOrSpan('on_chain_end', runId, parentRunId, {
@@ -570,6 +573,7 @@ class LangChainCallbackHandler extends base.BaseCallbackHandler {
570
573
  }
571
574
  _popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs) {
572
575
  const traceId = this._getTraceId(runId);
576
+ const isSpan = Boolean(parentRunId || this.parentTree[runId]);
573
577
  this._popParentOfRun(runId);
574
578
  const run = this._popRunMetadata(runId);
575
579
  if (!run) {
@@ -580,10 +584,10 @@ class LangChainCallbackHandler extends base.BaseCallbackHandler {
580
584
  return;
581
585
  }
582
586
  const actualParentRunId = this._getParentRunId(traceId, runId, parentRunId);
583
- this._captureTraceOrSpan(traceId, runId, run, outputs, actualParentRunId);
587
+ this._captureTraceOrSpan(traceId, runId, run, outputs, isSpan, actualParentRunId);
584
588
  }
585
- _captureTraceOrSpan(traceId, runId, run, outputs, parentRunId) {
586
- const eventName = parentRunId ? '$ai_span' : '$ai_trace';
589
+ _captureTraceOrSpan(traceId, runId, run, outputs, isSpan, parentRunId) {
590
+ const eventName = isSpan ? '$ai_span' : '$ai_trace';
587
591
  const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
588
592
  const eventProperties = {
589
593
  $ai_lib: 'posthog-ai',
@@ -647,7 +651,6 @@ class LangChainCallbackHandler extends base.BaseCallbackHandler {
647
651
  $ai_trace_id: traceId,
648
652
  $ai_span_id: runId,
649
653
  $ai_span_name: run.name,
650
- $ai_parent_id: parentRunId,
651
654
  $ai_provider: run.provider,
652
655
  $ai_model: run.model,
653
656
  $ai_model_parameters: run.modelParams,
@@ -657,6 +660,9 @@ class LangChainCallbackHandler extends base.BaseCallbackHandler {
657
660
  $ai_base_url: run.baseUrl,
658
661
  $ai_framework: 'langchain'
659
662
  };
663
+ if (parentRunId) {
664
+ eventProperties['$ai_parent_id'] = parentRunId;
665
+ }
660
666
  if (run.tools) {
661
667
  eventProperties['$ai_tools'] = run.tools;
662
668
  }