@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
|
@@ -3,16 +3,84 @@ let _anthropic_ai_claude_agent_sdk = require("@anthropic-ai/claude-agent-sdk");
|
|
|
3
3
|
let uuid = require("uuid");
|
|
4
4
|
let _posthog_core = require("@posthog/core");
|
|
5
5
|
//#region package.json
|
|
6
|
-
var version = "8.
|
|
6
|
+
var version = "8.21.1";
|
|
7
7
|
//#endregion
|
|
8
8
|
//#region src/captureAiEvent.ts
|
|
9
9
|
/** @internal */
|
|
10
10
|
function isFullAiCaptureEnabled(client) {
|
|
11
11
|
return client?.enableFullAiCapture === true;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
function userDistinctId(value) {
|
|
19
|
+
if (!value) return;
|
|
20
|
+
const id = String(value);
|
|
21
|
+
return id.trim() === "" ? void 0 : id;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The caller's per-event options when they are an object. A missing or `null` value means no
|
|
25
|
+
* options. An array or any other value is reported and ignored, so it is never spread.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
function readAiEventOptions(value) {
|
|
30
|
+
if (value === void 0 || value === null) return;
|
|
31
|
+
if (typeof value === "object" && !Array.isArray(value)) return value;
|
|
32
|
+
console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
|
|
36
|
+
* processing off and beats the caller's `process_person_profile`.
|
|
37
|
+
*
|
|
38
|
+
* posthog-node 6 takes the options as they are, so no option is written as a property.
|
|
39
|
+
* posthog-node 5 has no per-event options: each known option is sent as the property that version
|
|
40
|
+
* reads, and an unknown key is dropped.
|
|
41
|
+
*
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
function eventOptionFields(client, properties, options, personless) {
|
|
45
|
+
const userOptions = readAiEventOptions(options);
|
|
46
|
+
if (isPostHogNode6(client)) {
|
|
47
|
+
const eventOptions = {
|
|
48
|
+
...userOptions,
|
|
49
|
+
...personless ? { process_person_profile: false } : {}
|
|
50
|
+
};
|
|
51
|
+
return Object.keys(eventOptions).length > 0 ? {
|
|
52
|
+
properties,
|
|
53
|
+
options: eventOptions
|
|
54
|
+
} : { properties };
|
|
55
|
+
}
|
|
56
|
+
const withOptionProperties = {
|
|
57
|
+
...properties,
|
|
58
|
+
...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
|
|
59
|
+
};
|
|
60
|
+
if (personless) withOptionProperties.$process_person_profile = false;
|
|
61
|
+
return { properties: withOptionProperties };
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Whether the client is posthog-node 6 or later, the first major with per-event options.
|
|
65
|
+
*
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
function isPostHogNode6(client) {
|
|
69
|
+
return (0, _posthog_core.acceptsEventOptions)(client);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
|
|
73
|
+
* controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
|
|
74
|
+
* upgrade never changes how a 5.x client sends.
|
|
75
|
+
*
|
|
76
|
+
* @internal
|
|
77
|
+
*/
|
|
78
|
+
function usesAiLane(client) {
|
|
79
|
+
return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
|
|
80
|
+
}
|
|
13
81
|
/** @internal */
|
|
14
82
|
function captureAiEvent(client, event) {
|
|
15
|
-
if (
|
|
83
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
16
84
|
client.captureAi(event);
|
|
17
85
|
return;
|
|
18
86
|
}
|
|
@@ -20,7 +88,7 @@ function captureAiEvent(client, event) {
|
|
|
20
88
|
}
|
|
21
89
|
/** @internal */
|
|
22
90
|
async function captureAiEventImmediate(client, event) {
|
|
23
|
-
if (
|
|
91
|
+
if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
|
|
24
92
|
await client.captureAiImmediate(event);
|
|
25
93
|
return;
|
|
26
94
|
}
|
|
@@ -507,6 +575,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
507
575
|
//#endregion
|
|
508
576
|
//#region src/captureAiGeneration.ts
|
|
509
577
|
/**
|
|
578
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
579
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
580
|
+
*/
|
|
581
|
+
const captureAiException = (client, error, properties, uuid$1) => {
|
|
582
|
+
if (isPostHogNode6(client)) {
|
|
583
|
+
client.captureException(error, {
|
|
584
|
+
properties,
|
|
585
|
+
uuid: uuid$1
|
|
586
|
+
});
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
client.captureException.call(client, error, void 0, properties, uuid$1);
|
|
590
|
+
};
|
|
591
|
+
/**
|
|
510
592
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
511
593
|
*
|
|
512
594
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -542,7 +624,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
542
624
|
if (client.options?.enableExceptionAutocapture) {
|
|
543
625
|
if (!shouldRedact) {
|
|
544
626
|
exceptionId = (0, _posthog_core.uuidv7)();
|
|
545
|
-
client
|
|
627
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
546
628
|
}
|
|
547
629
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
548
630
|
}
|
|
@@ -568,6 +650,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
568
650
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
569
651
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
570
652
|
};
|
|
653
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
571
654
|
const properties = {
|
|
572
655
|
$ai_lib: "posthog-ai",
|
|
573
656
|
$ai_lib_version: version,
|
|
@@ -587,7 +670,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
587
670
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
588
671
|
...options.properties,
|
|
589
672
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
590
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
591
673
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
592
674
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
593
675
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -596,9 +678,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
596
678
|
...costOverrideData
|
|
597
679
|
};
|
|
598
680
|
const event = {
|
|
599
|
-
distinctId:
|
|
681
|
+
distinctId: distinctId ?? traceId,
|
|
600
682
|
event: eventType,
|
|
601
|
-
properties,
|
|
683
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
602
684
|
groups: options.groups
|
|
603
685
|
};
|
|
604
686
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|
|
@@ -900,6 +982,10 @@ var PostHogClaudeAgentProcessor = class {
|
|
|
900
982
|
properties: {
|
|
901
983
|
...this._traceOptions.properties,
|
|
902
984
|
...posthog?.properties
|
|
985
|
+
},
|
|
986
|
+
options: {
|
|
987
|
+
...readAiEventOptions(this._traceOptions.options),
|
|
988
|
+
...readAiEventOptions(posthog?.options)
|
|
903
989
|
}
|
|
904
990
|
};
|
|
905
991
|
for (const key of [
|
|
@@ -1142,6 +1228,7 @@ var PostHogClaudeAgentProcessor = class {
|
|
|
1142
1228
|
usage: generation.usage,
|
|
1143
1229
|
stopReason: generation.stopReason,
|
|
1144
1230
|
groups: trace.groups,
|
|
1231
|
+
options: trace.options,
|
|
1145
1232
|
privacyMode: trace.privacyMode ?? false,
|
|
1146
1233
|
captureImmediate: this._captureImmediate,
|
|
1147
1234
|
onError: this._onError,
|
|
@@ -1256,20 +1343,20 @@ var PostHogClaudeAgentProcessor = class {
|
|
|
1256
1343
|
}
|
|
1257
1344
|
}
|
|
1258
1345
|
async _captureLifecycleEvent(event, distinctId, state, trace, properties) {
|
|
1346
|
+
const eventProperties = {
|
|
1347
|
+
$ai_lib: "posthog-ai",
|
|
1348
|
+
$ai_lib_version: version,
|
|
1349
|
+
$ai_framework: FRAMEWORK,
|
|
1350
|
+
$ai_provider: PROVIDER,
|
|
1351
|
+
$ai_trace_id: state.traceId,
|
|
1352
|
+
...state.sessionId ? { $ai_session_id: state.sessionId } : {},
|
|
1353
|
+
...properties,
|
|
1354
|
+
...trace.properties
|
|
1355
|
+
};
|
|
1259
1356
|
const message = {
|
|
1260
1357
|
distinctId: distinctId ?? state.traceId,
|
|
1261
1358
|
event,
|
|
1262
|
-
|
|
1263
|
-
$ai_lib: "posthog-ai",
|
|
1264
|
-
$ai_lib_version: version,
|
|
1265
|
-
$ai_framework: FRAMEWORK,
|
|
1266
|
-
$ai_provider: PROVIDER,
|
|
1267
|
-
$ai_trace_id: state.traceId,
|
|
1268
|
-
...state.sessionId ? { $ai_session_id: state.sessionId } : {},
|
|
1269
|
-
...properties,
|
|
1270
|
-
...trace.properties,
|
|
1271
|
-
...distinctId ? {} : { $process_person_profile: false }
|
|
1272
|
-
},
|
|
1359
|
+
...eventOptionFields(this._client, eventProperties, trace.options, !distinctId),
|
|
1273
1360
|
groups: trace.groups
|
|
1274
1361
|
};
|
|
1275
1362
|
try {
|
|
@@ -1309,10 +1396,9 @@ function readUsage(usage) {
|
|
|
1309
1396
|
function resolveDistinctId(resolver, result) {
|
|
1310
1397
|
if (typeof resolver === "function") {
|
|
1311
1398
|
if (!result) return;
|
|
1312
|
-
|
|
1313
|
-
return resolved ? String(resolved) : void 0;
|
|
1399
|
+
return userDistinctId(resolver(result));
|
|
1314
1400
|
}
|
|
1315
|
-
return
|
|
1401
|
+
return userDistinctId(resolver);
|
|
1316
1402
|
}
|
|
1317
1403
|
//#endregion
|
|
1318
1404
|
//#region src/claude-agent-sdk/index.ts
|