A lightweight chain-based API for generating AG-UI protocol events with automatic state management and event subscription. Fully compliant with the AG-UI Protocol specification.
Features
🚀 Simple API: Easy-to-use chain-based interface
🔄 Automatic State Management: Automatically handles event start/end states
📡 Event Subscription: Subscribe to events for real-time processing
🛠️ Tool Call Support: Full support for tool call workflows
🎯 TypeScript Support: Full TypeScript support with type safety
📦 Lightweight: Minimal dependencies and bundle size
✅ AG-UI Compliant: Complete implementation of the AG-UI Protocol specification
⏰ Timestamp Support: Automatic timestamp generation for all events
🔗 Raw Event Support: Integration with external systems via raw events
const chain = new AguiChain()
.subscribe(event => {
// Send event via SSE
sseClient.send(event);
});
// Handle external stream data
res.on('data', data => {
if (data.type === 'text') {
chain.text(data.content);
} else if (data.type === 'thinking') {
chain.think(data.content);
}
});
res.on('end', () => {
chain.end();
});
API Reference
Constructor
new AguiChain(options?: RunOptions)
Options:
threadId?: string - Thread identifier
runId?: string - Run identifier
Core Methods
Event Subscription
// Subscribe to events
chain.subscribe(callback: (event: AguiEvent) => void)
// Unsubscribe from events
chain.unsubscribe(callback: (event: AguiEvent) => void)
Text Events
// Add text content with default role (assistant)
chain.text(content: string)
// Add text content with custom role
chain.text(content: string, role: string)
Parameters:
content: string - The text content to add
role?: string - The role of the message (optional, defaults to ‘assistant’)
// Add state snapshot
chain.stateSnapshot(snapshot: object)
// Add state delta
chain.stateDelta(delta: object[])
// Add messages snapshot
chain.messagesSnapshot(messages: object[])
Raw Events
// Add raw event
chain.raw(event: object, source?: string)
Control Methods
// End all current modes and finish chain
chain.end()
// Manually send RUN_STARTED event
chain.runStarted()
// Manually send RUN_FINISHED event
chain.runFinished()
// Set or update run options
chain.setRunOptions(options: RunOptions)
agui-chain
English | 简体中文
A lightweight chain-based API for generating AG-UI protocol events with automatic state management and event subscription. Fully compliant with the AG-UI Protocol specification.
Features
AG-UI Protocol Compliance
This library implements the complete AG-UI Protocol specification, including:
✅ Supported Event Types
RUN_STARTED,RUN_FINISHED,RUN_ERROR,STEP_STARTED,STEP_FINISHEDTEXT_MESSAGE_START,TEXT_MESSAGE_CONTENT,TEXT_MESSAGE_ENDTOOL_CALL_START,TOOL_CALL_ARGS,TOOL_CALL_END,TOOL_CALL_RESULTSTATE_SNAPSHOT,STATE_DELTA,MESSAGES_SNAPSHOTRAW,CUSTOM✅ Base Event Properties
All events automatically include:
type: Event type identifiertimestamp: Automatic timestamp generationrawEvent: Support for preserving original event data✅ Event Flow Patterns
Supports all AG-UI event flow patterns:
Installation
Quick Start
Basic Usage
Streaming from External Source
API Reference
Constructor
Options:
threadId?: string- Thread identifierrunId?: string- Run identifierCore Methods
Event Subscription
Text Events
Parameters:
content: string- The text content to addrole?: string- The role of the message (optional, defaults to ‘assistant’)Supported Roles:
'assistant'- Assistant role (default)'user'- User role'system'- System role'tool'- Tool roleExample:
Thinking Events
Tool Call Events
Custom Events
Error Events
Step Events
State Events
Raw Events
Control Methods
Examples
Basic Text and Thinking
Role-Based Text Messages
Tool Call Workflow
AG-UI Protocol Compliance
Event Flow Patterns
Raw Events for External Integration
Multiple Subscribers
Real-world Streaming
TypeScript Types
Event Types
The library generates events according to the AG-UI Protocol specification:
Lifecycle Events
RUN_STARTED- Run started eventRUN_FINISHED- Run finished eventRUN_ERROR- Run error eventSTEP_STARTED- Step startedSTEP_FINISHED- Step finishedText Message Events
TEXT_MESSAGE_START- Text message startedTEXT_MESSAGE_CONTENT- Text message contentTEXT_MESSAGE_END- Text message endedTool Call Events
TOOL_CALL_START- Tool call startedTOOL_CALL_ARGS- Tool call argumentsTOOL_CALL_END- Tool call endedTOOL_CALL_RESULT- Tool call resultState Management Events
STATE_SNAPSHOT- State snapshotSTATE_DELTA- State deltaMESSAGES_SNAPSHOT- Messages snapshotSpecial Events
RAW- Raw eventCUSTOM- Custom eventContributing
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)License
This project is licensed under the MIT License - see the LICENSE file for details.