@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/openai/index.cjs
CHANGED
|
@@ -19,9 +19,77 @@ const isObject = (value) => {
|
|
|
19
19
|
function isFullAiCaptureEnabled(client) {
|
|
20
20
|
return client?.enableFullAiCapture === true;
|
|
21
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
function userDistinctId(value) {
|
|
28
|
+
if (!value) return;
|
|
29
|
+
const id = String(value);
|
|
30
|
+
return id.trim() === "" ? void 0 : id;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The caller's per-event options when they are an object. A missing or `null` value means no
|
|
34
|
+
* options. An array or any other value is reported and ignored, so it is never spread.
|
|
35
|
+
*
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
function readAiEventOptions(value) {
|
|
39
|
+
if (value === void 0 || value === null) return;
|
|
40
|
+
if (typeof value === "object" && !Array.isArray(value)) return value;
|
|
41
|
+
console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
|
|
45
|
+
* processing off and beats the caller's `process_person_profile`.
|
|
46
|
+
*
|
|
47
|
+
* posthog-node 6 takes the options as they are, so no option is written as a property.
|
|
48
|
+
* posthog-node 5 has no per-event options: each known option is sent as the property that version
|
|
49
|
+
* reads, and an unknown key is dropped.
|
|
50
|
+
*
|
|
51
|
+
* @internal
|
|
52
|
+
*/
|
|
53
|
+
function eventOptionFields(client, properties, options, personless) {
|
|
54
|
+
const userOptions = readAiEventOptions(options);
|
|
55
|
+
if (isPostHogNode6(client)) {
|
|
56
|
+
const eventOptions = {
|
|
57
|
+
...userOptions,
|
|
58
|
+
...personless ? { process_person_profile: false } : {}
|
|
59
|
+
};
|
|
60
|
+
return Object.keys(eventOptions).length > 0 ? {
|
|
61
|
+
properties,
|
|
62
|
+
options: eventOptions
|
|
63
|
+
} : { properties };
|
|
64
|
+
}
|
|
65
|
+
const withOptionProperties = {
|
|
66
|
+
...properties,
|
|
67
|
+
...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
|
|
68
|
+
};
|
|
69
|
+
if (personless) withOptionProperties.$process_person_profile = false;
|
|
70
|
+
return { properties: withOptionProperties };
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Whether the client is posthog-node 6 or later, the first major with per-event options.
|
|
74
|
+
*
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
function isPostHogNode6(client) {
|
|
78
|
+
return (0, _posthog_core.acceptsEventOptions)(client);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
|
|
82
|
+
* controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
|
|
83
|
+
* upgrade never changes how a 5.x client sends.
|
|
84
|
+
*
|
|
85
|
+
* @internal
|
|
86
|
+
*/
|
|
87
|
+
function usesAiLane(client) {
|
|
88
|
+
return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
|
|
89
|
+
}
|
|
22
90
|
/** @internal */
|
|
23
91
|
function captureAiEvent(client, event) {
|
|
24
|
-
if (
|
|
92
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
25
93
|
client.captureAi(event);
|
|
26
94
|
return;
|
|
27
95
|
}
|
|
@@ -29,7 +97,7 @@ function captureAiEvent(client, event) {
|
|
|
29
97
|
}
|
|
30
98
|
/** @internal */
|
|
31
99
|
async function captureAiEventImmediate(client, event) {
|
|
32
|
-
if (
|
|
100
|
+
if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
|
|
33
101
|
await client.captureAiImmediate(event);
|
|
34
102
|
return;
|
|
35
103
|
}
|
|
@@ -512,7 +580,8 @@ const POSTHOG_PARAMS_MAP = {
|
|
|
512
580
|
posthogModelOverride: "modelOverride",
|
|
513
581
|
posthogProviderOverride: "providerOverride",
|
|
514
582
|
posthogCostOverride: "costOverride",
|
|
515
|
-
posthogCaptureImmediate: "captureImmediate"
|
|
583
|
+
posthogCaptureImmediate: "captureImmediate",
|
|
584
|
+
posthogOptions: "options"
|
|
516
585
|
};
|
|
517
586
|
function extractPosthogParams(body) {
|
|
518
587
|
const providerParams = {};
|
|
@@ -566,7 +635,7 @@ function formatOpenAIResponsesInput(input, instructions) {
|
|
|
566
635
|
}
|
|
567
636
|
//#endregion
|
|
568
637
|
//#region package.json
|
|
569
|
-
var version = "8.
|
|
638
|
+
var version = "8.21.1";
|
|
570
639
|
//#endregion
|
|
571
640
|
//#region src/serializeError.ts
|
|
572
641
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -761,6 +830,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
761
830
|
//#endregion
|
|
762
831
|
//#region src/captureAiGeneration.ts
|
|
763
832
|
/**
|
|
833
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
834
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
835
|
+
*/
|
|
836
|
+
const captureAiException = (client, error, properties, uuid$1) => {
|
|
837
|
+
if (isPostHogNode6(client)) {
|
|
838
|
+
client.captureException(error, {
|
|
839
|
+
properties,
|
|
840
|
+
uuid: uuid$1
|
|
841
|
+
});
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
client.captureException.call(client, error, void 0, properties, uuid$1);
|
|
845
|
+
};
|
|
846
|
+
/**
|
|
764
847
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
765
848
|
*
|
|
766
849
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -796,7 +879,7 @@ const captureAiGeneration$1 = async (client, options) => {
|
|
|
796
879
|
if (client.options?.enableExceptionAutocapture) {
|
|
797
880
|
if (!shouldRedact) {
|
|
798
881
|
exceptionId = (0, _posthog_core.uuidv7)();
|
|
799
|
-
client
|
|
882
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
800
883
|
}
|
|
801
884
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
802
885
|
}
|
|
@@ -822,6 +905,7 @@ const captureAiGeneration$1 = async (client, options) => {
|
|
|
822
905
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
823
906
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
824
907
|
};
|
|
908
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
825
909
|
const properties = {
|
|
826
910
|
$ai_lib: "posthog-ai",
|
|
827
911
|
$ai_lib_version: version,
|
|
@@ -841,7 +925,6 @@ const captureAiGeneration$1 = async (client, options) => {
|
|
|
841
925
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
842
926
|
...options.properties,
|
|
843
927
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
844
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
845
928
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
846
929
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
847
930
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -850,9 +933,9 @@ const captureAiGeneration$1 = async (client, options) => {
|
|
|
850
933
|
...costOverrideData
|
|
851
934
|
};
|
|
852
935
|
const event = {
|
|
853
|
-
distinctId:
|
|
936
|
+
distinctId: distinctId ?? traceId,
|
|
854
937
|
event: eventType,
|
|
855
|
-
properties,
|
|
938
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
856
939
|
groups: options.groups
|
|
857
940
|
};
|
|
858
941
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|