extrait 0.7.4 → 0.7.5

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/dist/index.cjs CHANGED
@@ -2227,7 +2227,8 @@ async function streamWithChatCompletionsWithMCP(options, fetcher, path, request,
2227
2227
  roundReasoning += reasoningDelta;
2228
2228
  reasoningFieldName ??= pickAssistantReasoningDeltaFieldName(json);
2229
2229
  }
2230
- const chunkToolCalls = nativeDelta.toolCalls.length > 0 ? mergeToolCalls(buildOpenAIStreamToolCalls(streamedToolCalls), nativeDelta.toolCalls) : undefined;
2230
+ const streamedSnapshot = buildOpenAIStreamToolCalls(streamedToolCalls);
2231
+ const chunkToolCalls = nativeDelta.toolCalls.length > 0 ? mergeToolCalls(streamedSnapshot, nativeDelta.toolCalls) : streamedSnapshot.length > 0 ? streamedSnapshot : undefined;
2231
2232
  emitOpenAIStreamChunk(callbacks, round, json, delta, reasoningDelta, chunkUsage, chunkFinishReason, chunkToolCalls);
2232
2233
  });
2233
2234
  const tail = nativeToolCalls.flush();
@@ -2421,7 +2422,8 @@ async function streamWithResponsesAPIWithMCP(options, fetcher, path, request, ca
2421
2422
  if (reasoningDelta) {
2422
2423
  roundReasoning += reasoningDelta;
2423
2424
  }
2424
- emitOpenAIStreamChunk(callbacks, round, json, delta, reasoningDelta, chunkUsage, chunkFinishReason);
2425
+ const chunkToolCalls = buildResponsesStreamToolCalls(streamedToolCalls);
2426
+ emitOpenAIStreamChunk(callbacks, round, json, delta, reasoningDelta, chunkUsage, chunkFinishReason, chunkToolCalls.length > 0 ? chunkToolCalls : undefined);
2425
2427
  });
2426
2428
  const resolvedRoundUsage = preferLatestUsage(roundUsage, roundPayload ? pickUsage(roundPayload) : undefined);
2427
2429
  state.aggregatedUsage = mergeUsage2(state.aggregatedUsage, resolvedRoundUsage);
@@ -3226,6 +3228,7 @@ async function streamPassThrough(options, fetcher, path, request, callbacks) {
3226
3228
  let text = "";
3227
3229
  let usage;
3228
3230
  let finishReason;
3231
+ const streamedToolCalls = new Map;
3229
3232
  await consumeSSE(response, (data) => {
3230
3233
  if (data === "[DONE]") {
3231
3234
  return;
@@ -3237,6 +3240,7 @@ async function streamPassThrough(options, fetcher, path, request, callbacks) {
3237
3240
  const delta = pickAnthropicDelta(json);
3238
3241
  const chunkUsage = pickUsage2(json);
3239
3242
  const chunkFinishReason = pickFinishReason2(json);
3243
+ collectAnthropicStreamToolCalls(json, streamedToolCalls);
3240
3244
  usage = preferLatestUsage(usage, chunkUsage);
3241
3245
  if (chunkFinishReason) {
3242
3246
  finishReason = chunkFinishReason;
@@ -3245,16 +3249,25 @@ async function streamPassThrough(options, fetcher, path, request, callbacks) {
3245
3249
  text += delta;
3246
3250
  callbacks.onToken?.(delta);
3247
3251
  }
3248
- if (delta || chunkUsage || chunkFinishReason) {
3252
+ const streamedSnapshot = buildAnthropicStreamToolCalls(streamedToolCalls);
3253
+ const chunkToolCalls = streamedSnapshot.length > 0 ? streamedSnapshot : undefined;
3254
+ if (delta || chunkUsage || chunkFinishReason || chunkToolCalls) {
3249
3255
  callbacks.onChunk?.({
3250
3256
  textDelta: delta,
3251
3257
  raw: json,
3252
3258
  usage: chunkUsage,
3253
- finishReason: chunkFinishReason
3259
+ finishReason: chunkFinishReason,
3260
+ toolCalls: chunkToolCalls
3254
3261
  });
3255
3262
  }
3256
3263
  });
3257
- const out = { text, usage, finishReason };
3264
+ const toolCalls = buildAnthropicStreamToolCalls(streamedToolCalls);
3265
+ const out = {
3266
+ text,
3267
+ usage,
3268
+ finishReason: finishReason ?? (toolCalls.length > 0 ? "tool_use" : undefined),
3269
+ toolCalls: toolCalls.length > 0 ? toolCalls : undefined
3270
+ };
3258
3271
  callbacks.onComplete?.(out);
3259
3272
  return out;
3260
3273
  }
@@ -3432,14 +3445,17 @@ async function streamWithMCPToolLoop(options, fetcher, path, request, callbacks)
3432
3445
  if (reasoningDelta) {
3433
3446
  roundReasoning += reasoningDelta;
3434
3447
  }
3435
- if (delta || reasoningDelta || chunkUsage || chunkFinishReason) {
3448
+ const streamedSnapshot = buildAnthropicStreamToolCalls(streamedToolCalls);
3449
+ const chunkToolCalls = streamedSnapshot.length > 0 ? streamedSnapshot : undefined;
3450
+ if (delta || reasoningDelta || chunkUsage || chunkFinishReason || chunkToolCalls) {
3436
3451
  const chunk = {
3437
3452
  textDelta: delta,
3438
3453
  reasoningDelta: reasoningDelta || undefined,
3439
3454
  turnIndex: round,
3440
3455
  raw: json,
3441
3456
  usage: chunkUsage,
3442
- finishReason: chunkFinishReason
3457
+ finishReason: chunkFinishReason,
3458
+ toolCalls: chunkToolCalls
3443
3459
  };
3444
3460
  callbacks.onChunk?.(chunk);
3445
3461
  }
package/dist/index.js CHANGED
@@ -2134,7 +2134,8 @@ async function streamWithChatCompletionsWithMCP(options, fetcher, path, request,
2134
2134
  roundReasoning += reasoningDelta;
2135
2135
  reasoningFieldName ??= pickAssistantReasoningDeltaFieldName(json);
2136
2136
  }
2137
- const chunkToolCalls = nativeDelta.toolCalls.length > 0 ? mergeToolCalls(buildOpenAIStreamToolCalls(streamedToolCalls), nativeDelta.toolCalls) : undefined;
2137
+ const streamedSnapshot = buildOpenAIStreamToolCalls(streamedToolCalls);
2138
+ const chunkToolCalls = nativeDelta.toolCalls.length > 0 ? mergeToolCalls(streamedSnapshot, nativeDelta.toolCalls) : streamedSnapshot.length > 0 ? streamedSnapshot : undefined;
2138
2139
  emitOpenAIStreamChunk(callbacks, round, json, delta, reasoningDelta, chunkUsage, chunkFinishReason, chunkToolCalls);
2139
2140
  });
2140
2141
  const tail = nativeToolCalls.flush();
@@ -2328,7 +2329,8 @@ async function streamWithResponsesAPIWithMCP(options, fetcher, path, request, ca
2328
2329
  if (reasoningDelta) {
2329
2330
  roundReasoning += reasoningDelta;
2330
2331
  }
2331
- emitOpenAIStreamChunk(callbacks, round, json, delta, reasoningDelta, chunkUsage, chunkFinishReason);
2332
+ const chunkToolCalls = buildResponsesStreamToolCalls(streamedToolCalls);
2333
+ emitOpenAIStreamChunk(callbacks, round, json, delta, reasoningDelta, chunkUsage, chunkFinishReason, chunkToolCalls.length > 0 ? chunkToolCalls : undefined);
2332
2334
  });
2333
2335
  const resolvedRoundUsage = preferLatestUsage(roundUsage, roundPayload ? pickUsage(roundPayload) : undefined);
2334
2336
  state.aggregatedUsage = mergeUsage2(state.aggregatedUsage, resolvedRoundUsage);
@@ -3133,6 +3135,7 @@ async function streamPassThrough(options, fetcher, path, request, callbacks) {
3133
3135
  let text = "";
3134
3136
  let usage;
3135
3137
  let finishReason;
3138
+ const streamedToolCalls = new Map;
3136
3139
  await consumeSSE(response, (data) => {
3137
3140
  if (data === "[DONE]") {
3138
3141
  return;
@@ -3144,6 +3147,7 @@ async function streamPassThrough(options, fetcher, path, request, callbacks) {
3144
3147
  const delta = pickAnthropicDelta(json);
3145
3148
  const chunkUsage = pickUsage2(json);
3146
3149
  const chunkFinishReason = pickFinishReason2(json);
3150
+ collectAnthropicStreamToolCalls(json, streamedToolCalls);
3147
3151
  usage = preferLatestUsage(usage, chunkUsage);
3148
3152
  if (chunkFinishReason) {
3149
3153
  finishReason = chunkFinishReason;
@@ -3152,16 +3156,25 @@ async function streamPassThrough(options, fetcher, path, request, callbacks) {
3152
3156
  text += delta;
3153
3157
  callbacks.onToken?.(delta);
3154
3158
  }
3155
- if (delta || chunkUsage || chunkFinishReason) {
3159
+ const streamedSnapshot = buildAnthropicStreamToolCalls(streamedToolCalls);
3160
+ const chunkToolCalls = streamedSnapshot.length > 0 ? streamedSnapshot : undefined;
3161
+ if (delta || chunkUsage || chunkFinishReason || chunkToolCalls) {
3156
3162
  callbacks.onChunk?.({
3157
3163
  textDelta: delta,
3158
3164
  raw: json,
3159
3165
  usage: chunkUsage,
3160
- finishReason: chunkFinishReason
3166
+ finishReason: chunkFinishReason,
3167
+ toolCalls: chunkToolCalls
3161
3168
  });
3162
3169
  }
3163
3170
  });
3164
- const out = { text, usage, finishReason };
3171
+ const toolCalls = buildAnthropicStreamToolCalls(streamedToolCalls);
3172
+ const out = {
3173
+ text,
3174
+ usage,
3175
+ finishReason: finishReason ?? (toolCalls.length > 0 ? "tool_use" : undefined),
3176
+ toolCalls: toolCalls.length > 0 ? toolCalls : undefined
3177
+ };
3165
3178
  callbacks.onComplete?.(out);
3166
3179
  return out;
3167
3180
  }
@@ -3339,14 +3352,17 @@ async function streamWithMCPToolLoop(options, fetcher, path, request, callbacks)
3339
3352
  if (reasoningDelta) {
3340
3353
  roundReasoning += reasoningDelta;
3341
3354
  }
3342
- if (delta || reasoningDelta || chunkUsage || chunkFinishReason) {
3355
+ const streamedSnapshot = buildAnthropicStreamToolCalls(streamedToolCalls);
3356
+ const chunkToolCalls = streamedSnapshot.length > 0 ? streamedSnapshot : undefined;
3357
+ if (delta || reasoningDelta || chunkUsage || chunkFinishReason || chunkToolCalls) {
3343
3358
  const chunk = {
3344
3359
  textDelta: delta,
3345
3360
  reasoningDelta: reasoningDelta || undefined,
3346
3361
  turnIndex: round,
3347
3362
  raw: json,
3348
3363
  usage: chunkUsage,
3349
- finishReason: chunkFinishReason
3364
+ finishReason: chunkFinishReason,
3365
+ toolCalls: chunkToolCalls
3350
3366
  };
3351
3367
  callbacks.onChunk?.(chunk);
3352
3368
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extrait",
3
- "version": "0.7.4",
3
+ "version": "0.7.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/tterrasson/extrait.git"