@tanstack/ai-svelte 0.2.2 → 0.4.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 +17 -2
- package/dist/types.d.ts +12 -3
- package/package.json +4 -4
- package/src/create-chat.svelte.ts +19 -2
- package/src/types.ts +13 -2
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
|
},
|
|
@@ -88,6 +96,9 @@ export function createChat(options) {
|
|
|
88
96
|
const addToolApprovalResponse = async (response) => {
|
|
89
97
|
await client.addToolApprovalResponse(response);
|
|
90
98
|
};
|
|
99
|
+
const updateBody = (newBody) => {
|
|
100
|
+
client.updateOptions({ body: newBody });
|
|
101
|
+
};
|
|
91
102
|
// Return the chat interface with reactive getters
|
|
92
103
|
// Using getters allows Svelte to track reactivity without needing $ prefix
|
|
93
104
|
return {
|
|
@@ -100,6 +111,9 @@ export function createChat(options) {
|
|
|
100
111
|
get error() {
|
|
101
112
|
return error;
|
|
102
113
|
},
|
|
114
|
+
get status() {
|
|
115
|
+
return status;
|
|
116
|
+
},
|
|
103
117
|
sendMessage,
|
|
104
118
|
append,
|
|
105
119
|
reload,
|
|
@@ -108,5 +122,6 @@ export function createChat(options) {
|
|
|
108
122
|
clear,
|
|
109
123
|
addToolResult,
|
|
110
124
|
addToolApprovalResponse,
|
|
125
|
+
updateBody,
|
|
111
126
|
};
|
|
112
127
|
}
|
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, UIMessage } from '@tanstack/ai-client';
|
|
3
|
+
export type { ChatRequestBody, 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,7 +17,7 @@ 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)
|
|
@@ -71,4 +72,12 @@ export interface CreateChatReturn<TTools extends ReadonlyArray<AnyClientTool> =
|
|
|
71
72
|
* Clear all messages
|
|
72
73
|
*/
|
|
73
74
|
clear: () => void;
|
|
75
|
+
/**
|
|
76
|
+
* Current generation status (reactive getter)
|
|
77
|
+
*/
|
|
78
|
+
readonly status: ChatClientState;
|
|
79
|
+
/**
|
|
80
|
+
* Update the body sent with requests (e.g., for changing model selection)
|
|
81
|
+
*/
|
|
82
|
+
updateBody: (body: Record<string, any>) => void;
|
|
74
83
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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.0"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"svelte": "^5.0.0",
|
|
40
|
-
"@tanstack/ai": "^0.
|
|
40
|
+
"@tanstack/ai": "^0.3.1"
|
|
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.3.1"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
55
|
"clean": "rm -rf ./dist",
|
|
@@ -1,4 +1,5 @@
|
|
|
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
4
|
import type { CreateChatOptions, CreateChatReturn, UIMessage } from './types'
|
|
4
5
|
|
|
@@ -44,6 +45,7 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
44
45
|
let messages = $state<Array<UIMessage<TTools>>>(options.initialMessages || [])
|
|
45
46
|
let isLoading = $state(false)
|
|
46
47
|
let error = $state<Error | undefined>(undefined)
|
|
48
|
+
let status = $state<ChatClientState>('ready')
|
|
47
49
|
|
|
48
50
|
// Create ChatClient instance
|
|
49
51
|
const client = new ChatClient({
|
|
@@ -53,8 +55,12 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
53
55
|
body: options.body,
|
|
54
56
|
onResponse: options.onResponse,
|
|
55
57
|
onChunk: options.onChunk,
|
|
56
|
-
onFinish:
|
|
57
|
-
|
|
58
|
+
onFinish: (message) => {
|
|
59
|
+
options.onFinish?.(message)
|
|
60
|
+
},
|
|
61
|
+
onError: (err) => {
|
|
62
|
+
options.onError?.(err)
|
|
63
|
+
},
|
|
58
64
|
tools: options.tools,
|
|
59
65
|
streamProcessor: options.streamProcessor,
|
|
60
66
|
onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {
|
|
@@ -63,6 +69,9 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
63
69
|
onLoadingChange: (newIsLoading: boolean) => {
|
|
64
70
|
isLoading = newIsLoading
|
|
65
71
|
},
|
|
72
|
+
onStatusChange: (newStatus: ChatClientState) => {
|
|
73
|
+
status = newStatus
|
|
74
|
+
},
|
|
66
75
|
onErrorChange: (newError: Error | undefined) => {
|
|
67
76
|
error = newError
|
|
68
77
|
},
|
|
@@ -115,6 +124,10 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
115
124
|
await client.addToolApprovalResponse(response)
|
|
116
125
|
}
|
|
117
126
|
|
|
127
|
+
const updateBody = (newBody: Record<string, any>) => {
|
|
128
|
+
client.updateOptions({ body: newBody })
|
|
129
|
+
}
|
|
130
|
+
|
|
118
131
|
// Return the chat interface with reactive getters
|
|
119
132
|
// Using getters allows Svelte to track reactivity without needing $ prefix
|
|
120
133
|
return {
|
|
@@ -127,6 +140,9 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
127
140
|
get error() {
|
|
128
141
|
return error
|
|
129
142
|
},
|
|
143
|
+
get status() {
|
|
144
|
+
return status
|
|
145
|
+
},
|
|
130
146
|
sendMessage,
|
|
131
147
|
append,
|
|
132
148
|
reload,
|
|
@@ -135,5 +151,6 @@ export function createChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
|
|
|
135
151
|
clear,
|
|
136
152
|
addToolResult,
|
|
137
153
|
addToolApprovalResponse,
|
|
154
|
+
updateBody,
|
|
138
155
|
}
|
|
139
156
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { AnyClientTool, ModelMessage } from '@tanstack/ai'
|
|
2
2
|
import type {
|
|
3
3
|
ChatClientOptions,
|
|
4
|
+
ChatClientState,
|
|
4
5
|
ChatRequestBody,
|
|
5
6
|
UIMessage,
|
|
6
7
|
} from '@tanstack/ai-client'
|
|
7
8
|
|
|
8
9
|
// Re-export types from ai-client
|
|
9
|
-
export type {
|
|
10
|
+
export type { ChatRequestBody, UIMessage }
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Options for the createChat function.
|
|
@@ -16,6 +17,7 @@ export type { UIMessage, ChatRequestBody }
|
|
|
16
17
|
* - `onMessagesChange` - Managed by Svelte state (exposed as `messages`)
|
|
17
18
|
* - `onLoadingChange` - Managed by Svelte state (exposed as `isLoading`)
|
|
18
19
|
* - `onErrorChange` - Managed by Svelte state (exposed as `error`)
|
|
20
|
+
* - `onStatusChange` - Managed by Svelte state (exposed as `status`)
|
|
19
21
|
*
|
|
20
22
|
* All other callbacks (onResponse, onChunk, onFinish, onError) are
|
|
21
23
|
* passed through to the underlying ChatClient and can be used for side effects.
|
|
@@ -27,7 +29,7 @@ export type CreateChatOptions<
|
|
|
27
29
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
28
30
|
> = Omit<
|
|
29
31
|
ChatClientOptions<TTools>,
|
|
30
|
-
'onMessagesChange' | 'onLoadingChange' | 'onErrorChange'
|
|
32
|
+
'onMessagesChange' | 'onLoadingChange' | 'onErrorChange' | 'onStatusChange'
|
|
31
33
|
>
|
|
32
34
|
|
|
33
35
|
export interface CreateChatReturn<
|
|
@@ -96,6 +98,15 @@ export interface CreateChatReturn<
|
|
|
96
98
|
* Clear all messages
|
|
97
99
|
*/
|
|
98
100
|
clear: () => void
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Current generation status (reactive getter)
|
|
104
|
+
*/
|
|
105
|
+
readonly status: ChatClientState
|
|
106
|
+
/**
|
|
107
|
+
* Update the body sent with requests (e.g., for changing model selection)
|
|
108
|
+
*/
|
|
109
|
+
updateBody: (body: Record<string, any>) => void
|
|
99
110
|
}
|
|
100
111
|
|
|
101
112
|
// Note: createChatClientOptions and InferChatMessages are now in @tanstack/ai-client
|