@posthog/ai 8.18.2 → 8.18.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.
Files changed (54) hide show
  1. package/dist/adk/index.cjs +1 -1
  2. package/dist/adk/index.cjs.map +1 -1
  3. package/dist/adk/index.mjs +1 -1
  4. package/dist/adk/index.mjs.map +1 -1
  5. package/dist/anthropic/index.cjs +1 -1
  6. package/dist/anthropic/index.cjs.map +1 -1
  7. package/dist/anthropic/index.mjs +1 -1
  8. package/dist/anthropic/index.mjs.map +1 -1
  9. package/dist/claude-agent-sdk/index.cjs +1 -1
  10. package/dist/claude-agent-sdk/index.cjs.map +1 -1
  11. package/dist/claude-agent-sdk/index.mjs +1 -1
  12. package/dist/claude-agent-sdk/index.mjs.map +1 -1
  13. package/dist/gemini/index.cjs +3 -2
  14. package/dist/gemini/index.cjs.map +1 -1
  15. package/dist/gemini/index.mjs +3 -2
  16. package/dist/gemini/index.mjs.map +1 -1
  17. package/dist/genkit/index.cjs +1 -1
  18. package/dist/genkit/index.cjs.map +1 -1
  19. package/dist/genkit/index.mjs +1 -1
  20. package/dist/genkit/index.mjs.map +1 -1
  21. package/dist/index.cjs +1 -1
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.mjs +1 -1
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/langchain/index.cjs +1 -1
  26. package/dist/langchain/index.cjs.map +1 -1
  27. package/dist/langchain/index.mjs +1 -1
  28. package/dist/langchain/index.mjs.map +1 -1
  29. package/dist/langchain/middleware/index.cjs +1 -1
  30. package/dist/langchain/middleware/index.cjs.map +1 -1
  31. package/dist/langchain/middleware/index.mjs +1 -1
  32. package/dist/langchain/middleware/index.mjs.map +1 -1
  33. package/dist/openai/index.cjs +5 -1
  34. package/dist/openai/index.cjs.map +1 -1
  35. package/dist/openai/index.mjs +5 -1
  36. package/dist/openai/index.mjs.map +1 -1
  37. package/dist/openai-agents/index.cjs +1 -1
  38. package/dist/openai-agents/index.cjs.map +1 -1
  39. package/dist/openai-agents/index.mjs +1 -1
  40. package/dist/openai-agents/index.mjs.map +1 -1
  41. package/dist/otel/index.browser.cjs +1075 -0
  42. package/dist/otel/index.browser.cjs.map +1 -0
  43. package/dist/otel/index.browser.mjs +1073 -0
  44. package/dist/otel/index.browser.mjs.map +1 -0
  45. package/dist/otel/index.cjs +283 -12
  46. package/dist/otel/index.cjs.map +1 -1
  47. package/dist/otel/index.d.ts +1 -0
  48. package/dist/otel/index.mjs +283 -12
  49. package/dist/otel/index.mjs.map +1 -1
  50. package/dist/vercel/index.cjs +1 -1
  51. package/dist/vercel/index.cjs.map +1 -1
  52. package/dist/vercel/index.mjs +1 -1
  53. package/dist/vercel/index.mjs.map +1 -1
  54. package/package.json +16 -13
@@ -1,5 +1,7 @@
1
- import { createRequire } from "node:module";
1
+ /*! Includes OpenTelemetry code. Copyright The OpenTelemetry Authors. Licensed under Apache-2.0. */
2
2
  import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
3
+ import { TraceFlags, context, createContextKey, diag } from "@opentelemetry/api";
4
+ import { inspect } from "node:util";
3
5
  //#region src/sanitization/base64_recognizer.ts
4
6
  const DATA_URL_PREFIX_RE = /^data:([^;,\s]+)(?:;[^;,\s]+)*;base64,/i;
5
7
  const BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/;
@@ -602,20 +604,289 @@ var PostHogTraceExporter = class extends OTLPTraceExporter {
602
604
  }
603
605
  };
604
606
  //#endregion
605
- //#region src/otel/batchProcessor.ts
606
- const require = createRequire(import.meta.url);
607
- function createBatchSpanProcessor(exporter) {
607
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@opentelemetry/core/2.2.0/b871f22d07b51323f729d44d521414927172cbc0ddfdf1942304eca90488e96b/node_modules/@opentelemetry/core/build/esm/trace/suppress-tracing.js
608
+ const SUPPRESS_TRACING_KEY = createContextKey("OpenTelemetry SDK Context Key SUPPRESS_TRACING");
609
+ function suppressTracing(context) {
610
+ return context.setValue(SUPPRESS_TRACING_KEY, true);
611
+ }
612
+ //#endregion
613
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@opentelemetry/core/2.2.0/b871f22d07b51323f729d44d521414927172cbc0ddfdf1942304eca90488e96b/node_modules/@opentelemetry/core/build/esm/common/logging-error-handler.js
614
+ /**
615
+ * Returns a function that logs an error using the provided logger, or a
616
+ * console logger if one was not provided.
617
+ */
618
+ function loggingErrorHandler() {
619
+ return (ex) => {
620
+ diag.error(stringifyException(ex));
621
+ };
622
+ }
623
+ /**
624
+ * Converts an exception into a string representation
625
+ * @param {Exception} ex
626
+ */
627
+ function stringifyException(ex) {
628
+ if (typeof ex === "string") return ex;
629
+ else return JSON.stringify(flattenException(ex));
630
+ }
631
+ /**
632
+ * Flattens an exception into key-value pairs by traversing the prototype chain
633
+ * and coercing values to strings. Duplicate properties will not be overwritten;
634
+ * the first insert wins.
635
+ */
636
+ function flattenException(ex) {
637
+ const result = {};
638
+ let current = ex;
639
+ while (current !== null) {
640
+ Object.getOwnPropertyNames(current).forEach((propertyName) => {
641
+ if (result[propertyName]) return;
642
+ const value = current[propertyName];
643
+ if (value) result[propertyName] = String(value);
644
+ });
645
+ current = Object.getPrototypeOf(current);
646
+ }
647
+ return result;
648
+ }
649
+ //#endregion
650
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@opentelemetry/core/2.2.0/b871f22d07b51323f729d44d521414927172cbc0ddfdf1942304eca90488e96b/node_modules/@opentelemetry/core/build/esm/common/global-error-handler.js
651
+ /** The global error handler delegate */
652
+ let delegateHandler = loggingErrorHandler();
653
+ /**
654
+ * Return the global error handler
655
+ * @param {Exception} ex
656
+ */
657
+ function globalErrorHandler(ex) {
608
658
  try {
609
- require.resolve("@opentelemetry/sdk-trace");
610
- } catch (error) {
611
- if (error.code !== "MODULE_NOT_FOUND") throw error;
612
- const { BatchSpanProcessor } = require("@opentelemetry/sdk-trace-base");
613
- return new BatchSpanProcessor(exporter);
659
+ delegateHandler(ex);
660
+ } catch {}
661
+ }
662
+ //#endregion
663
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@opentelemetry/core/2.2.0/b871f22d07b51323f729d44d521414927172cbc0ddfdf1942304eca90488e96b/node_modules/@opentelemetry/core/build/esm/platform/node/environment.js
664
+ /**
665
+ * Retrieves a number from an environment variable.
666
+ * - Returns `undefined` if the environment variable is empty, unset, contains only whitespace, or is not a number.
667
+ * - Returns a number in all other cases.
668
+ *
669
+ * @param {string} key - The name of the environment variable to retrieve.
670
+ * @returns {number | undefined} - The number value or `undefined`.
671
+ */
672
+ function getNumberFromEnv(key) {
673
+ const raw = process.env[key];
674
+ if (raw == null || raw.trim() === "") return;
675
+ const value = Number(raw);
676
+ if (isNaN(value)) {
677
+ diag.warn(`Unknown value ${inspect(raw)} for ${key}, expected a number, using defaults`);
678
+ return;
614
679
  }
615
- const { BatchSpanProcessor } = require("@opentelemetry/sdk-trace");
616
- return new BatchSpanProcessor({ exporter });
680
+ return value;
617
681
  }
618
682
  //#endregion
683
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@opentelemetry/core/2.2.0/b871f22d07b51323f729d44d521414927172cbc0ddfdf1942304eca90488e96b/node_modules/@opentelemetry/core/build/esm/ExportResult.js
684
+ var ExportResultCode;
685
+ (function(ExportResultCode) {
686
+ ExportResultCode[ExportResultCode["SUCCESS"] = 0] = "SUCCESS";
687
+ ExportResultCode[ExportResultCode["FAILED"] = 1] = "FAILED";
688
+ })(ExportResultCode || (ExportResultCode = {}));
689
+ //#endregion
690
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@opentelemetry/core/2.2.0/b871f22d07b51323f729d44d521414927172cbc0ddfdf1942304eca90488e96b/node_modules/@opentelemetry/core/build/esm/utils/promise.js
691
+ var Deferred = class {
692
+ _promise;
693
+ _resolve;
694
+ _reject;
695
+ constructor() {
696
+ this._promise = new Promise((resolve, reject) => {
697
+ this._resolve = resolve;
698
+ this._reject = reject;
699
+ });
700
+ }
701
+ get promise() {
702
+ return this._promise;
703
+ }
704
+ resolve(val) {
705
+ this._resolve(val);
706
+ }
707
+ reject(err) {
708
+ this._reject(err);
709
+ }
710
+ };
711
+ //#endregion
712
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@opentelemetry/core/2.2.0/b871f22d07b51323f729d44d521414927172cbc0ddfdf1942304eca90488e96b/node_modules/@opentelemetry/core/build/esm/utils/callback.js
713
+ /**
714
+ * Bind the callback and only invoke the callback once regardless how many times `BindOnceFuture.call` is invoked.
715
+ */
716
+ var BindOnceFuture = class {
717
+ _callback;
718
+ _that;
719
+ _isCalled = false;
720
+ _deferred = new Deferred();
721
+ constructor(_callback, _that) {
722
+ this._callback = _callback;
723
+ this._that = _that;
724
+ }
725
+ get isCalled() {
726
+ return this._isCalled;
727
+ }
728
+ get promise() {
729
+ return this._deferred.promise;
730
+ }
731
+ call(...args) {
732
+ if (!this._isCalled) {
733
+ this._isCalled = true;
734
+ try {
735
+ Promise.resolve(this._callback.call(this._that, ...args)).then((val) => this._deferred.resolve(val), (err) => this._deferred.reject(err));
736
+ } catch (err) {
737
+ this._deferred.reject(err);
738
+ }
739
+ }
740
+ return this._deferred.promise;
741
+ }
742
+ };
743
+ //#endregion
744
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@opentelemetry/sdk-trace-base/2.2.0/fa825708a19b4d5944c83585f1de1744c29637446df5cfc986c5a1263b781148/node_modules/@opentelemetry/sdk-trace-base/build/esm/export/BatchSpanProcessorBase.js
745
+ /**
746
+ * Implementation of the {@link SpanProcessor} that batches spans exported by
747
+ * the SDK then pushes them to the exporter pipeline.
748
+ */
749
+ var BatchSpanProcessorBase = class {
750
+ _exporter;
751
+ _maxExportBatchSize;
752
+ _maxQueueSize;
753
+ _scheduledDelayMillis;
754
+ _exportTimeoutMillis;
755
+ _isExporting = false;
756
+ _finishedSpans = [];
757
+ _timer;
758
+ _shutdownOnce;
759
+ _droppedSpansCount = 0;
760
+ constructor(_exporter, config) {
761
+ this._exporter = _exporter;
762
+ this._maxExportBatchSize = typeof config?.maxExportBatchSize === "number" ? config.maxExportBatchSize : getNumberFromEnv("OTEL_BSP_MAX_EXPORT_BATCH_SIZE") ?? 512;
763
+ this._maxQueueSize = typeof config?.maxQueueSize === "number" ? config.maxQueueSize : getNumberFromEnv("OTEL_BSP_MAX_QUEUE_SIZE") ?? 2048;
764
+ this._scheduledDelayMillis = typeof config?.scheduledDelayMillis === "number" ? config.scheduledDelayMillis : getNumberFromEnv("OTEL_BSP_SCHEDULE_DELAY") ?? 5e3;
765
+ this._exportTimeoutMillis = typeof config?.exportTimeoutMillis === "number" ? config.exportTimeoutMillis : getNumberFromEnv("OTEL_BSP_EXPORT_TIMEOUT") ?? 3e4;
766
+ this._shutdownOnce = new BindOnceFuture(this._shutdown, this);
767
+ if (this._maxExportBatchSize > this._maxQueueSize) {
768
+ diag.warn("BatchSpanProcessor: maxExportBatchSize must be smaller or equal to maxQueueSize, setting maxExportBatchSize to match maxQueueSize");
769
+ this._maxExportBatchSize = this._maxQueueSize;
770
+ }
771
+ }
772
+ forceFlush() {
773
+ if (this._shutdownOnce.isCalled) return this._shutdownOnce.promise;
774
+ return this._flushAll();
775
+ }
776
+ onStart(_span, _parentContext) {}
777
+ onEnd(span) {
778
+ if (this._shutdownOnce.isCalled) return;
779
+ if ((span.spanContext().traceFlags & TraceFlags.SAMPLED) === 0) return;
780
+ this._addToBuffer(span);
781
+ }
782
+ shutdown() {
783
+ return this._shutdownOnce.call();
784
+ }
785
+ _shutdown() {
786
+ return Promise.resolve().then(() => {
787
+ return this.onShutdown();
788
+ }).then(() => {
789
+ return this._flushAll();
790
+ }).then(() => {
791
+ return this._exporter.shutdown();
792
+ });
793
+ }
794
+ /** Add a span in the buffer. */
795
+ _addToBuffer(span) {
796
+ if (this._finishedSpans.length >= this._maxQueueSize) {
797
+ if (this._droppedSpansCount === 0) diag.debug("maxQueueSize reached, dropping spans");
798
+ this._droppedSpansCount++;
799
+ return;
800
+ }
801
+ if (this._droppedSpansCount > 0) {
802
+ diag.warn(`Dropped ${this._droppedSpansCount} spans because maxQueueSize reached`);
803
+ this._droppedSpansCount = 0;
804
+ }
805
+ this._finishedSpans.push(span);
806
+ this._maybeStartTimer();
807
+ }
808
+ /**
809
+ * Send all spans to the exporter respecting the batch size limit
810
+ * This function is used only on forceFlush or shutdown,
811
+ * for all other cases _flush should be used
812
+ * */
813
+ _flushAll() {
814
+ return new Promise((resolve, reject) => {
815
+ const promises = [];
816
+ const count = Math.ceil(this._finishedSpans.length / this._maxExportBatchSize);
817
+ for (let i = 0, j = count; i < j; i++) promises.push(this._flushOneBatch());
818
+ Promise.all(promises).then(() => {
819
+ resolve();
820
+ }).catch(reject);
821
+ });
822
+ }
823
+ _flushOneBatch() {
824
+ this._clearTimer();
825
+ if (this._finishedSpans.length === 0) return Promise.resolve();
826
+ return new Promise((resolve, reject) => {
827
+ const timer = setTimeout(() => {
828
+ reject(/* @__PURE__ */ new Error("Timeout"));
829
+ }, this._exportTimeoutMillis);
830
+ context.with(suppressTracing(context.active()), () => {
831
+ let spans;
832
+ if (this._finishedSpans.length <= this._maxExportBatchSize) {
833
+ spans = this._finishedSpans;
834
+ this._finishedSpans = [];
835
+ } else spans = this._finishedSpans.splice(0, this._maxExportBatchSize);
836
+ const doExport = () => this._exporter.export(spans, (result) => {
837
+ clearTimeout(timer);
838
+ if (result.code === ExportResultCode.SUCCESS) resolve();
839
+ else reject(result.error ?? /* @__PURE__ */ new Error("BatchSpanProcessor: span export failed"));
840
+ });
841
+ let pendingResources = null;
842
+ for (let i = 0, len = spans.length; i < len; i++) {
843
+ const span = spans[i];
844
+ if (span.resource.asyncAttributesPending && span.resource.waitForAsyncAttributes) {
845
+ pendingResources ??= [];
846
+ pendingResources.push(span.resource.waitForAsyncAttributes());
847
+ }
848
+ }
849
+ if (pendingResources === null) doExport();
850
+ else Promise.all(pendingResources).then(doExport, (err) => {
851
+ globalErrorHandler(err);
852
+ reject(err);
853
+ });
854
+ });
855
+ });
856
+ }
857
+ _maybeStartTimer() {
858
+ if (this._isExporting) return;
859
+ const flush = () => {
860
+ this._isExporting = true;
861
+ this._flushOneBatch().finally(() => {
862
+ this._isExporting = false;
863
+ if (this._finishedSpans.length > 0) {
864
+ this._clearTimer();
865
+ this._maybeStartTimer();
866
+ }
867
+ }).catch((e) => {
868
+ this._isExporting = false;
869
+ globalErrorHandler(e);
870
+ });
871
+ };
872
+ if (this._finishedSpans.length >= this._maxExportBatchSize) return flush();
873
+ if (this._timer !== void 0) return;
874
+ this._timer = setTimeout(() => flush(), this._scheduledDelayMillis);
875
+ if (typeof this._timer !== "number") this._timer.unref();
876
+ }
877
+ _clearTimer() {
878
+ if (this._timer !== void 0) {
879
+ clearTimeout(this._timer);
880
+ this._timer = void 0;
881
+ }
882
+ }
883
+ };
884
+ //#endregion
885
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@opentelemetry/sdk-trace-base/2.2.0/fa825708a19b4d5944c83585f1de1744c29637446df5cfc986c5a1263b781148/node_modules/@opentelemetry/sdk-trace-base/build/esm/platform/node/export/BatchSpanProcessor.js
886
+ var BatchSpanProcessor = class extends BatchSpanProcessorBase {
887
+ onShutdown() {}
888
+ };
889
+ //#endregion
619
890
  //#region src/privacyMode.ts
620
891
  /** Per-call settings can turn redaction on but never off. */
621
892
  const isPrivacyModeEnabled = (client, privacyMode = false) => privacyMode === true || client?.privacyMode === true || client?.privacy_mode === true || client?.options?.privacyMode === true;
@@ -765,7 +1036,7 @@ var PostHogSpanProcessor = class {
765
1036
  url: `${host}/i/v0/ai/otel`,
766
1037
  headers: { Authorization: `Bearer ${token}` }
767
1038
  });
768
- this.inner = createBatchSpanProcessor(exporter);
1039
+ this.inner = new BatchSpanProcessor(exporter);
769
1040
  }
770
1041
  }
771
1042
  onStart(span, parentContext) {