SDK concepts
This guide explains the fundamental concepts behind the Kapa React SDK to help you understand how the different parts work together.
Component relationships
The SDK has the following component structure:
┌───────────────────┐
│ KapaProvider │ ◄── Configures the SDK with your integration ID
└─────────┬─────────┘
│
▼
┌───────────┐
│ useChat │ ◄── Hook that exposes SDK functionality
└─────┬─────┘
│
▼
┌───────────────────┐
│ Your Components │ ◄── Your custom UI using the SDK
└───────────────────┘
The KapaProvider wraps your application and makes functionality available
through the useChat hook, which your components use to access the
conversation and control the chat.
Question-answer lifecycle
The question-answer lifecycle represents the states a QA pair goes through when building a stateful UI:
┌── ───────────┐ ┌────────────┐ ┌────────────┐ ┌────────────┐
│ Question │────►│ Preparing │────►│ Generating │────►│ Completed │
│ submitted │ │ answer │ │ answer │ │ answer │
└─────────────┘ └────────────┘ └────────────┘ └────────────┘
Each QA pair goes through these stages, with properties changing along the way:
1. Question submitted
When a user asks a question:
- A new QA pair is added to the conversation
idisnullansweris an empty stringisFeedbackSubmissionEnabledisfalse
2. Preparing answer
When the SDK connects to the backend:
isPreparingAnswerbecomestrueisGeneratingAnswerremainsfalse- The UI can show an initial loading state
3. Generating answer
As the answer begins streaming in:
isGeneratingAnswerbecomestrueisPreparingAnswerbecomesfalseanswertext updates incrementally as chunks arrivesourcesmay be updated as citations are identifiedidremainsnull
If the user calls stopGeneration():
isGenerationAbortedimmediately becomestrueisGeneratingAnswerbecomesfalse- The answer remains in its partial state
4. Completed answer
Once generation is complete:
isGeneratingAnswerbecomesfalseidis set to a unique stringisFeedbackSubmissionEnabledbecomestrueanswercontains the complete responsesourcescontains the final list of citations
At this point, feedback UI elements can be displayed since the QA pair has an ID and feedback is enabled.
Streaming behavior
The SDK uses streaming to display answers as they're generated:
- Answers appear word-by-word in real-time
- Source citations are extracted from the text
- User can stop generation at any time with
stopGeneration()
This provides a more responsive experience compared to waiting for the entire answer to be generated.
Conversation management
The SDK maintains the entire conversation history in the conversation object,
which:
- Is an array of question-answer pairs
- Contains all questions, answers, sources, and feedback
- Provides helper methods for accessing specific QA pairs
This conversation state is managed automatically.