@posthog/ai 8.20.1 → 8.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/adk/index.cjs +125 -36
- package/dist/adk/index.cjs.map +1 -1
- package/dist/adk/index.d.ts +7 -0
- package/dist/adk/index.mjs +126 -37
- package/dist/adk/index.mjs.map +1 -1
- package/dist/anthropic/index.cjs +115 -22
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +7 -0
- package/dist/anthropic/index.mjs +116 -23
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/claude-agent-sdk/index.cjs +131 -35
- package/dist/claude-agent-sdk/index.cjs.map +1 -1
- package/dist/claude-agent-sdk/index.d.ts +6 -0
- package/dist/claude-agent-sdk/index.mjs +132 -36
- package/dist/claude-agent-sdk/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +116 -23
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.d.ts +7 -0
- package/dist/gemini/index.mjs +116 -23
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/genkit/index.cjs +138 -43
- package/dist/genkit/index.cjs.map +1 -1
- package/dist/genkit/index.d.ts +7 -0
- package/dist/genkit/index.mjs +138 -43
- package/dist/genkit/index.mjs.map +1 -1
- package/dist/index.cjs +114 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.mjs +115 -22
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +99 -21
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.d.ts +7 -0
- package/dist/langchain/index.mjs +99 -21
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/langchain/middleware/index.cjs +99 -21
- package/dist/langchain/middleware/index.cjs.map +1 -1
- package/dist/langchain/middleware/index.d.ts +6 -0
- package/dist/langchain/middleware/index.mjs +99 -21
- package/dist/langchain/middleware/index.mjs.map +1 -1
- package/dist/openai/index.cjs +115 -22
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +7 -0
- package/dist/openai/index.mjs +116 -23
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +139 -60
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.d.ts +7 -0
- package/dist/openai-agents/index.mjs +139 -60
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/otel/index.browser.cjs +24 -14
- package/dist/otel/index.browser.cjs.map +1 -1
- package/dist/otel/index.browser.mjs +24 -14
- package/dist/otel/index.browser.mjs.map +1 -1
- package/dist/otel/index.cjs +24 -14
- package/dist/otel/index.cjs.map +1 -1
- package/dist/otel/index.d.ts +3 -3
- package/dist/otel/index.mjs +24 -14
- package/dist/otel/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +114 -21
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.d.ts +6 -0
- package/dist/vercel/index.mjs +115 -22
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { JsonType } from "@posthog/core";
|
|
1
2
|
import { LanguageModelV2, LanguageModelV3 } from "@ai-sdk/provider";
|
|
2
3
|
import { PostHog } from "posthog-node";
|
|
3
4
|
import "openai";
|
|
@@ -107,6 +108,11 @@ interface ClientOptions {
|
|
|
107
108
|
posthogProviderOverride?: string;
|
|
108
109
|
posthogCostOverride?: CostOverride;
|
|
109
110
|
posthogCaptureImmediate?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Per-event options for posthog-node, such as `{ process_person_profile: false }`. An event with
|
|
113
|
+
* no `posthogDistinctId` always has person processing turned off.
|
|
114
|
+
*/
|
|
115
|
+
posthogOptions?: Record<string, JsonType>;
|
|
110
116
|
}
|
|
111
117
|
/**
|
|
112
118
|
* Wraps a Vercel AI SDK language model (V2 or V3) with PostHog tracing.
|
|
@@ -289,6 +295,11 @@ interface CaptureAiGenerationOptions {
|
|
|
289
295
|
properties?: Record<string, unknown>;
|
|
290
296
|
/** Mapping of group type to group id, matching `EventMessage.groups`. */
|
|
291
297
|
groups?: Record<string, string | number>;
|
|
298
|
+
/**
|
|
299
|
+
* Per-event options for posthog-node, such as `{ process_person_profile: false }`. An event with
|
|
300
|
+
* no `distinctId` always has person processing turned off.
|
|
301
|
+
*/
|
|
302
|
+
options?: Record<string, JsonType>;
|
|
292
303
|
privacyMode?: boolean;
|
|
293
304
|
/**
|
|
294
305
|
* For SDK wrappers: overrides the auto-detected model. External callers
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { v4 } from "uuid";
|
|
2
|
-
import { toJsonSafeValue, uuidv7 } from "@posthog/core";
|
|
2
|
+
import { acceptsEventOptions, eventOptionsAsLegacyProperties, toJsonSafeValue, uuidv7 } from "@posthog/core";
|
|
3
3
|
//#region src/typeGuards.ts
|
|
4
4
|
const isString = (value) => {
|
|
5
5
|
return typeof value === "string";
|
|
@@ -13,9 +13,77 @@ const isObject = (value) => {
|
|
|
13
13
|
function isFullAiCaptureEnabled(client) {
|
|
14
14
|
return client?.enableFullAiCapture === true;
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
function userDistinctId(value) {
|
|
22
|
+
if (!value) return;
|
|
23
|
+
const id = String(value);
|
|
24
|
+
return id.trim() === "" ? void 0 : id;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The caller's per-event options when they are an object. A missing or `null` value means no
|
|
28
|
+
* options. An array or any other value is reported and ignored, so it is never spread.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
function readAiEventOptions(value) {
|
|
33
|
+
if (value === void 0 || value === null) return;
|
|
34
|
+
if (typeof value === "object" && !Array.isArray(value)) return value;
|
|
35
|
+
console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
|
|
39
|
+
* processing off and beats the caller's `process_person_profile`.
|
|
40
|
+
*
|
|
41
|
+
* posthog-node 6 takes the options as they are, so no option is written as a property.
|
|
42
|
+
* posthog-node 5 has no per-event options: each known option is sent as the property that version
|
|
43
|
+
* reads, and an unknown key is dropped.
|
|
44
|
+
*
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
function eventOptionFields(client, properties, options, personless) {
|
|
48
|
+
const userOptions = readAiEventOptions(options);
|
|
49
|
+
if (isPostHogNode6(client)) {
|
|
50
|
+
const eventOptions = {
|
|
51
|
+
...userOptions,
|
|
52
|
+
...personless ? { process_person_profile: false } : {}
|
|
53
|
+
};
|
|
54
|
+
return Object.keys(eventOptions).length > 0 ? {
|
|
55
|
+
properties,
|
|
56
|
+
options: eventOptions
|
|
57
|
+
} : { properties };
|
|
58
|
+
}
|
|
59
|
+
const withOptionProperties = {
|
|
60
|
+
...properties,
|
|
61
|
+
...eventOptionsAsLegacyProperties(userOptions)
|
|
62
|
+
};
|
|
63
|
+
if (personless) withOptionProperties.$process_person_profile = false;
|
|
64
|
+
return { properties: withOptionProperties };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Whether the client is posthog-node 6 or later, the first major with per-event options.
|
|
68
|
+
*
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
function isPostHogNode6(client) {
|
|
72
|
+
return acceptsEventOptions(client);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
|
|
76
|
+
* controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
|
|
77
|
+
* upgrade never changes how a 5.x client sends.
|
|
78
|
+
*
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
function usesAiLane(client) {
|
|
82
|
+
return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
|
|
83
|
+
}
|
|
16
84
|
/** @internal */
|
|
17
85
|
function captureAiEvent(client, event) {
|
|
18
|
-
if (
|
|
86
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
19
87
|
client.captureAi(event);
|
|
20
88
|
return;
|
|
21
89
|
}
|
|
@@ -23,7 +91,7 @@ function captureAiEvent(client, event) {
|
|
|
23
91
|
}
|
|
24
92
|
/** @internal */
|
|
25
93
|
async function captureAiEventImmediate(client, event) {
|
|
26
|
-
if (
|
|
94
|
+
if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
|
|
27
95
|
await client.captureAiImmediate(event);
|
|
28
96
|
return;
|
|
29
97
|
}
|
|
@@ -171,13 +239,17 @@ var MediaTypeContext = class MediaTypeContext {
|
|
|
171
239
|
//#region src/sanitization/binary_content_redactor.ts
|
|
172
240
|
const STRONG_CONTEXT_MIN_LENGTH = 64;
|
|
173
241
|
const WEAK_CONTEXT_MIN_LENGTH = 1024;
|
|
242
|
+
const MAX_NODES = 1e4;
|
|
243
|
+
const TRUNCATED = "[Truncated]";
|
|
174
244
|
var BinaryContentRedactor = class {
|
|
175
245
|
constructor(recognizer = new Base64Recognizer()) {
|
|
176
246
|
this.recognizer = recognizer;
|
|
177
|
-
this.
|
|
247
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
248
|
+
this.remainingNodes = MAX_NODES;
|
|
178
249
|
}
|
|
179
250
|
redact(value, mediaType) {
|
|
180
|
-
this.
|
|
251
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
252
|
+
this.remainingNodes = MAX_NODES;
|
|
181
253
|
return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
|
|
182
254
|
}
|
|
183
255
|
/** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
|
|
@@ -189,18 +261,24 @@ var BinaryContentRedactor = class {
|
|
|
189
261
|
if (typeof value === "string") return this.redactLeaf(value, ctx);
|
|
190
262
|
if (typeof value !== "object") return value;
|
|
191
263
|
if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
|
|
192
|
-
if (this.
|
|
193
|
-
this.
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
264
|
+
if (this.ancestors.has(value)) return null;
|
|
265
|
+
if (this.remainingNodes <= 0) return TRUNCATED;
|
|
266
|
+
this.remainingNodes--;
|
|
267
|
+
this.ancestors.add(value);
|
|
268
|
+
try {
|
|
269
|
+
if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
|
|
270
|
+
const obj = value;
|
|
271
|
+
const out = {};
|
|
272
|
+
for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
|
|
273
|
+
value: this.walk(obj[k], new MediaTypeContext(obj, k)),
|
|
274
|
+
enumerable: true,
|
|
275
|
+
writable: true,
|
|
276
|
+
configurable: true
|
|
277
|
+
});
|
|
278
|
+
return out;
|
|
279
|
+
} finally {
|
|
280
|
+
this.ancestors.delete(value);
|
|
281
|
+
}
|
|
204
282
|
}
|
|
205
283
|
redactString(value, ctx) {
|
|
206
284
|
const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
|
|
@@ -418,7 +496,7 @@ function sanitizeValues(obj) {
|
|
|
418
496
|
}
|
|
419
497
|
//#endregion
|
|
420
498
|
//#region package.json
|
|
421
|
-
var version = "8.
|
|
499
|
+
var version = "8.21.0";
|
|
422
500
|
//#endregion
|
|
423
501
|
//#region src/serializeError.ts
|
|
424
502
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -613,6 +691,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
613
691
|
//#endregion
|
|
614
692
|
//#region src/captureAiGeneration.ts
|
|
615
693
|
/**
|
|
694
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
695
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
696
|
+
*/
|
|
697
|
+
const captureAiException = (client, error, properties, uuid) => {
|
|
698
|
+
if (isPostHogNode6(client)) {
|
|
699
|
+
client.captureException(error, {
|
|
700
|
+
properties,
|
|
701
|
+
uuid
|
|
702
|
+
});
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
client.captureException.call(client, error, void 0, properties, uuid);
|
|
706
|
+
};
|
|
707
|
+
/**
|
|
616
708
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
617
709
|
*
|
|
618
710
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -648,7 +740,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
648
740
|
if (client.options?.enableExceptionAutocapture) {
|
|
649
741
|
if (!shouldRedact) {
|
|
650
742
|
exceptionId = uuidv7();
|
|
651
|
-
client
|
|
743
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
652
744
|
}
|
|
653
745
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
654
746
|
}
|
|
@@ -674,6 +766,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
674
766
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
675
767
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
676
768
|
};
|
|
769
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
677
770
|
const properties = {
|
|
678
771
|
$ai_lib: "posthog-ai",
|
|
679
772
|
$ai_lib_version: version,
|
|
@@ -693,7 +786,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
693
786
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
694
787
|
...options.properties,
|
|
695
788
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
696
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
697
789
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
698
790
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
699
791
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -702,9 +794,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
702
794
|
...costOverrideData
|
|
703
795
|
};
|
|
704
796
|
const event = {
|
|
705
|
-
distinctId:
|
|
797
|
+
distinctId: distinctId ?? traceId,
|
|
706
798
|
event: eventType,
|
|
707
|
-
properties,
|
|
799
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
708
800
|
groups: options.groups
|
|
709
801
|
};
|
|
710
802
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|
|
@@ -985,6 +1077,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
985
1077
|
traceId,
|
|
986
1078
|
properties: mergedOptions.posthogProperties,
|
|
987
1079
|
groups: mergedOptions.posthogGroups,
|
|
1080
|
+
options: mergedOptions.posthogOptions,
|
|
988
1081
|
privacyMode,
|
|
989
1082
|
modelOverride: mergedOptions.posthogModelOverride,
|
|
990
1083
|
providerOverride: mergedOptions.posthogProviderOverride,
|