@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/vercel/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";
|
|
@@ -21,6 +22,11 @@ interface ClientOptions {
|
|
|
21
22
|
posthogProviderOverride?: string;
|
|
22
23
|
posthogCostOverride?: CostOverride;
|
|
23
24
|
posthogCaptureImmediate?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Per-event options for posthog-node, such as `{ process_person_profile: false }`. An event with
|
|
27
|
+
* no `posthogDistinctId` always has person processing turned off.
|
|
28
|
+
*/
|
|
29
|
+
posthogOptions?: Record<string, JsonType>;
|
|
24
30
|
}
|
|
25
31
|
/**
|
|
26
32
|
* Wraps a Vercel AI SDK language model (V2 or V3) with PostHog tracing.
|
package/dist/vercel/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();
|
|
@@ -413,7 +491,7 @@ function sanitizeValues(obj) {
|
|
|
413
491
|
}
|
|
414
492
|
//#endregion
|
|
415
493
|
//#region package.json
|
|
416
|
-
var version = "8.
|
|
494
|
+
var version = "8.21.0";
|
|
417
495
|
//#endregion
|
|
418
496
|
//#region src/serializeError.ts
|
|
419
497
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -608,6 +686,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
608
686
|
//#endregion
|
|
609
687
|
//#region src/captureAiGeneration.ts
|
|
610
688
|
/**
|
|
689
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
690
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
691
|
+
*/
|
|
692
|
+
const captureAiException = (client, error, properties, uuid) => {
|
|
693
|
+
if (isPostHogNode6(client)) {
|
|
694
|
+
client.captureException(error, {
|
|
695
|
+
properties,
|
|
696
|
+
uuid
|
|
697
|
+
});
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
client.captureException.call(client, error, void 0, properties, uuid);
|
|
701
|
+
};
|
|
702
|
+
/**
|
|
611
703
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
612
704
|
*
|
|
613
705
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -643,7 +735,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
643
735
|
if (client.options?.enableExceptionAutocapture) {
|
|
644
736
|
if (!shouldRedact) {
|
|
645
737
|
exceptionId = uuidv7();
|
|
646
|
-
client
|
|
738
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
647
739
|
}
|
|
648
740
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
649
741
|
}
|
|
@@ -669,6 +761,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
669
761
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
670
762
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
671
763
|
};
|
|
764
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
672
765
|
const properties = {
|
|
673
766
|
$ai_lib: "posthog-ai",
|
|
674
767
|
$ai_lib_version: version,
|
|
@@ -688,7 +781,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
688
781
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
689
782
|
...options.properties,
|
|
690
783
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
691
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
692
784
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
693
785
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
694
786
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -697,9 +789,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
697
789
|
...costOverrideData
|
|
698
790
|
};
|
|
699
791
|
const event = {
|
|
700
|
-
distinctId:
|
|
792
|
+
distinctId: distinctId ?? traceId,
|
|
701
793
|
event: eventType,
|
|
702
|
-
properties,
|
|
794
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
703
795
|
groups: options.groups
|
|
704
796
|
};
|
|
705
797
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|
|
@@ -980,6 +1072,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
980
1072
|
traceId,
|
|
981
1073
|
properties: mergedOptions.posthogProperties,
|
|
982
1074
|
groups: mergedOptions.posthogGroups,
|
|
1075
|
+
options: mergedOptions.posthogOptions,
|
|
983
1076
|
privacyMode,
|
|
984
1077
|
modelOverride: mergedOptions.posthogModelOverride,
|
|
985
1078
|
providerOverride: mergedOptions.posthogProviderOverride,
|