@posthog/ai 8.7.1 → 8.8.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.
@@ -13,6 +13,33 @@ const isString = value => {
13
13
  return typeof value === 'string';
14
14
  };
15
15
 
16
+ /** @internal */
17
+
18
+ /** @internal */
19
+
20
+ /** @internal */
21
+ function isFullAiCaptureEnabled(client) {
22
+ return client?.enableFullAiCapture === true;
23
+ }
24
+
25
+ /** @internal */
26
+ function captureAiEvent(client, event) {
27
+ if (isFullAiCaptureEnabled(client) && typeof client.captureAi === 'function') {
28
+ client.captureAi(event);
29
+ return;
30
+ }
31
+ client.capture(event);
32
+ }
33
+
34
+ /** @internal */
35
+ async function captureAiEventImmediate(client, event) {
36
+ if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === 'function') {
37
+ await client.captureAiImmediate(event);
38
+ return;
39
+ }
40
+ await client.captureImmediate(event);
41
+ }
42
+
16
43
  const DATA_URL_PREFIX_RE = /^data:([^;,\s]+)(?:;[^;,\s]+)*;base64,/i;
17
44
  const BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/;
18
45
  class Base64Recognizer {
@@ -118,7 +145,6 @@ class BinaryContentRedactor {
118
145
  this.recognizer = recognizer;
119
146
  }
120
147
  redact(value, mediaType) {
121
- if (this.isMultimodalEnabled()) return value;
122
148
  this.visited = new WeakSet();
123
149
  return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY);
124
150
  }
@@ -162,15 +188,12 @@ class BinaryContentRedactor {
162
188
  if (mediaType === 'application/octet-stream') return '[base64 file redacted]';
163
189
  return `[base64 ${mediaType} redacted]`;
164
190
  }
165
- isMultimodalEnabled() {
166
- const val = process.env._INTERNAL_LLMA_MULTIMODAL || '';
167
- return val.toLowerCase() === 'true' || val === '1' || val.toLowerCase() === 'yes';
168
- }
169
191
  }
170
192
 
171
193
  const redactor = new BinaryContentRedactor();
172
- const sanitizeOpenAI = data => redactor.redact(data);
173
- const sanitizeOpenAIResponse = data => redactor.redact(data);
194
+ const sanitize = (data, client) => isFullAiCaptureEnabled(client) ? data : redactor.redact(data);
195
+ const sanitizeOpenAI = (data, client) => sanitize(data, client);
196
+ const sanitizeOpenAIResponse = (data, client) => sanitize(data, client);
174
197
 
175
198
  const TOKEN_PROPERTY_KEYS = new Set(['$ai_input_tokens', '$ai_output_tokens', '$ai_cache_read_input_tokens', '$ai_cache_creation_input_tokens', '$ai_total_tokens', '$ai_reasoning_tokens']);
176
199
 
@@ -553,7 +576,7 @@ function formatOpenAIResponsesInput(input, instructions) {
553
576
  return messages;
554
577
  }
555
578
 
556
- var version = "8.7.1";
579
+ var version = "8.8.0";
557
580
 
558
581
  const DEFAULT_MAX_DEPTH = 3;
559
582
  const MAX_STACK_LINES = 20;
@@ -802,9 +825,9 @@ const captureAiGeneration$1 = async (client, options) => {
802
825
  groups: options.groups
803
826
  };
804
827
  if (options.captureImmediate) {
805
- await client.captureImmediate(event);
828
+ await captureAiEventImmediate(client, event);
806
829
  } else {
807
- client.capture(event);
830
+ captureAiEvent(client, event);
808
831
  }
809
832
  } catch (error) {
810
833
  // Telemetry failures must never affect the instrumented provider call.
@@ -1471,8 +1494,8 @@ let WrappedCompletions$1 = class WrappedCompletions extends openai.AzureOpenAI.C
1471
1494
  ...posthogParams,
1472
1495
  model: openAIParams.model ?? modelFromResponse,
1473
1496
  provider: 'azure',
1474
- input: sanitizeOpenAI(openAIParams.messages),
1475
- output: sanitizeOpenAIResponse(formattedOutput),
1497
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
1498
+ output: sanitizeOpenAIResponse(formattedOutput, this.phClient),
1476
1499
  latency,
1477
1500
  timeToFirstToken,
1478
1501
  baseURL: this.baseURL,
@@ -1489,7 +1512,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends openai.AzureOpenAI.C
1489
1512
  ...posthogParams,
1490
1513
  model: openAIParams.model,
1491
1514
  provider: 'azure',
1492
- input: sanitizeOpenAI(openAIParams.messages),
1515
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
1493
1516
  output: [],
1494
1517
  latency: 0,
1495
1518
  baseURL: this.baseURL,
@@ -1528,8 +1551,8 @@ let WrappedCompletions$1 = class WrappedCompletions extends openai.AzureOpenAI.C
1528
1551
  ...posthogParams,
1529
1552
  model: openAIParams.model ?? result.model,
1530
1553
  provider: 'azure',
1531
- input: sanitizeOpenAI(openAIParams.messages),
1532
- output: sanitizeOpenAIResponse(formatResponseOpenAI(result)),
1554
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
1555
+ output: sanitizeOpenAIResponse(formatResponseOpenAI(result), this.phClient),
1533
1556
  latency,
1534
1557
  baseURL: this.baseURL,
1535
1558
  modelParameters: getModelParams(body, result.service_tier),
@@ -1555,7 +1578,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends openai.AzureOpenAI.C
1555
1578
  ...posthogParams,
1556
1579
  model: openAIParams.model,
1557
1580
  provider: 'azure',
1558
- input: sanitizeOpenAI(openAIParams.messages),
1581
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
1559
1582
  output: [],
1560
1583
  latency: 0,
1561
1584
  baseURL: this.baseURL,
@@ -1697,8 +1720,8 @@ let WrappedResponses$1 = class WrappedResponses extends openai.AzureOpenAI.Respo
1697
1720
  ...posthogParams,
1698
1721
  model: openAIParams.model ?? modelFromResponse,
1699
1722
  provider: 'azure',
1700
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1701
- output: sanitizeOpenAIResponse(finalContent),
1723
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1724
+ output: sanitizeOpenAIResponse(finalContent, this.phClient),
1702
1725
  latency,
1703
1726
  timeToFirstToken,
1704
1727
  baseURL: this.baseURL,
@@ -1720,7 +1743,7 @@ let WrappedResponses$1 = class WrappedResponses extends openai.AzureOpenAI.Respo
1720
1743
  ...posthogParams,
1721
1744
  model: openAIParams.model,
1722
1745
  provider: 'azure',
1723
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1746
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1724
1747
  output: [],
1725
1748
  latency: 0,
1726
1749
  baseURL: this.baseURL,
@@ -1760,8 +1783,8 @@ let WrappedResponses$1 = class WrappedResponses extends openai.AzureOpenAI.Respo
1760
1783
  ...posthogParams,
1761
1784
  model: openAIParams.model ?? result.model,
1762
1785
  provider: 'azure',
1763
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1764
- output: sanitizeOpenAIResponse(result.output),
1786
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1787
+ output: sanitizeOpenAIResponse(result.output, this.phClient),
1765
1788
  latency,
1766
1789
  baseURL: this.baseURL,
1767
1790
  modelParameters: getModelParams(body, result.service_tier),
@@ -1790,7 +1813,7 @@ let WrappedResponses$1 = class WrappedResponses extends openai.AzureOpenAI.Respo
1790
1813
  ...posthogParams,
1791
1814
  model: openAIParams.model,
1792
1815
  provider: 'azure',
1793
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1816
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1794
1817
  output: [],
1795
1818
  latency: 0,
1796
1819
  baseURL: this.baseURL,
@@ -1876,8 +1899,8 @@ let WrappedResponses$1 = class WrappedResponses extends openai.AzureOpenAI.Respo
1876
1899
  ...posthogParams,
1877
1900
  model: openAIParams.model ?? result.model,
1878
1901
  provider: 'azure',
1879
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1880
- output: sanitizeOpenAIResponse(result.output),
1902
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1903
+ output: sanitizeOpenAIResponse(result.output, this.phClient),
1881
1904
  latency,
1882
1905
  baseURL: this.baseURL,
1883
1906
  modelParameters: getModelParams(body, result.service_tier),
@@ -1904,7 +1927,7 @@ let WrappedResponses$1 = class WrappedResponses extends openai.AzureOpenAI.Respo
1904
1927
  ...posthogParams,
1905
1928
  model: openAIParams.model,
1906
1929
  provider: 'azure',
1907
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1930
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1908
1931
  output: [],
1909
1932
  latency: 0,
1910
1933
  baseURL: this.baseURL,
@@ -2180,8 +2203,8 @@ class WrappedCompletions extends Completions {
2180
2203
  ...posthogParams,
2181
2204
  model: openAIParams.model ?? modelFromResponse,
2182
2205
  provider: 'openai',
2183
- input: sanitizeOpenAI(openAIParams.messages),
2184
- output: sanitizeOpenAIResponse(formattedOutput),
2206
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
2207
+ output: sanitizeOpenAIResponse(formattedOutput, this.phClient),
2185
2208
  latency,
2186
2209
  timeToFirstToken,
2187
2210
  baseURL: this.baseURL,
@@ -2208,7 +2231,7 @@ class WrappedCompletions extends Completions {
2208
2231
  ...posthogParams,
2209
2232
  model: openAIParams.model,
2210
2233
  provider: 'openai',
2211
- input: sanitizeOpenAI(openAIParams.messages),
2234
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
2212
2235
  output: [],
2213
2236
  latency: 0,
2214
2237
  baseURL: this.baseURL,
@@ -2249,8 +2272,8 @@ class WrappedCompletions extends Completions {
2249
2272
  ...posthogParams,
2250
2273
  model: openAIParams.model ?? result.model,
2251
2274
  provider: 'openai',
2252
- input: sanitizeOpenAI(openAIParams.messages),
2253
- output: sanitizeOpenAIResponse(formattedOutput),
2275
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
2276
+ output: sanitizeOpenAIResponse(formattedOutput, this.phClient),
2254
2277
  latency,
2255
2278
  baseURL: this.baseURL,
2256
2279
  modelParameters: getModelParams(body, result.service_tier),
@@ -2280,7 +2303,7 @@ class WrappedCompletions extends Completions {
2280
2303
  ...posthogParams,
2281
2304
  model: openAIParams.model,
2282
2305
  provider: 'openai',
2283
- input: sanitizeOpenAI(openAIParams.messages),
2306
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
2284
2307
  output: [],
2285
2308
  latency: 0,
2286
2309
  baseURL: this.baseURL,
@@ -2314,7 +2337,7 @@ class WrappedResponses extends Responses {
2314
2337
  ...posthogParams,
2315
2338
  model: openAIParams.model ?? result.model,
2316
2339
  provider: 'openai',
2317
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2340
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2318
2341
  output: formatResponseOpenAI({
2319
2342
  output: result.output
2320
2343
  }),
@@ -2437,8 +2460,8 @@ class WrappedResponses extends Responses {
2437
2460
  ...posthogParams,
2438
2461
  model: openAIParams.model ?? modelFromResponse,
2439
2462
  provider: 'openai',
2440
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2441
- output: sanitizeOpenAIResponse(finalContent),
2463
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2464
+ output: sanitizeOpenAIResponse(finalContent, this.phClient),
2442
2465
  latency,
2443
2466
  timeToFirstToken,
2444
2467
  baseURL: this.baseURL,
@@ -2469,7 +2492,7 @@ class WrappedResponses extends Responses {
2469
2492
  ...posthogParams,
2470
2493
  model: openAIParams.model,
2471
2494
  provider: 'openai',
2472
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2495
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2473
2496
  output: [],
2474
2497
  latency: 0,
2475
2498
  baseURL: this.baseURL,
@@ -2513,8 +2536,8 @@ class WrappedResponses extends Responses {
2513
2536
  ...posthogParams,
2514
2537
  model: openAIParams.model ?? result.model,
2515
2538
  provider: 'openai',
2516
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2517
- output: sanitizeOpenAIResponse(formattedOutput),
2539
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2540
+ output: sanitizeOpenAIResponse(formattedOutput, this.phClient),
2518
2541
  latency,
2519
2542
  baseURL: this.baseURL,
2520
2543
  modelParameters: getModelParams(body, result.service_tier),
@@ -2545,7 +2568,7 @@ class WrappedResponses extends Responses {
2545
2568
  ...posthogParams,
2546
2569
  model: openAIParams.model,
2547
2570
  provider: 'openai',
2548
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2571
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2549
2572
  output: [],
2550
2573
  latency: 0,
2551
2574
  baseURL: this.baseURL,
@@ -2631,8 +2654,8 @@ class WrappedResponses extends Responses {
2631
2654
  ...posthogParams,
2632
2655
  model: openAIParams.model ?? result.model,
2633
2656
  provider: 'openai',
2634
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2635
- output: sanitizeOpenAIResponse(result.output),
2657
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2658
+ output: sanitizeOpenAIResponse(result.output, this.phClient),
2636
2659
  latency,
2637
2660
  baseURL: this.baseURL,
2638
2661
  modelParameters: getModelParams(body, result.service_tier),
@@ -2659,7 +2682,7 @@ class WrappedResponses extends Responses {
2659
2682
  ...posthogParams,
2660
2683
  model: openAIParams.model,
2661
2684
  provider: 'openai',
2662
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2685
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2663
2686
  output: [],
2664
2687
  latency: 0,
2665
2688
  baseURL: this.baseURL,
@@ -2804,7 +2827,7 @@ class WrappedTranscriptions extends Transcriptions {
2804
2827
  model: openAIParams.model,
2805
2828
  provider: 'openai',
2806
2829
  input: openAIParams.prompt,
2807
- output: sanitizeOpenAIResponse(finalContent),
2830
+ output: sanitizeOpenAIResponse(finalContent, this.phClient),
2808
2831
  latency,
2809
2832
  timeToFirstToken,
2810
2833
  baseURL: this.baseURL,
@@ -2849,7 +2872,7 @@ class WrappedTranscriptions extends Transcriptions {
2849
2872
  model: openAIParams.model,
2850
2873
  provider: 'openai',
2851
2874
  input: openAIParams.prompt,
2852
- output: sanitizeOpenAIResponse(result.text),
2875
+ output: sanitizeOpenAIResponse(result.text, this.phClient),
2853
2876
  latency,
2854
2877
  baseURL: this.baseURL,
2855
2878
  modelParameters: getModelParams(body),