@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
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import "@langchain/core/messages";
|
|
2
2
|
import { InteropZodObject } from "@langchain/core/utils/types";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
+
import { JsonType } from "@posthog/core";
|
|
4
5
|
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
|
|
5
6
|
import { PostHog } from "posthog-node";
|
|
6
7
|
import "@langchain/core/load/serializable";
|
|
@@ -13,6 +14,11 @@ interface LangChainCallbackHandlerOptions {
|
|
|
13
14
|
distinctId?: string | number;
|
|
14
15
|
traceId?: string | number;
|
|
15
16
|
properties?: Record<string, any>;
|
|
17
|
+
/**
|
|
18
|
+
* Per-event options for posthog-node, such as `{ process_person_profile: false }`, sent with
|
|
19
|
+
* every event. An event with no `distinctId` always has person processing turned off.
|
|
20
|
+
*/
|
|
21
|
+
options?: Record<string, JsonType>;
|
|
16
22
|
privacyMode?: boolean;
|
|
17
23
|
groups?: Record<string, any>;
|
|
18
24
|
debug?: boolean;
|
|
@@ -4,6 +4,7 @@ import { extendInteropZodObject } from "@langchain/core/utils/types";
|
|
|
4
4
|
import { createMiddleware } from "langchain";
|
|
5
5
|
import { v7 } from "uuid";
|
|
6
6
|
import { z } from "zod";
|
|
7
|
+
import { acceptsEventOptions, eventOptionsAsLegacyProperties } from "@posthog/core";
|
|
7
8
|
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
|
|
8
9
|
//#region src/typeGuards.ts
|
|
9
10
|
const isObject = (value) => {
|
|
@@ -15,9 +16,77 @@ const isObject = (value) => {
|
|
|
15
16
|
function isFullAiCaptureEnabled(client) {
|
|
16
17
|
return client?.enableFullAiCapture === true;
|
|
17
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
|
|
21
|
+
*
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
function userDistinctId(value) {
|
|
25
|
+
if (!value) return;
|
|
26
|
+
const id = String(value);
|
|
27
|
+
return id.trim() === "" ? void 0 : id;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The caller's per-event options when they are an object. A missing or `null` value means no
|
|
31
|
+
* options. An array or any other value is reported and ignored, so it is never spread.
|
|
32
|
+
*
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
function readAiEventOptions(value) {
|
|
36
|
+
if (value === void 0 || value === null) return;
|
|
37
|
+
if (typeof value === "object" && !Array.isArray(value)) return value;
|
|
38
|
+
console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
|
|
42
|
+
* processing off and beats the caller's `process_person_profile`.
|
|
43
|
+
*
|
|
44
|
+
* posthog-node 6 takes the options as they are, so no option is written as a property.
|
|
45
|
+
* posthog-node 5 has no per-event options: each known option is sent as the property that version
|
|
46
|
+
* reads, and an unknown key is dropped.
|
|
47
|
+
*
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
function eventOptionFields(client, properties, options, personless) {
|
|
51
|
+
const userOptions = readAiEventOptions(options);
|
|
52
|
+
if (isPostHogNode6(client)) {
|
|
53
|
+
const eventOptions = {
|
|
54
|
+
...userOptions,
|
|
55
|
+
...personless ? { process_person_profile: false } : {}
|
|
56
|
+
};
|
|
57
|
+
return Object.keys(eventOptions).length > 0 ? {
|
|
58
|
+
properties,
|
|
59
|
+
options: eventOptions
|
|
60
|
+
} : { properties };
|
|
61
|
+
}
|
|
62
|
+
const withOptionProperties = {
|
|
63
|
+
...properties,
|
|
64
|
+
...eventOptionsAsLegacyProperties(userOptions)
|
|
65
|
+
};
|
|
66
|
+
if (personless) withOptionProperties.$process_person_profile = false;
|
|
67
|
+
return { properties: withOptionProperties };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Whether the client is posthog-node 6 or later, the first major with per-event options.
|
|
71
|
+
*
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
function isPostHogNode6(client) {
|
|
75
|
+
return acceptsEventOptions(client);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
|
|
79
|
+
* controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
|
|
80
|
+
* upgrade never changes how a 5.x client sends.
|
|
81
|
+
*
|
|
82
|
+
* @internal
|
|
83
|
+
*/
|
|
84
|
+
function usesAiLane(client) {
|
|
85
|
+
return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
|
|
86
|
+
}
|
|
18
87
|
/** @internal */
|
|
19
88
|
function captureAiEvent(client, event) {
|
|
20
|
-
if (
|
|
89
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
21
90
|
client.captureAi(event);
|
|
22
91
|
return;
|
|
23
92
|
}
|
|
@@ -165,13 +234,17 @@ var MediaTypeContext = class MediaTypeContext {
|
|
|
165
234
|
//#region src/sanitization/binary_content_redactor.ts
|
|
166
235
|
const STRONG_CONTEXT_MIN_LENGTH = 64;
|
|
167
236
|
const WEAK_CONTEXT_MIN_LENGTH = 1024;
|
|
237
|
+
const MAX_NODES = 1e4;
|
|
238
|
+
const TRUNCATED = "[Truncated]";
|
|
168
239
|
var BinaryContentRedactor = class {
|
|
169
240
|
constructor(recognizer = new Base64Recognizer()) {
|
|
170
241
|
this.recognizer = recognizer;
|
|
171
|
-
this.
|
|
242
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
243
|
+
this.remainingNodes = MAX_NODES;
|
|
172
244
|
}
|
|
173
245
|
redact(value, mediaType) {
|
|
174
|
-
this.
|
|
246
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
247
|
+
this.remainingNodes = MAX_NODES;
|
|
175
248
|
return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
|
|
176
249
|
}
|
|
177
250
|
/** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
|
|
@@ -183,18 +256,24 @@ var BinaryContentRedactor = class {
|
|
|
183
256
|
if (typeof value === "string") return this.redactLeaf(value, ctx);
|
|
184
257
|
if (typeof value !== "object") return value;
|
|
185
258
|
if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
|
|
186
|
-
if (this.
|
|
187
|
-
this.
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
259
|
+
if (this.ancestors.has(value)) return null;
|
|
260
|
+
if (this.remainingNodes <= 0) return TRUNCATED;
|
|
261
|
+
this.remainingNodes--;
|
|
262
|
+
this.ancestors.add(value);
|
|
263
|
+
try {
|
|
264
|
+
if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
|
|
265
|
+
const obj = value;
|
|
266
|
+
const out = {};
|
|
267
|
+
for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
|
|
268
|
+
value: this.walk(obj[k], new MediaTypeContext(obj, k)),
|
|
269
|
+
enumerable: true,
|
|
270
|
+
writable: true,
|
|
271
|
+
configurable: true
|
|
272
|
+
});
|
|
273
|
+
return out;
|
|
274
|
+
} finally {
|
|
275
|
+
this.ancestors.delete(value);
|
|
276
|
+
}
|
|
198
277
|
}
|
|
199
278
|
redactString(value, ctx) {
|
|
200
279
|
const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
|
|
@@ -312,7 +391,7 @@ function redactMcpToolCredentials(tools) {
|
|
|
312
391
|
}
|
|
313
392
|
//#endregion
|
|
314
393
|
//#region package.json
|
|
315
|
-
var version = "8.
|
|
394
|
+
var version = "8.21.0";
|
|
316
395
|
//#endregion
|
|
317
396
|
//#region src/serializeError.ts
|
|
318
397
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -515,9 +594,10 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
|
|
|
515
594
|
this.runs = {};
|
|
516
595
|
this.parentTree = {};
|
|
517
596
|
this.client = options.client;
|
|
518
|
-
this.distinctId = options.distinctId;
|
|
597
|
+
this.distinctId = userDistinctId(options.distinctId);
|
|
519
598
|
this.traceId = options.traceId;
|
|
520
599
|
this.properties = options.properties || {};
|
|
600
|
+
this.eventOptions = options.options;
|
|
521
601
|
this.privacyMode = options.privacyMode || false;
|
|
522
602
|
this.groups = options.groups || {};
|
|
523
603
|
this.debug = options.debug || false;
|
|
@@ -730,7 +810,6 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
|
|
|
730
810
|
};
|
|
731
811
|
if (parentRunId) eventProperties["$ai_parent_id"] = parentRunId;
|
|
732
812
|
Object.assign(eventProperties, this.properties);
|
|
733
|
-
if (!this.distinctId) eventProperties["$process_person_profile"] = false;
|
|
734
813
|
if (outputs instanceof Error) {
|
|
735
814
|
if (isLangGraphControlFlow(outputs)) {
|
|
736
815
|
const interrupts = outputs.interrupts;
|
|
@@ -743,7 +822,7 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
|
|
|
743
822
|
this._safeCapture({
|
|
744
823
|
distinctId: this.distinctId ? this.distinctId.toString() : runId,
|
|
745
824
|
event: eventName,
|
|
746
|
-
|
|
825
|
+
...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
|
|
747
826
|
groups: this.groups
|
|
748
827
|
});
|
|
749
828
|
}
|
|
@@ -819,11 +898,10 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
|
|
|
819
898
|
if (completions) eventProperties["$ai_output_choices"] = withPrivacyMode(this.client, this.privacyMode, completions);
|
|
820
899
|
}
|
|
821
900
|
Object.assign(eventProperties, this.properties);
|
|
822
|
-
if (!this.distinctId) eventProperties["$process_person_profile"] = false;
|
|
823
901
|
this._safeCapture({
|
|
824
902
|
distinctId: this.distinctId ? this.distinctId.toString() : traceId,
|
|
825
903
|
event: "$ai_generation",
|
|
826
|
-
|
|
904
|
+
...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
|
|
827
905
|
groups: this.groups
|
|
828
906
|
});
|
|
829
907
|
}
|