@ai-sdk/svelte 2.1.11 → 3.0.0-alpha.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.
Files changed (35) hide show
  1. package/CHANGELOG.md +261 -20
  2. package/README.md +5 -5
  3. package/dist/chat.svelte.d.ts +13 -27
  4. package/dist/chat.svelte.d.ts.map +1 -1
  5. package/dist/chat.svelte.js +24 -61
  6. package/dist/completion.svelte.d.ts +2 -9
  7. package/dist/completion.svelte.d.ts.map +1 -1
  8. package/dist/completion.svelte.js +4 -22
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/structured-object-context.svelte.d.ts +1 -1
  12. package/dist/structured-object-context.svelte.d.ts.map +1 -1
  13. package/dist/structured-object.svelte.d.ts +1 -1
  14. package/dist/structured-object.svelte.d.ts.map +1 -1
  15. package/dist/structured-object.svelte.js +5 -5
  16. package/dist/tests/chat-synchronization.svelte +3 -3
  17. package/dist/tests/chat-synchronization.svelte.d.ts +3 -3
  18. package/dist/tests/chat-synchronization.svelte.d.ts.map +1 -1
  19. package/dist/tests/structured-object-synchronization.svelte +1 -1
  20. package/dist/tests/structured-object-synchronization.svelte.d.ts +1 -1
  21. package/dist/tests/structured-object-synchronization.svelte.d.ts.map +1 -1
  22. package/package.json +10 -7
  23. package/src/chat-context.svelte.ts +10 -5
  24. package/src/chat.svelte.ts +64 -119
  25. package/src/completion-context.svelte.ts +1 -1
  26. package/src/completion.svelte.ts +11 -28
  27. package/src/index.ts +1 -2
  28. package/src/structured-object-context.svelte.ts +1 -1
  29. package/src/structured-object.svelte.ts +5 -5
  30. package/src/tests/chat-synchronization.svelte +3 -3
  31. package/src/tests/structured-object-synchronization.svelte +1 -1
  32. package/dist/chat-context.svelte.d.ts +0 -14
  33. package/dist/chat-context.svelte.d.ts.map +0 -1
  34. package/dist/completion-context.svelte.d.ts +0 -15
  35. package/dist/completion-context.svelte.d.ts.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,48 +1,289 @@
1
1
  # @ai-sdk/svelte
2
2
 
3
- ## 2.1.11
3
+ ## 3.0.0-alpha.1
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - Updated dependencies [6c59ae7]
8
- - @ai-sdk/ui-utils@1.2.10
7
+ - Updated dependencies [b346545]
8
+ - Updated dependencies [109c0ac]
9
+ - ai@5.0.0-alpha.1
10
+ - @ai-sdk/provider-utils@3.0.0-alpha.1
9
11
 
10
- ## 2.1.10
12
+ ## 3.0.0-canary.23
11
13
 
12
14
  ### Patch Changes
13
15
 
14
- - Updated dependencies [62181ef]
15
- - @ai-sdk/ui-utils@1.2.9
16
+ - Updated dependencies [bedb239]
17
+ - Updated dependencies [507ac1d]
18
+ - Updated dependencies [2b9bbcd]
19
+ - Updated dependencies [f7e8bf4]
20
+ - Updated dependencies [cda32ba]
21
+ - Updated dependencies [50f0362]
22
+ - Updated dependencies [ed675de]
23
+ - Updated dependencies [faf8446]
24
+ - Updated dependencies [64f6d64]
25
+ - ai@5.0.0-canary.24
26
+ - @ai-sdk/provider-utils@3.0.0-canary.19
16
27
 
17
- ## 2.1.9
28
+ ## 3.0.0-canary.22
29
+
30
+ ### Major Changes
31
+
32
+ - 40acf9b: feat (ui): introduce ChatStore and ChatTransport
33
+
34
+ ### Patch Changes
35
+
36
+ - Updated dependencies [40acf9b]
37
+ - @ai-sdk/provider-utils@3.0.0-canary.18
38
+ - ai@5.0.0-canary.23
39
+
40
+ ## 3.0.0-canary.21
41
+
42
+ ### Patch Changes
43
+
44
+ - Updated dependencies [e7dc6c7]
45
+ - Updated dependencies [a34eb39]
46
+ - Updated dependencies [b33ed7a]
47
+ - Updated dependencies [765f1cd]
48
+ - ai@5.0.0-canary.22
49
+
50
+ ## 3.0.0-canary.20
51
+
52
+ ### Patch Changes
53
+
54
+ - Updated dependencies [d964901]
55
+ - Updated dependencies [0560977]
56
+ - Updated dependencies [66af894]
57
+ - Updated dependencies [516be5b]
58
+ - Updated dependencies [bfbfc4c]
59
+ - Updated dependencies [ea7a7c9]
60
+ - Updated dependencies [1409e13]
61
+ - ai@5.0.0-canary.21
62
+ - @ai-sdk/provider-utils@3.0.0-canary.17
63
+
64
+ ## 3.0.0-canary.19
65
+
66
+ ### Major Changes
67
+
68
+ - 496bbc1: chore (ui): inline/remove ChatRequest type
69
+
70
+ ### Patch Changes
71
+
72
+ - Updated dependencies [13fef90]
73
+ - Updated dependencies [e90d45d]
74
+ - Updated dependencies [bc3109f]
75
+ - Updated dependencies [496bbc1]
76
+ - Updated dependencies [da70d79]
77
+ - Updated dependencies [bcea599]
78
+ - Updated dependencies [48d675a]
79
+ - Updated dependencies [c7710a9]
80
+ - Updated dependencies [87b828f]
81
+ - Updated dependencies [35fc02c]
82
+ - Updated dependencies [b983b51]
83
+ - ai@5.0.0-canary.20
84
+ - @ai-sdk/provider-utils@3.0.0-canary.16
85
+
86
+ ## 3.0.0-canary.18
18
87
 
19
88
  ### Patch Changes
20
89
 
21
- - @ai-sdk/provider-utils@2.2.7
22
- - @ai-sdk/ui-utils@1.2.8
90
+ - Updated dependencies [2d03e19]
91
+ - Updated dependencies [319b989]
92
+ - Updated dependencies [441d042]
93
+ - Updated dependencies [dcc549b]
94
+ - Updated dependencies [cb2b53a]
95
+ - Updated dependencies [e244a78]
96
+ - ai@5.0.0-canary.19
23
97
 
24
- ## 2.1.8
98
+ ## 3.0.0-canary.17
25
99
 
26
100
  ### Patch Changes
27
101
 
28
- - @ai-sdk/provider-utils@2.2.6
29
- - @ai-sdk/ui-utils@1.2.7
102
+ - Updated dependencies [a571d6e]
103
+ - Updated dependencies [c60f895]
104
+ - Updated dependencies [332167b]
105
+ - Updated dependencies [a8c8bd5]
106
+ - Updated dependencies [a662dea]
107
+ - Updated dependencies [41fa418]
108
+ - @ai-sdk/provider-utils@3.0.0-canary.15
109
+ - ai@5.0.0-canary.18
30
110
 
31
- ## 2.1.7
111
+ ## 3.0.0-canary.16
32
112
 
33
113
  ### Patch Changes
34
114
 
35
- - Updated dependencies [c21fa6d]
36
- - @ai-sdk/provider-utils@2.2.5
37
- - @ai-sdk/ui-utils@1.2.6
115
+ - Updated dependencies [f04fb4a]
116
+ - Updated dependencies [fd1924b]
117
+ - Updated dependencies [957b739]
118
+ - Updated dependencies [fafc3f2]
119
+ - Updated dependencies [c9ad635]
120
+ - Updated dependencies [9bd5ab5]
121
+ - Updated dependencies [92cb0a2]
122
+ - ai@5.0.0-canary.17
123
+ - @ai-sdk/provider-utils@3.0.0-canary.14
124
+
125
+ ## 3.0.0-canary.15
126
+
127
+ ### Patch Changes
128
+
129
+ - Updated dependencies [ec78cdc]
130
+ - Updated dependencies [9b4d074]
131
+ - Updated dependencies [8b86e99]
132
+ - Updated dependencies [28ad69e]
133
+ - Updated dependencies [2524fc7]
134
+ - Updated dependencies [ec5933d]
135
+ - Updated dependencies [175b868]
136
+ - ai@5.0.0-canary.16
137
+
138
+ ## 3.0.0-canary.14
139
+
140
+ ### Patch Changes
141
+
142
+ - Updated dependencies [d9209ca]
143
+ - Updated dependencies [ea27cc6]
144
+ - Updated dependencies [0ff02bb]
145
+ - Updated dependencies [4bfe9ec]
146
+ - Updated dependencies [2877a74]
147
+ - ai@5.0.0-canary.15
148
+ - @ai-sdk/provider-utils@3.0.0-canary.13
149
+
150
+ ## 3.0.0-canary.13
151
+
152
+ ### Patch Changes
153
+
154
+ - Updated dependencies [9bf7291]
155
+ - Updated dependencies [4617fab]
156
+ - Updated dependencies [a76a62b]
157
+ - ai@5.0.0-canary.14
158
+ - @ai-sdk/provider-utils@3.0.0-canary.12
159
+
160
+ ## 3.0.0-canary.12
161
+
162
+ ### Patch Changes
163
+
164
+ - Updated dependencies [14cb3be]
165
+ - Updated dependencies [66962ed]
166
+ - Updated dependencies [9301f86]
167
+ - ai@5.0.0-canary.13
168
+ - @ai-sdk/provider-utils@3.0.0-canary.11
169
+
170
+ ## 3.0.0-canary.11
171
+
172
+ ### Patch Changes
173
+
174
+ - ai@5.0.0-canary.12
175
+ - @ai-sdk/provider-utils@3.0.0-canary.10
176
+
177
+ ## 3.0.0-canary.10
178
+
179
+ ### Patch Changes
180
+
181
+ - Updated dependencies [8e64e9c]
182
+ - ai@5.0.0-canary.11
183
+ - @ai-sdk/provider-utils@3.0.0-canary.9
184
+
185
+ ## 3.0.0-canary.9
186
+
187
+ ### Patch Changes
188
+
189
+ - Updated dependencies [d8aeaef]
190
+ - Updated dependencies [3e10408]
191
+ - ai@5.0.0-canary.10
192
+
193
+ ## 3.0.0-canary.8
194
+
195
+ ### Patch Changes
196
+
197
+ - Updated dependencies [a847c3e]
198
+ - Updated dependencies [5d142ab]
199
+ - Updated dependencies [cb9c9e4]
200
+ - Updated dependencies [8aa9e20]
201
+ - Updated dependencies [b32e192]
202
+ - ai@5.0.0-canary.9
203
+ - @ai-sdk/provider-utils@3.0.0-canary.8
204
+
205
+ ## 3.0.0-canary.7
206
+
207
+ ### Patch Changes
208
+
209
+ - Updated dependencies [5d1e3ba]
210
+ - Updated dependencies [26735b5]
211
+ - Updated dependencies [14c9410]
212
+ - Updated dependencies [7827a49]
213
+ - Updated dependencies [bd8a36c]
214
+ - Updated dependencies [b6f9f3c]
215
+ - Updated dependencies [92c8e66]
216
+ - Updated dependencies [fd65bc6]
217
+ - Updated dependencies [5bdff05]
218
+ - ai@5.0.0-canary.8
219
+ - @ai-sdk/provider-utils@3.0.0-canary.7
220
+
221
+ ## 3.0.0-canary.6
222
+
223
+ ### Patch Changes
224
+
225
+ - Updated dependencies [0b78e17]
226
+ - Updated dependencies [6fba4c7]
227
+ - Updated dependencies [3e3b9df]
228
+ - Updated dependencies [1766ede]
229
+ - Updated dependencies [f10304b]
230
+ - ai@5.0.0-canary.7
231
+ - @ai-sdk/provider-utils@3.0.0-canary.6
232
+
233
+ ## 3.0.0-canary.5
234
+
235
+ ### Patch Changes
236
+
237
+ - ai@5.0.0-canary.6
238
+ - @ai-sdk/provider-utils@3.0.0-canary.5
239
+
240
+ ## 3.0.0-canary.4
241
+
242
+ ### Patch Changes
243
+
244
+ - Updated dependencies [b71fe8d]
245
+ - Updated dependencies [d91b50d]
246
+ - ai@5.0.0-canary.5
247
+ - @ai-sdk/provider-utils@3.0.0-canary.4
248
+
249
+ ## 3.0.0-canary.3
250
+
251
+ ### Patch Changes
252
+
253
+ - Updated dependencies [a166433]
254
+ - Updated dependencies [9f95b35]
255
+ - @ai-sdk/provider-utils@3.0.0-canary.3
256
+ - @ai-sdk/ui-utils@2.0.0-canary.3
257
+
258
+ ## 3.0.0-canary.2
259
+
260
+ ### Patch Changes
261
+
262
+ - @ai-sdk/provider-utils@3.0.0-canary.2
263
+ - @ai-sdk/ui-utils@2.0.0-canary.2
264
+
265
+ ## 3.0.0-canary.1
266
+
267
+ ### Patch Changes
268
+
269
+ - Updated dependencies [060370c]
270
+ - Updated dependencies [0c0c0b3]
271
+ - Updated dependencies [63d791d]
272
+ - @ai-sdk/provider-utils@3.0.0-canary.1
273
+ - @ai-sdk/ui-utils@2.0.0-canary.1
274
+
275
+ ## 3.0.0-canary.0
276
+
277
+ ### Major Changes
38
278
 
39
- ## 2.1.6
279
+ - d5f588f: AI SDK 5
40
280
 
41
281
  ### Patch Changes
42
282
 
43
- - Updated dependencies [2c19b9a]
44
- - @ai-sdk/provider-utils@2.2.4
45
- - @ai-sdk/ui-utils@1.2.5
283
+ - Updated dependencies [d5f588f]
284
+ - Updated dependencies [9477ebb]
285
+ - @ai-sdk/provider-utils@3.0.0-canary.0
286
+ - @ai-sdk/ui-utils@2.0.0-canary.0
46
287
 
47
288
  ## 2.1.5
48
289
 
package/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # AI SDK: Svelte provider
2
2
 
3
- [Svelte](https://svelte.dev/) UI components for the [AI SDK](https://sdk.vercel.ai/docs):
3
+ [Svelte](https://svelte.dev/) UI components for the [AI SDK](https://ai-sdk.dev/docs):
4
4
 
5
- - [`Chat`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-chat)
6
- - [`Completion`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-completion)
7
- - [`StructuredObject`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-object)
5
+ - [`Chat`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat)
6
+ - [`Completion`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-completion)
7
+ - [`StructuredObject`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-object)
8
8
 
9
- For information on the few ways the Svelte APIs differ from the React ones, see [the docs](https://sdk.vercel.ai/docs/getting-started/svelte#how-does-ai-sdksvelte-differ-from-ai-sdkreact)
9
+ For information on the few ways the Svelte APIs differ from the React ones, see [the docs](https://ai-sdk.dev/docs/getting-started/svelte#how-does-ai-sdksvelte-differ-from-ai-sdkreact)
@@ -1,29 +1,13 @@
1
- import { type UIMessage, type UseChatOptions, type JSONValue, type Message, type CreateMessage, type ChatRequestOptions } from '@ai-sdk/ui-utils';
2
- export type ChatOptions = Readonly<Omit<UseChatOptions, 'keepLastMessageOnError'> & {
3
- /**
4
- * Maximum number of sequential LLM calls (steps), e.g. when you use tool calls.
5
- * Must be at least 1.
6
- * A maximum number is required to prevent infinite loops in the case of misconfigured tools.
7
- * By default, it's set to 1, which means that only a single LLM call is made.
8
- * @default 1
9
- */
10
- maxSteps?: number;
11
- }>;
12
- export type { CreateMessage, Message, UIMessage };
13
- export declare class Chat {
1
+ import { type ChatRequestOptions, type CreateUIMessage, type OriginalUseChatOptions, type UIMessage } from 'ai';
2
+ export type ChatOptions<MESSAGE_METADATA = unknown> = Readonly<OriginalUseChatOptions<MESSAGE_METADATA>>;
3
+ export type { CreateUIMessage, UIMessage };
4
+ export declare class Chat<MESSAGE_METADATA = unknown> {
14
5
  #private;
15
6
  /**
16
7
  * The id of the chat. If not provided through the constructor, a random ID will be generated
17
8
  * using the provided `generateId` function, or a built-in function if not provided.
18
9
  */
19
- readonly id: string;
20
- /**
21
- * Additional data added on the server via StreamData.
22
- *
23
- * This is writable, so you can use it to transform or clear the chat data.
24
- */
25
- get data(): JSONValue[] | undefined;
26
- set data(value: JSONValue[] | undefined);
10
+ readonly chatId: string;
27
11
  /**
28
12
  * Hook status:
29
13
  *
@@ -43,22 +27,22 @@ export declare class Chat {
43
27
  * This is writable, which is useful when you want to edit the messages on the client, and then
44
28
  * trigger {@link reload} to regenerate the AI response.
45
29
  */
46
- get messages(): UIMessage[];
47
- set messages(value: Message[]);
48
- constructor(options?: ChatOptions);
30
+ get messages(): UIMessage<MESSAGE_METADATA>[];
31
+ set messages(value: UIMessage<MESSAGE_METADATA>[]);
32
+ constructor(options?: ChatOptions<MESSAGE_METADATA>);
49
33
  /**
50
34
  * Append a user message to the chat list. This triggers the API call to fetch
51
35
  * the assistant's response.
52
36
  * @param message The message to append
53
37
  * @param options Additional options to pass to the API call
54
38
  */
55
- append: (message: Message | CreateMessage, { data, headers, body, experimental_attachments }?: ChatRequestOptions) => Promise<void>;
39
+ append: (message: UIMessage<MESSAGE_METADATA> | CreateUIMessage<MESSAGE_METADATA>, { headers, body }?: ChatRequestOptions) => Promise<void>;
56
40
  /**
57
41
  * Reload the last AI chat response for the given chat history. If the last
58
42
  * message isn't from the assistant, it will request the API to generate a
59
43
  * new response.
60
44
  */
61
- reload: ({ data, headers, body }?: ChatRequestOptions) => Promise<void>;
45
+ reload: ({ headers, body }?: ChatRequestOptions) => Promise<void>;
62
46
  /**
63
47
  * Abort the current request immediately, keep the generated tokens if any.
64
48
  */
@@ -66,7 +50,9 @@ export declare class Chat {
66
50
  /** Form submission handler to automatically reset input and append a user message */
67
51
  handleSubmit: (event?: {
68
52
  preventDefault?: () => void;
69
- }, options?: ChatRequestOptions) => Promise<void>;
53
+ }, options?: ChatRequestOptions & {
54
+ files?: FileList;
55
+ }) => Promise<void>;
70
56
  addToolResult: ({ toolCallId, result, }: {
71
57
  toolCallId: string;
72
58
  result: unknown;
@@ -1 +1 @@
1
- {"version":3,"file":"chat.svelte.d.ts","sourceRoot":"","sources":["../src/chat.svelte.ts"],"names":[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vocationStep, callChatApi, shouldResubmitMessages, prepareAttachmentsForRequest, getMessageParts, updateToolCallResult, isAssistantMessageWithCompletedToolCalls, } from '@ai-sdk/ui-utils';
2
1
  import { isAbortError } from '@ai-sdk/provider-utils';
3
- import { KeyedChatStore, getChatContext, hasChatContext, } from './chat-context.svelte.js';
2
+ import { callChatApi, convertFileListToFileUIParts, extractMaxToolInvocationStep, generateId, getToolInvocations, isAssistantMessageWithCompletedToolCalls, shouldResubmitMessages, updateToolCallResult, } from 'ai';
4
3
  import { untrack } from 'svelte';
4
+ import { KeyedChatStore, getChatContext, hasChatContext, } from './chat-context.svelte.js';
5
5
  export class Chat {
6
6
  #options = {};
7
7
  #api = $derived(this.#options.api ?? '/api/chat');
8
8
  #generateId = $derived(this.#options.generateId ?? generateId);
9
9
  #maxSteps = $derived(this.#options.maxSteps ?? 1);
10
- #streamProtocol = $derived(this.#options.streamProtocol ?? 'data');
10
+ #streamProtocol = $derived(this.#options.streamProtocol ?? 'ui-message');
11
11
  #keyedStore = $state();
12
12
  /**
13
13
  * The id of the chat. If not provided through the constructor, a random ID will be generated
14
14
  * using the provided `generateId` function, or a built-in function if not provided.
15
15
  */
16
- id = $derived(this.#options.id ?? this.#generateId());
17
- #store = $derived(this.#keyedStore.get(this.id));
16
+ chatId = $derived(this.#options.chatId ?? this.#generateId());
17
+ #store = $derived(this.#keyedStore.get(this.chatId));
18
+ #messageMetadataSchema = $derived(this.#options.messageMetadataSchema);
18
19
  #abortController;
19
- /**
20
- * Additional data added on the server via StreamData.
21
- *
22
- * This is writable, so you can use it to transform or clear the chat data.
23
- */
24
- get data() {
25
- return this.#store.data;
26
- }
27
- set data(value) {
28
- this.#store.data = value;
29
- }
30
20
  /**
31
21
  * Hook status:
32
22
  *
@@ -54,7 +44,7 @@ export class Chat {
54
44
  return this.#store.messages;
55
45
  }
56
46
  set messages(value) {
57
- untrack(() => (this.#store.messages = fillMessageParts(value)));
47
+ untrack(() => (this.#store.messages = value));
58
48
  }
59
49
  constructor(options = {}) {
60
50
  if (hasChatContext()) {
@@ -73,23 +63,19 @@ export class Chat {
73
63
  * @param message The message to append
74
64
  * @param options Additional options to pass to the API call
75
65
  */
76
- append = async (message, { data, headers, body, experimental_attachments } = {}) => {
77
- const attachmentsForRequest = await prepareAttachmentsForRequest(experimental_attachments);
66
+ append = async (message, { headers, body } = {}) => {
78
67
  const messages = this.messages.concat({
79
68
  ...message,
80
69
  id: message.id ?? this.#generateId(),
81
- createdAt: message.createdAt ?? new Date(),
82
- experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : undefined,
83
- parts: getMessageParts(message),
84
70
  });
85
- return this.#triggerRequest({ messages, headers, body, data });
71
+ return this.#triggerRequest({ messages, headers, body });
86
72
  };
87
73
  /**
88
74
  * Reload the last AI chat response for the given chat history. If the last
89
75
  * message isn't from the assistant, it will request the API to generate a
90
76
  * new response.
91
77
  */
92
- reload = async ({ data, headers, body } = {}) => {
78
+ reload = async ({ headers, body } = {}) => {
93
79
  if (this.messages.length === 0) {
94
80
  return;
95
81
  }
@@ -100,7 +86,6 @@ export class Chat {
100
86
  : this.messages,
101
87
  headers,
102
88
  body,
103
- data,
104
89
  });
105
90
  };
106
91
  /**
@@ -121,24 +106,21 @@ export class Chat {
121
106
  /** Form submission handler to automatically reset input and append a user message */
122
107
  handleSubmit = async (event, options = {}) => {
123
108
  event?.preventDefault?.();
124
- if (!this.input && !options.allowEmptySubmit)
109
+ const fileParts = Array.isArray(options?.files)
110
+ ? options.files
111
+ : await convertFileListToFileUIParts(options?.files);
112
+ if (!this.input && fileParts.length === 0)
125
113
  return;
126
- const attachmentsForRequest = await prepareAttachmentsForRequest(options.experimental_attachments);
127
114
  const messages = this.messages.concat({
128
115
  id: this.#generateId(),
129
- createdAt: new Date(),
130
116
  role: 'user',
131
- content: this.input,
132
- experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : undefined,
133
- parts: [{ type: 'text', text: this.input }],
117
+ parts: [...fileParts, { type: 'text', text: this.input }],
134
118
  });
135
- const chatRequest = {
119
+ const request = this.#triggerRequest({
136
120
  messages,
137
121
  headers: options.headers,
138
122
  body: options.body,
139
- data: options.data,
140
- };
141
- const request = this.#triggerRequest(chatRequest);
123
+ });
142
124
  this.input = '';
143
125
  await request;
144
126
  };
@@ -161,34 +143,19 @@ export class Chat {
161
143
  #triggerRequest = async (chatRequest) => {
162
144
  this.#store.status = 'submitted';
163
145
  this.#store.error = undefined;
164
- const messages = fillMessageParts(chatRequest.messages);
146
+ const messages = chatRequest.messages;
165
147
  const messageCount = messages.length;
166
- const maxStep = extractMaxToolInvocationStep(messages[messages.length - 1]?.toolInvocations);
148
+ const maxStep = extractMaxToolInvocationStep(getToolInvocations(messages[messages.length - 1]));
167
149
  try {
168
150
  const abortController = new AbortController();
169
151
  this.#abortController = abortController;
170
152
  // Optimistically update messages
171
153
  this.messages = messages;
172
- const constructedMessagesPayload = this.#options.sendExtraMessageFields
173
- ? messages
174
- : messages.map(({ role, content, experimental_attachments, data, annotations, toolInvocations, parts, }) => ({
175
- role,
176
- content,
177
- ...(experimental_attachments !== undefined && {
178
- experimental_attachments,
179
- }),
180
- ...(data !== undefined && { data }),
181
- ...(annotations !== undefined && { annotations }),
182
- ...(toolInvocations !== undefined && { toolInvocations }),
183
- ...(parts !== undefined && { parts }),
184
- }));
185
- const existingData = this.data ?? [];
186
154
  await callChatApi({
187
155
  api: this.#api,
188
156
  body: {
189
- id: this.id,
190
- messages: constructedMessagesPayload,
191
- data: chatRequest.data,
157
+ id: this.chatId,
158
+ messages,
192
159
  ...$state.snapshot(this.#options.body),
193
160
  ...chatRequest.body,
194
161
  },
@@ -199,10 +166,9 @@ export class Chat {
199
166
  ...chatRequest.headers,
200
167
  },
201
168
  abortController: () => abortController,
202
- restoreMessagesOnFailure: () => { },
203
- onResponse: this.#options.onResponse,
204
- onUpdate: ({ message, data, replaceLastMessage }) => {
169
+ onUpdate: ({ message }) => {
205
170
  this.#store.status = 'streaming';
171
+ const replaceLastMessage = message.id === messages[messages.length - 1].id;
206
172
  this.messages = messages;
207
173
  if (replaceLastMessage) {
208
174
  this.messages[this.messages.length - 1] = message;
@@ -210,10 +176,6 @@ export class Chat {
210
176
  else {
211
177
  this.messages.push(message);
212
178
  }
213
- if (data?.length) {
214
- this.data = existingData;
215
- this.data.push(...data);
216
- }
217
179
  },
218
180
  onToolCall: this.#options.onToolCall,
219
181
  onFinish: this.#options.onFinish,
@@ -221,6 +183,7 @@ export class Chat {
221
183
  fetch: this.#options.fetch,
222
184
  // callChatApi calls structuredClone on the message
223
185
  lastMessage: $state.snapshot(this.messages[this.messages.length - 1]),
186
+ messageMetadataSchema: this.#messageMetadataSchema,
224
187
  });
225
188
  this.#abortController = undefined;
226
189
  this.#store.status = 'ready';
@@ -1,17 +1,10 @@
1
- import { type UseCompletionOptions, type JSONValue, type RequestOptions } from '@ai-sdk/ui-utils';
1
+ import { type CompletionRequestOptions, type UseCompletionOptions } from 'ai';
2
2
  export type CompletionOptions = Readonly<UseCompletionOptions>;
3
3
  export declare class Completion {
4
4
  #private;
5
5
  /** The current completion result */
6
6
  get completion(): string;
7
7
  set completion(value: string);
8
- /**
9
- * Additional data added on the server via StreamData.
10
- *
11
- * This is writable, so you can use it to transform or clear the chat data.
12
- */
13
- get data(): JSONValue[];
14
- set data(value: JSONValue[]);
15
8
  /** The error object of the API request */
16
9
  get error(): Error | undefined;
17
10
  /** The current value of the input. Writable, so it can be bound to form inputs. */
@@ -28,7 +21,7 @@ export declare class Completion {
28
21
  /**
29
22
  * Send a new prompt to the API endpoint and update the completion state.
30
23
  */
31
- complete: (prompt: string, options?: RequestOptions) => Promise<string | null | undefined>;
24
+ complete: (prompt: string, options?: CompletionRequestOptions) => Promise<string | null | undefined>;
32
25
  /** Form submission handler to automatically reset input and call the completion API */
33
26
  handleSubmit: (event?: {
34
27
  preventDefault?: () => void;
@@ -1 +1 @@
1
- {"version":3,"file":"completion.svelte.d.ts","sourceRoot":"","sources":["../src/completion.svelte.ts"],"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IAAI,aASF;IAEF;;OAEG;IACH,QAAQ,WAAkB,MAAM,YAAY,wBAAwB,wCAC5B;IAExC,uFAAuF;IACvF,YAAY,WAAkB;QAAE,cAAc,CAAC,EAAE,MAAM,IAAI,CAAA;KAAE,mBAK3D;CAkCH"}
@@ -1,4 +1,4 @@
1
- import { generateId, callCompletionApi, } from '@ai-sdk/ui-utils';
1
+ import { callCompletionApi, generateId, } from 'ai';
2
2
  import { KeyedCompletionStore, getCompletionContext, hasCompletionContext, } from './completion-context.svelte.js';
3
3
  export class Completion {
4
4
  #options = {};
@@ -15,17 +15,6 @@ export class Completion {
15
15
  set completion(value) {
16
16
  this.#store.completions.set(this.#id, value);
17
17
  }
18
- /**
19
- * Additional data added on the server via StreamData.
20
- *
21
- * This is writable, so you can use it to transform or clear the chat data.
22
- */
23
- get data() {
24
- return this.#store.data;
25
- }
26
- set data(value) {
27
- this.#store.data = value;
28
- }
29
18
  /** The error object of the API request */
30
19
  get error() {
31
20
  return this.#store.error;
@@ -39,12 +28,9 @@ export class Completion {
39
28
  return this.#store.loading;
40
29
  }
41
30
  constructor(options = {}) {
42
- if (hasCompletionContext()) {
43
- this.#keyedStore = getCompletionContext();
44
- }
45
- else {
46
- this.#keyedStore = new KeyedCompletionStore();
47
- }
31
+ this.#keyedStore = hasCompletionContext()
32
+ ? getCompletionContext()
33
+ : new KeyedCompletionStore();
48
34
  this.#options = options;
49
35
  this.completion = options.initialCompletion ?? '';
50
36
  this.input = options.initialInput ?? '';
@@ -91,9 +77,6 @@ export class Completion {
91
77
  setCompletion: completion => {
92
78
  this.completion = completion;
93
79
  },
94
- onData: data => {
95
- this.data.push(...data);
96
- },
97
80
  setLoading: loading => {
98
81
  this.#store.loading = loading;
99
82
  },
@@ -103,7 +86,6 @@ export class Completion {
103
86
  setAbortController: abortController => {
104
87
  this.#abortController = abortController ?? undefined;
105
88
  },
106
- onResponse: this.#options.onResponse,
107
89
  onFinish: this.#options.onFinish,
108
90
  onError: this.#options.onError,
109
91
  });