Skip to content

Commit 85d85fc

Browse files
Copilothotlong
andcommitted
Refine: wrap API mode functions in useCallback, improve comment clarity
Agent-Logs-Url: https://github.com/objectstack-ai/objectui/sessions/cbd1330e-479e-448d-a9b6-9e34df3edfb5 Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent db97787 commit 85d85fc

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

packages/plugin-chatbot/src/useObjectChat.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,10 @@ export function useObjectChat(options: UseObjectChatOptions = {}): UseObjectChat
172172
content: msg.content,
173173
}));
174174

175-
// --- @ai-sdk/react useChat (always called, but only active in API mode) ---
175+
// --- @ai-sdk/react useChat (always called to satisfy Rules of Hooks, but only active in API mode) ---
176+
// When in local mode, useChat is initialized with minimal config and its results are ignored.
176177
const chatResult = useChat({
177-
api: isApiMode ? api! : '/noop',
178+
api: isApiMode ? api! : '/api/noop',
178179
initialMessages: isApiMode && aiInitialMessages.length > 0 ? aiInitialMessages : undefined,
179180
headers: isApiMode ? {
180181
...headers,
@@ -232,7 +233,7 @@ export function useObjectChat(options: UseObjectChatOptions = {}): UseObjectChat
232233
streaming: isLoading && msg.id === aiMessages[aiMessages.length - 1]?.id && msg.role === 'assistant',
233234
}));
234235

235-
const sendMessage = (content: string) => {
236+
const sendMessage = useCallback((content: string) => {
236237
const trimmed = content.trim();
237238
if (!trimmed) return;
238239

@@ -251,11 +252,11 @@ export function useObjectChat(options: UseObjectChatOptions = {}): UseObjectChat
251252

252253
append(newMsg);
253254
onSend?.(trimmed, nextMessages);
254-
};
255+
}, [append, onSend, messages]);
255256

256-
const clear = () => {
257+
const clear = useCallback(() => {
257258
setMessages([]);
258-
};
259+
}, [setMessages]);
259260

260261
return {
261262
messages,

0 commit comments

Comments
 (0)