@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
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import AnthropicOriginal, { APIPromise, ClientOptions } from "@anthropic-ai/sdk";
|
|
2
|
+
import { JsonType } from "@posthog/core";
|
|
2
3
|
import { Stream } from "@anthropic-ai/sdk/streaming";
|
|
3
4
|
import { PostHog } from "posthog-node";
|
|
4
5
|
import "openai";
|
|
@@ -14,6 +15,12 @@ interface MonitoringEventPropertiesWithDefaults {
|
|
|
14
15
|
providerOverride?: string;
|
|
15
16
|
costOverride?: CostOverride;
|
|
16
17
|
captureImmediate?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Per-event options for posthog-node, such as `{ process_person_profile: false }`. posthog-node 5
|
|
20
|
+
* has no per-event options, so each option it knows is sent the way that version reads it, and
|
|
21
|
+
* other keys are dropped. An event with no distinct id always has person processing turned off.
|
|
22
|
+
*/
|
|
23
|
+
options?: Record<string, JsonType>;
|
|
17
24
|
}
|
|
18
25
|
type MonitoringEventProperties = Partial<MonitoringEventPropertiesWithDefaults>;
|
|
19
26
|
type MonitoringParams = { [K in keyof MonitoringEventProperties as `posthog${Capitalize<string & K>}`]: MonitoringEventProperties[K]; };
|
package/dist/anthropic/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import AnthropicOriginal from "@anthropic-ai/sdk";
|
|
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 "@anthropic-ai/sdk/streaming";
|
|
5
5
|
//#region src/typeGuards.ts
|
|
6
6
|
const isObject = (value) => {
|
|
@@ -12,9 +12,77 @@ const isObject = (value) => {
|
|
|
12
12
|
function isFullAiCaptureEnabled(client) {
|
|
13
13
|
return client?.enableFullAiCapture === true;
|
|
14
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
|
+
...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 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
|
+
}
|
|
15
83
|
/** @internal */
|
|
16
84
|
function captureAiEvent(client, event) {
|
|
17
|
-
if (
|
|
85
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
18
86
|
client.captureAi(event);
|
|
19
87
|
return;
|
|
20
88
|
}
|
|
@@ -22,7 +90,7 @@ function captureAiEvent(client, event) {
|
|
|
22
90
|
}
|
|
23
91
|
/** @internal */
|
|
24
92
|
async function captureAiEventImmediate(client, event) {
|
|
25
|
-
if (
|
|
93
|
+
if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
|
|
26
94
|
await client.captureAiImmediate(event);
|
|
27
95
|
return;
|
|
28
96
|
}
|
|
@@ -376,7 +444,8 @@ const POSTHOG_PARAMS_MAP = {
|
|
|
376
444
|
posthogModelOverride: "modelOverride",
|
|
377
445
|
posthogProviderOverride: "providerOverride",
|
|
378
446
|
posthogCostOverride: "costOverride",
|
|
379
|
-
posthogCaptureImmediate: "captureImmediate"
|
|
447
|
+
posthogCaptureImmediate: "captureImmediate",
|
|
448
|
+
posthogOptions: "options"
|
|
380
449
|
};
|
|
381
450
|
function extractPosthogParams(body) {
|
|
382
451
|
const providerParams = {};
|
|
@@ -398,7 +467,7 @@ function addDefaults(params) {
|
|
|
398
467
|
}
|
|
399
468
|
//#endregion
|
|
400
469
|
//#region package.json
|
|
401
|
-
var version = "8.
|
|
470
|
+
var version = "8.21.1";
|
|
402
471
|
//#endregion
|
|
403
472
|
//#region src/serializeError.ts
|
|
404
473
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -593,6 +662,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
593
662
|
//#endregion
|
|
594
663
|
//#region src/captureAiGeneration.ts
|
|
595
664
|
/**
|
|
665
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
666
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
667
|
+
*/
|
|
668
|
+
const captureAiException = (client, error, properties, uuid) => {
|
|
669
|
+
if (isPostHogNode6(client)) {
|
|
670
|
+
client.captureException(error, {
|
|
671
|
+
properties,
|
|
672
|
+
uuid
|
|
673
|
+
});
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
client.captureException.call(client, error, void 0, properties, uuid);
|
|
677
|
+
};
|
|
678
|
+
/**
|
|
596
679
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
597
680
|
*
|
|
598
681
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -628,7 +711,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
628
711
|
if (client.options?.enableExceptionAutocapture) {
|
|
629
712
|
if (!shouldRedact) {
|
|
630
713
|
exceptionId = uuidv7();
|
|
631
|
-
client
|
|
714
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
632
715
|
}
|
|
633
716
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
634
717
|
}
|
|
@@ -654,6 +737,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
654
737
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
655
738
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
656
739
|
};
|
|
740
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
657
741
|
const properties = {
|
|
658
742
|
$ai_lib: "posthog-ai",
|
|
659
743
|
$ai_lib_version: version,
|
|
@@ -673,7 +757,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
673
757
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
674
758
|
...options.properties,
|
|
675
759
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
676
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
677
760
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
678
761
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
679
762
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -682,9 +765,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
682
765
|
...costOverrideData
|
|
683
766
|
};
|
|
684
767
|
const event = {
|
|
685
|
-
distinctId:
|
|
768
|
+
distinctId: distinctId ?? traceId,
|
|
686
769
|
event: eventType,
|
|
687
|
-
properties,
|
|
770
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
688
771
|
groups: options.groups
|
|
689
772
|
};
|
|
690
773
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|
|
@@ -1129,7 +1212,7 @@ var WrappedMessages = class extends AnthropicOriginal.Messages {
|
|
|
1129
1212
|
currentReasoningBlock = null;
|
|
1130
1213
|
const tool = toolsInProgress.get(chunk.index);
|
|
1131
1214
|
if (tool) {
|
|
1132
|
-
try {
|
|
1215
|
+
if (tool.inputString) try {
|
|
1133
1216
|
tool.block.function.arguments = JSON.parse(tool.inputString);
|
|
1134
1217
|
} catch (e) {
|
|
1135
1218
|
console.error("Error parsing tool input:", e);
|