@tanstack/ai-svelte 0.3.0 → 0.5.0
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/README.md +2 -2
- package/dist/create-chat.svelte.js +13 -2
- package/dist/types.d.ts +11 -5
- package/package.json +4 -4
- package/src/create-chat.svelte.ts +21 -4
- package/src/types.ts +12 -4
package/README.md
CHANGED
|
@@ -65,8 +65,8 @@ This library uses Svelte 5 runes (`$state`) internally, providing a clean API wh
|
|
|
65
65
|
|
|
66
66
|
```svelte
|
|
67
67
|
<script>
|
|
68
|
-
const chat = createChat({
|
|
69
|
-
|
|
68
|
+
const chat = createChat({})
|
|
69
|
+
|
|
70
70
|
// No $ needed - these are reactive getters!
|
|
71
71
|
console.log(chat.messages)
|
|
72
72
|
console.log(chat.isLoading)
|
|
@@ -37,6 +37,7 @@ export function createChat(options) {
|
|
|
37
37
|
let messages = $state(options.initialMessages || []);
|
|
38
38
|
let isLoading = $state(false);
|
|
39
39
|
let error = $state(undefined);
|
|
40
|
+
let status = $state('ready');
|
|
40
41
|
// Create ChatClient instance
|
|
41
42
|
const client = new ChatClient({
|
|
42
43
|
connection: options.connection,
|
|
@@ -45,8 +46,12 @@ export function createChat(options) {
|
|
|
45
46
|
body: options.body,
|
|
46
47
|
onResponse: options.onResponse,
|
|
47
48
|
onChunk: options.onChunk,
|
|
48
|
-
onFinish:
|
|
49
|
-
|
|
49
|
+
onFinish: (message) => {
|
|
50
|
+
options.onFinish?.(message);
|
|
51
|
+
},
|
|
52
|
+
onError: (err) => {
|
|
53
|
+
options.onError?.(err);
|
|
54
|
+
},
|
|
50
55
|
tools: options.tools,
|
|
51
56
|
streamProcessor: options.streamProcessor,
|
|
52
57
|
onMessagesChange: (newMessages) => {
|
|
@@ -55,6 +60,9 @@ export function createChat(options) {
|
|
|
55
60
|
onLoadingChange: (newIsLoading) => {
|
|
56
61
|
isLoading = newIsLoading;
|
|
57
62
|
},
|
|
63
|
+
onStatusChange: (newStatus) => {
|
|
64
|
+
status = newStatus;
|
|
65
|
+
},
|
|
58
66
|
onErrorChange: (newError) => {
|
|
59
67
|
error = newError;
|
|
60
68
|
},
|
|
@@ -103,6 +111,9 @@ export function createChat(options) {
|
|
|
103
111
|
get error() {
|
|
104
112
|
return error;
|
|
105
113
|
},
|
|
114
|
+
get status() {
|
|
115
|
+
return status;
|
|
116
|
+
},
|
|
106
117
|
sendMessage,
|
|
107
118
|
append,
|
|
108
119
|
reload,
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AnyClientTool, ModelMessage } from '@tanstack/ai';
|
|
2
|
-
import type { ChatClientOptions, ChatRequestBody, UIMessage } from '@tanstack/ai-client';
|
|
3
|
-
export type {
|
|
2
|
+
import type { ChatClientOptions, ChatClientState, ChatRequestBody, MultimodalContent, UIMessage } from '@tanstack/ai-client';
|
|
3
|
+
export type { ChatRequestBody, MultimodalContent, UIMessage };
|
|
4
4
|
/**
|
|
5
5
|
* Options for the createChat function.
|
|
6
6
|
*
|
|
@@ -9,6 +9,7 @@ export type { UIMessage, ChatRequestBody };
|
|
|
9
9
|
* - `onMessagesChange` - Managed by Svelte state (exposed as `messages`)
|
|
10
10
|
* - `onLoadingChange` - Managed by Svelte state (exposed as `isLoading`)
|
|
11
11
|
* - `onErrorChange` - Managed by Svelte state (exposed as `error`)
|
|
12
|
+
* - `onStatusChange` - Managed by Svelte state (exposed as `status`)
|
|
12
13
|
*
|
|
13
14
|
* All other callbacks (onResponse, onChunk, onFinish, onError) are
|
|
14
15
|
* passed through to the underlying ChatClient and can be used for side effects.
|
|
@@ -16,16 +17,17 @@ export type { UIMessage, ChatRequestBody };
|
|
|
16
17
|
* Note: Connection and body changes will recreate the ChatClient instance.
|
|
17
18
|
* To update these options, remount the component or use a key prop.
|
|
18
19
|
*/
|
|
19
|
-
export type CreateChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any> = Omit<ChatClientOptions<TTools>, 'onMessagesChange' | 'onLoadingChange' | 'onErrorChange'>;
|
|
20
|
+
export type CreateChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any> = Omit<ChatClientOptions<TTools>, 'onMessagesChange' | 'onLoadingChange' | 'onErrorChange' | 'onStatusChange'>;
|
|
20
21
|
export interface CreateChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any> {
|
|
21
22
|
/**
|
|
22
23
|
* Current messages in the conversation (reactive getter)
|
|
23
24
|
*/
|
|
24
25
|
readonly messages: Array<UIMessage<TTools>>;
|
|
25
26
|
/**
|
|
26
|
-
* Send a message and get a response
|
|
27
|
+
* Send a message and get a response.
|
|
28
|
+
* Can be a simple string or multimodal content with images, audio, etc.
|
|
27
29
|
*/
|
|
28
|
-
sendMessage: (content: string) => Promise<void>;
|
|
30
|
+
sendMessage: (content: string | MultimodalContent) => Promise<void>;
|
|
29
31
|
/**
|
|
30
32
|
* Append a message to the conversation
|
|
31
33
|
*/
|
|
@@ -71,6 +73,10 @@ export interface CreateChatReturn<TTools extends ReadonlyArray<AnyClientTool> =
|
|
|
71
73
|
* Clear all messages
|
|
72
74
|
*/
|
|
73
75
|
clear: () => void;
|
|
76
|
+
/**
|
|
77
|
+
* Current generation status (reactive getter)
|
|
78
|
+
*/
|
|
79
|
+
readonly status: ChatClientState;
|
|
74
80
|
/**
|
|
75
81
|
* Update the body sent with requests (e.g., for changing model selection)
|
|
76
82
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Svelte bindings for TanStack AI",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
"streaming"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@tanstack/ai-client": "0.
|
|
36
|
+
"@tanstack/ai-client": "0.4.1"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"svelte": "^5.0.0",
|
|
40
|
-
"@tanstack/ai": "^0.
|
|
40
|
+
"@tanstack/ai": "^0.4.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@sveltejs/package": "^2.3.10",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"svelte-check": "^4.2.0",
|
|
50
50
|
"typescript": "5.9.3",
|
|
51
51
|
"vite": "^7.2.7",
|
|
52
|
-
"@tanstack/ai": "0.
|
|
52
|
+
"@tanstack/ai": "0.4.0"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
55
|
"clean": "rm -rf ./dist",
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { ChatClient } from '@tanstack/ai-client'
|
|
2
|
+
import type { ChatClientState } from '@tanstack/ai-client'
|
|
2
3
|
import type { AnyClientTool, ModelMessage } from '@tanstack/ai'
|
|
3
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
CreateChatOptions,
|
|
6
|
+
CreateChatReturn,
|
|
7
|
+
MultimodalContent,
|
|
8
|
+
UIMessage,
|
|
9
|
+
} from './types'
|
|
4
10
|
|
|
5
11
|
/**
|
|
6
12
|
* Creates a reactive chat instance for Svelte 5.
|
|
@@ -44,6 +50,7 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
44
50
|
let messages = $state<Array<UIMessage<TTools>>>(options.initialMessages || [])
|
|
45
51
|
let isLoading = $state(false)
|
|
46
52
|
let error = $state<Error | undefined>(undefined)
|
|
53
|
+
let status = $state<ChatClientState>('ready')
|
|
47
54
|
|
|
48
55
|
// Create ChatClient instance
|
|
49
56
|
const client = new ChatClient({
|
|
@@ -53,8 +60,12 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
53
60
|
body: options.body,
|
|
54
61
|
onResponse: options.onResponse,
|
|
55
62
|
onChunk: options.onChunk,
|
|
56
|
-
onFinish:
|
|
57
|
-
|
|
63
|
+
onFinish: (message) => {
|
|
64
|
+
options.onFinish?.(message)
|
|
65
|
+
},
|
|
66
|
+
onError: (err) => {
|
|
67
|
+
options.onError?.(err)
|
|
68
|
+
},
|
|
58
69
|
tools: options.tools,
|
|
59
70
|
streamProcessor: options.streamProcessor,
|
|
60
71
|
onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {
|
|
@@ -63,6 +74,9 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
63
74
|
onLoadingChange: (newIsLoading: boolean) => {
|
|
64
75
|
isLoading = newIsLoading
|
|
65
76
|
},
|
|
77
|
+
onStatusChange: (newStatus: ChatClientState) => {
|
|
78
|
+
status = newStatus
|
|
79
|
+
},
|
|
66
80
|
onErrorChange: (newError: Error | undefined) => {
|
|
67
81
|
error = newError
|
|
68
82
|
},
|
|
@@ -74,7 +88,7 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
74
88
|
// Users should call chat.stop() in their component's cleanup if needed.
|
|
75
89
|
|
|
76
90
|
// Define methods
|
|
77
|
-
const sendMessage = async (content: string) => {
|
|
91
|
+
const sendMessage = async (content: string | MultimodalContent) => {
|
|
78
92
|
await client.sendMessage(content)
|
|
79
93
|
}
|
|
80
94
|
|
|
@@ -131,6 +145,9 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
131
145
|
get error() {
|
|
132
146
|
return error
|
|
133
147
|
},
|
|
148
|
+
get status() {
|
|
149
|
+
return status
|
|
150
|
+
},
|
|
134
151
|
sendMessage,
|
|
135
152
|
append,
|
|
136
153
|
reload,
|
package/src/types.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type { AnyClientTool, ModelMessage } from '@tanstack/ai'
|
|
2
2
|
import type {
|
|
3
3
|
ChatClientOptions,
|
|
4
|
+
ChatClientState,
|
|
4
5
|
ChatRequestBody,
|
|
6
|
+
MultimodalContent,
|
|
5
7
|
UIMessage,
|
|
6
8
|
} from '@tanstack/ai-client'
|
|
7
9
|
|
|
8
10
|
// Re-export types from ai-client
|
|
9
|
-
export type {
|
|
11
|
+
export type { ChatRequestBody, MultimodalContent, UIMessage }
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
14
|
* Options for the createChat function.
|
|
@@ -16,6 +18,7 @@ export type { UIMessage, ChatRequestBody }
|
|
|
16
18
|
* - `onMessagesChange` - Managed by Svelte state (exposed as `messages`)
|
|
17
19
|
* - `onLoadingChange` - Managed by Svelte state (exposed as `isLoading`)
|
|
18
20
|
* - `onErrorChange` - Managed by Svelte state (exposed as `error`)
|
|
21
|
+
* - `onStatusChange` - Managed by Svelte state (exposed as `status`)
|
|
19
22
|
*
|
|
20
23
|
* All other callbacks (onResponse, onChunk, onFinish, onError) are
|
|
21
24
|
* passed through to the underlying ChatClient and can be used for side effects.
|
|
@@ -27,7 +30,7 @@ export type CreateChatOptions<
|
|
|
27
30
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
28
31
|
> = Omit<
|
|
29
32
|
ChatClientOptions<TTools>,
|
|
30
|
-
'onMessagesChange' | 'onLoadingChange' | 'onErrorChange'
|
|
33
|
+
'onMessagesChange' | 'onLoadingChange' | 'onErrorChange' | 'onStatusChange'
|
|
31
34
|
>
|
|
32
35
|
|
|
33
36
|
export interface CreateChatReturn<
|
|
@@ -39,9 +42,10 @@ export interface CreateChatReturn<
|
|
|
39
42
|
readonly messages: Array<UIMessage<TTools>>
|
|
40
43
|
|
|
41
44
|
/**
|
|
42
|
-
* Send a message and get a response
|
|
45
|
+
* Send a message and get a response.
|
|
46
|
+
* Can be a simple string or multimodal content with images, audio, etc.
|
|
43
47
|
*/
|
|
44
|
-
sendMessage: (content: string) => Promise<void>
|
|
48
|
+
sendMessage: (content: string | MultimodalContent) => Promise<void>
|
|
45
49
|
|
|
46
50
|
/**
|
|
47
51
|
* Append a message to the conversation
|
|
@@ -97,6 +101,10 @@ export interface CreateChatReturn<
|
|
|
97
101
|
*/
|
|
98
102
|
clear: () => void
|
|
99
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Current generation status (reactive getter)
|
|
106
|
+
*/
|
|
107
|
+
readonly status: ChatClientState
|
|
100
108
|
/**
|
|
101
109
|
* Update the body sent with requests (e.g., for changing model selection)
|
|
102
110
|
*/
|