Skip to content

FluxbaseAIChat

AI Chat client for WebSocket-based chat with AI chatbots

const chat = new FluxbaseAIChat({
wsUrl: 'ws://localhost:8080/ai/ws',
token: 'my-jwt-token',
onContent: (delta, convId) => {
process.stdout.write(delta)
},
onProgress: (step, message) => {
console.log(`[${step}] ${message}`)
},
onQueryResult: (query, summary, rowCount, data) => {
console.log(`Query: ${query}`)
console.log(`Result: ${summary} (${rowCount} rows)`)
},
onDone: (usage) => {
console.log(`\nTokens: ${usage?.total_tokens}`)
},
onError: (error, code) => {
console.error(`Error: ${error} (${code})`)
},
})
await chat.connect()
const convId = await chat.startChat('sql-assistant')
await chat.sendMessage(convId, 'Show me the top 10 users by order count')

new FluxbaseAIChat(options): FluxbaseAIChat

Parameter Type
options AIChatOptions

FluxbaseAIChat

cancel(conversationId): void

Cancel an ongoing message generation

Parameter Type Description
conversationId string Conversation ID

void


connect(): Promise<void>

Connect to the AI chat WebSocket

Promise<void>

Promise that resolves when connected


disconnect(): void

Disconnect from the AI chat WebSocket

void


getAccumulatedContent(conversationId): string

Get the full accumulated response content for a conversation

Parameter Type Description
conversationId string Conversation ID

string

Accumulated content string


isConnected(): boolean

Check if connected

boolean


sendMessage(conversationId, content, options?): void

Send a message in a conversation

Parameter Type Description
conversationId string Conversation ID
content string Message content
options? { pageContext?: string; } Optional per-message options
options.pageContext? string Page context string for page-aware chatbots. The supervisor looks up the matching PageProfile (if any) and uses it to bias routing and override per-page config. Missing or unknown values fall back to the chatbot’s global config.

void


startChat(chatbot, namespace?, conversationId?, impersonateUserId?): Promise<string>

Start a new chat session with a chatbot

Parameter Type Description
chatbot string Chatbot name
namespace? string Optional namespace. If not provided and a lookup function is available, performs smart resolution: - If exactly one chatbot with this name exists, uses it - If multiple exist, tries “default” namespace - If ambiguous and not in default, throws error with available namespaces If no lookup function, falls back to “default” namespace.
conversationId? string Optional conversation ID to resume
impersonateUserId? string Optional user ID to impersonate (admin only)

Promise<string>

Promise resolving to conversation ID