@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/openai/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import OpenAIOrignal, { APIPromise, AzureOpenAI, ClientOptions, OpenAI } from "openai";
|
|
2
|
+
import { JsonType } from "@posthog/core";
|
|
2
3
|
import { Stream } from "openai/streaming";
|
|
3
4
|
import { PostHog } from "posthog-node";
|
|
4
5
|
import "@anthropic-ai/sdk";
|
|
@@ -16,6 +17,12 @@ interface MonitoringEventPropertiesWithDefaults {
|
|
|
16
17
|
providerOverride?: string;
|
|
17
18
|
costOverride?: CostOverride;
|
|
18
19
|
captureImmediate?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Per-event options for posthog-node, such as `{ process_person_profile: false }`. posthog-node 5
|
|
22
|
+
* has no per-event options, so each option it knows is sent the way that version reads it, and
|
|
23
|
+
* other keys are dropped. An event with no distinct id always has person processing turned off.
|
|
24
|
+
*/
|
|
25
|
+
options?: Record<string, JsonType>;
|
|
19
26
|
}
|
|
20
27
|
type MonitoringEventProperties = Partial<MonitoringEventPropertiesWithDefaults>;
|
|
21
28
|
type MonitoringParams = { [K in keyof MonitoringEventProperties as `posthog${Capitalize<string & K>}`]: MonitoringEventProperties[K]; };
|
package/dist/openai/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AzureOpenAI, OpenAI } from "openai";
|
|
2
2
|
import { v4 } from "uuid";
|
|
3
|
-
import { toJsonSafeValue, uuidv7 } from "@posthog/core";
|
|
3
|
+
import { acceptsEventOptions, eventOptionsAsLegacyProperties, toJsonSafeValue, uuidv7 } from "@posthog/core";
|
|
4
4
|
import { Stream } from "openai/streaming";
|
|
5
5
|
//#region src/typeGuards.ts
|
|
6
6
|
const isString = (value) => {
|
|
@@ -15,9 +15,77 @@ const isObject = (value) => {
|
|
|
15
15
|
function isFullAiCaptureEnabled(client) {
|
|
16
16
|
return client?.enableFullAiCapture === true;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
function userDistinctId(value) {
|
|
24
|
+
if (!value) return;
|
|
25
|
+
const id = String(value);
|
|
26
|
+
return id.trim() === "" ? void 0 : id;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The caller's per-event options when they are an object. A missing or `null` value means no
|
|
30
|
+
* options. An array or any other value is reported and ignored, so it is never spread.
|
|
31
|
+
*
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
function readAiEventOptions(value) {
|
|
35
|
+
if (value === void 0 || value === null) return;
|
|
36
|
+
if (typeof value === "object" && !Array.isArray(value)) return value;
|
|
37
|
+
console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
|
|
41
|
+
* processing off and beats the caller's `process_person_profile`.
|
|
42
|
+
*
|
|
43
|
+
* posthog-node 6 takes the options as they are, so no option is written as a property.
|
|
44
|
+
* posthog-node 5 has no per-event options: each known option is sent as the property that version
|
|
45
|
+
* reads, and an unknown key is dropped.
|
|
46
|
+
*
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
function eventOptionFields(client, properties, options, personless) {
|
|
50
|
+
const userOptions = readAiEventOptions(options);
|
|
51
|
+
if (isPostHogNode6(client)) {
|
|
52
|
+
const eventOptions = {
|
|
53
|
+
...userOptions,
|
|
54
|
+
...personless ? { process_person_profile: false } : {}
|
|
55
|
+
};
|
|
56
|
+
return Object.keys(eventOptions).length > 0 ? {
|
|
57
|
+
properties,
|
|
58
|
+
options: eventOptions
|
|
59
|
+
} : { properties };
|
|
60
|
+
}
|
|
61
|
+
const withOptionProperties = {
|
|
62
|
+
...properties,
|
|
63
|
+
...eventOptionsAsLegacyProperties(userOptions)
|
|
64
|
+
};
|
|
65
|
+
if (personless) withOptionProperties.$process_person_profile = false;
|
|
66
|
+
return { properties: withOptionProperties };
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Whether the client is posthog-node 6 or later, the first major with per-event options.
|
|
70
|
+
*
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
73
|
+
function isPostHogNode6(client) {
|
|
74
|
+
return acceptsEventOptions(client);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
|
|
78
|
+
* controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
|
|
79
|
+
* upgrade never changes how a 5.x client sends.
|
|
80
|
+
*
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
function usesAiLane(client) {
|
|
84
|
+
return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
|
|
85
|
+
}
|
|
18
86
|
/** @internal */
|
|
19
87
|
function captureAiEvent(client, event) {
|
|
20
|
-
if (
|
|
88
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
21
89
|
client.captureAi(event);
|
|
22
90
|
return;
|
|
23
91
|
}
|
|
@@ -25,7 +93,7 @@ function captureAiEvent(client, event) {
|
|
|
25
93
|
}
|
|
26
94
|
/** @internal */
|
|
27
95
|
async function captureAiEventImmediate(client, event) {
|
|
28
|
-
if (
|
|
96
|
+
if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
|
|
29
97
|
await client.captureAiImmediate(event);
|
|
30
98
|
return;
|
|
31
99
|
}
|
|
@@ -173,13 +241,17 @@ var MediaTypeContext = class MediaTypeContext {
|
|
|
173
241
|
//#region src/sanitization/binary_content_redactor.ts
|
|
174
242
|
const STRONG_CONTEXT_MIN_LENGTH = 64;
|
|
175
243
|
const WEAK_CONTEXT_MIN_LENGTH = 1024;
|
|
244
|
+
const MAX_NODES$1 = 1e4;
|
|
245
|
+
const TRUNCATED$1 = "[Truncated]";
|
|
176
246
|
var BinaryContentRedactor = class {
|
|
177
247
|
constructor(recognizer = new Base64Recognizer()) {
|
|
178
248
|
this.recognizer = recognizer;
|
|
179
|
-
this.
|
|
249
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
250
|
+
this.remainingNodes = MAX_NODES$1;
|
|
180
251
|
}
|
|
181
252
|
redact(value, mediaType) {
|
|
182
|
-
this.
|
|
253
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
254
|
+
this.remainingNodes = MAX_NODES$1;
|
|
183
255
|
return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
|
|
184
256
|
}
|
|
185
257
|
/** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
|
|
@@ -191,18 +263,24 @@ var BinaryContentRedactor = class {
|
|
|
191
263
|
if (typeof value === "string") return this.redactLeaf(value, ctx);
|
|
192
264
|
if (typeof value !== "object") return value;
|
|
193
265
|
if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
|
|
194
|
-
if (this.
|
|
195
|
-
this.
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
266
|
+
if (this.ancestors.has(value)) return null;
|
|
267
|
+
if (this.remainingNodes <= 0) return TRUNCATED$1;
|
|
268
|
+
this.remainingNodes--;
|
|
269
|
+
this.ancestors.add(value);
|
|
270
|
+
try {
|
|
271
|
+
if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
|
|
272
|
+
const obj = value;
|
|
273
|
+
const out = {};
|
|
274
|
+
for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
|
|
275
|
+
value: this.walk(obj[k], new MediaTypeContext(obj, k)),
|
|
276
|
+
enumerable: true,
|
|
277
|
+
writable: true,
|
|
278
|
+
configurable: true
|
|
279
|
+
});
|
|
280
|
+
return out;
|
|
281
|
+
} finally {
|
|
282
|
+
this.ancestors.delete(value);
|
|
283
|
+
}
|
|
206
284
|
}
|
|
207
285
|
redactString(value, ctx) {
|
|
208
286
|
const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
|
|
@@ -498,7 +576,8 @@ const POSTHOG_PARAMS_MAP = {
|
|
|
498
576
|
posthogModelOverride: "modelOverride",
|
|
499
577
|
posthogProviderOverride: "providerOverride",
|
|
500
578
|
posthogCostOverride: "costOverride",
|
|
501
|
-
posthogCaptureImmediate: "captureImmediate"
|
|
579
|
+
posthogCaptureImmediate: "captureImmediate",
|
|
580
|
+
posthogOptions: "options"
|
|
502
581
|
};
|
|
503
582
|
function extractPosthogParams(body) {
|
|
504
583
|
const providerParams = {};
|
|
@@ -552,7 +631,7 @@ function formatOpenAIResponsesInput(input, instructions) {
|
|
|
552
631
|
}
|
|
553
632
|
//#endregion
|
|
554
633
|
//#region package.json
|
|
555
|
-
var version = "8.
|
|
634
|
+
var version = "8.21.0";
|
|
556
635
|
//#endregion
|
|
557
636
|
//#region src/serializeError.ts
|
|
558
637
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -747,6 +826,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
747
826
|
//#endregion
|
|
748
827
|
//#region src/captureAiGeneration.ts
|
|
749
828
|
/**
|
|
829
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
830
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
831
|
+
*/
|
|
832
|
+
const captureAiException = (client, error, properties, uuid) => {
|
|
833
|
+
if (isPostHogNode6(client)) {
|
|
834
|
+
client.captureException(error, {
|
|
835
|
+
properties,
|
|
836
|
+
uuid
|
|
837
|
+
});
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
client.captureException.call(client, error, void 0, properties, uuid);
|
|
841
|
+
};
|
|
842
|
+
/**
|
|
750
843
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
751
844
|
*
|
|
752
845
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -782,7 +875,7 @@ const captureAiGeneration$1 = async (client, options) => {
|
|
|
782
875
|
if (client.options?.enableExceptionAutocapture) {
|
|
783
876
|
if (!shouldRedact) {
|
|
784
877
|
exceptionId = uuidv7();
|
|
785
|
-
client
|
|
878
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
786
879
|
}
|
|
787
880
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
788
881
|
}
|
|
@@ -808,6 +901,7 @@ const captureAiGeneration$1 = async (client, options) => {
|
|
|
808
901
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
809
902
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
810
903
|
};
|
|
904
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
811
905
|
const properties = {
|
|
812
906
|
$ai_lib: "posthog-ai",
|
|
813
907
|
$ai_lib_version: version,
|
|
@@ -827,7 +921,6 @@ const captureAiGeneration$1 = async (client, options) => {
|
|
|
827
921
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
828
922
|
...options.properties,
|
|
829
923
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
830
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
831
924
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
832
925
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
833
926
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -836,9 +929,9 @@ const captureAiGeneration$1 = async (client, options) => {
|
|
|
836
929
|
...costOverrideData
|
|
837
930
|
};
|
|
838
931
|
const event = {
|
|
839
|
-
distinctId:
|
|
932
|
+
distinctId: distinctId ?? traceId,
|
|
840
933
|
event: eventType,
|
|
841
|
-
properties,
|
|
934
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
842
935
|
groups: options.groups
|
|
843
936
|
};
|
|
844
937
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|