Stream Agent Real-Time UI
User Intent
Operation
Complete Code Example (TypeScript)
import { Graphlit } from 'graphlit-client';
import { AgentStreamEvent } from 'graphlit-client/dist/generated/graphql-types';
const graphlit = new Graphlit();
// UI State
let conversationId: string | undefined;
let currentMessage = '';
let isTyping = false;
await graphlit.streamAgent(
'What are the key findings in the research papers?',
async (event: AgentStreamEvent) => {
switch (event.type) {
case 'conversation_started':
// UI: Store conversation ID, show typing indicator
conversationId = event.conversationId;
isTyping = true;
updateUI({ showTyping: true });
break;
case 'message_update':
// UI: Append text chunk to message bubble (real-time)
currentMessage += event.message.message;
updateMessageBubble(currentMessage);
if (!event.isStreaming) {
// UI: Message complete, hide typing, show final metadata
isTyping = false;
finalizeMessage({
text: currentMessage,
tokens: event.message.tokens,
model: event.message.model
});
currentMessage = ''; // Reset for next message
}
break;
case 'tool_update':
// UI: Show tool execution card with status
updateToolCard({
name: event.toolCall.name,
status: event.status, // 'preparing' | 'executing' | 'completed' | 'failed'
arguments: event.toolCall.arguments,
result: event.result,
error: event.error
});
break;
case 'reasoning_update':
// UI: Show expandable "Thinking..." section (Claude extended thinking)
updateReasoningBlock({
content: event.reasoning,
isVisible: true
});
break;
case 'conversation_completed':
// UI: Hide typing indicator, show token count badge
isTyping = false;
updateUI({
showTyping: false,
metadata: {
tokens: event.message.tokens,
model: event.message.model,
throughput: event.message.throughput
}
});
break;
case 'error':
// UI: Show error toast, enable retry button
showError({
message: event.error.message,
code: event.error.code,
recoverable: event.error.recoverable
});
if (event.error.recoverable) {
showRetryButton();
}
break;
}
},
conversationId, // Continue existing conversation
undefined, // Use default specification
[], // Tools (optional)
{} // Tool handlers (optional)
);Event Types → UI Patterns
1. conversation_started
conversation_started2. message_update
message_update3. tool_update
tool_update4. reasoning_update
reasoning_update5. conversation_completed
conversation_completed6. error
errorMulti-Turn Conversation
Tool Calling UI
Cancellation
Production Pattern (React Example)
Key Differences: streamAgent vs promptConversation
Feature
streamAgent
promptConversation
Common Issues
Last updated