@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/anthropic/index.cjs
CHANGED
|
@@ -39,9 +39,77 @@ const isObject = (value) => {
|
|
|
39
39
|
function isFullAiCaptureEnabled(client) {
|
|
40
40
|
return client?.enableFullAiCapture === true;
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
|
|
44
|
+
*
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
function userDistinctId(value) {
|
|
48
|
+
if (!value) return;
|
|
49
|
+
const id = String(value);
|
|
50
|
+
return id.trim() === "" ? void 0 : id;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The caller's per-event options when they are an object. A missing or `null` value means no
|
|
54
|
+
* options. An array or any other value is reported and ignored, so it is never spread.
|
|
55
|
+
*
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
function readAiEventOptions(value) {
|
|
59
|
+
if (value === void 0 || value === null) return;
|
|
60
|
+
if (typeof value === "object" && !Array.isArray(value)) return value;
|
|
61
|
+
console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
|
|
65
|
+
* processing off and beats the caller's `process_person_profile`.
|
|
66
|
+
*
|
|
67
|
+
* posthog-node 6 takes the options as they are, so no option is written as a property.
|
|
68
|
+
* posthog-node 5 has no per-event options: each known option is sent as the property that version
|
|
69
|
+
* reads, and an unknown key is dropped.
|
|
70
|
+
*
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
73
|
+
function eventOptionFields(client, properties, options, personless) {
|
|
74
|
+
const userOptions = readAiEventOptions(options);
|
|
75
|
+
if (isPostHogNode6(client)) {
|
|
76
|
+
const eventOptions = {
|
|
77
|
+
...userOptions,
|
|
78
|
+
...personless ? { process_person_profile: false } : {}
|
|
79
|
+
};
|
|
80
|
+
return Object.keys(eventOptions).length > 0 ? {
|
|
81
|
+
properties,
|
|
82
|
+
options: eventOptions
|
|
83
|
+
} : { properties };
|
|
84
|
+
}
|
|
85
|
+
const withOptionProperties = {
|
|
86
|
+
...properties,
|
|
87
|
+
...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
|
|
88
|
+
};
|
|
89
|
+
if (personless) withOptionProperties.$process_person_profile = false;
|
|
90
|
+
return { properties: withOptionProperties };
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Whether the client is posthog-node 6 or later, the first major with per-event options.
|
|
94
|
+
*
|
|
95
|
+
* @internal
|
|
96
|
+
*/
|
|
97
|
+
function isPostHogNode6(client) {
|
|
98
|
+
return (0, _posthog_core.acceptsEventOptions)(client);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
|
|
102
|
+
* controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
|
|
103
|
+
* upgrade never changes how a 5.x client sends.
|
|
104
|
+
*
|
|
105
|
+
* @internal
|
|
106
|
+
*/
|
|
107
|
+
function usesAiLane(client) {
|
|
108
|
+
return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
|
|
109
|
+
}
|
|
42
110
|
/** @internal */
|
|
43
111
|
function captureAiEvent(client, event) {
|
|
44
|
-
if (
|
|
112
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
45
113
|
client.captureAi(event);
|
|
46
114
|
return;
|
|
47
115
|
}
|
|
@@ -49,7 +117,7 @@ function captureAiEvent(client, event) {
|
|
|
49
117
|
}
|
|
50
118
|
/** @internal */
|
|
51
119
|
async function captureAiEventImmediate(client, event) {
|
|
52
|
-
if (
|
|
120
|
+
if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
|
|
53
121
|
await client.captureAiImmediate(event);
|
|
54
122
|
return;
|
|
55
123
|
}
|
|
@@ -403,7 +471,8 @@ const POSTHOG_PARAMS_MAP = {
|
|
|
403
471
|
posthogModelOverride: "modelOverride",
|
|
404
472
|
posthogProviderOverride: "providerOverride",
|
|
405
473
|
posthogCostOverride: "costOverride",
|
|
406
|
-
posthogCaptureImmediate: "captureImmediate"
|
|
474
|
+
posthogCaptureImmediate: "captureImmediate",
|
|
475
|
+
posthogOptions: "options"
|
|
407
476
|
};
|
|
408
477
|
function extractPosthogParams(body) {
|
|
409
478
|
const providerParams = {};
|
|
@@ -425,7 +494,7 @@ function addDefaults(params) {
|
|
|
425
494
|
}
|
|
426
495
|
//#endregion
|
|
427
496
|
//#region package.json
|
|
428
|
-
var version = "8.
|
|
497
|
+
var version = "8.21.1";
|
|
429
498
|
//#endregion
|
|
430
499
|
//#region src/serializeError.ts
|
|
431
500
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -620,6 +689,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
620
689
|
//#endregion
|
|
621
690
|
//#region src/captureAiGeneration.ts
|
|
622
691
|
/**
|
|
692
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
693
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
694
|
+
*/
|
|
695
|
+
const captureAiException = (client, error, properties, uuid$1) => {
|
|
696
|
+
if (isPostHogNode6(client)) {
|
|
697
|
+
client.captureException(error, {
|
|
698
|
+
properties,
|
|
699
|
+
uuid: uuid$1
|
|
700
|
+
});
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
client.captureException.call(client, error, void 0, properties, uuid$1);
|
|
704
|
+
};
|
|
705
|
+
/**
|
|
623
706
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
624
707
|
*
|
|
625
708
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -655,7 +738,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
655
738
|
if (client.options?.enableExceptionAutocapture) {
|
|
656
739
|
if (!shouldRedact) {
|
|
657
740
|
exceptionId = (0, _posthog_core.uuidv7)();
|
|
658
|
-
client
|
|
741
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
659
742
|
}
|
|
660
743
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
661
744
|
}
|
|
@@ -681,6 +764,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
681
764
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
682
765
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
683
766
|
};
|
|
767
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
684
768
|
const properties = {
|
|
685
769
|
$ai_lib: "posthog-ai",
|
|
686
770
|
$ai_lib_version: version,
|
|
@@ -700,7 +784,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
700
784
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
701
785
|
...options.properties,
|
|
702
786
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
703
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
704
787
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
705
788
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
706
789
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -709,9 +792,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
709
792
|
...costOverrideData
|
|
710
793
|
};
|
|
711
794
|
const event = {
|
|
712
|
-
distinctId:
|
|
795
|
+
distinctId: distinctId ?? traceId,
|
|
713
796
|
event: eventType,
|
|
714
|
-
properties,
|
|
797
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
715
798
|
groups: options.groups
|
|
716
799
|
};
|
|
717
800
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|
|
@@ -1156,7 +1239,7 @@ var WrappedMessages = class extends _anthropic_ai_sdk.default.Messages {
|
|
|
1156
1239
|
currentReasoningBlock = null;
|
|
1157
1240
|
const tool = toolsInProgress.get(chunk.index);
|
|
1158
1241
|
if (tool) {
|
|
1159
|
-
try {
|
|
1242
|
+
if (tool.inputString) try {
|
|
1160
1243
|
tool.block.function.arguments = JSON.parse(tool.inputString);
|
|
1161
1244
|
} catch (e) {
|
|
1162
1245
|
console.error("Error parsing tool input:", e);
|