@ai-sdk/svelte 3.0.0-canary.15 → 3.0.0-canary.16

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 CHANGED
@@ -1,5 +1,19 @@
1
1
  # @ai-sdk/svelte
2
2
 
3
+ ## 3.0.0-canary.16
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [f04fb4a]
8
+ - Updated dependencies [fd1924b]
9
+ - Updated dependencies [957b739]
10
+ - Updated dependencies [fafc3f2]
11
+ - Updated dependencies [c9ad635]
12
+ - Updated dependencies [9bd5ab5]
13
+ - Updated dependencies [92cb0a2]
14
+ - ai@5.0.0-canary.17
15
+ - @ai-sdk/provider-utils@3.0.0-canary.14
16
+
3
17
  ## 3.0.0-canary.15
4
18
 
5
19
  ### Patch Changes
@@ -8,6 +8,9 @@ export type ChatOptions = Readonly<Omit<UseChatOptions, 'keepLastMessageOnError'
8
8
  * @default 1
9
9
  */
10
10
  maxSteps?: number;
11
+ '~internal'?: {
12
+ currentDate?: () => Date;
13
+ };
11
14
  }>;
12
15
  export type { CreateUIMessage, UIMessage };
13
16
  export declare class Chat {
@@ -45,6 +48,7 @@ export declare class Chat {
45
48
  */
46
49
  get messages(): UIMessage[];
47
50
  set messages(value: UIMessage[]);
51
+ private currentDate;
48
52
  constructor(options?: ChatOptions);
49
53
  /**
50
54
  * Append a user message to the chat list. This triggers the API call to fetch
@@ -52,7 +56,7 @@ export declare class Chat {
52
56
  * @param message The message to append
53
57
  * @param options Additional options to pass to the API call
54
58
  */
55
- append: (message: UIMessage | CreateUIMessage, { data, headers, body, experimental_attachments }?: ChatRequestOptions) => Promise<void>;
59
+ append: (message: UIMessage | CreateUIMessage, { data, headers, body }?: ChatRequestOptions) => Promise<void>;
56
60
  /**
57
61
  * Reload the last AI chat response for the given chat history. If the last
58
62
  * message isn't from the assistant, it will request the API to generate a
@@ -66,7 +70,9 @@ export declare class Chat {
66
70
  /** Form submission handler to automatically reset input and append a user message */
67
71
  handleSubmit: (event?: {
68
72
  preventDefault?: () => void;
69
- }, options?: ChatRequestOptions) => Promise<void>;
73
+ }, options?: ChatRequestOptions & {
74
+ files?: FileList;
75
+ }) => Promise<void>;
70
76
  addToolResult: ({ toolCallId, result, }: {
71
77
  toolCallId: string;
72
78
  result: unknown;
@@ -1 +1 @@
1
- {"version":3,"file":"chat.svelte.d.ts","sourceRoot":"","sources":["../src/chat.svelte.ts"],"names":[],"mappings":"AACA,OAAO,EAUL,KAAK,kBAAkB,EACvB,KAAK,eAAe,EACpB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,cAAc,EACpB,MAAM,IAAI,CAAC;AAQZ,MAAM,MAAM,WAAW,GAAG,QAAQ,CAChC,IAAI,CAAC,cAAc,EAAE,wBAAwB,CAAC,GAAG;IAC/C;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CACF,CAAC;AAEF,YAAY,EAAE,eAAe,EAAE,SAAS,EAAE,CAAC;AAE3C,qBAAa,IAAI;;IAOf;;;OAGG;IACH,QAAQ,CAAC,EAAE,SAAoD;IAI/D;;;;OAIG;IACH,IAAI,IAAI,IAGQ,SAAS,EAAE,GAAG,SAAS,CADtC;IACD,IAAI,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,SAAS,EAEtC;IAED;;;;;;;OAOG;IACH,IAAI,MAAM,kDAET;IAED,0CAA0C;IAC1C,IAAI,KAAK,sBAER;IAED,mFAAmF;IACnF,KAAK,SAAqB;IAE1B;;;;;OAKG;IACH,IAAI,QAAQ,IAAI,SAAS,EAAE,CAE1B;IACD,IAAI,QAAQ,CAAC,KAAK,EAAE,SAAS,EAAE,EAE9B;gBAEW,OAAO,GAAE,WAAgB;IAYrC;;;;;OAKG;IACH,MAAM,YACK,SAAS,GAAG,eAAe,sDACe,kBAAkB,mBAgBrE;IAEF;;;;OAIG;IACH,MAAM,6BAAmC,kBAAkB,mBAezD;IAEF;;OA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M,YACK,SAAS,GAAG,eAAe,4BACX,kBAAkB,mBAU3C;IAEF;;;;OAIG;IACH,MAAM,6BAAmC,kBAAkB,mBAezD;IAEF;;OAEG;IACH,IAAI,aASF;IAEF,qFAAqF;IACrF,YAAY,WACF;QAAE,cAAc,CAAC,EAAE,MAAM,IAAI,CAAA;KAAE,YAC9B,kBAAkB,GAAG;QAAE,KAAK,CAAC,EAAE,QAAQ,CAAA;KAAE,mBA2BlD;IAEF,aAAa,4BAGV;QACD,UAAU,EAAE,MAAM,CAAC;QACnB,MAAM,EAAE,OAAO,CAAC;KACjB,mBAmBC;CAqGH"}
@@ -1,5 +1,5 @@
1
1
  import { isAbortError } from '@ai-sdk/provider-utils';
2
- import { callChatApi, extractMaxToolInvocationStep, generateId, getToolInvocations, isAssistantMessageWithCompletedToolCalls, prepareAttachmentsForRequest, shouldResubmitMessages, updateToolCallResult, } from 'ai';
2
+ import { callChatApi, convertFileListToFileUIParts, extractMaxToolInvocationStep, generateId, getToolInvocations, isAssistantMessageWithCompletedToolCalls, shouldResubmitMessages, updateToolCallResult, } from 'ai';
3
3
  import { untrack } from 'svelte';
4
4
  import { KeyedChatStore, getChatContext, hasChatContext, } from './chat-context.svelte.js';
5
5
  export class Chat {
@@ -56,6 +56,7 @@ export class Chat {
56
56
  set messages(value) {
57
57
  untrack(() => (this.#store.messages = value));
58
58
  }
59
+ currentDate = $derived(this.#options['~internal']?.currentDate ?? (() => new Date()));
59
60
  constructor(options = {}) {
60
61
  if (hasChatContext()) {
61
62
  this.#keyedStore = getChatContext();
@@ -73,13 +74,11 @@ export class Chat {
73
74
  * @param message The message to append
74
75
  * @param options Additional options to pass to the API call
75
76
  */
76
- append = async (message, { data, headers, body, experimental_attachments } = {}) => {
77
- const attachmentsForRequest = await prepareAttachmentsForRequest(experimental_attachments);
77
+ append = async (message, { data, headers, body } = {}) => {
78
78
  const messages = this.messages.concat({
79
79
  ...message,
80
80
  id: message.id ?? this.#generateId(),
81
- createdAt: message.createdAt ?? new Date(),
82
- experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : undefined,
81
+ createdAt: message.createdAt ?? this.currentDate(),
83
82
  parts: message.parts,
84
83
  });
85
84
  return this.#triggerRequest({ messages, headers, body, data });
@@ -123,14 +122,15 @@ export class Chat {
123
122
  event?.preventDefault?.();
124
123
  if (!this.input && !options.allowEmptySubmit)
125
124
  return;
126
- const attachmentsForRequest = await prepareAttachmentsForRequest(options.experimental_attachments);
125
+ const fileParts = Array.isArray(options?.files)
126
+ ? options.files
127
+ : await convertFileListToFileUIParts(options?.files);
127
128
  const messages = this.messages.concat({
128
129
  id: this.#generateId(),
129
- createdAt: new Date(),
130
+ createdAt: this.currentDate(),
130
131
  role: 'user',
131
132
  content: this.input,
132
- experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : undefined,
133
- parts: [{ type: 'text', text: this.input }],
133
+ parts: [...fileParts, { type: 'text', text: this.input }],
134
134
  });
135
135
  const chatRequest = {
136
136
  messages,
@@ -171,12 +171,9 @@ export class Chat {
171
171
  this.messages = messages;
172
172
  const constructedMessagesPayload = this.#options.sendExtraMessageFields
173
173
  ? messages
174
- : messages.map(({ role, content, experimental_attachments, annotations, parts, }) => ({
174
+ : messages.map(({ role, content, annotations, parts }) => ({
175
175
  role,
176
176
  content,
177
- ...(experimental_attachments !== undefined && {
178
- experimental_attachments,
179
- }),
180
177
  ...(annotations !== undefined && { annotations }),
181
178
  ...(parts !== undefined && { parts }),
182
179
  }));
@@ -216,6 +213,7 @@ export class Chat {
216
213
  onToolCall: this.#options.onToolCall,
217
214
  onFinish: this.#options.onFinish,
218
215
  generateId: this.#generateId,
216
+ getCurrentDate: this.currentDate,
219
217
  fetch: this.#options.fetch,
220
218
  // callChatApi calls structuredClone on the message
221
219
  lastMessage: $state.snapshot(this.messages[this.messages.length - 1]),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/svelte",
3
- "version": "3.0.0-canary.15",
3
+ "version": "3.0.0-canary.16",
4
4
  "license": "Apache-2.0",
5
5
  "files": [
6
6
  "dist",
@@ -36,8 +36,8 @@
36
36
  }
37
37
  },
38
38
  "dependencies": {
39
- "@ai-sdk/provider-utils": "3.0.0-canary.13",
40
- "ai": "5.0.0-canary.16"
39
+ "@ai-sdk/provider-utils": "3.0.0-canary.14",
40
+ "ai": "5.0.0-canary.17"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "20.17.24",
@@ -1,11 +1,11 @@
1
1
  import { isAbortError } from '@ai-sdk/provider-utils';
2
2
  import {
3
3
  callChatApi,
4
+ convertFileListToFileUIParts,
4
5
  extractMaxToolInvocationStep,
5
6
  generateId,
6
7
  getToolInvocations,
7
8
  isAssistantMessageWithCompletedToolCalls,
8
- prepareAttachmentsForRequest,
9
9
  shouldResubmitMessages,
10
10
  updateToolCallResult,
11
11
  type ChatRequest,
@@ -32,6 +32,10 @@ export type ChatOptions = Readonly<
32
32
  * @default 1
33
33
  */
34
34
  maxSteps?: number;
35
+
36
+ '~internal'?: {
37
+ currentDate?: () => Date;
38
+ };
35
39
  }
36
40
  >;
37
41
 
@@ -97,6 +101,10 @@ export class Chat {
97
101
  untrack(() => (this.#store.messages = value));
98
102
  }
99
103
 
104
+ private currentDate = $derived(
105
+ this.#options['~internal']?.currentDate ?? (() => new Date()),
106
+ );
107
+
100
108
  constructor(options: ChatOptions = {}) {
101
109
  if (hasChatContext()) {
102
110
  this.#keyedStore = getChatContext();
@@ -117,18 +125,12 @@ export class Chat {
117
125
  */
118
126
  append = async (
119
127
  message: UIMessage | CreateUIMessage,
120
- { data, headers, body, experimental_attachments }: ChatRequestOptions = {},
128
+ { data, headers, body }: ChatRequestOptions = {},
121
129
  ) => {
122
- const attachmentsForRequest = await prepareAttachmentsForRequest(
123
- experimental_attachments,
124
- );
125
-
126
130
  const messages = this.messages.concat({
127
131
  ...message,
128
132
  id: message.id ?? this.#generateId(),
129
- createdAt: message.createdAt ?? new Date(),
130
- experimental_attachments:
131
- attachmentsForRequest.length > 0 ? attachmentsForRequest : undefined,
133
+ createdAt: message.createdAt ?? this.currentDate(),
132
134
  parts: message.parts,
133
135
  });
134
136
 
@@ -174,23 +176,21 @@ export class Chat {
174
176
  /** Form submission handler to automatically reset input and append a user message */
175
177
  handleSubmit = async (
176
178
  event?: { preventDefault?: () => void },
177
- options: ChatRequestOptions = {},
179
+ options: ChatRequestOptions & { files?: FileList } = {},
178
180
  ) => {
179
181
  event?.preventDefault?.();
180
182
  if (!this.input && !options.allowEmptySubmit) return;
181
183
 
182
- const attachmentsForRequest = await prepareAttachmentsForRequest(
183
- options.experimental_attachments,
184
- );
184
+ const fileParts = Array.isArray(options?.files)
185
+ ? options.files
186
+ : await convertFileListToFileUIParts(options?.files);
185
187
 
186
188
  const messages = this.messages.concat({
187
189
  id: this.#generateId(),
188
- createdAt: new Date(),
190
+ createdAt: this.currentDate(),
189
191
  role: 'user',
190
192
  content: this.input,
191
- experimental_attachments:
192
- attachmentsForRequest.length > 0 ? attachmentsForRequest : undefined,
193
- parts: [{ type: 'text', text: this.input }],
193
+ parts: [...fileParts, { type: 'text', text: this.input }],
194
194
  });
195
195
 
196
196
  const chatRequest: ChatRequest = {
@@ -251,23 +251,12 @@ export class Chat {
251
251
 
252
252
  const constructedMessagesPayload = this.#options.sendExtraMessageFields
253
253
  ? messages
254
- : messages.map(
255
- ({
256
- role,
257
- content,
258
- experimental_attachments,
259
- annotations,
260
- parts,
261
- }) => ({
262
- role,
263
- content,
264
- ...(experimental_attachments !== undefined && {
265
- experimental_attachments,
266
- }),
267
- ...(annotations !== undefined && { annotations }),
268
- ...(parts !== undefined && { parts }),
269
- }),
270
- );
254
+ : messages.map(({ role, content, annotations, parts }) => ({
255
+ role,
256
+ content,
257
+ ...(annotations !== undefined && { annotations }),
258
+ ...(parts !== undefined && { parts }),
259
+ }));
271
260
 
272
261
  const existingData = this.data ?? [];
273
262
  await callChatApi({
@@ -306,6 +295,7 @@ export class Chat {
306
295
  onToolCall: this.#options.onToolCall,
307
296
  onFinish: this.#options.onFinish,
308
297
  generateId: this.#generateId,
298
+ getCurrentDate: this.currentDate,
309
299
  fetch: this.#options.fetch,
310
300
  // callChatApi calls structuredClone on the message
311
301
  lastMessage: $state.snapshot(this.messages[this.messages.length - 1]),