@ai-sdk/svelte 3.0.0-alpha.7 → 3.0.0-alpha.9
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 +20 -0
- package/dist/chat.svelte.d.ts +8 -51
- package/dist/chat.svelte.d.ts.map +1 -1
- package/dist/chat.svelte.js +29 -119
- package/dist/context-provider.d.ts +1 -3
- package/dist/context-provider.d.ts.map +1 -1
- package/dist/context-provider.js +1 -10
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/structured-object.svelte.d.ts +7 -6
- package/dist/structured-object.svelte.d.ts.map +1 -1
- package/dist/structured-object.svelte.js +0 -1
- package/dist/tests/structured-object-synchronization.svelte.d.ts +2 -2
- package/package.json +5 -5
- package/src/chat.svelte.ts +51 -166
- package/src/context-provider.ts +1 -16
- package/src/index.ts +3 -13
- package/src/structured-object.svelte.ts +16 -7
- package/dist/chat-store.svelte.d.ts +0 -4
- package/dist/chat-store.svelte.d.ts.map +0 -1
- package/dist/chat-store.svelte.js +0 -43
- package/dist/tests/chat-synchronization.svelte +0 -16
- package/dist/tests/chat-synchronization.svelte.d.ts +0 -11
- package/dist/tests/chat-synchronization.svelte.d.ts.map +0 -1
- package/src/chat-store.svelte.ts +0 -85
- package/src/tests/chat-synchronization.svelte +0 -16
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @ai-sdk/svelte
|
|
2
2
|
|
|
3
|
+
## 3.0.0-alpha.9
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [8255639]
|
|
8
|
+
- Updated dependencies [9ae327d]
|
|
9
|
+
- ai@5.0.0-alpha.9
|
|
10
|
+
- @ai-sdk/provider-utils@3.0.0-alpha.9
|
|
11
|
+
|
|
12
|
+
## 3.0.0-alpha.8
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Updated dependencies [4fef487]
|
|
17
|
+
- Updated dependencies [6b1c55c]
|
|
18
|
+
- Updated dependencies [2e4f9e4]
|
|
19
|
+
- Updated dependencies [c25cbce]
|
|
20
|
+
- @ai-sdk/provider-utils@3.0.0-alpha.8
|
|
21
|
+
- ai@5.0.0-alpha.8
|
|
22
|
+
|
|
3
23
|
## 3.0.0-alpha.7
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/dist/chat.svelte.d.ts
CHANGED
|
@@ -1,63 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type
|
|
3
|
-
export type { CreateUIMessage, UIMessage };
|
|
4
|
-
export declare class Chat<MESSAGE_METADATA = unknown, DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas> {
|
|
5
|
-
#private;
|
|
1
|
+
import { AbstractChat, type BaseChatInit, type ChatRequestOptions, type CreateUIMessage, type UIDataPartSchemas, type UIMessage } from 'ai';
|
|
2
|
+
export type ChatInit<MESSAGE_METADATA = unknown, DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas> = Readonly<BaseChatInit<MESSAGE_METADATA, DATA_PART_SCHEMAS>> & {
|
|
6
3
|
/**
|
|
7
|
-
*
|
|
8
|
-
* using the provided `generateId` function, or a built-in function if not provided.
|
|
4
|
+
* Initial input of the chat.
|
|
9
5
|
*/
|
|
10
|
-
|
|
6
|
+
initialInput?: string;
|
|
7
|
+
};
|
|
8
|
+
export type { CreateUIMessage, UIMessage };
|
|
9
|
+
export declare class Chat<MESSAGE_METADATA = unknown, DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas> extends AbstractChat<MESSAGE_METADATA, DATA_PART_SCHEMAS> {
|
|
11
10
|
/** The current value of the input. Writable, so it can be bound to form inputs. */
|
|
12
11
|
input: string;
|
|
13
|
-
|
|
14
|
-
* Current messages in the chat.
|
|
15
|
-
*
|
|
16
|
-
* This is writable, which is useful when you want to edit the messages on the client, and then
|
|
17
|
-
* trigger {@link reload} to regenerate the AI response.
|
|
18
|
-
*/
|
|
19
|
-
get messages(): UIMessage<MESSAGE_METADATA, InferUIDataParts<DATA_PART_SCHEMAS>>[];
|
|
20
|
-
set messages(messages: UIMessage<MESSAGE_METADATA, InferUIDataParts<DATA_PART_SCHEMAS>>[]);
|
|
21
|
-
/**
|
|
22
|
-
* Hook status:
|
|
23
|
-
*
|
|
24
|
-
* - `submitted`: The message has been sent to the API and we're awaiting the start of the response stream.
|
|
25
|
-
* - `streaming`: The response is actively streaming in from the API, receiving chunks of data.
|
|
26
|
-
* - `ready`: The full response has been received and processed; a new user message can be submitted.
|
|
27
|
-
* - `error`: An error occurred during the API request, preventing successful completion.
|
|
28
|
-
*/
|
|
29
|
-
get status(): ChatStatus;
|
|
30
|
-
set status(value: ChatStatus);
|
|
31
|
-
/** The error object of the API request */
|
|
32
|
-
get error(): Error | undefined;
|
|
33
|
-
set error(value: Error | undefined);
|
|
34
|
-
constructor(options?: () => ChatOptions<MESSAGE_METADATA, DATA_PART_SCHEMAS>);
|
|
35
|
-
/**
|
|
36
|
-
* Append a user message to the chat list. This triggers the API call to fetch
|
|
37
|
-
* the assistant's response.
|
|
38
|
-
* @param message The message to append
|
|
39
|
-
* @param options Additional options to pass to the API call
|
|
40
|
-
*/
|
|
41
|
-
append: (message: UIMessage<MESSAGE_METADATA, InferUIDataParts<DATA_PART_SCHEMAS>> | CreateUIMessage<MESSAGE_METADATA, InferUIDataParts<DATA_PART_SCHEMAS>>, { headers, body }?: ChatRequestOptions) => Promise<void>;
|
|
42
|
-
/**
|
|
43
|
-
* Reload the last AI chat response for the given chat history. If the last
|
|
44
|
-
* message isn't from the assistant, it will request the API to generate a
|
|
45
|
-
* new response.
|
|
46
|
-
*/
|
|
47
|
-
reload: ({ headers, body }?: ChatRequestOptions) => Promise<void>;
|
|
48
|
-
/**
|
|
49
|
-
* Abort the current request immediately, keep the generated tokens if any.
|
|
50
|
-
*/
|
|
51
|
-
stop: () => void;
|
|
12
|
+
constructor(init: ChatInit<MESSAGE_METADATA, DATA_PART_SCHEMAS>);
|
|
52
13
|
/** Form submission handler to automatically reset input and append a user message */
|
|
53
14
|
handleSubmit: (event?: {
|
|
54
15
|
preventDefault?: () => void;
|
|
55
16
|
}, options?: ChatRequestOptions & {
|
|
56
17
|
files?: FileList;
|
|
57
18
|
}) => Promise<void>;
|
|
58
|
-
addToolResult: ({ toolCallId, result, }: {
|
|
59
|
-
toolCallId: string;
|
|
60
|
-
result: unknown;
|
|
61
|
-
}) => Promise<void>;
|
|
62
19
|
}
|
|
63
20
|
//# sourceMappingURL=chat.svelte.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat.svelte.d.ts","sourceRoot":"","sources":["../src/chat.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"chat.svelte.d.ts","sourceRoot":"","sources":["../src/chat.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,kBAAkB,EAGvB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EAEtB,KAAK,SAAS,EAEf,MAAM,IAAI,CAAC;AAEZ,MAAM,MAAM,QAAQ,CAClB,gBAAgB,GAAG,OAAO,EAC1B,iBAAiB,SAAS,iBAAiB,GAAG,iBAAiB,IAC7D,QAAQ,CAAC,YAAY,CAAC,gBAAgB,EAAE,iBAAiB,CAAC,CAAC,GAAG;IAChE;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,YAAY,EAAE,eAAe,EAAE,SAAS,EAAE,CAAC;AAE3C,qBAAa,IAAI,CACf,gBAAgB,GAAG,OAAO,EAC1B,iBAAiB,SAAS,iBAAiB,GAAG,iBAAiB,CAC/D,SAAQ,YAAY,CAAC,gBAAgB,EAAE,iBAAiB,CAAC;IACzD,mFAAmF;IACnF,KAAK,EAAE,MAAM,CAAC;gBAEF,IAAI,EAAE,QAAQ,CAAC,gBAAgB,EAAE,iBAAiB,CAAC;IAS/D,qFAAqF;IACrF,YAAY,WACF;QAAE,cAAc,CAAC,EAAE,MAAM,IAAI,CAAA;KAAE,YAC9B,kBAAkB,GAAG;QAAE,KAAK,CAAC,EAAE,QAAQ,CAAA;KAAE,mBAwBlD;CACH"}
|
package/dist/chat.svelte.js
CHANGED
|
@@ -1,118 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export class Chat {
|
|
4
|
-
#options;
|
|
5
|
-
#generateId;
|
|
6
|
-
#chatStore;
|
|
7
|
-
/**
|
|
8
|
-
* The id of the chat. If not provided through the constructor, a random ID will be generated
|
|
9
|
-
* using the provided `generateId` function, or a built-in function if not provided.
|
|
10
|
-
*/
|
|
11
|
-
chatId;
|
|
1
|
+
import { AbstractChat, convertFileListToFileUIParts, } from 'ai';
|
|
2
|
+
export class Chat extends AbstractChat {
|
|
12
3
|
/** The current value of the input. Writable, so it can be bound to form inputs. */
|
|
13
|
-
input
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
* trigger {@link reload} to regenerate the AI response.
|
|
19
|
-
*/
|
|
20
|
-
get messages() {
|
|
21
|
-
return this.#chatStore.getMessages(this.chatId);
|
|
22
|
-
}
|
|
23
|
-
set messages(messages) {
|
|
24
|
-
this.#chatStore.setMessages({ id: this.chatId, messages });
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Hook status:
|
|
28
|
-
*
|
|
29
|
-
* - `submitted`: The message has been sent to the API and we're awaiting the start of the response stream.
|
|
30
|
-
* - `streaming`: The response is actively streaming in from the API, receiving chunks of data.
|
|
31
|
-
* - `ready`: The full response has been received and processed; a new user message can be submitted.
|
|
32
|
-
* - `error`: An error occurred during the API request, preventing successful completion.
|
|
33
|
-
*/
|
|
34
|
-
get status() {
|
|
35
|
-
return this.#chatStore.getStatus(this.chatId);
|
|
36
|
-
}
|
|
37
|
-
set status(value) {
|
|
38
|
-
this.#chatStore.setStatus({ id: this.chatId, status: value });
|
|
39
|
-
}
|
|
40
|
-
/** The error object of the API request */
|
|
41
|
-
get error() {
|
|
42
|
-
return this.#chatStore.getError(this.chatId);
|
|
43
|
-
}
|
|
44
|
-
set error(value) {
|
|
45
|
-
this.#chatStore.setStatus({
|
|
46
|
-
id: this.chatId,
|
|
47
|
-
status: 'error',
|
|
48
|
-
error: value,
|
|
4
|
+
input;
|
|
5
|
+
constructor(init) {
|
|
6
|
+
super({
|
|
7
|
+
...init,
|
|
8
|
+
state: new SvelteChatState(init.messages),
|
|
49
9
|
});
|
|
10
|
+
this.input = $state(init.initialInput ?? '');
|
|
50
11
|
}
|
|
51
|
-
constructor(options = () => ({})) {
|
|
52
|
-
this.#options = $derived.by(options);
|
|
53
|
-
this.#generateId = $derived(this.#options.generateId ?? generateId);
|
|
54
|
-
this.chatId = $derived(this.#options.chatId ?? this.#generateId());
|
|
55
|
-
if (this.#options.chatStore) {
|
|
56
|
-
if (typeof this.#options.chatStore === 'function') {
|
|
57
|
-
this.#chatStore = createChatStore(this.#options.chatStore());
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
this.#chatStore = this.#options.chatStore;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
else if (hasChatStoreContext()) {
|
|
64
|
-
this.#chatStore = getChatStoreContext();
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
this.#chatStore = createChatStore(defaultChatStoreOptions({
|
|
68
|
-
api: '/api/chat',
|
|
69
|
-
generateId: this.#options.generateId || generateId,
|
|
70
|
-
})());
|
|
71
|
-
}
|
|
72
|
-
this.input = this.#options.initialInput ?? '';
|
|
73
|
-
if (!this.#chatStore.hasChat(this.chatId)) {
|
|
74
|
-
const messages = $state([]);
|
|
75
|
-
this.#chatStore.addChat(this.chatId, messages);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Append a user message to the chat list. This triggers the API call to fetch
|
|
80
|
-
* the assistant's response.
|
|
81
|
-
* @param message The message to append
|
|
82
|
-
* @param options Additional options to pass to the API call
|
|
83
|
-
*/
|
|
84
|
-
append = async (message, { headers, body } = {}) => {
|
|
85
|
-
await this.#chatStore.submitMessage({
|
|
86
|
-
chatId: this.chatId,
|
|
87
|
-
message,
|
|
88
|
-
headers,
|
|
89
|
-
body,
|
|
90
|
-
onError: this.#options.onError,
|
|
91
|
-
onToolCall: this.#options.onToolCall,
|
|
92
|
-
onFinish: this.#options.onFinish,
|
|
93
|
-
});
|
|
94
|
-
};
|
|
95
|
-
/**
|
|
96
|
-
* Reload the last AI chat response for the given chat history. If the last
|
|
97
|
-
* message isn't from the assistant, it will request the API to generate a
|
|
98
|
-
* new response.
|
|
99
|
-
*/
|
|
100
|
-
reload = async ({ headers, body } = {}) => {
|
|
101
|
-
await this.#chatStore.resubmitLastUserMessage({
|
|
102
|
-
chatId: this.chatId,
|
|
103
|
-
headers,
|
|
104
|
-
body,
|
|
105
|
-
onError: this.#options.onError,
|
|
106
|
-
onToolCall: this.#options.onToolCall,
|
|
107
|
-
onFinish: this.#options.onFinish,
|
|
108
|
-
});
|
|
109
|
-
};
|
|
110
|
-
/**
|
|
111
|
-
* Abort the current request immediately, keep the generated tokens if any.
|
|
112
|
-
*/
|
|
113
|
-
stop = () => {
|
|
114
|
-
this.#chatStore.stopStream({ chatId: this.chatId });
|
|
115
|
-
};
|
|
116
12
|
/** Form submission handler to automatically reset input and append a user message */
|
|
117
13
|
handleSubmit = async (event, options = {}) => {
|
|
118
14
|
event?.preventDefault?.();
|
|
@@ -122,7 +18,7 @@ export class Chat {
|
|
|
122
18
|
if (!this.input && fileParts.length === 0)
|
|
123
19
|
return;
|
|
124
20
|
const request = this.append({
|
|
125
|
-
id: this
|
|
21
|
+
id: this.generateId(),
|
|
126
22
|
role: 'user',
|
|
127
23
|
parts: [...fileParts, { type: 'text', text: this.input }],
|
|
128
24
|
}, {
|
|
@@ -132,11 +28,25 @@ export class Chat {
|
|
|
132
28
|
this.input = '';
|
|
133
29
|
await request;
|
|
134
30
|
};
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
31
|
+
}
|
|
32
|
+
class SvelteChatState {
|
|
33
|
+
messages;
|
|
34
|
+
status = $state('ready');
|
|
35
|
+
error = $state(undefined);
|
|
36
|
+
constructor(messages = []) {
|
|
37
|
+
this.messages = $state(messages);
|
|
38
|
+
}
|
|
39
|
+
setMessages = (messages) => {
|
|
40
|
+
this.messages = messages;
|
|
41
|
+
};
|
|
42
|
+
pushMessage = (message) => {
|
|
43
|
+
this.messages.push(message);
|
|
44
|
+
};
|
|
45
|
+
popMessage = () => {
|
|
46
|
+
this.messages.pop();
|
|
47
|
+
};
|
|
48
|
+
replaceMessage = (index, message) => {
|
|
49
|
+
this.messages[index] = message;
|
|
141
50
|
};
|
|
51
|
+
snapshot = (thing) => $state.snapshot(thing);
|
|
142
52
|
}
|
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function createAIContext(chatStore?: ChatStore): void;
|
|
3
|
-
export declare function createChatStoreContext(chatStore?: ChatStore): void;
|
|
1
|
+
export declare function createAIContext(): void;
|
|
4
2
|
//# sourceMappingURL=context-provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-provider.d.ts","sourceRoot":"","sources":["../src/context-provider.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"context-provider.d.ts","sourceRoot":"","sources":["../src/context-provider.ts"],"names":[],"mappings":"AASA,wBAAgB,eAAe,SAM9B"}
|
package/dist/context-provider.js
CHANGED
|
@@ -1,17 +1,8 @@
|
|
|
1
|
-
import { defaultChatStoreOptions } from 'ai';
|
|
2
|
-
import { createChatStore, setChatStoreContext } from './chat-store.svelte.js';
|
|
3
1
|
import { KeyedCompletionStore, setCompletionContext, } from './completion-context.svelte.js';
|
|
4
2
|
import { KeyedStructuredObjectStore, setStructuredObjectContext, } from './structured-object-context.svelte.js';
|
|
5
|
-
export function createAIContext(
|
|
6
|
-
createChatStoreContext(chatStore);
|
|
3
|
+
export function createAIContext() {
|
|
7
4
|
const completionStore = new KeyedCompletionStore();
|
|
8
5
|
setCompletionContext(completionStore);
|
|
9
6
|
const objectStore = new KeyedStructuredObjectStore();
|
|
10
7
|
setStructuredObjectContext(objectStore);
|
|
11
8
|
}
|
|
12
|
-
export function createChatStoreContext(chatStore) {
|
|
13
|
-
setChatStoreContext(chatStore ??
|
|
14
|
-
createChatStore(defaultChatStoreOptions({
|
|
15
|
-
api: '/api/chat',
|
|
16
|
-
})()));
|
|
17
|
-
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export { Chat, type
|
|
2
|
-
export { StructuredObject as Experimental_StructuredObject, type Experimental_StructuredObjectOptions, } from './structured-object.svelte.js';
|
|
1
|
+
export { Chat, type CreateUIMessage, type UIMessage } from './chat.svelte.js';
|
|
3
2
|
export { Completion, type CompletionOptions } from './completion.svelte.js';
|
|
4
3
|
export { createAIContext } from './context-provider.js';
|
|
5
|
-
export {
|
|
4
|
+
export { StructuredObject as Experimental_StructuredObject, type Experimental_StructuredObjectOptions, } from './structured-object.svelte.js';
|
|
6
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,eAAe,EAAE,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC9E,OAAO,EAAE,UAAU,EAAE,KAAK,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC5E,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EACL,gBAAgB,IAAI,6BAA6B,EACjD,KAAK,oCAAoC,GAC1C,MAAM,+BAA+B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
export { Chat
|
|
2
|
-
export { StructuredObject as Experimental_StructuredObject, } from './structured-object.svelte.js';
|
|
1
|
+
export { Chat } from './chat.svelte.js';
|
|
3
2
|
export { Completion } from './completion.svelte.js';
|
|
4
3
|
export { createAIContext } from './context-provider.js';
|
|
5
|
-
export {
|
|
4
|
+
export { StructuredObject as Experimental_StructuredObject, } from './structured-object.svelte.js';
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { type FetchFunction } from '@ai-sdk/provider-utils';
|
|
1
|
+
import { type FetchFunction, type InferSchema } from '@ai-sdk/provider-utils';
|
|
2
2
|
import { type DeepPartial, type Schema } from 'ai';
|
|
3
|
-
import
|
|
4
|
-
|
|
3
|
+
import type * as z3 from 'zod/v3';
|
|
4
|
+
import type * as z4 from 'zod/v4/core';
|
|
5
|
+
export type Experimental_StructuredObjectOptions<SCHEMA extends z3.Schema | z4.$ZodType | Schema, RESULT = InferSchema<SCHEMA>> = {
|
|
5
6
|
/**
|
|
6
7
|
* The API endpoint. It should stream JSON that matches the schema as chunked text.
|
|
7
8
|
*/
|
|
@@ -9,7 +10,7 @@ export type Experimental_StructuredObjectOptions<RESULT> = {
|
|
|
9
10
|
/**
|
|
10
11
|
* A Zod schema that defines the shape of the complete object.
|
|
11
12
|
*/
|
|
12
|
-
schema:
|
|
13
|
+
schema: SCHEMA;
|
|
13
14
|
/**
|
|
14
15
|
* An unique identifier. If not provided, a random one will be
|
|
15
16
|
* generated. When provided, the `useObject` hook with the same `id` will
|
|
@@ -54,7 +55,7 @@ export type Experimental_StructuredObjectOptions<RESULT> = {
|
|
|
54
55
|
*/
|
|
55
56
|
credentials?: RequestCredentials;
|
|
56
57
|
};
|
|
57
|
-
export declare class StructuredObject<
|
|
58
|
+
export declare class StructuredObject<SCHEMA extends z3.Schema | z4.$ZodType | Schema, RESULT = InferSchema<SCHEMA>, INPUT = unknown> {
|
|
58
59
|
#private;
|
|
59
60
|
/**
|
|
60
61
|
* The current value for the generated object. Updated as the API streams JSON chunks.
|
|
@@ -66,7 +67,7 @@ export declare class StructuredObject<RESULT, INPUT = unknown> {
|
|
|
66
67
|
* Flag that indicates whether an API request is in progress.
|
|
67
68
|
*/
|
|
68
69
|
get loading(): boolean;
|
|
69
|
-
constructor(options: Experimental_StructuredObjectOptions<RESULT>);
|
|
70
|
+
constructor(options: Experimental_StructuredObjectOptions<SCHEMA, RESULT>);
|
|
70
71
|
/**
|
|
71
72
|
* Abort the current request immediately, keep the current partial object if any.
|
|
72
73
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"structured-object.svelte.d.ts","sourceRoot":"","sources":["../src/structured-object.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,aAAa,
|
|
1
|
+
{"version":3,"file":"structured-object.svelte.d.ts","sourceRoot":"","sources":["../src/structured-object.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,aAAa,EAClB,KAAK,WAAW,EACjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAIL,KAAK,WAAW,EAChB,KAAK,MAAM,EACZ,MAAM,IAAI,CAAC;AACZ,OAAO,KAAK,KAAK,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,KAAK,KAAK,EAAE,MAAM,aAAa,CAAC;AAQvC,MAAM,MAAM,oCAAoC,CAC9C,MAAM,SAAS,EAAE,CAAC,MAAM,GAAG,EAAE,CAAC,QAAQ,GAAG,MAAM,EAC/C,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,IAC1B;IACF;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;;;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;IAE3C;;;;OAIG;IACH,WAAW,CAAC,EAAE,kBAAkB,CAAC;CAClC,CAAC;AAEF,qBAAa,gBAAgB,CAC3B,MAAM,SAAS,EAAE,CAAC,MAAM,GAAG,EAAE,CAAC,QAAQ,GAAG,MAAM,EAC/C,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,EAC5B,KAAK,GAAG,OAAO;;IAWf;;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,EAAE,MAAM,CAAC;IAUzE;;OAEG;IACH,IAAI,aASF;IAEF;;OAEG;IACH,MAAM,UAAiB,KAAK,mBAqF1B;CACH"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { generateId, isAbortError, safeValidateTypes, } from '@ai-sdk/provider-utils';
|
|
2
2
|
import { asSchema, isDeepEqualData, parsePartialJson, } from 'ai';
|
|
3
|
-
import {} from 'zod';
|
|
4
3
|
import { getStructuredObjectContext, hasStructuredObjectContext, KeyedStructuredObjectStore, } from './structured-object-context.svelte.js';
|
|
5
4
|
export class StructuredObject {
|
|
6
5
|
#options = {};
|
|
@@ -11,8 +11,8 @@ declare class __sveltets_Render<RESULT> {
|
|
|
11
11
|
slots(): {};
|
|
12
12
|
bindings(): "";
|
|
13
13
|
exports(): {
|
|
14
|
-
object1: StructuredObject<RESULT, unknown>;
|
|
15
|
-
object2: StructuredObject<RESULT, unknown>;
|
|
14
|
+
object1: StructuredObject<z.ZodType<RESULT, z.ZodTypeDef, unknown> | Schema<RESULT>, RESULT, unknown>;
|
|
15
|
+
object2: StructuredObject<z.ZodType<RESULT, z.ZodTypeDef, unknown> | Schema<RESULT>, RESULT, unknown>;
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
18
|
interface $$IsomorphicComponent {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/svelte",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.9",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"svelte": "^5.31.0",
|
|
31
|
-
"zod": "^3.
|
|
31
|
+
"zod": "^3.25.49"
|
|
32
32
|
},
|
|
33
33
|
"peerDependenciesMeta": {
|
|
34
34
|
"zod": {
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@ai-sdk/provider-utils": "3.0.0-alpha.
|
|
40
|
-
"ai": "5.0.0-alpha.
|
|
39
|
+
"@ai-sdk/provider-utils": "3.0.0-alpha.9",
|
|
40
|
+
"ai": "5.0.0-alpha.9"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "20.17.24",
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"typescript-eslint": "^8.20.0",
|
|
59
59
|
"vite": "^6.0.0",
|
|
60
60
|
"vitest": "^3.0.0",
|
|
61
|
-
"zod": "3.
|
|
61
|
+
"zod": "3.25.49",
|
|
62
62
|
"@vercel/ai-tsconfig": "0.0.0"
|
|
63
63
|
},
|
|
64
64
|
"homepage": "https://ai-sdk.dev/docs",
|
package/src/chat.svelte.ts
CHANGED
|
@@ -1,178 +1,44 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
defaultChatStoreOptions,
|
|
5
|
-
generateId,
|
|
2
|
+
AbstractChat,
|
|
3
|
+
type BaseChatInit,
|
|
6
4
|
type ChatRequestOptions,
|
|
5
|
+
type ChatState,
|
|
7
6
|
type ChatStatus,
|
|
8
7
|
type CreateUIMessage,
|
|
9
|
-
type IdGenerator,
|
|
10
|
-
type InferUIDataParts,
|
|
11
8
|
type UIDataPartSchemas,
|
|
9
|
+
type UIDataTypes,
|
|
12
10
|
type UIMessage,
|
|
13
|
-
|
|
11
|
+
convertFileListToFileUIParts,
|
|
14
12
|
} from 'ai';
|
|
15
|
-
import {
|
|
16
|
-
getChatStoreContext,
|
|
17
|
-
hasChatStoreContext,
|
|
18
|
-
createChatStore,
|
|
19
|
-
} from './chat-store.svelte.js';
|
|
20
13
|
|
|
21
|
-
export type
|
|
14
|
+
export type ChatInit<
|
|
22
15
|
MESSAGE_METADATA = unknown,
|
|
23
16
|
DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas,
|
|
24
|
-
> = Readonly<
|
|
17
|
+
> = Readonly<BaseChatInit<MESSAGE_METADATA, DATA_PART_SCHEMAS>> & {
|
|
18
|
+
/**
|
|
19
|
+
* Initial input of the chat.
|
|
20
|
+
*/
|
|
21
|
+
initialInput?: string;
|
|
22
|
+
};
|
|
25
23
|
|
|
26
24
|
export type { CreateUIMessage, UIMessage };
|
|
27
25
|
|
|
28
26
|
export class Chat<
|
|
29
27
|
MESSAGE_METADATA = unknown,
|
|
30
28
|
DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas,
|
|
31
|
-
> {
|
|
32
|
-
readonly #options: ChatOptions<MESSAGE_METADATA, DATA_PART_SCHEMAS>;
|
|
33
|
-
readonly #generateId: IdGenerator;
|
|
34
|
-
readonly #chatStore: ChatStore<MESSAGE_METADATA, DATA_PART_SCHEMAS>;
|
|
35
|
-
/**
|
|
36
|
-
* The id of the chat. If not provided through the constructor, a random ID will be generated
|
|
37
|
-
* using the provided `generateId` function, or a built-in function if not provided.
|
|
38
|
-
*/
|
|
39
|
-
readonly chatId: string;
|
|
40
|
-
|
|
29
|
+
> extends AbstractChat<MESSAGE_METADATA, DATA_PART_SCHEMAS> {
|
|
41
30
|
/** The current value of the input. Writable, so it can be bound to form inputs. */
|
|
42
|
-
input
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Current messages in the chat.
|
|
46
|
-
*
|
|
47
|
-
* This is writable, which is useful when you want to edit the messages on the client, and then
|
|
48
|
-
* trigger {@link reload} to regenerate the AI response.
|
|
49
|
-
*/
|
|
50
|
-
get messages(): UIMessage<
|
|
51
|
-
MESSAGE_METADATA,
|
|
52
|
-
InferUIDataParts<DATA_PART_SCHEMAS>
|
|
53
|
-
>[] {
|
|
54
|
-
return this.#chatStore.getMessages(this.chatId);
|
|
55
|
-
}
|
|
56
|
-
set messages(
|
|
57
|
-
messages: UIMessage<
|
|
58
|
-
MESSAGE_METADATA,
|
|
59
|
-
InferUIDataParts<DATA_PART_SCHEMAS>
|
|
60
|
-
>[],
|
|
61
|
-
) {
|
|
62
|
-
this.#chatStore.setMessages({ id: this.chatId, messages });
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Hook status:
|
|
67
|
-
*
|
|
68
|
-
* - `submitted`: The message has been sent to the API and we're awaiting the start of the response stream.
|
|
69
|
-
* - `streaming`: The response is actively streaming in from the API, receiving chunks of data.
|
|
70
|
-
* - `ready`: The full response has been received and processed; a new user message can be submitted.
|
|
71
|
-
* - `error`: An error occurred during the API request, preventing successful completion.
|
|
72
|
-
*/
|
|
73
|
-
get status(): ChatStatus {
|
|
74
|
-
return this.#chatStore.getStatus(this.chatId);
|
|
75
|
-
}
|
|
76
|
-
set status(value: ChatStatus) {
|
|
77
|
-
this.#chatStore.setStatus({ id: this.chatId, status: value });
|
|
78
|
-
}
|
|
31
|
+
input: string;
|
|
79
32
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
set error(value: Error | undefined) {
|
|
85
|
-
this.#chatStore.setStatus({
|
|
86
|
-
id: this.chatId,
|
|
87
|
-
status: 'error',
|
|
88
|
-
error: value,
|
|
33
|
+
constructor(init: ChatInit<MESSAGE_METADATA, DATA_PART_SCHEMAS>) {
|
|
34
|
+
super({
|
|
35
|
+
...init,
|
|
36
|
+
state: new SvelteChatState(init.messages),
|
|
89
37
|
});
|
|
90
|
-
}
|
|
91
38
|
|
|
92
|
-
|
|
93
|
-
options: () => ChatOptions<
|
|
94
|
-
MESSAGE_METADATA,
|
|
95
|
-
DATA_PART_SCHEMAS
|
|
96
|
-
> = () => ({}),
|
|
97
|
-
) {
|
|
98
|
-
this.#options = $derived.by(options);
|
|
99
|
-
this.#generateId = $derived(this.#options.generateId ?? generateId);
|
|
100
|
-
this.chatId = $derived(this.#options.chatId ?? this.#generateId());
|
|
101
|
-
|
|
102
|
-
if (this.#options.chatStore) {
|
|
103
|
-
if (typeof this.#options.chatStore === 'function') {
|
|
104
|
-
this.#chatStore = createChatStore(this.#options.chatStore());
|
|
105
|
-
} else {
|
|
106
|
-
this.#chatStore = this.#options.chatStore;
|
|
107
|
-
}
|
|
108
|
-
} else if (hasChatStoreContext()) {
|
|
109
|
-
this.#chatStore = getChatStoreContext() as ChatStore<
|
|
110
|
-
MESSAGE_METADATA,
|
|
111
|
-
DATA_PART_SCHEMAS
|
|
112
|
-
>;
|
|
113
|
-
} else {
|
|
114
|
-
this.#chatStore = createChatStore(
|
|
115
|
-
defaultChatStoreOptions<MESSAGE_METADATA, DATA_PART_SCHEMAS>({
|
|
116
|
-
api: '/api/chat',
|
|
117
|
-
generateId: this.#options.generateId || generateId,
|
|
118
|
-
})(),
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
this.input = this.#options.initialInput ?? '';
|
|
123
|
-
|
|
124
|
-
if (!this.#chatStore.hasChat(this.chatId)) {
|
|
125
|
-
const messages = $state([]);
|
|
126
|
-
this.#chatStore.addChat(this.chatId, messages);
|
|
127
|
-
}
|
|
39
|
+
this.input = $state(init.initialInput ?? '');
|
|
128
40
|
}
|
|
129
41
|
|
|
130
|
-
/**
|
|
131
|
-
* Append a user message to the chat list. This triggers the API call to fetch
|
|
132
|
-
* the assistant's response.
|
|
133
|
-
* @param message The message to append
|
|
134
|
-
* @param options Additional options to pass to the API call
|
|
135
|
-
*/
|
|
136
|
-
append = async (
|
|
137
|
-
message:
|
|
138
|
-
| UIMessage<MESSAGE_METADATA, InferUIDataParts<DATA_PART_SCHEMAS>>
|
|
139
|
-
| CreateUIMessage<MESSAGE_METADATA, InferUIDataParts<DATA_PART_SCHEMAS>>,
|
|
140
|
-
{ headers, body }: ChatRequestOptions = {},
|
|
141
|
-
) => {
|
|
142
|
-
await this.#chatStore.submitMessage({
|
|
143
|
-
chatId: this.chatId,
|
|
144
|
-
message,
|
|
145
|
-
headers,
|
|
146
|
-
body,
|
|
147
|
-
onError: this.#options.onError,
|
|
148
|
-
onToolCall: this.#options.onToolCall,
|
|
149
|
-
onFinish: this.#options.onFinish,
|
|
150
|
-
});
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Reload the last AI chat response for the given chat history. If the last
|
|
155
|
-
* message isn't from the assistant, it will request the API to generate a
|
|
156
|
-
* new response.
|
|
157
|
-
*/
|
|
158
|
-
reload = async ({ headers, body }: ChatRequestOptions = {}) => {
|
|
159
|
-
await this.#chatStore.resubmitLastUserMessage({
|
|
160
|
-
chatId: this.chatId,
|
|
161
|
-
headers,
|
|
162
|
-
body,
|
|
163
|
-
onError: this.#options.onError,
|
|
164
|
-
onToolCall: this.#options.onToolCall,
|
|
165
|
-
onFinish: this.#options.onFinish,
|
|
166
|
-
});
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Abort the current request immediately, keep the generated tokens if any.
|
|
171
|
-
*/
|
|
172
|
-
stop = () => {
|
|
173
|
-
this.#chatStore.stopStream({ chatId: this.chatId });
|
|
174
|
-
};
|
|
175
|
-
|
|
176
42
|
/** Form submission handler to automatically reset input and append a user message */
|
|
177
43
|
handleSubmit = async (
|
|
178
44
|
event?: { preventDefault?: () => void },
|
|
@@ -188,7 +54,7 @@ export class Chat<
|
|
|
188
54
|
|
|
189
55
|
const request = this.append(
|
|
190
56
|
{
|
|
191
|
-
id: this
|
|
57
|
+
id: this.generateId(),
|
|
192
58
|
role: 'user',
|
|
193
59
|
parts: [...fileParts, { type: 'text', text: this.input }],
|
|
194
60
|
},
|
|
@@ -201,18 +67,37 @@ export class Chat<
|
|
|
201
67
|
this.input = '';
|
|
202
68
|
await request;
|
|
203
69
|
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
class SvelteChatState<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes>
|
|
73
|
+
implements ChatState<MESSAGE_METADATA, DATA_TYPES>
|
|
74
|
+
{
|
|
75
|
+
messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[];
|
|
76
|
+
status = $state<ChatStatus>('ready');
|
|
77
|
+
error = $state<Error | undefined>(undefined);
|
|
78
|
+
|
|
79
|
+
constructor(messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[] = []) {
|
|
80
|
+
this.messages = $state(messages);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
setMessages = (messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[]) => {
|
|
84
|
+
this.messages = messages;
|
|
85
|
+
};
|
|
204
86
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
result,
|
|
208
|
-
}: {
|
|
209
|
-
toolCallId: string;
|
|
210
|
-
result: unknown;
|
|
211
|
-
}) => {
|
|
212
|
-
await this.#chatStore.addToolResult({
|
|
213
|
-
chatId: this.chatId,
|
|
214
|
-
toolCallId,
|
|
215
|
-
result,
|
|
216
|
-
});
|
|
87
|
+
pushMessage = (message: UIMessage<MESSAGE_METADATA, DATA_TYPES>) => {
|
|
88
|
+
this.messages.push(message);
|
|
217
89
|
};
|
|
90
|
+
|
|
91
|
+
popMessage = () => {
|
|
92
|
+
this.messages.pop();
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
replaceMessage = (
|
|
96
|
+
index: number,
|
|
97
|
+
message: UIMessage<MESSAGE_METADATA, DATA_TYPES>,
|
|
98
|
+
) => {
|
|
99
|
+
this.messages[index] = message;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
snapshot = <T>(thing: T): T => $state.snapshot(thing) as T;
|
|
218
103
|
}
|
package/src/context-provider.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { defaultChatStoreOptions, type ChatStore } from 'ai';
|
|
2
|
-
import { createChatStore, setChatStoreContext } from './chat-store.svelte.js';
|
|
3
1
|
import {
|
|
4
2
|
KeyedCompletionStore,
|
|
5
3
|
setCompletionContext,
|
|
@@ -9,23 +7,10 @@ import {
|
|
|
9
7
|
setStructuredObjectContext,
|
|
10
8
|
} from './structured-object-context.svelte.js';
|
|
11
9
|
|
|
12
|
-
export function createAIContext(
|
|
13
|
-
createChatStoreContext(chatStore);
|
|
14
|
-
|
|
10
|
+
export function createAIContext() {
|
|
15
11
|
const completionStore = new KeyedCompletionStore();
|
|
16
12
|
setCompletionContext(completionStore);
|
|
17
13
|
|
|
18
14
|
const objectStore = new KeyedStructuredObjectStore();
|
|
19
15
|
setStructuredObjectContext(objectStore);
|
|
20
16
|
}
|
|
21
|
-
|
|
22
|
-
export function createChatStoreContext(chatStore?: ChatStore) {
|
|
23
|
-
setChatStoreContext(
|
|
24
|
-
chatStore ??
|
|
25
|
-
createChatStore(
|
|
26
|
-
defaultChatStoreOptions({
|
|
27
|
-
api: '/api/chat',
|
|
28
|
-
})(),
|
|
29
|
-
),
|
|
30
|
-
);
|
|
31
|
-
}
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,7 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type CreateUIMessage,
|
|
5
|
-
type UIMessage,
|
|
6
|
-
} from './chat.svelte.js';
|
|
7
|
-
|
|
1
|
+
export { Chat, type CreateUIMessage, type UIMessage } from './chat.svelte.js';
|
|
2
|
+
export { Completion, type CompletionOptions } from './completion.svelte.js';
|
|
3
|
+
export { createAIContext } from './context-provider.js';
|
|
8
4
|
export {
|
|
9
5
|
StructuredObject as Experimental_StructuredObject,
|
|
10
6
|
type Experimental_StructuredObjectOptions,
|
|
11
7
|
} from './structured-object.svelte.js';
|
|
12
|
-
|
|
13
|
-
export { Completion, type CompletionOptions } from './completion.svelte.js';
|
|
14
|
-
|
|
15
|
-
export { createAIContext } from './context-provider.js';
|
|
16
|
-
|
|
17
|
-
export { createChatStore } from './chat-store.svelte.js';
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
isAbortError,
|
|
4
4
|
safeValidateTypes,
|
|
5
5
|
type FetchFunction,
|
|
6
|
+
type InferSchema,
|
|
6
7
|
} from '@ai-sdk/provider-utils';
|
|
7
8
|
import {
|
|
8
9
|
asSchema,
|
|
@@ -11,7 +12,8 @@ import {
|
|
|
11
12
|
type DeepPartial,
|
|
12
13
|
type Schema,
|
|
13
14
|
} from 'ai';
|
|
14
|
-
import
|
|
15
|
+
import type * as z3 from 'zod/v3';
|
|
16
|
+
import type * as z4 from 'zod/v4/core';
|
|
15
17
|
import {
|
|
16
18
|
getStructuredObjectContext,
|
|
17
19
|
hasStructuredObjectContext,
|
|
@@ -19,7 +21,10 @@ import {
|
|
|
19
21
|
type StructuredObjectStore,
|
|
20
22
|
} from './structured-object-context.svelte.js';
|
|
21
23
|
|
|
22
|
-
export type Experimental_StructuredObjectOptions<
|
|
24
|
+
export type Experimental_StructuredObjectOptions<
|
|
25
|
+
SCHEMA extends z3.Schema | z4.$ZodType | Schema,
|
|
26
|
+
RESULT = InferSchema<SCHEMA>,
|
|
27
|
+
> = {
|
|
23
28
|
/**
|
|
24
29
|
* The API endpoint. It should stream JSON that matches the schema as chunked text.
|
|
25
30
|
*/
|
|
@@ -28,7 +33,7 @@ export type Experimental_StructuredObjectOptions<RESULT> = {
|
|
|
28
33
|
/**
|
|
29
34
|
* A Zod schema that defines the shape of the complete object.
|
|
30
35
|
*/
|
|
31
|
-
schema:
|
|
36
|
+
schema: SCHEMA;
|
|
32
37
|
|
|
33
38
|
/**
|
|
34
39
|
* An unique identifier. If not provided, a random one will be
|
|
@@ -82,9 +87,13 @@ export type Experimental_StructuredObjectOptions<RESULT> = {
|
|
|
82
87
|
credentials?: RequestCredentials;
|
|
83
88
|
};
|
|
84
89
|
|
|
85
|
-
export class StructuredObject<
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
export class StructuredObject<
|
|
91
|
+
SCHEMA extends z3.Schema | z4.$ZodType | Schema,
|
|
92
|
+
RESULT = InferSchema<SCHEMA>,
|
|
93
|
+
INPUT = unknown,
|
|
94
|
+
> {
|
|
95
|
+
#options: Experimental_StructuredObjectOptions<SCHEMA, RESULT> =
|
|
96
|
+
{} as Experimental_StructuredObjectOptions<SCHEMA, RESULT>;
|
|
88
97
|
readonly #id = $derived(this.#options.id ?? generateId());
|
|
89
98
|
readonly #keyedStore = $state<KeyedStructuredObjectStore>()!;
|
|
90
99
|
readonly #store = $derived(
|
|
@@ -114,7 +123,7 @@ export class StructuredObject<RESULT, INPUT = unknown> {
|
|
|
114
123
|
return this.#store.loading;
|
|
115
124
|
}
|
|
116
125
|
|
|
117
|
-
constructor(options: Experimental_StructuredObjectOptions<RESULT>) {
|
|
126
|
+
constructor(options: Experimental_StructuredObjectOptions<SCHEMA, RESULT>) {
|
|
118
127
|
if (hasStructuredObjectContext()) {
|
|
119
128
|
this.#keyedStore = getStructuredObjectContext();
|
|
120
129
|
} else {
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { ChatStore, type ChatStoreOptions, type UIDataPartSchemas } from 'ai';
|
|
2
|
-
export declare const hasChatStoreContext: () => boolean, getChatStoreContext: () => ChatStore<unknown, UIDataPartSchemas>, setChatStoreContext: (value: ChatStore<unknown, UIDataPartSchemas>) => ChatStore<unknown, UIDataPartSchemas>;
|
|
3
|
-
export declare function createChatStore<MESSAGE_METADATA = unknown, DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas>(options: ChatStoreOptions<MESSAGE_METADATA, DATA_PART_SCHEMAS>): ChatStore<MESSAGE_METADATA, DATA_PART_SCHEMAS>;
|
|
4
|
-
//# sourceMappingURL=chat-store.svelte.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"chat-store.svelte.d.ts","sourceRoot":"","sources":["../src/chat-store.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EAKT,KAAK,gBAAgB,EAErB,KAAK,iBAAiB,EAGvB,MAAM,IAAI,CAAC;AAGZ,eAAO,MACO,mBAAmB,iBACnB,mBAAmB,+CACnB,mBAAmB,yFACQ,CAAC;AAqD1C,wBAAgB,eAAe,CAC7B,gBAAgB,GAAG,OAAO,EAC1B,iBAAiB,SAAS,iBAAiB,GAAG,iBAAiB,EAE/D,OAAO,EAAE,gBAAgB,CAAC,gBAAgB,EAAE,iBAAiB,CAAC,GAC7D,SAAS,CAAC,gBAAgB,EAAE,iBAAiB,CAAC,CAQhD"}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { ChatStore, SerialJobExecutor, } from 'ai';
|
|
2
|
-
import { createContext } from './utils.svelte.js';
|
|
3
|
-
export const { hasContext: hasChatStoreContext, getContext: getChatStoreContext, setContext: setChatStoreContext, } = createContext('ChatStore');
|
|
4
|
-
class SvelteChat {
|
|
5
|
-
messages;
|
|
6
|
-
status = $state('ready');
|
|
7
|
-
error = $state(undefined);
|
|
8
|
-
activeResponse = undefined;
|
|
9
|
-
jobExecutor = new SerialJobExecutor();
|
|
10
|
-
constructor(messages) {
|
|
11
|
-
this.messages = $state(messages ?? []);
|
|
12
|
-
}
|
|
13
|
-
setStatus = (status) => {
|
|
14
|
-
this.status = status;
|
|
15
|
-
};
|
|
16
|
-
setError = (error) => {
|
|
17
|
-
this.error = error;
|
|
18
|
-
};
|
|
19
|
-
setActiveResponse = (activeResponse) => {
|
|
20
|
-
this.activeResponse = activeResponse;
|
|
21
|
-
};
|
|
22
|
-
setMessages = (messages) => {
|
|
23
|
-
this.messages = messages;
|
|
24
|
-
};
|
|
25
|
-
pushMessage = (message) => {
|
|
26
|
-
this.messages.push(message);
|
|
27
|
-
};
|
|
28
|
-
popMessage = () => {
|
|
29
|
-
this.messages.pop();
|
|
30
|
-
};
|
|
31
|
-
replaceMessage = (index, message) => {
|
|
32
|
-
this.messages[index] = message;
|
|
33
|
-
};
|
|
34
|
-
snapshot = (thing) => {
|
|
35
|
-
return $state.snapshot(thing);
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
export function createChatStore(options) {
|
|
39
|
-
return new ChatStore({
|
|
40
|
-
...options,
|
|
41
|
-
createChat: options => new SvelteChat(options.messages),
|
|
42
|
-
});
|
|
43
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import { createChatStore } from '../chat-store.svelte.js';
|
|
3
|
-
import { Chat } from '../chat.svelte.js';
|
|
4
|
-
import { defaultChatStoreOptions } from 'ai';
|
|
5
|
-
import { createAIContext } from '../context-provider.js';
|
|
6
|
-
|
|
7
|
-
let { chatId }: { chatId?: string } = $props();
|
|
8
|
-
|
|
9
|
-
createAIContext(
|
|
10
|
-
createChatStore(defaultChatStoreOptions({ api: '/api/chat' })()),
|
|
11
|
-
);
|
|
12
|
-
const chat1 = new Chat(() => ({ chatId }));
|
|
13
|
-
const chat2 = new Chat(() => ({ chatId }));
|
|
14
|
-
|
|
15
|
-
export { chat1, chat2 };
|
|
16
|
-
</script>
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Chat } from '../chat.svelte.js';
|
|
2
|
-
type $$ComponentProps = {
|
|
3
|
-
chatId?: string;
|
|
4
|
-
};
|
|
5
|
-
declare const ChatSynchronization: import("svelte").Component<$$ComponentProps, {
|
|
6
|
-
chat1: Chat<unknown, import("ai").UIDataPartSchemas>;
|
|
7
|
-
chat2: Chat<unknown, import("ai").UIDataPartSchemas>;
|
|
8
|
-
}, "">;
|
|
9
|
-
type ChatSynchronization = ReturnType<typeof ChatSynchronization>;
|
|
10
|
-
export default ChatSynchronization;
|
|
11
|
-
//# sourceMappingURL=chat-synchronization.svelte.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"chat-synchronization.svelte.d.ts","sourceRoot":"","sources":["../../src/tests/chat-synchronization.svelte.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAIxC,KAAK,gBAAgB,GAAI;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAoB9C,QAAA,MAAM,mBAAmB;;;MAAsC,CAAC;AAChE,KAAK,mBAAmB,GAAG,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAClE,eAAe,mBAAmB,CAAC"}
|
package/src/chat-store.svelte.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ChatStore,
|
|
3
|
-
SerialJobExecutor,
|
|
4
|
-
type ActiveResponse,
|
|
5
|
-
type Chat,
|
|
6
|
-
type ChatStatus,
|
|
7
|
-
type ChatStoreOptions,
|
|
8
|
-
type InferUIDataParts,
|
|
9
|
-
type UIDataPartSchemas,
|
|
10
|
-
type UIDataTypes,
|
|
11
|
-
type UIMessage,
|
|
12
|
-
} from 'ai';
|
|
13
|
-
import { createContext } from './utils.svelte.js';
|
|
14
|
-
|
|
15
|
-
export const {
|
|
16
|
-
hasContext: hasChatStoreContext,
|
|
17
|
-
getContext: getChatStoreContext,
|
|
18
|
-
setContext: setChatStoreContext,
|
|
19
|
-
} = createContext<ChatStore>('ChatStore');
|
|
20
|
-
|
|
21
|
-
class SvelteChat<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes>
|
|
22
|
-
implements Chat<MESSAGE_METADATA, DATA_TYPES>
|
|
23
|
-
{
|
|
24
|
-
messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[];
|
|
25
|
-
status = $state<ChatStatus>('ready');
|
|
26
|
-
error = $state<Error | undefined>(undefined);
|
|
27
|
-
activeResponse: ActiveResponse<MESSAGE_METADATA> | undefined = undefined;
|
|
28
|
-
jobExecutor = new SerialJobExecutor();
|
|
29
|
-
|
|
30
|
-
constructor(messages?: UIMessage<MESSAGE_METADATA, DATA_TYPES>[]) {
|
|
31
|
-
this.messages = $state(messages ?? []);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
setStatus = (status: ChatStatus) => {
|
|
35
|
-
this.status = status;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
setError = (error: Error | undefined) => {
|
|
39
|
-
this.error = error;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
setActiveResponse = (
|
|
43
|
-
activeResponse: ActiveResponse<MESSAGE_METADATA> | undefined,
|
|
44
|
-
) => {
|
|
45
|
-
this.activeResponse = activeResponse;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
setMessages = (messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[]) => {
|
|
49
|
-
this.messages = messages;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
pushMessage = (message: UIMessage<MESSAGE_METADATA, DATA_TYPES>) => {
|
|
53
|
-
this.messages.push(message);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
popMessage = () => {
|
|
57
|
-
this.messages.pop();
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
replaceMessage = (
|
|
61
|
-
index: number,
|
|
62
|
-
message: UIMessage<MESSAGE_METADATA, DATA_TYPES>,
|
|
63
|
-
) => {
|
|
64
|
-
this.messages[index] = message;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
snapshot = <T>(thing: T): T => {
|
|
68
|
-
return $state.snapshot(thing) as T;
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function createChatStore<
|
|
73
|
-
MESSAGE_METADATA = unknown,
|
|
74
|
-
DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas,
|
|
75
|
-
>(
|
|
76
|
-
options: ChatStoreOptions<MESSAGE_METADATA, DATA_PART_SCHEMAS>,
|
|
77
|
-
): ChatStore<MESSAGE_METADATA, DATA_PART_SCHEMAS> {
|
|
78
|
-
return new ChatStore<MESSAGE_METADATA, DATA_PART_SCHEMAS>({
|
|
79
|
-
...options,
|
|
80
|
-
createChat: options =>
|
|
81
|
-
new SvelteChat<MESSAGE_METADATA, InferUIDataParts<DATA_PART_SCHEMAS>>(
|
|
82
|
-
options.messages,
|
|
83
|
-
),
|
|
84
|
-
});
|
|
85
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import { createChatStore } from '../chat-store.svelte.js';
|
|
3
|
-
import { Chat } from '../chat.svelte.js';
|
|
4
|
-
import { defaultChatStoreOptions } from 'ai';
|
|
5
|
-
import { createAIContext } from '../context-provider.js';
|
|
6
|
-
|
|
7
|
-
let { chatId }: { chatId?: string } = $props();
|
|
8
|
-
|
|
9
|
-
createAIContext(
|
|
10
|
-
createChatStore(defaultChatStoreOptions({ api: '/api/chat' })()),
|
|
11
|
-
);
|
|
12
|
-
const chat1 = new Chat(() => ({ chatId }));
|
|
13
|
-
const chat2 = new Chat(() => ({ chatId }));
|
|
14
|
-
|
|
15
|
-
export { chat1, chat2 };
|
|
16
|
-
</script>
|