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. 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?stringOptional conversation ID to resume
impersonateUserId?stringOptional user ID to impersonate (admin only)

Promise<string>

Promise resolving to conversation ID