@posthog/ai 8.0.0 → 8.1.0

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.
@@ -6,6 +6,9 @@ import { uuidv7 } from '@posthog/core';
6
6
  const isString = value => {
7
7
  return typeof value === 'string';
8
8
  };
9
+ const isObject = value => {
10
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
11
+ };
9
12
 
10
13
  const DATA_URL_PREFIX_RE = /^data:([^;,\s]+)(?:;[^;,\s]+)*;base64,/i;
11
14
  const BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/;
@@ -392,7 +395,7 @@ function sanitizeValues(obj) {
392
395
  return jsonSafe;
393
396
  }
394
397
 
395
- var version = "8.0.0";
398
+ var version = "8.1.0";
396
399
 
397
400
  const DEFAULT_MAX_DEPTH = 3;
398
401
  const MAX_STACK_LINES = 20;
@@ -800,6 +803,36 @@ const extractProvider = model => {
800
803
  return providerName;
801
804
  };
802
805
 
806
+ /**
807
+ * Recover the base URL so gateway calls self-identify via `$ai_base_url` (dedup
808
+ * keys on it). The spec exposes none, so we read the off-spec provider `config`:
809
+ * `@ai-sdk/anthropic` keeps a `config.baseURL` string; `@ai-sdk/openai`/
810
+ * `openai-compatible` bury it in a `config.url({ path })` closure. Unknown shapes
811
+ * degrade to `''` — those providers (or a custom `fetch`) stay invisible to dedup.
812
+ */
813
+ const extractBaseURL = model => {
814
+ try {
815
+ const config = model.config;
816
+ if (!isObject(config)) {
817
+ return '';
818
+ }
819
+ if (isString(config.baseURL)) {
820
+ return config.baseURL;
821
+ }
822
+ const urlFn = config.url;
823
+ if (typeof urlFn === 'function') {
824
+ const url = urlFn({
825
+ path: '',
826
+ modelId: model.modelId
827
+ });
828
+ return isString(url) ? url : '';
829
+ }
830
+ } catch {
831
+ // Unknown config shape or url() threw.
832
+ }
833
+ return '';
834
+ };
835
+
803
836
  // Extract web search count from provider metadata (works for both V2 and V3)
804
837
  const extractWebSearchCount = (providerMetadata, usage) => {
805
838
  // Try Anthropic-specific extraction
@@ -952,11 +985,11 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
952
985
  ...mapVercelParams(params)
953
986
  };
954
987
  const availableTools = extractAvailableToolCalls('vercel', params);
988
+ const baseURL = extractBaseURL(model);
955
989
  try {
956
990
  const result = await model.doGenerate(params);
957
991
  const modelId = mergedOptions.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
958
992
  const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
959
- const baseURL = ''; // cannot currently get baseURL from vercel
960
993
  // result.content is undefined when the model returns only tool calls with no text output
961
994
  const content = mapVercelOutput(result.content ?? []);
962
995
  const latency = (Date.now() - startTime) / 1000;
@@ -1021,7 +1054,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1021
1054
  input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
1022
1055
  output: [],
1023
1056
  latency: 0,
1024
- baseURL: '',
1057
+ baseURL,
1025
1058
  modelParameters: getModelParams(mergedParams),
1026
1059
  usage: {
1027
1060
  inputTokens: 0,
@@ -1053,7 +1086,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1053
1086
  const modelId = mergedOptions.posthogModelOverride ?? model.modelId;
1054
1087
  const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
1055
1088
  const availableTools = extractAvailableToolCalls('vercel', params);
1056
- const baseURL = ''; // cannot currently get baseURL from vercel
1089
+ const baseURL = extractBaseURL(model);
1057
1090
 
1058
1091
  // Map to track in-progress tool calls
1059
1092
  const toolCallsInProgress = new Map();
@@ -1211,7 +1244,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1211
1244
  input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
1212
1245
  output: [],
1213
1246
  latency: 0,
1214
- baseURL: '',
1247
+ baseURL,
1215
1248
  modelParameters: getModelParams(mergedParams),
1216
1249
  usage: {
1217
1250
  inputTokens: 0,