@core-ai/openai 0.13.1 → 0.14.0

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/compat.js CHANGED
@@ -228,6 +228,7 @@ async function* transformStream(stream) {
228
228
  const bufferedToolCalls = /* @__PURE__ */ new Map();
229
229
  const emittedToolCalls = /* @__PURE__ */ new Set();
230
230
  let finishReason = "unknown";
231
+ let textOpen = false;
231
232
  let usage = {
232
233
  inputTokens: 0,
233
234
  outputTokens: 0,
@@ -237,6 +238,20 @@ async function* transformStream(stream) {
237
238
  },
238
239
  outputTokenDetails: {}
239
240
  };
241
+ const startText = function* () {
242
+ if (textOpen) {
243
+ return;
244
+ }
245
+ textOpen = true;
246
+ yield { type: "text-start" };
247
+ };
248
+ const closeText = function* () {
249
+ if (!textOpen) {
250
+ return;
251
+ }
252
+ textOpen = false;
253
+ yield { type: "text-end" };
254
+ };
240
255
  for await (const chunk of stream) {
241
256
  if (chunk.usage) {
242
257
  const reasoningTokens = chunk.usage.completion_tokens_details?.reasoning_tokens;
@@ -257,12 +272,14 @@ async function* transformStream(stream) {
257
272
  continue;
258
273
  }
259
274
  if (choice.delta.content) {
275
+ yield* startText();
260
276
  yield {
261
277
  type: "text-delta",
262
278
  text: choice.delta.content
263
279
  };
264
280
  }
265
281
  if (choice.delta.tool_calls) {
282
+ yield* closeText();
266
283
  for (const partialToolCall of choice.delta.tool_calls) {
267
284
  const current = bufferedToolCalls.get(
268
285
  partialToolCall.index
@@ -300,6 +317,7 @@ async function* transformStream(stream) {
300
317
  finishReason = mapFinishReason(choice.finish_reason);
301
318
  }
302
319
  if (finishReason === "tool-calls") {
320
+ yield* closeText();
303
321
  for (const toolCall of bufferedToolCalls.values()) {
304
322
  if (emittedToolCalls.has(toolCall.id)) {
305
323
  continue;
@@ -316,6 +334,7 @@ async function* transformStream(stream) {
316
334
  }
317
335
  }
318
336
  }
337
+ yield* closeText();
319
338
  yield {
320
339
  type: "finish",
321
340
  finishReason,
package/dist/index.js CHANGED
@@ -369,6 +369,7 @@ async function* transformStream(stream) {
369
369
  const seenSummaryDeltas = /* @__PURE__ */ new Set();
370
370
  const emittedReasoningItems = /* @__PURE__ */ new Set();
371
371
  let latestResponse;
372
+ let textOpen = false;
372
373
  let reasoningStarted = false;
373
374
  let latestReasoningSummaryPart;
374
375
  const upsertBufferedToolCall = (outputIndex, getNextToolCall) => {
@@ -383,6 +384,20 @@ async function* transformStream(stream) {
383
384
  reasoningStarted = transition.nextReasoningStarted;
384
385
  return transition.event;
385
386
  };
387
+ const startText = function* () {
388
+ if (textOpen) {
389
+ return;
390
+ }
391
+ textOpen = true;
392
+ yield { type: "text-start" };
393
+ };
394
+ const closeText = function* () {
395
+ if (!textOpen) {
396
+ return;
397
+ }
398
+ textOpen = false;
399
+ yield { type: "text-end" };
400
+ };
386
401
  const getNextReasoningEndEvent = (providerMetadata) => {
387
402
  const transition = getReasoningEndTransition(
388
403
  reasoningStarted,
@@ -415,6 +430,7 @@ async function* transformStream(stream) {
415
430
  emittedReasoningItems.add(event.item_id);
416
431
  const reasoningStartEvent = getNextReasoningStartEvent();
417
432
  if (reasoningStartEvent) {
433
+ yield* closeText();
418
434
  yield reasoningStartEvent;
419
435
  }
420
436
  const separatorEvent = getReasoningSummarySeparatorEvent(summaryPart);
@@ -437,6 +453,7 @@ async function* transformStream(stream) {
437
453
  emittedReasoningItems.add(event.item_id);
438
454
  const reasoningStartEvent = getNextReasoningStartEvent();
439
455
  if (reasoningStartEvent) {
456
+ yield* closeText();
440
457
  yield reasoningStartEvent;
441
458
  }
442
459
  const separatorEvent = getReasoningSummarySeparatorEvent(summaryPart);
@@ -451,6 +468,11 @@ async function* transformStream(stream) {
451
468
  continue;
452
469
  }
453
470
  if (event.type === "response.output_text.delta") {
471
+ const reasoningEndEvent2 = getNextReasoningEndEvent({ openai: {} });
472
+ if (reasoningEndEvent2) {
473
+ yield reasoningEndEvent2;
474
+ }
475
+ yield* startText();
454
476
  yield {
455
477
  type: "text-delta",
456
478
  text: event.delta
@@ -461,6 +483,7 @@ async function* transformStream(stream) {
461
483
  if (!isFunctionToolCall(event.item)) {
462
484
  continue;
463
485
  }
486
+ yield* closeText();
464
487
  const toolCallId = event.item.call_id;
465
488
  const toolCallName = event.item.name;
466
489
  const toolCallArguments = event.item.arguments;
@@ -481,6 +504,7 @@ async function* transformStream(stream) {
481
504
  continue;
482
505
  }
483
506
  if (event.type === "response.function_call_arguments.delta") {
507
+ yield* closeText();
484
508
  const currentToolCall = upsertBufferedToolCall(
485
509
  event.output_index,
486
510
  (bufferedToolCall) => ({
@@ -516,6 +540,7 @@ async function* transformStream(stream) {
516
540
  if (summaryText.length > 0) {
517
541
  const reasoningStartEvent = getNextReasoningStartEvent();
518
542
  if (reasoningStartEvent) {
543
+ yield* closeText();
519
544
  yield reasoningStartEvent;
520
545
  }
521
546
  yield {
@@ -528,6 +553,7 @@ async function* transformStream(stream) {
528
553
  if (encryptedContent) {
529
554
  const reasoningStartEvent = getNextReasoningStartEvent();
530
555
  if (reasoningStartEvent) {
556
+ yield* closeText();
531
557
  yield reasoningStartEvent;
532
558
  }
533
559
  }
@@ -544,6 +570,7 @@ async function* transformStream(stream) {
544
570
  if (!isFunctionToolCall(event.item)) {
545
571
  continue;
546
572
  }
573
+ yield* closeText();
547
574
  const toolCallId = event.item.call_id;
548
575
  const toolCallName = event.item.name;
549
576
  const toolCallArguments = event.item.arguments;
@@ -572,6 +599,7 @@ async function* transformStream(stream) {
572
599
  }
573
600
  if (event.type === "response.completed") {
574
601
  latestResponse = event.response;
602
+ yield* closeText();
575
603
  const reasoningEndEvent2 = getNextReasoningEndEvent({ openai: {} });
576
604
  if (reasoningEndEvent2) {
577
605
  yield reasoningEndEvent2;
@@ -604,6 +632,7 @@ async function* transformStream(stream) {
604
632
  const reasoningEndEvent = getNextReasoningEndEvent({
605
633
  openai: {}
606
634
  });
635
+ yield* closeText();
607
636
  if (reasoningEndEvent) {
608
637
  yield reasoningEndEvent;
609
638
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@core-ai/openai",
3
- "version": "0.13.1",
3
+ "version": "0.14.0",
4
4
  "description": "OpenAI provider package for @core-ai/core-ai",
5
5
  "license": "MIT",
6
6
  "author": "Omnifact (https://omnifact.ai)",
@@ -50,7 +50,7 @@
50
50
  "test:watch": "vitest"
51
51
  },
52
52
  "dependencies": {
53
- "@core-ai/core-ai": "^0.13.1",
53
+ "@core-ai/core-ai": "^0.14.0",
54
54
  "openai": "^6.46.0"
55
55
  },
56
56
  "peerDependencies": {