ai 2.2.33 → 2.2.34

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
@@ -353,10 +353,11 @@ var experimental_StreamData = class {
353
353
  controller.enqueue(encodedData);
354
354
  }
355
355
  if (self.messageAnnotations.length) {
356
- const encodedmessageAnnotations = self.encoder.encode(
356
+ const encodedMessageAnnotations = self.encoder.encode(
357
357
  formatStreamPart("message_annotations", self.messageAnnotations)
358
358
  );
359
- controller.enqueue(encodedmessageAnnotations);
359
+ self.messageAnnotations = [];
360
+ controller.enqueue(encodedMessageAnnotations);
360
361
  }
361
362
  controller.enqueue(chunk);
362
363
  },
@@ -611,14 +612,29 @@ function createParser2(res) {
611
612
  }
612
613
  });
613
614
  }
615
+ async function* streamable2(stream) {
616
+ for await (const chunk of stream) {
617
+ if (chunk.eventType === "text-generation") {
618
+ const text = chunk.text;
619
+ if (text)
620
+ yield text;
621
+ }
622
+ }
623
+ }
614
624
  function CohereStream(reader, callbacks) {
615
- return createParser2(reader).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
616
- createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
617
- );
625
+ if (Symbol.asyncIterator in reader) {
626
+ return readableFromAsyncIterable(streamable2(reader)).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
627
+ createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
628
+ );
629
+ } else {
630
+ return createParser2(reader).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
631
+ createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
632
+ );
633
+ }
618
634
  }
619
635
 
620
636
  // streams/google-generative-ai-stream.ts
621
- async function* streamable2(response) {
637
+ async function* streamable3(response) {
622
638
  var _a, _b, _c;
623
639
  for await (const chunk of response.stream) {
624
640
  const parts = (_c = (_b = (_a = chunk.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts;
@@ -632,7 +648,7 @@ async function* streamable2(response) {
632
648
  }
633
649
  }
634
650
  function GoogleGenerativeAIStream(response, cb) {
635
- return readableFromAsyncIterable(streamable2(response)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData));
651
+ return readableFromAsyncIterable(streamable3(response)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData));
636
652
  }
637
653
 
638
654
  // streams/huggingface-stream.ts
@@ -731,7 +747,7 @@ function parseOpenAIStream() {
731
747
  const extract = chunkToText();
732
748
  return (data) => extract(JSON.parse(data));
733
749
  }
734
- async function* streamable3(stream) {
750
+ async function* streamable4(stream) {
735
751
  const extract = chunkToText();
736
752
  for await (let chunk of stream) {
737
753
  if ("promptFilterResults" in chunk) {
@@ -820,7 +836,7 @@ function OpenAIStream(res, callbacks) {
820
836
  const cb = callbacks;
821
837
  let stream;
822
838
  if (Symbol.asyncIterator in res) {
823
- stream = readableFromAsyncIterable(streamable3(res)).pipeThrough(
839
+ stream = readableFromAsyncIterable(streamable4(res)).pipeThrough(
824
840
  createCallbacksTransformer(
825
841
  (cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
826
842
  ...cb,
@@ -1091,6 +1107,11 @@ async function* readDataStream(reader, {
1091
1107
  }
1092
1108
 
1093
1109
  // shared/parse-complex-response.ts
1110
+ function assignAnnotationsToMessage(message, annotations) {
1111
+ if (!message || !annotations || !annotations.length)
1112
+ return message;
1113
+ return { ...message, annotations: [...annotations] };
1114
+ }
1094
1115
  async function parseComplexResponse({
1095
1116
  reader,
1096
1117
  abortControllerRef,
@@ -1103,6 +1124,7 @@ async function parseComplexResponse({
1103
1124
  const prefixMap = {
1104
1125
  data: []
1105
1126
  };
1127
+ let message_annotations = void 0;
1106
1128
  for await (const { type, value } of readDataStream(reader, {
1107
1129
  isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
1108
1130
  })) {
@@ -1121,22 +1143,6 @@ async function parseComplexResponse({
1121
1143
  };
1122
1144
  }
1123
1145
  }
1124
- if (type == "message_annotations") {
1125
- if (prefixMap["text"]) {
1126
- prefixMap["text"] = {
1127
- ...prefixMap["text"],
1128
- annotations: [...prefixMap["text"].annotations || [], ...value]
1129
- };
1130
- } else {
1131
- prefixMap["text"] = {
1132
- id: generateId(),
1133
- role: "assistant",
1134
- content: "",
1135
- annotations: [...value],
1136
- createdAt
1137
- };
1138
- }
1139
- }
1140
1146
  let functionCallMessage = null;
1141
1147
  if (type === "function_call") {
1142
1148
  prefixMap["function_call"] = {
@@ -1163,12 +1169,41 @@ async function parseComplexResponse({
1163
1169
  if (type === "data") {
1164
1170
  prefixMap["data"].push(...value);
1165
1171
  }
1166
- const responseMessage = prefixMap["text"];
1167
- const merged = [
1168
- functionCallMessage,
1169
- toolCallMessage,
1170
- responseMessage
1171
- ].filter(Boolean);
1172
+ let responseMessage = prefixMap["text"];
1173
+ if (type === "message_annotations") {
1174
+ if (!message_annotations) {
1175
+ message_annotations = [...value];
1176
+ } else {
1177
+ message_annotations.push(...value);
1178
+ }
1179
+ functionCallMessage = assignAnnotationsToMessage(
1180
+ prefixMap["function_call"],
1181
+ message_annotations
1182
+ );
1183
+ toolCallMessage = assignAnnotationsToMessage(
1184
+ prefixMap["tool_calls"],
1185
+ message_annotations
1186
+ );
1187
+ responseMessage = assignAnnotationsToMessage(
1188
+ prefixMap["text"],
1189
+ message_annotations
1190
+ );
1191
+ }
1192
+ if (message_annotations == null ? void 0 : message_annotations.length) {
1193
+ const messagePrefixKeys = [
1194
+ "text",
1195
+ "function_call",
1196
+ "tool_calls"
1197
+ ];
1198
+ messagePrefixKeys.forEach((key) => {
1199
+ if (prefixMap[key]) {
1200
+ prefixMap[key].annotations = [...message_annotations];
1201
+ }
1202
+ });
1203
+ }
1204
+ const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({
1205
+ ...assignAnnotationsToMessage(message, message_annotations)
1206
+ }));
1172
1207
  update(merged, [...prefixMap["data"]]);
1173
1208
  }
1174
1209
  onFinish == null ? void 0 : onFinish(prefixMap);