@tanstack/ai-remix 0.6.4 → 0.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ai-remix",
3
- "version": "0.6.4",
3
+ "version": "0.7.1",
4
4
  "description": "Remix 3 bindings for TanStack AI streaming chat, structured outputs, and media generation.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -47,11 +47,11 @@
47
47
  "README.md"
48
48
  ],
49
49
  "dependencies": {
50
- "@tanstack/ai-client": "^0.39.3"
50
+ "@tanstack/ai-client": "^0.40.1"
51
51
  },
52
52
  "peerDependencies": {
53
53
  "remix": "^3.0.0-rc.1",
54
- "@tanstack/ai": "^0.69.0"
54
+ "@tanstack/ai": "^0.71.0"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@types/node": "^24.10.1",
@@ -60,7 +60,7 @@
60
60
  "remix": "^3.0.0-rc.1",
61
61
  "vite": "^8.2.1",
62
62
  "vitest": "^4.1.10",
63
- "@tanstack/ai": "0.69.0"
63
+ "@tanstack/ai": "0.71.0"
64
64
  },
65
65
  "scripts": {
66
66
  "lint:fix": "oxlint src --type-aware --fix",
@@ -2,13 +2,9 @@ import { ChatClient } from '@tanstack/ai-client'
2
2
  import { createChatDevtoolsBridge } from '@tanstack/ai-client/devtools'
3
3
  import type { Handle } from 'remix/ui'
4
4
  import type {
5
- ChatClientState,
6
5
  ResolvableChatInterrupt,
7
- ChatInterruptState,
8
6
  ChatResumeState,
9
- ConnectionStatus,
10
7
  InferredClientContext,
11
- QueuedMessage,
12
8
  SendMessageOptions,
13
9
  StructuredOutputPart,
14
10
  } from '@tanstack/ai-client'
@@ -29,16 +25,12 @@ import type {
29
25
  UIMessage,
30
26
  } from './types.ts'
31
27
 
32
- const EMPTY_INTERRUPTS = Object.freeze([])
33
- const EMPTY_INTERRUPT_ERRORS = Object.freeze([])
34
-
35
28
  /**
36
29
  * Create a chat helper for a Remix component.
37
30
  *
38
31
  * Call this in setup with the component Handle from `remix/ui`. The helper
39
- * wraps ChatClient and stores chat state in local variables. ChatClient state
40
- * callbacks write those variables and then call `handle.update()`, so render
41
- * reads the latest snapshot through getters.
32
+ * wraps ChatClient and calls `handle.update()` when the client snapshot
33
+ * changes, so render reads the latest frozen snapshot through getters.
42
34
  *
43
35
  * The default thread id is `options.threadId ?? handle.id`. Cleanup runs when
44
36
  * `handle.signal` aborts. Do not pass identification in `options.devtools`;
@@ -81,23 +73,6 @@ export function createChat<
81
73
  handle: Pick<Handle, 'id' | 'update' | 'signal'>,
82
74
  options: CreateChatOptions<TTools, TSchema, TContext, TInterrupts>,
83
75
  ) {
84
- let messages = options.initialMessages || []
85
- let isLoading = false
86
- let hasOlderMessages = false
87
- let error: Error | undefined
88
- let status: ChatClientState = 'ready'
89
- let isSubscribed = false
90
- let connectionStatus: ConnectionStatus = 'disconnected'
91
- let sessionGenerating = false
92
- let isHydrating = false
93
- let queue: Array<QueuedMessage> = []
94
- let runId: string | null = null
95
- let interruptState: ChatInterruptState<TTools, TInterrupts> = {
96
- interrupts: EMPTY_INTERRUPTS,
97
- pendingInterrupts: EMPTY_INTERRUPTS,
98
- interruptErrors: EMPTY_INTERRUPT_ERRORS,
99
- resuming: false,
100
- }
101
76
  let closed = false
102
77
 
103
78
  type Partial = DeepPartial<InferSchemaType<NonNullable<TSchema>>>
@@ -168,69 +143,19 @@ export function createChat<
168
143
  ...(options.streamProcessor !== undefined && {
169
144
  streamProcessor: options.streamProcessor,
170
145
  }),
171
- onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {
172
- messages = newMessages
173
- hasOlderMessages = client.getHasOlderMessages()
174
- commit()
175
- },
176
- onLoadingChange: (newIsLoading: boolean) => {
177
- isLoading = newIsLoading
178
- syncResumeState()
179
- commit()
180
- },
181
- onStatusChange: (newStatus: ChatClientState) => {
182
- status = newStatus
183
- commit()
184
- },
185
- onErrorChange: (newError: Error | undefined) => {
186
- error = newError
187
- commit()
188
- },
189
- onSubscriptionChange: (nextIsSubscribed: boolean) => {
190
- isSubscribed = nextIsSubscribed
191
- commit()
192
- },
193
- onConnectionStatusChange: (nextStatus: ConnectionStatus) => {
194
- connectionStatus = nextStatus
195
- commit()
196
- },
197
- onSessionGeneratingChange: (isGenerating: boolean) => {
198
- sessionGenerating = isGenerating
199
- commit()
200
- },
201
- onHydratingChange: (nextIsHydrating: boolean) => {
202
- isHydrating = nextIsHydrating
203
- commit()
204
- },
205
146
  ...(options.queue !== undefined && { queue: options.queue }),
206
- onQueueChange: (nextQueue: Array<QueuedMessage>) => {
207
- queue = nextQueue
208
- commit()
209
- },
210
- onRunIdChange: (nextRunId) => {
211
- runId = nextRunId
212
- commit()
213
- },
214
147
  onInterruptStateChange: (nextInterruptState, context) => {
215
- interruptState = nextInterruptState
216
148
  options.onInterruptStateChange?.(nextInterruptState, context)
217
- commit()
218
149
  },
219
150
  })
220
151
 
221
- function syncResumeState() {
222
- runId = client.getCurrentRunId()
223
- interruptState = client.getInterruptState()
224
- }
225
-
226
- messages = client.getMessages()
227
- isHydrating = client.getIsHydrating()
228
- interruptState = client.getInterruptState()
229
- runId = client.getCurrentRunId()
152
+ // Render reads the frozen client snapshot through the getters below.
153
+ const unsubscribeSnapshot = client.subscribeSnapshot(commit)
230
154
 
231
155
  function close() {
232
156
  if (closed) return
233
157
  closed = true
158
+ unsubscribeSnapshot()
234
159
  client.detach()
235
160
  if (options.live) {
236
161
  client.unsubscribe()
@@ -265,37 +190,23 @@ export function createChat<
265
190
  sendOptions?: SendMessageOptions,
266
191
  ) => {
267
192
  syncTools()
268
- try {
269
- await client.sendMessage(content, undefined, sendOptions)
270
- } finally {
271
- syncResumeState()
272
- }
193
+ await client.sendMessage(content, undefined, sendOptions)
273
194
  }
274
195
 
275
196
  const cancelQueued = (id: string) => client.cancelQueued(id)
276
197
 
277
198
  const append = async (message: ModelMessage | UIMessage<TTools>) => {
278
199
  syncTools()
279
- try {
280
- await client.append(message)
281
- } finally {
282
- syncResumeState()
283
- }
200
+ await client.append(message)
284
201
  }
285
202
 
286
203
  const reload = async () => {
287
204
  syncTools()
288
- try {
289
- await client.reload()
290
- } finally {
291
- syncResumeState()
292
- }
205
+ await client.reload()
293
206
  }
294
207
 
295
208
  const loadOlderMessages = async () => {
296
209
  await client.loadOlderMessages()
297
- hasOlderMessages = client.getHasOlderMessages()
298
- commit()
299
210
  }
300
211
 
301
212
  const stop = () => {
@@ -304,10 +215,9 @@ export function createChat<
304
215
 
305
216
  const clear = () => {
306
217
  client.clear()
307
- syncResumeState()
308
218
  }
309
219
 
310
- const setMessages = (newMessages: Array<UIMessage<TTools>>) => {
220
+ const setMessages = (newMessages: ReadonlyArray<UIMessage<TTools>>) => {
311
221
  client.setMessagesManually(newMessages)
312
222
  }
313
223
 
@@ -329,7 +239,6 @@ export function createChat<
329
239
  }) => {
330
240
  syncTools()
331
241
  await client.addToolApprovalResponse(response)
332
- syncResumeState()
333
242
  }
334
243
 
335
244
  const resumeInterrupts = async (
@@ -337,9 +246,7 @@ export function createChat<
337
246
  state?: ChatResumeState,
338
247
  ) => {
339
248
  syncTools()
340
- const result = await client.resumeInterrupts(resumeItems, state)
341
- syncResumeState()
342
- return result
249
+ return await client.resumeInterrupts(resumeItems, state)
343
250
  }
344
251
 
345
252
  const resolveInterrupts = (
@@ -375,6 +282,7 @@ export function createChat<
375
282
  }
376
283
 
377
284
  function activeStructuredPart(): StructuredOutputPart | null {
285
+ const { messages } = client.getSnapshot()
378
286
  let lastUserIndex = -1
379
287
  for (let i = messages.length - 1; i >= 0; i--) {
380
288
  if (messages[i]?.role === 'user') {
@@ -396,49 +304,49 @@ export function createChat<
396
304
 
397
305
  return {
398
306
  get messages() {
399
- return messages
307
+ return client.getSnapshot().messages
400
308
  },
401
309
  get isLoading() {
402
- return isLoading
310
+ return client.getSnapshot().isLoading
403
311
  },
404
312
  get hasOlderMessages() {
405
- return hasOlderMessages
313
+ return client.getSnapshot().hasOlderMessages
406
314
  },
407
315
  get error() {
408
- return error
316
+ return client.getSnapshot().error
409
317
  },
410
318
  get status() {
411
- return status
319
+ return client.getSnapshot().status
412
320
  },
413
321
  get isSubscribed() {
414
- return isSubscribed
322
+ return client.getSnapshot().isSubscribed
415
323
  },
416
324
  get connectionStatus() {
417
- return connectionStatus
325
+ return client.getSnapshot().connectionStatus
418
326
  },
419
327
  get sessionGenerating() {
420
- return sessionGenerating
328
+ return client.getSnapshot().sessionGenerating
421
329
  },
422
330
  get isHydrating() {
423
- return isHydrating
331
+ return client.getSnapshot().isHydrating
424
332
  },
425
333
  get queue() {
426
- return queue
334
+ return client.getSnapshot().queue
427
335
  },
428
336
  get runId() {
429
- return runId
337
+ return client.getSnapshot().runId
430
338
  },
431
339
  get interrupts() {
432
- return interruptState.interrupts
340
+ return client.getSnapshot().interruptState.interrupts
433
341
  },
434
342
  get pendingInterrupts() {
435
- return interruptState.interrupts
343
+ return client.getSnapshot().interruptState.interrupts
436
344
  },
437
345
  get interruptErrors() {
438
- return interruptState.interruptErrors
346
+ return client.getSnapshot().interruptState.interruptErrors
439
347
  },
440
348
  get resuming() {
441
- return interruptState.resuming
349
+ return client.getSnapshot().interruptState.resuming
442
350
  },
443
351
  get partial() {
444
352
  const part = activeStructuredPart()
package/src/types.ts CHANGED
@@ -168,7 +168,7 @@ interface BaseCreateChatReturn<
168
168
  * `messages[i].parts.find(p => p.type === 'structured-output')` is typed
169
169
  * with the schema's inferred shape: `data: T`, `partial: DeepPartial<T>`.
170
170
  */
171
- messages: Array<UIMessage<TTools, TData>>
171
+ messages: ReadonlyArray<UIMessage<TTools, TData>>
172
172
 
173
173
  /**
174
174
  * Send a message and get a response.
@@ -187,7 +187,7 @@ interface BaseCreateChatReturn<
187
187
  * Pending messages queued while the client is busy (streaming, claiming a
188
188
  * send, or draining). Separate from `messages` until they drain.
189
189
  */
190
- queue: Array<QueuedMessage>
190
+ queue: ReadonlyArray<QueuedMessage>
191
191
 
192
192
  /**
193
193
  * Cancel a queued message before it drains. No-op if already sent.
@@ -320,7 +320,7 @@ interface BaseCreateChatReturn<
320
320
  /**
321
321
  * Set messages manually
322
322
  */
323
- setMessages: (messages: Array<UIMessage<TTools, TData>>) => void
323
+ setMessages: (messages: ReadonlyArray<UIMessage<TTools, TData>>) => void
324
324
 
325
325
  /**
326
326
  * Clear all messages