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

ParameterType
optionsAIChatOptions

FluxbaseAIChat

cancel(conversationId): void

Cancel an ongoing message generation

ParameterTypeDescription
conversationIdstringConversation 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

ParameterTypeDescription
conversationIdstringConversation ID

string

Accumulated content string


isConnected(): boolean

Check if connected

boolean


sendMessage(conversationId, content): void

Send a message in a conversation

ParameterTypeDescription
conversationIdstringConversation ID
contentstringMessage content

void


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

Start a new chat session with a chatbot

ParameterTypeDescription
chatbotstringChatbot name
namespace?stringOptional namespace (defaults to ‘default’)
conversationId?stringOptional conversation ID to resume
impersonateUserId?stringOptional user ID to impersonate (admin only)

Promise<string>

Promise resolving to conversation ID