ai 2.2.32 → 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.d.ts CHANGED
@@ -352,6 +352,41 @@ type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPr
352
352
  */
353
353
  declare const COMPLEX_HEADER = "X-Experimental-Stream-Data";
354
354
 
355
+ declare interface AzureChatCompletions {
356
+ id: string;
357
+ created: Date;
358
+ choices: AzureChatChoice[];
359
+ systemFingerprint?: string;
360
+ usage?: AzureCompletionsUsage;
361
+ promptFilterResults: any[];
362
+ }
363
+ declare interface AzureChatChoice {
364
+ message?: AzureChatResponseMessage;
365
+ index: number;
366
+ finishReason: string | null;
367
+ delta?: AzureChatResponseMessage;
368
+ }
369
+ declare interface AzureChatResponseMessage {
370
+ role: string;
371
+ content: string | null;
372
+ toolCalls: AzureChatCompletionsFunctionToolCall[];
373
+ functionCall?: AzureFunctionCall;
374
+ }
375
+ declare interface AzureCompletionsUsage {
376
+ completionTokens: number;
377
+ promptTokens: number;
378
+ totalTokens: number;
379
+ }
380
+ declare interface AzureFunctionCall {
381
+ name: string;
382
+ arguments: string;
383
+ }
384
+ declare interface AzureChatCompletionsFunctionToolCall {
385
+ type: 'function';
386
+ function: AzureFunctionCall;
387
+ id: string;
388
+ }
389
+
355
390
  type OpenAIStreamCallbacks = AIStreamCallbacksAndOptions & {
356
391
  /**
357
392
  * @example
@@ -536,7 +571,7 @@ interface CompletionUsage {
536
571
  */
537
572
  total_tokens: number;
538
573
  }
539
- type AsyncIterableOpenAIStreamReturnTypes = AsyncIterable<ChatCompletionChunk> | AsyncIterable<Completion>;
574
+ type AsyncIterableOpenAIStreamReturnTypes = AsyncIterable<ChatCompletionChunk> | AsyncIterable<Completion> | AsyncIterable<AzureChatCompletions>;
540
575
  declare function OpenAIStream(res: Response | AsyncIterableOpenAIStreamReturnTypes, callbacks?: OpenAIStreamCallbacks): ReadableStream;
541
576
 
542
577
  interface FunctionCallPayload {
@@ -760,7 +795,11 @@ declare function AWSBedrockCohereStream(response: AWSBedrockResponse, callbacks?
760
795
  declare function AWSBedrockLlama2Stream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
761
796
  declare function AWSBedrockStream(response: AWSBedrockResponse, callbacks: AIStreamCallbacksAndOptions | undefined, extractTextDeltaFromChunk: (chunk: any) => string): ReadableStream<any>;
762
797
 
763
- declare function CohereStream(reader: Response, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
798
+ interface StreamChunk {
799
+ text?: string;
800
+ eventType: 'stream-start' | 'search-queries-generation' | 'search-results' | 'text-generation' | 'citation-generation' | 'stream-end';
801
+ }
802
+ declare function CohereStream(reader: Response | AsyncIterable<StreamChunk>, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
764
803
 
765
804
  interface GenerateContentResponse {
766
805
  candidates?: GenerateContentCandidate[];
package/dist/index.js CHANGED
@@ -402,10 +402,11 @@ var experimental_StreamData = class {
402
402
  controller.enqueue(encodedData);
403
403
  }
404
404
  if (self.messageAnnotations.length) {
405
- const encodedmessageAnnotations = self.encoder.encode(
405
+ const encodedMessageAnnotations = self.encoder.encode(
406
406
  formatStreamPart("message_annotations", self.messageAnnotations)
407
407
  );
408
- controller.enqueue(encodedmessageAnnotations);
408
+ self.messageAnnotations = [];
409
+ controller.enqueue(encodedMessageAnnotations);
409
410
  }
410
411
  controller.enqueue(chunk);
411
412
  },
@@ -660,14 +661,29 @@ function createParser2(res) {
660
661
  }
661
662
  });
662
663
  }
664
+ async function* streamable2(stream) {
665
+ for await (const chunk of stream) {
666
+ if (chunk.eventType === "text-generation") {
667
+ const text = chunk.text;
668
+ if (text)
669
+ yield text;
670
+ }
671
+ }
672
+ }
663
673
  function CohereStream(reader, callbacks) {
664
- return createParser2(reader).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
665
- createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
666
- );
674
+ if (Symbol.asyncIterator in reader) {
675
+ return readableFromAsyncIterable(streamable2(reader)).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
676
+ createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
677
+ );
678
+ } else {
679
+ return createParser2(reader).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
680
+ createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
681
+ );
682
+ }
667
683
  }
668
684
 
669
685
  // streams/google-generative-ai-stream.ts
670
- async function* streamable2(response) {
686
+ async function* streamable3(response) {
671
687
  var _a, _b, _c;
672
688
  for await (const chunk of response.stream) {
673
689
  const parts = (_c = (_b = (_a = chunk.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts;
@@ -681,7 +697,7 @@ async function* streamable2(response) {
681
697
  }
682
698
  }
683
699
  function GoogleGenerativeAIStream(response, cb) {
684
- return readableFromAsyncIterable(streamable2(response)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData));
700
+ return readableFromAsyncIterable(streamable3(response)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData));
685
701
  }
686
702
 
687
703
  // streams/huggingface-stream.ts
@@ -780,9 +796,37 @@ function parseOpenAIStream() {
780
796
  const extract = chunkToText();
781
797
  return (data) => extract(JSON.parse(data));
782
798
  }
783
- async function* streamable3(stream) {
799
+ async function* streamable4(stream) {
784
800
  const extract = chunkToText();
785
- for await (const chunk of stream) {
801
+ for await (let chunk of stream) {
802
+ if ("promptFilterResults" in chunk) {
803
+ chunk = {
804
+ id: chunk.id,
805
+ created: chunk.created.getDate(),
806
+ object: chunk.object,
807
+ // not exposed by Azure API
808
+ model: chunk.model,
809
+ // not exposed by Azure API
810
+ choices: chunk.choices.map((choice) => {
811
+ var _a, _b, _c, _d, _e, _f, _g;
812
+ return {
813
+ delta: {
814
+ content: (_a = choice.delta) == null ? void 0 : _a.content,
815
+ function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
816
+ role: (_c = choice.delta) == null ? void 0 : _c.role,
817
+ tool_calls: ((_e = (_d = choice.delta) == null ? void 0 : _d.toolCalls) == null ? void 0 : _e.length) ? (_g = (_f = choice.delta) == null ? void 0 : _f.toolCalls) == null ? void 0 : _g.map((toolCall, index) => ({
818
+ index,
819
+ id: toolCall.id,
820
+ function: toolCall.function,
821
+ type: toolCall.type
822
+ })) : void 0
823
+ },
824
+ finish_reason: choice.finishReason,
825
+ index: choice.index
826
+ };
827
+ })
828
+ };
829
+ }
786
830
  const text = extract(chunk);
787
831
  if (text)
788
832
  yield text;
@@ -841,7 +885,7 @@ function OpenAIStream(res, callbacks) {
841
885
  const cb = callbacks;
842
886
  let stream;
843
887
  if (Symbol.asyncIterator in res) {
844
- stream = readableFromAsyncIterable(streamable3(res)).pipeThrough(
888
+ stream = readableFromAsyncIterable(streamable4(res)).pipeThrough(
845
889
  createCallbacksTransformer(
846
890
  (cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
847
891
  ...cb,
@@ -1112,6 +1156,11 @@ async function* readDataStream(reader, {
1112
1156
  }
1113
1157
 
1114
1158
  // shared/parse-complex-response.ts
1159
+ function assignAnnotationsToMessage(message, annotations) {
1160
+ if (!message || !annotations || !annotations.length)
1161
+ return message;
1162
+ return { ...message, annotations: [...annotations] };
1163
+ }
1115
1164
  async function parseComplexResponse({
1116
1165
  reader,
1117
1166
  abortControllerRef,
@@ -1124,6 +1173,7 @@ async function parseComplexResponse({
1124
1173
  const prefixMap = {
1125
1174
  data: []
1126
1175
  };
1176
+ let message_annotations = void 0;
1127
1177
  for await (const { type, value } of readDataStream(reader, {
1128
1178
  isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
1129
1179
  })) {
@@ -1142,22 +1192,6 @@ async function parseComplexResponse({
1142
1192
  };
1143
1193
  }
1144
1194
  }
1145
- if (type == "message_annotations") {
1146
- if (prefixMap["text"]) {
1147
- prefixMap["text"] = {
1148
- ...prefixMap["text"],
1149
- annotations: [...prefixMap["text"].annotations || [], ...value]
1150
- };
1151
- } else {
1152
- prefixMap["text"] = {
1153
- id: generateId(),
1154
- role: "assistant",
1155
- content: "",
1156
- annotations: [...value],
1157
- createdAt
1158
- };
1159
- }
1160
- }
1161
1195
  let functionCallMessage = null;
1162
1196
  if (type === "function_call") {
1163
1197
  prefixMap["function_call"] = {
@@ -1184,12 +1218,41 @@ async function parseComplexResponse({
1184
1218
  if (type === "data") {
1185
1219
  prefixMap["data"].push(...value);
1186
1220
  }
1187
- const responseMessage = prefixMap["text"];
1188
- const merged = [
1189
- functionCallMessage,
1190
- toolCallMessage,
1191
- responseMessage
1192
- ].filter(Boolean);
1221
+ let responseMessage = prefixMap["text"];
1222
+ if (type === "message_annotations") {
1223
+ if (!message_annotations) {
1224
+ message_annotations = [...value];
1225
+ } else {
1226
+ message_annotations.push(...value);
1227
+ }
1228
+ functionCallMessage = assignAnnotationsToMessage(
1229
+ prefixMap["function_call"],
1230
+ message_annotations
1231
+ );
1232
+ toolCallMessage = assignAnnotationsToMessage(
1233
+ prefixMap["tool_calls"],
1234
+ message_annotations
1235
+ );
1236
+ responseMessage = assignAnnotationsToMessage(
1237
+ prefixMap["text"],
1238
+ message_annotations
1239
+ );
1240
+ }
1241
+ if (message_annotations == null ? void 0 : message_annotations.length) {
1242
+ const messagePrefixKeys = [
1243
+ "text",
1244
+ "function_call",
1245
+ "tool_calls"
1246
+ ];
1247
+ messagePrefixKeys.forEach((key) => {
1248
+ if (prefixMap[key]) {
1249
+ prefixMap[key].annotations = [...message_annotations];
1250
+ }
1251
+ });
1252
+ }
1253
+ const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({
1254
+ ...assignAnnotationsToMessage(message, message_annotations)
1255
+ }));
1193
1256
  update(merged, [...prefixMap["data"]]);
1194
1257
  }
1195
1258
  onFinish == null ? void 0 : onFinish(prefixMap);