Import
Usage
Options
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
availableActions | ActionDefinition[] | Yes | — | Actions the AI can perform |
getContext | () => object | No | () => ({}) | Returns current app state |
onAction | (action: ToolAction) => void | No | — | Called when action executes |
tokenEndpoint | string | No | /api/mwai/token | Token generation endpoint |
endUserId | string | No | Auto-generated | User identifier |
escalationMessage | string | No | Default message | Custom message when escalating to support |
onError | (error: Error) => void | No | — | Error callback |
Return Value
Chat State
| Property | Type | Description |
|---|---|---|
messages | Message[] | All messages in the conversation |
status | "ready" | "streaming" | "submitted" | "error" | Current chat state |
error | Error | null | Last error if any |
Input Handling
| Property | Type | Description |
|---|---|---|
input | string | Current input value |
setInput | (value: string) => void | Update input value |
handleSubmit | (e: FormEvent) => void | Form submit handler |
sendMessage | (text: string) => void | Send message programmatically |
Token State
| Property | Type | Description |
|---|---|---|
isReady | boolean | Token loaded and ready |
tokenError | Error | null | Token fetch error |
refreshToken | () => Promise<void> | Manually refresh token |
Approval
| Property | Type | Description |
|---|---|---|
addToolApprovalResponse | (toolCallId: string, approved: boolean) => void | Respond to approval request |
Examples
Basic Usage
With Context
With Custom Token Endpoint
Handling Errors
Programmatic Messages
Status Values
| Status | Description |
|---|---|
ready | Waiting for user input |
submitted | Message sent, waiting for response |
streaming | Receiving streamed response |
error | An error occurred |