@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.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- import { LanguageModelV2, LanguageModelV3 } from '@ai-sdk/provider';
2
- import { PostHog } from 'posthog-node';
3
-
1
+ import { LanguageModelV2, LanguageModelV3 } from "@ai-sdk/provider";
2
+ import { PostHog } from "posthog-node";
3
+ import "openai";
4
+ import "@anthropic-ai/sdk";
5
+ import "openai/lib/ResponsesParser";
6
+ //#region src/types.d.ts
4
7
  /**
5
8
  * Token usage reported by the provider.
6
9
  *
@@ -11,42 +14,42 @@ import { PostHog } from 'posthog-node';
11
14
  * exists.
12
15
  */
13
16
  interface TokenUsage {
14
- inputTokens?: number;
15
- outputTokens?: number;
16
- reasoningTokens?: unknown;
17
- cacheReadInputTokens?: unknown;
18
- cacheCreationInputTokens?: unknown;
19
- cacheReportingExclusive?: boolean;
20
- webSearchCount?: number;
21
- rawUsage?: unknown;
17
+ inputTokens?: number;
18
+ outputTokens?: number;
19
+ reasoningTokens?: unknown;
20
+ cacheReadInputTokens?: unknown;
21
+ cacheCreationInputTokens?: unknown;
22
+ cacheReportingExclusive?: boolean;
23
+ webSearchCount?: number;
24
+ rawUsage?: unknown;
22
25
  }
23
26
  /**
24
27
  * Options for fetching a prompt
25
28
  */
26
29
  interface GetPromptOptions {
27
- cacheTtlSeconds?: number;
28
- fallback?: string;
29
- /** Specific prompt version to fetch. Mutually exclusive with label. */
30
- version?: number;
31
- /** Fetch the version this label currently points to, e.g. 'production'. Mutually exclusive with version. */
32
- label?: string;
30
+ cacheTtlSeconds?: number;
31
+ fallback?: string;
32
+ /** Specific prompt version to fetch. Mutually exclusive with label. */
33
+ version?: number;
34
+ /** Fetch the version this label currently points to, e.g. 'production'. Mutually exclusive with version. */
35
+ label?: string;
33
36
  }
34
37
  /**
35
38
  * Result from the Prompts API or local cache — carries real metadata.
36
39
  */
37
40
  interface PromptRemoteResult {
38
- source: 'api' | 'cache' | 'stale_cache';
39
- prompt: string;
40
- name: string;
41
- version: number;
42
- /** The label the prompt was fetched by, when fetching with the label option. */
43
- label?: string;
44
- /**
45
- * JSON object of model parameters or agent configuration stored with the
46
- * prompt version, or null when the version has none. Use defensive access,
47
- * e.g. `result.config ?? {}` — fallback results carry no config.
48
- */
49
- config: Record<string, unknown> | null;
41
+ source: 'api' | 'cache' | 'stale_cache';
42
+ prompt: string;
43
+ name: string;
44
+ version: number;
45
+ /** The label the prompt was fetched by, when fetching with the label option. */
46
+ label?: string;
47
+ /**
48
+ * JSON object of model parameters or agent configuration stored with the
49
+ * prompt version, or null when the version has none. Use defensive access,
50
+ * e.g. `result.config ?? {}` — fallback results carry no config.
51
+ */
52
+ config: Record<string, unknown> | null;
50
53
  }
51
54
  /**
52
55
  * Result when the fetch failed and no cache was available — fell back to the
@@ -54,12 +57,12 @@ interface PromptRemoteResult {
54
57
  * accessible on the PromptResult union without a type guard.
55
58
  */
56
59
  interface PromptCodeFallbackResult {
57
- source: 'code_fallback';
58
- prompt: string;
59
- name: undefined;
60
- version: undefined;
61
- label: undefined;
62
- config: undefined;
60
+ source: 'code_fallback';
61
+ prompt: string;
62
+ name: undefined;
63
+ version: undefined;
64
+ label: undefined;
65
+ config: undefined;
63
66
  }
64
67
  /**
65
68
  * Discriminated union returned by `Prompts.get()`.
@@ -76,42 +79,45 @@ type PromptVariables = Record<string, string | number | boolean>;
76
79
  * Direct options for initializing Prompts without a PostHog client
77
80
  */
78
81
  interface PromptsDirectOptions {
79
- personalApiKey: string;
80
- projectApiKey: string;
81
- host?: string;
82
- defaultCacheTtlSeconds?: number;
82
+ personalApiKey: string;
83
+ projectApiKey: string;
84
+ host?: string;
85
+ defaultCacheTtlSeconds?: number;
83
86
  }
84
-
87
+ //#endregion
88
+ //#region src/utils.d.ts
85
89
  interface CostOverride {
86
- inputCost: number;
87
- outputCost: number;
90
+ inputCost: number;
91
+ outputCost: number;
88
92
  }
89
93
  declare enum AIEvent {
90
- Generation = "$ai_generation",
91
- Embedding = "$ai_embedding"
94
+ Generation = "$ai_generation",
95
+ Embedding = "$ai_embedding"
92
96
  }
93
-
97
+ //#endregion
98
+ //#region src/vercel/middleware.d.ts
94
99
  type LanguageModel = LanguageModelV2 | LanguageModelV3;
95
100
  interface ClientOptions {
96
- posthogDistinctId?: string;
97
- posthogTraceId?: string;
98
- posthogProperties?: Record<string, any>;
99
- posthogPrivacyMode?: boolean;
100
- posthogGroups?: Record<string, any>;
101
- posthogModelOverride?: string;
102
- posthogProviderOverride?: string;
103
- posthogCostOverride?: CostOverride;
104
- posthogCaptureImmediate?: boolean;
101
+ posthogDistinctId?: string;
102
+ posthogTraceId?: string;
103
+ posthogProperties?: Record<string, any>;
104
+ posthogPrivacyMode?: boolean;
105
+ posthogGroups?: Record<string, any>;
106
+ posthogModelOverride?: string;
107
+ posthogProviderOverride?: string;
108
+ posthogCostOverride?: CostOverride;
109
+ posthogCaptureImmediate?: boolean;
105
110
  }
106
111
  /**
107
112
  * Wraps a Vercel AI SDK language model (V2 or V3) with PostHog tracing.
108
113
  * Automatically detects the model version and applies appropriate instrumentation.
109
114
  */
110
115
  declare const wrapVercelLanguageModel: <T extends LanguageModel>(model: T, phClient: PostHog, options: ClientOptions) => T;
111
-
116
+ //#endregion
117
+ //#region src/prompts.d.ts
112
118
  interface PromptsWithPostHogOptions {
113
- posthog: PostHog;
114
- defaultCacheTtlSeconds?: number;
119
+ posthog: PostHog;
120
+ defaultCacheTtlSeconds?: number;
115
121
  }
116
122
  type PromptsOptions = PromptsWithPostHogOptions | PromptsDirectOptions;
117
123
  /**
@@ -153,116 +159,117 @@ type PromptsOptions = PromptsWithPostHogOptions | PromptsDirectOptions;
153
159
  * ```
154
160
  */
155
161
  declare class Prompts {
156
- private personalApiKey;
157
- private projectApiKey;
158
- private host;
159
- private defaultCacheTtlSeconds;
160
- private cache;
161
- constructor(options: PromptsOptions);
162
- private getPromptCache;
163
- private getOrCreatePromptCache;
164
- private getPromptReference;
165
- /**
166
- * Fetch a prompt by name from the PostHog API.
167
- *
168
- * Returns a `PromptResult` object carrying the prompt text alongside `source`,
169
- * `name`, `version`, and `config` metadata. Read `result.prompt` for the
170
- * template string and `result.config ?? {}` for model parameters or agent
171
- * configuration stored with the version.
172
- */
173
- get(name: string, options?: GetPromptOptions): Promise<PromptResult>;
174
- /**
175
- * Internal method that handles cache + fetch logic, returning full metadata.
176
- * Does NOT handle the string `fallback` option — callers handle that.
177
- */
178
- private getInternal;
179
- /**
180
- * Compile a prompt template with variable substitution
181
- *
182
- * Variables in the format `{{variableName}}` will be replaced with values from the variables object.
183
- * Unmatched variables are left unchanged.
184
- *
185
- * @param prompt - The prompt template string
186
- * @param variables - Object containing variable values
187
- * @returns The compiled prompt string
188
- */
189
- compile(prompt: string, variables: PromptVariables): string;
190
- /**
191
- * Clear the cache for a specific prompt or all prompts
192
- *
193
- * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt unless a version is also provided.
194
- * @param version - Optional prompt version to clear. Requires a prompt name.
195
- */
196
- clearCache(name?: string, version?: number): void;
197
- private fetchPromptFromApi;
162
+ private personalApiKey;
163
+ private projectApiKey;
164
+ private host;
165
+ private defaultCacheTtlSeconds;
166
+ private cache;
167
+ constructor(options: PromptsOptions);
168
+ private getPromptCache;
169
+ private getOrCreatePromptCache;
170
+ private getPromptReference;
171
+ /**
172
+ * Fetch a prompt by name from the PostHog API.
173
+ *
174
+ * Returns a `PromptResult` object carrying the prompt text alongside `source`,
175
+ * `name`, `version`, and `config` metadata. Read `result.prompt` for the
176
+ * template string and `result.config ?? {}` for model parameters or agent
177
+ * configuration stored with the version.
178
+ */
179
+ get(name: string, options?: GetPromptOptions): Promise<PromptResult>;
180
+ /**
181
+ * Internal method that handles cache + fetch logic, returning full metadata.
182
+ * Does NOT handle the string `fallback` option — callers handle that.
183
+ */
184
+ private getInternal;
185
+ /**
186
+ * Compile a prompt template with variable substitution
187
+ *
188
+ * Variables in the format `{{variableName}}` will be replaced with values from the variables object.
189
+ * Unmatched variables are left unchanged.
190
+ *
191
+ * @param prompt - The prompt template string
192
+ * @param variables - Object containing variable values
193
+ * @returns The compiled prompt string
194
+ */
195
+ compile(prompt: string, variables: PromptVariables): string;
196
+ /**
197
+ * Clear the cache for a specific prompt or all prompts
198
+ *
199
+ * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt unless a version is also provided.
200
+ * @param version - Optional prompt version to clear. Requires a prompt name.
201
+ */
202
+ clearCache(name?: string, version?: number): void;
203
+ private fetchPromptFromApi;
198
204
  }
199
-
205
+ //#endregion
206
+ //#region src/captureAiGeneration.d.ts
200
207
  /**
201
208
  * Options for `captureAiGeneration`. Mirrors the `$ai_generation` event shape
202
209
  * directly so that any caller — first-party SDK wrappers and external code
203
210
  * alike — produces an identical event.
204
211
  */
205
212
  interface CaptureAiGenerationOptions {
206
- distinctId?: string;
207
- /** Auto-generated when omitted. */
208
- traceId?: string;
209
- /** Defaults to `$ai_generation`. */
210
- eventType?: AIEvent;
211
- /** Required for the event to be useful, but accepted as optional so SDK wrappers can pass through whatever they detect. */
212
- model?: string;
213
- provider: string;
214
- input: unknown;
215
- output: unknown;
216
- /** Maps to `$ai_model_parameters` (temperature, max_tokens, top_p, …). */
217
- modelParameters?: Record<string, unknown>;
218
- /**
219
- * `null` explicitly signals no base URL and omits `$ai_base_url` from the event.
220
- * `undefined`/omitted keeps the existing `''` default for backward compatibility.
221
- */
222
- baseURL?: string | null;
223
- httpStatus?: number;
224
- /** Wall-clock latency in seconds. */
225
- latency?: number;
226
- /** Time from request start to the first streamed token, in seconds. */
227
- timeToFirstToken?: number;
228
- usage?: TokenUsage;
229
- /** Extra event properties merged into the captured event. */
230
- properties?: Record<string, unknown>;
231
- /** Mapping of group type to group id, matching `EventMessage.groups`. */
232
- groups?: Record<string, string | number>;
233
- privacyMode?: boolean;
234
- /**
235
- * For SDK wrappers: overrides the auto-detected model. External callers
236
- * should pass `model` directly instead.
237
- */
238
- modelOverride?: string;
239
- /**
240
- * For SDK wrappers: overrides the auto-detected provider. External callers
241
- * should pass `provider` directly instead.
242
- */
243
- providerOverride?: string;
244
- costOverride?: CostOverride;
245
- tools?: unknown[] | null;
246
- stopReason?: string;
247
- /**
248
- * Provider-assigned ID for the generation (e.g. OpenAI's `chatcmpl-…` /
249
- * `resp_…`). Maps to `$ai_completion_id`. Response IDs generalize across
250
- * providers, so this lives in the shared schema rather than under
251
- * `providerMetadata`.
252
- */
253
- completionId?: string;
254
- /**
255
- * Provider-specific response metadata that has no place in the shared,
256
- * provider-agnostic `$ai_*` schema (e.g. OpenAI's `system_fingerprint` and
257
- * `request_id`). Maps to `$ai_provider_metadata`; omitted when empty.
258
- */
259
- providerMetadata?: Record<string, unknown>;
260
- /** When set, the event is captured as an error. */
261
- error?: unknown;
262
- /** Awaits delivery instead of batching. Useful in serverless environments. */
263
- captureImmediate?: boolean;
264
- /** Invoked when generation telemetry cannot be captured. Errors thrown by this callback are ignored. */
265
- onError?: (error: unknown) => void;
213
+ distinctId?: string;
214
+ /** Auto-generated when omitted. */
215
+ traceId?: string;
216
+ /** Defaults to `$ai_generation`. */
217
+ eventType?: AIEvent;
218
+ /** Required for the event to be useful, but accepted as optional so SDK wrappers can pass through whatever they detect. */
219
+ model?: string;
220
+ provider: string;
221
+ input: unknown;
222
+ output: unknown;
223
+ /** Maps to `$ai_model_parameters` (temperature, max_tokens, top_p, …). */
224
+ modelParameters?: Record<string, unknown>;
225
+ /**
226
+ * `null` explicitly signals no base URL and omits `$ai_base_url` from the event.
227
+ * `undefined`/omitted keeps the existing `''` default for backward compatibility.
228
+ */
229
+ baseURL?: string | null;
230
+ httpStatus?: number;
231
+ /** Wall-clock latency in seconds. */
232
+ latency?: number;
233
+ /** Time from request start to the first streamed token, in seconds. */
234
+ timeToFirstToken?: number;
235
+ usage?: TokenUsage;
236
+ /** Extra event properties merged into the captured event. */
237
+ properties?: Record<string, unknown>;
238
+ /** Mapping of group type to group id, matching `EventMessage.groups`. */
239
+ groups?: Record<string, string | number>;
240
+ privacyMode?: boolean;
241
+ /**
242
+ * For SDK wrappers: overrides the auto-detected model. External callers
243
+ * should pass `model` directly instead.
244
+ */
245
+ modelOverride?: string;
246
+ /**
247
+ * For SDK wrappers: overrides the auto-detected provider. External callers
248
+ * should pass `provider` directly instead.
249
+ */
250
+ providerOverride?: string;
251
+ costOverride?: CostOverride;
252
+ tools?: unknown[] | null;
253
+ stopReason?: string;
254
+ /**
255
+ * Provider-assigned ID for the generation (e.g. OpenAI's `chatcmpl-…` /
256
+ * `resp_…`). Maps to `$ai_completion_id`. Response IDs generalize across
257
+ * providers, so this lives in the shared schema rather than under
258
+ * `providerMetadata`.
259
+ */
260
+ completionId?: string;
261
+ /**
262
+ * Provider-specific response metadata that has no place in the shared,
263
+ * provider-agnostic `$ai_*` schema (e.g. OpenAI's `system_fingerprint` and
264
+ * `request_id`). Maps to `$ai_provider_metadata`; omitted when empty.
265
+ */
266
+ providerMetadata?: Record<string, unknown>;
267
+ /** When set, the event is captured as an error. */
268
+ error?: unknown;
269
+ /** Awaits delivery instead of batching. Useful in serverless environments. */
270
+ captureImmediate?: boolean;
271
+ /** Invoked when generation telemetry cannot be captured. Errors thrown by this callback are ignored. */
272
+ onError?: (error: unknown) => void;
266
273
  }
267
274
  /**
268
275
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
@@ -278,6 +285,6 @@ interface CaptureAiGenerationOptions {
278
285
  * so callers can re-throw the original error reference safely.
279
286
  */
280
287
  declare const captureAiGeneration: (client: PostHog, options: CaptureAiGenerationOptions) => Promise<void>;
281
-
282
- export { AIEvent, Prompts, captureAiGeneration, wrapVercelLanguageModel as withTracing };
283
- export type { CaptureAiGenerationOptions, PromptCodeFallbackResult, PromptRemoteResult, PromptResult };
288
+ //#endregion
289
+ export { AIEvent, type CaptureAiGenerationOptions, type PromptCodeFallbackResult, type PromptRemoteResult, type PromptResult, Prompts, captureAiGeneration, wrapVercelLanguageModel as withTracing };
290
+ //# sourceMappingURL=index.d.ts.map