ai 3.0.0 → 3.0.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.
@@ -418,10 +418,14 @@ function createCallbacksTransformer(cb) {
418
418
  await callbacks.onStart();
419
419
  },
420
420
  async transform(message, controller) {
421
- controller.enqueue(textEncoder.encode(message));
422
- aggregatedResponse += message;
421
+ const content = typeof message === "string" ? message : message.content;
422
+ controller.enqueue(textEncoder.encode(content));
423
+ aggregatedResponse += content;
423
424
  if (callbacks.onToken)
424
- await callbacks.onToken(message);
425
+ await callbacks.onToken(content);
426
+ if (callbacks.onText && typeof message === "string") {
427
+ await callbacks.onText(message);
428
+ }
425
429
  },
426
430
  async flush() {
427
431
  const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
@@ -565,25 +569,46 @@ function chunkToText() {
565
569
  const delta = (_a = json.choices[0]) == null ? void 0 : _a.delta;
566
570
  if ((_b = delta.function_call) == null ? void 0 : _b.name) {
567
571
  isFunctionStreamingIn = true;
568
- return `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`;
572
+ return {
573
+ isText: false,
574
+ content: `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`
575
+ };
569
576
  } else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
570
577
  isFunctionStreamingIn = true;
571
578
  const toolCall = delta.tool_calls[0];
572
579
  if (toolCall.index === 0) {
573
- return `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`;
580
+ return {
581
+ isText: false,
582
+ content: `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`
583
+ };
574
584
  } else {
575
- return `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`;
585
+ return {
586
+ isText: false,
587
+ content: `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`
588
+ };
576
589
  }
577
590
  } else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
578
- return cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments);
591
+ return {
592
+ isText: false,
593
+ content: cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments)
594
+ };
579
595
  } else if ((_l = (_k = (_j = delta.tool_calls) == null ? void 0 : _j[0]) == null ? void 0 : _k.function) == null ? void 0 : _l.arguments) {
580
- return cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments);
596
+ return {
597
+ isText: false,
598
+ content: cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments)
599
+ };
581
600
  } else if (isFunctionStreamingIn && (((_p = json.choices[0]) == null ? void 0 : _p.finish_reason) === "function_call" || ((_q = json.choices[0]) == null ? void 0 : _q.finish_reason) === "stop")) {
582
601
  isFunctionStreamingIn = false;
583
- return '"}}';
602
+ return {
603
+ isText: false,
604
+ content: '"}}'
605
+ };
584
606
  } else if (isFunctionStreamingIn && ((_r = json.choices[0]) == null ? void 0 : _r.finish_reason) === "tool_calls") {
585
607
  isFunctionStreamingIn = false;
586
- return '"}}]}';
608
+ return {
609
+ isText: false,
610
+ content: '"}}]}'
611
+ };
587
612
  }
588
613
  }
589
614
  const text = trimStartOfStream(
@@ -941,6 +966,11 @@ function render(options) {
941
966
  };
942
967
  }
943
968
  ) : void 0;
969
+ if (functions && tools) {
970
+ throw new Error(
971
+ "You can't have both functions and tools defined. Please choose one or the other."
972
+ );
973
+ }
944
974
  let finished;
945
975
  async function handleRender(args, renderer, res) {
946
976
  if (!renderer)
@@ -954,17 +984,21 @@ function render(options) {
954
984
  res.update(node);
955
985
  finished == null ? void 0 : finished.resolve(void 0);
956
986
  } else if (value && typeof value === "object" && Symbol.asyncIterator in value) {
957
- for await (const node of value) {
958
- res.update(node);
987
+ const it = value;
988
+ while (true) {
989
+ const { done, value: value2 } = await it.next();
990
+ res.update(value2);
991
+ if (done)
992
+ break;
959
993
  }
960
994
  finished == null ? void 0 : finished.resolve(void 0);
961
995
  } else if (value && typeof value === "object" && Symbol.iterator in value) {
962
996
  const it = value;
963
997
  while (true) {
964
998
  const { done, value: value2 } = it.next();
999
+ res.update(value2);
965
1000
  if (done)
966
1001
  break;
967
- res.update(value2);
968
1002
  }
969
1003
  finished == null ? void 0 : finished.resolve(void 0);
970
1004
  } else {
@@ -990,15 +1024,17 @@ function render(options) {
990
1024
  } : {}
991
1025
  }),
992
1026
  {
993
- async experimental_onFunctionCall(functionCallPayload) {
994
- var _a, _b;
995
- hasFunction = true;
996
- handleRender(
997
- functionCallPayload.arguments,
998
- (_b = (_a = options.functions) == null ? void 0 : _a[functionCallPayload.name]) == null ? void 0 : _b.render,
999
- ui
1000
- );
1001
- },
1027
+ ...functions ? {
1028
+ async experimental_onFunctionCall(functionCallPayload) {
1029
+ var _a, _b;
1030
+ hasFunction = true;
1031
+ handleRender(
1032
+ functionCallPayload.arguments,
1033
+ (_b = (_a = options.functions) == null ? void 0 : _a[functionCallPayload.name]) == null ? void 0 : _b.render,
1034
+ ui
1035
+ );
1036
+ }
1037
+ } : {},
1002
1038
  ...tools ? {
1003
1039
  async experimental_onToolCall(toolCallPayload) {
1004
1040
  var _a, _b;
@@ -1012,15 +1048,16 @@ function render(options) {
1012
1048
  }
1013
1049
  }
1014
1050
  } : {},
1015
- onToken(token) {
1016
- text += token;
1017
- if (hasFunction)
1018
- return;
1051
+ onText(chunk) {
1052
+ text += chunk;
1019
1053
  handleRender({ content: text, done: false }, options.text, ui);
1020
1054
  },
1021
1055
  async onFinal() {
1022
- if (hasFunction)
1056
+ if (hasFunction) {
1057
+ await (finished == null ? void 0 : finished.promise);
1058
+ ui.done();
1023
1059
  return;
1060
+ }
1024
1061
  handleRender({ content: text, done: true }, options.text, ui);
1025
1062
  await (finished == null ? void 0 : finished.promise);
1026
1063
  ui.done();