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