Skip to content

Chat Widget

There is no packaged, npm install chat widget (an embed script or web component like <vai-avatar> is for voice and avatar). What exists is a complete, working reference implementation you can read and adapt.

Try it: play.wetel.dev/tools/chat-widget/ is a running chat UI wired to a live agent, not a mockup. It is one HTML file with no build step and no framework; most integrators copy it and restyle the markup rather than writing the mutation and subscription wiring from scratch.

The widget runs the SDK flow of the Core API Flow from the browser:

  1. Configure. A panel takes the GraphQL HTTP and WebSocket endpoints, an API key and a numeric agent id. ?agentId= and ?apiKey= in the page URL pre-fill the fields.
  2. Start. sdkStart(input: { agentId }) with the X-Api-Key header returns sessionId and avatarToken. Every later call sends Authorization: Bearer <avatarToken> instead of the key.
  3. Listen. It opens one WebSocket with the graphql-transport-ws protocol, passing x-huat-platform: customer and the Authorization header in connectionParams, and subscribes to sessionEvents(sessionId). The subscription selects each event type it renders by name (AiResponseEvent, NodeExecutingEvent, NodeFailedEvent, SessionWillEndEvent, SessionEndedEvent), because a union member that is not selected never arrives. See Events & Subscriptions.
  4. Send. Each user turn is sdkSendMessage(input: { sessionId, text }). The reply arrives on the subscription, not in the mutation’s response; NodeExecutingEvents show as small progress chips while the workflow runs, and the bubble is finished when an AiResponseEvent has isFinal: true.
  5. End. On SessionEndedEvent the input is disabled and the stored session is cleared.

The widget stores sessionId and avatarToken in localStorage under one key per agent id, so a reload continues the same session. A resumed chat starts with an empty window: the query that returns a session’s message history needs a dashboard login, which a browser holding only an avatarToken does not have. If the stored session has expired, the widget starts a fresh one.

The demo calls sdkStart from the browser with the API key typed into the page. In production, your server calls sdkStart and hands only sessionId and avatarToken to the browser; the API key never reaches client code. See Authentication.

See also: Live Demos for every running example on this site, Getting Started, Agents.