@posthog/ai 8.10.0 → 8.10.2

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