forked from vercel/ai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuse-assistant.ts
265 lines (228 loc) · 6.73 KB
/
use-assistant.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { isAbortError } from '@ai-sdk/provider-utils';
import type {
AssistantStatus,
CreateMessage,
Message,
UseAssistantOptions,
} from '@ai-sdk/ui-utils';
import { generateId, readDataStream } from '@ai-sdk/ui-utils';
import { Readable, Writable, get, writable } from 'svelte/store';
// use function to allow for mocking in tests:
const getOriginalFetch = () => fetch;
let uniqueId = 0;
const store: Record<string, any> = {};
export type UseAssistantHelpers = {
/**
* The current array of chat messages.
*/
messages: Readable<Message[]>;
/**
* Update the message store with a new array of messages.
*/
setMessages: (messages: Message[]) => void;
/**
* The current thread ID.
*/
threadId: Readable<string | undefined>;
/**
* The current value of the input field.
*/
input: Writable<string>;
/**
* Append a user message to the chat list. This triggers the API call to fetch
* the assistant's response.
* @param message The message to append
* @param requestOptions Additional options to pass to the API call
*/
append: (
message: Message | CreateMessage,
requestOptions?: { data?: Record<string, string> },
) => Promise<void>;
/**
Abort the current request immediately, keep the generated tokens if any.
*/
stop: () => void;
/**
* Form submission handler that automatically resets the input field and appends a user message.
*/
submitMessage: (
event?: { preventDefault?: () => void },
requestOptions?: { data?: Record<string, string> },
) => Promise<void>;
/**
* The current status of the assistant. This can be used to show a loading indicator.
*/
status: Readable<AssistantStatus>;
/**
* The error thrown during the assistant message processing, if any.
*/
error: Readable<undefined | Error>;
};
/**
* @deprecated Use `useAssistant` from `@ai-sdk/svelte` instead.
*/
export function useAssistant({
api,
threadId: threadIdParam,
credentials,
headers,
body,
onError,
fetch,
}: UseAssistantOptions): UseAssistantHelpers {
// Generate a unique thread ID
const threadIdStore = writable<string | undefined>(threadIdParam);
// Initialize message, input, status, and error stores
const key = `${api}|${threadIdParam ?? `completion-${uniqueId++}`}`;
const messages = writable<Message[]>(store[key] || []);
const input = writable('');
const status = writable<AssistantStatus>('awaiting_message');
const error = writable<undefined | Error>(undefined);
// To manage aborting the current fetch request
let abortController: AbortController | null = null;
// Update the message store
const mutateMessages = (newMessages: Message[]) => {
store[key] = newMessages;
messages.set(newMessages);
};
// Function to handle API calls and state management
async function append(
message: Message | CreateMessage,
requestOptions?: { data?: Record<string, string> },
) {
status.set('in_progress');
abortController = new AbortController(); // Initialize a new AbortController
// Add the new message to the existing array
mutateMessages([
...get(messages),
{ ...message, id: message.id ?? generateId() },
]);
input.set('');
try {
const actualFetch = fetch ?? getOriginalFetch();
const response = await actualFetch(api, {
method: 'POST',
credentials,
signal: abortController.signal,
headers: { 'Content-Type': 'application/json', ...headers },
body: JSON.stringify({
...body,
// always use user-provided threadId when available:
threadId: threadIdParam ?? get(threadIdStore) ?? null,
message: message.content,
// optional request data:
data: requestOptions?.data,
}),
});
if (!response.ok) {
throw new Error(
(await response.text()) ?? 'Failed to fetch the assistant response.',
);
}
if (response.body == null) {
throw new Error('The response body is empty.');
}
// Read the streamed response data
for await (const { type, value } of readDataStream(
response.body.getReader(),
)) {
switch (type) {
case 'assistant_message': {
mutateMessages([
...get(messages),
{
id: value.id,
role: value.role,
content: value.content[0].text.value,
},
]);
break;
}
case 'text': {
// text delta - add to last message:
mutateMessages(
get(messages).map((msg, index, array) => {
if (index === array.length - 1) {
return { ...msg, content: msg.content + value };
}
return msg;
}),
);
break;
}
case 'data_message': {
mutateMessages([
...get(messages),
{
id: value.id ?? generateId(),
role: 'data',
content: '',
data: value.data,
},
]);
break;
}
case 'assistant_control_data': {
threadIdStore.set(value.threadId);
mutateMessages(
get(messages).map((msg, index, array) => {
if (index === array.length - 1) {
return { ...msg, id: value.messageId };
}
return msg;
}),
);
break;
}
case 'error': {
error.set(new Error(value));
break;
}
}
}
} catch (err) {
// Ignore abort errors as they are expected when the user cancels the request:
if (isAbortError(error) && abortController?.signal?.aborted) {
abortController = null;
return;
}
if (onError && err instanceof Error) {
onError(err);
}
error.set(err as Error);
} finally {
abortController = null;
status.set('awaiting_message');
}
}
function setMessages(messages: Message[]) {
mutateMessages(messages);
}
function stop() {
if (abortController) {
abortController.abort();
abortController = null;
}
}
// Function to handle form submission
async function submitMessage(
event?: { preventDefault?: () => void },
requestOptions?: { data?: Record<string, string> },
) {
event?.preventDefault?.();
const inputValue = get(input);
if (!inputValue) return;
await append({ role: 'user', content: inputValue }, requestOptions);
}
return {
messages,
error,
threadId: threadIdStore,
input,
append,
submitMessage,
status,
setMessages,
stop,
};
}