@posthog/ai 8.21.1 → 8.22.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 +43 -3
- package/dist/adk/index.cjs +1 -1
- package/dist/adk/index.mjs +1 -1
- package/dist/anthropic/index.cjs +1 -1
- package/dist/anthropic/index.mjs +1 -1
- package/dist/claude-agent-sdk/index.cjs +1 -1
- package/dist/claude-agent-sdk/index.mjs +1 -1
- package/dist/evaluations/index.cjs +142 -15
- package/dist/evaluations/index.cjs.map +1 -1
- package/dist/evaluations/index.d.ts +62 -1
- package/dist/evaluations/index.mjs +142 -15
- package/dist/evaluations/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +1 -1
- package/dist/gemini/index.mjs +1 -1
- package/dist/genkit/index.cjs +1 -1
- package/dist/genkit/index.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/langchain/index.cjs +1 -1
- package/dist/langchain/index.mjs +1 -1
- package/dist/langchain/middleware/index.cjs +1 -1
- package/dist/langchain/middleware/index.mjs +1 -1
- package/dist/openai/index.cjs +68 -47
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +68 -47
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +1 -1
- package/dist/openai-agents/index.mjs +1 -1
- package/dist/vercel/index.cjs +1 -1
- package/dist/vercel/index.mjs +1 -1
- package/package.json +3 -3
package/dist/openai/index.mjs
CHANGED
|
@@ -631,7 +631,7 @@ function formatOpenAIResponsesInput(input, instructions) {
|
|
|
631
631
|
}
|
|
632
632
|
//#endregion
|
|
633
633
|
//#region package.json
|
|
634
|
-
var version = "8.
|
|
634
|
+
var version = "8.22.1";
|
|
635
635
|
//#endregion
|
|
636
636
|
//#region src/serializeError.ts
|
|
637
637
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -1372,36 +1372,75 @@ function monitoredStreamTee(source, createStream) {
|
|
|
1372
1372
|
}
|
|
1373
1373
|
//#endregion
|
|
1374
1374
|
//#region src/openai/stream-accumulators.ts
|
|
1375
|
+
function formatChoiceContent(state) {
|
|
1376
|
+
const content = [];
|
|
1377
|
+
if (state.content) content.push({
|
|
1378
|
+
type: "text",
|
|
1379
|
+
text: state.content
|
|
1380
|
+
});
|
|
1381
|
+
if (state.refusal) content.push({
|
|
1382
|
+
type: "refusal",
|
|
1383
|
+
refusal: state.refusal
|
|
1384
|
+
});
|
|
1385
|
+
for (const toolCall of state.toolCalls.values()) if (toolCall.name) content.push({
|
|
1386
|
+
type: "function",
|
|
1387
|
+
id: toolCall.id,
|
|
1388
|
+
function: {
|
|
1389
|
+
name: toolCall.name,
|
|
1390
|
+
arguments: toolCall.arguments
|
|
1391
|
+
}
|
|
1392
|
+
});
|
|
1393
|
+
return content;
|
|
1394
|
+
}
|
|
1375
1395
|
/** Pure state accumulator for OpenAI-compatible Chat Completions chunks. */
|
|
1376
1396
|
var OpenAIChatStreamAccumulator = class {
|
|
1377
1397
|
constructor() {
|
|
1378
|
-
this.
|
|
1379
|
-
this.accumulatedRefusal = "";
|
|
1398
|
+
this.choices = /* @__PURE__ */ new Map();
|
|
1380
1399
|
this.usage = { webSearchCount: 0 };
|
|
1381
|
-
this.toolCalls = /* @__PURE__ */ new Map();
|
|
1382
1400
|
}
|
|
1383
1401
|
consume(chunk, receivedAt = Date.now()) {
|
|
1384
1402
|
this.model ||= chunk.model || void 0;
|
|
1385
1403
|
this.completionId ||= chunk.id || void 0;
|
|
1386
1404
|
this.systemFingerprint ||= chunk.system_fingerprint || void 0;
|
|
1387
1405
|
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
1406
|
const webSearchCount = calculateWebSearchCount(chunk);
|
|
1391
1407
|
if (webSearchCount > (this.usage.webSearchCount ?? 0)) this.usage.webSearchCount = webSearchCount;
|
|
1392
|
-
|
|
1408
|
+
for (const choice of chunk.choices ?? []) this.consumeChoice(choice, receivedAt);
|
|
1409
|
+
if (chunk.usage) this.usage = {
|
|
1410
|
+
...this.usage,
|
|
1411
|
+
inputTokens: chunk.usage.prompt_tokens ?? 0,
|
|
1412
|
+
outputTokens: chunk.usage.completion_tokens ?? 0,
|
|
1413
|
+
reasoningTokens: chunk.usage.completion_tokens_details?.reasoning_tokens ?? 0,
|
|
1414
|
+
cacheReadInputTokens: chunk.usage.prompt_tokens_details?.cached_tokens ?? 0,
|
|
1415
|
+
cacheCreationInputTokens: extractCacheWriteTokens(chunk.usage.prompt_tokens_details),
|
|
1416
|
+
rawUsage: chunk.usage
|
|
1417
|
+
};
|
|
1418
|
+
}
|
|
1419
|
+
consumeChoice(choice, receivedAt) {
|
|
1420
|
+
const index = choice.index ?? 0;
|
|
1421
|
+
let state = this.choices.get(index);
|
|
1422
|
+
if (!state) {
|
|
1423
|
+
state = {
|
|
1424
|
+
content: "",
|
|
1425
|
+
refusal: "",
|
|
1426
|
+
toolCalls: /* @__PURE__ */ new Map()
|
|
1427
|
+
};
|
|
1428
|
+
this.choices.set(index, state);
|
|
1429
|
+
}
|
|
1430
|
+
if (choice.finish_reason && index === 0) this.stopReason = choice.finish_reason;
|
|
1431
|
+
if (choice.delta?.content) {
|
|
1393
1432
|
this.firstTokenTime ??= receivedAt;
|
|
1394
|
-
|
|
1433
|
+
state.content += choice.delta.content;
|
|
1395
1434
|
}
|
|
1396
|
-
if (choice
|
|
1435
|
+
if (choice.delta?.refusal) {
|
|
1397
1436
|
this.firstTokenTime ??= receivedAt;
|
|
1398
|
-
|
|
1437
|
+
state.refusal += choice.delta.refusal;
|
|
1399
1438
|
}
|
|
1400
|
-
if (Array.isArray(choice
|
|
1439
|
+
if (Array.isArray(choice.delta?.tool_calls)) {
|
|
1401
1440
|
this.firstTokenTime ??= receivedAt;
|
|
1402
1441
|
for (const toolCall of choice.delta.tool_calls) {
|
|
1403
|
-
const key = this.toolCallKey(toolCall);
|
|
1404
|
-
const current =
|
|
1442
|
+
const key = this.toolCallKey(state, toolCall);
|
|
1443
|
+
const current = state.toolCalls.get(key) ?? {
|
|
1405
1444
|
id: "",
|
|
1406
1445
|
name: "",
|
|
1407
1446
|
arguments: ""
|
|
@@ -1411,55 +1450,37 @@ var OpenAIChatStreamAccumulator = class {
|
|
|
1411
1450
|
const argumentsDelta = toolCall.function?.arguments ?? toolCall.custom?.input;
|
|
1412
1451
|
if (name) current.name = name;
|
|
1413
1452
|
if (argumentsDelta) current.arguments += argumentsDelta;
|
|
1414
|
-
|
|
1415
|
-
|
|
1453
|
+
state.toolCalls.set(key, current);
|
|
1454
|
+
state.lastToolCallKey = key;
|
|
1416
1455
|
}
|
|
1417
1456
|
}
|
|
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
1457
|
}
|
|
1428
1458
|
/**
|
|
1429
1459
|
* Some OpenAI-compatible providers stream tool calls without an `index`. Match those deltas by a known
|
|
1430
1460
|
* `id`; a new `id`, a function name, or a custom tool name opens a new call, and anything else continues
|
|
1431
1461
|
* the latest call.
|
|
1432
1462
|
*/
|
|
1433
|
-
toolCallKey(toolCall) {
|
|
1463
|
+
toolCallKey(state, toolCall) {
|
|
1434
1464
|
if (toolCall.index !== void 0) return toolCall.index;
|
|
1435
1465
|
if (toolCall.id) {
|
|
1436
|
-
for (const [key, existing] of
|
|
1466
|
+
for (const [key, existing] of state.toolCalls) if (existing.id === toolCall.id) return key;
|
|
1437
1467
|
}
|
|
1438
|
-
if (toolCall.id || toolCall.function?.name || toolCall.custom?.name ||
|
|
1439
|
-
return
|
|
1468
|
+
if (toolCall.id || toolCall.function?.name || toolCall.custom?.name || state.lastToolCallKey === void 0) return `no-index-${state.toolCalls.size}`;
|
|
1469
|
+
return state.lastToolCallKey;
|
|
1440
1470
|
}
|
|
1441
1471
|
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
|
-
});
|
|
1472
|
+
const output = [];
|
|
1473
|
+
for (const [, state] of [...this.choices].sort(([a], [b]) => a - b)) {
|
|
1474
|
+
const content = formatChoiceContent(state);
|
|
1475
|
+
if (content.length > 0) output.push({
|
|
1476
|
+
role: "assistant",
|
|
1477
|
+
content
|
|
1478
|
+
});
|
|
1479
|
+
}
|
|
1459
1480
|
return {
|
|
1460
|
-
output: [{
|
|
1481
|
+
output: output.length > 0 ? output : [{
|
|
1461
1482
|
role: "assistant",
|
|
1462
|
-
content:
|
|
1483
|
+
content: [{
|
|
1463
1484
|
type: "text",
|
|
1464
1485
|
text: ""
|
|
1465
1486
|
}]
|