@posthog/ai 8.22.1 → 8.22.3

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.
Files changed (59) hide show
  1. package/README.md +19 -12
  2. package/dist/adk/index.cjs +76 -28
  3. package/dist/adk/index.cjs.map +1 -1
  4. package/dist/adk/index.mjs +76 -28
  5. package/dist/adk/index.mjs.map +1 -1
  6. package/dist/anthropic/index.cjs +77 -29
  7. package/dist/anthropic/index.cjs.map +1 -1
  8. package/dist/anthropic/index.mjs +77 -29
  9. package/dist/anthropic/index.mjs.map +1 -1
  10. package/dist/claude-agent-sdk/index.cjs +76 -28
  11. package/dist/claude-agent-sdk/index.cjs.map +1 -1
  12. package/dist/claude-agent-sdk/index.mjs +76 -28
  13. package/dist/claude-agent-sdk/index.mjs.map +1 -1
  14. package/dist/evaluations/index.cjs +1 -1
  15. package/dist/evaluations/index.cjs.map +1 -1
  16. package/dist/evaluations/index.d.ts +1 -1
  17. package/dist/evaluations/index.mjs +1 -1
  18. package/dist/evaluations/index.mjs.map +1 -1
  19. package/dist/gemini/index.cjs +80 -29
  20. package/dist/gemini/index.cjs.map +1 -1
  21. package/dist/gemini/index.mjs +80 -29
  22. package/dist/gemini/index.mjs.map +1 -1
  23. package/dist/genkit/index.cjs +84 -41
  24. package/dist/genkit/index.cjs.map +1 -1
  25. package/dist/genkit/index.mjs +84 -41
  26. package/dist/genkit/index.mjs.map +1 -1
  27. package/dist/index.cjs +77 -29
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.mjs +77 -29
  30. package/dist/index.mjs.map +1 -1
  31. package/dist/langchain/index.cjs +76 -28
  32. package/dist/langchain/index.cjs.map +1 -1
  33. package/dist/langchain/index.mjs +77 -29
  34. package/dist/langchain/index.mjs.map +1 -1
  35. package/dist/langchain/middleware/index.cjs +76 -28
  36. package/dist/langchain/middleware/index.cjs.map +1 -1
  37. package/dist/langchain/middleware/index.mjs +77 -29
  38. package/dist/langchain/middleware/index.mjs.map +1 -1
  39. package/dist/openai/index.cjs +77 -29
  40. package/dist/openai/index.cjs.map +1 -1
  41. package/dist/openai/index.mjs +77 -29
  42. package/dist/openai/index.mjs.map +1 -1
  43. package/dist/openai-agents/index.cjs +26 -1
  44. package/dist/openai-agents/index.cjs.map +1 -1
  45. package/dist/openai-agents/index.mjs +26 -1
  46. package/dist/openai-agents/index.mjs.map +1 -1
  47. package/dist/otel/index.browser.cjs +51 -2
  48. package/dist/otel/index.browser.cjs.map +1 -1
  49. package/dist/otel/index.browser.mjs +51 -2
  50. package/dist/otel/index.browser.mjs.map +1 -1
  51. package/dist/otel/index.cjs +51 -2
  52. package/dist/otel/index.cjs.map +1 -1
  53. package/dist/otel/index.mjs +51 -2
  54. package/dist/otel/index.mjs.map +1 -1
  55. package/dist/vercel/index.cjs +77 -29
  56. package/dist/vercel/index.cjs.map +1 -1
  57. package/dist/vercel/index.mjs +77 -29
  58. package/dist/vercel/index.mjs.map +1 -1
  59. package/package.json +2 -2
package/README.md CHANGED
@@ -146,12 +146,15 @@ requirements and does not require a `posthog-node` analytics client. Every netwo
146
146
  returns a Promise that resolves only after a validated server acknowledgment; there is no
147
147
  capture queue or flush step. Your application executes models and evaluators.
148
148
 
149
+ For this example, give the project's secret API key `dataset:read`, `llm_analytics:read`, and
150
+ `offline_evaluation_ingestion:write`. You can also pass a personal API key to `secretKey`.
151
+
149
152
  ```ts
150
153
  import { OfflineEvaluations, EvaluationItem } from '@posthog/ai/evaluations'
151
154
 
152
155
  const evaluations = new OfflineEvaluations({
153
156
  projectId: 123,
154
- secretKey: process.env.POSTHOG_PERSONAL_API_KEY!,
157
+ secretKey: process.env.POSTHOG_PROJECT_SECRET_API_KEY!,
155
158
  // host: 'https://eu.posthog.com',
156
159
  // timeoutMs: 15_000,
157
160
  // maxRetries: 3, // additional attempts, only for operations safe to replay
@@ -267,7 +270,7 @@ output bounds are checked during ingestion. The SDK never evaluates formulas or
267
270
  Use `calculate: true` without a value, payload, error, evaluator trace, or evaluation time.
268
271
  Ingestion-only keys must supply every pinned input in the same upload, including on exact retries.
269
272
  Reusing stored inputs without resubmitting them requires both `evaluation:read` and
270
- `llm_analytics:read`. Bulk uploads preserve caller order and keep all supplied results for a
273
+ `llm_analytics:read` in addition to `offline_evaluation_ingestion:write`. Bulk uploads preserve caller order and keep all supplied results for a
271
274
  composite-bearing item together, including intervening results. Group each item's results together
272
275
  in large batches: an indivisible group exceeding 1,000 items/results or 5 MiB rejects the entire
273
276
  bulk call before any request is sent.
@@ -281,18 +284,22 @@ is open creates it again and counts as new usage for a reported result. Older re
281
284
  without outcomes expose `status: null` and `errorCode: null`. Save and restore calculation
282
285
  declarations with the same serialization and recovery methods as reported results.
283
286
 
284
- | Operation | Credential and scope |
285
- | -------------------------------------- | -------------------------------------------------------------------- |
286
- | Dataset reads/writes | Personal key: `dataset:read` / `dataset:write` |
287
- | Scorer reads/writes | Personal key: `llm_analytics:read` / `llm_analytics:write` |
288
- | Formula validation | Personal key: `llm_analytics:read` |
289
- | Experiment create/upload/complete/fail | Personal or project secret key: `offline_evaluation_ingestion:write` |
290
- | Experiment tags | Personal key: `evaluation:read` / `evaluation:write` |
291
- | Item and result revision reads | Personal key: `evaluation:read` |
292
- | Item, result, and payload mutations | Personal key: `evaluation:write` and evaluation editor access |
287
+ | Operation | Required scopes |
288
+ | -------------------------------------- | -------------------------------------------- |
289
+ | Dataset reads/writes | `dataset:read` / `dataset:write` |
290
+ | Scorer reads/writes | `llm_analytics:read` / `llm_analytics:write` |
291
+ | Formula validation | `llm_analytics:read` |
292
+ | Experiment create/upload/complete/fail | `offline_evaluation_ingestion:write` |
293
+ | Experiment tags | `evaluation:read` / `evaluation:write` |
294
+ | Item and result revision reads | `evaluation:read` |
295
+ | Item, result, and payload mutations | `evaluation:write` |
296
+
297
+ These scopes apply to personal and project secret API keys. A project secret key belongs to one
298
+ project. Personal keys also need the user's resource permissions, including evaluation editor
299
+ access for corrections. Dataset exports require a personal API key with `dataset:read`.
293
300
 
294
301
  Reading a result revision, editing or deleting a result or its payload, and deleting an item
295
- also require `llm_analytics:read` and access to the affected scorers and composite inputs.
302
+ also require `llm_analytics:read`. Personal keys need access to the affected scorers and composite inputs.
296
303
 
297
304
  Feature availability and permissions are enforced by the service. Known ingestion hosts map to
298
305
  the app API host; custom host path prefixes are preserved. Pagination exposes `nextOffset` or,
@@ -3,13 +3,88 @@ let _google_adk = require("@google/adk");
3
3
  let uuid = require("uuid");
4
4
  let _posthog_core = require("@posthog/core");
5
5
  //#region package.json
6
- var version = "8.22.1";
6
+ var version = "8.22.3";
7
7
  //#endregion
8
8
  //#region src/typeGuards.ts
9
9
  const isObject = (value) => {
10
10
  return value !== null && typeof value === "object" && !Array.isArray(value);
11
11
  };
12
12
  //#endregion
13
+ //#region src/mcpCredentials.ts
14
+ const MCP_CREDENTIAL_FIELDS = ["headers", "authorization"];
15
+ const MCP_TOOL_TYPES = /* @__PURE__ */ new Set(["mcp", "mcp_server"]);
16
+ const MCP_PROVIDER_TOOL_IDS = /* @__PURE__ */ new Set([
17
+ "openai.mcp",
18
+ "xai.mcp",
19
+ "google.mcp_server"
20
+ ]);
21
+ const isMcpProviderTool = (tool) => (tool.type === "provider" || tool.type === "provider-defined") && typeof tool.id === "string" && MCP_PROVIDER_TOOL_IDS.has(tool.id);
22
+ const withoutMcpCredentials = (config) => {
23
+ const redacted = { ...config };
24
+ for (const field of MCP_CREDENTIAL_FIELDS) if (field in redacted) redacted[field] = "[REDACTED]";
25
+ return redacted;
26
+ };
27
+ const normalizeKey = (key) => key.toLowerCase().replace(/[-_]/g, "");
28
+ const isPlainObject = (value) => {
29
+ if (!isObject(value)) return false;
30
+ const prototype = Object.getPrototypeOf(value);
31
+ return prototype === Object.prototype || prototype === null;
32
+ };
33
+ /**
34
+ * Replaces the value of every key in `keys` (normalized) with `[REDACTED]`, through plain objects and arrays.
35
+ * Class instances, such as the MCP client inside an `mcpToTool()` tool, are returned as they are.
36
+ */
37
+ function redactKeys(value, keys) {
38
+ if (Array.isArray(value)) return value.map((item) => redactKeys(item, keys));
39
+ if (!isPlainObject(value)) return value;
40
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, keys.has(normalizeKey(key)) ? "[REDACTED]" : redactKeys(item, keys)]));
41
+ }
42
+ const GEMINI_TOOL_FIELDS = new Set([
43
+ "codeExecution",
44
+ "computerUse",
45
+ "enterpriseWebSearch",
46
+ "exaAiSearch",
47
+ "fileSearch",
48
+ "functionDeclarations",
49
+ "googleMaps",
50
+ "googleSearch",
51
+ "googleSearchRetrieval",
52
+ "mcpServers",
53
+ "parallelAiSearch",
54
+ "retrieval",
55
+ "urlContext"
56
+ ].map(normalizeKey));
57
+ const GEMINI_SCHEMA_FIELD = normalizeKey("functionDeclarations");
58
+ const GEMINI_CREDENTIAL_KEYS = new Set([
59
+ "apiAuth",
60
+ "authConfig",
61
+ "apiKey",
62
+ "headers"
63
+ ].map(normalizeKey));
64
+ const isGeminiTool = (tool) => Object.keys(tool).map(normalizeKey).some((key) => key !== GEMINI_SCHEMA_FIELD && GEMINI_TOOL_FIELDS.has(key));
65
+ /**
66
+ * Masks the credential keys under every field of a `@google/genai` tool except its function declarations.
67
+ * A class-based tool, such as `mcpToTool(client)`, holds a live client with its auth headers, so only a stub is kept.
68
+ */
69
+ function redactGeminiToolDefinition(tool) {
70
+ if (!isPlainObject(tool)) return isObject(tool) ? { type: "callable_tool" } : tool;
71
+ return Object.fromEntries(Object.entries(tool).map(([key, value]) => [key, normalizeKey(key) === GEMINI_SCHEMA_FIELD ? value : redactKeys((0, _posthog_core.toJsonSafeValue)(value), GEMINI_CREDENTIAL_KEYS)]));
72
+ }
73
+ /** Hides the credentials of a remote MCP or Gemini tool definition; everything else is kept as sent. */
74
+ function redactMcpToolDefinition(tool) {
75
+ if (!isObject(tool)) return tool;
76
+ if (typeof tool.type === "string" && MCP_TOOL_TYPES.has(tool.type)) return withoutMcpCredentials(tool);
77
+ if (isMcpProviderTool(tool) && isObject(tool.args)) return {
78
+ ...tool,
79
+ args: withoutMcpCredentials(tool.args)
80
+ };
81
+ if (isPlainObject(tool) && isGeminiTool(tool)) return redactGeminiToolDefinition(tool);
82
+ return tool;
83
+ }
84
+ function redactMcpToolCredentials(tools) {
85
+ return Array.isArray(tools) ? tools.map(redactMcpToolDefinition) : tools;
86
+ }
87
+ //#endregion
13
88
  //#region src/captureAiEvent.ts
14
89
  /** @internal */
15
90
  function isFullAiCaptureEnabled(client) {
@@ -583,33 +658,6 @@ function truncateStack(stack) {
583
658
  return [...lines.slice(0, MAX_STACK_LINES), "... (truncated)"].join("\n");
584
659
  }
585
660
  //#endregion
586
- //#region src/mcpCredentials.ts
587
- const MCP_CREDENTIAL_FIELDS = ["headers", "authorization"];
588
- const MCP_PROVIDER_TOOL_IDS = /* @__PURE__ */ new Set([
589
- "openai.mcp",
590
- "xai.mcp",
591
- "google.mcp_server"
592
- ]);
593
- const isMcpProviderTool = (tool) => (tool.type === "provider" || tool.type === "provider-defined") && typeof tool.id === "string" && MCP_PROVIDER_TOOL_IDS.has(tool.id);
594
- const withoutMcpCredentials = (config) => {
595
- const redacted = { ...config };
596
- for (const field of MCP_CREDENTIAL_FIELDS) if (field in redacted) redacted[field] = "[REDACTED]";
597
- return redacted;
598
- };
599
- /** Hides the credentials of a remote MCP tool definition; everything else is kept as sent. */
600
- function redactMcpToolDefinition(tool) {
601
- if (!isObject(tool)) return tool;
602
- if (tool.type === "mcp") return withoutMcpCredentials(tool);
603
- if (isMcpProviderTool(tool) && isObject(tool.args)) return {
604
- ...tool,
605
- args: withoutMcpCredentials(tool.args)
606
- };
607
- return tool;
608
- }
609
- function redactMcpToolCredentials(tools) {
610
- return Array.isArray(tools) ? tools.map(redactMcpToolDefinition) : tools;
611
- }
612
- //#endregion
613
661
  //#region src/gatewayWarning.ts
614
662
  const POSTHOG_AI_GATEWAY_HOSTS = [
615
663
  "gateway.posthog.com",