@tanstack/ai-react 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.
@@ -1,6 +1,6 @@
1
1
  import { AnyClientTool, ModelMessage } from '@tanstack/ai';
2
- import { ChatClientOptions, ChatRequestBody, UIMessage } from '@tanstack/ai-client';
3
- export type { UIMessage, ChatRequestBody };
2
+ import { ChatClientOptions, ChatClientState, ChatRequestBody, MultimodalContent, UIMessage } from '@tanstack/ai-client';
3
+ export type { ChatRequestBody, MultimodalContent, UIMessage };
4
4
  /**
5
5
  * Options for the useChat hook.
6
6
  *
@@ -9,6 +9,7 @@ export type { UIMessage, ChatRequestBody };
9
9
  * - `onMessagesChange` - Managed by React state (exposed as `messages`)
10
10
  * - `onLoadingChange` - Managed by React state (exposed as `isLoading`)
11
11
  * - `onErrorChange` - Managed by React state (exposed as `error`)
12
+ * - `onStatusChange` - Managed by React 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 UseChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any> = Omit<ChatClientOptions<TTools>, 'onMessagesChange' | 'onLoadingChange' | 'onErrorChange'>;
20
+ export type UseChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any> = Omit<ChatClientOptions<TTools>, 'onMessagesChange' | 'onLoadingChange' | 'onErrorChange' | 'onStatusChange'>;
20
21
  export interface UseChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any> {
21
22
  /**
22
23
  * Current messages in the conversation
23
24
  */
24
25
  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
  */
@@ -63,6 +65,10 @@ export interface UseChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any
63
65
  * Current error, if any
64
66
  */
65
67
  error: Error | undefined;
68
+ /**
69
+ * Current status of the chat client
70
+ */
71
+ status: ChatClientState;
66
72
  /**
67
73
  * Set messages manually
68
74
  */
@@ -1,5 +1,5 @@
1
- import { useId, useState, useRef, useEffect, useMemo, useCallback } from "react";
2
1
  import { ChatClient } from "@tanstack/ai-client";
2
+ import { useId, useState, useRef, useEffect, useMemo, useCallback } from "react";
3
3
  function useChat(options) {
4
4
  const hookId = useId();
5
5
  const clientId = options.id || hookId;
@@ -8,6 +8,7 @@ function useChat(options) {
8
8
  );
9
9
  const [isLoading, setIsLoading] = useState(false);
10
10
  const [error, setError] = useState(void 0);
11
+ const [status, setStatus] = useState("ready");
11
12
  const messagesRef = useRef(
12
13
  options.initialMessages || []
13
14
  );
@@ -27,8 +28,12 @@ function useChat(options) {
27
28
  body: optionsRef.current.body,
28
29
  onResponse: optionsRef.current.onResponse,
29
30
  onChunk: optionsRef.current.onChunk,
30
- onFinish: optionsRef.current.onFinish,
31
- onError: optionsRef.current.onError,
31
+ onFinish: (message) => {
32
+ optionsRef.current.onFinish?.(message);
33
+ },
34
+ onError: (error2) => {
35
+ optionsRef.current.onError?.(error2);
36
+ },
32
37
  tools: optionsRef.current.tools,
33
38
  streamProcessor: options.streamProcessor,
34
39
  onMessagesChange: (newMessages) => {
@@ -39,6 +44,9 @@ function useChat(options) {
39
44
  },
40
45
  onErrorChange: (newError) => {
41
46
  setError(newError);
47
+ },
48
+ onStatusChange: (status2) => {
49
+ setStatus(status2);
42
50
  }
43
51
  });
44
52
  }, [clientId]);
@@ -104,6 +112,7 @@ function useChat(options) {
104
112
  stop,
105
113
  isLoading,
106
114
  error,
115
+ status,
107
116
  setMessages: setMessagesManually,
108
117
  clear,
109
118
  addToolResult,
@@ -1 +1 @@
1
- {"version":3,"file":"use-chat.js","sources":["../../src/use-chat.ts"],"sourcesContent":["import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport { ChatClient } from '@tanstack/ai-client'\nimport type { AnyClientTool, ModelMessage } from '@tanstack/ai'\n\nimport type { UIMessage, UseChatOptions, UseChatReturn } from './types'\n\nexport function useChat<TTools extends ReadonlyArray<AnyClientTool> = any>(\n options: UseChatOptions<TTools>,\n): UseChatReturn<TTools> {\n const hookId = useId()\n const clientId = options.id || hookId\n\n const [messages, setMessages] = useState<Array<UIMessage<TTools>>>(\n options.initialMessages || [],\n )\n const [isLoading, setIsLoading] = useState(false)\n const [error, setError] = useState<Error | undefined>(undefined)\n\n // Track current messages in a ref to preserve them when client is recreated\n const messagesRef = useRef<Array<UIMessage<TTools>>>(\n options.initialMessages || [],\n )\n const isFirstMountRef = useRef(true)\n\n // Update ref whenever messages change\n useEffect(() => {\n messagesRef.current = messages\n }, [messages])\n\n // Track current options in a ref to avoid recreating client when options change\n const optionsRef = useRef<UseChatOptions<TTools>>(options)\n optionsRef.current = options\n\n // Create ChatClient instance with callbacks to sync state\n // Note: Options are captured at client creation time.\n // The connection adapter can use functions for dynamic values (url, headers, etc.)\n // which are evaluated lazily on each request.\n const client = useMemo(() => {\n // On first mount, use initialMessages. On subsequent recreations, preserve existing messages.\n const messagesToUse = isFirstMountRef.current\n ? options.initialMessages || []\n : messagesRef.current\n\n isFirstMountRef.current = false\n\n return new ChatClient({\n connection: optionsRef.current.connection,\n id: clientId,\n initialMessages: messagesToUse,\n body: optionsRef.current.body,\n onResponse: optionsRef.current.onResponse,\n onChunk: optionsRef.current.onChunk,\n onFinish: optionsRef.current.onFinish,\n onError: optionsRef.current.onError,\n tools: optionsRef.current.tools,\n streamProcessor: options.streamProcessor,\n onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {\n setMessages(newMessages)\n },\n onLoadingChange: (newIsLoading: boolean) => {\n setIsLoading(newIsLoading)\n },\n onErrorChange: (newError: Error | undefined) => {\n setError(newError)\n },\n })\n }, [clientId])\n\n // Sync body changes to the client\n // This allows dynamic body values (like model selection) to be updated without recreating the client\n useEffect(() => {\n client.updateOptions({ body: options.body })\n }, [client, options.body])\n\n // Sync initial messages on mount only\n // Note: initialMessages are passed to ChatClient constructor, but we also\n // set them here to ensure React state is in sync\n useEffect(() => {\n if (options.initialMessages && options.initialMessages.length > 0) {\n // Only set if current messages are empty (initial state)\n if (messages.length === 0) {\n client.setMessagesManually(options.initialMessages)\n }\n }\n }, []) // Only run on mount - initialMessages are handled by ChatClient constructor\n\n // Cleanup on unmount: stop any in-flight requests\n // Note: We only cleanup when client changes or component unmounts.\n // DO NOT include isLoading in dependencies - that would cause the cleanup\n // to run when isLoading changes, aborting continuation requests.\n useEffect(() => {\n return () => {\n // Stop any active generation when component unmounts or client changes\n client.stop()\n }\n }, [client])\n\n // Note: Callback options (onResponse, onChunk, onFinish, onError, onToolCall)\n // are captured at client creation time. Changes to these callbacks require\n // remounting the component or changing the connection to recreate the client.\n\n const sendMessage = useCallback(\n async (content: string) => {\n await client.sendMessage(content)\n },\n [client],\n )\n\n const append = useCallback(\n async (message: ModelMessage | UIMessage) => {\n await client.append(message)\n },\n [client],\n )\n\n const reload = useCallback(async () => {\n await client.reload()\n }, [client])\n\n const stop = useCallback(() => {\n client.stop()\n }, [client])\n\n const clear = useCallback(() => {\n client.clear()\n }, [client])\n\n const setMessagesManually = useCallback(\n (newMessages: Array<UIMessage<TTools>>) => {\n client.setMessagesManually(newMessages)\n },\n [client],\n )\n\n const addToolResult = useCallback(\n async (result: {\n toolCallId: string\n tool: string\n output: any\n state?: 'output-available' | 'output-error'\n errorText?: string\n }) => {\n await client.addToolResult(result)\n },\n [client],\n )\n\n const addToolApprovalResponse = useCallback(\n async (response: { id: string; approved: boolean }) => {\n await client.addToolApprovalResponse(response)\n },\n [client],\n )\n\n return {\n messages,\n sendMessage,\n append,\n reload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AACrD,YAAM,OAAO,wBAAwB,QAAQ;AAAA,IAC/C;AAAA,IACA,CAAC,MAAM;AAAA,EAAA;AAGT,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,aAAa;AAAA,IACb;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
1
+ {"version":3,"file":"use-chat.js","sources":["../../src/use-chat.ts"],"sourcesContent":["import { ChatClient } from '@tanstack/ai-client'\nimport { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport type { AnyClientTool, ModelMessage } from '@tanstack/ai'\nimport type { ChatClientState } from '@tanstack/ai-client'\n\nimport type {\n MultimodalContent,\n UIMessage,\n UseChatOptions,\n UseChatReturn,\n} from './types'\n\nexport function useChat<TTools extends ReadonlyArray<AnyClientTool> = any>(\n options: UseChatOptions<TTools>,\n): UseChatReturn<TTools> {\n const hookId = useId()\n const clientId = options.id || hookId\n\n const [messages, setMessages] = useState<Array<UIMessage<TTools>>>(\n options.initialMessages || [],\n )\n const [isLoading, setIsLoading] = useState(false)\n const [error, setError] = useState<Error | undefined>(undefined)\n const [status, setStatus] = useState<ChatClientState>('ready')\n\n // Track current messages in a ref to preserve them when client is recreated\n const messagesRef = useRef<Array<UIMessage<TTools>>>(\n options.initialMessages || [],\n )\n const isFirstMountRef = useRef(true)\n\n // Update ref whenever messages change\n useEffect(() => {\n messagesRef.current = messages\n }, [messages])\n\n // Track current options in a ref to avoid recreating client when options change\n const optionsRef = useRef<UseChatOptions<TTools>>(options)\n optionsRef.current = options\n\n // Create ChatClient instance with callbacks to sync state\n // Note: Options are captured at client creation time.\n // The connection adapter can use functions for dynamic values (url, headers, etc.)\n // which are evaluated lazily on each request.\n const client = useMemo(() => {\n // On first mount, use initialMessages. On subsequent recreations, preserve existing messages.\n const messagesToUse = isFirstMountRef.current\n ? options.initialMessages || []\n : messagesRef.current\n\n isFirstMountRef.current = false\n\n return new ChatClient({\n connection: optionsRef.current.connection,\n id: clientId,\n initialMessages: messagesToUse,\n body: optionsRef.current.body,\n onResponse: optionsRef.current.onResponse,\n onChunk: optionsRef.current.onChunk,\n onFinish: (message: UIMessage<TTools>) => {\n optionsRef.current.onFinish?.(message)\n },\n onError: (error: Error) => {\n optionsRef.current.onError?.(error)\n },\n tools: optionsRef.current.tools,\n streamProcessor: options.streamProcessor,\n onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {\n setMessages(newMessages)\n },\n onLoadingChange: (newIsLoading: boolean) => {\n setIsLoading(newIsLoading)\n },\n onErrorChange: (newError: Error | undefined) => {\n setError(newError)\n },\n onStatusChange: (status: ChatClientState) => {\n setStatus(status)\n },\n })\n }, [clientId])\n\n // Sync body changes to the client\n // This allows dynamic body values (like model selection) to be updated without recreating the client\n useEffect(() => {\n client.updateOptions({ body: options.body })\n }, [client, options.body])\n\n // Sync initial messages on mount only\n // Note: initialMessages are passed to ChatClient constructor, but we also\n // set them here to ensure React state is in sync\n useEffect(() => {\n if (options.initialMessages && options.initialMessages.length > 0) {\n // Only set if current messages are empty (initial state)\n if (messages.length === 0) {\n client.setMessagesManually(options.initialMessages)\n }\n }\n }, []) // Only run on mount - initialMessages are handled by ChatClient constructor\n\n // Cleanup on unmount: stop any in-flight requests\n // Note: We only cleanup when client changes or component unmounts.\n // DO NOT include isLoading in dependencies - that would cause the cleanup\n // to run when isLoading changes, aborting continuation requests.\n useEffect(() => {\n return () => {\n // Stop any active generation when component unmounts or client changes\n client.stop()\n }\n }, [client])\n\n // Note: Callback options (onResponse, onChunk, onFinish, onError, onToolCall)\n // are captured at client creation time. Changes to these callbacks require\n // remounting the component or changing the connection to recreate the client.\n\n const sendMessage = useCallback(\n async (content: string | MultimodalContent) => {\n await client.sendMessage(content)\n },\n [client],\n )\n\n const append = useCallback(\n async (message: ModelMessage | UIMessage) => {\n await client.append(message)\n },\n [client],\n )\n\n const reload = useCallback(async () => {\n await client.reload()\n }, [client])\n\n const stop = useCallback(() => {\n client.stop()\n }, [client])\n\n const clear = useCallback(() => {\n client.clear()\n }, [client])\n\n const setMessagesManually = useCallback(\n (newMessages: Array<UIMessage<TTools>>) => {\n client.setMessagesManually(newMessages)\n },\n [client],\n )\n\n const addToolResult = useCallback(\n async (result: {\n toolCallId: string\n tool: string\n output: any\n state?: 'output-available' | 'output-error'\n errorText?: string\n }) => {\n await client.addToolResult(result)\n },\n [client],\n )\n\n const addToolApprovalResponse = useCallback(\n async (response: { id: string; approved: boolean }) => {\n await client.addToolApprovalResponse(response)\n },\n [client],\n )\n\n return {\n messages,\n sendMessage,\n append,\n reload,\n stop,\n isLoading,\n error,\n status,\n setMessages: setMessagesManually,\n clear,\n addToolResult,\n addToolApprovalResponse,\n }\n}\n"],"names":["error","status"],"mappings":";;AAYO,SAAS,QACd,SACuB;AACvB,QAAM,SAAS,MAAA;AACf,QAAM,WAAW,QAAQ,MAAM;AAE/B,QAAM,CAAC,UAAU,WAAW,IAAI;AAAA,IAC9B,QAAQ,mBAAmB,CAAA;AAAA,EAAC;AAE9B,QAAM,CAAC,WAAW,YAAY,IAAI,SAAS,KAAK;AAChD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAA4B,MAAS;AAC/D,QAAM,CAAC,QAAQ,SAAS,IAAI,SAA0B,OAAO;AAG7D,QAAM,cAAc;AAAA,IAClB,QAAQ,mBAAmB,CAAA;AAAA,EAAC;AAE9B,QAAM,kBAAkB,OAAO,IAAI;AAGnC,YAAU,MAAM;AACd,gBAAY,UAAU;AAAA,EACxB,GAAG,CAAC,QAAQ,CAAC;AAGb,QAAM,aAAa,OAA+B,OAAO;AACzD,aAAW,UAAU;AAMrB,QAAM,SAAS,QAAQ,MAAM;AAE3B,UAAM,gBAAgB,gBAAgB,UAClC,QAAQ,mBAAmB,CAAA,IAC3B,YAAY;AAEhB,oBAAgB,UAAU;AAE1B,WAAO,IAAI,WAAW;AAAA,MACpB,YAAY,WAAW,QAAQ;AAAA,MAC/B,IAAI;AAAA,M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AAAA,IAC7B;AAAA,IACA,CAAC,MAAM;AAAA,EAAA;AAGT,QAAM,SAAS,YAAY,YAAY;AACrC,UAAM,OAAO,OAAA;AAAA,EACf,GAAG,CAAC,MAAM,CAAC;AAEX,QAAM,OAAO,YAAY,MAAM;AAC7B,WAAO,KAAA;AAAA,EACT,GAAG,CAAC,MAAM,CAAC;AAEX,QAAM,QAAQ,YAAY,MAAM;AAC9B,WAAO,MAAA;AAAA,EACT,GAAG,CAAC,MAAM,CAAC;AAEX,QAAM,sBAAsB;AAAA,IAC1B,CAAC,gBAA0C;AACzC,aAAO,oBAAoB,WAAW;AAAA,IACxC;AAAA,IACA,CAAC,MAAM;AAAA,EAAA;AAGT,QAAM,gBAAgB;AAAA,IACpB,OAAO,WAMD;AACJ,YAAM,OAAO,cAAc,MAAM;AAAA,IACnC;AAAA,IACA,CAAC,MAAM;AAAA,EAAA;AAGT,QAAM,0BAA0B;AAAA,IAC9B,OAAO,aAAgD;AACrD,YAAM,OAAO,wBAAwB,QAAQ;AAAA,IAC/C;AAAA,IACA,CAAC,MAAM;AAAA,EAAA;AAGT,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,aAAa;AAAA,IACb;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ai-react",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "React hooks for TanStack AI",
5
5
  "author": "",
6
6
  "license": "MIT",
@@ -31,12 +31,12 @@
31
31
  "streaming"
32
32
  ],
33
33
  "dependencies": {
34
- "@tanstack/ai-client": "0.3.0"
34
+ "@tanstack/ai-client": "0.4.1"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "@types/react": ">=18.0.0",
38
38
  "react": ">=18.0.0",
39
- "@tanstack/ai": "^0.3.0"
39
+ "@tanstack/ai": "^0.4.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@testing-library/react": "^16.3.0",
@@ -45,7 +45,7 @@
45
45
  "jsdom": "^27.2.0",
46
46
  "react": "^19.2.3",
47
47
  "vite": "^7.2.7",
48
- "@tanstack/ai": "0.3.0"
48
+ "@tanstack/ai": "0.4.0"
49
49
  },
50
50
  "scripts": {
51
51
  "clean": "premove ./build ./dist",
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 { UIMessage, ChatRequestBody }
11
+ export type { ChatRequestBody, MultimodalContent, UIMessage }
10
12
 
11
13
  /**
12
14
  * Options for the useChat hook.
@@ -16,6 +18,7 @@ export type { UIMessage, ChatRequestBody }
16
18
  * - `onMessagesChange` - Managed by React state (exposed as `messages`)
17
19
  * - `onLoadingChange` - Managed by React state (exposed as `isLoading`)
18
20
  * - `onErrorChange` - Managed by React state (exposed as `error`)
21
+ * - `onStatusChange` - Managed by React 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.
@@ -26,7 +29,7 @@ export type { UIMessage, ChatRequestBody }
26
29
  export type UseChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any> =
27
30
  Omit<
28
31
  ChatClientOptions<TTools>,
29
- 'onMessagesChange' | 'onLoadingChange' | 'onErrorChange'
32
+ 'onMessagesChange' | 'onLoadingChange' | 'onErrorChange' | 'onStatusChange'
30
33
  >
31
34
 
32
35
  export interface UseChatReturn<
@@ -38,9 +41,10 @@ export interface UseChatReturn<
38
41
  messages: Array<UIMessage<TTools>>
39
42
 
40
43
  /**
41
- * Send a message and get a response
44
+ * Send a message and get a response.
45
+ * Can be a simple string or multimodal content with images, audio, etc.
42
46
  */
43
- sendMessage: (content: string) => Promise<void>
47
+ sendMessage: (content: string | MultimodalContent) => Promise<void>
44
48
 
45
49
  /**
46
50
  * Append a message to the conversation
@@ -86,6 +90,11 @@ export interface UseChatReturn<
86
90
  */
87
91
  error: Error | undefined
88
92
 
93
+ /**
94
+ * Current status of the chat client
95
+ */
96
+ status: ChatClientState
97
+
89
98
  /**
90
99
  * Set messages manually
91
100
  */
package/src/use-chat.ts CHANGED
@@ -1,8 +1,14 @@
1
- import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'
2
1
  import { ChatClient } from '@tanstack/ai-client'
2
+ import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'
3
3
  import type { AnyClientTool, ModelMessage } from '@tanstack/ai'
4
+ import type { ChatClientState } from '@tanstack/ai-client'
4
5
 
5
- import type { UIMessage, UseChatOptions, UseChatReturn } from './types'
6
+ import type {
7
+ MultimodalContent,
8
+ UIMessage,
9
+ UseChatOptions,
10
+ UseChatReturn,
11
+ } from './types'
6
12
 
7
13
  export function useChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
8
14
  options: UseChatOptions<TTools>,
@@ -15,6 +21,7 @@ export function useChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
15
21
  )
16
22
  const [isLoading, setIsLoading] = useState(false)
17
23
  const [error, setError] = useState<Error | undefined>(undefined)
24
+ const [status, setStatus] = useState<ChatClientState>('ready')
18
25
 
19
26
  // Track current messages in a ref to preserve them when client is recreated
20
27
  const messagesRef = useRef<Array<UIMessage<TTools>>>(
@@ -50,8 +57,12 @@ export function useChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
50
57
  body: optionsRef.current.body,
51
58
  onResponse: optionsRef.current.onResponse,
52
59
  onChunk: optionsRef.current.onChunk,
53
- onFinish: optionsRef.current.onFinish,
54
- onError: optionsRef.current.onError,
60
+ onFinish: (message: UIMessage<TTools>) => {
61
+ optionsRef.current.onFinish?.(message)
62
+ },
63
+ onError: (error: Error) => {
64
+ optionsRef.current.onError?.(error)
65
+ },
55
66
  tools: optionsRef.current.tools,
56
67
  streamProcessor: options.streamProcessor,
57
68
  onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {
@@ -63,6 +74,9 @@ export function useChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
63
74
  onErrorChange: (newError: Error | undefined) => {
64
75
  setError(newError)
65
76
  },
77
+ onStatusChange: (status: ChatClientState) => {
78
+ setStatus(status)
79
+ },
66
80
  })
67
81
  }, [clientId])
68
82
 
@@ -100,7 +114,7 @@ export function useChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
100
114
  // remounting the component or changing the connection to recreate the client.
101
115
 
102
116
  const sendMessage = useCallback(
103
- async (content: string) => {
117
+ async (content: string | MultimodalContent) => {
104
118
  await client.sendMessage(content)
105
119
  },
106
120
  [client],
@@ -160,6 +174,7 @@ export function useChat<TTools extends ReadonlyArray<AnyClientTool> = any>(
160
174
  stop,
161
175
  isLoading,
162
176
  error,
177
+ status,
163
178
  setMessages: setMessagesManually,
164
179
  clear,
165
180
  addToolResult,