@absolutejs/ai 0.3.4 → 0.4.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.
@@ -567,7 +567,7 @@ async function* tapStream(source, params, providerName) {
567
567
  let lastUsage;
568
568
  try {
569
569
  for await (const chunk of source) {
570
- if (chunk.type === "done" && chunk.usage) {
570
+ if ((chunk.type === "done" || chunk.type === "usage_update" && chunk.final) && chunk.usage) {
571
571
  lastUsage = chunk.usage;
572
572
  }
573
573
  yield chunk;
@@ -1180,6 +1180,8 @@ var processCompleted = function* (parsed) {
1180
1180
  const { response } = parsed;
1181
1181
  const usage = extractUsage(response);
1182
1182
  const metadata = extractResponseMetadata(response);
1183
+ if (usage)
1184
+ yield { type: "usage_update", usage, metadata, final: true };
1183
1185
  if (isRecordArray(response.output)) {
1184
1186
  yield* extractCitationsFromOutput(response.output);
1185
1187
  yield* extractImageFromOutput(response.output);
@@ -1218,6 +1220,15 @@ var processSSEEvent = function* (eventType, parsed, pendingCalls, providerName)
1218
1220
  break;
1219
1221
  case "response.output_item.added":
1220
1222
  processOutputItemAdded(parsed, pendingCalls);
1223
+ if (isRecord(parsed.item) && parsed.item.type === "function_call" && typeof parsed.item.name === "string") {
1224
+ const id = typeof parsed.item.call_id === "string" ? parsed.item.call_id : parsed.item.id;
1225
+ if (typeof id === "string")
1226
+ yield {
1227
+ type: "tool_input_start",
1228
+ id,
1229
+ name: parsed.item.name
1230
+ };
1231
+ }
1221
1232
  break;
1222
1233
  case "response.output_item.done":
1223
1234
  yield* processOutputItemDone(parsed);
@@ -1234,8 +1245,19 @@ var processSSEEvent = function* (eventType, parsed, pendingCalls, providerName)
1234
1245
  case "response.failed":
1235
1246
  case "response.incomplete":
1236
1247
  case "response.error":
1237
- case "error":
1248
+ case "error": {
1249
+ if (isRecord(parsed.response)) {
1250
+ const usage = extractUsage(parsed.response);
1251
+ if (usage)
1252
+ yield {
1253
+ type: "usage_update",
1254
+ usage,
1255
+ metadata: extractResponseMetadata(parsed.response),
1256
+ final: false
1257
+ };
1258
+ }
1238
1259
  throw responseFailure(eventType, parsed, providerName);
1260
+ }
1239
1261
  }
1240
1262
  };
1241
1263
  var flushSSEBuffer = function* (state) {
@@ -1657,6 +1679,14 @@ var processDelta = function* (delta, pendingToolCalls) {
1657
1679
  }
1658
1680
  if (isRecordArray2(delta.tool_calls)) {
1659
1681
  processToolCallDeltas(delta.tool_calls, pendingToolCalls);
1682
+ for (const call of delta.tool_calls) {
1683
+ if (isRecord2(call.function) && typeof call.function.name === "string" && call.function.name)
1684
+ yield {
1685
+ type: "tool_input_start",
1686
+ id: typeof call.id === "string" ? call.id : String(call.index),
1687
+ name: call.function.name
1688
+ };
1689
+ }
1660
1690
  }
1661
1691
  if (Array.isArray(delta.annotations)) {
1662
1692
  for (const annotation of delta.annotations) {
@@ -1755,7 +1785,12 @@ var processSSELine2 = function* (line, pendingToolCalls, providerName) {
1755
1785
  }
1756
1786
  const usageUpdate = narrowUsageRecord(parsed);
1757
1787
  if (usageUpdate) {
1758
- yield { type: "usage_update", usage: usageUpdate };
1788
+ yield {
1789
+ type: "usage_update",
1790
+ usage: usageUpdate,
1791
+ final: Array.isArray(parsed.choices) && parsed.choices.length === 0,
1792
+ metadata: narrowResponseMetadata(parsed)
1793
+ };
1759
1794
  }
1760
1795
  const metadata = narrowResponseMetadata(parsed);
1761
1796
  if (metadata) {
@@ -1784,10 +1819,9 @@ var processSSELine2 = function* (line, pendingToolCalls, providerName) {
1784
1819
  }
1785
1820
  yield* processChoice(firstChoice, pendingToolCalls);
1786
1821
  };
1787
- var isUsageUpdate = (chunk) => chunk.type === "usage_update";
1788
1822
  var collectYieldableChunks = (line, pendingToolCalls, usageRef, metadataRef, providerName) => {
1789
1823
  const allChunks = Array.from(processSSELine2(line, pendingToolCalls, providerName));
1790
- const usageChunks = allChunks.filter(isUsageUpdate);
1824
+ const usageChunks = allChunks.filter((chunk) => chunk.type === "usage_update");
1791
1825
  const lastUsage = usageChunks.at(NOT_FOUND);
1792
1826
  if (lastUsage) {
1793
1827
  usageRef.current = lastUsage.usage;
@@ -1804,7 +1838,7 @@ var collectYieldableChunks = (line, pendingToolCalls, usageRef, metadataRef, pro
1804
1838
  }
1805
1839
  };
1806
1840
  }
1807
- return allChunks.filter((chunk) => !isUsageUpdate(chunk) && chunk.type !== "response_metadata");
1841
+ return allChunks.filter((chunk) => chunk.type !== "response_metadata").map((chunk) => chunk.type === "usage_update" ? { ...chunk, metadata: metadataRef.current } : chunk);
1808
1842
  };
1809
1843
  var processSSELines2 = function* (lines, pendingToolCalls, usageRef, metadataRef, providerName) {
1810
1844
  for (const line of lines) {
@@ -1960,5 +1994,5 @@ export {
1960
1994
  xai
1961
1995
  };
1962
1996
 
1963
- //# debugId=46B67058661E11F264756E2164756E21
1997
+ //# debugId=276A9B2DDBF8D44964756E2164756E21
1964
1998
  //# sourceMappingURL=openaiCompatible.js.map