useAiChat Data Responsive Stream
@yh-ui/hooks provides extremely powerful Headless Hooks Abstraction for AI workflows. The useAiChat and useAiConversations data engines completely separate visual rendering from pure data modeling.
In a traditional setup, handling AI model REST API requires maintaining Token streams, handling Stop/Abort capabilities, managing loading states, and keeping conversations synchronized.
Now, simply inject a custom request functional interceptor to useAiChat, and unlock the full-link state cycle immediately.
Basic Integration
Use simple Promises to take over the isolation state without manually doing messages.push.
Native Event Stream and Instant Abort
Most large language models utilize text/event-stream for lower Time-To-First-Byte (TTFB). Just turn your request engine into an AsyncGenerator (or yield streams). The Hook core automatically converts chunks into brilliant & silky-smooth continuous typewriter outputs!
It safely halts all subsequent fetches entirely by invoking the native browser AbortController from stop().
Conversation Sidebar Navigation (useAiConversations)
A complete ChatGPT-like client requires managing side menus. The useAiConversations quickly sets up immutable data lists for chats that securely handles state changes.
useAiConversationsAPI Reference
useAiChat Options
| Property | Description | Type |
|---|---|---|
initialMessages | Array of default chat records | AiChatMessage[] |
request | Core response adapter: handles REST or streaming requests (AsyncGenerator) containing history and abort controller. | Function |
idGenerator | Replaces random ID strings or UUID. | () => string |
onError | Hook triggered when catching unpredictable errors | (err) => void |
useAiChat Returns
The state hook unpacks all data and methods automatically:
| Property | Description | Type |
|---|---|---|
messages | Returns state representation bound directly to conversations. | Ref<AiChatMessage[]> |
isGenerating | Checking networking actions. | Ref<boolean> |
sendMessage | Main trigger appending custom queries to AI | (content: string) => Promise<void> |
stop | Stop streaming and invoke local network suspension. | () => void |
clear | Clears local history tracking window | () => void |
removeMessage | Single deletion handler | (id: string) => void |