@posthog/ai 8.20.1 → 8.21.0
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 +125 -36
- package/dist/adk/index.cjs.map +1 -1
- package/dist/adk/index.d.ts +7 -0
- package/dist/adk/index.mjs +126 -37
- package/dist/adk/index.mjs.map +1 -1
- package/dist/anthropic/index.cjs +115 -22
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +7 -0
- package/dist/anthropic/index.mjs +116 -23
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/claude-agent-sdk/index.cjs +131 -35
- 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 +132 -36
- package/dist/claude-agent-sdk/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +116 -23
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.d.ts +7 -0
- package/dist/gemini/index.mjs +116 -23
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/genkit/index.cjs +138 -43
- package/dist/genkit/index.cjs.map +1 -1
- package/dist/genkit/index.d.ts +7 -0
- package/dist/genkit/index.mjs +138 -43
- package/dist/genkit/index.mjs.map +1 -1
- package/dist/index.cjs +114 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.mjs +115 -22
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +99 -21
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.d.ts +7 -0
- package/dist/langchain/index.mjs +99 -21
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/langchain/middleware/index.cjs +99 -21
- package/dist/langchain/middleware/index.cjs.map +1 -1
- package/dist/langchain/middleware/index.d.ts +6 -0
- package/dist/langchain/middleware/index.mjs +99 -21
- package/dist/langchain/middleware/index.mjs.map +1 -1
- package/dist/openai/index.cjs +115 -22
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +7 -0
- package/dist/openai/index.mjs +116 -23
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +139 -60
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.d.ts +7 -0
- package/dist/openai-agents/index.mjs +139 -60
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/otel/index.browser.cjs +24 -14
- package/dist/otel/index.browser.cjs.map +1 -1
- package/dist/otel/index.browser.mjs +24 -14
- package/dist/otel/index.browser.mjs.map +1 -1
- package/dist/otel/index.cjs +24 -14
- package/dist/otel/index.cjs.map +1 -1
- package/dist/otel/index.d.ts +3 -3
- package/dist/otel/index.mjs +24 -14
- package/dist/otel/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +114 -21
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.d.ts +6 -0
- package/dist/vercel/index.mjs +115 -22
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/gemini/index.cjs
CHANGED
|
@@ -2,17 +2,85 @@ Object.defineProperties(exports, {
|
|
|
2
2
|
__esModule: { value: true },
|
|
3
3
|
[Symbol.toStringTag]: { value: "Module" }
|
|
4
4
|
});
|
|
5
|
+
let _posthog_core = require("@posthog/core");
|
|
5
6
|
let _google_genai = require("@google/genai");
|
|
6
7
|
let uuid = require("uuid");
|
|
7
|
-
let _posthog_core = require("@posthog/core");
|
|
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
|
}
|
|
@@ -176,13 +244,17 @@ var MediaTypeContext = class MediaTypeContext {
|
|
|
176
244
|
//#region src/sanitization/binary_content_redactor.ts
|
|
177
245
|
const STRONG_CONTEXT_MIN_LENGTH = 64;
|
|
178
246
|
const WEAK_CONTEXT_MIN_LENGTH = 1024;
|
|
247
|
+
const MAX_NODES$1 = 1e4;
|
|
248
|
+
const TRUNCATED$1 = "[Truncated]";
|
|
179
249
|
var BinaryContentRedactor = class {
|
|
180
250
|
constructor(recognizer = new Base64Recognizer()) {
|
|
181
251
|
this.recognizer = recognizer;
|
|
182
|
-
this.
|
|
252
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
253
|
+
this.remainingNodes = MAX_NODES$1;
|
|
183
254
|
}
|
|
184
255
|
redact(value, mediaType) {
|
|
185
|
-
this.
|
|
256
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
257
|
+
this.remainingNodes = MAX_NODES$1;
|
|
186
258
|
return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
|
|
187
259
|
}
|
|
188
260
|
/** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
|
|
@@ -194,18 +266,24 @@ var BinaryContentRedactor = class {
|
|
|
194
266
|
if (typeof value === "string") return this.redactLeaf(value, ctx);
|
|
195
267
|
if (typeof value !== "object") return value;
|
|
196
268
|
if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
|
|
197
|
-
if (this.
|
|
198
|
-
this.
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
269
|
+
if (this.ancestors.has(value)) return null;
|
|
270
|
+
if (this.remainingNodes <= 0) return TRUNCATED$1;
|
|
271
|
+
this.remainingNodes--;
|
|
272
|
+
this.ancestors.add(value);
|
|
273
|
+
try {
|
|
274
|
+
if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
|
|
275
|
+
const obj = value;
|
|
276
|
+
const out = {};
|
|
277
|
+
for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
|
|
278
|
+
value: this.walk(obj[k], new MediaTypeContext(obj, k)),
|
|
279
|
+
enumerable: true,
|
|
280
|
+
writable: true,
|
|
281
|
+
configurable: true
|
|
282
|
+
});
|
|
283
|
+
return out;
|
|
284
|
+
} finally {
|
|
285
|
+
this.ancestors.delete(value);
|
|
286
|
+
}
|
|
209
287
|
}
|
|
210
288
|
redactString(value, ctx) {
|
|
211
289
|
const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
|
|
@@ -443,7 +521,8 @@ const POSTHOG_PARAMS_MAP = {
|
|
|
443
521
|
posthogModelOverride: "modelOverride",
|
|
444
522
|
posthogProviderOverride: "providerOverride",
|
|
445
523
|
posthogCostOverride: "costOverride",
|
|
446
|
-
posthogCaptureImmediate: "captureImmediate"
|
|
524
|
+
posthogCaptureImmediate: "captureImmediate",
|
|
525
|
+
posthogOptions: "options"
|
|
447
526
|
};
|
|
448
527
|
function extractPosthogParams(body) {
|
|
449
528
|
const providerParams = {};
|
|
@@ -465,7 +544,7 @@ function addDefaults(params) {
|
|
|
465
544
|
}
|
|
466
545
|
//#endregion
|
|
467
546
|
//#region package.json
|
|
468
|
-
var version = "8.
|
|
547
|
+
var version = "8.21.0";
|
|
469
548
|
//#endregion
|
|
470
549
|
//#region src/serializeError.ts
|
|
471
550
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -660,6 +739,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
660
739
|
//#endregion
|
|
661
740
|
//#region src/captureAiGeneration.ts
|
|
662
741
|
/**
|
|
742
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
743
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
744
|
+
*/
|
|
745
|
+
const captureAiException = (client, error, properties, uuid$1) => {
|
|
746
|
+
if (isPostHogNode6(client)) {
|
|
747
|
+
client.captureException(error, {
|
|
748
|
+
properties,
|
|
749
|
+
uuid: uuid$1
|
|
750
|
+
});
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
client.captureException.call(client, error, void 0, properties, uuid$1);
|
|
754
|
+
};
|
|
755
|
+
/**
|
|
663
756
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
664
757
|
*
|
|
665
758
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -695,7 +788,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
695
788
|
if (client.options?.enableExceptionAutocapture) {
|
|
696
789
|
if (!shouldRedact) {
|
|
697
790
|
exceptionId = (0, _posthog_core.uuidv7)();
|
|
698
|
-
client
|
|
791
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
699
792
|
}
|
|
700
793
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
701
794
|
}
|
|
@@ -721,6 +814,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
721
814
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
722
815
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
723
816
|
};
|
|
817
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
724
818
|
const properties = {
|
|
725
819
|
$ai_lib: "posthog-ai",
|
|
726
820
|
$ai_lib_version: version,
|
|
@@ -740,7 +834,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
740
834
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
741
835
|
...options.properties,
|
|
742
836
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
743
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
744
837
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
745
838
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
746
839
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -749,9 +842,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
749
842
|
...costOverrideData
|
|
750
843
|
};
|
|
751
844
|
const event = {
|
|
752
|
-
distinctId:
|
|
845
|
+
distinctId: distinctId ?? traceId,
|
|
753
846
|
event: eventType,
|
|
754
|
-
properties,
|
|
847
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
755
848
|
groups: options.groups
|
|
756
849
|
};
|
|
757
850
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|