How It Works
Your application owns any mapping between its resources and Caylex session IDs. Caylex only needs the Caylex session ID.Choose The Initial Session
PassinitialSessionId when your application should control which conversation opens:
initialSessionId is an initialization-time prop. To switch sessions after the widget has loaded, use the widget’s built-in session menu.
If the supplied session ID cannot be restored, the widget calls
onError. It does not silently create a replacement session.Observe Session Changes
The widget provides two session callbacks:sessionType is either:
chat— a normal interactive chat sessionbackground_task— a background-task transcript or approval view
onSessionCreated first, followed by onSessionChanged. Restoring an existing session triggers only onSessionChanged.
The change callback also runs when the user switches between a normal chat and a background-task view, even when both views reference the same session ID.
Example
The following example lets the host application restore a mapped session or explicitly request a new one:null rather than omitting the prop. This guarantees that the widget creates a new session instead of restoring an unrelated browser session.
Programmatic Chat Turns
Your backend can send a normal user turn to an existing widget session. The message behaves like a turn typed in the composer: Caylex appends it to the transcript, runs the agent, stores the response, and returns the completed turn. Enable external synchronization on widgets that need to display these turns:onSessionCreated, store them in your backend, and call the platform-authenticated session-message API from your backend:
Background Task Sessions
The chat widget can also display background agent tasks. When a user opens a background task or approval,onSessionChanged reports sessionType: "background_task". When the user returns to an interactive chat, it reports sessionType: "chat".
This allows the host application to keep its own UI synchronized with the conversation currently displayed inside the widget.