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.
package/dist/index.mjs CHANGED
@@ -245,10 +245,14 @@ function createCallbacksTransformer(cb) {
245
245
  await callbacks.onStart();
246
246
  },
247
247
  async transform(message, controller) {
248
- controller.enqueue(textEncoder.encode(message));
249
- aggregatedResponse += message;
248
+ const content = typeof message === "string" ? message : message.content;
249
+ controller.enqueue(textEncoder.encode(content));
250
+ aggregatedResponse += content;
250
251
  if (callbacks.onToken)
251
- await callbacks.onToken(message);
252
+ await callbacks.onToken(content);
253
+ if (callbacks.onText && typeof message === "string") {
254
+ await callbacks.onText(message);
255
+ }
252
256
  },
253
257
  async flush() {
254
258
  const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
@@ -848,25 +852,46 @@ function chunkToText() {
848
852
  const delta = (_a = json.choices[0]) == null ? void 0 : _a.delta;
849
853
  if ((_b = delta.function_call) == null ? void 0 : _b.name) {
850
854
  isFunctionStreamingIn = true;
851
- return `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`;
855
+ return {
856
+ isText: false,
857
+ content: `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`
858
+ };
852
859
  } else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
853
860
  isFunctionStreamingIn = true;
854
861
  const toolCall = delta.tool_calls[0];
855
862
  if (toolCall.index === 0) {
856
- return `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`;
863
+ return {
864
+ isText: false,
865
+ content: `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`
866
+ };
857
867
  } else {
858
- return `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`;
868
+ return {
869
+ isText: false,
870
+ content: `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`
871
+ };
859
872
  }
860
873
  } else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
861
- return cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments);
874
+ return {
875
+ isText: false,
876
+ content: cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments)
877
+ };
862
878
  } else if ((_l = (_k = (_j = delta.tool_calls) == null ? void 0 : _j[0]) == null ? void 0 : _k.function) == null ? void 0 : _l.arguments) {
863
- return cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments);
879
+ return {
880
+ isText: false,
881
+ content: cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments)
882
+ };
864
883
  } 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")) {
865
884
  isFunctionStreamingIn = false;
866
- return '"}}';
885
+ return {
886
+ isText: false,
887
+ content: '"}}'
888
+ };
867
889
  } else if (isFunctionStreamingIn && ((_r = json.choices[0]) == null ? void 0 : _r.finish_reason) === "tool_calls") {
868
890
  isFunctionStreamingIn = false;
869
- return '"}}]}';
891
+ return {
892
+ isText: false,
893
+ content: '"}}]}'
894
+ };
870
895
  }
871
896
  }
872
897
  const text = trimStartOfStream(