@posthog/ai 8.9.3 → 8.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adk/index.cjs +977 -0
- package/dist/adk/index.cjs.map +1 -0
- package/dist/adk/index.d.ts +149 -0
- package/dist/adk/index.mjs +976 -0
- package/dist/adk/index.mjs.map +1 -0
- package/dist/anthropic/index.cjs +927 -1104
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +34 -33
- package/dist/anthropic/index.mjs +899 -1095
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +867 -1112
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.d.ts +38 -35
- package/dist/gemini/index.mjs +862 -1107
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +1218 -1539
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +170 -157
- package/dist/index.mjs +1216 -1537
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +851 -1029
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.d.ts +75 -75
- package/dist/langchain/index.mjs +850 -1027
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/langchain/middleware/index.cjs +1016 -1225
- package/dist/langchain/middleware/index.cjs.map +1 -1
- package/dist/langchain/middleware/index.d.ts +29 -25
- package/dist/langchain/middleware/index.mjs +1015 -1223
- package/dist/langchain/middleware/index.mjs.map +1 -1
- package/dist/openai/index.cjs +1990 -2516
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +106 -104
- package/dist/openai/index.mjs +1985 -2511
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +745 -827
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.d.ts +48 -47
- package/dist/openai-agents/index.mjs +744 -825
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/otel/index.cjs +427 -486
- package/dist/otel/index.cjs.map +1 -1
- package/dist/otel/index.d.ts +36 -35
- package/dist/otel/index.mjs +426 -484
- package/dist/otel/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +992 -1336
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.d.ts +21 -16
- package/dist/vercel/index.mjs +991 -1334
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +23 -12
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { LanguageModelV2, LanguageModelV3 } from
|
|
2
|
-
import { PostHog } from
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
87
|
-
|
|
90
|
+
inputCost: number;
|
|
91
|
+
outputCost: number;
|
|
88
92
|
}
|
|
89
93
|
declare enum AIEvent {
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
114
|
-
|
|
119
|
+
posthog: PostHog;
|
|
120
|
+
defaultCacheTtlSeconds?: number;
|
|
115
121
|
}
|
|
116
122
|
type PromptsOptions = PromptsWithPostHogOptions | PromptsDirectOptions;
|
|
117
123
|
/**
|
|
@@ -153,110 +159,117 @@ type PromptsOptions = PromptsWithPostHogOptions | PromptsDirectOptions;
|
|
|
153
159
|
* ```
|
|
154
160
|
*/
|
|
155
161
|
declare class Prompts {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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;
|
|
260
273
|
}
|
|
261
274
|
/**
|
|
262
275
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
@@ -272,6 +285,6 @@ interface CaptureAiGenerationOptions {
|
|
|
272
285
|
* so callers can re-throw the original error reference safely.
|
|
273
286
|
*/
|
|
274
287
|
declare const captureAiGeneration: (client: PostHog, options: CaptureAiGenerationOptions) => Promise<void>;
|
|
275
|
-
|
|
276
|
-
export { AIEvent, Prompts, captureAiGeneration, wrapVercelLanguageModel as withTracing };
|
|
277
|
-
|
|
288
|
+
//#endregion
|
|
289
|
+
export { AIEvent, type CaptureAiGenerationOptions, type PromptCodeFallbackResult, type PromptRemoteResult, type PromptResult, Prompts, captureAiGeneration, wrapVercelLanguageModel as withTracing };
|
|
290
|
+
//# sourceMappingURL=index.d.ts.map
|