ai 2.2.35 → 2.2.36
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 +159 -159
- package/dist/index.js +731 -728
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +733 -730
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
@@ -280,6 +280,78 @@ type DataMessage = {
|
|
280
280
|
data: JSONValue;
|
281
281
|
};
|
282
282
|
|
283
|
+
interface StreamPart<CODE extends string, NAME extends string, TYPE> {
|
284
|
+
code: CODE;
|
285
|
+
name: NAME;
|
286
|
+
parse: (value: JSONValue) => {
|
287
|
+
type: NAME;
|
288
|
+
value: TYPE;
|
289
|
+
};
|
290
|
+
}
|
291
|
+
declare const textStreamPart: StreamPart<'0', 'text', string>;
|
292
|
+
declare const functionCallStreamPart: StreamPart<'1', 'function_call', {
|
293
|
+
function_call: FunctionCall;
|
294
|
+
}>;
|
295
|
+
declare const dataStreamPart: StreamPart<'2', 'data', Array<JSONValue>>;
|
296
|
+
declare const errorStreamPart: StreamPart<'3', 'error', string>;
|
297
|
+
declare const assistantMessageStreamPart: StreamPart<'4', 'assistant_message', AssistantMessage>;
|
298
|
+
declare const assistantControlDataStreamPart: StreamPart<'5', 'assistant_control_data', {
|
299
|
+
threadId: string;
|
300
|
+
messageId: string;
|
301
|
+
}>;
|
302
|
+
declare const dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage>;
|
303
|
+
declare const toolCallStreamPart: StreamPart<'7', 'tool_calls', {
|
304
|
+
tool_calls: ToolCall[];
|
305
|
+
}>;
|
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>;
|
308
|
+
/**
|
309
|
+
* The map of prefixes for data in the stream
|
310
|
+
*
|
311
|
+
* - 0: Text from the LLM response
|
312
|
+
* - 1: (OpenAI) function_call responses
|
313
|
+
* - 2: custom JSON added by the user using `Data`
|
314
|
+
* - 6: (OpenAI) tool_call responses
|
315
|
+
*
|
316
|
+
* Example:
|
317
|
+
* ```
|
318
|
+
* 0:Vercel
|
319
|
+
* 0:'s
|
320
|
+
* 0: AI
|
321
|
+
* 0: AI
|
322
|
+
* 0: SDK
|
323
|
+
* 0: is great
|
324
|
+
* 0:!
|
325
|
+
* 2: { "someJson": "value" }
|
326
|
+
* 1: {"function_call": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}
|
327
|
+
* 6: {"tool_call": {"id": "tool_0", "type": "function", "function": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}}
|
328
|
+
*```
|
329
|
+
*/
|
330
|
+
declare const StreamStringPrefixes: {
|
331
|
+
readonly text: "0";
|
332
|
+
readonly function_call: "1";
|
333
|
+
readonly data: "2";
|
334
|
+
readonly error: "3";
|
335
|
+
readonly assistant_message: "4";
|
336
|
+
readonly assistant_control_data: "5";
|
337
|
+
readonly data_message: "6";
|
338
|
+
readonly tool_calls: "7";
|
339
|
+
readonly message_annotations: "8";
|
340
|
+
};
|
341
|
+
|
342
|
+
declare const nanoid: (size?: number | undefined) => string;
|
343
|
+
declare function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
|
344
|
+
declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefined) => string;
|
345
|
+
declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
|
346
|
+
declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
|
347
|
+
|
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`;
|
349
|
+
type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
|
350
|
+
/**
|
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)
|
352
|
+
*/
|
353
|
+
declare const COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
354
|
+
|
283
355
|
declare interface AzureChatCompletions {
|
284
356
|
id: string;
|
285
357
|
created: Date;
|
@@ -625,63 +697,6 @@ declare function AIStream(response: Response, customParser?: AIStreamParser, cal
|
|
625
697
|
*/
|
626
698
|
declare function readableFromAsyncIterable<T>(iterable: AsyncIterable<T>): ReadableStream<T>;
|
627
699
|
|
628
|
-
interface AWSBedrockResponse {
|
629
|
-
body?: AsyncIterable<{
|
630
|
-
chunk?: {
|
631
|
-
bytes?: Uint8Array;
|
632
|
-
};
|
633
|
-
}>;
|
634
|
-
}
|
635
|
-
declare function AWSBedrockAnthropicStream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
|
636
|
-
declare function AWSBedrockCohereStream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
|
637
|
-
declare function AWSBedrockLlama2Stream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
|
638
|
-
declare function AWSBedrockStream(response: AWSBedrockResponse, callbacks: AIStreamCallbacksAndOptions | undefined, extractTextDeltaFromChunk: (chunk: any) => string): ReadableStream<any>;
|
639
|
-
|
640
|
-
/**
|
641
|
-
* A stream wrapper to send custom JSON-encoded data back to the client.
|
642
|
-
*/
|
643
|
-
declare class experimental_StreamData {
|
644
|
-
private encoder;
|
645
|
-
private controller;
|
646
|
-
stream: TransformStream<Uint8Array, Uint8Array>;
|
647
|
-
private isClosedPromise;
|
648
|
-
private isClosedPromiseResolver;
|
649
|
-
private isClosed;
|
650
|
-
private data;
|
651
|
-
private messageAnnotations;
|
652
|
-
constructor();
|
653
|
-
close(): Promise<void>;
|
654
|
-
append(value: JSONValue): void;
|
655
|
-
appendMessageAnnotation(value: JSONValue): void;
|
656
|
-
}
|
657
|
-
/**
|
658
|
-
* A TransformStream for LLMs that do not have their own transform stream handlers managing encoding (e.g. OpenAIStream has one for function call handling).
|
659
|
-
* This assumes every chunk is a 'text' chunk.
|
660
|
-
*/
|
661
|
-
declare function createStreamDataTransformer(experimental_streamData: boolean | undefined): TransformStream<any, any>;
|
662
|
-
|
663
|
-
/**
|
664
|
-
* A utility class for streaming text responses.
|
665
|
-
*/
|
666
|
-
declare class StreamingTextResponse extends Response {
|
667
|
-
constructor(res: ReadableStream, init?: ResponseInit, data?: experimental_StreamData);
|
668
|
-
}
|
669
|
-
/**
|
670
|
-
* A utility function to stream a ReadableStream to a Node.js response-like object.
|
671
|
-
*/
|
672
|
-
declare function streamToResponse(res: ReadableStream, response: ServerResponse, init?: {
|
673
|
-
headers?: Record<string, string>;
|
674
|
-
status?: number;
|
675
|
-
}): void;
|
676
|
-
|
677
|
-
declare function HuggingFaceStream(res: AsyncGenerator<any>, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
|
678
|
-
|
679
|
-
interface StreamChunk {
|
680
|
-
text?: string;
|
681
|
-
eventType: 'stream-start' | 'search-queries-generation' | 'search-results' | 'text-generation' | 'citation-generation' | 'stream-end';
|
682
|
-
}
|
683
|
-
declare function CohereStream(reader: Response | AsyncIterable<StreamChunk>, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
|
684
|
-
|
685
700
|
interface CompletionChunk {
|
686
701
|
/**
|
687
702
|
* The resulting completion up to and excluding the stop sequences.
|
@@ -756,6 +771,61 @@ interface MessageStopEvent {
|
|
756
771
|
*/
|
757
772
|
declare function AnthropicStream(res: Response | AsyncIterable<CompletionChunk> | AsyncIterable<MessageStreamEvent>, cb?: AIStreamCallbacksAndOptions): ReadableStream;
|
758
773
|
|
774
|
+
type AssistantResponseSettings = {
|
775
|
+
threadId: string;
|
776
|
+
messageId: string;
|
777
|
+
};
|
778
|
+
type AssistantResponseCallback = (stream: {
|
779
|
+
threadId: string;
|
780
|
+
messageId: string;
|
781
|
+
sendMessage: (message: AssistantMessage) => void;
|
782
|
+
sendDataMessage: (message: DataMessage) => void;
|
783
|
+
}) => Promise<void>;
|
784
|
+
declare function experimental_AssistantResponse({ threadId, messageId }: AssistantResponseSettings, process: AssistantResponseCallback): Response;
|
785
|
+
|
786
|
+
interface AWSBedrockResponse {
|
787
|
+
body?: AsyncIterable<{
|
788
|
+
chunk?: {
|
789
|
+
bytes?: Uint8Array;
|
790
|
+
};
|
791
|
+
}>;
|
792
|
+
}
|
793
|
+
declare function AWSBedrockAnthropicStream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
|
794
|
+
declare function AWSBedrockCohereStream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
|
795
|
+
declare function AWSBedrockLlama2Stream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
|
796
|
+
declare function AWSBedrockStream(response: AWSBedrockResponse, callbacks: AIStreamCallbacksAndOptions | undefined, extractTextDeltaFromChunk: (chunk: any) => string): ReadableStream<any>;
|
797
|
+
|
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;
|
803
|
+
|
804
|
+
interface GenerateContentResponse {
|
805
|
+
candidates?: GenerateContentCandidate[];
|
806
|
+
}
|
807
|
+
interface GenerateContentCandidate {
|
808
|
+
index: number;
|
809
|
+
content: Content;
|
810
|
+
}
|
811
|
+
interface Content {
|
812
|
+
role: string;
|
813
|
+
parts: Part[];
|
814
|
+
}
|
815
|
+
type Part = TextPart | InlineDataPart;
|
816
|
+
interface InlineDataPart {
|
817
|
+
text?: never;
|
818
|
+
}
|
819
|
+
interface TextPart {
|
820
|
+
text: string;
|
821
|
+
inlineData?: never;
|
822
|
+
}
|
823
|
+
declare function GoogleGenerativeAIStream(response: {
|
824
|
+
stream: AsyncIterable<GenerateContentResponse>;
|
825
|
+
}, cb?: AIStreamCallbacksAndOptions): ReadableStream;
|
826
|
+
|
827
|
+
declare function HuggingFaceStream(res: AsyncGenerator<any>, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
|
828
|
+
|
759
829
|
type InkeepOnFinalMetadata = {
|
760
830
|
chat_session_id: string;
|
761
831
|
records_cited: any;
|
@@ -830,112 +900,28 @@ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptio
|
|
830
900
|
headers?: Record<string, string>;
|
831
901
|
}): Promise<ReadableStream>;
|
832
902
|
|
833
|
-
interface StreamPart<CODE extends string, NAME extends string, TYPE> {
|
834
|
-
code: CODE;
|
835
|
-
name: NAME;
|
836
|
-
parse: (value: JSONValue) => {
|
837
|
-
type: NAME;
|
838
|
-
value: TYPE;
|
839
|
-
};
|
840
|
-
}
|
841
|
-
declare const textStreamPart: StreamPart<'0', 'text', string>;
|
842
|
-
declare const functionCallStreamPart: StreamPart<'1', 'function_call', {
|
843
|
-
function_call: FunctionCall;
|
844
|
-
}>;
|
845
|
-
declare const dataStreamPart: StreamPart<'2', 'data', Array<JSONValue>>;
|
846
|
-
declare const errorStreamPart: StreamPart<'3', 'error', string>;
|
847
|
-
declare const assistantMessageStreamPart: StreamPart<'4', 'assistant_message', AssistantMessage>;
|
848
|
-
declare const assistantControlDataStreamPart: StreamPart<'5', 'assistant_control_data', {
|
849
|
-
threadId: string;
|
850
|
-
messageId: string;
|
851
|
-
}>;
|
852
|
-
declare const dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage>;
|
853
|
-
declare const toolCallStreamPart: StreamPart<'7', 'tool_calls', {
|
854
|
-
tool_calls: ToolCall[];
|
855
|
-
}>;
|
856
|
-
declare const messageAnnotationsStreamPart: StreamPart<'8', 'message_annotations', Array<JSONValue>>;
|
857
|
-
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>;
|
858
903
|
/**
|
859
|
-
*
|
860
|
-
*
|
861
|
-
* - 0: Text from the LLM response
|
862
|
-
* - 1: (OpenAI) function_call responses
|
863
|
-
* - 2: custom JSON added by the user using `Data`
|
864
|
-
* - 6: (OpenAI) tool_call responses
|
865
|
-
*
|
866
|
-
* Example:
|
867
|
-
* ```
|
868
|
-
* 0:Vercel
|
869
|
-
* 0:'s
|
870
|
-
* 0: AI
|
871
|
-
* 0: AI
|
872
|
-
* 0: SDK
|
873
|
-
* 0: is great
|
874
|
-
* 0:!
|
875
|
-
* 2: { "someJson": "value" }
|
876
|
-
* 1: {"function_call": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}
|
877
|
-
* 6: {"tool_call": {"id": "tool_0", "type": "function", "function": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}}
|
878
|
-
*```
|
904
|
+
* A stream wrapper to send custom JSON-encoded data back to the client.
|
879
905
|
*/
|
880
|
-
declare
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefined) => string;
|
895
|
-
declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
|
896
|
-
declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
|
897
|
-
|
898
|
-
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`;
|
899
|
-
type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
|
906
|
+
declare class experimental_StreamData {
|
907
|
+
private encoder;
|
908
|
+
private controller;
|
909
|
+
stream: TransformStream<Uint8Array, Uint8Array>;
|
910
|
+
private isClosedPromise;
|
911
|
+
private isClosedPromiseResolver;
|
912
|
+
private isClosed;
|
913
|
+
private data;
|
914
|
+
private messageAnnotations;
|
915
|
+
constructor();
|
916
|
+
close(): Promise<void>;
|
917
|
+
append(value: JSONValue): void;
|
918
|
+
appendMessageAnnotation(value: JSONValue): void;
|
919
|
+
}
|
900
920
|
/**
|
901
|
-
* A
|
921
|
+
* A TransformStream for LLMs that do not have their own transform stream handlers managing encoding (e.g. OpenAIStream has one for function call handling).
|
922
|
+
* This assumes every chunk is a 'text' chunk.
|
902
923
|
*/
|
903
|
-
declare
|
904
|
-
|
905
|
-
type AssistantResponseSettings = {
|
906
|
-
threadId: string;
|
907
|
-
messageId: string;
|
908
|
-
};
|
909
|
-
type AssistantResponseCallback = (stream: {
|
910
|
-
threadId: string;
|
911
|
-
messageId: string;
|
912
|
-
sendMessage: (message: AssistantMessage) => void;
|
913
|
-
sendDataMessage: (message: DataMessage) => void;
|
914
|
-
}) => Promise<void>;
|
915
|
-
declare function experimental_AssistantResponse({ threadId, messageId }: AssistantResponseSettings, process: AssistantResponseCallback): Response;
|
916
|
-
|
917
|
-
interface GenerateContentResponse {
|
918
|
-
candidates?: GenerateContentCandidate[];
|
919
|
-
}
|
920
|
-
interface GenerateContentCandidate {
|
921
|
-
index: number;
|
922
|
-
content: Content;
|
923
|
-
}
|
924
|
-
interface Content {
|
925
|
-
role: string;
|
926
|
-
parts: Part[];
|
927
|
-
}
|
928
|
-
type Part = TextPart | InlineDataPart;
|
929
|
-
interface InlineDataPart {
|
930
|
-
text?: never;
|
931
|
-
}
|
932
|
-
interface TextPart {
|
933
|
-
text: string;
|
934
|
-
inlineData?: never;
|
935
|
-
}
|
936
|
-
declare function GoogleGenerativeAIStream(response: {
|
937
|
-
stream: AsyncIterable<GenerateContentResponse>;
|
938
|
-
}, cb?: AIStreamCallbacksAndOptions): ReadableStream;
|
924
|
+
declare function createStreamDataTransformer(experimental_streamData: boolean | undefined): TransformStream<any, any>;
|
939
925
|
|
940
926
|
/**
|
941
927
|
* This is a naive implementation of the streaming React response API.
|
@@ -969,4 +955,18 @@ declare class experimental_StreamingReactResponse {
|
|
969
955
|
});
|
970
956
|
}
|
971
957
|
|
958
|
+
/**
|
959
|
+
* A utility class for streaming text responses.
|
960
|
+
*/
|
961
|
+
declare class StreamingTextResponse extends Response {
|
962
|
+
constructor(res: ReadableStream, init?: ResponseInit, data?: experimental_StreamData);
|
963
|
+
}
|
964
|
+
/**
|
965
|
+
* A utility function to stream a ReadableStream to a Node.js response-like object.
|
966
|
+
*/
|
967
|
+
declare function streamToResponse(res: ReadableStream, response: ServerResponse, init?: {
|
968
|
+
headers?: Record<string, string>;
|
969
|
+
status?: number;
|
970
|
+
}): void;
|
971
|
+
|
972
972
|
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, InkeepAIStreamCallbacksAndOptions, InkeepChatResultCallbacks, InkeepOnFinalMetadata, InkeepStream, 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 };
|