t-agent Developer Documentation
Overview
t-agent is a conversational agent SDK built with TypeScript for creating feature-rich conversational applications. It supports plugin extensions and is a pure SDK without UI components, allowing it to work with any framework.
t-agent features powerful Hook mechanisms, error handling, message state management, and other functionalities, providing an excellent development experience.
For more detailed information, please visit the official documentation.
Core Concepts
ChatAgent
ChatAgent is the core class of the entire conversation system, responsible for handling conversation lifecycle, message creation, persistence, and other core functions, and supports extending functionality through plugins and hooks.
Main Properties:
agent.session: Stores session dataagent.plugins: Contains methods and hooks from applied plugins
Core Methods:
agent.start(): Activates the conversation agentagent.dispose(): Deactivates the conversation agentagent.pushInputBlocks(): Pushes user messages to the ChatAgentagent.createMessage(): Creates a messageagent.emitTileEvent(): Sends a tile eventagent.removeMessage(): Removes a messageagent.flushStreamToShow(): Streams updates to message
Lifecycle Hooks
ChatAgent implements event-driven behavior patterns through a hook system. Hooks execute in the following order:
Main hooks include:
onAgentStart: Triggered when the agent initializesonChatStart: Triggered when a conversation startsonChatResume: Triggered when a conversation continuesonMessageListInit: Triggered when the message list initializesonInputBlocksPush: Triggered when input message blocks are receivedonMessageChange: Triggered when a message updatesonMessagePersist: Triggered when a message is persistedonTileEvent: Called when a message block event is triggeredonAgentDispose: Triggered when the agent is destroyedonUserAbort: Triggered when the user terminates the conversationonError: Triggered when an error occurs
Enhanced Hooks
onMessageFeedback: Triggered when message feedback is receivedonClearHistory: Triggered when clearing history
Usage example:
ChatSession
ChatSession holds the message list and session-related data, created along with the ChatAgent.
Main Properties:
session.messages: Message listsession.sessionId: Unique session IDsession.isNewChat: Distinguishes between new sessions and continued sessions
Main Methods:
session.set/get: Sets/gets session datasession.getData: Gets all session datasession.getLatestMessage: Gets the latest message
Hooks:
session.onChange: Registers callbacks for session data changes
ChatMessage
ChatMessage is an abstract representation of conversation content and state, composed of multiple ChatTiles.
Main Properties:
message.id: Unique message identifiermessage.role: Message role ('user'/'assistant')message.status: Message status (ChatMessageStatusenum)message.tiles: List of tiles contained in the messagemessage.bubble: Quick access to the main bubble tilemessage.meta: Additional metadata attached to the messagemessage.isShow: Whether it has been displayed on the interface
Main Methods:
message.show/update/remove: Controls message display statemessage.persist: Persists the messagemessage.addTile/removeTile: Manages message blocksmessage.setTilesLocked: Sets the locked state of all tiles in the messagemessage.set: Sets message propertiesmessage.setMetaValue: Sets meta properties by key-valuemessage.deleteMetaValue: Deletes meta propertiesmessage.setMeta: Directly sets meta objectmessage.findTileByType: Finds tiles by tile type
ChatTile
ChatTile is a visual element in a message, which can be text, images, videos, or other different types.
Main Properties:
tile.id: Unique identifiertile.type: Tile typetile.data: Tile datatile.children: List of child tilestile.locked: Whether it's lockedtile.fallback: Fallback content when tile cannot be displayedtile.message: Reference to the message it belongs to
Main Methods:
tile.update: Shortcut totile.message.updatetile.show: Shortcut totile.message.showtile.setLocked: Sets the locked statetile.addTile: Adds child tilestile.setData: Sets tile datatile.setFallback: Sets fallback display contenttile.findByType: Finds child tiles by tile type
Bubble Message Shortcuts
message.bubble is a shortcut that quickly adds a bubble tile to the current message when accessed:
For bubble messages, additional methods are provided:
message.bubble.text: Read bubble textmessage.bubble.setText: Set bubble textmessage.bubble.isMarkdown: Whether it's markdown formatmessage.bubble.setIsMarkdown: Set whether it's markdown formatmessage.bubble.status: Bubble statusmessage.bubble.setStatus: Set bubble statusmessage.bubble.info: Bubble informationmessage.bubble.setInfo: Set bubble informationmessage.bubble.initWithInputBlocks: Initialize bubble with input blocks
Plugin System
t-agent supports extending functionality through plugins. Plugins are implemented based on the Hook mechanism, and plugins can expose methods and properties for developers to use.
Common plugins include:
withUI(): Provides UI-related functionality and message buswithDebug(): Provides debugging functionalitywithAIStream(): Connects to the mini-program AI agent platformwithBuildIn(): Provides built-in functionality
Error Handling
Enhanced error handling mechanism with support for more detailed error information and error classification:
Built-in supported error types:
network-offline: Network is disconnectedtimeout: Send timeoutinvalid-params: Invalid parameterssession-create-failed: Connection failedconnection-closed: Connection closed
Built-in Plugins
withUI
The withUI plugin provides UI-related functionality and message bus capabilities. It is a required plugin that must be the first plugin applied.
Main features:
- Message bus system
- UI state management
- Event handling
- Context management
withDebug
The withDebug plugin provides debugging functionality, printing detailed logs in the console to help developers debug.
Features:
- Hook execution logs
- Message state change logs
- Error logs
- Performance monitoring
withAIStream
The withAIStream plugin connects to the mini-program AI agent platform, providing streaming conversation capabilities.
withBuildIn
The withBuildIn plugin provides built-in functionality, including smart home control, knowledge base search, etc.
Summary
t-agent is a flexible conversation agent SDK that can build rich conversational interaction experiences through its core concepts and hook system. Its plugin system allows for flexible extension of functionality to meet the needs of different scenarios. Developers can use t-agent to quickly build conversational applications with modern UI and powerful functionality, such as chatbots, customer service systems, or AI assistants.
For more detailed information and API documentation, please visit the official documentation.