@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.
@@ -9,6 +9,33 @@ const isString = value => {
9
9
  return typeof value === 'string';
10
10
  };
11
11
 
12
+ /** @internal */
13
+
14
+ /** @internal */
15
+
16
+ /** @internal */
17
+ function isFullAiCaptureEnabled(client) {
18
+ return client?.enableFullAiCapture === true;
19
+ }
20
+
21
+ /** @internal */
22
+ function captureAiEvent(client, event) {
23
+ if (isFullAiCaptureEnabled(client) && typeof client.captureAi === 'function') {
24
+ client.captureAi(event);
25
+ return;
26
+ }
27
+ client.capture(event);
28
+ }
29
+
30
+ /** @internal */
31
+ async function captureAiEventImmediate(client, event) {
32
+ if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === 'function') {
33
+ await client.captureAiImmediate(event);
34
+ return;
35
+ }
36
+ await client.captureImmediate(event);
37
+ }
38
+
12
39
  const DATA_URL_PREFIX_RE = /^data:([^;,\s]+)(?:;[^;,\s]+)*;base64,/i;
13
40
  const BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/;
14
41
  class Base64Recognizer {
@@ -114,7 +141,6 @@ class BinaryContentRedactor {
114
141
  this.recognizer = recognizer;
115
142
  }
116
143
  redact(value, mediaType) {
117
- if (this.isMultimodalEnabled()) return value;
118
144
  this.visited = new WeakSet();
119
145
  return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY);
120
146
  }
@@ -158,15 +184,12 @@ class BinaryContentRedactor {
158
184
  if (mediaType === 'application/octet-stream') return '[base64 file redacted]';
159
185
  return `[base64 ${mediaType} redacted]`;
160
186
  }
161
- isMultimodalEnabled() {
162
- const val = process.env._INTERNAL_LLMA_MULTIMODAL || '';
163
- return val.toLowerCase() === 'true' || val === '1' || val.toLowerCase() === 'yes';
164
- }
165
187
  }
166
188
 
167
189
  const redactor = new BinaryContentRedactor();
168
- const sanitizeOpenAI = data => redactor.redact(data);
169
- const sanitizeOpenAIResponse = data => redactor.redact(data);
190
+ const sanitize = (data, client) => isFullAiCaptureEnabled(client) ? data : redactor.redact(data);
191
+ const sanitizeOpenAI = (data, client) => sanitize(data, client);
192
+ const sanitizeOpenAIResponse = (data, client) => sanitize(data, client);
170
193
 
171
194
  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']);
172
195
 
@@ -549,7 +572,7 @@ function formatOpenAIResponsesInput(input, instructions) {
549
572
  return messages;
550
573
  }
551
574
 
552
- var version = "8.7.1";
575
+ var version = "8.8.0";
553
576
 
554
577
  const DEFAULT_MAX_DEPTH = 3;
555
578
  const MAX_STACK_LINES = 20;
@@ -798,9 +821,9 @@ const captureAiGeneration$1 = async (client, options) => {
798
821
  groups: options.groups
799
822
  };
800
823
  if (options.captureImmediate) {
801
- await client.captureImmediate(event);
824
+ await captureAiEventImmediate(client, event);
802
825
  } else {
803
- client.capture(event);
826
+ captureAiEvent(client, event);
804
827
  }
805
828
  } catch (error) {
806
829
  // Telemetry failures must never affect the instrumented provider call.
@@ -1467,8 +1490,8 @@ let WrappedCompletions$1 = class WrappedCompletions extends AzureOpenAI.Chat.Com
1467
1490
  ...posthogParams,
1468
1491
  model: openAIParams.model ?? modelFromResponse,
1469
1492
  provider: 'azure',
1470
- input: sanitizeOpenAI(openAIParams.messages),
1471
- output: sanitizeOpenAIResponse(formattedOutput),
1493
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
1494
+ output: sanitizeOpenAIResponse(formattedOutput, this.phClient),
1472
1495
  latency,
1473
1496
  timeToFirstToken,
1474
1497
  baseURL: this.baseURL,
@@ -1485,7 +1508,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends AzureOpenAI.Chat.Com
1485
1508
  ...posthogParams,
1486
1509
  model: openAIParams.model,
1487
1510
  provider: 'azure',
1488
- input: sanitizeOpenAI(openAIParams.messages),
1511
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
1489
1512
  output: [],
1490
1513
  latency: 0,
1491
1514
  baseURL: this.baseURL,
@@ -1524,8 +1547,8 @@ let WrappedCompletions$1 = class WrappedCompletions extends AzureOpenAI.Chat.Com
1524
1547
  ...posthogParams,
1525
1548
  model: openAIParams.model ?? result.model,
1526
1549
  provider: 'azure',
1527
- input: sanitizeOpenAI(openAIParams.messages),
1528
- output: sanitizeOpenAIResponse(formatResponseOpenAI(result)),
1550
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
1551
+ output: sanitizeOpenAIResponse(formatResponseOpenAI(result), this.phClient),
1529
1552
  latency,
1530
1553
  baseURL: this.baseURL,
1531
1554
  modelParameters: getModelParams(body, result.service_tier),
@@ -1551,7 +1574,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends AzureOpenAI.Chat.Com
1551
1574
  ...posthogParams,
1552
1575
  model: openAIParams.model,
1553
1576
  provider: 'azure',
1554
- input: sanitizeOpenAI(openAIParams.messages),
1577
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
1555
1578
  output: [],
1556
1579
  latency: 0,
1557
1580
  baseURL: this.baseURL,
@@ -1693,8 +1716,8 @@ let WrappedResponses$1 = class WrappedResponses extends AzureOpenAI.Responses {
1693
1716
  ...posthogParams,
1694
1717
  model: openAIParams.model ?? modelFromResponse,
1695
1718
  provider: 'azure',
1696
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1697
- output: sanitizeOpenAIResponse(finalContent),
1719
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1720
+ output: sanitizeOpenAIResponse(finalContent, this.phClient),
1698
1721
  latency,
1699
1722
  timeToFirstToken,
1700
1723
  baseURL: this.baseURL,
@@ -1716,7 +1739,7 @@ let WrappedResponses$1 = class WrappedResponses extends AzureOpenAI.Responses {
1716
1739
  ...posthogParams,
1717
1740
  model: openAIParams.model,
1718
1741
  provider: 'azure',
1719
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1742
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1720
1743
  output: [],
1721
1744
  latency: 0,
1722
1745
  baseURL: this.baseURL,
@@ -1756,8 +1779,8 @@ let WrappedResponses$1 = class WrappedResponses extends AzureOpenAI.Responses {
1756
1779
  ...posthogParams,
1757
1780
  model: openAIParams.model ?? result.model,
1758
1781
  provider: 'azure',
1759
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1760
- output: sanitizeOpenAIResponse(result.output),
1782
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1783
+ output: sanitizeOpenAIResponse(result.output, this.phClient),
1761
1784
  latency,
1762
1785
  baseURL: this.baseURL,
1763
1786
  modelParameters: getModelParams(body, result.service_tier),
@@ -1786,7 +1809,7 @@ let WrappedResponses$1 = class WrappedResponses extends AzureOpenAI.Responses {
1786
1809
  ...posthogParams,
1787
1810
  model: openAIParams.model,
1788
1811
  provider: 'azure',
1789
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1812
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1790
1813
  output: [],
1791
1814
  latency: 0,
1792
1815
  baseURL: this.baseURL,
@@ -1872,8 +1895,8 @@ let WrappedResponses$1 = class WrappedResponses extends AzureOpenAI.Responses {
1872
1895
  ...posthogParams,
1873
1896
  model: openAIParams.model ?? result.model,
1874
1897
  provider: 'azure',
1875
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1876
- output: sanitizeOpenAIResponse(result.output),
1898
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1899
+ output: sanitizeOpenAIResponse(result.output, this.phClient),
1877
1900
  latency,
1878
1901
  baseURL: this.baseURL,
1879
1902
  modelParameters: getModelParams(body, result.service_tier),
@@ -1900,7 +1923,7 @@ let WrappedResponses$1 = class WrappedResponses extends AzureOpenAI.Responses {
1900
1923
  ...posthogParams,
1901
1924
  model: openAIParams.model,
1902
1925
  provider: 'azure',
1903
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
1926
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
1904
1927
  output: [],
1905
1928
  latency: 0,
1906
1929
  baseURL: this.baseURL,
@@ -2176,8 +2199,8 @@ class WrappedCompletions extends Completions {
2176
2199
  ...posthogParams,
2177
2200
  model: openAIParams.model ?? modelFromResponse,
2178
2201
  provider: 'openai',
2179
- input: sanitizeOpenAI(openAIParams.messages),
2180
- output: sanitizeOpenAIResponse(formattedOutput),
2202
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
2203
+ output: sanitizeOpenAIResponse(formattedOutput, this.phClient),
2181
2204
  latency,
2182
2205
  timeToFirstToken,
2183
2206
  baseURL: this.baseURL,
@@ -2204,7 +2227,7 @@ class WrappedCompletions extends Completions {
2204
2227
  ...posthogParams,
2205
2228
  model: openAIParams.model,
2206
2229
  provider: 'openai',
2207
- input: sanitizeOpenAI(openAIParams.messages),
2230
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
2208
2231
  output: [],
2209
2232
  latency: 0,
2210
2233
  baseURL: this.baseURL,
@@ -2245,8 +2268,8 @@ class WrappedCompletions extends Completions {
2245
2268
  ...posthogParams,
2246
2269
  model: openAIParams.model ?? result.model,
2247
2270
  provider: 'openai',
2248
- input: sanitizeOpenAI(openAIParams.messages),
2249
- output: sanitizeOpenAIResponse(formattedOutput),
2271
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
2272
+ output: sanitizeOpenAIResponse(formattedOutput, this.phClient),
2250
2273
  latency,
2251
2274
  baseURL: this.baseURL,
2252
2275
  modelParameters: getModelParams(body, result.service_tier),
@@ -2276,7 +2299,7 @@ class WrappedCompletions extends Completions {
2276
2299
  ...posthogParams,
2277
2300
  model: openAIParams.model,
2278
2301
  provider: 'openai',
2279
- input: sanitizeOpenAI(openAIParams.messages),
2302
+ input: sanitizeOpenAI(openAIParams.messages, this.phClient),
2280
2303
  output: [],
2281
2304
  latency: 0,
2282
2305
  baseURL: this.baseURL,
@@ -2310,7 +2333,7 @@ class WrappedResponses extends Responses {
2310
2333
  ...posthogParams,
2311
2334
  model: openAIParams.model ?? result.model,
2312
2335
  provider: 'openai',
2313
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2336
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2314
2337
  output: formatResponseOpenAI({
2315
2338
  output: result.output
2316
2339
  }),
@@ -2433,8 +2456,8 @@ class WrappedResponses extends Responses {
2433
2456
  ...posthogParams,
2434
2457
  model: openAIParams.model ?? modelFromResponse,
2435
2458
  provider: 'openai',
2436
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2437
- output: sanitizeOpenAIResponse(finalContent),
2459
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2460
+ output: sanitizeOpenAIResponse(finalContent, this.phClient),
2438
2461
  latency,
2439
2462
  timeToFirstToken,
2440
2463
  baseURL: this.baseURL,
@@ -2465,7 +2488,7 @@ class WrappedResponses extends Responses {
2465
2488
  ...posthogParams,
2466
2489
  model: openAIParams.model,
2467
2490
  provider: 'openai',
2468
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2491
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2469
2492
  output: [],
2470
2493
  latency: 0,
2471
2494
  baseURL: this.baseURL,
@@ -2509,8 +2532,8 @@ class WrappedResponses extends Responses {
2509
2532
  ...posthogParams,
2510
2533
  model: openAIParams.model ?? result.model,
2511
2534
  provider: 'openai',
2512
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2513
- output: sanitizeOpenAIResponse(formattedOutput),
2535
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2536
+ output: sanitizeOpenAIResponse(formattedOutput, this.phClient),
2514
2537
  latency,
2515
2538
  baseURL: this.baseURL,
2516
2539
  modelParameters: getModelParams(body, result.service_tier),
@@ -2541,7 +2564,7 @@ class WrappedResponses extends Responses {
2541
2564
  ...posthogParams,
2542
2565
  model: openAIParams.model,
2543
2566
  provider: 'openai',
2544
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2567
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2545
2568
  output: [],
2546
2569
  latency: 0,
2547
2570
  baseURL: this.baseURL,
@@ -2627,8 +2650,8 @@ class WrappedResponses extends Responses {
2627
2650
  ...posthogParams,
2628
2651
  model: openAIParams.model ?? result.model,
2629
2652
  provider: 'openai',
2630
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2631
- output: sanitizeOpenAIResponse(result.output),
2653
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2654
+ output: sanitizeOpenAIResponse(result.output, this.phClient),
2632
2655
  latency,
2633
2656
  baseURL: this.baseURL,
2634
2657
  modelParameters: getModelParams(body, result.service_tier),
@@ -2655,7 +2678,7 @@ class WrappedResponses extends Responses {
2655
2678
  ...posthogParams,
2656
2679
  model: openAIParams.model,
2657
2680
  provider: 'openai',
2658
- input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input), openAIParams.instructions),
2681
+ input: formatOpenAIResponsesInput(sanitizeOpenAIResponse(openAIParams.input, this.phClient), openAIParams.instructions),
2659
2682
  output: [],
2660
2683
  latency: 0,
2661
2684
  baseURL: this.baseURL,
@@ -2800,7 +2823,7 @@ class WrappedTranscriptions extends Transcriptions {
2800
2823
  model: openAIParams.model,
2801
2824
  provider: 'openai',
2802
2825
  input: openAIParams.prompt,
2803
- output: sanitizeOpenAIResponse(finalContent),
2826
+ output: sanitizeOpenAIResponse(finalContent, this.phClient),
2804
2827
  latency,
2805
2828
  timeToFirstToken,
2806
2829
  baseURL: this.baseURL,
@@ -2845,7 +2868,7 @@ class WrappedTranscriptions extends Transcriptions {
2845
2868
  model: openAIParams.model,
2846
2869
  provider: 'openai',
2847
2870
  input: openAIParams.prompt,
2848
- output: sanitizeOpenAIResponse(result.text),
2871
+ output: sanitizeOpenAIResponse(result.text, this.phClient),
2849
2872
  latency,
2850
2873
  baseURL: this.baseURL,
2851
2874
  modelParameters: getModelParams(body),