@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/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
|
}
|
|
@@ -423,7 +491,7 @@ function sanitizeValues(obj) {
|
|
|
423
491
|
}
|
|
424
492
|
//#endregion
|
|
425
493
|
//#region package.json
|
|
426
|
-
var version = "8.
|
|
494
|
+
var version = "8.21.1";
|
|
427
495
|
//#endregion
|
|
428
496
|
//#region src/serializeError.ts
|
|
429
497
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -618,6 +686,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
618
686
|
//#endregion
|
|
619
687
|
//#region src/captureAiGeneration.ts
|
|
620
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
|
+
/**
|
|
621
703
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
622
704
|
*
|
|
623
705
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -653,7 +735,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
653
735
|
if (client.options?.enableExceptionAutocapture) {
|
|
654
736
|
if (!shouldRedact) {
|
|
655
737
|
exceptionId = uuidv7();
|
|
656
|
-
client
|
|
738
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
657
739
|
}
|
|
658
740
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
659
741
|
}
|
|
@@ -679,6 +761,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
679
761
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
680
762
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
681
763
|
};
|
|
764
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
682
765
|
const properties = {
|
|
683
766
|
$ai_lib: "posthog-ai",
|
|
684
767
|
$ai_lib_version: version,
|
|
@@ -698,7 +781,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
698
781
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
699
782
|
...options.properties,
|
|
700
783
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
701
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
702
784
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
703
785
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
704
786
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -707,9 +789,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
707
789
|
...costOverrideData
|
|
708
790
|
};
|
|
709
791
|
const event = {
|
|
710
|
-
distinctId:
|
|
792
|
+
distinctId: distinctId ?? traceId,
|
|
711
793
|
event: eventType,
|
|
712
|
-
properties,
|
|
794
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
713
795
|
groups: options.groups
|
|
714
796
|
};
|
|
715
797
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|
|
@@ -990,6 +1072,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
990
1072
|
traceId,
|
|
991
1073
|
properties: mergedOptions.posthogProperties,
|
|
992
1074
|
groups: mergedOptions.posthogGroups,
|
|
1075
|
+
options: mergedOptions.posthogOptions,
|
|
993
1076
|
privacyMode,
|
|
994
1077
|
modelOverride: mergedOptions.posthogModelOverride,
|
|
995
1078
|
providerOverride: mergedOptions.posthogProviderOverride,
|