Key Concepts
Understanding these core concepts will help you build effective healthcare concierge experiences with the Sunny Agents SDK. Whether you’re building provider search, appointment booking, or health information tools, these concepts form the foundation of your application.
Environments
The SDK connects to different endpoints depending on your environment:
The default websocketUrl is the staging URL. Override it for production via the websocketUrl option in createSunnyChat. Use the same environment for Sunny Central and the WebSocket URL (staging credentials with staging chat, production with production).
Conversations
A conversation is a collection of messages between a patient (or healthcare user) and the AI concierge. Each conversation has:
- ID: A unique identifier (UUID)
- Title: Optional human-readable title (can be auto-generated)
- Messages: Array of messages in chronological order
- Quick Responses: Optional suggested responses for the user
When authenticated via createSunnyChat, conversations are automatically persisted on the server.
Messages
A message represents a single exchange in a conversation. Messages have:
- ID: Unique identifier
- Role:
'user','assistant', or'system' - Text: The message content
- Created At: Timestamp
- Is Streaming: Whether the message is currently being streamed
- Output Items: Rich content items (artifacts, approval requests, etc.)
- Feedback: Optional user feedback (positive/negative)
Message Roles
- User: Messages sent by the user
- Assistant: Messages generated by the AI
- System: System-level messages (rarely used)
Artifacts
Artifacts are rich content objects delivered inline in message text via the WebSocket. Common examples include:
- Doctor Profiles: Healthcare provider information with NPI, specialty, ratings, etc.
- Structured Data: Custom data objects specific to your use case
Artifacts are delivered inline in message text via the WebSocket. The backend expands artifact tags JIT during streaming; clients receive the full artifact JSON (item_content, item_type) embedded in message text—no separate fetch required.
Authentication
The SDK supports two authentication modes, both configured through createSunnyChat:
Token Exchange
For applications with custom authentication providers. Your app provides an ID token and the SDK exchanges it for an access token:
- Your app provides an ID token via
idTokenProvider - SDK exchanges it for an access token
- Access token is used for WebSocket authentication
- Token is automatically refreshed when expired
Passwordless
Email or SMS-based authentication without passwords. The SDK renders a verification UI in the chat:
Connection Lifecycle
The SDK manages WebSocket connections automatically:
- Config fetch:
createSunnyChatcallsGET /sdk/configto retrieve auth settings - Auth activation: SDK activates the chosen auth mode (token exchange or passwordless)
- Connection: WebSocket connects when the user sends their first message
- Authentication: SDK sends
auth.upgrademessage with credentials - Messaging: Send/receive messages in real-time
- Reconnection: Automatically reconnect on failure
- Cleanup: Close connection when
chat.destroy()is called
Events
The SDK uses an event-driven architecture. Subscribe to events via chat.client to update your UI:
snapshot: Emitted when state changesconversationCreated: New conversation createdmessagesUpdated: Messages updatedstreamingDelta: Text streaming in real-timestreamingDone: Streaming completedquickResponses: Quick response suggestions available
MCP Approvals
MCP (Model Context Protocol) Approvals allow users to approve or reject tool execution requests. When the AI needs to execute a tool, it sends an approval request:
The approvalRequestId is the id field of the mcp_approval_request output item in the message’s outputItems array.
File Attachments
Send files as base64-encoded attachments:
State Management
The SDK maintains state internally. Access it via chat.client:
State updates trigger events, allowing you to keep your UI in sync.