@tanstack/ai-svelte 0.11.1 → 0.12.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.
- package/dist/create-chat.svelte.d.ts +2 -1
- package/dist/create-chat.svelte.js +6 -1
- package/dist/types.d.ts +10 -5
- package/package.json +4 -4
- package/src/create-chat.svelte.ts +13 -5
- package/src/types.ts +15 -3
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { InferredClientContext } from '@tanstack/ai-client';
|
|
1
2
|
import type { AnyClientTool, SchemaInput } from '@tanstack/ai';
|
|
2
3
|
import type { CreateChatOptions, CreateChatReturn } from './types';
|
|
3
4
|
/**
|
|
@@ -30,4 +31,4 @@ import type { CreateChatOptions, CreateChatReturn } from './types';
|
|
|
30
31
|
* </div>
|
|
31
32
|
* ```
|
|
32
33
|
*/
|
|
33
|
-
export declare function createChat<TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined
|
|
34
|
+
export declare function createChat<TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined, TContext = InferredClientContext<TTools>>(options: CreateChatOptions<TTools, TSchema, TContext>): CreateChatReturn<TTools, TSchema, TContext>;
|
|
@@ -66,6 +66,7 @@ export function createChat(options) {
|
|
|
66
66
|
...(options.forwardedProps !== undefined && {
|
|
67
67
|
forwardedProps: options.forwardedProps,
|
|
68
68
|
}),
|
|
69
|
+
...(options.context !== undefined && { context: options.context }),
|
|
69
70
|
devtools: {
|
|
70
71
|
...options.devtools,
|
|
71
72
|
framework: 'svelte',
|
|
@@ -157,6 +158,9 @@ export function createChat(options) {
|
|
|
157
158
|
const updateForwardedProps = (newForwardedProps) => {
|
|
158
159
|
client.updateOptions({ forwardedProps: newForwardedProps });
|
|
159
160
|
};
|
|
161
|
+
const updateContext = (newContext) => {
|
|
162
|
+
client.updateOptions({ context: newContext });
|
|
163
|
+
};
|
|
160
164
|
// The "active" structured-output part is the one on the assistant message
|
|
161
165
|
// after the latest user message. When no user message exists yet (e.g.
|
|
162
166
|
// `initialMessages` carries only a stale assistant turn), we return null
|
|
@@ -194,7 +198,7 @@ export function createChat(options) {
|
|
|
194
198
|
: null);
|
|
195
199
|
// Return the chat interface with reactive getters
|
|
196
200
|
// Using getters allows Svelte to track reactivity without needing $ prefix
|
|
197
|
-
// eslint-disable-next-line no-restricted-syntax -- rune return shape diverges from generic CreateChatReturn<TTools, TSchema> due to TSchema conditional partial/final fields; TS can't structurally narrow.
|
|
201
|
+
// eslint-disable-next-line no-restricted-syntax -- rune return shape diverges from generic CreateChatReturn<TTools, TSchema, TContext> due to TSchema conditional partial/final fields; TS can't structurally narrow.
|
|
198
202
|
return {
|
|
199
203
|
get messages() {
|
|
200
204
|
return messages;
|
|
@@ -234,5 +238,6 @@ export function createChat(options) {
|
|
|
234
238
|
addToolApprovalResponse,
|
|
235
239
|
updateBody,
|
|
236
240
|
updateForwardedProps,
|
|
241
|
+
updateContext,
|
|
237
242
|
};
|
|
238
243
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AnyClientTool, InferSchemaType, ModelMessage, SchemaInput } from '@tanstack/ai';
|
|
2
|
-
import type { AIDevtoolsDisplayOptions, ChatClientOptions, ChatClientState, ChatRequestBody, ConnectionStatus, DistributedOmit, MultimodalContent, UIMessage } from '@tanstack/ai-client';
|
|
2
|
+
import type { AIDevtoolsDisplayOptions, ChatClientOptions, ChatClientState, ChatRequestBody, ClientContextOptionFromTools, ConnectionStatus, DistributedOmit, InferredClientContext, MultimodalContent, UIMessage } from '@tanstack/ai-client';
|
|
3
3
|
export type { ChatRequestBody, MultimodalContent, UIMessage };
|
|
4
4
|
/**
|
|
5
5
|
* Recursive partial — every property and every nested array element is
|
|
@@ -30,7 +30,7 @@ export type DeepPartial<T> = T extends ReadonlyArray<infer U> ? Array<DeepPartia
|
|
|
30
30
|
* Note: Connection and body changes will recreate the ChatClient instance.
|
|
31
31
|
* To update these options, remount the component or use a key prop.
|
|
32
32
|
*/
|
|
33
|
-
export type CreateChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined
|
|
33
|
+
export type CreateChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined, TContext = InferredClientContext<TTools>> = DistributedOmit<ChatClientOptions<TTools, TContext>, 'onMessagesChange' | 'onLoadingChange' | 'onErrorChange' | 'onStatusChange' | 'onSubscriptionChange' | 'onConnectionStatusChange' | 'onSessionGeneratingChange' | 'context' | 'devtools'> & {
|
|
34
34
|
live?: boolean;
|
|
35
35
|
/** Display options for TanStack AI Devtools. */
|
|
36
36
|
devtools?: AIDevtoolsDisplayOptions;
|
|
@@ -39,13 +39,13 @@ export type CreateChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
|
39
39
|
* Schema). Used to infer the shape of `partial` and `final`.
|
|
40
40
|
*/
|
|
41
41
|
outputSchema?: TSchema;
|
|
42
|
-
}
|
|
42
|
+
} & ClientContextOptionFromTools<TTools, TContext>;
|
|
43
43
|
/**
|
|
44
44
|
* Discriminated return shape: when `outputSchema` is supplied, the return adds
|
|
45
45
|
* typed `partial` / `final` reactive getters; otherwise the return is
|
|
46
46
|
* unchanged.
|
|
47
47
|
*/
|
|
48
|
-
export type CreateChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined> = BaseCreateChatReturn<TTools, TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown> & (TSchema extends SchemaInput ? {
|
|
48
|
+
export type CreateChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined, TContext = unknown> = BaseCreateChatReturn<TTools, TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown, TContext> & (TSchema extends SchemaInput ? {
|
|
49
49
|
/**
|
|
50
50
|
* Live progressively-parsed structured output (reactive getter).
|
|
51
51
|
* Derived from the latest assistant message's structured-output part.
|
|
@@ -58,7 +58,7 @@ export type CreateChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
|
58
58
|
*/
|
|
59
59
|
readonly final: InferSchemaType<TSchema> | null;
|
|
60
60
|
} : Record<never, never>);
|
|
61
|
-
interface BaseCreateChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any, TData = unknown> {
|
|
61
|
+
interface BaseCreateChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any, TData = unknown, TContext = unknown> {
|
|
62
62
|
/**
|
|
63
63
|
* Current messages in the conversation (reactive getter). When
|
|
64
64
|
* `outputSchema` is supplied, `messages[i].parts.find(p => p.type ===
|
|
@@ -149,4 +149,9 @@ interface BaseCreateChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any
|
|
|
149
149
|
* changing model selection or other client-driven options).
|
|
150
150
|
*/
|
|
151
151
|
updateForwardedProps: (forwardedProps: Record<string, any>) => void;
|
|
152
|
+
/**
|
|
153
|
+
* Update the client-local runtime context passed to client tool
|
|
154
|
+
* implementations. This value is not serialized to the server.
|
|
155
|
+
*/
|
|
156
|
+
updateContext: (context: TContext) => void;
|
|
152
157
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"description": "Svelte 5 bindings for TanStack AI streaming chat, structured outputs, and media generation.",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,11 +38,11 @@
|
|
|
38
38
|
"media-generation"
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@tanstack/ai-client": "0.
|
|
41
|
+
"@tanstack/ai-client": "0.15.1"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"svelte": "^5.0.0",
|
|
45
|
-
"@tanstack/ai": "^0.
|
|
45
|
+
"@tanstack/ai": "^0.25.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@standard-schema/spec": "^1.1.0",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"svelte-check": "^4.2.0",
|
|
56
56
|
"typescript": "5.9.3",
|
|
57
57
|
"vite": "^7.3.3",
|
|
58
|
-
"@tanstack/ai": "0.
|
|
58
|
+
"@tanstack/ai": "0.25.0"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"clean": "rm -rf ./dist",
|
|
@@ -3,6 +3,7 @@ import { createChatDevtoolsBridge } from '@tanstack/ai-client/devtools'
|
|
|
3
3
|
import type {
|
|
4
4
|
ChatClientState,
|
|
5
5
|
ConnectionStatus,
|
|
6
|
+
InferredClientContext,
|
|
6
7
|
StructuredOutputPart,
|
|
7
8
|
} from '@tanstack/ai-client'
|
|
8
9
|
import type {
|
|
@@ -53,9 +54,10 @@ import type {
|
|
|
53
54
|
export function createChat<
|
|
54
55
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
55
56
|
TSchema extends SchemaInput | undefined = undefined,
|
|
57
|
+
TContext = InferredClientContext<TTools>,
|
|
56
58
|
>(
|
|
57
|
-
options: CreateChatOptions<TTools, TSchema>,
|
|
58
|
-
): CreateChatReturn<TTools, TSchema> {
|
|
59
|
+
options: CreateChatOptions<TTools, TSchema, TContext>,
|
|
60
|
+
): CreateChatReturn<TTools, TSchema, TContext> {
|
|
59
61
|
// Generate a unique ID for this chat instance
|
|
60
62
|
const clientId =
|
|
61
63
|
options.id ||
|
|
@@ -91,7 +93,7 @@ export function createChat<
|
|
|
91
93
|
? { connection: options.connection }
|
|
92
94
|
: { fetcher: options.fetcher }
|
|
93
95
|
|
|
94
|
-
const client = new ChatClient({
|
|
96
|
+
const client = new ChatClient<TTools, TContext>({
|
|
95
97
|
devtoolsBridgeFactory: createChatDevtoolsBridge,
|
|
96
98
|
...transport,
|
|
97
99
|
id: clientId,
|
|
@@ -102,6 +104,7 @@ export function createChat<
|
|
|
102
104
|
...(options.forwardedProps !== undefined && {
|
|
103
105
|
forwardedProps: options.forwardedProps,
|
|
104
106
|
}),
|
|
107
|
+
...(options.context !== undefined && { context: options.context }),
|
|
105
108
|
devtools: {
|
|
106
109
|
...options.devtools,
|
|
107
110
|
framework: 'svelte',
|
|
@@ -217,6 +220,10 @@ export function createChat<
|
|
|
217
220
|
client.updateOptions({ forwardedProps: newForwardedProps })
|
|
218
221
|
}
|
|
219
222
|
|
|
223
|
+
const updateContext = (newContext: TContext) => {
|
|
224
|
+
client.updateOptions({ context: newContext })
|
|
225
|
+
}
|
|
226
|
+
|
|
220
227
|
// The "active" structured-output part is the one on the assistant message
|
|
221
228
|
// after the latest user message. When no user message exists yet (e.g.
|
|
222
229
|
// `initialMessages` carries only a stale assistant turn), we return null
|
|
@@ -257,7 +264,7 @@ export function createChat<
|
|
|
257
264
|
|
|
258
265
|
// Return the chat interface with reactive getters
|
|
259
266
|
// Using getters allows Svelte to track reactivity without needing $ prefix
|
|
260
|
-
// eslint-disable-next-line no-restricted-syntax -- rune return shape diverges from generic CreateChatReturn<TTools, TSchema> due to TSchema conditional partial/final fields; TS can't structurally narrow.
|
|
267
|
+
// eslint-disable-next-line no-restricted-syntax -- rune return shape diverges from generic CreateChatReturn<TTools, TSchema, TContext> due to TSchema conditional partial/final fields; TS can't structurally narrow.
|
|
261
268
|
return {
|
|
262
269
|
get messages() {
|
|
263
270
|
return messages
|
|
@@ -297,5 +304,6 @@ export function createChat<
|
|
|
297
304
|
addToolApprovalResponse,
|
|
298
305
|
updateBody,
|
|
299
306
|
updateForwardedProps,
|
|
300
|
-
|
|
307
|
+
updateContext,
|
|
308
|
+
} as unknown as CreateChatReturn<TTools, TSchema, TContext>
|
|
301
309
|
}
|
package/src/types.ts
CHANGED
|
@@ -9,8 +9,10 @@ import type {
|
|
|
9
9
|
ChatClientOptions,
|
|
10
10
|
ChatClientState,
|
|
11
11
|
ChatRequestBody,
|
|
12
|
+
ClientContextOptionFromTools,
|
|
12
13
|
ConnectionStatus,
|
|
13
14
|
DistributedOmit,
|
|
15
|
+
InferredClientContext,
|
|
14
16
|
MultimodalContent,
|
|
15
17
|
UIMessage,
|
|
16
18
|
} from '@tanstack/ai-client'
|
|
@@ -54,8 +56,9 @@ export type DeepPartial<T> =
|
|
|
54
56
|
export type CreateChatOptions<
|
|
55
57
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
56
58
|
TSchema extends SchemaInput | undefined = undefined,
|
|
59
|
+
TContext = InferredClientContext<TTools>,
|
|
57
60
|
> = DistributedOmit<
|
|
58
|
-
ChatClientOptions<TTools>,
|
|
61
|
+
ChatClientOptions<TTools, TContext>,
|
|
59
62
|
| 'onMessagesChange'
|
|
60
63
|
| 'onLoadingChange'
|
|
61
64
|
| 'onErrorChange'
|
|
@@ -63,6 +66,7 @@ export type CreateChatOptions<
|
|
|
63
66
|
| 'onSubscriptionChange'
|
|
64
67
|
| 'onConnectionStatusChange'
|
|
65
68
|
| 'onSessionGeneratingChange'
|
|
69
|
+
| 'context'
|
|
66
70
|
| 'devtools'
|
|
67
71
|
> & {
|
|
68
72
|
live?: boolean
|
|
@@ -73,7 +77,7 @@ export type CreateChatOptions<
|
|
|
73
77
|
* Schema). Used to infer the shape of `partial` and `final`.
|
|
74
78
|
*/
|
|
75
79
|
outputSchema?: TSchema
|
|
76
|
-
}
|
|
80
|
+
} & ClientContextOptionFromTools<TTools, TContext>
|
|
77
81
|
|
|
78
82
|
/**
|
|
79
83
|
* Discriminated return shape: when `outputSchema` is supplied, the return adds
|
|
@@ -83,9 +87,11 @@ export type CreateChatOptions<
|
|
|
83
87
|
export type CreateChatReturn<
|
|
84
88
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
85
89
|
TSchema extends SchemaInput | undefined = undefined,
|
|
90
|
+
TContext = unknown,
|
|
86
91
|
> = BaseCreateChatReturn<
|
|
87
92
|
TTools,
|
|
88
|
-
TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown
|
|
93
|
+
TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown,
|
|
94
|
+
TContext
|
|
89
95
|
> &
|
|
90
96
|
(TSchema extends SchemaInput
|
|
91
97
|
? {
|
|
@@ -106,6 +112,7 @@ export type CreateChatReturn<
|
|
|
106
112
|
interface BaseCreateChatReturn<
|
|
107
113
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
108
114
|
TData = unknown,
|
|
115
|
+
TContext = unknown,
|
|
109
116
|
> {
|
|
110
117
|
/**
|
|
111
118
|
* Current messages in the conversation (reactive getter). When
|
|
@@ -209,6 +216,11 @@ interface BaseCreateChatReturn<
|
|
|
209
216
|
* changing model selection or other client-driven options).
|
|
210
217
|
*/
|
|
211
218
|
updateForwardedProps: (forwardedProps: Record<string, any>) => void
|
|
219
|
+
/**
|
|
220
|
+
* Update the client-local runtime context passed to client tool
|
|
221
|
+
* implementations. This value is not serialized to the server.
|
|
222
|
+
*/
|
|
223
|
+
updateContext: (context: TContext) => void
|
|
212
224
|
}
|
|
213
225
|
|
|
214
226
|
// Note: createChatClientOptions and InferChatMessages are now in @tanstack/ai-client
|