@posthog/ai 8.20.2 → 8.21.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/README.md +20 -0
- package/dist/adk/index.cjs +101 -22
- package/dist/adk/index.cjs.map +1 -1
- package/dist/adk/index.d.ts +7 -0
- package/dist/adk/index.mjs +102 -23
- package/dist/adk/index.mjs.map +1 -1
- package/dist/anthropic/index.cjs +92 -9
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +7 -0
- package/dist/anthropic/index.mjs +93 -10
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/claude-agent-sdk/index.cjs +107 -21
- 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 +108 -22
- package/dist/claude-agent-sdk/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +92 -9
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.d.ts +7 -0
- package/dist/gemini/index.mjs +92 -9
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/genkit/index.cjs +114 -29
- package/dist/genkit/index.cjs.map +1 -1
- package/dist/genkit/index.d.ts +7 -0
- package/dist/genkit/index.mjs +114 -29
- package/dist/genkit/index.mjs.map +1 -1
- package/dist/index.cjs +90 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.mjs +91 -8
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +75 -7
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.d.ts +7 -0
- package/dist/langchain/index.mjs +75 -7
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/langchain/middleware/index.cjs +75 -7
- package/dist/langchain/middleware/index.cjs.map +1 -1
- package/dist/langchain/middleware/index.d.ts +6 -0
- package/dist/langchain/middleware/index.mjs +75 -7
- package/dist/langchain/middleware/index.mjs.map +1 -1
- package/dist/openai/index.cjs +91 -8
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +7 -0
- package/dist/openai/index.mjs +92 -9
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +115 -46
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.d.ts +7 -0
- package/dist/openai-agents/index.mjs +115 -46
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/otel/index.browser.cjs.map +1 -1
- package/dist/otel/index.browser.mjs.map +1 -1
- package/dist/otel/index.cjs.map +1 -1
- package/dist/otel/index.d.ts +3 -3
- package/dist/otel/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +90 -7
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.d.ts +6 -0
- package/dist/vercel/index.mjs +91 -8
- 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
|
}
|
|
@@ -428,7 +496,7 @@ function sanitizeValues(obj) {
|
|
|
428
496
|
}
|
|
429
497
|
//#endregion
|
|
430
498
|
//#region package.json
|
|
431
|
-
var version = "8.
|
|
499
|
+
var version = "8.21.1";
|
|
432
500
|
//#endregion
|
|
433
501
|
//#region src/serializeError.ts
|
|
434
502
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -623,6 +691,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
623
691
|
//#endregion
|
|
624
692
|
//#region src/captureAiGeneration.ts
|
|
625
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
|
+
/**
|
|
626
708
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
627
709
|
*
|
|
628
710
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -658,7 +740,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
658
740
|
if (client.options?.enableExceptionAutocapture) {
|
|
659
741
|
if (!shouldRedact) {
|
|
660
742
|
exceptionId = uuidv7();
|
|
661
|
-
client
|
|
743
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
662
744
|
}
|
|
663
745
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
664
746
|
}
|
|
@@ -684,6 +766,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
684
766
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
685
767
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
686
768
|
};
|
|
769
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
687
770
|
const properties = {
|
|
688
771
|
$ai_lib: "posthog-ai",
|
|
689
772
|
$ai_lib_version: version,
|
|
@@ -703,7 +786,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
703
786
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
704
787
|
...options.properties,
|
|
705
788
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
706
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
707
789
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
708
790
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
709
791
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -712,9 +794,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
712
794
|
...costOverrideData
|
|
713
795
|
};
|
|
714
796
|
const event = {
|
|
715
|
-
distinctId:
|
|
797
|
+
distinctId: distinctId ?? traceId,
|
|
716
798
|
event: eventType,
|
|
717
|
-
properties,
|
|
799
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
718
800
|
groups: options.groups
|
|
719
801
|
};
|
|
720
802
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|
|
@@ -995,6 +1077,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
995
1077
|
traceId,
|
|
996
1078
|
properties: mergedOptions.posthogProperties,
|
|
997
1079
|
groups: mergedOptions.posthogGroups,
|
|
1080
|
+
options: mergedOptions.posthogOptions,
|
|
998
1081
|
privacyMode,
|
|
999
1082
|
modelOverride: mergedOptions.posthogModelOverride,
|
|
1000
1083
|
providerOverride: mergedOptions.posthogProviderOverride,
|