ai 2.2.30 → 2.2.32

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
@@ -95,6 +95,10 @@ interface Message$1 {
95
95
  * the tool call name and arguments. Otherwise, the field should not be set.
96
96
  */
97
97
  tool_calls?: string | ToolCall[];
98
+ /**
99
+ * Additional message-specific information added on the server via StreamData
100
+ */
101
+ annotations?: JSONValue[] | undefined;
98
102
  }
99
103
  type CreateMessage = Omit<Message$1, 'id'> & {
100
104
  id?: Message$1['id'];
@@ -299,7 +303,8 @@ declare const dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage
299
303
  declare const toolCallStreamPart: StreamPart<'7', 'tool_calls', {
300
304
  tool_calls: ToolCall[];
301
305
  }>;
302
- type StreamPartType = ReturnType<typeof textStreamPart.parse> | ReturnType<typeof functionCallStreamPart.parse> | ReturnType<typeof dataStreamPart.parse> | ReturnType<typeof errorStreamPart.parse> | ReturnType<typeof assistantMessageStreamPart.parse> | ReturnType<typeof assistantControlDataStreamPart.parse> | ReturnType<typeof dataMessageStreamPart.parse> | ReturnType<typeof toolCallStreamPart.parse>;
306
+ declare const messageAnnotationsStreamPart: StreamPart<'8', 'message_annotations', Array<JSONValue>>;
307
+ type StreamPartType = ReturnType<typeof textStreamPart.parse> | ReturnType<typeof functionCallStreamPart.parse> | ReturnType<typeof dataStreamPart.parse> | ReturnType<typeof errorStreamPart.parse> | ReturnType<typeof assistantMessageStreamPart.parse> | ReturnType<typeof assistantControlDataStreamPart.parse> | ReturnType<typeof dataMessageStreamPart.parse> | ReturnType<typeof toolCallStreamPart.parse> | ReturnType<typeof messageAnnotationsStreamPart.parse>;
303
308
  /**
304
309
  * The map of prefixes for data in the stream
305
310
  *
@@ -331,6 +336,7 @@ declare const StreamStringPrefixes: {
331
336
  readonly assistant_control_data: "5";
332
337
  readonly data_message: "6";
333
338
  readonly tool_calls: "7";
339
+ readonly message_annotations: "8";
334
340
  };
335
341
 
336
342
  declare const nanoid: (size?: number | undefined) => string;
@@ -339,7 +345,7 @@ declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefi
339
345
  declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
340
346
  declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
341
347
 
342
- declare const isStreamStringEqualToType: (type: keyof typeof StreamStringPrefixes, value: string) => value is `0:${string}\n` | `1:${string}\n` | `2:${string}\n` | `3:${string}\n` | `4:${string}\n` | `5:${string}\n` | `6:${string}\n` | `7:${string}\n`;
348
+ declare const isStreamStringEqualToType: (type: keyof typeof StreamStringPrefixes, value: string) => value is `0:${string}\n` | `1:${string}\n` | `2:${string}\n` | `3:${string}\n` | `4:${string}\n` | `5:${string}\n` | `6:${string}\n` | `7:${string}\n` | `8:${string}\n`;
343
349
  type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
344
350
  /**
345
351
  * A header sent to the client so it knows how to handle parsing the stream (as a deprecated text response or using the new prefixed protocol)
@@ -568,12 +574,23 @@ interface AIStreamCallbacksAndOptions {
568
574
  */
569
575
  experimental_streamData?: boolean;
570
576
  }
577
+ /**
578
+ * Options for the AIStreamParser.
579
+ * @interface
580
+ * @property {string} event - The event (type) from the server side event stream.
581
+ */
582
+ interface AIStreamParserOptions {
583
+ event?: string;
584
+ }
571
585
  /**
572
586
  * Custom parser for AIStream data.
573
587
  * @interface
588
+ * @param {string} data - The data to be parsed.
589
+ * @param {AIStreamParserOptions} options - The options for the parser.
590
+ * @returns {string | void} The parsed data or void.
574
591
  */
575
592
  interface AIStreamParser {
576
- (data: string): string | void;
593
+ (data: string, options: AIStreamParserOptions): string | void;
577
594
  }
578
595
  /**
579
596
  * Creates a TransformStream that parses events from an EventSource stream using a custom parser.
@@ -844,9 +861,11 @@ declare class experimental_StreamData {
844
861
  private isClosedPromiseResolver;
845
862
  private isClosed;
846
863
  private data;
864
+ private messageAnnotations;
847
865
  constructor();
848
866
  close(): Promise<void>;
849
867
  append(value: JSONValue): void;
868
+ appendMessageAnnotation(value: JSONValue): void;
850
869
  }
851
870
  /**
852
871
  * A TransformStream for LLMs that do not have their own transform stream handlers managing encoding (e.g. OpenAIStream has one for function call handling).
@@ -900,4 +919,4 @@ declare function streamToResponse(res: ReadableStream, response: ServerResponse,
900
919
  status?: number;
901
920
  }): void;
902
921
 
903
- export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantMessage, COMPLEX_HEADER, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CreateMessage, DataMessage, Function, FunctionCall, FunctionCallHandler, FunctionCallPayload, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, JSONValue, LangChainStream, Message$1 as Message, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamString, StreamingTextResponse, Tool, ToolCall, ToolCallHandler, ToolCallPayload, ToolChoice, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, isStreamStringEqualToType, nanoid, readableFromAsyncIterable, streamToResponse, trimStartOfStreamHelper };
922
+ export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantMessage, COMPLEX_HEADER, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CreateMessage, DataMessage, Function, FunctionCall, FunctionCallHandler, FunctionCallPayload, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, JSONValue, LangChainStream, Message$1 as Message, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamString, StreamingTextResponse, Tool, ToolCall, ToolCallHandler, ToolCallPayload, ToolChoice, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, isStreamStringEqualToType, nanoid, readableFromAsyncIterable, streamToResponse, trimStartOfStreamHelper };
package/dist/index.js CHANGED
@@ -165,6 +165,16 @@ var toolCallStreamPart = {
165
165
  };
166
166
  }
167
167
  };
168
+ var messageAnnotationsStreamPart = {
169
+ code: "8",
170
+ name: "message_annotations",
171
+ parse: (value) => {
172
+ if (!Array.isArray(value)) {
173
+ throw new Error('"message_annotations" parts expect an array value.');
174
+ }
175
+ return { type: "message_annotations", value };
176
+ }
177
+ };
168
178
  var streamParts = [
169
179
  textStreamPart,
170
180
  functionCallStreamPart,
@@ -173,7 +183,8 @@ var streamParts = [
173
183
  assistantMessageStreamPart,
174
184
  assistantControlDataStreamPart,
175
185
  dataMessageStreamPart,
176
- toolCallStreamPart
186
+ toolCallStreamPart,
187
+ messageAnnotationsStreamPart
177
188
  ];
178
189
  var streamPartsByCode = {
179
190
  [textStreamPart.code]: textStreamPart,
@@ -183,7 +194,8 @@ var streamPartsByCode = {
183
194
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
184
195
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
185
196
  [dataMessageStreamPart.code]: dataMessageStreamPart,
186
- [toolCallStreamPart.code]: toolCallStreamPart
197
+ [toolCallStreamPart.code]: toolCallStreamPart,
198
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
187
199
  };
188
200
  var StreamStringPrefixes = {
189
201
  [textStreamPart.name]: textStreamPart.code,
@@ -193,7 +205,8 @@ var StreamStringPrefixes = {
193
205
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
194
206
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
195
207
  [dataMessageStreamPart.name]: dataMessageStreamPart.code,
196
- [toolCallStreamPart.name]: toolCallStreamPart.code
208
+ [toolCallStreamPart.name]: toolCallStreamPart.code,
209
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
197
210
  };
198
211
  var validCodes = streamParts.map((part) => part.code);
199
212
  var parseStreamPart = (line) => {
@@ -257,7 +270,9 @@ function createEventStreamTransformer(customParser) {
257
270
  return;
258
271
  }
259
272
  if ("data" in event) {
260
- const parsedMessage = customParser ? customParser(event.data) : event.data;
273
+ const parsedMessage = customParser ? customParser(event.data, {
274
+ event: event.event
275
+ }) : event.data;
261
276
  if (parsedMessage)
262
277
  controller.enqueue(parsedMessage);
263
278
  }
@@ -369,6 +384,7 @@ var experimental_StreamData = class {
369
384
  this.isClosed = false;
370
385
  // array to store appended data
371
386
  this.data = [];
387
+ this.messageAnnotations = [];
372
388
  this.isClosedPromise = new Promise((resolve) => {
373
389
  this.isClosedPromiseResolver = resolve;
374
390
  });
@@ -385,6 +401,12 @@ var experimental_StreamData = class {
385
401
  self.data = [];
386
402
  controller.enqueue(encodedData);
387
403
  }
404
+ if (self.messageAnnotations.length) {
405
+ const encodedmessageAnnotations = self.encoder.encode(
406
+ formatStreamPart("message_annotations", self.messageAnnotations)
407
+ );
408
+ controller.enqueue(encodedmessageAnnotations);
409
+ }
388
410
  controller.enqueue(chunk);
389
411
  },
390
412
  async flush(controller) {
@@ -403,6 +425,12 @@ var experimental_StreamData = class {
403
425
  );
404
426
  controller.enqueue(encodedData);
405
427
  }
428
+ if (self.messageAnnotations.length) {
429
+ const encodedData = self.encoder.encode(
430
+ formatStreamPart("message_annotations", self.messageAnnotations)
431
+ );
432
+ controller.enqueue(encodedData);
433
+ }
406
434
  }
407
435
  });
408
436
  }
@@ -423,6 +451,12 @@ var experimental_StreamData = class {
423
451
  }
424
452
  this.data.push(value);
425
453
  }
454
+ appendMessageAnnotation(value) {
455
+ if (this.isClosed) {
456
+ throw new Error("Data Stream has already been closed.");
457
+ }
458
+ this.messageAnnotations.push(value);
459
+ }
426
460
  };
427
461
  function createStreamDataTransformer(experimental_streamData) {
428
462
  if (!experimental_streamData) {
@@ -1108,6 +1142,22 @@ async function parseComplexResponse({
1108
1142
  };
1109
1143
  }
1110
1144
  }
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
+ }
1111
1161
  let functionCallMessage = null;
1112
1162
  if (type === "function_call") {
1113
1163
  prefixMap["function_call"] = {