@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/langchain/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
require("uuid");
|
|
3
|
+
let _posthog_core = require("@posthog/core");
|
|
3
4
|
let _langchain_core_callbacks_base = require("@langchain/core/callbacks/base");
|
|
4
5
|
//#region src/typeGuards.ts
|
|
5
6
|
const isObject = (value) => {
|
|
@@ -11,9 +12,77 @@ const isObject = (value) => {
|
|
|
11
12
|
function isFullAiCaptureEnabled(client) {
|
|
12
13
|
return client?.enableFullAiCapture === true;
|
|
13
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
|
|
17
|
+
*
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
function userDistinctId(value) {
|
|
21
|
+
if (!value) return;
|
|
22
|
+
const id = String(value);
|
|
23
|
+
return id.trim() === "" ? void 0 : id;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The caller's per-event options when they are an object. A missing or `null` value means no
|
|
27
|
+
* options. An array or any other value is reported and ignored, so it is never spread.
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
function readAiEventOptions(value) {
|
|
32
|
+
if (value === void 0 || value === null) return;
|
|
33
|
+
if (typeof value === "object" && !Array.isArray(value)) return value;
|
|
34
|
+
console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
|
|
38
|
+
* processing off and beats the caller's `process_person_profile`.
|
|
39
|
+
*
|
|
40
|
+
* posthog-node 6 takes the options as they are, so no option is written as a property.
|
|
41
|
+
* posthog-node 5 has no per-event options: each known option is sent as the property that version
|
|
42
|
+
* reads, and an unknown key is dropped.
|
|
43
|
+
*
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
function eventOptionFields(client, properties, options, personless) {
|
|
47
|
+
const userOptions = readAiEventOptions(options);
|
|
48
|
+
if (isPostHogNode6(client)) {
|
|
49
|
+
const eventOptions = {
|
|
50
|
+
...userOptions,
|
|
51
|
+
...personless ? { process_person_profile: false } : {}
|
|
52
|
+
};
|
|
53
|
+
return Object.keys(eventOptions).length > 0 ? {
|
|
54
|
+
properties,
|
|
55
|
+
options: eventOptions
|
|
56
|
+
} : { properties };
|
|
57
|
+
}
|
|
58
|
+
const withOptionProperties = {
|
|
59
|
+
...properties,
|
|
60
|
+
...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
|
|
61
|
+
};
|
|
62
|
+
if (personless) withOptionProperties.$process_person_profile = false;
|
|
63
|
+
return { properties: withOptionProperties };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Whether the client is posthog-node 6 or later, the first major with per-event options.
|
|
67
|
+
*
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
function isPostHogNode6(client) {
|
|
71
|
+
return (0, _posthog_core.acceptsEventOptions)(client);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
|
|
75
|
+
* controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
|
|
76
|
+
* upgrade never changes how a 5.x client sends.
|
|
77
|
+
*
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
function usesAiLane(client) {
|
|
81
|
+
return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
|
|
82
|
+
}
|
|
14
83
|
/** @internal */
|
|
15
84
|
function captureAiEvent(client, event) {
|
|
16
|
-
if (
|
|
85
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
17
86
|
client.captureAi(event);
|
|
18
87
|
return;
|
|
19
88
|
}
|
|
@@ -161,13 +230,17 @@ var MediaTypeContext = class MediaTypeContext {
|
|
|
161
230
|
//#region src/sanitization/binary_content_redactor.ts
|
|
162
231
|
const STRONG_CONTEXT_MIN_LENGTH = 64;
|
|
163
232
|
const WEAK_CONTEXT_MIN_LENGTH = 1024;
|
|
233
|
+
const MAX_NODES = 1e4;
|
|
234
|
+
const TRUNCATED = "[Truncated]";
|
|
164
235
|
var BinaryContentRedactor = class {
|
|
165
236
|
constructor(recognizer = new Base64Recognizer()) {
|
|
166
237
|
this.recognizer = recognizer;
|
|
167
|
-
this.
|
|
238
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
239
|
+
this.remainingNodes = MAX_NODES;
|
|
168
240
|
}
|
|
169
241
|
redact(value, mediaType) {
|
|
170
|
-
this.
|
|
242
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
243
|
+
this.remainingNodes = MAX_NODES;
|
|
171
244
|
return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
|
|
172
245
|
}
|
|
173
246
|
/** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
|
|
@@ -179,18 +252,24 @@ var BinaryContentRedactor = class {
|
|
|
179
252
|
if (typeof value === "string") return this.redactLeaf(value, ctx);
|
|
180
253
|
if (typeof value !== "object") return value;
|
|
181
254
|
if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
|
|
182
|
-
if (this.
|
|
183
|
-
this.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
255
|
+
if (this.ancestors.has(value)) return null;
|
|
256
|
+
if (this.remainingNodes <= 0) return TRUNCATED;
|
|
257
|
+
this.remainingNodes--;
|
|
258
|
+
this.ancestors.add(value);
|
|
259
|
+
try {
|
|
260
|
+
if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
|
|
261
|
+
const obj = value;
|
|
262
|
+
const out = {};
|
|
263
|
+
for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
|
|
264
|
+
value: this.walk(obj[k], new MediaTypeContext(obj, k)),
|
|
265
|
+
enumerable: true,
|
|
266
|
+
writable: true,
|
|
267
|
+
configurable: true
|
|
268
|
+
});
|
|
269
|
+
return out;
|
|
270
|
+
} finally {
|
|
271
|
+
this.ancestors.delete(value);
|
|
272
|
+
}
|
|
194
273
|
}
|
|
195
274
|
redactString(value, ctx) {
|
|
196
275
|
const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
|
|
@@ -308,7 +387,7 @@ function redactMcpToolCredentials(tools) {
|
|
|
308
387
|
}
|
|
309
388
|
//#endregion
|
|
310
389
|
//#region package.json
|
|
311
|
-
var version = "8.
|
|
390
|
+
var version = "8.21.0";
|
|
312
391
|
//#endregion
|
|
313
392
|
//#region src/serializeError.ts
|
|
314
393
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -511,9 +590,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
|
|
|
511
590
|
this.runs = {};
|
|
512
591
|
this.parentTree = {};
|
|
513
592
|
this.client = options.client;
|
|
514
|
-
this.distinctId = options.distinctId;
|
|
593
|
+
this.distinctId = userDistinctId(options.distinctId);
|
|
515
594
|
this.traceId = options.traceId;
|
|
516
595
|
this.properties = options.properties || {};
|
|
596
|
+
this.eventOptions = options.options;
|
|
517
597
|
this.privacyMode = options.privacyMode || false;
|
|
518
598
|
this.groups = options.groups || {};
|
|
519
599
|
this.debug = options.debug || false;
|
|
@@ -726,7 +806,6 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
|
|
|
726
806
|
};
|
|
727
807
|
if (parentRunId) eventProperties["$ai_parent_id"] = parentRunId;
|
|
728
808
|
Object.assign(eventProperties, this.properties);
|
|
729
|
-
if (!this.distinctId) eventProperties["$process_person_profile"] = false;
|
|
730
809
|
if (outputs instanceof Error) {
|
|
731
810
|
if (isLangGraphControlFlow(outputs)) {
|
|
732
811
|
const interrupts = outputs.interrupts;
|
|
@@ -739,7 +818,7 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
|
|
|
739
818
|
this._safeCapture({
|
|
740
819
|
distinctId: this.distinctId ? this.distinctId.toString() : runId,
|
|
741
820
|
event: eventName,
|
|
742
|
-
|
|
821
|
+
...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
|
|
743
822
|
groups: this.groups
|
|
744
823
|
});
|
|
745
824
|
}
|
|
@@ -815,11 +894,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
|
|
|
815
894
|
if (completions) eventProperties["$ai_output_choices"] = withPrivacyMode(this.client, this.privacyMode, completions);
|
|
816
895
|
}
|
|
817
896
|
Object.assign(eventProperties, this.properties);
|
|
818
|
-
if (!this.distinctId) eventProperties["$process_person_profile"] = false;
|
|
819
897
|
this._safeCapture({
|
|
820
898
|
distinctId: this.distinctId ? this.distinctId.toString() : traceId,
|
|
821
899
|
event: "$ai_generation",
|
|
822
|
-
|
|
900
|
+
...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
|
|
823
901
|
groups: this.groups
|
|
824
902
|
});
|
|
825
903
|
}
|