feat: status property (ready, submitted, streaming error) for useChat #247
Mergednikas-belogolov(GitHub profile, opens in a new tab)merged 17 commits into
main fromrefs/pull/247/headmerged by AlemTuzlak View on GitHub- packages/typescript/ai-client/src/types.ts lines 203 to 206OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -191,3 +200,7 @@191200 */192201 onErrorChange?: (error: Error | undefined) => void193202203+ /**204+ * Callback when stream starts205+ */206+ onStreamStart?: () => voidpackages/typescript/ai-svelte/src/create-chat.svelte.ts lines 1 to 2OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1,2 +1,2 @@1-import { ChatClient } from '@tanstack/ai-client'21 import type { AnyClientTool, ModelMessage } from '@tanstack/ai'2+import { ChatClient, ChatClientState } from '@tanstack/ai-client'packages/typescript/ai-svelte/src/create-chat.svelte.ts lines 78 to 80OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -58,8 +60,21 @@60+ },61+ onError: (err) => {62+ status = 'error'63+ options.onError?.(err)64+ },5865 tools: options.tools,5966 streamProcessor: options.streamProcessor,6067 onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {6168 messages = newMessages6269 },6370 onLoadingChange: (newIsLoading: boolean) => {6471 isLoading = newIsLoading72+ if (newIsLoading) {73+ status = 'submitted'74+ } else {75+ status = error ? 'error' : 'ready'76+ }77+ },78+ onStreamStart: () => {79+ status = 'streaming'6580 },packages/typescript/ai-vue/src/use-chat.ts lines 48 to 50OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -28,8 +30,21 @@30+ },31+ onError: (err) => {32+ status.value = 'error'33+ options.onError?.(err)34+ },2835 tools: options.tools,2936 streamProcessor: options.streamProcessor,3037 onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {3138 messages.value = newMessages3239 },3340 onLoadingChange: (newIsLoading: boolean) => {3441 isLoading.value = newIsLoading42+ if (newIsLoading) {43+ status.value = 'submitted'44+ } else {45+ status.value = error.value ? 'error' : 'ready'46+ }47+ },48+ onStreamStart: () => {49+ status.value = 'streaming'3550 },
- packages/typescript/ai-preact/src/use-chat.ts lines 76 to 78OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -56,8 +58,21 @@58+ },59+ onError: (err) => {60+ setStatus('error')61+ optionsRef.current.onError?.(err)62+ },5663 tools: optionsRef.current.tools,5764 streamProcessor: options.streamProcessor,5865 onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {5966 setMessages(newMessages)6067 },6168 onLoadingChange: (newIsLoading: boolean) => {6269 setIsLoading(newIsLoading)70+ if (newIsLoading) {71+ setStatus('submitted')72+ } else {73+ setStatus((prev) => (prev === 'error' ? 'error' : 'ready'))74+ }75+ },76+ onStreamStart: () => {77+ setStatus('streaming')6378 },packages/typescript/ai-react/src/use-chat.ts lines 54 to 56OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -50,5 +51,6 @@5051 body: optionsRef.current.body,5152 onResponse: optionsRef.current.onResponse,5253 onChunk: optionsRef.current.onChunk,53- onFinish: optionsRef.current.onFinish,54- onError: optionsRef.current.onError,54+ onStreamStart: () => {55+ setStatus("streaming")56+ },packages/typescript/ai-solid/src/use-chat.ts lines 58 to 60OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -37,8 +40,21 @@40+ },41+ onError: (err) => {42+ setStatus('error')43+ options.onError?.(err)44+ },3745 tools: options.tools,3846 streamProcessor: options.streamProcessor,3947 onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {4048 setMessages(newMessages)4149 },4250 onLoadingChange: (newIsLoading: boolean) => {4351 setIsLoading(newIsLoading)52+ if (newIsLoading) {53+ setStatus('submitted')54+ } else {55+ setStatus((prev) => (prev === 'error' ? 'error' : 'ready'))56+ }57+ },58+ onStreamStart: () => {59+ setStatus('streaming')4460 },packages/typescript/ai-vue/src/use-chat.ts lines 3 to 5OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -1,4 +1,4 @@1-import { ChatClient } from '@tanstack/ai-client'2-import { onScopeDispose, readonly, shallowRef, useId } from 'vue'31 import type { AnyClientTool, ModelMessage } from '@tanstack/ai'2+import { ChatClient, type ChatClientState } from '@tanstack/ai-client'3+import { onScopeDispose, readonly, shallowRef, useId } from 'vue'44 import type { UIMessage, UseChatOptions, UseChatReturn } from './types'
- packages/typescript/ai-react/src/use-chat.ts lines 1 to 4OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -1,3 +1,4 @@1-import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'2-import { ChatClient } from '@tanstack/ai-client'31 import type { AnyClientTool, ModelMessage } from '@tanstack/ai'2+import { ChatClient } from '@tanstack/ai-client'3+import type { ChatClientState } from '@tanstack/ai-client'4+import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'packages/typescript/ai-vue/src/use-chat.ts lines 1 to 4OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1,2 +1,4 @@1+import type { AnyClientTool, ModelMessage } from '@tanstack/ai'12 import { ChatClient } from '@tanstack/ai-client'3+import type { ChatClientState } from '@tanstack/ai-client'24 import { onScopeDispose, readonly, shallowRef, useId } from 'vue'
AlemTuzlak reviewed
packages/typescript/ai-react/src/use-chat.ts lines 73 to 77OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -55,7 +57,21 @@57+ },58+ onFinish: (message: UIMessage<TTools>) => {59+ setStatus('ready')60+ optionsRef.current.onFinish?.(message)61+ },62+ onError: (error: Error) => {63+ setStatus('error')64+ optionsRef.current.onError?.(error)65+ },5566 tools: optionsRef.current.tools,5667 streamProcessor: options.streamProcessor,5768 onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {5869 setMessages(newMessages)5970 },6071 onLoadingChange: (newIsLoading: boolean) => {6172 setIsLoading(newIsLoading)73+ if (newIsLoading) {74+ setStatus('submitted')75+ } else {76+ setStatus((prev) => (prev === 'error' ? 'error' : 'ready'))77+ }AlemTuzlak requested changes
- packages/typescript/ai-vue/tests/test-utils.ts lines 1 to 5Resolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -1,5 +1,5 @@1-import { defineComponent } from 'vue'1+import type { UIMessage } from '@tanstack/ai-client'22 import { mount } from '@vue/test-utils'3-import { useChat } from '../src/use-chat'3+import { defineComponent } from 'vue'44 import type { UseChatOptions } from '../src/types'5-import type { UIMessage } from '@tanstack/ai-client'5+import { useChat } from '../src/use-chat'
- packages/typescript/ai-preact/tests/use-chat.test.ts lines 1 to 10Resolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -1,8 +1,10 @@1-import { describe, expect, it, vi } from 'vitest'1+import type { ModelMessage } from '@tanstack/ai'22 import { act, waitFor } from '@testing-library/preact'3+import { describe, expect, it, vi } from 'vitest'4+import type { UIMessage } from '../src/types'35 import {46 createMockConnectionAdapter,57 createTextChunks,68 createToolCallChunks,79 renderUseChat,810 } from './test-utils'packages/typescript/ai-vue/tests/use-chat.test.ts lines 1 to 10Resolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -1,8 +1,10 @@1-import { describe, expect, it, vi } from 'vitest'1+import type { ModelMessage } from '@tanstack/ai'22 import { flushPromises } from '@vue/test-utils'3+import { describe, expect, it, vi } from 'vitest'4+import type { UIMessage } from '../src/types'35 import {46 createMockConnectionAdapter,57 createTextChunks,68 createToolCallChunks,79 renderUseChat,810 } from './test-utils'
AlemTuzlak reviewed
packages/typescript/ai-client/src/chat-client.ts lines 205 to 207OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -187,7 +193,15 @@187193 this.events.loadingChanged(isLoading)188194 }189195196+ private setStatus(status: ChatClientState): void {197+ this.status = status198+ this.callbacksRef.current.onStatusChange(status)199+ }200+190201 private setError(error: Error | undefined): void {191202 this.error = error192203 this.callbacksRef.current.onErrorChange(error)193204 this.events.errorChanged(error?.message || null)205+ if (error) {206+ this.setStatus('error')207+ }AlemTuzlak approved these changes
- packages/typescript/ai-client/src/chat-client.ts lines 88 to 91Resolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -75,11 +79,13 @@7579 this.callbacksRef.current.onMessagesChange(messages)7680 },7781 onStreamStart: () => {78- // Stream started82+ this.setStatus('streaming')7983 },8084 onStreamEnd: (message: UIMessage) => {8185 this.callbacksRef.current.onFinish(message)86+ this.setStatus('ready')8287 },8388 onError: (error: Error) => {8489 this.setError(error)90+ this.setStatus('error')8591 this.callbacksRef.current.onError(error)
- packages/typescript/ai-preact/tests/use-chat.test.ts lines 566 to 597Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -561,0 +630,21 @@630+ // act(() => {631+ // sendPromise = result.current.sendMessage('Test')632+ // })633+634+ // await waitFor(() => {635+ // expect(result.current.status).not.toBe('ready')636+ // })637+638+ // act(() => {639+ // result.current.stop()640+ // })641+642+ // await waitFor(() => {643+ // expect(result.current.status).toBe('ready')644+ // })645+646+ // await act(async () => {647+ // await sendPromise!.catch(() => {})648+ // })649+ // })650+ })
AlemTuzlak reviewed
packages/typescript/ai-client/tests/chat-client.test.ts line 395OutdatedResolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -392,4 +393,3 @@392393 })393394394395 describe('status', () => {395- it('should have initial status of ready', () => {- packages/typescript/ai-solid/src/use-chat.ts lines 52 to 54Resolvedby nikas-belogolov(GitHub profile, opens in a new tab)@@ -42,3 +49,6 @@4249 onLoadingChange: (newIsLoading: boolean) => {4350 setIsLoading(newIsLoading)4451 },52+ onStatusChange: (newStatus: ChatClientState) => {53+ setStatus(newStatus)54+ },
AlemTuzlak merged this99ccee5
AlemTuzlak closed this
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
Adds a ChatClientState lifecycle type ('ready' | 'submitted' | 'streaming' | 'error'); exposes a new
statusfield across useChat/createChat APIs; implements client-side status tracking withonStatusChangeandgetStatus()on ChatClient; updates tests and test-utils to assert status transitions.Changes#
\.changeset/shy-ravens-sink.mdstatusproperty onuseChat.packages/typescript/ai-client/src/chat-client.ts,packages/typescript/ai-client/src/types.ts,packages/typescript/ai-client/src/index.tsChatClientStatetype; addonStatusChange?: (status: ChatClientState) => voidto options; add privatestatus+setStatus(); emit transitions (submitted,streaming,ready,error); exposegetStatus().packages/typescript/*/src/*types.ts,packages/typescript/*/src/*use-chat*.ts,packages/typescript/*/src/create-chat.svelte.tsChatClientState; omitonStatusChangefrom public option mappings; wire clientonStatusChangeto framework state; exposestatusin UseChat/CreateChat return shapes; wrap onFinish/onError to preserve callbacks.packages/typescript/ai-preact/src/types.ts,packages/typescript/ai-preact/src/use-chat.ts,packages/typescript/ai-preact/tests/use-chat.test.tsstatus: ChatClientStateto return type; hook subscribes toonStatusChange; tests assert lifecycle transitions.packages/typescript/ai-react/src/types.ts,packages/typescript/ai-react/src/use-chat.ts,packages/typescript/ai-react/tests/use-chat.test.tsstatus: ChatClientStateto return type; hook subscribes toonStatusChange; tests updated to assert transitions.packages/typescript/ai-svelte/src/types.ts,packages/typescript/ai-svelte/src/create-chat.svelte.ts,packages/typescript/ai-svelte/tests/use-chat.test.tsstatusinitialized to'ready'; exposeget status()on returned chat object; types and tests updated.packages/typescript/ai-solid/src/types.ts,packages/typescript/ai-solid/src/use-chat.ts,packages/typescript/ai-solid/tests/*,packages/typescript/ai-solid/tests/test-utils.tsstatus: Accessor<ChatClientState>; update hook, test-utils, and tests to usestatus().packages/typescript/ai-vue/src/types.ts,packages/typescript/ai-vue/src/use-chat.ts,packages/typescript/ai-vue/tests/*,packages/typescript/ai-vue/tests/test-utils.tsstatusexposed as readonly/DeepReadonly; wireonStatusChange; update types, test-utils, and tests to assert transitions.packages/typescript/*/tests/*use-chat.test.ts,packages/typescript/ai-client/tests/chat-client.test.ts,packages/typescript/*/tests/test-utils.ts'ready', transitions (submitted/streaming→ready), error state, and stop/reset behavior; test-utils exposestatuswhere appropriate.Sequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs#
statusthrough that integration.Suggested reviewers#
Poem#
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Tip
🧪 Unit Test Generation v2 is now available!
We have significantly improved our unit test generation capabilities.
To enable: Add this to your
.coderabbit.yamlconfiguration:Try it out by using the
@coderabbitai generate unit testscommand on your code files or under ✨ Finishing Touches on the walkthrough!Have feedback? Share your thoughts on our Discord thread!
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share