embedWidget(), and the on() method on the widget instance.
Quick Start
Event Reference
Thread Events
threadChanged
Fires when the active conversation thread changes — for example, when the user selects a different thread from the thread list, or a new thread is created after the first message is sent.
| Parameter | Type | Description |
|---|---|---|
threadId | string | The ID of the newly active thread |
newThread
Fires when the user clicks “New Chat” to start a fresh conversation. At this point no thread has been created on the backend yet — the thread is created once the first message is sent (which then fires threadChanged).
Message Events
userMessageSent
Fires when the user sends a message to the agent.
| Parameter | Type | Description |
|---|---|---|
message | string | The message content |
threadId | string | The thread the message was sent in |
generationStarted
Fires when the agent begins generating a response.
| Parameter | Type | Description |
|---|---|---|
threadId | string | The thread the response is being generated in |
messageId | string | The ID of the assistant message being generated |
generationEnded
Fires when the agent finishes generating a response. The messageId is the persisted backend ID that you can use with the Thesys API to fetch the message content.
| Parameter | Type | Description |
|---|---|---|
threadId | string | The thread the response was generated in |
messageId | string | The backend ID of the completed assistant message |
Tool Execution Events
These events fire when the agent uses a tool (web search, image search, artifact builder, etc.) during response generation. Internal tools used by the system are excluded — only user-facing tools trigger these events.toolExecutionStarted
Fires when the agent starts executing a tool.
| Parameter | Type | Description |
|---|---|---|
toolName | string | The name of the tool being executed |
threadId | string | The thread the tool is executing in |
toolExecutionEnded
Fires when a tool finishes executing. If the tool failed, the error parameter contains the error message.
| Parameter | Type | Description |
|---|---|---|
toolName | string | The name of the tool that finished |
threadId | string | The thread the tool executed in |
error | string | undefined | Error message if the tool failed, undefined on success |
Error Events
agentError
Fires when an error occurs during message processing or response streaming.
| Parameter | Type | Description |
|---|---|---|
code | string | Error code (MESSAGE_PROCESSING_ERROR or STREAM_PROCESSING_ERROR) |
message | string | Human-readable error description |
identityTokenError
Fires when a BYOI identity token is invalid or cannot be refreshed. This is separate from the normal token refresh flow — it indicates a persistent authentication failure.
| Parameter | Type | Description |
|---|---|---|
code | string | Error code identifying the failure type |
message | string | Human-readable error description |
Inline Callbacks
All events can also be passed as callbacks directly toembedWidget(). Callback names use the on prefix with PascalCase:
on() listeners fire for the same event — you can use either or both.
widget.on(event, callback)
Subscribe to events dynamically after the widget is created. Returns an unsubscribe function.
on() returns its own independent unsubscribe function.
Typical Event Flow
Here’s the sequence of events during a normal conversation turn:Direct Iframe Integration
If you’re embedding the agent via a direct iframe instead of the embed widget npm package, you can listen for the same events using the browser’spostMessage API. Each event is sent as a message with a type field prefixed with THESYS_.
Event Mapping
| Embed Widget Event | postMessage type | Payload |
|---|---|---|
threadChanged | THESYS_THREAD_CHANGED | { threadId: string } |
newThread | THESYS_NEW_THREAD | (none) |
userMessageSent | THESYS_USER_MESSAGE_SENT | { message: string, threadId: string } |
generationStarted | THESYS_GENERATION_STARTED | { threadId: string, messageId: string } |
generationEnded | THESYS_GENERATION_ENDED | { threadId: string, messageId: string } |
toolExecutionStarted | THESYS_TOOL_EXECUTION_STARTED | { toolName: string, threadId: string } |
toolExecutionEnded | THESYS_TOOL_EXECUTION_ENDED | { toolName: string, threadId: string, error?: string } |
agentError | THESYS_AGENT_ERROR | { code: string, message: string } |
identityTokenError | THESYS_IDENTITY_TOKEN_ERROR | { code: string, message: string } |
| (widget-only) | THESYS_APP_READY | (none) — the agent has finished loading |
| (widget-only) | THESYS_WIDGET_CLOSE | (none) — the agent requested to close |
| (widget-only) | THESYS_WIDGET_OPEN | (none) — the agent requested to open |
| (widget-only) | THESYS_WIDGET_TOGGLE | (none) — the agent requested to toggle visibility |
| (widget-only) | THESYS_IDENTITY_TOKEN_REFRESH_NEEDED | (none) — token expired, parent should refresh |