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