@posthog/ai 8.9.3 → 8.10.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.
Files changed (51) hide show
  1. package/dist/adk/index.cjs +977 -0
  2. package/dist/adk/index.cjs.map +1 -0
  3. package/dist/adk/index.d.ts +149 -0
  4. package/dist/adk/index.mjs +976 -0
  5. package/dist/adk/index.mjs.map +1 -0
  6. package/dist/anthropic/index.cjs +927 -1104
  7. package/dist/anthropic/index.cjs.map +1 -1
  8. package/dist/anthropic/index.d.ts +34 -33
  9. package/dist/anthropic/index.mjs +899 -1095
  10. package/dist/anthropic/index.mjs.map +1 -1
  11. package/dist/gemini/index.cjs +867 -1112
  12. package/dist/gemini/index.cjs.map +1 -1
  13. package/dist/gemini/index.d.ts +38 -35
  14. package/dist/gemini/index.mjs +862 -1107
  15. package/dist/gemini/index.mjs.map +1 -1
  16. package/dist/index.cjs +1218 -1539
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.ts +170 -157
  19. package/dist/index.mjs +1216 -1537
  20. package/dist/index.mjs.map +1 -1
  21. package/dist/langchain/index.cjs +851 -1029
  22. package/dist/langchain/index.cjs.map +1 -1
  23. package/dist/langchain/index.d.ts +75 -75
  24. package/dist/langchain/index.mjs +850 -1027
  25. package/dist/langchain/index.mjs.map +1 -1
  26. package/dist/langchain/middleware/index.cjs +1016 -1225
  27. package/dist/langchain/middleware/index.cjs.map +1 -1
  28. package/dist/langchain/middleware/index.d.ts +29 -25
  29. package/dist/langchain/middleware/index.mjs +1015 -1223
  30. package/dist/langchain/middleware/index.mjs.map +1 -1
  31. package/dist/openai/index.cjs +1990 -2516
  32. package/dist/openai/index.cjs.map +1 -1
  33. package/dist/openai/index.d.ts +106 -104
  34. package/dist/openai/index.mjs +1985 -2511
  35. package/dist/openai/index.mjs.map +1 -1
  36. package/dist/openai-agents/index.cjs +745 -827
  37. package/dist/openai-agents/index.cjs.map +1 -1
  38. package/dist/openai-agents/index.d.ts +48 -47
  39. package/dist/openai-agents/index.mjs +744 -825
  40. package/dist/openai-agents/index.mjs.map +1 -1
  41. package/dist/otel/index.cjs +427 -486
  42. package/dist/otel/index.cjs.map +1 -1
  43. package/dist/otel/index.d.ts +36 -35
  44. package/dist/otel/index.mjs +426 -484
  45. package/dist/otel/index.mjs.map +1 -1
  46. package/dist/vercel/index.cjs +992 -1336
  47. package/dist/vercel/index.cjs.map +1 -1
  48. package/dist/vercel/index.d.ts +21 -16
  49. package/dist/vercel/index.mjs +991 -1334
  50. package/dist/vercel/index.mjs.map +1 -1
  51. package/package.json +23 -12
@@ -1,1423 +1,1080 @@
1
- import { v4 } from 'uuid';
2
- import { toJsonSafeValue, uuidv7 } from '@posthog/core';
3
-
4
- // Type guards for safer type checking
5
-
6
- const isString = value => {
7
- return typeof value === 'string';
1
+ import { v4 } from "uuid";
2
+ import { toJsonSafeValue, uuidv7 } from "@posthog/core";
3
+ //#region src/typeGuards.ts
4
+ const isString = (value) => {
5
+ return typeof value === "string";
8
6
  };
9
- const isObject = value => {
10
- return value !== null && typeof value === 'object' && !Array.isArray(value);
7
+ const isObject = (value) => {
8
+ return value !== null && typeof value === "object" && !Array.isArray(value);
11
9
  };
12
-
13
- /** @internal */
14
-
15
- /** @internal */
16
-
10
+ //#endregion
11
+ //#region src/captureAiEvent.ts
17
12
  /** @internal */
18
13
  function isFullAiCaptureEnabled(client) {
19
- return client?.enableFullAiCapture === true;
14
+ return client?.enableFullAiCapture === true;
20
15
  }
21
-
22
16
  /** @internal */
23
17
  function captureAiEvent(client, event) {
24
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === 'function') {
25
- client.captureAi(event);
26
- return;
27
- }
28
- client.capture(event);
18
+ if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
19
+ client.captureAi(event);
20
+ return;
21
+ }
22
+ client.capture(event);
29
23
  }
30
-
31
24
  /** @internal */
32
25
  async function captureAiEventImmediate(client, event) {
33
- if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === 'function') {
34
- await client.captureAiImmediate(event);
35
- return;
36
- }
37
- await client.captureImmediate(event);
26
+ if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === "function") {
27
+ await client.captureAiImmediate(event);
28
+ return;
29
+ }
30
+ await client.captureImmediate(event);
38
31
  }
39
-
32
+ //#endregion
33
+ //#region src/sanitization/base64_recognizer.ts
40
34
  const DATA_URL_PREFIX_RE = /^data:([^;,\s]+)(?:;[^;,\s]+)*;base64,/i;
41
35
  const BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/;
42
- class Base64Recognizer {
43
- recognize(value, minLength) {
44
- const dataUrl = DATA_URL_PREFIX_RE.exec(value);
45
- if (dataUrl) return {
46
- kind: 'data-url',
47
- mediaType: dataUrl[1]
48
- };
49
- if (value.length < minLength) return {
50
- kind: 'none'
51
- };
52
- const confidencePrefix = value.slice(0, minLength);
53
- if (BASE64_ALPHABET_RE.test(confidencePrefix)) {
54
- return {
55
- kind: 'raw'
56
- };
57
- } else {
58
- return {
59
- kind: 'none'
60
- };
61
- }
62
- }
63
- }
64
-
65
- const MIME_HINT_KEYS = ['mediaType', 'media_type', 'mimeType', 'mime_type'];
66
- const STRONG_CONTEXT_KEYS = new Set(['data', 'file_data', 'fileData', 'image_url', 'imageUrl', 'video_url', 'videoUrl', 'audio', 'audio_data', 'audioData', 'inline_data', 'inlineData', 'source', 'result']);
67
- const STRONG_CONTEXT_TYPES = new Set(['image', 'image_url', 'input_image', 'audio', 'input_audio', 'video', 'video_url', 'file', 'input_file', 'document', 'media', 'file-data']);
68
- const FILE_FAMILY_TYPES = new Set(['file', 'input_file', 'document', 'media', 'file-data']);
69
- const KNOWN_AUDIO_FORMATS = new Set(['wav', 'mp3', 'ogg', 'flac', 'm4a', 'aac', 'webm']);
70
- class MediaTypeContext {
71
- static EMPTY = new MediaTypeContext(undefined, undefined);
72
- constructor(parent, key, explicitMediaType) {
73
- this.parent = parent;
74
- this.key = key;
75
- this.explicitMediaType = explicitMediaType;
76
- }
77
- inferMediaType() {
78
- return this.inferFromSiblingMime() ?? this.inferFromSiblingFormat() ?? this.inferFromParentType() ?? this.inferFromKey();
79
- }
80
- inferFromSiblingMime() {
81
- if (this.explicitMediaType) return this.explicitMediaType;
82
- if (!this.parent) return undefined;
83
- for (const hint of MIME_HINT_KEYS) {
84
- const v = this.parent[hint];
85
- if (typeof v === 'string') return v;
86
- }
87
- return undefined;
88
- }
89
- inferFromSiblingFormat() {
90
- if (!this.parent) return undefined;
91
- const fmt = this.parent.format;
92
- if (typeof fmt === 'string' && KNOWN_AUDIO_FORMATS.has(fmt.toLowerCase())) {
93
- return `audio/${fmt.toLowerCase()}`;
94
- }
95
- return undefined;
96
- }
97
- inferFromParentType() {
98
- if (!this.parent) return undefined;
99
- const t = this.parent.type;
100
- if (typeof t !== 'string') return undefined;
101
- if (t === 'image' || t === 'image_url' || t === 'input_image') return 'image';
102
- if (t === 'audio' || t === 'input_audio') return 'audio';
103
- if (t === 'video' || t === 'video_url') return 'video';
104
- if (FILE_FAMILY_TYPES.has(t)) return 'application/octet-stream';
105
- return undefined;
106
- }
107
- inferFromKey() {
108
- if (!this.key) return undefined;
109
- const key = this.key.toLowerCase();
110
- if (key.includes('audio')) return 'audio';
111
- if (key.includes('video')) return 'video';
112
- if (key.includes('image')) return 'image';
113
- if (key.includes('file') || key.includes('document')) return 'application/octet-stream';
114
- return undefined;
115
- }
116
- hasExplicitBinaryMediaType() {
117
- if (!this.explicitMediaType && (!this.parent || !this.key || !STRONG_CONTEXT_KEYS.has(this.key))) return false;
118
- const mediaType = this.inferFromSiblingMime();
119
- return mediaType !== undefined && !mediaType.toLowerCase().startsWith('text/');
120
- }
121
- signalsBinary() {
122
- if (this.explicitMediaType) return true;
123
- if (this.parent) {
124
- for (const hint of MIME_HINT_KEYS) {
125
- if (typeof this.parent[hint] === 'string') return true;
126
- }
127
- const fmt = this.parent.format;
128
- if (typeof fmt === 'string' && KNOWN_AUDIO_FORMATS.has(fmt.toLowerCase())) return true;
129
- const t = this.parent.type;
130
- if (typeof t === 'string' && STRONG_CONTEXT_TYPES.has(t)) return true;
131
- }
132
- if (this.key && STRONG_CONTEXT_KEYS.has(this.key)) return true;
133
- return false;
134
- }
135
- }
136
-
36
+ var Base64Recognizer = class {
37
+ recognize(value, minLength) {
38
+ const dataUrl = DATA_URL_PREFIX_RE.exec(value);
39
+ if (dataUrl) return {
40
+ kind: "data-url",
41
+ mediaType: dataUrl[1]
42
+ };
43
+ if (value.length < minLength) return { kind: "none" };
44
+ const confidencePrefix = value.slice(0, minLength);
45
+ if (BASE64_ALPHABET_RE.test(confidencePrefix)) return { kind: "raw" };
46
+ else return { kind: "none" };
47
+ }
48
+ };
49
+ //#endregion
50
+ //#region src/sanitization/media_type_context.ts
51
+ const MIME_HINT_KEYS = [
52
+ "mediaType",
53
+ "media_type",
54
+ "mimeType",
55
+ "mime_type"
56
+ ];
57
+ const STRONG_CONTEXT_KEYS = /* @__PURE__ */ new Set([
58
+ "data",
59
+ "file_data",
60
+ "fileData",
61
+ "image_url",
62
+ "imageUrl",
63
+ "video_url",
64
+ "videoUrl",
65
+ "audio",
66
+ "audio_data",
67
+ "audioData",
68
+ "inline_data",
69
+ "inlineData",
70
+ "source",
71
+ "result"
72
+ ]);
73
+ const STRONG_CONTEXT_TYPES = /* @__PURE__ */ new Set([
74
+ "image",
75
+ "image_url",
76
+ "input_image",
77
+ "audio",
78
+ "input_audio",
79
+ "video",
80
+ "video_url",
81
+ "file",
82
+ "input_file",
83
+ "document",
84
+ "media",
85
+ "file-data"
86
+ ]);
87
+ const FILE_FAMILY_TYPES = /* @__PURE__ */ new Set([
88
+ "file",
89
+ "input_file",
90
+ "document",
91
+ "media",
92
+ "file-data"
93
+ ]);
94
+ const KNOWN_AUDIO_FORMATS = /* @__PURE__ */ new Set([
95
+ "wav",
96
+ "mp3",
97
+ "ogg",
98
+ "flac",
99
+ "m4a",
100
+ "aac",
101
+ "webm"
102
+ ]);
103
+ var MediaTypeContext = class MediaTypeContext {
104
+ static {
105
+ this.EMPTY = new MediaTypeContext(void 0, void 0);
106
+ }
107
+ constructor(parent, key, explicitMediaType) {
108
+ this.parent = parent;
109
+ this.key = key;
110
+ this.explicitMediaType = explicitMediaType;
111
+ }
112
+ inferMediaType() {
113
+ return this.inferFromSiblingMime() ?? this.inferFromSiblingFormat() ?? this.inferFromParentType() ?? this.inferFromKey();
114
+ }
115
+ inferFromSiblingMime() {
116
+ if (this.explicitMediaType) return this.explicitMediaType;
117
+ if (!this.parent) return void 0;
118
+ for (const hint of MIME_HINT_KEYS) {
119
+ const v = this.parent[hint];
120
+ if (typeof v === "string") return v;
121
+ }
122
+ }
123
+ inferFromSiblingFormat() {
124
+ if (!this.parent) return void 0;
125
+ const fmt = this.parent.format;
126
+ if (typeof fmt === "string" && KNOWN_AUDIO_FORMATS.has(fmt.toLowerCase())) return `audio/${fmt.toLowerCase()}`;
127
+ }
128
+ inferFromParentType() {
129
+ if (!this.parent) return void 0;
130
+ const t = this.parent.type;
131
+ if (typeof t !== "string") return void 0;
132
+ if (t === "image" || t === "image_url" || t === "input_image") return "image";
133
+ if (t === "audio" || t === "input_audio") return "audio";
134
+ if (t === "video" || t === "video_url") return "video";
135
+ if (FILE_FAMILY_TYPES.has(t)) return "application/octet-stream";
136
+ }
137
+ inferFromKey() {
138
+ if (!this.key) return void 0;
139
+ const key = this.key.toLowerCase();
140
+ if (key.includes("audio")) return "audio";
141
+ if (key.includes("video")) return "video";
142
+ if (key.includes("image")) return "image";
143
+ if (key.includes("file") || key.includes("document")) return "application/octet-stream";
144
+ }
145
+ hasExplicitBinaryMediaType() {
146
+ if (!this.explicitMediaType && (!this.parent || !this.key || !STRONG_CONTEXT_KEYS.has(this.key))) return false;
147
+ const mediaType = this.inferFromSiblingMime();
148
+ return mediaType !== void 0 && !mediaType.toLowerCase().startsWith("text/");
149
+ }
150
+ signalsBinary() {
151
+ if (this.explicitMediaType) return true;
152
+ if (this.parent) {
153
+ for (const hint of MIME_HINT_KEYS) if (typeof this.parent[hint] === "string") return true;
154
+ const fmt = this.parent.format;
155
+ if (typeof fmt === "string" && KNOWN_AUDIO_FORMATS.has(fmt.toLowerCase())) return true;
156
+ const t = this.parent.type;
157
+ if (typeof t === "string" && STRONG_CONTEXT_TYPES.has(t)) return true;
158
+ }
159
+ if (this.key && STRONG_CONTEXT_KEYS.has(this.key)) return true;
160
+ return false;
161
+ }
162
+ };
163
+ //#endregion
164
+ //#region src/sanitization/binary_content_redactor.ts
137
165
  const STRONG_CONTEXT_MIN_LENGTH = 64;
138
166
  const WEAK_CONTEXT_MIN_LENGTH = 1024;
139
- class BinaryContentRedactor {
140
- visited = new WeakSet();
141
- constructor(recognizer = new Base64Recognizer()) {
142
- this.recognizer = recognizer;
143
- }
144
- redact(value, mediaType) {
145
- this.visited = new WeakSet();
146
- return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY);
147
- }
148
- walk(value, ctx) {
149
- if (value === null || value === undefined) return value;
150
- if (typeof value === 'string') return this.redactString(value, ctx);
151
- if (typeof value !== 'object') return value;
152
-
153
- // Buffer extends Uint8Array, so this branch catches both.
154
- if (typeof Uint8Array !== 'undefined' && value instanceof Uint8Array) {
155
- return this.placeholderFor(ctx.inferMediaType());
156
- }
157
- if (this.visited.has(value)) return null;
158
- this.visited.add(value);
159
- if (Array.isArray(value)) {
160
- return value.map(item => this.walk(item, ctx));
161
- }
162
- const obj = value;
163
- const out = {};
164
- for (const k of Object.keys(obj)) {
165
- out[k] = this.walk(obj[k], new MediaTypeContext(obj, k));
166
- }
167
- return out;
168
- }
169
- redactString(value, ctx) {
170
- const hasExplicitBinaryMediaType = ctx.hasExplicitBinaryMediaType();
171
- const recognitionValue = hasExplicitBinaryMediaType ? value.replace(/[\r\n]/g, '') : value;
172
- const minLength = hasExplicitBinaryMediaType ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH) : ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
173
- const recognition = this.recognizer.recognize(recognitionValue, minLength);
174
- switch (recognition.kind) {
175
- case 'data-url':
176
- return this.placeholderFor(recognition.mediaType);
177
- case 'raw':
178
- return this.placeholderFor(ctx.inferMediaType());
179
- case 'none':
180
- return value;
181
- }
182
- }
183
- placeholderFor(mediaType) {
184
- if (!mediaType) return '[base64 redacted]';
185
- if (mediaType === 'application/octet-stream') return '[base64 file redacted]';
186
- return `[base64 ${mediaType} redacted]`;
187
- }
188
- }
189
-
167
+ var BinaryContentRedactor = class {
168
+ constructor(recognizer = new Base64Recognizer()) {
169
+ this.recognizer = recognizer;
170
+ this.visited = /* @__PURE__ */ new WeakSet();
171
+ }
172
+ redact(value, mediaType) {
173
+ this.visited = /* @__PURE__ */ new WeakSet();
174
+ return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
175
+ }
176
+ walk(value, ctx) {
177
+ if (value === null || value === void 0) return value;
178
+ if (typeof value === "string") return this.redactString(value, ctx);
179
+ if (typeof value !== "object") return value;
180
+ if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.placeholderFor(ctx.inferMediaType());
181
+ if (this.visited.has(value)) return null;
182
+ this.visited.add(value);
183
+ if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
184
+ const obj = value;
185
+ const out = {};
186
+ for (const k of Object.keys(obj)) out[k] = this.walk(obj[k], new MediaTypeContext(obj, k));
187
+ return out;
188
+ }
189
+ redactString(value, ctx) {
190
+ const hasExplicitBinaryMediaType = ctx.hasExplicitBinaryMediaType();
191
+ const recognitionValue = hasExplicitBinaryMediaType ? value.replace(/[\r\n]/g, "") : value;
192
+ const minLength = hasExplicitBinaryMediaType ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH) : ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
193
+ const recognition = this.recognizer.recognize(recognitionValue, minLength);
194
+ switch (recognition.kind) {
195
+ case "data-url": return this.placeholderFor(recognition.mediaType);
196
+ case "raw": return this.placeholderFor(ctx.inferMediaType());
197
+ case "none": return value;
198
+ }
199
+ }
200
+ placeholderFor(mediaType) {
201
+ if (!mediaType) return "[base64 redacted]";
202
+ if (mediaType === "application/octet-stream") return "[base64 file redacted]";
203
+ return `[base64 ${mediaType} redacted]`;
204
+ }
205
+ };
206
+ //#endregion
207
+ //#region src/sanitization.ts
190
208
  const redactor = new BinaryContentRedactor();
191
209
  function redactBase64DataUrl(str, mediaType) {
192
- return redactor.redact(str, mediaType);
210
+ return redactor.redact(str, mediaType);
193
211
  }
194
212
  const sanitize = (data, client) => isFullAiCaptureEnabled(client) ? data : redactor.redact(data);
195
213
  const sanitizeVercel = (data, client) => sanitize(data, client);
196
-
197
- 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']);
198
-
214
+ //#endregion
215
+ //#region src/utils.ts
216
+ const TOKEN_PROPERTY_KEYS = /* @__PURE__ */ new Set([
217
+ "$ai_input_tokens",
218
+ "$ai_output_tokens",
219
+ "$ai_cache_read_input_tokens",
220
+ "$ai_cache_creation_input_tokens",
221
+ "$ai_total_tokens",
222
+ "$ai_reasoning_tokens"
223
+ ]);
199
224
  /**
200
- * Whether the caller supplied their own token counts, which override the ones the SDK
201
- * derived from the provider response.
202
- */
225
+ * Whether the caller supplied their own token counts, which override the ones the SDK
226
+ * derived from the provider response.
227
+ */
203
228
  function hasTokenOverrides(posthogProperties) {
204
- return !!posthogProperties && Object.keys(posthogProperties).some(key => TOKEN_PROPERTY_KEYS.has(key));
229
+ return !!posthogProperties && Object.keys(posthogProperties).some((key) => TOKEN_PROPERTY_KEYS.has(key));
205
230
  }
206
231
  function getTokensSource(posthogProperties) {
207
- return hasTokenOverrides(posthogProperties) ? 'passthrough' : 'sdk';
232
+ return hasTokenOverrides(posthogProperties) ? "passthrough" : "sdk";
208
233
  }
209
-
210
- // limit large outputs by truncating to 200kb (approx 200k bytes)
211
- const MAX_OUTPUT_SIZE = 200000;
212
- const STRING_FORMAT = 'utf8';
213
-
214
- // Reused across calls to avoid per-invocation allocation; truncate() runs
215
- // hundreds of times for prompts with many parts.
234
+ const MAX_OUTPUT_SIZE = 2e5;
235
+ const STRING_FORMAT = "utf8";
216
236
  const sharedTextEncoder = new TextEncoder();
217
- const sharedTextDecoder = new TextDecoder(STRING_FORMAT, {
218
- fatal: false
219
- });
220
- const utf8ByteLength = str => sharedTextEncoder.encode(str).byteLength;
221
-
237
+ const sharedTextDecoder = new TextDecoder(STRING_FORMAT, { fatal: false });
238
+ const utf8ByteLength = (str) => sharedTextEncoder.encode(str).byteLength;
222
239
  /**
223
- * Safely converts content to a string, preserving structure for objects/arrays.
224
- * - If content is already a string, returns it as-is
225
- * - If content is an object or array, stringifies it with JSON.stringify to preserve structure
226
- * - Otherwise, converts to string with String()
227
- *
228
- * This prevents the "[object Object]" bug when objects are naively converted to strings.
229
- *
230
- * @param content - The content to convert to a string
231
- * @returns A string representation that preserves structure for complex types
232
- */
240
+ * Safely converts content to a string, preserving structure for objects/arrays.
241
+ * - If content is already a string, returns it as-is
242
+ * - If content is an object or array, stringifies it with JSON.stringify to preserve structure
243
+ * - Otherwise, converts to string with String()
244
+ *
245
+ * This prevents the "[object Object]" bug when objects are naively converted to strings.
246
+ *
247
+ * @param content - The content to convert to a string
248
+ * @returns A string representation that preserves structure for complex types
249
+ */
233
250
  function toContentString(content) {
234
- if (typeof content === 'string') {
235
- return content;
236
- }
237
- if (content !== undefined && content !== null && typeof content === 'object') {
238
- try {
239
- return JSON.stringify(content);
240
- } catch {
241
- // Fallback for circular refs, BigInt, or objects with throwing toJSON
242
- return String(content);
243
- }
244
- }
245
- return String(content);
251
+ if (typeof content === "string") return content;
252
+ if (content !== void 0 && content !== null && typeof content === "object") try {
253
+ return JSON.stringify(content);
254
+ } catch {
255
+ return String(content);
256
+ }
257
+ return String(content);
246
258
  }
247
259
  const getModelParams = (params, responseServiceTier) => {
248
- if (!params) {
249
- return {};
250
- }
251
- const modelParams = {};
252
- const paramKeys = ['temperature', 'max_tokens', 'max_completion_tokens', 'top_p', 'frequency_penalty', 'presence_penalty', 'n', 'stop', 'stream', 'streaming', 'language', 'response_format', 'timestamp_granularities', 'service_tier'];
253
- for (const key of paramKeys) {
254
- if (key in params && params[key] !== undefined) {
255
- modelParams[key] = params[key];
256
- }
257
- }
258
- return modelParams;
260
+ if (!params) return {};
261
+ const modelParams = {};
262
+ for (const key of [
263
+ "temperature",
264
+ "max_tokens",
265
+ "max_completion_tokens",
266
+ "top_p",
267
+ "frequency_penalty",
268
+ "presence_penalty",
269
+ "n",
270
+ "stop",
271
+ "stream",
272
+ "streaming",
273
+ "language",
274
+ "response_format",
275
+ "timestamp_granularities",
276
+ "service_tier"
277
+ ]) if (key in params && params[key] !== void 0) modelParams[key] = params[key];
278
+ if (responseServiceTier != null) modelParams.service_tier = responseServiceTier;
279
+ return modelParams;
259
280
  };
260
281
  const withPrivacyMode = (client, privacyMode, input) => {
261
- return client.privacy_mode || privacyMode ? null : input;
282
+ return client.privacy_mode || privacyMode ? null : input;
262
283
  };
263
284
  function toSafeString(input) {
264
- if (input === undefined || input === null) {
265
- return '';
266
- }
267
- if (typeof input === 'string') {
268
- return input;
269
- }
270
- try {
271
- return JSON.stringify(input);
272
- } catch {
273
- console.warn('Failed to stringify input', input);
274
- return '';
275
- }
285
+ if (input === void 0 || input === null) return "";
286
+ if (typeof input === "string") return input;
287
+ try {
288
+ return JSON.stringify(input);
289
+ } catch {
290
+ console.warn("Failed to stringify input", input);
291
+ return "";
292
+ }
276
293
  }
277
294
  const truncate = (input, client) => {
278
- const str = toSafeString(input);
279
- if (str === '') {
280
- return '';
281
- }
282
- if (isFullAiCaptureEnabled(client)) {
283
- return str;
284
- }
285
-
286
- // Check if we need to truncate and ensure STRING_FORMAT is respected
287
- const buffer = sharedTextEncoder.encode(str);
288
- if (buffer.length <= MAX_OUTPUT_SIZE) {
289
- // Ensure STRING_FORMAT is respected
290
- return sharedTextDecoder.decode(buffer);
291
- }
292
-
293
- // Truncate the buffer and ensure a valid string is returned.
294
- // fatal: false means we get U+FFFD at the end if truncation broke the encoding.
295
- const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
296
- let truncatedStr = sharedTextDecoder.decode(truncatedBuffer);
297
- if (truncatedStr.endsWith('\uFFFD')) {
298
- truncatedStr = truncatedStr.slice(0, -1);
299
- }
300
- return `${truncatedStr}... [truncated]`;
295
+ const str = toSafeString(input);
296
+ if (str === "") return "";
297
+ if (isFullAiCaptureEnabled(client)) return str;
298
+ const buffer = sharedTextEncoder.encode(str);
299
+ if (buffer.length <= 2e5) return sharedTextDecoder.decode(buffer);
300
+ const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
301
+ let truncatedStr = sharedTextDecoder.decode(truncatedBuffer);
302
+ if (truncatedStr.endsWith("�")) truncatedStr = truncatedStr.slice(0, -1);
303
+ return `${truncatedStr}... [truncated]`;
301
304
  };
302
-
303
305
  /**
304
- * Calculate web search count from raw API response.
305
- *
306
- * Uses a two-tier detection strategy:
307
- * Priority 1 (Exact Count): Count actual web search calls when available
308
- * Priority 2 (Binary Detection): Return 1 if web search indicators are present, 0 otherwise
309
- *
310
- * @param result - Raw API response from any provider (OpenAI, Perplexity, OpenRouter, Gemini, etc.)
311
- * @returns Number of web searches performed (exact count or binary 1/0)
312
- */
306
+ * Calculate web search count from raw API response.
307
+ *
308
+ * Uses a two-tier detection strategy:
309
+ * Priority 1 (Exact Count): Count actual web search calls when available
310
+ * Priority 2 (Binary Detection): Return 1 if web search indicators are present, 0 otherwise
311
+ *
312
+ * @param result - Raw API response from any provider (OpenAI, Perplexity, OpenRouter, Gemini, etc.)
313
+ * @returns Number of web searches performed (exact count or binary 1/0)
314
+ */
313
315
  function calculateWebSearchCount(result) {
314
- if (!result || typeof result !== 'object') {
315
- return 0;
316
- }
317
-
318
- // Priority 1: Exact Count
319
- // Check for OpenAI Responses API web_search_call items
320
- if ('output' in result && Array.isArray(result.output)) {
321
- let count = 0;
322
- for (const item of result.output) {
323
- if (typeof item === 'object' && item !== null && 'type' in item && item.type === 'web_search_call') {
324
- count++;
325
- }
326
- }
327
- if (count > 0) {
328
- return count;
329
- }
330
- }
331
-
332
- // Priority 2: Binary Detection (1 or 0)
333
-
334
- // Check for citations at root level (Perplexity)
335
- if ('citations' in result && Array.isArray(result.citations) && result.citations.length > 0) {
336
- return 1;
337
- }
338
-
339
- // Check for search_results at root level (Perplexity via OpenRouter)
340
- if ('search_results' in result && Array.isArray(result.search_results) && result.search_results.length > 0) {
341
- return 1;
342
- }
343
-
344
- // Check for usage.search_context_size (Perplexity via OpenRouter)
345
- if ('usage' in result && typeof result.usage === 'object' && result.usage !== null) {
346
- if ('search_context_size' in result.usage && result.usage.search_context_size) {
347
- return 1;
348
- }
349
- }
350
-
351
- // Check for annotations with url_citation in choices[].message or choices[].delta (OpenAI/Perplexity)
352
- if ('choices' in result && Array.isArray(result.choices)) {
353
- for (const choice of result.choices) {
354
- if (typeof choice === 'object' && choice !== null) {
355
- // Check both message (non-streaming) and delta (streaming) for annotations
356
- const content = ('message' in choice ? choice.message : null) || ('delta' in choice ? choice.delta : null);
357
- if (typeof content === 'object' && content !== null && 'annotations' in content) {
358
- const annotations = content.annotations;
359
- if (Array.isArray(annotations)) {
360
- const hasUrlCitation = annotations.some(ann => {
361
- return typeof ann === 'object' && ann !== null && 'type' in ann && ann.type === 'url_citation';
362
- });
363
- if (hasUrlCitation) {
364
- return 1;
365
- }
366
- }
367
- }
368
- }
369
- }
370
- }
371
-
372
- // Check for annotations in output[].content[] (OpenAI Responses API)
373
- if ('output' in result && Array.isArray(result.output)) {
374
- for (const item of result.output) {
375
- if (typeof item === 'object' && item !== null && 'content' in item) {
376
- const content = item.content;
377
- if (Array.isArray(content)) {
378
- for (const contentItem of content) {
379
- if (typeof contentItem === 'object' && contentItem !== null && 'annotations' in contentItem) {
380
- const annotations = contentItem.annotations;
381
- if (Array.isArray(annotations)) {
382
- const hasUrlCitation = annotations.some(ann => {
383
- return typeof ann === 'object' && ann !== null && 'type' in ann && ann.type === 'url_citation';
384
- });
385
- if (hasUrlCitation) {
386
- return 1;
387
- }
388
- }
389
- }
390
- }
391
- }
392
- }
393
- }
394
- }
395
-
396
- // Check for grounding_metadata (Gemini)
397
- if ('candidates' in result && Array.isArray(result.candidates)) {
398
- for (const candidate of result.candidates) {
399
- if (typeof candidate === 'object' && candidate !== null && 'grounding_metadata' in candidate && candidate.grounding_metadata) {
400
- return 1;
401
- }
402
- }
403
- }
404
- return 0;
316
+ if (!result || typeof result !== "object") return 0;
317
+ if ("output" in result && Array.isArray(result.output)) {
318
+ let count = 0;
319
+ for (const item of result.output) if (typeof item === "object" && item !== null && "type" in item && item.type === "web_search_call") count++;
320
+ if (count > 0) return count;
321
+ }
322
+ if ("citations" in result && Array.isArray(result.citations) && result.citations.length > 0) return 1;
323
+ if ("search_results" in result && Array.isArray(result.search_results) && result.search_results.length > 0) return 1;
324
+ if ("usage" in result && typeof result.usage === "object" && result.usage !== null) {
325
+ if ("search_context_size" in result.usage && result.usage.search_context_size) return 1;
326
+ }
327
+ if ("choices" in result && Array.isArray(result.choices)) {
328
+ for (const choice of result.choices) if (typeof choice === "object" && choice !== null) {
329
+ const content = ("message" in choice ? choice.message : null) || ("delta" in choice ? choice.delta : null);
330
+ if (typeof content === "object" && content !== null && "annotations" in content) {
331
+ const annotations = content.annotations;
332
+ if (Array.isArray(annotations)) {
333
+ if (annotations.some((ann) => {
334
+ return typeof ann === "object" && ann !== null && "type" in ann && ann.type === "url_citation";
335
+ })) return 1;
336
+ }
337
+ }
338
+ }
339
+ }
340
+ if ("output" in result && Array.isArray(result.output)) {
341
+ for (const item of result.output) if (typeof item === "object" && item !== null && "content" in item) {
342
+ const content = item.content;
343
+ if (Array.isArray(content)) {
344
+ for (const contentItem of content) if (typeof contentItem === "object" && contentItem !== null && "annotations" in contentItem) {
345
+ const annotations = contentItem.annotations;
346
+ if (Array.isArray(annotations)) {
347
+ if (annotations.some((ann) => {
348
+ return typeof ann === "object" && ann !== null && "type" in ann && ann.type === "url_citation";
349
+ })) return 1;
350
+ }
351
+ }
352
+ }
353
+ }
354
+ }
355
+ if ("candidates" in result && Array.isArray(result.candidates)) {
356
+ for (const candidate of result.candidates) if (typeof candidate === "object" && candidate !== null && "grounding_metadata" in candidate && candidate.grounding_metadata) return 1;
357
+ }
358
+ return 0;
405
359
  }
406
-
407
360
  /**
408
- * Extract available tool calls from the request parameters.
409
- * These are the tools provided to the LLM, not the tool calls in the response.
410
- */
361
+ * Extract available tool calls from the request parameters.
362
+ * These are the tools provided to the LLM, not the tool calls in the response.
363
+ */
411
364
  const extractAvailableToolCalls = (provider, params) => {
412
- {
413
- if (params.tools) {
414
- return params.tools;
415
- }
416
- return null;
417
- }
365
+ if (provider === "anthropic") {
366
+ if (params.tools) return params.tools;
367
+ return null;
368
+ } else if (provider === "gemini") {
369
+ if (params.config && params.config.tools) return params.config.tools;
370
+ return null;
371
+ } else if (provider === "openai") {
372
+ if (params.tools) return params.tools;
373
+ return null;
374
+ } else if (provider === "vercel") {
375
+ if (params.tools) return params.tools;
376
+ return null;
377
+ }
378
+ return null;
418
379
  };
419
- let AIEvent = /*#__PURE__*/function (AIEvent) {
420
- AIEvent["Generation"] = "$ai_generation";
421
- AIEvent["Embedding"] = "$ai_embedding";
422
- return AIEvent;
423
- }({});
424
380
  function sanitizeValues(obj) {
425
- if (obj === undefined || obj === null) {
426
- return obj;
427
- }
428
- const jsonSafe = JSON.parse(JSON.stringify(obj));
429
- if (typeof jsonSafe === 'string') {
430
- // Sanitize lone surrogates by round-tripping through UTF-8
431
- return new TextDecoder().decode(new TextEncoder().encode(jsonSafe));
432
- } else if (Array.isArray(jsonSafe)) {
433
- return jsonSafe.map(sanitizeValues);
434
- } else if (jsonSafe && typeof jsonSafe === 'object') {
435
- return Object.fromEntries(Object.entries(jsonSafe).map(([k, v]) => [k, sanitizeValues(v)]));
436
- }
437
- return jsonSafe;
381
+ if (obj === void 0 || obj === null) return obj;
382
+ const jsonSafe = JSON.parse(JSON.stringify(obj));
383
+ if (typeof jsonSafe === "string") return new TextDecoder().decode(new TextEncoder().encode(jsonSafe));
384
+ else if (Array.isArray(jsonSafe)) return jsonSafe.map(sanitizeValues);
385
+ else if (jsonSafe && typeof jsonSafe === "object") return Object.fromEntries(Object.entries(jsonSafe).map(([k, v]) => [k, sanitizeValues(v)]));
386
+ return jsonSafe;
438
387
  }
439
-
440
- var version = "8.9.3";
441
-
388
+ //#endregion
389
+ //#region package.json
390
+ var version = "8.10.1";
391
+ //#endregion
392
+ //#region src/serializeError.ts
442
393
  const DEFAULT_MAX_DEPTH = 3;
443
394
  const MAX_STACK_LINES = 20;
444
395
  function serializeError(value, depth = DEFAULT_MAX_DEPTH) {
445
- if (depth < 0 || value === null || typeof value !== 'object') {
446
- return value;
447
- }
448
- if (value instanceof Error) {
449
- const out = {
450
- name: value.name,
451
- message: value.message,
452
- stack: truncateStack(value.stack)
453
- };
454
- for (const key of Object.keys(value)) {
455
- out[key] = serializeError(value[key], depth - 1);
456
- }
457
- if (value.cause !== undefined) {
458
- out.cause = serializeError(value.cause, depth - 1);
459
- }
460
- return out;
461
- }
462
- if (Array.isArray(value)) {
463
- return value.map(item => serializeError(item, depth - 1));
464
- }
465
- return value;
396
+ if (depth < 0 || value === null || typeof value !== "object") return value;
397
+ if (value instanceof Error) {
398
+ const out = {
399
+ name: value.name,
400
+ message: value.message,
401
+ stack: truncateStack(value.stack)
402
+ };
403
+ for (const key of Object.keys(value)) out[key] = serializeError(value[key], depth - 1);
404
+ if (value.cause !== void 0) out.cause = serializeError(value.cause, depth - 1);
405
+ return out;
406
+ }
407
+ if (Array.isArray(value)) return value.map((item) => serializeError(item, depth - 1));
408
+ return value;
466
409
  }
467
410
  function stringifyError(error) {
468
- try {
469
- return JSON.stringify(sanitizeValues(serializeError(error)));
470
- } catch {
471
- if (error instanceof Error) {
472
- return JSON.stringify({
473
- name: error.name,
474
- message: error.message
475
- });
476
- }
477
- return JSON.stringify({
478
- message: String(error)
479
- });
480
- }
411
+ try {
412
+ return JSON.stringify(sanitizeValues(serializeError(error)));
413
+ } catch {
414
+ if (error instanceof Error) return JSON.stringify({
415
+ name: error.name,
416
+ message: error.message
417
+ });
418
+ return JSON.stringify({ message: String(error) });
419
+ }
481
420
  }
482
421
  function truncateStack(stack) {
483
- if (!stack) {
484
- return stack;
485
- }
486
- const lines = stack.split('\n');
487
- if (lines.length <= MAX_STACK_LINES) {
488
- return stack;
489
- }
490
- return [...lines.slice(0, MAX_STACK_LINES), '... (truncated)'].join('\n');
422
+ if (!stack) return stack;
423
+ const lines = stack.split("\n");
424
+ if (lines.length <= MAX_STACK_LINES) return stack;
425
+ return [...lines.slice(0, MAX_STACK_LINES), "... (truncated)"].join("\n");
491
426
  }
492
-
493
- // Warn when a wrapper's base_url points at the PostHog AI Gateway: the gateway
494
- // emits its own $ai_generation, so each call would be captured (and, for billable
495
- // products, billed) twice. We only warn — the wrapper's event carries data the
496
- // gateway never sees (groups, custom properties, trace hierarchy).
497
-
498
- // Keep in sync with the gateway's deployed hosts (see services/llm-gateway in the
499
- // main repo). gateway.us.posthog.com is live today; the rest are listed ahead of
500
- // any traffic moving to them.
501
- const POSTHOG_AI_GATEWAY_HOSTS = ['gateway.posthog.com', 'gateway.us.posthog.com', 'gateway.eu.posthog.com', 'ai-gateway.us.posthog.com', 'ai-gateway.eu.posthog.com'];
502
-
503
- // Swap for the dedicated AI Gateway page once it ships.
504
- const GATEWAY_DOCS_URL = 'https://posthog.com/docs/ai-observability';
505
- const extractHost = baseURL => {
506
- try {
507
- // Tolerate bare hosts that omit a scheme, e.g. "gateway.us.posthog.com/v1".
508
- const hasScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(baseURL);
509
- return new URL(hasScheme ? baseURL : `https://${baseURL}`).hostname.toLowerCase();
510
- } catch {
511
- return undefined;
512
- }
427
+ //#endregion
428
+ //#region src/gatewayWarning.ts
429
+ const POSTHOG_AI_GATEWAY_HOSTS = [
430
+ "gateway.posthog.com",
431
+ "gateway.us.posthog.com",
432
+ "gateway.eu.posthog.com",
433
+ "ai-gateway.us.posthog.com",
434
+ "ai-gateway.eu.posthog.com"
435
+ ];
436
+ const GATEWAY_DOCS_URL = "https://posthog.com/docs/ai-observability";
437
+ const extractHost = (baseURL) => {
438
+ try {
439
+ const hasScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(baseURL);
440
+ return new URL(hasScheme ? baseURL : `https://${baseURL}`).hostname.toLowerCase();
441
+ } catch {
442
+ return;
443
+ }
513
444
  };
514
- const isPostHogAiGatewayUrl = baseURL => {
515
- if (!baseURL) {
516
- return false;
517
- }
518
- const host = extractHost(baseURL);
519
- return host !== undefined && POSTHOG_AI_GATEWAY_HOSTS.includes(host);
445
+ const isPostHogAiGatewayUrl = (baseURL) => {
446
+ if (!baseURL) return false;
447
+ const host = extractHost(baseURL);
448
+ return host !== void 0 && POSTHOG_AI_GATEWAY_HOSTS.includes(host);
520
449
  };
521
-
522
- // Warns on every gateway call by design: the misconfiguration is impossible to
523
- // miss that way, and a doubled bill is worse than noisy logs.
524
- const warnIfPostHogAiGateway = baseURL => {
525
- if (!isPostHogAiGatewayUrl(baseURL)) {
526
- return;
527
- }
528
- console.warn('[PostHog] The PostHog AI wrapper is pointed at the PostHog AI Gateway. ' + 'Both capture $ai_generation, so every call is double-counted and double-billed. ' + `Use one or the other — see ${GATEWAY_DOCS_URL}.`);
450
+ const warnIfPostHogAiGateway = (baseURL) => {
451
+ if (!isPostHogAiGatewayUrl(baseURL)) return;
452
+ console.warn(`[PostHog] The PostHog AI wrapper is pointed at the PostHog AI Gateway. Both capture \$ai_generation, so every call is double-counted and double-billed. Use one or the other see ${GATEWAY_DOCS_URL}.`);
529
453
  };
530
-
454
+ //#endregion
455
+ //#region src/captureAiGeneration.ts
531
456
  /**
532
- * Options for `captureAiGeneration`. Mirrors the `$ai_generation` event shape
533
- * directly so that any caller — first-party SDK wrappers and external code
534
- * alike produces an identical event.
535
- */
536
-
537
- /**
538
- * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
539
- *
540
- * This is the canonical primitive that every `@posthog/ai` wrapper
541
- * (`withTracing`, `OpenAI`, `Anthropic`, `GoogleGenAI`, …) funnels through, so
542
- * external code can use it directly to instrument LLM calls made through
543
- * arbitrary clients (Cloudflare Workers AI, custom HTTP, etc.) and get the
544
- * same events the SDK wrappers produce.
545
- *
546
- * When `error` is set, the event is captured as an error. If the error is an
547
- * object, it is mutated in place to set `__posthog_previously_captured_error`
548
- * so callers can re-throw the original error reference safely.
549
- */
457
+ * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
458
+ *
459
+ * This is the canonical primitive that every `@posthog/ai` wrapper
460
+ * (`withTracing`, `OpenAI`, `Anthropic`, `GoogleGenAI`, …) funnels through, so
461
+ * external code can use it directly to instrument LLM calls made through
462
+ * arbitrary clients (Cloudflare Workers AI, custom HTTP, etc.) and get the
463
+ * same events the SDK wrappers produce.
464
+ *
465
+ * When `error` is set, the event is captured as an error. If the error is an
466
+ * object, it is mutated in place to set `__posthog_previously_captured_error`
467
+ * so callers can re-throw the original error reference safely.
468
+ */
550
469
  const captureAiGeneration = async (client, options) => {
551
- try {
552
- if (!client.capture) {
553
- return;
554
- }
555
- warnIfPostHogAiGateway(options.baseURL);
556
- const traceId = options.traceId ?? v4();
557
- const eventType = options.eventType ?? AIEvent.Generation;
558
- const privacyMode = options.privacyMode ?? false;
559
- const usage = options.usage ?? {};
560
-
561
- // Check privacy before reading or traversing input/output. Besides avoiding
562
- // needless work, this ensures hostile getters/proxies cannot observe a value
563
- // that the caller explicitly requested us to redact.
564
- const shouldRedact = withPrivacyMode(client, privacyMode, false) === null;
565
- const safeInput = shouldRedact ? null : toJsonSafeValue(options.input);
566
- const safeOutput = shouldRedact ? null : toJsonSafeValue(options.output);
567
- let httpStatus = options.httpStatus;
568
- let errorData = {};
569
- if (options.error) {
570
- if (httpStatus === undefined) {
571
- if (typeof options.error === 'object' && 'status' in options.error && typeof options.error.status === 'number') {
572
- httpStatus = options.error.status;
573
- } else if (typeof options.error === 'object' && 'statusCode' in options.error && typeof options.error.statusCode === 'number') {
574
- httpStatus = options.error.statusCode;
575
- } else {
576
- httpStatus = 500;
577
- }
578
- }
579
- let exceptionId;
580
- if (client.options?.enableExceptionAutocapture) {
581
- exceptionId = uuidv7();
582
- client.captureException(options.error, undefined, {
583
- $ai_trace_id: traceId
584
- }, exceptionId);
585
- if (typeof options.error === 'object') {
586
- ;
587
- options.error.__posthog_previously_captured_error = true;
588
- }
589
- }
590
- errorData = {
591
- $ai_is_error: true,
592
- $ai_error: stringifyError(options.error),
593
- $exception_event_id: exceptionId
594
- };
595
- }
596
- httpStatus = httpStatus ?? 200;
597
-
598
- // A configured price applies only to a count the provider reported, so a call with no
599
- // reported usage sends no cost instead of asserting $0. $ai_total_cost_usd sums the sides
600
- // that were priced, which makes it the cost of the known side alone when the other side
601
- // went unreported: a lower bound on the true total, not an assertion of it.
602
- const costOverrideData = {};
603
- if (options.costOverride) {
604
- if (usage.inputTokens !== undefined) {
605
- costOverrideData.$ai_input_cost_usd = (options.costOverride.inputCost ?? 0) * usage.inputTokens;
606
- }
607
- if (usage.outputTokens !== undefined) {
608
- costOverrideData.$ai_output_cost_usd = (options.costOverride.outputCost ?? 0) * usage.outputTokens;
609
- }
610
- if (Object.keys(costOverrideData).length > 0) {
611
- costOverrideData.$ai_total_cost_usd = (costOverrideData.$ai_input_cost_usd ?? 0) + (costOverrideData.$ai_output_cost_usd ?? 0);
612
- }
613
- }
614
-
615
- // The caller's own token counts override the SDK-derived ones further down, via the
616
- // `options.properties` spread.
617
- const tokensOverridden = hasTokenOverrides(options.properties);
618
- const additionalTokenValues = {
619
- ...(usage.reasoningTokens ? {
620
- $ai_reasoning_tokens: usage.reasoningTokens
621
- } : {}),
622
- ...(usage.cacheReadInputTokens ? {
623
- $ai_cache_read_input_tokens: usage.cacheReadInputTokens
624
- } : {}),
625
- ...(usage.cacheCreationInputTokens ? {
626
- $ai_cache_creation_input_tokens: usage.cacheCreationInputTokens
627
- } : {}),
628
- // Checked against undefined rather than truthiness, because false is the meaningful
629
- // value here and a truthiness guard would drop it.
630
- //
631
- // Dropped entirely when the caller overrides the token counts: the flag describes how
632
- // the SDK-derived counts relate to each other, so against passthrough counts it can be
633
- // wrong in the expensive direction. Declaring inclusive over counts that are actually
634
- // exclusive makes ingestion subtract the cache pool that was never in the input. A
635
- // caller who knows their own accounting model can still pass
636
- // `$ai_cache_reporting_exclusive` themselves, and that value wins.
637
- ...(usage.cacheReportingExclusive !== undefined && !tokensOverridden ? {
638
- $ai_cache_reporting_exclusive: usage.cacheReportingExclusive
639
- } : {}),
640
- ...(usage.webSearchCount ? {
641
- $ai_web_search_count: usage.webSearchCount
642
- } : {}),
643
- ...(usage.rawUsage ? {
644
- $ai_usage: usage.rawUsage
645
- } : {})
646
- };
647
- const properties = {
648
- $ai_lib: 'posthog-ai',
649
- $ai_lib_version: version,
650
- $ai_provider: options.providerOverride ?? options.provider,
651
- $ai_model: options.modelOverride ?? options.model,
652
- $ai_model_parameters: options.modelParameters ?? {},
653
- $ai_input: safeInput,
654
- $ai_output_choices: safeOutput,
655
- $ai_http_status: httpStatus,
656
- ...(usage.inputTokens !== undefined ? {
657
- $ai_input_tokens: usage.inputTokens
658
- } : {}),
659
- ...(usage.outputTokens !== undefined ? {
660
- $ai_output_tokens: usage.outputTokens
661
- } : {}),
662
- ...additionalTokenValues,
663
- ...(options.latency !== undefined ? {
664
- $ai_latency: options.latency
665
- } : {}),
666
- ...(options.timeToFirstToken !== undefined ? {
667
- $ai_time_to_first_token: options.timeToFirstToken
668
- } : {}),
669
- $ai_trace_id: traceId,
670
- $ai_base_url: options.baseURL ?? '',
671
- ...options.properties,
672
- $ai_tokens_source: getTokensSource(options.properties),
673
- ...(options.distinctId ? {} : {
674
- $process_person_profile: false
675
- }),
676
- ...(options.stopReason ? {
677
- $ai_stop_reason: options.stopReason
678
- } : {}),
679
- ...(options.tools ? {
680
- $ai_tools: options.tools
681
- } : {}),
682
- ...(options.completionId ? {
683
- $ai_completion_id: options.completionId
684
- } : {}),
685
- ...(options.providerMetadata && Object.keys(options.providerMetadata).length > 0 ? {
686
- $ai_provider_metadata: options.providerMetadata
687
- } : {}),
688
- ...errorData,
689
- ...costOverrideData
690
- };
691
- const event = {
692
- distinctId: options.distinctId ?? traceId,
693
- event: eventType,
694
- properties,
695
- groups: options.groups
696
- };
697
- if (options.captureImmediate) {
698
- await captureAiEventImmediate(client, event);
699
- } else {
700
- captureAiEvent(client, event);
701
- }
702
- } catch (error) {
703
- // Telemetry failures must never affect the instrumented provider call.
704
- console.warn('[PostHog AI] Failed to capture generation telemetry:', error);
705
- }
470
+ try {
471
+ if (!client.capture) return;
472
+ warnIfPostHogAiGateway(options.baseURL);
473
+ const traceId = options.traceId ?? v4();
474
+ const eventType = options.eventType ?? "$ai_generation";
475
+ const privacyMode = options.privacyMode ?? false;
476
+ const usage = options.usage ?? {};
477
+ const shouldRedact = withPrivacyMode(client, privacyMode, false) === null;
478
+ const safeInput = shouldRedact ? null : toJsonSafeValue(options.input);
479
+ const safeOutput = shouldRedact ? null : toJsonSafeValue(options.output);
480
+ let httpStatus = options.httpStatus;
481
+ let errorData = {};
482
+ if (options.error) {
483
+ if (httpStatus === void 0) {
484
+ if (typeof options.error === "object" && "status" in options.error && typeof options.error.status === "number") httpStatus = options.error.status;
485
+ else if (typeof options.error === "object" && "statusCode" in options.error && typeof options.error.statusCode === "number") httpStatus = options.error.statusCode;
486
+ else httpStatus = 500;
487
+ }
488
+ let exceptionId;
489
+ if (client.options?.enableExceptionAutocapture) {
490
+ exceptionId = uuidv7();
491
+ client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
492
+ if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
493
+ }
494
+ errorData = {
495
+ $ai_is_error: true,
496
+ $ai_error: stringifyError(options.error),
497
+ $exception_event_id: exceptionId
498
+ };
499
+ }
500
+ httpStatus = httpStatus ?? 200;
501
+ const costOverrideData = {};
502
+ if (options.costOverride) {
503
+ if (usage.inputTokens !== void 0) costOverrideData.$ai_input_cost_usd = (options.costOverride.inputCost ?? 0) * usage.inputTokens;
504
+ if (usage.outputTokens !== void 0) costOverrideData.$ai_output_cost_usd = (options.costOverride.outputCost ?? 0) * usage.outputTokens;
505
+ if (Object.keys(costOverrideData).length > 0) costOverrideData.$ai_total_cost_usd = (costOverrideData.$ai_input_cost_usd ?? 0) + (costOverrideData.$ai_output_cost_usd ?? 0);
506
+ }
507
+ const tokensOverridden = hasTokenOverrides(options.properties);
508
+ const additionalTokenValues = {
509
+ ...usage.reasoningTokens ? { $ai_reasoning_tokens: usage.reasoningTokens } : {},
510
+ ...usage.cacheReadInputTokens ? { $ai_cache_read_input_tokens: usage.cacheReadInputTokens } : {},
511
+ ...usage.cacheCreationInputTokens ? { $ai_cache_creation_input_tokens: usage.cacheCreationInputTokens } : {},
512
+ ...usage.cacheReportingExclusive !== void 0 && !tokensOverridden ? { $ai_cache_reporting_exclusive: usage.cacheReportingExclusive } : {},
513
+ ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
514
+ ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
515
+ };
516
+ const properties = {
517
+ $ai_lib: "posthog-ai",
518
+ $ai_lib_version: version,
519
+ $ai_provider: options.providerOverride ?? options.provider,
520
+ $ai_model: options.modelOverride ?? options.model,
521
+ $ai_model_parameters: options.modelParameters ?? {},
522
+ $ai_input: safeInput,
523
+ $ai_output_choices: safeOutput,
524
+ $ai_http_status: httpStatus,
525
+ ...usage.inputTokens !== void 0 ? { $ai_input_tokens: usage.inputTokens } : {},
526
+ ...usage.outputTokens !== void 0 ? { $ai_output_tokens: usage.outputTokens } : {},
527
+ ...additionalTokenValues,
528
+ ...options.latency !== void 0 ? { $ai_latency: options.latency } : {},
529
+ ...options.timeToFirstToken !== void 0 ? { $ai_time_to_first_token: options.timeToFirstToken } : {},
530
+ $ai_trace_id: traceId,
531
+ ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
532
+ ...options.properties,
533
+ $ai_tokens_source: getTokensSource(options.properties),
534
+ ...options.distinctId ? {} : { $process_person_profile: false },
535
+ ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
536
+ ...options.tools ? { $ai_tools: options.tools } : {},
537
+ ...options.completionId ? { $ai_completion_id: options.completionId } : {},
538
+ ...options.providerMetadata && Object.keys(options.providerMetadata).length > 0 ? { $ai_provider_metadata: options.providerMetadata } : {},
539
+ ...errorData,
540
+ ...costOverrideData
541
+ };
542
+ const event = {
543
+ distinctId: options.distinctId ?? traceId,
544
+ event: eventType,
545
+ properties,
546
+ groups: options.groups
547
+ };
548
+ if (options.captureImmediate) await captureAiEventImmediate(client, event);
549
+ else captureAiEvent(client, event);
550
+ } catch (error) {
551
+ try {
552
+ options.onError?.(error);
553
+ } catch {}
554
+ console.warn("[PostHog AI] Failed to capture generation telemetry:", error);
555
+ }
706
556
  };
707
-
708
- // Union types for dual version support
709
-
710
- // Type guards
557
+ //#endregion
558
+ //#region src/vercel/middleware.ts
711
559
  function isV3Model(model) {
712
- return model.specificationVersion === 'v3';
560
+ return model.specificationVersion === "v3";
713
561
  }
714
562
  function getSpecificationVersion(model) {
715
- if (typeof model === 'object' && model !== null && 'specificationVersion' in model) {
716
- return model.specificationVersion;
717
- }
718
- return undefined;
563
+ if (typeof model === "object" && model !== null && "specificationVersion" in model) return model.specificationVersion;
719
564
  }
720
-
721
- // Content types for the output array
722
-
723
565
  const redactFileData = (data, mediaType, client) => {
724
- if (data instanceof URL) {
725
- return isFullAiCaptureEnabled(client) ? data.toString() : redactBase64DataUrl(data.toString(), data.protocol === 'data:' ? mediaType : undefined);
726
- }
727
- if (isString(data)) {
728
- return isFullAiCaptureEnabled(client) ? data : redactBase64DataUrl(data, mediaType);
729
- }
730
- return undefined;
566
+ if (data instanceof URL) return isFullAiCaptureEnabled(client) ? data.toString() : redactBase64DataUrl(data.toString(), data.protocol === "data:" ? mediaType : void 0);
567
+ if (isString(data)) return isFullAiCaptureEnabled(client) ? data : redactBase64DataUrl(data, mediaType);
731
568
  };
732
- const mapVercelParams = params => {
733
- return {
734
- temperature: params.temperature,
735
- max_output_tokens: params.maxOutputTokens,
736
- top_p: params.topP,
737
- frequency_penalty: params.frequencyPenalty,
738
- presence_penalty: params.presencePenalty,
739
- stop: params.stopSequences,
740
- stream: params.stream
741
- };
569
+ const mapVercelParams = (params) => {
570
+ return {
571
+ temperature: params.temperature,
572
+ max_output_tokens: params.maxOutputTokens,
573
+ top_p: params.topP,
574
+ frequency_penalty: params.frequencyPenalty,
575
+ presence_penalty: params.presencePenalty,
576
+ stop: params.stopSequences,
577
+ stream: params.stream
578
+ };
742
579
  };
743
580
  const mapVercelPrompt = (messages, client) => {
744
- // Map and truncate individual content
745
- const inputs = messages.map(message => {
746
- let content;
747
-
748
- // Handle system role which has string content
749
- if (message.role === 'system') {
750
- content = [{
751
- type: 'text',
752
- text: truncate(toContentString(message.content), client)
753
- }];
754
- } else {
755
- // Handle other roles which have array content
756
- if (Array.isArray(message.content)) {
757
- content = message.content.map(c => {
758
- if (c.type === 'text') {
759
- return {
760
- type: 'text',
761
- text: truncate(c.text, client)
762
- };
763
- } else if (c.type === 'file') {
764
- // Redact base64 data URLs and raw base64 to prevent oversized events
765
- const fileData = redactFileData(c.data, c.mediaType, client) ?? 'raw files not supported';
766
- return {
767
- type: 'file',
768
- file: fileData,
769
- mediaType: c.mediaType
770
- };
771
- } else if (c.type === 'reasoning') {
772
- return {
773
- type: 'reasoning',
774
- text: truncate(c.text, client)
775
- };
776
- } else if (c.type === 'tool-call') {
777
- return {
778
- type: 'tool-call',
779
- toolCallId: c.toolCallId,
780
- toolName: c.toolName,
781
- input: c.input
782
- };
783
- } else if (c.type === 'tool-result') {
784
- return {
785
- type: 'tool-result',
786
- toolCallId: c.toolCallId,
787
- toolName: c.toolName,
788
- output: sanitizeVercel(c.output, client),
789
- isError: c.isError
790
- };
791
- }
792
- return {
793
- type: 'text',
794
- text: ''
795
- };
796
- });
797
- } else {
798
- // Fallback for non-array content
799
- content = [{
800
- type: 'text',
801
- text: truncate(toContentString(message.content), client)
802
- }];
803
- }
804
- }
805
- return {
806
- role: message.role,
807
- content
808
- };
809
- });
810
-
811
- // Full AI capture means no truncation of any kind; the aggregate trim below exists
812
- // only to keep the default-mode payload under MAX_OUTPUT_SIZE.
813
- if (isFullAiCaptureEnabled(client)) {
814
- return inputs;
815
- }
816
- try {
817
- // Trim the inputs array until its serialized JSON size fits within MAX_OUTPUT_SIZE.
818
- // Pre-compute each message's byte size once so we can shift by accumulated budget
819
- // in a single linear pass, instead of re-stringifying the whole array per iteration.
820
- const messageSizes = inputs.map(m => utf8ByteLength(JSON.stringify(m)));
821
- // Account for the surrounding `[` `]` plus a comma between each pair of elements.
822
- let totalBytes = 2 + Math.max(0, messageSizes.length - 1);
823
- for (const size of messageSizes) {
824
- totalBytes += size;
825
- }
826
- let removedCount = 0;
827
- while (totalBytes > MAX_OUTPUT_SIZE && removedCount < messageSizes.length) {
828
- totalBytes -= messageSizes[removedCount];
829
- // Each removed message past the first also drops the comma that joined it.
830
- if (removedCount < messageSizes.length - 1) {
831
- totalBytes -= 1;
832
- }
833
- removedCount++;
834
- }
835
- if (removedCount > 0) {
836
- inputs.splice(0, removedCount);
837
- // Add one placeholder to indicate how many were removed
838
- inputs.unshift({
839
- role: 'posthog',
840
- content: `[${removedCount} message${removedCount === 1 ? '' : 's'} removed due to size limit]`
841
- });
842
- }
843
- } catch (error) {
844
- console.error('Error stringifying inputs', error);
845
- return [{
846
- role: 'posthog',
847
- content: 'An error occurred while processing your request. Please try again.'
848
- }];
849
- }
850
- return inputs;
581
+ const inputs = messages.map((message) => {
582
+ let content;
583
+ if (message.role === "system") content = [{
584
+ type: "text",
585
+ text: truncate(toContentString(message.content), client)
586
+ }];
587
+ else if (Array.isArray(message.content)) content = message.content.map((c) => {
588
+ if (c.type === "text") return {
589
+ type: "text",
590
+ text: truncate(c.text, client)
591
+ };
592
+ else if (c.type === "file") return {
593
+ type: "file",
594
+ file: redactFileData(c.data, c.mediaType, client) ?? "raw files not supported",
595
+ mediaType: c.mediaType
596
+ };
597
+ else if (c.type === "reasoning") return {
598
+ type: "reasoning",
599
+ text: truncate(c.text, client)
600
+ };
601
+ else if (c.type === "tool-call") return {
602
+ type: "tool-call",
603
+ toolCallId: c.toolCallId,
604
+ toolName: c.toolName,
605
+ input: c.input
606
+ };
607
+ else if (c.type === "tool-result") return {
608
+ type: "tool-result",
609
+ toolCallId: c.toolCallId,
610
+ toolName: c.toolName,
611
+ output: sanitizeVercel(c.output, client),
612
+ isError: c.isError
613
+ };
614
+ return {
615
+ type: "text",
616
+ text: ""
617
+ };
618
+ });
619
+ else content = [{
620
+ type: "text",
621
+ text: truncate(toContentString(message.content), client)
622
+ }];
623
+ return {
624
+ role: message.role,
625
+ content
626
+ };
627
+ });
628
+ if (isFullAiCaptureEnabled(client)) return inputs;
629
+ try {
630
+ const messageSizes = inputs.map((m) => utf8ByteLength(JSON.stringify(m)));
631
+ let totalBytes = 2 + Math.max(0, messageSizes.length - 1);
632
+ for (const size of messageSizes) totalBytes += size;
633
+ let removedCount = 0;
634
+ while (totalBytes > 2e5 && removedCount < messageSizes.length) {
635
+ totalBytes -= messageSizes[removedCount];
636
+ if (removedCount < messageSizes.length - 1) totalBytes -= 1;
637
+ removedCount++;
638
+ }
639
+ if (removedCount > 0) {
640
+ inputs.splice(0, removedCount);
641
+ inputs.unshift({
642
+ role: "posthog",
643
+ content: `[${removedCount} message${removedCount === 1 ? "" : "s"} removed due to size limit]`
644
+ });
645
+ }
646
+ } catch (error) {
647
+ console.error("Error stringifying inputs", error);
648
+ return [{
649
+ role: "posthog",
650
+ content: "An error occurred while processing your request. Please try again."
651
+ }];
652
+ }
653
+ return inputs;
851
654
  };
852
655
  const mapVercelOutput = (result, client) => {
853
- const content = result.map(item => {
854
- if (item.type === 'text') {
855
- return {
856
- type: 'text',
857
- text: truncate(item.text, client)
858
- };
859
- }
860
- if (item.type === 'tool-call') {
861
- const toolCall = item;
862
- const rawArgs = toolCall.input ?? toolCall.args ?? toolCall.arguments ?? {};
863
- return {
864
- type: 'tool-call',
865
- id: item.toolCallId,
866
- function: {
867
- name: item.toolName,
868
- arguments: typeof rawArgs === 'string' ? rawArgs : JSON.stringify(rawArgs)
869
- }
870
- };
871
- }
872
- if (item.type === 'reasoning') {
873
- return {
874
- type: 'reasoning',
875
- text: truncate(item.text, client)
876
- };
877
- }
878
- if (item.type === 'file') {
879
- // Handle files similar to input mapping - avoid large base64 data
880
- let fileData = redactFileData(item.data, item.mediaType, client) ?? `[binary ${item.mediaType} file]`;
881
-
882
- // Skipped under full AI capture: media stays untouched, so no placeholder swap either.
883
- if (!isFullAiCaptureEnabled(client) && typeof item.data === 'string' && fileData === item.data && item.data.length > 1000) {
884
- fileData = `[${item.mediaType} file - ${item.data.length} bytes]`;
885
- }
886
- return {
887
- type: 'file',
888
- name: 'generated_file',
889
- mediaType: item.mediaType,
890
- data: fileData
891
- };
892
- }
893
- if (item.type === 'source') {
894
- return {
895
- type: 'source',
896
- sourceType: item.sourceType,
897
- id: item.id,
898
- url: item.url || '',
899
- title: item.title || ''
900
- };
901
- }
902
- // Fallback for unknown types - try to extract text if possible
903
- return {
904
- type: 'text',
905
- text: truncate(JSON.stringify(item), client)
906
- };
907
- });
908
- if (content.length > 0) {
909
- return [{
910
- role: 'assistant',
911
- content: content.length === 1 && content[0].type === 'text' ? content[0].text : content
912
- }];
913
- }
914
- // otherwise stringify and truncate
915
- try {
916
- const jsonOutput = JSON.stringify(result);
917
- return [{
918
- content: truncate(jsonOutput, client),
919
- role: 'assistant'
920
- }];
921
- } catch {
922
- console.error('Error stringifying output');
923
- return [];
924
- }
656
+ const content = result.map((item) => {
657
+ if (item.type === "text") return {
658
+ type: "text",
659
+ text: truncate(item.text, client)
660
+ };
661
+ if (item.type === "tool-call") {
662
+ const toolCall = item;
663
+ const rawArgs = toolCall.input ?? toolCall.args ?? toolCall.arguments ?? {};
664
+ return {
665
+ type: "tool-call",
666
+ id: item.toolCallId,
667
+ function: {
668
+ name: item.toolName,
669
+ arguments: typeof rawArgs === "string" ? rawArgs : JSON.stringify(rawArgs)
670
+ }
671
+ };
672
+ }
673
+ if (item.type === "reasoning") return {
674
+ type: "reasoning",
675
+ text: truncate(item.text, client)
676
+ };
677
+ if (item.type === "file") {
678
+ let fileData = redactFileData(item.data, item.mediaType, client) ?? `[binary ${item.mediaType} file]`;
679
+ if (!isFullAiCaptureEnabled(client) && typeof item.data === "string" && fileData === item.data && item.data.length > 1e3) fileData = `[${item.mediaType} file - ${item.data.length} bytes]`;
680
+ return {
681
+ type: "file",
682
+ name: "generated_file",
683
+ mediaType: item.mediaType,
684
+ data: fileData
685
+ };
686
+ }
687
+ if (item.type === "source") return {
688
+ type: "source",
689
+ sourceType: item.sourceType,
690
+ id: item.id,
691
+ url: item.url || "",
692
+ title: item.title || ""
693
+ };
694
+ return {
695
+ type: "text",
696
+ text: truncate(JSON.stringify(item), client)
697
+ };
698
+ });
699
+ if (content.length > 0) return [{
700
+ role: "assistant",
701
+ content: content.length === 1 && content[0].type === "text" ? content[0].text : content
702
+ }];
703
+ try {
704
+ const jsonOutput = JSON.stringify(result);
705
+ return [{
706
+ content: truncate(jsonOutput, client),
707
+ role: "assistant"
708
+ }];
709
+ } catch {
710
+ console.error("Error stringifying output");
711
+ return [];
712
+ }
925
713
  };
926
- const extractProvider = model => {
927
- const provider = model.provider.toLowerCase();
928
- const providerName = provider.split('.')[0];
929
- return providerName;
714
+ const extractProvider = (model) => {
715
+ return model.provider.toLowerCase().split(".")[0];
930
716
  };
931
-
932
717
  /**
933
- * Recover the base URL so gateway calls self-identify via `$ai_base_url` (dedup
934
- * keys on it). The spec exposes none, so we read the off-spec provider `config`:
935
- * `@ai-sdk/anthropic` keeps a `config.baseURL` string; `@ai-sdk/openai`/
936
- * `openai-compatible` bury it in a `config.url({ path })` closure. Unknown shapes
937
- * degrade to `''` — those providers (or a custom `fetch`) stay invisible to dedup.
938
- */
939
- const extractBaseURL = model => {
940
- try {
941
- const config = model.config;
942
- if (!isObject(config)) {
943
- return '';
944
- }
945
- if (isString(config.baseURL)) {
946
- return config.baseURL;
947
- }
948
- const urlFn = config.url;
949
- if (typeof urlFn === 'function') {
950
- const url = urlFn({
951
- path: '',
952
- modelId: model.modelId
953
- });
954
- return isString(url) ? url : '';
955
- }
956
- } catch {
957
- // Unknown config shape or url() threw.
958
- }
959
- return '';
718
+ * Recover the base URL so gateway calls self-identify via `$ai_base_url` (dedup
719
+ * keys on it). The spec exposes none, so we read the off-spec provider `config`:
720
+ * `@ai-sdk/anthropic` keeps a `config.baseURL` string; `@ai-sdk/openai`/
721
+ * `openai-compatible` bury it in a `config.url({ path })` closure. Unknown shapes
722
+ * degrade to `''` — those providers (or a custom `fetch`) stay invisible to dedup.
723
+ */
724
+ const extractBaseURL = (model) => {
725
+ try {
726
+ const config = model.config;
727
+ if (!isObject(config)) return "";
728
+ if (isString(config.baseURL)) return config.baseURL;
729
+ const urlFn = config.url;
730
+ if (typeof urlFn === "function") {
731
+ const url = urlFn({
732
+ path: "",
733
+ modelId: model.modelId
734
+ });
735
+ return isString(url) ? url : "";
736
+ }
737
+ } catch {}
738
+ return "";
960
739
  };
961
-
962
- // Extract web search count from provider metadata (works for both V2 and V3)
963
740
  const extractWebSearchCount = (providerMetadata, usage) => {
964
- // Try Anthropic-specific extraction
965
- if (providerMetadata && typeof providerMetadata === 'object' && 'anthropic' in providerMetadata && providerMetadata.anthropic && typeof providerMetadata.anthropic === 'object' && 'server_tool_use' in providerMetadata.anthropic) {
966
- const serverToolUse = providerMetadata.anthropic.server_tool_use;
967
- if (serverToolUse && typeof serverToolUse === 'object' && 'web_search_requests' in serverToolUse && typeof serverToolUse.web_search_requests === 'number') {
968
- return serverToolUse.web_search_requests;
969
- }
970
- }
971
-
972
- // Fall back to generic calculation
973
- return calculateWebSearchCount({
974
- usage,
975
- providerMetadata
976
- });
741
+ if (providerMetadata && typeof providerMetadata === "object" && "anthropic" in providerMetadata && providerMetadata.anthropic && typeof providerMetadata.anthropic === "object" && "server_tool_use" in providerMetadata.anthropic) {
742
+ const serverToolUse = providerMetadata.anthropic.server_tool_use;
743
+ if (serverToolUse && typeof serverToolUse === "object" && "web_search_requests" in serverToolUse && typeof serverToolUse.web_search_requests === "number") return serverToolUse.web_search_requests;
744
+ }
745
+ return calculateWebSearchCount({
746
+ usage,
747
+ providerMetadata
748
+ });
977
749
  };
978
-
979
- // Helper to extract numeric token value from V2 (number) or V3 (object with .total) usage formats
980
- const extractTokenCount = value => {
981
- if (typeof value === 'number') {
982
- return value;
983
- }
984
- if (value && typeof value === 'object' && 'total' in value && typeof value.total === 'number') {
985
- return value.total;
986
- }
987
- return undefined;
750
+ const extractTokenCount = (value) => {
751
+ if (typeof value === "number") return value;
752
+ if (value && typeof value === "object" && "total" in value && typeof value.total === "number") return value.total;
988
753
  };
989
754
  const extractUsageToken = (usage, topLevelKey, nestedObjectKey, nestedValueKey) => {
990
- if (topLevelKey in usage) {
991
- return usage[topLevelKey];
992
- }
993
- const nestedTokens = usage[nestedObjectKey];
994
- if (nestedTokens && typeof nestedTokens === 'object' && nestedValueKey in nestedTokens) {
995
- return nestedTokens[nestedValueKey];
996
- }
997
- return undefined;
755
+ if (topLevelKey in usage) return usage[topLevelKey];
756
+ const nestedTokens = usage[nestedObjectKey];
757
+ if (nestedTokens && typeof nestedTokens === "object" && nestedValueKey in nestedTokens) return nestedTokens[nestedValueKey];
998
758
  };
999
-
1000
- // Helper to extract reasoning tokens from V2 (usage.reasoningTokens) or V3 (usage.outputTokens.reasoning)
1001
- const extractReasoningTokens = usage => extractUsageToken(usage, 'reasoningTokens', 'outputTokens', 'reasoning');
1002
-
1003
- // Helper to extract cached input tokens from V2 (usage.cachedInputTokens) or V3 (usage.inputTokens.cacheRead)
1004
- const extractCacheReadTokens = usage => extractUsageToken(usage, 'cachedInputTokens', 'inputTokens', 'cacheRead');
1005
-
1006
- // Helper to extract cache write tokens from V3 (usage.inputTokens.cacheWrite). Providers like
1007
- // Amazon Bedrock populate this standardized field instead of providerMetadata.anthropic.
1008
- const extractCacheWriteTokens = usage => {
1009
- if ('inputTokens' in usage && usage.inputTokens && typeof usage.inputTokens === 'object' && 'cacheWrite' in usage.inputTokens) {
1010
- return usage.inputTokens.cacheWrite;
1011
- }
1012
- return undefined;
759
+ const extractReasoningTokens = (usage) => extractUsageToken(usage, "reasoningTokens", "outputTokens", "reasoning");
760
+ const extractCacheReadTokens = (usage) => extractUsageToken(usage, "cachedInputTokens", "inputTokens", "cacheRead");
761
+ const extractCacheWriteTokens = (usage) => {
762
+ if ("inputTokens" in usage && usage.inputTokens && typeof usage.inputTokens === "object" && "cacheWrite" in usage.inputTokens) return usage.inputTokens.cacheWrite;
1013
763
  };
1014
-
1015
- // Extract additional token values from provider metadata, with a V3 standardized fallback
1016
- // (e.g. Amazon Bedrock exposes cache write tokens via usage.inputTokens.cacheWrite rather
1017
- // than providerMetadata.anthropic.cacheCreationInputTokens). A cacheWrite of 0 is treated
1018
- // as absent so we preserve the pre-fallback event shape on providers that simply omit the
1019
- // field — consumers downstream saw `$ai_cache_creation_input_tokens` missing, not 0.
1020
764
  const extractAdditionalTokenValues = (providerMetadata, usage) => {
1021
- if (providerMetadata && typeof providerMetadata === 'object' && 'anthropic' in providerMetadata && providerMetadata.anthropic && typeof providerMetadata.anthropic === 'object' && 'cacheCreationInputTokens' in providerMetadata.anthropic) {
1022
- return {
1023
- cacheCreationInputTokens: providerMetadata.anthropic.cacheCreationInputTokens
1024
- };
1025
- }
1026
- if (usage && typeof usage === 'object') {
1027
- const cacheWrite = extractCacheWriteTokens(usage);
1028
- if (typeof cacheWrite === 'number' && cacheWrite > 0) {
1029
- return {
1030
- cacheCreationInputTokens: cacheWrite
1031
- };
1032
- }
1033
- }
1034
- return {};
765
+ if (providerMetadata && typeof providerMetadata === "object" && "anthropic" in providerMetadata && providerMetadata.anthropic && typeof providerMetadata.anthropic === "object" && "cacheCreationInputTokens" in providerMetadata.anthropic) return { cacheCreationInputTokens: providerMetadata.anthropic.cacheCreationInputTokens };
766
+ if (usage && typeof usage === "object") {
767
+ const cacheWrite = extractCacheWriteTokens(usage);
768
+ if (typeof cacheWrite === "number" && cacheWrite > 0) return { cacheCreationInputTokens: cacheWrite };
769
+ }
770
+ return {};
1035
771
  };
1036
-
1037
- // Detects Anthropic Claude regardless of host (direct Anthropic, Amazon Bedrock, Google Vertex, etc.).
1038
- // The server applies exclusive cache token accounting based on the model name, so any Claude model
1039
- // needs its V3 input tokens adjusted to exclude cache tokens — not just those routed through a
1040
- // provider whose name contains "anthropic". Accepts the resolved modelId string (not the raw model)
1041
- // so it sees the same id the server does after posthogModelOverride / response.modelId fallbacks.
1042
772
  const isAnthropicClaudeModel = (modelId, provider) => {
1043
- if (provider.toLowerCase().includes('anthropic')) {
1044
- return true;
1045
- }
1046
- return /claude|anthropic/i.test(modelId);
773
+ if (provider.toLowerCase().includes("anthropic")) return true;
774
+ return /claude|anthropic/i.test(modelId);
1047
775
  };
1048
-
1049
- // For Anthropic providers in V3, inputTokens.total is the sum of all tokens (uncached + cache read + cache write).
1050
- // Our cost calculation expects inputTokens to be only the uncached portion for Anthropic.
1051
- // This helper subtracts cache tokens from inputTokens for Anthropic V3 models.
1052
776
  const adjustAnthropicV3CacheTokens = (model, modelId, provider, usage) => {
1053
- if (isV3Model(model) && isAnthropicClaudeModel(modelId, provider)) {
1054
- const cacheReadTokens = usage.cacheReadInputTokens || 0;
1055
- const cacheWriteTokens = usage.cacheCreationInputTokens || 0;
1056
- const cacheTokens = cacheReadTokens + cacheWriteTokens;
1057
- if (usage.inputTokens && cacheTokens > 0) {
1058
- usage.inputTokens = Math.max(usage.inputTokens - cacheTokens, 0);
1059
- }
1060
- }
777
+ if (isV3Model(model) && isAnthropicClaudeModel(modelId, provider)) {
778
+ const cacheTokens = (usage.cacheReadInputTokens || 0) + (usage.cacheCreationInputTokens || 0);
779
+ if (usage.inputTokens && cacheTokens > 0) usage.inputTokens = Math.max(usage.inputTokens - cacheTokens, 0);
780
+ }
1061
781
  };
1062
-
1063
782
  /**
1064
- * Wraps a Vercel AI SDK language model (V2 or V3) with PostHog tracing.
1065
- * Automatically detects the model version and applies appropriate instrumentation.
1066
- */
783
+ * Wraps a Vercel AI SDK language model (V2 or V3) with PostHog tracing.
784
+ * Automatically detects the model version and applies appropriate instrumentation.
785
+ */
1067
786
  const wrapVercelLanguageModel = (model, phClient, options) => {
1068
- const specificationVersion = getSpecificationVersion(model);
1069
- if (specificationVersion !== 'v2' && specificationVersion !== 'v3') {
1070
- throw new Error(`[PostHog AI] withTracing supports Vercel AI SDK v5 and v6 models only. ` + `Use @ai-sdk/otel with @posthog/ai/otel for AI SDK v7 models.`);
1071
- }
1072
- const traceId = options.posthogTraceId ?? v4();
1073
- const mergedOptions = {
1074
- ...options,
1075
- posthogTraceId: traceId,
1076
- posthogDistinctId: options.posthogDistinctId,
1077
- posthogProperties: {
1078
- ...options.posthogProperties,
1079
- $ai_framework: 'vercel',
1080
- $ai_framework_version: model.specificationVersion === 'v3' ? '6' : '5'
1081
- }
1082
- };
1083
-
1084
- // Shared `captureAiGeneration` options for every call site in this wrapper.
1085
- const baseOptions = {
1086
- distinctId: mergedOptions.posthogDistinctId,
1087
- traceId,
1088
- properties: mergedOptions.posthogProperties,
1089
- groups: mergedOptions.posthogGroups,
1090
- privacyMode: mergedOptions.posthogPrivacyMode,
1091
- modelOverride: mergedOptions.posthogModelOverride,
1092
- providerOverride: mergedOptions.posthogProviderOverride,
1093
- costOverride: mergedOptions.posthogCostOverride,
1094
- captureImmediate: mergedOptions.posthogCaptureImmediate
1095
- };
1096
-
1097
- // Create wrapped model using Object.create to preserve the prototype chain
1098
- // This automatically inherits all properties (including getters) from the model
1099
- const wrappedModel = Object.create(model, {
1100
- doGenerate: {
1101
- value: async params => {
1102
- const startTime = Date.now();
1103
- const mergedParams = {
1104
- ...mergedOptions,
1105
- ...mapVercelParams(params)
1106
- };
1107
- const availableTools = extractAvailableToolCalls('vercel', params);
1108
- const baseURL = extractBaseURL(model);
1109
- try {
1110
- const result = await model.doGenerate(params);
1111
- const modelId = mergedOptions.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
1112
- const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
1113
- // result.content is undefined when the model returns only tool calls with no text output
1114
- const content = mapVercelOutput(result.content ?? [], phClient);
1115
- const latency = (Date.now() - startTime) / 1000;
1116
- const providerMetadata = result.providerMetadata;
1117
- const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, result.usage);
1118
- const webSearchCount = extractWebSearchCount(providerMetadata, result.usage);
1119
-
1120
- // V2 usage has simple numbers, V3 has objects with .total - normalize both
1121
- const usageObj = result.usage;
1122
-
1123
- // Extract raw response for providers that include detailed usage metadata
1124
- // For Gemini, candidatesTokensDetails is in result.response.body.usageMetadata
1125
- const rawUsageData = {
1126
- usage: result.usage,
1127
- providerMetadata
1128
- };
1129
-
1130
- // Include response body usageMetadata if it contains detailed token breakdown (e.g., candidatesTokensDetails)
1131
- if (result.response && typeof result.response === 'object') {
1132
- const responseBody = result.response.body;
1133
- if (responseBody && typeof responseBody === 'object' && 'usageMetadata' in responseBody) {
1134
- rawUsageData.rawResponse = {
1135
- usageMetadata: responseBody.usageMetadata
1136
- };
1137
- }
1138
- }
1139
- const usage = {
1140
- inputTokens: extractTokenCount(result.usage.inputTokens),
1141
- outputTokens: extractTokenCount(result.usage.outputTokens),
1142
- reasoningTokens: extractReasoningTokens(usageObj),
1143
- cacheReadInputTokens: extractCacheReadTokens(usageObj),
1144
- webSearchCount,
1145
- ...additionalTokenValues,
1146
- rawUsage: rawUsageData
1147
- };
1148
- adjustAnthropicV3CacheTokens(model, modelId, provider, usage);
1149
-
1150
- // Extract finish reason - V2 returns a string, V3 returns an object with .unified
1151
- const rawFinishReason = result.finishReason;
1152
- const finishReasonStr = typeof rawFinishReason === 'string' ? rawFinishReason : rawFinishReason && typeof rawFinishReason === 'object' && 'unified' in rawFinishReason ? String(rawFinishReason.unified) : undefined;
1153
- await captureAiGeneration(phClient, {
1154
- ...baseOptions,
1155
- model: modelId,
1156
- provider: provider,
1157
- input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt, phClient),
1158
- output: content,
1159
- latency,
1160
- baseURL,
1161
- modelParameters: getModelParams(mergedParams),
1162
- httpStatus: 200,
1163
- usage,
1164
- stopReason: finishReasonStr,
1165
- tools: availableTools
1166
- });
1167
- return result;
1168
- } catch (error) {
1169
- const modelId = model.modelId;
1170
- await captureAiGeneration(phClient, {
1171
- ...baseOptions,
1172
- model: modelId,
1173
- provider: model.provider,
1174
- input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt, phClient),
1175
- output: [],
1176
- latency: (Date.now() - startTime) / 1000,
1177
- baseURL,
1178
- modelParameters: getModelParams(mergedParams),
1179
- usage: {},
1180
- error: error,
1181
- tools: availableTools
1182
- });
1183
- throw error;
1184
- }
1185
- },
1186
- writable: true,
1187
- configurable: true,
1188
- enumerable: false
1189
- },
1190
- doStream: {
1191
- value: async params => {
1192
- const startTime = Date.now();
1193
- let firstTokenTime;
1194
- let generatedText = '';
1195
- let reasoningText = '';
1196
- let stopReason;
1197
- let usage = {};
1198
- let providerMetadata = undefined;
1199
- const mergedParams = {
1200
- ...mergedOptions,
1201
- ...mapVercelParams(params)
1202
- };
1203
- const modelId = mergedOptions.posthogModelOverride ?? model.modelId;
1204
- const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
1205
- const availableTools = extractAvailableToolCalls('vercel', params);
1206
- const baseURL = extractBaseURL(model);
1207
-
1208
- // Map to track in-progress tool calls
1209
- const toolCallsInProgress = new Map();
1210
- const captureStreamGeneration = async captureOptions => {
1211
- try {
1212
- await captureAiGeneration(phClient, captureOptions);
1213
- } catch (error) {
1214
- // Telemetry must never change the provider stream's behavior.
1215
- console.warn('[PostHog AI] Failed to capture Vercel stream telemetry:', error);
1216
- }
1217
- };
1218
- try {
1219
- const {
1220
- stream,
1221
- ...rest
1222
- } = await model.doStream(params);
1223
- const reader = stream.getReader();
1224
- let inBandError;
1225
- let hasInBandError = false;
1226
- let finalizationPromise;
1227
- const observeChunk = chunk => {
1228
- // Handle streaming patterns - compatible with both V2 and V3
1229
- if (chunk.type === 'text-delta') {
1230
- if (firstTokenTime === undefined) {
1231
- firstTokenTime = Date.now();
1232
- }
1233
- generatedText += chunk.delta;
1234
- }
1235
- if (chunk.type === 'reasoning-delta') {
1236
- if (firstTokenTime === undefined) {
1237
- firstTokenTime = Date.now();
1238
- }
1239
- reasoningText += chunk.delta;
1240
- }
1241
-
1242
- // Handle tool call chunks
1243
- if (chunk.type === 'tool-input-start') {
1244
- if (firstTokenTime === undefined) {
1245
- firstTokenTime = Date.now();
1246
- }
1247
- toolCallsInProgress.set(chunk.id, {
1248
- toolCallId: chunk.id,
1249
- toolName: chunk.toolName,
1250
- input: ''
1251
- });
1252
- }
1253
- if (chunk.type === 'tool-input-delta') {
1254
- const toolCall = toolCallsInProgress.get(chunk.id);
1255
- if (toolCall) {
1256
- toolCall.input += chunk.delta;
1257
- }
1258
- }
1259
- if (chunk.type === 'tool-call') {
1260
- if (firstTokenTime === undefined) {
1261
- firstTokenTime = Date.now();
1262
- }
1263
- toolCallsInProgress.set(chunk.toolCallId, {
1264
- toolCallId: chunk.toolCallId,
1265
- toolName: chunk.toolName,
1266
- input: chunk.input
1267
- });
1268
- }
1269
- if (chunk.type === 'error') {
1270
- hasInBandError = true;
1271
- inBandError = chunk.error;
1272
- }
1273
- if (chunk.type === 'finish') {
1274
- providerMetadata = chunk.providerMetadata;
1275
- const chunkUsage = chunk.usage || {};
1276
- const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, chunkUsage);
1277
- usage = {
1278
- inputTokens: extractTokenCount(chunk.usage?.inputTokens),
1279
- outputTokens: extractTokenCount(chunk.usage?.outputTokens),
1280
- reasoningTokens: extractReasoningTokens(chunkUsage),
1281
- cacheReadInputTokens: extractCacheReadTokens(chunkUsage),
1282
- ...additionalTokenValues
1283
- };
1284
-
1285
- // Extract finish reason - V2 returns a string, V3 returns an object with .unified
1286
- const rawFinishReason = chunk.finishReason;
1287
- if (typeof rawFinishReason === 'string') {
1288
- stopReason = rawFinishReason;
1289
- } else if (rawFinishReason && typeof rawFinishReason === 'object' && 'unified' in rawFinishReason) {
1290
- stopReason = String(rawFinishReason.unified);
1291
- }
1292
- }
1293
- };
1294
- const finalize = (terminalError, isError = false) => {
1295
- if (finalizationPromise) {
1296
- return finalizationPromise;
1297
- }
1298
- finalizationPromise = (async () => {
1299
- const latency = (Date.now() - startTime) / 1000;
1300
- const timeToFirstToken = firstTokenTime !== undefined ? (firstTokenTime - startTime) / 1000 : undefined;
1301
- const content = [];
1302
- if (reasoningText) {
1303
- content.push({
1304
- type: 'reasoning',
1305
- text: truncate(reasoningText, phClient)
1306
- });
1307
- }
1308
- if (generatedText) {
1309
- content.push({
1310
- type: 'text',
1311
- text: truncate(generatedText, phClient)
1312
- });
1313
- }
1314
- for (const toolCall of toolCallsInProgress.values()) {
1315
- if (toolCall.toolName) {
1316
- content.push({
1317
- type: 'tool-call',
1318
- id: toolCall.toolCallId,
1319
- function: {
1320
- name: toolCall.toolName,
1321
- arguments: toolCall.input
1322
- }
1323
- });
1324
- }
1325
- }
1326
- const output = content.length > 0 ? [{
1327
- role: 'assistant',
1328
- content: content.length === 1 && content[0].type === 'text' ? content[0].text : content
1329
- }] : [];
1330
- const webSearchCount = extractWebSearchCount(providerMetadata, usage);
1331
- const finalUsage = {
1332
- ...usage,
1333
- webSearchCount,
1334
- rawUsage: {
1335
- usage,
1336
- providerMetadata
1337
- }
1338
- };
1339
- adjustAnthropicV3CacheTokens(model, modelId, provider, finalUsage);
1340
- const finishError = stopReason === 'error' ? new Error('Vercel AI SDK stream finished with an error') : undefined;
1341
- const error = isError ? terminalError ?? new Error('Vercel AI SDK stream failed') : hasInBandError ? inBandError ?? new Error('Vercel AI SDK stream emitted an error chunk') : finishError;
1342
- await captureStreamGeneration({
1343
- ...baseOptions,
1344
- model: modelId,
1345
- provider: provider,
1346
- input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt, phClient),
1347
- output,
1348
- latency,
1349
- timeToFirstToken,
1350
- baseURL,
1351
- modelParameters: getModelParams(mergedParams),
1352
- httpStatus: error ? undefined : 200,
1353
- usage: finalUsage,
1354
- stopReason,
1355
- error,
1356
- tools: availableTools
1357
- });
1358
- })().catch(error => {
1359
- // Building telemetry must not change the provider stream's behavior.
1360
- console.warn('[PostHog AI] Failed to capture Vercel stream telemetry:', error);
1361
- });
1362
- return finalizationPromise;
1363
- };
1364
- const instrumentedStream = new ReadableStream({
1365
- async pull(controller) {
1366
- let result;
1367
- try {
1368
- result = await reader.read();
1369
- } catch (error) {
1370
- void finalize(error, true);
1371
- controller.error(error);
1372
- return;
1373
- }
1374
- if (result.done) {
1375
- controller.close();
1376
- void finalize();
1377
- return;
1378
- }
1379
- try {
1380
- observeChunk(result.value);
1381
- } catch {
1382
- // Instrumentation must not alter or suppress provider chunks.
1383
- }
1384
- controller.enqueue(result.value);
1385
- },
1386
- cancel(reason) {
1387
- void finalize(reason ?? new Error('Vercel AI SDK stream was cancelled'), true);
1388
- return reader.cancel(reason);
1389
- }
1390
- }, {
1391
- highWaterMark: 0
1392
- });
1393
- return {
1394
- stream: instrumentedStream,
1395
- ...rest
1396
- };
1397
- } catch (error) {
1398
- await captureStreamGeneration({
1399
- ...baseOptions,
1400
- model: modelId,
1401
- provider: provider,
1402
- input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt, phClient),
1403
- output: [],
1404
- latency: (Date.now() - startTime) / 1000,
1405
- baseURL,
1406
- modelParameters: getModelParams(mergedParams),
1407
- usage: {},
1408
- error: error,
1409
- tools: availableTools
1410
- });
1411
- throw error;
1412
- }
1413
- },
1414
- writable: true,
1415
- configurable: true,
1416
- enumerable: false
1417
- }
1418
- });
1419
- return wrappedModel;
787
+ const specificationVersion = getSpecificationVersion(model);
788
+ if (specificationVersion !== "v2" && specificationVersion !== "v3") throw new Error("[PostHog AI] withTracing supports Vercel AI SDK v5 and v6 models only. Use @ai-sdk/otel with @posthog/ai/otel for AI SDK v7 models.");
789
+ const traceId = options.posthogTraceId ?? v4();
790
+ const mergedOptions = {
791
+ ...options,
792
+ posthogTraceId: traceId,
793
+ posthogDistinctId: options.posthogDistinctId,
794
+ posthogProperties: {
795
+ ...options.posthogProperties,
796
+ $ai_framework: "vercel",
797
+ $ai_framework_version: model.specificationVersion === "v3" ? "6" : "5"
798
+ }
799
+ };
800
+ const baseOptions = {
801
+ distinctId: mergedOptions.posthogDistinctId,
802
+ traceId,
803
+ properties: mergedOptions.posthogProperties,
804
+ groups: mergedOptions.posthogGroups,
805
+ privacyMode: mergedOptions.posthogPrivacyMode,
806
+ modelOverride: mergedOptions.posthogModelOverride,
807
+ providerOverride: mergedOptions.posthogProviderOverride,
808
+ costOverride: mergedOptions.posthogCostOverride,
809
+ captureImmediate: mergedOptions.posthogCaptureImmediate
810
+ };
811
+ return Object.create(model, {
812
+ doGenerate: {
813
+ value: async (params) => {
814
+ const startTime = Date.now();
815
+ const mergedParams = {
816
+ ...mergedOptions,
817
+ ...mapVercelParams(params)
818
+ };
819
+ const availableTools = extractAvailableToolCalls("vercel", params);
820
+ const baseURL = extractBaseURL(model);
821
+ try {
822
+ const result = await model.doGenerate(params);
823
+ const modelId = mergedOptions.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
824
+ const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
825
+ const content = mapVercelOutput(result.content ?? [], phClient);
826
+ const latency = (Date.now() - startTime) / 1e3;
827
+ const providerMetadata = result.providerMetadata;
828
+ const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, result.usage);
829
+ const webSearchCount = extractWebSearchCount(providerMetadata, result.usage);
830
+ const usageObj = result.usage;
831
+ const rawUsageData = {
832
+ usage: result.usage,
833
+ providerMetadata
834
+ };
835
+ if (result.response && typeof result.response === "object") {
836
+ const responseBody = result.response.body;
837
+ if (responseBody && typeof responseBody === "object" && "usageMetadata" in responseBody) rawUsageData.rawResponse = { usageMetadata: responseBody.usageMetadata };
838
+ }
839
+ const usage = {
840
+ inputTokens: extractTokenCount(result.usage.inputTokens),
841
+ outputTokens: extractTokenCount(result.usage.outputTokens),
842
+ reasoningTokens: extractReasoningTokens(usageObj),
843
+ cacheReadInputTokens: extractCacheReadTokens(usageObj),
844
+ webSearchCount,
845
+ ...additionalTokenValues,
846
+ rawUsage: rawUsageData
847
+ };
848
+ adjustAnthropicV3CacheTokens(model, modelId, provider, usage);
849
+ const rawFinishReason = result.finishReason;
850
+ const finishReasonStr = typeof rawFinishReason === "string" ? rawFinishReason : rawFinishReason && typeof rawFinishReason === "object" && "unified" in rawFinishReason ? String(rawFinishReason.unified) : void 0;
851
+ await captureAiGeneration(phClient, {
852
+ ...baseOptions,
853
+ model: modelId,
854
+ provider,
855
+ input: mergedOptions.posthogPrivacyMode ? "" : mapVercelPrompt(params.prompt, phClient),
856
+ output: content,
857
+ latency,
858
+ baseURL,
859
+ modelParameters: getModelParams(mergedParams),
860
+ httpStatus: 200,
861
+ usage,
862
+ stopReason: finishReasonStr,
863
+ tools: availableTools
864
+ });
865
+ return result;
866
+ } catch (error) {
867
+ const modelId = model.modelId;
868
+ await captureAiGeneration(phClient, {
869
+ ...baseOptions,
870
+ model: modelId,
871
+ provider: model.provider,
872
+ input: mergedOptions.posthogPrivacyMode ? "" : mapVercelPrompt(params.prompt, phClient),
873
+ output: [],
874
+ latency: (Date.now() - startTime) / 1e3,
875
+ baseURL,
876
+ modelParameters: getModelParams(mergedParams),
877
+ usage: {},
878
+ error,
879
+ tools: availableTools
880
+ });
881
+ throw error;
882
+ }
883
+ },
884
+ writable: true,
885
+ configurable: true,
886
+ enumerable: false
887
+ },
888
+ doStream: {
889
+ value: async (params) => {
890
+ const startTime = Date.now();
891
+ let firstTokenTime;
892
+ let generatedText = "";
893
+ let reasoningText = "";
894
+ let stopReason;
895
+ let usage = {};
896
+ let providerMetadata = void 0;
897
+ const mergedParams = {
898
+ ...mergedOptions,
899
+ ...mapVercelParams(params)
900
+ };
901
+ const modelId = mergedOptions.posthogModelOverride ?? model.modelId;
902
+ const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
903
+ const availableTools = extractAvailableToolCalls("vercel", params);
904
+ const baseURL = extractBaseURL(model);
905
+ const toolCallsInProgress = /* @__PURE__ */ new Map();
906
+ const captureStreamGeneration = async (captureOptions) => {
907
+ try {
908
+ await captureAiGeneration(phClient, captureOptions);
909
+ } catch (error) {
910
+ console.warn("[PostHog AI] Failed to capture Vercel stream telemetry:", error);
911
+ }
912
+ };
913
+ try {
914
+ const { stream, ...rest } = await model.doStream(params);
915
+ const reader = stream.getReader();
916
+ let inBandError;
917
+ let hasInBandError = false;
918
+ let finalizationPromise;
919
+ const observeChunk = (chunk) => {
920
+ if (chunk.type === "text-delta") {
921
+ if (firstTokenTime === void 0) firstTokenTime = Date.now();
922
+ generatedText += chunk.delta;
923
+ }
924
+ if (chunk.type === "reasoning-delta") {
925
+ if (firstTokenTime === void 0) firstTokenTime = Date.now();
926
+ reasoningText += chunk.delta;
927
+ }
928
+ if (chunk.type === "tool-input-start") {
929
+ if (firstTokenTime === void 0) firstTokenTime = Date.now();
930
+ toolCallsInProgress.set(chunk.id, {
931
+ toolCallId: chunk.id,
932
+ toolName: chunk.toolName,
933
+ input: ""
934
+ });
935
+ }
936
+ if (chunk.type === "tool-input-delta") {
937
+ const toolCall = toolCallsInProgress.get(chunk.id);
938
+ if (toolCall) toolCall.input += chunk.delta;
939
+ }
940
+ if (chunk.type === "tool-call") {
941
+ if (firstTokenTime === void 0) firstTokenTime = Date.now();
942
+ toolCallsInProgress.set(chunk.toolCallId, {
943
+ toolCallId: chunk.toolCallId,
944
+ toolName: chunk.toolName,
945
+ input: chunk.input
946
+ });
947
+ }
948
+ if (chunk.type === "error") {
949
+ hasInBandError = true;
950
+ inBandError = chunk.error;
951
+ }
952
+ if (chunk.type === "finish") {
953
+ providerMetadata = chunk.providerMetadata;
954
+ const chunkUsage = chunk.usage || {};
955
+ const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, chunkUsage);
956
+ usage = {
957
+ inputTokens: extractTokenCount(chunk.usage?.inputTokens),
958
+ outputTokens: extractTokenCount(chunk.usage?.outputTokens),
959
+ reasoningTokens: extractReasoningTokens(chunkUsage),
960
+ cacheReadInputTokens: extractCacheReadTokens(chunkUsage),
961
+ ...additionalTokenValues
962
+ };
963
+ const rawFinishReason = chunk.finishReason;
964
+ if (typeof rawFinishReason === "string") stopReason = rawFinishReason;
965
+ else if (rawFinishReason && typeof rawFinishReason === "object" && "unified" in rawFinishReason) stopReason = String(rawFinishReason.unified);
966
+ }
967
+ };
968
+ const finalize = (terminalError, isError = false) => {
969
+ if (finalizationPromise) return finalizationPromise;
970
+ finalizationPromise = (async () => {
971
+ const latency = (Date.now() - startTime) / 1e3;
972
+ const timeToFirstToken = firstTokenTime !== void 0 ? (firstTokenTime - startTime) / 1e3 : void 0;
973
+ const content = [];
974
+ if (reasoningText) content.push({
975
+ type: "reasoning",
976
+ text: truncate(reasoningText, phClient)
977
+ });
978
+ if (generatedText) content.push({
979
+ type: "text",
980
+ text: truncate(generatedText, phClient)
981
+ });
982
+ for (const toolCall of toolCallsInProgress.values()) if (toolCall.toolName) content.push({
983
+ type: "tool-call",
984
+ id: toolCall.toolCallId,
985
+ function: {
986
+ name: toolCall.toolName,
987
+ arguments: toolCall.input
988
+ }
989
+ });
990
+ const output = content.length > 0 ? [{
991
+ role: "assistant",
992
+ content: content.length === 1 && content[0].type === "text" ? content[0].text : content
993
+ }] : [];
994
+ const webSearchCount = extractWebSearchCount(providerMetadata, usage);
995
+ const finalUsage = {
996
+ ...usage,
997
+ webSearchCount,
998
+ rawUsage: {
999
+ usage,
1000
+ providerMetadata
1001
+ }
1002
+ };
1003
+ adjustAnthropicV3CacheTokens(model, modelId, provider, finalUsage);
1004
+ const error = isError ? terminalError ?? /* @__PURE__ */ new Error("Vercel AI SDK stream failed") : hasInBandError ? inBandError ?? /* @__PURE__ */ new Error("Vercel AI SDK stream emitted an error chunk") : stopReason === "error" ? /* @__PURE__ */ new Error("Vercel AI SDK stream finished with an error") : void 0;
1005
+ await captureStreamGeneration({
1006
+ ...baseOptions,
1007
+ model: modelId,
1008
+ provider,
1009
+ input: mergedOptions.posthogPrivacyMode ? "" : mapVercelPrompt(params.prompt, phClient),
1010
+ output,
1011
+ latency,
1012
+ timeToFirstToken,
1013
+ baseURL,
1014
+ modelParameters: getModelParams(mergedParams),
1015
+ httpStatus: error ? void 0 : 200,
1016
+ usage: finalUsage,
1017
+ stopReason,
1018
+ error,
1019
+ tools: availableTools
1020
+ });
1021
+ })().catch((error) => {
1022
+ console.warn("[PostHog AI] Failed to capture Vercel stream telemetry:", error);
1023
+ });
1024
+ return finalizationPromise;
1025
+ };
1026
+ return {
1027
+ stream: new ReadableStream({
1028
+ async pull(controller) {
1029
+ let result;
1030
+ try {
1031
+ result = await reader.read();
1032
+ } catch (error) {
1033
+ finalize(error, true);
1034
+ controller.error(error);
1035
+ return;
1036
+ }
1037
+ if (result.done) {
1038
+ controller.close();
1039
+ finalize();
1040
+ return;
1041
+ }
1042
+ try {
1043
+ observeChunk(result.value);
1044
+ } catch {}
1045
+ controller.enqueue(result.value);
1046
+ },
1047
+ cancel(reason) {
1048
+ finalize(reason ?? /* @__PURE__ */ new Error("Vercel AI SDK stream was cancelled"), true);
1049
+ return reader.cancel(reason);
1050
+ }
1051
+ }, { highWaterMark: 0 }),
1052
+ ...rest
1053
+ };
1054
+ } catch (error) {
1055
+ await captureStreamGeneration({
1056
+ ...baseOptions,
1057
+ model: modelId,
1058
+ provider,
1059
+ input: mergedOptions.posthogPrivacyMode ? "" : mapVercelPrompt(params.prompt, phClient),
1060
+ output: [],
1061
+ latency: (Date.now() - startTime) / 1e3,
1062
+ baseURL,
1063
+ modelParameters: getModelParams(mergedParams),
1064
+ usage: {},
1065
+ error,
1066
+ tools: availableTools
1067
+ });
1068
+ throw error;
1069
+ }
1070
+ },
1071
+ writable: true,
1072
+ configurable: true,
1073
+ enumerable: false
1074
+ }
1075
+ });
1420
1076
  };
1421
-
1077
+ //#endregion
1422
1078
  export { wrapVercelLanguageModel as withTracing };
1423
- //# sourceMappingURL=index.mjs.map
1079
+
1080
+ //# sourceMappingURL=index.mjs.map