@ai-sdk/react 0.0.0-01d6317c-20260129172110
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/CHANGELOG.md +3610 -0
- package/LICENSE +13 -0
- package/README.md +7 -0
- package/dist/index.d.mts +178 -0
- package/dist/index.d.ts +178 -0
- package/dist/index.js +538 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +506 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +84 -0
- package/src/chat.react.ts +130 -0
- package/src/index.ts +4 -0
- package/src/setup-test-component.tsx +26 -0
- package/src/throttle.ts +8 -0
- package/src/use-chat.ts +173 -0
- package/src/use-completion.ts +208 -0
- package/src/use-object.ts +269 -0
- package/src/util/use-stable-value.ts +18 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2023 Vercel, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# AI SDK: React provider
|
|
2
|
+
|
|
3
|
+
[React](https://react.dev/) UI components for the [AI SDK](https://ai-sdk.dev/docs):
|
|
4
|
+
|
|
5
|
+
- [`useChat`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat) hook
|
|
6
|
+
- [`useCompletion`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-completion) hook
|
|
7
|
+
- [`useObject`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-object) hook
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { UIMessage, AbstractChat, ChatInit, CompletionRequestOptions, UseCompletionOptions, DeepPartial } from 'ai';
|
|
2
|
+
export { CreateUIMessage, UIMessage, UseCompletionOptions } from 'ai';
|
|
3
|
+
import { FlexibleSchema, FetchFunction, Resolvable, InferSchema } from '@ai-sdk/provider-utils';
|
|
4
|
+
|
|
5
|
+
declare class Chat<UI_MESSAGE extends UIMessage> extends AbstractChat<UI_MESSAGE> {
|
|
6
|
+
#private;
|
|
7
|
+
constructor({ messages, ...init }: ChatInit<UI_MESSAGE>);
|
|
8
|
+
'~registerMessagesCallback': (onChange: () => void, throttleWaitMs?: number) => (() => void);
|
|
9
|
+
'~registerStatusCallback': (onChange: () => void) => (() => void);
|
|
10
|
+
'~registerErrorCallback': (onChange: () => void) => (() => void);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type UseChatHelpers<UI_MESSAGE extends UIMessage> = {
|
|
14
|
+
/**
|
|
15
|
+
* The id of the chat.
|
|
16
|
+
*/
|
|
17
|
+
readonly id: string;
|
|
18
|
+
/**
|
|
19
|
+
* Update the `messages` state locally. This is useful when you want to
|
|
20
|
+
* edit the messages on the client, and then trigger the `reload` method
|
|
21
|
+
* manually to regenerate the AI response.
|
|
22
|
+
*/
|
|
23
|
+
setMessages: (messages: UI_MESSAGE[] | ((messages: UI_MESSAGE[]) => UI_MESSAGE[])) => void;
|
|
24
|
+
error: Error | undefined;
|
|
25
|
+
} & Pick<AbstractChat<UI_MESSAGE>, 'sendMessage' | 'regenerate' | 'stop' | 'resumeStream' | 'addToolResult' | 'addToolOutput' | 'addToolApprovalResponse' | 'status' | 'messages' | 'clearError'>;
|
|
26
|
+
type UseChatOptions<UI_MESSAGE extends UIMessage> = ({
|
|
27
|
+
chat: Chat<UI_MESSAGE>;
|
|
28
|
+
} | ChatInit<UI_MESSAGE>) & {
|
|
29
|
+
/**
|
|
30
|
+
* Custom throttle wait in ms for the chat messages and data updates.
|
|
31
|
+
* Default is undefined, which disables throttling.
|
|
32
|
+
*/
|
|
33
|
+
experimental_throttle?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Whether to resume an ongoing chat generation stream.
|
|
36
|
+
*/
|
|
37
|
+
resume?: boolean;
|
|
38
|
+
};
|
|
39
|
+
declare function useChat<UI_MESSAGE extends UIMessage = UIMessage>({ experimental_throttle: throttleWaitMs, resume, ...options }?: UseChatOptions<UI_MESSAGE>): UseChatHelpers<UI_MESSAGE>;
|
|
40
|
+
|
|
41
|
+
type UseCompletionHelpers = {
|
|
42
|
+
/** The current completion result */
|
|
43
|
+
completion: string;
|
|
44
|
+
/**
|
|
45
|
+
* Send a new prompt to the API endpoint and update the completion state.
|
|
46
|
+
*/
|
|
47
|
+
complete: (prompt: string, options?: CompletionRequestOptions) => Promise<string | null | undefined>;
|
|
48
|
+
/** The error object of the API request */
|
|
49
|
+
error: undefined | Error;
|
|
50
|
+
/**
|
|
51
|
+
* Abort the current API request but keep the generated tokens.
|
|
52
|
+
*/
|
|
53
|
+
stop: () => void;
|
|
54
|
+
/**
|
|
55
|
+
* Update the `completion` state locally.
|
|
56
|
+
*/
|
|
57
|
+
setCompletion: (completion: string) => void;
|
|
58
|
+
/** The current value of the input */
|
|
59
|
+
input: string;
|
|
60
|
+
/** setState-powered method to update the input value */
|
|
61
|
+
setInput: React.Dispatch<React.SetStateAction<string>>;
|
|
62
|
+
/**
|
|
63
|
+
* An input/textarea-ready onChange handler to control the value of the input
|
|
64
|
+
* @example
|
|
65
|
+
* ```jsx
|
|
66
|
+
* <input onChange={handleInputChange} value={input} />
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
handleInputChange: (event: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
|
|
70
|
+
/**
|
|
71
|
+
* Form submission handler to automatically reset input and append a user message
|
|
72
|
+
* @example
|
|
73
|
+
* ```jsx
|
|
74
|
+
* <form onSubmit={handleSubmit}>
|
|
75
|
+
* <input onChange={handleInputChange} value={input} />
|
|
76
|
+
* </form>
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
handleSubmit: (event?: {
|
|
80
|
+
preventDefault?: () => void;
|
|
81
|
+
}) => void;
|
|
82
|
+
/** Whether the API request is in progress */
|
|
83
|
+
isLoading: boolean;
|
|
84
|
+
};
|
|
85
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamProtocol, fetch, onFinish, onError, experimental_throttle: throttleWaitMs, }?: UseCompletionOptions & {
|
|
86
|
+
/**
|
|
87
|
+
* Custom throttle wait in ms for the completion and data updates.
|
|
88
|
+
* Default is undefined, which disables throttling.
|
|
89
|
+
*/
|
|
90
|
+
experimental_throttle?: number;
|
|
91
|
+
}): UseCompletionHelpers;
|
|
92
|
+
|
|
93
|
+
type Experimental_UseObjectOptions<SCHEMA extends FlexibleSchema, RESULT> = {
|
|
94
|
+
/**
|
|
95
|
+
* The API endpoint. It should stream JSON that matches the schema as chunked text.
|
|
96
|
+
*/
|
|
97
|
+
api: string;
|
|
98
|
+
/**
|
|
99
|
+
* A schema that defines the shape of the complete object.
|
|
100
|
+
*/
|
|
101
|
+
schema: SCHEMA;
|
|
102
|
+
/**
|
|
103
|
+
* An unique identifier. If not provided, a random one will be
|
|
104
|
+
* generated. When provided, the `useObject` hook with the same `id` will
|
|
105
|
+
* have shared states across components.
|
|
106
|
+
*/
|
|
107
|
+
id?: string;
|
|
108
|
+
/**
|
|
109
|
+
* An optional value for the initial object.
|
|
110
|
+
*/
|
|
111
|
+
initialValue?: DeepPartial<RESULT>;
|
|
112
|
+
/**
|
|
113
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
114
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
115
|
+
*/
|
|
116
|
+
fetch?: FetchFunction;
|
|
117
|
+
/**
|
|
118
|
+
* Callback that is called when the stream has finished.
|
|
119
|
+
*/
|
|
120
|
+
onFinish?: (event: {
|
|
121
|
+
/**
|
|
122
|
+
* The generated object (typed according to the schema).
|
|
123
|
+
* Can be undefined if the final object does not match the schema.
|
|
124
|
+
*/
|
|
125
|
+
object: RESULT | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.
|
|
128
|
+
*/
|
|
129
|
+
error: Error | undefined;
|
|
130
|
+
}) => Promise<void> | void;
|
|
131
|
+
/**
|
|
132
|
+
* Callback function to be called when an error is encountered.
|
|
133
|
+
*/
|
|
134
|
+
onError?: (error: Error) => void;
|
|
135
|
+
/**
|
|
136
|
+
* Additional HTTP headers to be included in the request.
|
|
137
|
+
* Can be a static object, a function that returns headers, or an async function
|
|
138
|
+
* for dynamic auth tokens.
|
|
139
|
+
*/
|
|
140
|
+
headers?: Resolvable<Record<string, string> | Headers>;
|
|
141
|
+
/**
|
|
142
|
+
* The credentials mode to be used for the fetch request.
|
|
143
|
+
* Possible values are: 'omit', 'same-origin', 'include'.
|
|
144
|
+
* Defaults to 'same-origin'.
|
|
145
|
+
*/
|
|
146
|
+
credentials?: RequestCredentials;
|
|
147
|
+
};
|
|
148
|
+
type Experimental_UseObjectHelpers<RESULT, INPUT> = {
|
|
149
|
+
/**
|
|
150
|
+
* Calls the API with the provided input as JSON body.
|
|
151
|
+
*/
|
|
152
|
+
submit: (input: INPUT) => void;
|
|
153
|
+
/**
|
|
154
|
+
* The current value for the generated object. Updated as the API streams JSON chunks.
|
|
155
|
+
*/
|
|
156
|
+
object: DeepPartial<RESULT> | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* The error object of the API request if any.
|
|
159
|
+
*/
|
|
160
|
+
error: Error | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* Flag that indicates whether an API request is in progress.
|
|
163
|
+
*/
|
|
164
|
+
isLoading: boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Abort the current request immediately, keep the current partial object if any.
|
|
167
|
+
*/
|
|
168
|
+
stop: () => void;
|
|
169
|
+
/**
|
|
170
|
+
* Clear the object state.
|
|
171
|
+
*/
|
|
172
|
+
clear: () => void;
|
|
173
|
+
};
|
|
174
|
+
declare function useObject<SCHEMA extends FlexibleSchema, RESULT = InferSchema<SCHEMA>, INPUT = any>({ api, id, schema, // required, in the future we will use it for validation
|
|
175
|
+
initialValue, fetch, onError, onFinish, headers, credentials, }: Experimental_UseObjectOptions<SCHEMA, RESULT>): Experimental_UseObjectHelpers<RESULT, INPUT>;
|
|
176
|
+
declare const experimental_useObject: typeof useObject;
|
|
177
|
+
|
|
178
|
+
export { Chat, Experimental_UseObjectHelpers, Experimental_UseObjectOptions, UseChatHelpers, UseChatOptions, UseCompletionHelpers, experimental_useObject, useChat, useCompletion };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { UIMessage, AbstractChat, ChatInit, CompletionRequestOptions, UseCompletionOptions, DeepPartial } from 'ai';
|
|
2
|
+
export { CreateUIMessage, UIMessage, UseCompletionOptions } from 'ai';
|
|
3
|
+
import { FlexibleSchema, FetchFunction, Resolvable, InferSchema } from '@ai-sdk/provider-utils';
|
|
4
|
+
|
|
5
|
+
declare class Chat<UI_MESSAGE extends UIMessage> extends AbstractChat<UI_MESSAGE> {
|
|
6
|
+
#private;
|
|
7
|
+
constructor({ messages, ...init }: ChatInit<UI_MESSAGE>);
|
|
8
|
+
'~registerMessagesCallback': (onChange: () => void, throttleWaitMs?: number) => (() => void);
|
|
9
|
+
'~registerStatusCallback': (onChange: () => void) => (() => void);
|
|
10
|
+
'~registerErrorCallback': (onChange: () => void) => (() => void);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type UseChatHelpers<UI_MESSAGE extends UIMessage> = {
|
|
14
|
+
/**
|
|
15
|
+
* The id of the chat.
|
|
16
|
+
*/
|
|
17
|
+
readonly id: string;
|
|
18
|
+
/**
|
|
19
|
+
* Update the `messages` state locally. This is useful when you want to
|
|
20
|
+
* edit the messages on the client, and then trigger the `reload` method
|
|
21
|
+
* manually to regenerate the AI response.
|
|
22
|
+
*/
|
|
23
|
+
setMessages: (messages: UI_MESSAGE[] | ((messages: UI_MESSAGE[]) => UI_MESSAGE[])) => void;
|
|
24
|
+
error: Error | undefined;
|
|
25
|
+
} & Pick<AbstractChat<UI_MESSAGE>, 'sendMessage' | 'regenerate' | 'stop' | 'resumeStream' | 'addToolResult' | 'addToolOutput' | 'addToolApprovalResponse' | 'status' | 'messages' | 'clearError'>;
|
|
26
|
+
type UseChatOptions<UI_MESSAGE extends UIMessage> = ({
|
|
27
|
+
chat: Chat<UI_MESSAGE>;
|
|
28
|
+
} | ChatInit<UI_MESSAGE>) & {
|
|
29
|
+
/**
|
|
30
|
+
* Custom throttle wait in ms for the chat messages and data updates.
|
|
31
|
+
* Default is undefined, which disables throttling.
|
|
32
|
+
*/
|
|
33
|
+
experimental_throttle?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Whether to resume an ongoing chat generation stream.
|
|
36
|
+
*/
|
|
37
|
+
resume?: boolean;
|
|
38
|
+
};
|
|
39
|
+
declare function useChat<UI_MESSAGE extends UIMessage = UIMessage>({ experimental_throttle: throttleWaitMs, resume, ...options }?: UseChatOptions<UI_MESSAGE>): UseChatHelpers<UI_MESSAGE>;
|
|
40
|
+
|
|
41
|
+
type UseCompletionHelpers = {
|
|
42
|
+
/** The current completion result */
|
|
43
|
+
completion: string;
|
|
44
|
+
/**
|
|
45
|
+
* Send a new prompt to the API endpoint and update the completion state.
|
|
46
|
+
*/
|
|
47
|
+
complete: (prompt: string, options?: CompletionRequestOptions) => Promise<string | null | undefined>;
|
|
48
|
+
/** The error object of the API request */
|
|
49
|
+
error: undefined | Error;
|
|
50
|
+
/**
|
|
51
|
+
* Abort the current API request but keep the generated tokens.
|
|
52
|
+
*/
|
|
53
|
+
stop: () => void;
|
|
54
|
+
/**
|
|
55
|
+
* Update the `completion` state locally.
|
|
56
|
+
*/
|
|
57
|
+
setCompletion: (completion: string) => void;
|
|
58
|
+
/** The current value of the input */
|
|
59
|
+
input: string;
|
|
60
|
+
/** setState-powered method to update the input value */
|
|
61
|
+
setInput: React.Dispatch<React.SetStateAction<string>>;
|
|
62
|
+
/**
|
|
63
|
+
* An input/textarea-ready onChange handler to control the value of the input
|
|
64
|
+
* @example
|
|
65
|
+
* ```jsx
|
|
66
|
+
* <input onChange={handleInputChange} value={input} />
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
handleInputChange: (event: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
|
|
70
|
+
/**
|
|
71
|
+
* Form submission handler to automatically reset input and append a user message
|
|
72
|
+
* @example
|
|
73
|
+
* ```jsx
|
|
74
|
+
* <form onSubmit={handleSubmit}>
|
|
75
|
+
* <input onChange={handleInputChange} value={input} />
|
|
76
|
+
* </form>
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
handleSubmit: (event?: {
|
|
80
|
+
preventDefault?: () => void;
|
|
81
|
+
}) => void;
|
|
82
|
+
/** Whether the API request is in progress */
|
|
83
|
+
isLoading: boolean;
|
|
84
|
+
};
|
|
85
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamProtocol, fetch, onFinish, onError, experimental_throttle: throttleWaitMs, }?: UseCompletionOptions & {
|
|
86
|
+
/**
|
|
87
|
+
* Custom throttle wait in ms for the completion and data updates.
|
|
88
|
+
* Default is undefined, which disables throttling.
|
|
89
|
+
*/
|
|
90
|
+
experimental_throttle?: number;
|
|
91
|
+
}): UseCompletionHelpers;
|
|
92
|
+
|
|
93
|
+
type Experimental_UseObjectOptions<SCHEMA extends FlexibleSchema, RESULT> = {
|
|
94
|
+
/**
|
|
95
|
+
* The API endpoint. It should stream JSON that matches the schema as chunked text.
|
|
96
|
+
*/
|
|
97
|
+
api: string;
|
|
98
|
+
/**
|
|
99
|
+
* A schema that defines the shape of the complete object.
|
|
100
|
+
*/
|
|
101
|
+
schema: SCHEMA;
|
|
102
|
+
/**
|
|
103
|
+
* An unique identifier. If not provided, a random one will be
|
|
104
|
+
* generated. When provided, the `useObject` hook with the same `id` will
|
|
105
|
+
* have shared states across components.
|
|
106
|
+
*/
|
|
107
|
+
id?: string;
|
|
108
|
+
/**
|
|
109
|
+
* An optional value for the initial object.
|
|
110
|
+
*/
|
|
111
|
+
initialValue?: DeepPartial<RESULT>;
|
|
112
|
+
/**
|
|
113
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
114
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
115
|
+
*/
|
|
116
|
+
fetch?: FetchFunction;
|
|
117
|
+
/**
|
|
118
|
+
* Callback that is called when the stream has finished.
|
|
119
|
+
*/
|
|
120
|
+
onFinish?: (event: {
|
|
121
|
+
/**
|
|
122
|
+
* The generated object (typed according to the schema).
|
|
123
|
+
* Can be undefined if the final object does not match the schema.
|
|
124
|
+
*/
|
|
125
|
+
object: RESULT | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.
|
|
128
|
+
*/
|
|
129
|
+
error: Error | undefined;
|
|
130
|
+
}) => Promise<void> | void;
|
|
131
|
+
/**
|
|
132
|
+
* Callback function to be called when an error is encountered.
|
|
133
|
+
*/
|
|
134
|
+
onError?: (error: Error) => void;
|
|
135
|
+
/**
|
|
136
|
+
* Additional HTTP headers to be included in the request.
|
|
137
|
+
* Can be a static object, a function that returns headers, or an async function
|
|
138
|
+
* for dynamic auth tokens.
|
|
139
|
+
*/
|
|
140
|
+
headers?: Resolvable<Record<string, string> | Headers>;
|
|
141
|
+
/**
|
|
142
|
+
* The credentials mode to be used for the fetch request.
|
|
143
|
+
* Possible values are: 'omit', 'same-origin', 'include'.
|
|
144
|
+
* Defaults to 'same-origin'.
|
|
145
|
+
*/
|
|
146
|
+
credentials?: RequestCredentials;
|
|
147
|
+
};
|
|
148
|
+
type Experimental_UseObjectHelpers<RESULT, INPUT> = {
|
|
149
|
+
/**
|
|
150
|
+
* Calls the API with the provided input as JSON body.
|
|
151
|
+
*/
|
|
152
|
+
submit: (input: INPUT) => void;
|
|
153
|
+
/**
|
|
154
|
+
* The current value for the generated object. Updated as the API streams JSON chunks.
|
|
155
|
+
*/
|
|
156
|
+
object: DeepPartial<RESULT> | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* The error object of the API request if any.
|
|
159
|
+
*/
|
|
160
|
+
error: Error | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* Flag that indicates whether an API request is in progress.
|
|
163
|
+
*/
|
|
164
|
+
isLoading: boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Abort the current request immediately, keep the current partial object if any.
|
|
167
|
+
*/
|
|
168
|
+
stop: () => void;
|
|
169
|
+
/**
|
|
170
|
+
* Clear the object state.
|
|
171
|
+
*/
|
|
172
|
+
clear: () => void;
|
|
173
|
+
};
|
|
174
|
+
declare function useObject<SCHEMA extends FlexibleSchema, RESULT = InferSchema<SCHEMA>, INPUT = any>({ api, id, schema, // required, in the future we will use it for validation
|
|
175
|
+
initialValue, fetch, onError, onFinish, headers, credentials, }: Experimental_UseObjectOptions<SCHEMA, RESULT>): Experimental_UseObjectHelpers<RESULT, INPUT>;
|
|
176
|
+
declare const experimental_useObject: typeof useObject;
|
|
177
|
+
|
|
178
|
+
export { Chat, Experimental_UseObjectHelpers, Experimental_UseObjectOptions, UseChatHelpers, UseChatOptions, UseCompletionHelpers, experimental_useObject, useChat, useCompletion };
|