ai 2.2.31 → 2.2.33
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 +59 -5
- package/dist/index.js +83 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +83 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/prompts/dist/index.d.ts +4 -0
- package/react/dist/index.d.ts +11 -1
- package/react/dist/index.js +39 -5
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +39 -5
- package/react/dist/index.mjs.map +1 -1
- package/solid/dist/index.d.ts +4 -0
- package/solid/dist/index.js +32 -3
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +32 -3
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.ts +4 -0
- package/svelte/dist/index.js +32 -3
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +32 -3
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.ts +4 -0
- package/vue/dist/index.js +32 -3
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +32 -3
- package/vue/dist/index.mjs.map +1 -1
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
|
-
|
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,13 +345,48 @@ 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)
|
346
352
|
*/
|
347
353
|
declare const COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
348
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
|
+
|
349
390
|
type OpenAIStreamCallbacks = AIStreamCallbacksAndOptions & {
|
350
391
|
/**
|
351
392
|
* @example
|
@@ -530,7 +571,7 @@ interface CompletionUsage {
|
|
530
571
|
*/
|
531
572
|
total_tokens: number;
|
532
573
|
}
|
533
|
-
type AsyncIterableOpenAIStreamReturnTypes = AsyncIterable<ChatCompletionChunk> | AsyncIterable<Completion>;
|
574
|
+
type AsyncIterableOpenAIStreamReturnTypes = AsyncIterable<ChatCompletionChunk> | AsyncIterable<Completion> | AsyncIterable<AzureChatCompletions>;
|
534
575
|
declare function OpenAIStream(res: Response | AsyncIterableOpenAIStreamReturnTypes, callbacks?: OpenAIStreamCallbacks): ReadableStream;
|
535
576
|
|
536
577
|
interface FunctionCallPayload {
|
@@ -568,12 +609,23 @@ interface AIStreamCallbacksAndOptions {
|
|
568
609
|
*/
|
569
610
|
experimental_streamData?: boolean;
|
570
611
|
}
|
612
|
+
/**
|
613
|
+
* Options for the AIStreamParser.
|
614
|
+
* @interface
|
615
|
+
* @property {string} event - The event (type) from the server side event stream.
|
616
|
+
*/
|
617
|
+
interface AIStreamParserOptions {
|
618
|
+
event?: string;
|
619
|
+
}
|
571
620
|
/**
|
572
621
|
* Custom parser for AIStream data.
|
573
622
|
* @interface
|
623
|
+
* @param {string} data - The data to be parsed.
|
624
|
+
* @param {AIStreamParserOptions} options - The options for the parser.
|
625
|
+
* @returns {string | void} The parsed data or void.
|
574
626
|
*/
|
575
627
|
interface AIStreamParser {
|
576
|
-
(data: string): string | void;
|
628
|
+
(data: string, options: AIStreamParserOptions): string | void;
|
577
629
|
}
|
578
630
|
/**
|
579
631
|
* Creates a TransformStream that parses events from an EventSource stream using a custom parser.
|
@@ -844,9 +896,11 @@ declare class experimental_StreamData {
|
|
844
896
|
private isClosedPromiseResolver;
|
845
897
|
private isClosed;
|
846
898
|
private data;
|
899
|
+
private messageAnnotations;
|
847
900
|
constructor();
|
848
901
|
close(): Promise<void>;
|
849
902
|
append(value: JSONValue): void;
|
903
|
+
appendMessageAnnotation(value: JSONValue): void;
|
850
904
|
}
|
851
905
|
/**
|
852
906
|
* 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 +954,4 @@ declare function streamToResponse(res: ReadableStream, response: ServerResponse,
|
|
900
954
|
status?: number;
|
901
955
|
}): void;
|
902
956
|
|
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 };
|
957
|
+
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
|
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) {
|
@@ -748,7 +782,35 @@ function parseOpenAIStream() {
|
|
748
782
|
}
|
749
783
|
async function* streamable3(stream) {
|
750
784
|
const extract = chunkToText();
|
751
|
-
for await (
|
785
|
+
for await (let chunk of stream) {
|
786
|
+
if ("promptFilterResults" in chunk) {
|
787
|
+
chunk = {
|
788
|
+
id: chunk.id,
|
789
|
+
created: chunk.created.getDate(),
|
790
|
+
object: chunk.object,
|
791
|
+
// not exposed by Azure API
|
792
|
+
model: chunk.model,
|
793
|
+
// not exposed by Azure API
|
794
|
+
choices: chunk.choices.map((choice) => {
|
795
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
796
|
+
return {
|
797
|
+
delta: {
|
798
|
+
content: (_a = choice.delta) == null ? void 0 : _a.content,
|
799
|
+
function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
|
800
|
+
role: (_c = choice.delta) == null ? void 0 : _c.role,
|
801
|
+
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) => ({
|
802
|
+
index,
|
803
|
+
id: toolCall.id,
|
804
|
+
function: toolCall.function,
|
805
|
+
type: toolCall.type
|
806
|
+
})) : void 0
|
807
|
+
},
|
808
|
+
finish_reason: choice.finishReason,
|
809
|
+
index: choice.index
|
810
|
+
};
|
811
|
+
})
|
812
|
+
};
|
813
|
+
}
|
752
814
|
const text = extract(chunk);
|
753
815
|
if (text)
|
754
816
|
yield text;
|
@@ -1108,6 +1170,22 @@ async function parseComplexResponse({
|
|
1108
1170
|
};
|
1109
1171
|
}
|
1110
1172
|
}
|
1173
|
+
if (type == "message_annotations") {
|
1174
|
+
if (prefixMap["text"]) {
|
1175
|
+
prefixMap["text"] = {
|
1176
|
+
...prefixMap["text"],
|
1177
|
+
annotations: [...prefixMap["text"].annotations || [], ...value]
|
1178
|
+
};
|
1179
|
+
} else {
|
1180
|
+
prefixMap["text"] = {
|
1181
|
+
id: generateId(),
|
1182
|
+
role: "assistant",
|
1183
|
+
content: "",
|
1184
|
+
annotations: [...value],
|
1185
|
+
createdAt
|
1186
|
+
};
|
1187
|
+
}
|
1188
|
+
}
|
1111
1189
|
let functionCallMessage = null;
|
1112
1190
|
if (type === "function_call") {
|
1113
1191
|
prefixMap["function_call"] = {
|