Skip to content

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.

Minimal Engine

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().

Data Streaming with Native Abort

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.

My Conversations
Handled by useAiConversations
Conversation Management Hook

API Reference

useAiChat Options

PropertyDescriptionType
initialMessagesArray of default chat recordsAiChatMessage[]
requestCore response adapter: handles REST or streaming requests (AsyncGenerator) containing history and abort controller.Function
idGeneratorReplaces random ID strings or UUID.() => string
onErrorHook triggered when catching unpredictable errors(err) => void

useAiChat Returns

The state hook unpacks all data and methods automatically:

PropertyDescriptionType
messagesReturns state representation bound directly to conversations.Ref<AiChatMessage[]>
isGeneratingChecking networking actions.Ref<boolean>
sendMessageMain trigger appending custom queries to AI(content: string) => Promise<void>
stopStop streaming and invoke local network suspension.() => void
clearClears local history tracking window() => void
removeMessageSingle deletion handler(id: string) => void

Released under the MIT License.