@posthog/ai 8.22.0 → 8.22.2
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/dist/adk/index.cjs +76 -28
- package/dist/adk/index.cjs.map +1 -1
- package/dist/adk/index.mjs +76 -28
- package/dist/adk/index.mjs.map +1 -1
- package/dist/anthropic/index.cjs +77 -29
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.mjs +77 -29
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/claude-agent-sdk/index.cjs +76 -28
- package/dist/claude-agent-sdk/index.cjs.map +1 -1
- package/dist/claude-agent-sdk/index.mjs +76 -28
- package/dist/claude-agent-sdk/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +77 -29
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +77 -29
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/genkit/index.cjs +84 -41
- package/dist/genkit/index.cjs.map +1 -1
- package/dist/genkit/index.mjs +84 -41
- package/dist/genkit/index.mjs.map +1 -1
- package/dist/index.cjs +77 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +77 -29
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +76 -28
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +77 -29
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/langchain/middleware/index.cjs +76 -28
- package/dist/langchain/middleware/index.cjs.map +1 -1
- package/dist/langchain/middleware/index.mjs +77 -29
- package/dist/langchain/middleware/index.mjs.map +1 -1
- package/dist/openai/index.cjs +144 -75
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +144 -75
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +26 -1
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.mjs +26 -1
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/otel/index.browser.cjs +51 -2
- package/dist/otel/index.browser.cjs.map +1 -1
- package/dist/otel/index.browser.mjs +51 -2
- package/dist/otel/index.browser.mjs.map +1 -1
- package/dist/otel/index.cjs +51 -2
- package/dist/otel/index.cjs.map +1 -1
- package/dist/otel/index.mjs +51 -2
- package/dist/otel/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +77 -29
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +77 -29
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/openai/index.mjs
CHANGED
|
@@ -10,6 +10,81 @@ 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(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) {
|
|
@@ -548,7 +623,7 @@ const extractAvailableToolCalls = (provider, params) => {
|
|
|
548
623
|
if (params.tools) return params.tools;
|
|
549
624
|
return null;
|
|
550
625
|
} else if (provider === "gemini") {
|
|
551
|
-
if (params.config && params.config.tools) return params.config.tools;
|
|
626
|
+
if (params.config && Array.isArray(params.config.tools)) return params.config.tools.map(redactGeminiToolDefinition);
|
|
552
627
|
return null;
|
|
553
628
|
} else if (provider === "openai") {
|
|
554
629
|
if (params.tools) return params.tools;
|
|
@@ -631,7 +706,7 @@ function formatOpenAIResponsesInput(input, instructions) {
|
|
|
631
706
|
}
|
|
632
707
|
//#endregion
|
|
633
708
|
//#region package.json
|
|
634
|
-
var version = "8.22.
|
|
709
|
+
var version = "8.22.2";
|
|
635
710
|
//#endregion
|
|
636
711
|
//#region src/serializeError.ts
|
|
637
712
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -770,33 +845,6 @@ function truncateStack(stack) {
|
|
|
770
845
|
return [...lines.slice(0, MAX_STACK_LINES), "... (truncated)"].join("\n");
|
|
771
846
|
}
|
|
772
847
|
//#endregion
|
|
773
|
-
//#region src/mcpCredentials.ts
|
|
774
|
-
const MCP_CREDENTIAL_FIELDS = ["headers", "authorization"];
|
|
775
|
-
const MCP_PROVIDER_TOOL_IDS = /* @__PURE__ */ new Set([
|
|
776
|
-
"openai.mcp",
|
|
777
|
-
"xai.mcp",
|
|
778
|
-
"google.mcp_server"
|
|
779
|
-
]);
|
|
780
|
-
const isMcpProviderTool = (tool) => (tool.type === "provider" || tool.type === "provider-defined") && typeof tool.id === "string" && MCP_PROVIDER_TOOL_IDS.has(tool.id);
|
|
781
|
-
const withoutMcpCredentials = (config) => {
|
|
782
|
-
const redacted = { ...config };
|
|
783
|
-
for (const field of MCP_CREDENTIAL_FIELDS) if (field in redacted) redacted[field] = "[REDACTED]";
|
|
784
|
-
return redacted;
|
|
785
|
-
};
|
|
786
|
-
/** Hides the credentials of a remote MCP tool definition; everything else is kept as sent. */
|
|
787
|
-
function redactMcpToolDefinition(tool) {
|
|
788
|
-
if (!isObject(tool)) return tool;
|
|
789
|
-
if (tool.type === "mcp") return withoutMcpCredentials(tool);
|
|
790
|
-
if (isMcpProviderTool(tool) && isObject(tool.args)) return {
|
|
791
|
-
...tool,
|
|
792
|
-
args: withoutMcpCredentials(tool.args)
|
|
793
|
-
};
|
|
794
|
-
return tool;
|
|
795
|
-
}
|
|
796
|
-
function redactMcpToolCredentials(tools) {
|
|
797
|
-
return Array.isArray(tools) ? tools.map(redactMcpToolDefinition) : tools;
|
|
798
|
-
}
|
|
799
|
-
//#endregion
|
|
800
848
|
//#region src/gatewayWarning.ts
|
|
801
849
|
const POSTHOG_AI_GATEWAY_HOSTS = [
|
|
802
850
|
"gateway.posthog.com",
|
|
@@ -1372,36 +1420,75 @@ function monitoredStreamTee(source, createStream) {
|
|
|
1372
1420
|
}
|
|
1373
1421
|
//#endregion
|
|
1374
1422
|
//#region src/openai/stream-accumulators.ts
|
|
1423
|
+
function formatChoiceContent(state) {
|
|
1424
|
+
const content = [];
|
|
1425
|
+
if (state.content) content.push({
|
|
1426
|
+
type: "text",
|
|
1427
|
+
text: state.content
|
|
1428
|
+
});
|
|
1429
|
+
if (state.refusal) content.push({
|
|
1430
|
+
type: "refusal",
|
|
1431
|
+
refusal: state.refusal
|
|
1432
|
+
});
|
|
1433
|
+
for (const toolCall of state.toolCalls.values()) if (toolCall.name) content.push({
|
|
1434
|
+
type: "function",
|
|
1435
|
+
id: toolCall.id,
|
|
1436
|
+
function: {
|
|
1437
|
+
name: toolCall.name,
|
|
1438
|
+
arguments: toolCall.arguments
|
|
1439
|
+
}
|
|
1440
|
+
});
|
|
1441
|
+
return content;
|
|
1442
|
+
}
|
|
1375
1443
|
/** Pure state accumulator for OpenAI-compatible Chat Completions chunks. */
|
|
1376
1444
|
var OpenAIChatStreamAccumulator = class {
|
|
1377
1445
|
constructor() {
|
|
1378
|
-
this.
|
|
1379
|
-
this.accumulatedRefusal = "";
|
|
1446
|
+
this.choices = /* @__PURE__ */ new Map();
|
|
1380
1447
|
this.usage = { webSearchCount: 0 };
|
|
1381
|
-
this.toolCalls = /* @__PURE__ */ new Map();
|
|
1382
1448
|
}
|
|
1383
1449
|
consume(chunk, receivedAt = Date.now()) {
|
|
1384
1450
|
this.model ||= chunk.model || void 0;
|
|
1385
1451
|
this.completionId ||= chunk.id || void 0;
|
|
1386
1452
|
this.systemFingerprint ||= chunk.system_fingerprint || void 0;
|
|
1387
1453
|
if (chunk.service_tier != null) this.serviceTier = chunk.service_tier;
|
|
1388
|
-
const choice = chunk.choices?.[0];
|
|
1389
|
-
if (choice?.finish_reason) this.stopReason = choice.finish_reason;
|
|
1390
1454
|
const webSearchCount = calculateWebSearchCount(chunk);
|
|
1391
1455
|
if (webSearchCount > (this.usage.webSearchCount ?? 0)) this.usage.webSearchCount = webSearchCount;
|
|
1392
|
-
|
|
1456
|
+
for (const choice of chunk.choices ?? []) this.consumeChoice(choice, receivedAt);
|
|
1457
|
+
if (chunk.usage) this.usage = {
|
|
1458
|
+
...this.usage,
|
|
1459
|
+
inputTokens: chunk.usage.prompt_tokens ?? 0,
|
|
1460
|
+
outputTokens: chunk.usage.completion_tokens ?? 0,
|
|
1461
|
+
reasoningTokens: chunk.usage.completion_tokens_details?.reasoning_tokens ?? 0,
|
|
1462
|
+
cacheReadInputTokens: chunk.usage.prompt_tokens_details?.cached_tokens ?? 0,
|
|
1463
|
+
cacheCreationInputTokens: extractCacheWriteTokens(chunk.usage.prompt_tokens_details),
|
|
1464
|
+
rawUsage: chunk.usage
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
consumeChoice(choice, receivedAt) {
|
|
1468
|
+
const index = choice.index ?? 0;
|
|
1469
|
+
let state = this.choices.get(index);
|
|
1470
|
+
if (!state) {
|
|
1471
|
+
state = {
|
|
1472
|
+
content: "",
|
|
1473
|
+
refusal: "",
|
|
1474
|
+
toolCalls: /* @__PURE__ */ new Map()
|
|
1475
|
+
};
|
|
1476
|
+
this.choices.set(index, state);
|
|
1477
|
+
}
|
|
1478
|
+
if (choice.finish_reason && index === 0) this.stopReason = choice.finish_reason;
|
|
1479
|
+
if (choice.delta?.content) {
|
|
1393
1480
|
this.firstTokenTime ??= receivedAt;
|
|
1394
|
-
|
|
1481
|
+
state.content += choice.delta.content;
|
|
1395
1482
|
}
|
|
1396
|
-
if (choice
|
|
1483
|
+
if (choice.delta?.refusal) {
|
|
1397
1484
|
this.firstTokenTime ??= receivedAt;
|
|
1398
|
-
|
|
1485
|
+
state.refusal += choice.delta.refusal;
|
|
1399
1486
|
}
|
|
1400
|
-
if (Array.isArray(choice
|
|
1487
|
+
if (Array.isArray(choice.delta?.tool_calls)) {
|
|
1401
1488
|
this.firstTokenTime ??= receivedAt;
|
|
1402
1489
|
for (const toolCall of choice.delta.tool_calls) {
|
|
1403
|
-
const key = this.toolCallKey(toolCall);
|
|
1404
|
-
const current =
|
|
1490
|
+
const key = this.toolCallKey(state, toolCall);
|
|
1491
|
+
const current = state.toolCalls.get(key) ?? {
|
|
1405
1492
|
id: "",
|
|
1406
1493
|
name: "",
|
|
1407
1494
|
arguments: ""
|
|
@@ -1411,55 +1498,37 @@ var OpenAIChatStreamAccumulator = class {
|
|
|
1411
1498
|
const argumentsDelta = toolCall.function?.arguments ?? toolCall.custom?.input;
|
|
1412
1499
|
if (name) current.name = name;
|
|
1413
1500
|
if (argumentsDelta) current.arguments += argumentsDelta;
|
|
1414
|
-
|
|
1415
|
-
|
|
1501
|
+
state.toolCalls.set(key, current);
|
|
1502
|
+
state.lastToolCallKey = key;
|
|
1416
1503
|
}
|
|
1417
1504
|
}
|
|
1418
|
-
if (chunk.usage) this.usage = {
|
|
1419
|
-
...this.usage,
|
|
1420
|
-
inputTokens: chunk.usage.prompt_tokens ?? 0,
|
|
1421
|
-
outputTokens: chunk.usage.completion_tokens ?? 0,
|
|
1422
|
-
reasoningTokens: chunk.usage.completion_tokens_details?.reasoning_tokens ?? 0,
|
|
1423
|
-
cacheReadInputTokens: chunk.usage.prompt_tokens_details?.cached_tokens ?? 0,
|
|
1424
|
-
cacheCreationInputTokens: extractCacheWriteTokens(chunk.usage.prompt_tokens_details),
|
|
1425
|
-
rawUsage: chunk.usage
|
|
1426
|
-
};
|
|
1427
1505
|
}
|
|
1428
1506
|
/**
|
|
1429
1507
|
* Some OpenAI-compatible providers stream tool calls without an `index`. Match those deltas by a known
|
|
1430
1508
|
* `id`; a new `id`, a function name, or a custom tool name opens a new call, and anything else continues
|
|
1431
1509
|
* the latest call.
|
|
1432
1510
|
*/
|
|
1433
|
-
toolCallKey(toolCall) {
|
|
1511
|
+
toolCallKey(state, toolCall) {
|
|
1434
1512
|
if (toolCall.index !== void 0) return toolCall.index;
|
|
1435
1513
|
if (toolCall.id) {
|
|
1436
|
-
for (const [key, existing] of
|
|
1514
|
+
for (const [key, existing] of state.toolCalls) if (existing.id === toolCall.id) return key;
|
|
1437
1515
|
}
|
|
1438
|
-
if (toolCall.id || toolCall.function?.name || toolCall.custom?.name ||
|
|
1439
|
-
return
|
|
1516
|
+
if (toolCall.id || toolCall.function?.name || toolCall.custom?.name || state.lastToolCallKey === void 0) return `no-index-${state.toolCalls.size}`;
|
|
1517
|
+
return state.lastToolCallKey;
|
|
1440
1518
|
}
|
|
1441
1519
|
result() {
|
|
1442
|
-
const
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
});
|
|
1451
|
-
for (const toolCall of this.toolCalls.values()) if (toolCall.name) content.push({
|
|
1452
|
-
type: "function",
|
|
1453
|
-
id: toolCall.id,
|
|
1454
|
-
function: {
|
|
1455
|
-
name: toolCall.name,
|
|
1456
|
-
arguments: toolCall.arguments
|
|
1457
|
-
}
|
|
1458
|
-
});
|
|
1520
|
+
const output = [];
|
|
1521
|
+
for (const [, state] of [...this.choices].sort(([a], [b]) => a - b)) {
|
|
1522
|
+
const content = formatChoiceContent(state);
|
|
1523
|
+
if (content.length > 0) output.push({
|
|
1524
|
+
role: "assistant",
|
|
1525
|
+
content
|
|
1526
|
+
});
|
|
1527
|
+
}
|
|
1459
1528
|
return {
|
|
1460
|
-
output: [{
|
|
1529
|
+
output: output.length > 0 ? output : [{
|
|
1461
1530
|
role: "assistant",
|
|
1462
|
-
content:
|
|
1531
|
+
content: [{
|
|
1463
1532
|
type: "text",
|
|
1464
1533
|
text: ""
|
|
1465
1534
|
}]
|