@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.
@@ -544,7 +544,7 @@ function addDefaults(params) {
544
544
  }
545
545
  //#endregion
546
546
  //#region package.json
547
- var version = "8.21.1";
547
+ var version = "8.22.1";
548
548
  //#endregion
549
549
  //#region src/serializeError.ts
550
550
  const DEFAULT_MAX_DEPTH = 3;
@@ -540,7 +540,7 @@ function addDefaults(params) {
540
540
  }
541
541
  //#endregion
542
542
  //#region package.json
543
- var version = "8.21.1";
543
+ var version = "8.22.1";
544
544
  //#endregion
545
545
  //#region src/serializeError.ts
546
546
  const DEFAULT_MAX_DEPTH = 3;
@@ -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.21.1";
5
+ var version = "8.22.1";
6
6
  //#endregion
7
7
  //#region src/captureAiEvent.ts
8
8
  /** @internal */
@@ -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.21.1";
4
+ var version = "8.22.1";
5
5
  //#endregion
6
6
  //#region src/captureAiEvent.ts
7
7
  /** @internal */
package/dist/index.cjs CHANGED
@@ -497,7 +497,7 @@ function sanitizeValues(obj) {
497
497
  }
498
498
  //#endregion
499
499
  //#region package.json
500
- var version = "8.21.1";
500
+ var version = "8.22.1";
501
501
  //#endregion
502
502
  //#region src/serializeError.ts
503
503
  const DEFAULT_MAX_DEPTH = 3;
package/dist/index.mjs CHANGED
@@ -496,7 +496,7 @@ function sanitizeValues(obj) {
496
496
  }
497
497
  //#endregion
498
498
  //#region package.json
499
- var version = "8.21.1";
499
+ var version = "8.22.1";
500
500
  //#endregion
501
501
  //#region src/serializeError.ts
502
502
  const DEFAULT_MAX_DEPTH = 3;
@@ -387,7 +387,7 @@ function redactMcpToolCredentials(tools) {
387
387
  }
388
388
  //#endregion
389
389
  //#region package.json
390
- var version = "8.21.1";
390
+ var version = "8.22.1";
391
391
  //#endregion
392
392
  //#region src/serializeError.ts
393
393
  const DEFAULT_MAX_DEPTH = 3;
@@ -386,7 +386,7 @@ function redactMcpToolCredentials(tools) {
386
386
  }
387
387
  //#endregion
388
388
  //#region package.json
389
- var version = "8.21.1";
389
+ var version = "8.22.1";
390
390
  //#endregion
391
391
  //#region src/serializeError.ts
392
392
  const DEFAULT_MAX_DEPTH = 3;
@@ -392,7 +392,7 @@ function redactMcpToolCredentials(tools) {
392
392
  }
393
393
  //#endregion
394
394
  //#region package.json
395
- var version = "8.21.1";
395
+ var version = "8.22.1";
396
396
  //#endregion
397
397
  //#region src/serializeError.ts
398
398
  const DEFAULT_MAX_DEPTH = 3;
@@ -391,7 +391,7 @@ function redactMcpToolCredentials(tools) {
391
391
  }
392
392
  //#endregion
393
393
  //#region package.json
394
- var version = "8.21.1";
394
+ var version = "8.22.1";
395
395
  //#endregion
396
396
  //#region src/serializeError.ts
397
397
  const DEFAULT_MAX_DEPTH = 3;
@@ -635,7 +635,7 @@ function formatOpenAIResponsesInput(input, instructions) {
635
635
  }
636
636
  //#endregion
637
637
  //#region package.json
638
- var version = "8.21.1";
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.accumulatedContent = "";
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
- if (choice?.delta?.content) {
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
- this.accumulatedContent += choice.delta.content;
1437
+ state.content += choice.delta.content;
1399
1438
  }
1400
- if (choice?.delta?.refusal) {
1439
+ if (choice.delta?.refusal) {
1401
1440
  this.firstTokenTime ??= receivedAt;
1402
- this.accumulatedRefusal += choice.delta.refusal;
1441
+ state.refusal += choice.delta.refusal;
1403
1442
  }
1404
- if (Array.isArray(choice?.delta?.tool_calls)) {
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 = this.toolCalls.get(key) ?? {
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
- this.toolCalls.set(key, current);
1419
- this.lastToolCallKey = key;
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 this.toolCalls) if (existing.id === toolCall.id) return key;
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 || this.lastToolCallKey === void 0) return `no-index-${this.toolCalls.size}`;
1443
- return this.lastToolCallKey;
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 content = [];
1447
- if (this.accumulatedContent) content.push({
1448
- type: "text",
1449
- text: this.accumulatedContent
1450
- });
1451
- if (this.accumulatedRefusal) content.push({
1452
- type: "refusal",
1453
- refusal: this.accumulatedRefusal
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: content.length > 0 ? content : [{
1487
+ content: [{
1467
1488
  type: "text",
1468
1489
  text: ""
1469
1490
  }]