@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.
@@ -631,7 +631,7 @@ function formatOpenAIResponsesInput(input, instructions) {
631
631
  }
632
632
  //#endregion
633
633
  //#region package.json
634
- var version = "8.21.1";
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.accumulatedContent = "";
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
- if (choice?.delta?.content) {
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
- this.accumulatedContent += choice.delta.content;
1433
+ state.content += choice.delta.content;
1395
1434
  }
1396
- if (choice?.delta?.refusal) {
1435
+ if (choice.delta?.refusal) {
1397
1436
  this.firstTokenTime ??= receivedAt;
1398
- this.accumulatedRefusal += choice.delta.refusal;
1437
+ state.refusal += choice.delta.refusal;
1399
1438
  }
1400
- if (Array.isArray(choice?.delta?.tool_calls)) {
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 = this.toolCalls.get(key) ?? {
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
- this.toolCalls.set(key, current);
1415
- this.lastToolCallKey = key;
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 this.toolCalls) if (existing.id === toolCall.id) return key;
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 || this.lastToolCallKey === void 0) return `no-index-${this.toolCalls.size}`;
1439
- return this.lastToolCallKey;
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 content = [];
1443
- if (this.accumulatedContent) content.push({
1444
- type: "text",
1445
- text: this.accumulatedContent
1446
- });
1447
- if (this.accumulatedRefusal) content.push({
1448
- type: "refusal",
1449
- refusal: this.accumulatedRefusal
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: content.length > 0 ? content : [{
1483
+ content: [{
1463
1484
  type: "text",
1464
1485
  text: ""
1465
1486
  }]