@ai-sdk/svelte 1.1.24 → 2.0.1
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 +13 -0
- package/README.md +5 -3
- package/dist/chat-context.svelte.d.ts +14 -0
- package/dist/chat-context.svelte.d.ts.map +1 -0
- package/dist/chat-context.svelte.js +13 -0
- package/dist/chat.svelte.d.ts +75 -0
- package/dist/chat.svelte.d.ts.map +1 -0
- package/dist/chat.svelte.js +244 -0
- package/dist/completion-context.svelte.d.ts +15 -0
- package/dist/completion-context.svelte.d.ts.map +1 -0
- package/dist/completion-context.svelte.js +14 -0
- package/dist/completion.svelte.d.ts +37 -0
- package/dist/completion.svelte.d.ts.map +1 -0
- package/dist/completion.svelte.js +111 -0
- package/dist/context-provider.d.ts +2 -0
- package/dist/context-provider.d.ts.map +1 -0
- package/dist/context-provider.js +11 -0
- package/dist/index.d.ts +5 -166
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -551
- package/dist/structured-object-context.svelte.d.ts +12 -0
- package/dist/structured-object-context.svelte.d.ts.map +1 -0
- package/dist/structured-object-context.svelte.js +12 -0
- package/dist/structured-object.svelte.d.ts +73 -0
- package/dist/structured-object.svelte.d.ts.map +1 -0
- package/dist/structured-object.svelte.js +123 -0
- package/dist/tests/chat-synchronization.svelte +12 -0
- package/dist/tests/chat-synchronization.svelte.d.ts +11 -0
- package/dist/tests/chat-synchronization.svelte.d.ts.map +1 -0
- package/dist/tests/completion-synchronization.svelte +12 -0
- package/dist/tests/completion-synchronization.svelte.d.ts +11 -0
- package/dist/tests/completion-synchronization.svelte.d.ts.map +1 -0
- package/dist/tests/structured-object-synchronization.svelte +22 -0
- package/dist/tests/structured-object-synchronization.svelte.d.ts +28 -0
- package/dist/tests/structured-object-synchronization.svelte.d.ts.map +1 -0
- package/dist/utils.svelte.d.ts +17 -0
- package/dist/utils.svelte.d.ts.map +1 -0
- package/dist/utils.svelte.js +50 -0
- package/package.json +56 -35
- package/src/chat-context.svelte.ts +23 -0
- package/src/chat.svelte.ts +341 -0
- package/src/completion-context.svelte.ts +24 -0
- package/src/completion.svelte.ts +133 -0
- package/src/context-provider.ts +20 -0
- package/src/index.ts +16 -0
- package/src/structured-object-context.svelte.ts +27 -0
- package/src/structured-object.svelte.ts +222 -0
- package/src/tests/chat-synchronization.svelte +12 -0
- package/src/tests/completion-synchronization.svelte +12 -0
- package/src/tests/structured-object-synchronization.svelte +22 -0
- package/src/utils.svelte.ts +66 -0
- package/dist/index.d.mts +0 -166
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -532
- package/dist/index.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,551 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
useAssistant: () => useAssistant,
|
|
24
|
-
useChat: () => useChat,
|
|
25
|
-
useCompletion: () => useCompletion
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(src_exports);
|
|
28
|
-
|
|
29
|
-
// src/use-chat.ts
|
|
30
|
-
var import_ui_utils = require("@ai-sdk/ui-utils");
|
|
31
|
-
var import_store = require("svelte/store");
|
|
32
|
-
var store = (0, import_store.writable)({});
|
|
33
|
-
function useChat({
|
|
34
|
-
api = "/api/chat",
|
|
35
|
-
id,
|
|
36
|
-
initialMessages = [],
|
|
37
|
-
initialInput = "",
|
|
38
|
-
sendExtraMessageFields,
|
|
39
|
-
streamProtocol = "data",
|
|
40
|
-
onResponse,
|
|
41
|
-
onFinish,
|
|
42
|
-
onError,
|
|
43
|
-
onToolCall,
|
|
44
|
-
credentials,
|
|
45
|
-
headers,
|
|
46
|
-
body,
|
|
47
|
-
generateId: generateId2 = import_ui_utils.generateId,
|
|
48
|
-
fetch: fetch2,
|
|
49
|
-
keepLastMessageOnError = true,
|
|
50
|
-
maxSteps = 1
|
|
51
|
-
} = {}) {
|
|
52
|
-
const chatId = id != null ? id : generateId2();
|
|
53
|
-
const key = `${api}|${chatId}`;
|
|
54
|
-
const messages = (0, import_store.derived)(
|
|
55
|
-
[store],
|
|
56
|
-
([$store]) => {
|
|
57
|
-
var _a;
|
|
58
|
-
return (_a = $store[key]) != null ? _a : (0, import_ui_utils.fillMessageParts)(initialMessages);
|
|
59
|
-
}
|
|
60
|
-
);
|
|
61
|
-
const streamData = (0, import_store.writable)(void 0);
|
|
62
|
-
const status = (0, import_store.writable)(
|
|
63
|
-
"ready"
|
|
64
|
-
);
|
|
65
|
-
const mutate = (data) => {
|
|
66
|
-
store.update((value) => {
|
|
67
|
-
value[key] = data;
|
|
68
|
-
return value;
|
|
69
|
-
});
|
|
70
|
-
};
|
|
71
|
-
let abortController = null;
|
|
72
|
-
const extraMetadata = {
|
|
73
|
-
credentials,
|
|
74
|
-
headers,
|
|
75
|
-
body
|
|
76
|
-
};
|
|
77
|
-
const error = (0, import_store.writable)(void 0);
|
|
78
|
-
async function triggerRequest(chatRequest) {
|
|
79
|
-
var _a;
|
|
80
|
-
status.set("submitted");
|
|
81
|
-
error.set(void 0);
|
|
82
|
-
const messagesSnapshot = (0, import_store.get)(messages);
|
|
83
|
-
const messageCount = messagesSnapshot.length;
|
|
84
|
-
const maxStep = (0, import_ui_utils.extractMaxToolInvocationStep)(
|
|
85
|
-
(_a = chatRequest.messages[chatRequest.messages.length - 1]) == null ? void 0 : _a.toolInvocations
|
|
86
|
-
);
|
|
87
|
-
try {
|
|
88
|
-
abortController = new AbortController();
|
|
89
|
-
const chatMessages = (0, import_ui_utils.fillMessageParts)(chatRequest.messages);
|
|
90
|
-
mutate(chatMessages);
|
|
91
|
-
const existingData = (0, import_store.get)(streamData);
|
|
92
|
-
const previousMessages = (0, import_store.get)(messages);
|
|
93
|
-
const constructedMessagesPayload = sendExtraMessageFields ? chatMessages : chatMessages.map(
|
|
94
|
-
({
|
|
95
|
-
role,
|
|
96
|
-
content,
|
|
97
|
-
experimental_attachments,
|
|
98
|
-
data,
|
|
99
|
-
annotations,
|
|
100
|
-
toolInvocations,
|
|
101
|
-
parts
|
|
102
|
-
}) => ({
|
|
103
|
-
role,
|
|
104
|
-
content,
|
|
105
|
-
...experimental_attachments !== void 0 && {
|
|
106
|
-
experimental_attachments
|
|
107
|
-
},
|
|
108
|
-
...data !== void 0 && { data },
|
|
109
|
-
...annotations !== void 0 && { annotations },
|
|
110
|
-
...toolInvocations !== void 0 && { toolInvocations },
|
|
111
|
-
...parts !== void 0 && { parts }
|
|
112
|
-
})
|
|
113
|
-
);
|
|
114
|
-
await (0, import_ui_utils.callChatApi)({
|
|
115
|
-
api,
|
|
116
|
-
body: {
|
|
117
|
-
id: chatId,
|
|
118
|
-
messages: constructedMessagesPayload,
|
|
119
|
-
data: chatRequest.data,
|
|
120
|
-
...extraMetadata.body,
|
|
121
|
-
...chatRequest.body
|
|
122
|
-
},
|
|
123
|
-
streamProtocol,
|
|
124
|
-
credentials: extraMetadata.credentials,
|
|
125
|
-
headers: {
|
|
126
|
-
...extraMetadata.headers,
|
|
127
|
-
...chatRequest.headers
|
|
128
|
-
},
|
|
129
|
-
abortController: () => abortController,
|
|
130
|
-
restoreMessagesOnFailure() {
|
|
131
|
-
if (!keepLastMessageOnError) {
|
|
132
|
-
mutate(previousMessages);
|
|
133
|
-
}
|
|
134
|
-
},
|
|
135
|
-
onResponse,
|
|
136
|
-
onUpdate({ message, data, replaceLastMessage }) {
|
|
137
|
-
status.set("streaming");
|
|
138
|
-
mutate([
|
|
139
|
-
...replaceLastMessage ? chatMessages.slice(0, chatMessages.length - 1) : chatMessages,
|
|
140
|
-
message
|
|
141
|
-
]);
|
|
142
|
-
if (data == null ? void 0 : data.length) {
|
|
143
|
-
streamData.set([...existingData != null ? existingData : [], ...data]);
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
onFinish,
|
|
147
|
-
generateId: generateId2,
|
|
148
|
-
onToolCall,
|
|
149
|
-
fetch: fetch2,
|
|
150
|
-
lastMessage: chatMessages[chatMessages.length - 1]
|
|
151
|
-
});
|
|
152
|
-
status.set("ready");
|
|
153
|
-
} catch (err) {
|
|
154
|
-
if (err.name === "AbortError") {
|
|
155
|
-
abortController = null;
|
|
156
|
-
status.set("ready");
|
|
157
|
-
return null;
|
|
158
|
-
}
|
|
159
|
-
if (onError && err instanceof Error) {
|
|
160
|
-
onError(err);
|
|
161
|
-
}
|
|
162
|
-
error.set(err);
|
|
163
|
-
status.set("error");
|
|
164
|
-
} finally {
|
|
165
|
-
abortController = null;
|
|
166
|
-
}
|
|
167
|
-
const newMessagesSnapshot = (0, import_store.get)(messages);
|
|
168
|
-
if ((0, import_ui_utils.shouldResubmitMessages)({
|
|
169
|
-
originalMaxToolInvocationStep: maxStep,
|
|
170
|
-
originalMessageCount: messageCount,
|
|
171
|
-
maxSteps,
|
|
172
|
-
messages: newMessagesSnapshot
|
|
173
|
-
})) {
|
|
174
|
-
await triggerRequest({ messages: newMessagesSnapshot });
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
const append = async (message, { data, headers: headers2, body: body2, experimental_attachments } = {}) => {
|
|
178
|
-
var _a, _b;
|
|
179
|
-
const attachmentsForRequest = await (0, import_ui_utils.prepareAttachmentsForRequest)(
|
|
180
|
-
experimental_attachments
|
|
181
|
-
);
|
|
182
|
-
return triggerRequest({
|
|
183
|
-
messages: (0, import_store.get)(messages).concat({
|
|
184
|
-
...message,
|
|
185
|
-
id: (_a = message.id) != null ? _a : generateId2(),
|
|
186
|
-
createdAt: (_b = message.createdAt) != null ? _b : /* @__PURE__ */ new Date(),
|
|
187
|
-
experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : void 0,
|
|
188
|
-
parts: (0, import_ui_utils.getMessageParts)(message)
|
|
189
|
-
}),
|
|
190
|
-
headers: headers2,
|
|
191
|
-
body: body2,
|
|
192
|
-
data
|
|
193
|
-
});
|
|
194
|
-
};
|
|
195
|
-
const reload = async ({
|
|
196
|
-
data,
|
|
197
|
-
headers: headers2,
|
|
198
|
-
body: body2
|
|
199
|
-
} = {}) => {
|
|
200
|
-
const messagesSnapshot = (0, import_store.get)(messages);
|
|
201
|
-
if (messagesSnapshot.length === 0) {
|
|
202
|
-
return null;
|
|
203
|
-
}
|
|
204
|
-
const lastMessage = messagesSnapshot.at(-1);
|
|
205
|
-
return triggerRequest({
|
|
206
|
-
messages: (lastMessage == null ? void 0 : lastMessage.role) === "assistant" ? messagesSnapshot.slice(0, -1) : messagesSnapshot,
|
|
207
|
-
headers: headers2,
|
|
208
|
-
body: body2,
|
|
209
|
-
data
|
|
210
|
-
});
|
|
211
|
-
};
|
|
212
|
-
const stop = () => {
|
|
213
|
-
if (abortController) {
|
|
214
|
-
abortController.abort();
|
|
215
|
-
abortController = null;
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
const setMessages = (messagesArg) => {
|
|
219
|
-
if (typeof messagesArg === "function") {
|
|
220
|
-
messagesArg = messagesArg((0, import_store.get)(messages));
|
|
221
|
-
}
|
|
222
|
-
mutate((0, import_ui_utils.fillMessageParts)(messagesArg));
|
|
223
|
-
};
|
|
224
|
-
const setData = (dataArg) => {
|
|
225
|
-
if (typeof dataArg === "function") {
|
|
226
|
-
dataArg = dataArg((0, import_store.get)(streamData));
|
|
227
|
-
}
|
|
228
|
-
streamData.set(dataArg);
|
|
229
|
-
};
|
|
230
|
-
const input = (0, import_store.writable)(initialInput);
|
|
231
|
-
const handleSubmit = async (event, options = {}) => {
|
|
232
|
-
var _a;
|
|
233
|
-
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
234
|
-
const inputValue = (0, import_store.get)(input);
|
|
235
|
-
if (!inputValue && !options.allowEmptySubmit)
|
|
236
|
-
return;
|
|
237
|
-
const attachmentsForRequest = await (0, import_ui_utils.prepareAttachmentsForRequest)(
|
|
238
|
-
options.experimental_attachments
|
|
239
|
-
);
|
|
240
|
-
triggerRequest({
|
|
241
|
-
messages: (0, import_store.get)(messages).concat({
|
|
242
|
-
id: generateId2(),
|
|
243
|
-
content: inputValue,
|
|
244
|
-
role: "user",
|
|
245
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
246
|
-
experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : void 0,
|
|
247
|
-
parts: [{ type: "text", text: inputValue }]
|
|
248
|
-
}),
|
|
249
|
-
body: options.body,
|
|
250
|
-
headers: options.headers,
|
|
251
|
-
data: options.data
|
|
252
|
-
});
|
|
253
|
-
input.set("");
|
|
254
|
-
};
|
|
255
|
-
const addToolResult = ({
|
|
256
|
-
toolCallId,
|
|
257
|
-
result
|
|
258
|
-
}) => {
|
|
259
|
-
var _a;
|
|
260
|
-
const messagesSnapshot = (_a = (0, import_store.get)(messages)) != null ? _a : [];
|
|
261
|
-
(0, import_ui_utils.updateToolCallResult)({
|
|
262
|
-
messages: messagesSnapshot,
|
|
263
|
-
toolCallId,
|
|
264
|
-
toolResult: result
|
|
265
|
-
});
|
|
266
|
-
mutate(messagesSnapshot);
|
|
267
|
-
const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
|
|
268
|
-
if ((0, import_ui_utils.isAssistantMessageWithCompletedToolCalls)(lastMessage)) {
|
|
269
|
-
triggerRequest({ messages: messagesSnapshot });
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
return {
|
|
273
|
-
id: chatId,
|
|
274
|
-
messages,
|
|
275
|
-
error,
|
|
276
|
-
append,
|
|
277
|
-
reload,
|
|
278
|
-
stop,
|
|
279
|
-
setMessages,
|
|
280
|
-
input,
|
|
281
|
-
handleSubmit,
|
|
282
|
-
isLoading: (0, import_store.derived)(
|
|
283
|
-
status,
|
|
284
|
-
($status) => $status === "submitted" || $status === "streaming"
|
|
285
|
-
),
|
|
286
|
-
status,
|
|
287
|
-
data: streamData,
|
|
288
|
-
setData,
|
|
289
|
-
addToolResult
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// src/use-completion.ts
|
|
294
|
-
var import_ui_utils2 = require("@ai-sdk/ui-utils");
|
|
295
|
-
var import_store2 = require("svelte/store");
|
|
296
|
-
var uniqueId = 0;
|
|
297
|
-
var store2 = (0, import_store2.writable)({});
|
|
298
|
-
function useCompletion({
|
|
299
|
-
api = "/api/completion",
|
|
300
|
-
id,
|
|
301
|
-
initialCompletion = "",
|
|
302
|
-
initialInput = "",
|
|
303
|
-
credentials,
|
|
304
|
-
headers,
|
|
305
|
-
body,
|
|
306
|
-
streamProtocol = "data",
|
|
307
|
-
onResponse,
|
|
308
|
-
onFinish,
|
|
309
|
-
onError,
|
|
310
|
-
fetch: fetch2
|
|
311
|
-
} = {}) {
|
|
312
|
-
const completionId = id || `completion-${uniqueId++}`;
|
|
313
|
-
const key = `${api}|${completionId}`;
|
|
314
|
-
const data = (0, import_store2.derived)([store2], ([$store]) => {
|
|
315
|
-
var _a;
|
|
316
|
-
return (_a = $store[key]) != null ? _a : initialCompletion;
|
|
317
|
-
});
|
|
318
|
-
const streamData = (0, import_store2.writable)(void 0);
|
|
319
|
-
const loading = (0, import_store2.writable)(false);
|
|
320
|
-
const mutate = (data2) => {
|
|
321
|
-
store2.update((value) => {
|
|
322
|
-
value[key] = data2;
|
|
323
|
-
return value;
|
|
324
|
-
});
|
|
325
|
-
};
|
|
326
|
-
const completion = data;
|
|
327
|
-
const error = (0, import_store2.writable)(void 0);
|
|
328
|
-
let abortController = null;
|
|
329
|
-
const complete = async (prompt, options) => {
|
|
330
|
-
const existingData = (0, import_store2.get)(streamData);
|
|
331
|
-
return (0, import_ui_utils2.callCompletionApi)({
|
|
332
|
-
api,
|
|
333
|
-
prompt,
|
|
334
|
-
credentials,
|
|
335
|
-
headers: {
|
|
336
|
-
...headers,
|
|
337
|
-
...options == null ? void 0 : options.headers
|
|
338
|
-
},
|
|
339
|
-
body: {
|
|
340
|
-
...body,
|
|
341
|
-
...options == null ? void 0 : options.body
|
|
342
|
-
},
|
|
343
|
-
streamProtocol,
|
|
344
|
-
setCompletion: mutate,
|
|
345
|
-
setLoading: (loadingState) => loading.set(loadingState),
|
|
346
|
-
setError: (err) => error.set(err),
|
|
347
|
-
setAbortController: (controller) => {
|
|
348
|
-
abortController = controller;
|
|
349
|
-
},
|
|
350
|
-
onResponse,
|
|
351
|
-
onFinish,
|
|
352
|
-
onError,
|
|
353
|
-
onData(data2) {
|
|
354
|
-
streamData.set([...existingData || [], ...data2 || []]);
|
|
355
|
-
},
|
|
356
|
-
fetch: fetch2
|
|
357
|
-
});
|
|
358
|
-
};
|
|
359
|
-
const stop = () => {
|
|
360
|
-
if (abortController) {
|
|
361
|
-
abortController.abort();
|
|
362
|
-
abortController = null;
|
|
363
|
-
}
|
|
364
|
-
};
|
|
365
|
-
const setCompletion = (completion2) => {
|
|
366
|
-
mutate(completion2);
|
|
367
|
-
};
|
|
368
|
-
const input = (0, import_store2.writable)(initialInput);
|
|
369
|
-
const handleSubmit = (event) => {
|
|
370
|
-
var _a;
|
|
371
|
-
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
372
|
-
const inputValue = (0, import_store2.get)(input);
|
|
373
|
-
return inputValue ? complete(inputValue) : void 0;
|
|
374
|
-
};
|
|
375
|
-
return {
|
|
376
|
-
completion,
|
|
377
|
-
complete,
|
|
378
|
-
error,
|
|
379
|
-
stop,
|
|
380
|
-
setCompletion,
|
|
381
|
-
input,
|
|
382
|
-
handleSubmit,
|
|
383
|
-
isLoading: loading,
|
|
384
|
-
data: streamData
|
|
385
|
-
};
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// src/use-assistant.ts
|
|
389
|
-
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
390
|
-
var import_ui_utils3 = require("@ai-sdk/ui-utils");
|
|
391
|
-
var import_store3 = require("svelte/store");
|
|
392
|
-
var getOriginalFetch = () => fetch;
|
|
393
|
-
var uniqueId2 = 0;
|
|
394
|
-
var store3 = {};
|
|
395
|
-
function useAssistant({
|
|
396
|
-
api,
|
|
397
|
-
threadId: threadIdParam,
|
|
398
|
-
credentials,
|
|
399
|
-
headers,
|
|
400
|
-
body,
|
|
401
|
-
onError,
|
|
402
|
-
fetch: fetch2
|
|
403
|
-
}) {
|
|
404
|
-
const threadIdStore = (0, import_store3.writable)(threadIdParam);
|
|
405
|
-
const key = `${api}|${threadIdParam != null ? threadIdParam : `completion-${uniqueId2++}`}`;
|
|
406
|
-
const messages = (0, import_store3.writable)(store3[key] || []);
|
|
407
|
-
const input = (0, import_store3.writable)("");
|
|
408
|
-
const status = (0, import_store3.writable)("awaiting_message");
|
|
409
|
-
const error = (0, import_store3.writable)(void 0);
|
|
410
|
-
let abortController = null;
|
|
411
|
-
const mutateMessages = (newMessages) => {
|
|
412
|
-
store3[key] = newMessages;
|
|
413
|
-
messages.set(newMessages);
|
|
414
|
-
};
|
|
415
|
-
async function append(message, requestOptions) {
|
|
416
|
-
var _a, _b, _c, _d;
|
|
417
|
-
status.set("in_progress");
|
|
418
|
-
abortController = new AbortController();
|
|
419
|
-
mutateMessages([
|
|
420
|
-
...(0, import_store3.get)(messages),
|
|
421
|
-
{ ...message, id: (_a = message.id) != null ? _a : (0, import_ui_utils3.generateId)() }
|
|
422
|
-
]);
|
|
423
|
-
input.set("");
|
|
424
|
-
try {
|
|
425
|
-
const actualFetch = fetch2 != null ? fetch2 : getOriginalFetch();
|
|
426
|
-
const response = await actualFetch(api, {
|
|
427
|
-
method: "POST",
|
|
428
|
-
credentials,
|
|
429
|
-
signal: abortController.signal,
|
|
430
|
-
headers: { "Content-Type": "application/json", ...headers },
|
|
431
|
-
body: JSON.stringify({
|
|
432
|
-
...body,
|
|
433
|
-
// always use user-provided threadId when available:
|
|
434
|
-
threadId: (_b = threadIdParam != null ? threadIdParam : (0, import_store3.get)(threadIdStore)) != null ? _b : null,
|
|
435
|
-
message: message.content,
|
|
436
|
-
// optional request data:
|
|
437
|
-
data: requestOptions == null ? void 0 : requestOptions.data
|
|
438
|
-
})
|
|
439
|
-
});
|
|
440
|
-
if (!response.ok) {
|
|
441
|
-
throw new Error(
|
|
442
|
-
(_c = await response.text()) != null ? _c : "Failed to fetch the assistant response."
|
|
443
|
-
);
|
|
444
|
-
}
|
|
445
|
-
if (response.body == null) {
|
|
446
|
-
throw new Error("The response body is empty.");
|
|
447
|
-
}
|
|
448
|
-
await (0, import_ui_utils3.processAssistantStream)({
|
|
449
|
-
stream: response.body,
|
|
450
|
-
onAssistantMessagePart(value) {
|
|
451
|
-
mutateMessages([
|
|
452
|
-
...(0, import_store3.get)(messages),
|
|
453
|
-
{
|
|
454
|
-
id: value.id,
|
|
455
|
-
role: value.role,
|
|
456
|
-
content: value.content[0].text.value,
|
|
457
|
-
parts: []
|
|
458
|
-
}
|
|
459
|
-
]);
|
|
460
|
-
},
|
|
461
|
-
onTextPart(value) {
|
|
462
|
-
mutateMessages(
|
|
463
|
-
(0, import_store3.get)(messages).map((msg, index, array) => {
|
|
464
|
-
if (index === array.length - 1) {
|
|
465
|
-
return { ...msg, content: msg.content + value };
|
|
466
|
-
}
|
|
467
|
-
return msg;
|
|
468
|
-
})
|
|
469
|
-
);
|
|
470
|
-
},
|
|
471
|
-
onAssistantControlDataPart(value) {
|
|
472
|
-
threadIdStore.set(value.threadId);
|
|
473
|
-
mutateMessages(
|
|
474
|
-
(0, import_store3.get)(messages).map((msg, index, array) => {
|
|
475
|
-
if (index === array.length - 1) {
|
|
476
|
-
return { ...msg, id: value.messageId };
|
|
477
|
-
}
|
|
478
|
-
return msg;
|
|
479
|
-
})
|
|
480
|
-
);
|
|
481
|
-
},
|
|
482
|
-
onDataMessagePart(value) {
|
|
483
|
-
var _a2;
|
|
484
|
-
mutateMessages([
|
|
485
|
-
...(0, import_store3.get)(messages),
|
|
486
|
-
{
|
|
487
|
-
id: (_a2 = value.id) != null ? _a2 : (0, import_ui_utils3.generateId)(),
|
|
488
|
-
role: "data",
|
|
489
|
-
content: "",
|
|
490
|
-
data: value.data,
|
|
491
|
-
parts: []
|
|
492
|
-
}
|
|
493
|
-
]);
|
|
494
|
-
},
|
|
495
|
-
onErrorPart(value) {
|
|
496
|
-
error.set(new Error(value));
|
|
497
|
-
}
|
|
498
|
-
});
|
|
499
|
-
} catch (err) {
|
|
500
|
-
if ((0, import_provider_utils.isAbortError)(error) && ((_d = abortController == null ? void 0 : abortController.signal) == null ? void 0 : _d.aborted)) {
|
|
501
|
-
abortController = null;
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
if (onError && err instanceof Error) {
|
|
505
|
-
onError(err);
|
|
506
|
-
}
|
|
507
|
-
error.set(err);
|
|
508
|
-
} finally {
|
|
509
|
-
abortController = null;
|
|
510
|
-
status.set("awaiting_message");
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
function setMessages(messages2) {
|
|
514
|
-
mutateMessages(messages2);
|
|
515
|
-
}
|
|
516
|
-
function stop() {
|
|
517
|
-
if (abortController) {
|
|
518
|
-
abortController.abort();
|
|
519
|
-
abortController = null;
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
async function submitMessage(event, requestOptions) {
|
|
523
|
-
var _a;
|
|
524
|
-
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
525
|
-
const inputValue = (0, import_store3.get)(input);
|
|
526
|
-
if (!inputValue)
|
|
527
|
-
return;
|
|
528
|
-
await append(
|
|
529
|
-
{ role: "user", content: inputValue, parts: [] },
|
|
530
|
-
requestOptions
|
|
531
|
-
);
|
|
532
|
-
}
|
|
533
|
-
return {
|
|
534
|
-
messages,
|
|
535
|
-
error,
|
|
536
|
-
threadId: threadIdStore,
|
|
537
|
-
input,
|
|
538
|
-
append,
|
|
539
|
-
submitMessage,
|
|
540
|
-
status,
|
|
541
|
-
setMessages,
|
|
542
|
-
stop
|
|
543
|
-
};
|
|
544
|
-
}
|
|
545
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
546
|
-
0 && (module.exports = {
|
|
547
|
-
useAssistant,
|
|
548
|
-
useChat,
|
|
549
|
-
useCompletion
|
|
550
|
-
});
|
|
551
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export { Chat, } from './chat.svelte.js';
|
|
2
|
+
export { StructuredObject as Experimental_StructuredObject, } from './structured-object.svelte.js';
|
|
3
|
+
export { Completion } from './completion.svelte.js';
|
|
4
|
+
export { createAIContext } from './context-provider.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DeepPartial } from '@ai-sdk/ui-utils';
|
|
2
|
+
import { KeyedStore } from './utils.svelte.js';
|
|
3
|
+
export declare class StructuredObjectStore<RESULT> {
|
|
4
|
+
object: DeepPartial<RESULT> | undefined;
|
|
5
|
+
loading: boolean;
|
|
6
|
+
error: Error | undefined;
|
|
7
|
+
}
|
|
8
|
+
export declare class KeyedStructuredObjectStore extends KeyedStore<StructuredObjectStore<unknown>> {
|
|
9
|
+
constructor(value?: Iterable<readonly [string, StructuredObjectStore<unknown>]> | null | undefined);
|
|
10
|
+
}
|
|
11
|
+
export declare const hasStructuredObjectContext: () => boolean, getStructuredObjectContext: () => KeyedStructuredObjectStore, setStructuredObjectContext: (value: KeyedStructuredObjectStore) => KeyedStructuredObjectStore;
|
|
12
|
+
//# sourceMappingURL=structured-object-context.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structured-object-context.svelte.d.ts","sourceRoot":"","sources":["../src/structured-object-context.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAiB,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE9D,qBAAa,qBAAqB,CAAC,MAAM;IACvC,MAAM,kCAAiC;IACvC,OAAO,UAAiB;IACxB,KAAK,oBAAmB;CACzB;AAED,qBAAa,0BAA2B,SAAQ,UAAU,CACxD,qBAAqB,CAAC,OAAO,CAAC,CAC/B;gBAEG,KAAK,CAAC,EACF,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC,GAC3D,IAAI,GACJ,SAAS;CAIhB;AAED,eAAO,MACO,0BAA0B,iBAC1B,0BAA0B,oCAC1B,0BAA0B,mEACyB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext, KeyedStore } from './utils.svelte.js';
|
|
2
|
+
export class StructuredObjectStore {
|
|
3
|
+
object = $state();
|
|
4
|
+
loading = $state(false);
|
|
5
|
+
error = $state();
|
|
6
|
+
}
|
|
7
|
+
export class KeyedStructuredObjectStore extends KeyedStore {
|
|
8
|
+
constructor(value) {
|
|
9
|
+
super(StructuredObjectStore, value);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export const { hasContext: hasStructuredObjectContext, getContext: getStructuredObjectContext, setContext: setStructuredObjectContext, } = createContext('StructuredObject');
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type FetchFunction } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { type DeepPartial, type Schema } from '@ai-sdk/ui-utils';
|
|
3
|
+
import { type z } from 'zod';
|
|
4
|
+
export type Experimental_StructuredObjectOptions<RESULT> = {
|
|
5
|
+
/**
|
|
6
|
+
* The API endpoint. It should stream JSON that matches the schema as chunked text.
|
|
7
|
+
*/
|
|
8
|
+
api: string;
|
|
9
|
+
/**
|
|
10
|
+
* A Zod schema that defines the shape of the complete object.
|
|
11
|
+
*/
|
|
12
|
+
schema: z.Schema<RESULT, z.ZodTypeDef, unknown> | Schema<RESULT>;
|
|
13
|
+
/**
|
|
14
|
+
* An unique identifier. If not provided, a random one will be
|
|
15
|
+
* generated. When provided, the `useObject` hook with the same `id` will
|
|
16
|
+
* have shared states across components.
|
|
17
|
+
*/
|
|
18
|
+
id?: string;
|
|
19
|
+
/**
|
|
20
|
+
* An optional value for the initial object.
|
|
21
|
+
*/
|
|
22
|
+
initialValue?: DeepPartial<RESULT>;
|
|
23
|
+
/**
|
|
24
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
25
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
26
|
+
*/
|
|
27
|
+
fetch?: FetchFunction;
|
|
28
|
+
/**
|
|
29
|
+
* Callback that is called when the stream has finished.
|
|
30
|
+
*/
|
|
31
|
+
onFinish?: (event: {
|
|
32
|
+
/**
|
|
33
|
+
* The generated object (typed according to the schema).
|
|
34
|
+
* Can be undefined if the final object does not match the schema.
|
|
35
|
+
*/
|
|
36
|
+
object: RESULT | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.
|
|
39
|
+
*/
|
|
40
|
+
error: Error | undefined;
|
|
41
|
+
}) => Promise<void> | void;
|
|
42
|
+
/**
|
|
43
|
+
* Callback function to be called when an error is encountered.
|
|
44
|
+
*/
|
|
45
|
+
onError?: (error: Error) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Additional HTTP headers to be included in the request.
|
|
48
|
+
*/
|
|
49
|
+
headers?: Record<string, string> | Headers;
|
|
50
|
+
};
|
|
51
|
+
export declare class StructuredObject<RESULT, INPUT = unknown> {
|
|
52
|
+
#private;
|
|
53
|
+
/**
|
|
54
|
+
* The current value for the generated object. Updated as the API streams JSON chunks.
|
|
55
|
+
*/
|
|
56
|
+
get object(): DeepPartial<RESULT> | undefined;
|
|
57
|
+
/** The error object of the API request */
|
|
58
|
+
get error(): Error | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Flag that indicates whether an API request is in progress.
|
|
61
|
+
*/
|
|
62
|
+
get loading(): boolean;
|
|
63
|
+
constructor(options: Experimental_StructuredObjectOptions<RESULT>);
|
|
64
|
+
/**
|
|
65
|
+
* Abort the current request immediately, keep the current partial object if any.
|
|
66
|
+
*/
|
|
67
|
+
stop: () => void;
|
|
68
|
+
/**
|
|
69
|
+
* Calls the API with the provided input as JSON body.
|
|
70
|
+
*/
|
|
71
|
+
submit: (input: INPUT) => Promise<void>;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=structured-object.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structured-object.svelte.d.ts","sourceRoot":"","sources":["../src/structured-object.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,aAAa,EACnB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAIL,KAAK,WAAW,EAChB,KAAK,MAAM,EACZ,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,CAAC;AAQ7B,MAAM,MAAM,oCAAoC,CAAC,MAAM,IAAI;IACzD;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IAEjE;;;;OAIG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,YAAY,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAEnC;;;OAGG;IACH,KAAK,CAAC,EAAE,aAAa,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE;QACjB;;;WAGG;QACH,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;QAE3B;;WAEG;QACH,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;KAC1B,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAE3B;;OAEG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAEjC;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC;CAC5C,CAAC;AAEF,qBAAa,gBAAgB,CAAC,MAAM,EAAE,KAAK,GAAG,OAAO;;IAUnD;;OAEG;IACH,IAAI,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC,GAAG,SAAS,CAE5C;IAKD,0CAA0C;IAC1C,IAAI,KAAK,sBAER;IAED;;OAEG;IACH,IAAI,OAAO,YAEV;gBAEW,OAAO,EAAE,oCAAoC,CAAC,MAAM,CAAC;IAUjE;;OAEG;IACH,IAAI,aASF;IAEF;;OAEG;IACH,MAAM,UAAiB,KAAK,mBAoF1B;CACH"}
|