@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/genkit/index.cjs
CHANGED
|
@@ -1,17 +1,85 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
let uuid = require("uuid");
|
|
3
2
|
let _posthog_core = require("@posthog/core");
|
|
3
|
+
let uuid = require("uuid");
|
|
4
4
|
//#region package.json
|
|
5
|
-
var version = "8.
|
|
5
|
+
var version = "8.21.1";
|
|
6
6
|
//#endregion
|
|
7
7
|
//#region src/captureAiEvent.ts
|
|
8
8
|
/** @internal */
|
|
9
9
|
function isFullAiCaptureEnabled(client) {
|
|
10
10
|
return client?.enableFullAiCapture === true;
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
function userDistinctId(value) {
|
|
18
|
+
if (!value) return;
|
|
19
|
+
const id = String(value);
|
|
20
|
+
return id.trim() === "" ? void 0 : id;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The caller's per-event options when they are an object. A missing or `null` value means no
|
|
24
|
+
* options. An array or any other value is reported and ignored, so it is never spread.
|
|
25
|
+
*
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
function readAiEventOptions(value) {
|
|
29
|
+
if (value === void 0 || value === null) return;
|
|
30
|
+
if (typeof value === "object" && !Array.isArray(value)) return value;
|
|
31
|
+
console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
|
|
35
|
+
* processing off and beats the caller's `process_person_profile`.
|
|
36
|
+
*
|
|
37
|
+
* posthog-node 6 takes the options as they are, so no option is written as a property.
|
|
38
|
+
* posthog-node 5 has no per-event options: each known option is sent as the property that version
|
|
39
|
+
* reads, and an unknown key is dropped.
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
function eventOptionFields(client, properties, options, personless) {
|
|
44
|
+
const userOptions = readAiEventOptions(options);
|
|
45
|
+
if (isPostHogNode6(client)) {
|
|
46
|
+
const eventOptions = {
|
|
47
|
+
...userOptions,
|
|
48
|
+
...personless ? { process_person_profile: false } : {}
|
|
49
|
+
};
|
|
50
|
+
return Object.keys(eventOptions).length > 0 ? {
|
|
51
|
+
properties,
|
|
52
|
+
options: eventOptions
|
|
53
|
+
} : { properties };
|
|
54
|
+
}
|
|
55
|
+
const withOptionProperties = {
|
|
56
|
+
...properties,
|
|
57
|
+
...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
|
|
58
|
+
};
|
|
59
|
+
if (personless) withOptionProperties.$process_person_profile = false;
|
|
60
|
+
return { properties: withOptionProperties };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Whether the client is posthog-node 6 or later, the first major with per-event options.
|
|
64
|
+
*
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
function isPostHogNode6(client) {
|
|
68
|
+
return (0, _posthog_core.acceptsEventOptions)(client);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
|
|
72
|
+
* controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
|
|
73
|
+
* upgrade never changes how a 5.x client sends.
|
|
74
|
+
*
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
function usesAiLane(client) {
|
|
78
|
+
return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
|
|
79
|
+
}
|
|
12
80
|
/** @internal */
|
|
13
81
|
function captureAiEvent(client, event) {
|
|
14
|
-
if (
|
|
82
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
15
83
|
client.captureAi(event);
|
|
16
84
|
return;
|
|
17
85
|
}
|
|
@@ -19,7 +87,7 @@ function captureAiEvent(client, event) {
|
|
|
19
87
|
}
|
|
20
88
|
/** @internal */
|
|
21
89
|
async function captureAiEventImmediate(client, event) {
|
|
22
|
-
if (
|
|
90
|
+
if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
|
|
23
91
|
await client.captureAiImmediate(event);
|
|
24
92
|
return;
|
|
25
93
|
}
|
|
@@ -521,6 +589,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
521
589
|
//#endregion
|
|
522
590
|
//#region src/captureAiGeneration.ts
|
|
523
591
|
/**
|
|
592
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
593
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
594
|
+
*/
|
|
595
|
+
const captureAiException = (client, error, properties, uuid$1) => {
|
|
596
|
+
if (isPostHogNode6(client)) {
|
|
597
|
+
client.captureException(error, {
|
|
598
|
+
properties,
|
|
599
|
+
uuid: uuid$1
|
|
600
|
+
});
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
client.captureException.call(client, error, void 0, properties, uuid$1);
|
|
604
|
+
};
|
|
605
|
+
/**
|
|
524
606
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
525
607
|
*
|
|
526
608
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -556,7 +638,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
556
638
|
if (client.options?.enableExceptionAutocapture) {
|
|
557
639
|
if (!shouldRedact) {
|
|
558
640
|
exceptionId = (0, _posthog_core.uuidv7)();
|
|
559
|
-
client
|
|
641
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
560
642
|
}
|
|
561
643
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
562
644
|
}
|
|
@@ -582,6 +664,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
582
664
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
583
665
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
584
666
|
};
|
|
667
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
585
668
|
const properties = {
|
|
586
669
|
$ai_lib: "posthog-ai",
|
|
587
670
|
$ai_lib_version: version,
|
|
@@ -601,7 +684,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
601
684
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
602
685
|
...options.properties,
|
|
603
686
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
604
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
605
687
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
606
688
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
607
689
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -610,9 +692,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
610
692
|
...costOverrideData
|
|
611
693
|
};
|
|
612
694
|
const event = {
|
|
613
|
-
distinctId:
|
|
695
|
+
distinctId: distinctId ?? traceId,
|
|
614
696
|
event: eventType,
|
|
615
|
-
properties,
|
|
697
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
616
698
|
groups: options.groups
|
|
617
699
|
};
|
|
618
700
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|
|
@@ -759,15 +841,16 @@ var PostHogGenkitSpanProcessor = class {
|
|
|
759
841
|
constructor(options) {
|
|
760
842
|
this._spanDistinctIds = /* @__PURE__ */ new WeakMap();
|
|
761
843
|
this._client = options.client;
|
|
762
|
-
this._distinctId = options.distinctId;
|
|
844
|
+
this._distinctId = userDistinctId(options.distinctId);
|
|
763
845
|
this._privacyMode = options.privacyMode ?? false;
|
|
764
846
|
this._groups = options.groups;
|
|
765
847
|
this._properties = options.properties ?? {};
|
|
848
|
+
this._eventOptions = options.options;
|
|
766
849
|
this._onError = options.onError;
|
|
767
850
|
}
|
|
768
851
|
onStart(span) {
|
|
769
852
|
try {
|
|
770
|
-
const distinctId = this._distinctId ?? this._client.getContext?.()?.distinctId;
|
|
853
|
+
const distinctId = this._distinctId ?? userDistinctId(this._client.getContext?.()?.distinctId);
|
|
771
854
|
if (distinctId !== void 0) this._spanDistinctIds.set(span, distinctId);
|
|
772
855
|
} catch (error) {
|
|
773
856
|
this._handleError(error);
|
|
@@ -823,6 +906,7 @@ var PostHogGenkitSpanProcessor = class {
|
|
|
823
906
|
},
|
|
824
907
|
stopReason: finishReason,
|
|
825
908
|
groups: this._groups,
|
|
909
|
+
options: this._eventOptions,
|
|
826
910
|
privacyMode: this._privacyMode,
|
|
827
911
|
onError: this._onError,
|
|
828
912
|
error: spanError(span),
|
|
@@ -850,6 +934,7 @@ var PostHogGenkitSpanProcessor = class {
|
|
|
850
934
|
output: null,
|
|
851
935
|
latency: latencySeconds(span),
|
|
852
936
|
groups: this._groups,
|
|
937
|
+
options: this._eventOptions,
|
|
853
938
|
privacyMode: this._privacyMode,
|
|
854
939
|
onError: this._onError,
|
|
855
940
|
error: spanError(span),
|
|
@@ -866,28 +951,28 @@ var PostHogGenkitSpanProcessor = class {
|
|
|
866
951
|
const isRoot = span.attributes["genkit:isRoot"] === true;
|
|
867
952
|
const { traceId, spanId, parentId } = spanIds(span);
|
|
868
953
|
const error = spanError(span);
|
|
954
|
+
const properties = {
|
|
955
|
+
$ai_lib: "posthog-ai",
|
|
956
|
+
$ai_lib_version: version,
|
|
957
|
+
$ai_framework: "genkit",
|
|
958
|
+
$ai_trace_id: traceId,
|
|
959
|
+
$ai_span_id: spanId,
|
|
960
|
+
...!isRoot && parentId ? { $ai_parent_id: parentId } : {},
|
|
961
|
+
$ai_span_name: span.name,
|
|
962
|
+
$ai_span_type: span.attributes["genkit:metadata:subtype"] ?? span.attributes["genkit:type"],
|
|
963
|
+
$ai_input_state: this._formatState(span, "genkit:input"),
|
|
964
|
+
$ai_output_state: this._formatState(span, "genkit:output"),
|
|
965
|
+
$ai_latency: latencySeconds(span),
|
|
966
|
+
...error ? {
|
|
967
|
+
$ai_is_error: true,
|
|
968
|
+
$ai_error: formatAiError(error, isPrivacyModeEnabled(this._client, this._privacyMode))
|
|
969
|
+
} : {},
|
|
970
|
+
...this._properties
|
|
971
|
+
};
|
|
869
972
|
captureAiEvent(this._client, {
|
|
870
973
|
distinctId: distinctId ?? traceId,
|
|
871
974
|
event: isRoot ? "$ai_trace" : "$ai_span",
|
|
872
|
-
properties
|
|
873
|
-
$ai_lib: "posthog-ai",
|
|
874
|
-
$ai_lib_version: version,
|
|
875
|
-
$ai_framework: "genkit",
|
|
876
|
-
$ai_trace_id: traceId,
|
|
877
|
-
$ai_span_id: spanId,
|
|
878
|
-
...!isRoot && parentId ? { $ai_parent_id: parentId } : {},
|
|
879
|
-
$ai_span_name: span.name,
|
|
880
|
-
$ai_span_type: span.attributes["genkit:metadata:subtype"] ?? span.attributes["genkit:type"],
|
|
881
|
-
$ai_input_state: this._formatState(span, "genkit:input"),
|
|
882
|
-
$ai_output_state: this._formatState(span, "genkit:output"),
|
|
883
|
-
$ai_latency: latencySeconds(span),
|
|
884
|
-
...error ? {
|
|
885
|
-
$ai_is_error: true,
|
|
886
|
-
$ai_error: formatAiError(error, isPrivacyModeEnabled(this._client, this._privacyMode))
|
|
887
|
-
} : {},
|
|
888
|
-
...this._properties,
|
|
889
|
-
...distinctId ? {} : { $process_person_profile: false }
|
|
890
|
-
},
|
|
975
|
+
...eventOptionFields(this._client, properties, this._eventOptions, !distinctId),
|
|
891
976
|
groups: this._groups
|
|
892
977
|
});
|
|
893
978
|
}
|