@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
package/dist/gemini/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { JsonType } from "@posthog/core";
|
|
1
2
|
import { EmbedContentParameters, EmbedContentResponse, GenerateContentParameters, GenerateContentResponse, GoogleGenAI, GoogleGenAIOptions } from "@google/genai";
|
|
2
3
|
import { PostHog } from "posthog-node";
|
|
3
4
|
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/gemini/index.mjs
CHANGED
|
@@ -1,14 +1,82 @@
|
|
|
1
|
+
import { acceptsEventOptions, eventOptionsAsLegacyProperties, toJsonSafeValue, uuidv7 } from "@posthog/core";
|
|
1
2
|
import { GoogleGenAI } from "@google/genai";
|
|
2
3
|
import { v4 } from "uuid";
|
|
3
|
-
import { toJsonSafeValue, uuidv7 } from "@posthog/core";
|
|
4
4
|
//#region src/captureAiEvent.ts
|
|
5
5
|
/** @internal */
|
|
6
6
|
function isFullAiCaptureEnabled(client) {
|
|
7
7
|
return client?.enableFullAiCapture === true;
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
function userDistinctId(value) {
|
|
15
|
+
if (!value) return;
|
|
16
|
+
const id = String(value);
|
|
17
|
+
return id.trim() === "" ? void 0 : id;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The caller's per-event options when they are an object. A missing or `null` value means no
|
|
21
|
+
* options. An array or any other value is reported and ignored, so it is never spread.
|
|
22
|
+
*
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
function readAiEventOptions(value) {
|
|
26
|
+
if (value === void 0 || value === null) return;
|
|
27
|
+
if (typeof value === "object" && !Array.isArray(value)) return value;
|
|
28
|
+
console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
|
|
32
|
+
* processing off and beats the caller's `process_person_profile`.
|
|
33
|
+
*
|
|
34
|
+
* posthog-node 6 takes the options as they are, so no option is written as a property.
|
|
35
|
+
* posthog-node 5 has no per-event options: each known option is sent as the property that version
|
|
36
|
+
* reads, and an unknown key is dropped.
|
|
37
|
+
*
|
|
38
|
+
* @internal
|
|
39
|
+
*/
|
|
40
|
+
function eventOptionFields(client, properties, options, personless) {
|
|
41
|
+
const userOptions = readAiEventOptions(options);
|
|
42
|
+
if (isPostHogNode6(client)) {
|
|
43
|
+
const eventOptions = {
|
|
44
|
+
...userOptions,
|
|
45
|
+
...personless ? { process_person_profile: false } : {}
|
|
46
|
+
};
|
|
47
|
+
return Object.keys(eventOptions).length > 0 ? {
|
|
48
|
+
properties,
|
|
49
|
+
options: eventOptions
|
|
50
|
+
} : { properties };
|
|
51
|
+
}
|
|
52
|
+
const withOptionProperties = {
|
|
53
|
+
...properties,
|
|
54
|
+
...eventOptionsAsLegacyProperties(userOptions)
|
|
55
|
+
};
|
|
56
|
+
if (personless) withOptionProperties.$process_person_profile = false;
|
|
57
|
+
return { properties: withOptionProperties };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Whether the client is posthog-node 6 or later, the first major with per-event options.
|
|
61
|
+
*
|
|
62
|
+
* @internal
|
|
63
|
+
*/
|
|
64
|
+
function isPostHogNode6(client) {
|
|
65
|
+
return acceptsEventOptions(client);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
|
|
69
|
+
* controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
|
|
70
|
+
* upgrade never changes how a 5.x client sends.
|
|
71
|
+
*
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
function usesAiLane(client) {
|
|
75
|
+
return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
|
|
76
|
+
}
|
|
9
77
|
/** @internal */
|
|
10
78
|
function captureAiEvent(client, event) {
|
|
11
|
-
if (
|
|
79
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
12
80
|
client.captureAi(event);
|
|
13
81
|
return;
|
|
14
82
|
}
|
|
@@ -16,7 +84,7 @@ function captureAiEvent(client, event) {
|
|
|
16
84
|
}
|
|
17
85
|
/** @internal */
|
|
18
86
|
async function captureAiEventImmediate(client, event) {
|
|
19
|
-
if (
|
|
87
|
+
if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
|
|
20
88
|
await client.captureAiImmediate(event);
|
|
21
89
|
return;
|
|
22
90
|
}
|
|
@@ -449,7 +517,8 @@ const POSTHOG_PARAMS_MAP = {
|
|
|
449
517
|
posthogModelOverride: "modelOverride",
|
|
450
518
|
posthogProviderOverride: "providerOverride",
|
|
451
519
|
posthogCostOverride: "costOverride",
|
|
452
|
-
posthogCaptureImmediate: "captureImmediate"
|
|
520
|
+
posthogCaptureImmediate: "captureImmediate",
|
|
521
|
+
posthogOptions: "options"
|
|
453
522
|
};
|
|
454
523
|
function extractPosthogParams(body) {
|
|
455
524
|
const providerParams = {};
|
|
@@ -471,7 +540,7 @@ function addDefaults(params) {
|
|
|
471
540
|
}
|
|
472
541
|
//#endregion
|
|
473
542
|
//#region package.json
|
|
474
|
-
var version = "8.
|
|
543
|
+
var version = "8.21.1";
|
|
475
544
|
//#endregion
|
|
476
545
|
//#region src/serializeError.ts
|
|
477
546
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -666,6 +735,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
666
735
|
//#endregion
|
|
667
736
|
//#region src/captureAiGeneration.ts
|
|
668
737
|
/**
|
|
738
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
739
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
740
|
+
*/
|
|
741
|
+
const captureAiException = (client, error, properties, uuid) => {
|
|
742
|
+
if (isPostHogNode6(client)) {
|
|
743
|
+
client.captureException(error, {
|
|
744
|
+
properties,
|
|
745
|
+
uuid
|
|
746
|
+
});
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
client.captureException.call(client, error, void 0, properties, uuid);
|
|
750
|
+
};
|
|
751
|
+
/**
|
|
669
752
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
670
753
|
*
|
|
671
754
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -701,7 +784,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
701
784
|
if (client.options?.enableExceptionAutocapture) {
|
|
702
785
|
if (!shouldRedact) {
|
|
703
786
|
exceptionId = uuidv7();
|
|
704
|
-
client
|
|
787
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
705
788
|
}
|
|
706
789
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
707
790
|
}
|
|
@@ -727,6 +810,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
727
810
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
728
811
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
729
812
|
};
|
|
813
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
730
814
|
const properties = {
|
|
731
815
|
$ai_lib: "posthog-ai",
|
|
732
816
|
$ai_lib_version: version,
|
|
@@ -746,7 +830,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
746
830
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
747
831
|
...options.properties,
|
|
748
832
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
749
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
750
833
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
751
834
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
752
835
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -755,9 +838,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
755
838
|
...costOverrideData
|
|
756
839
|
};
|
|
757
840
|
const event = {
|
|
758
|
-
distinctId:
|
|
841
|
+
distinctId: distinctId ?? traceId,
|
|
759
842
|
event: eventType,
|
|
760
|
-
properties,
|
|
843
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
761
844
|
groups: options.groups
|
|
762
845
|
};
|
|
763
846
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|