Message API
Overview
The Message API enables real-time communication with AI agents through text messages.
Methods
Send Message
typescript
async function sendMessage(message: Message): Promise<void>
1
Sends a message to the selected agent.
Parameters
message
(Message): The message object to send
Example
typescript
await client.message.sendMessage({
id: crypto.randomUUID(),
agentId: 'donald-trump',
content: 'Tell me about the quantum timeline',
timestamp: new Date(),
type: 'user'
});
1
2
3
4
5
6
7
2
3
4
5
6
7
Get History
typescript
async function getHistory(agentId: string): Promise<Message[]>
1
Retrieves conversation history with an agent.
Parameters
agentId
(string): The agent's unique identifier
Returns
Message[]
: Array of messages in chronological order
Example
typescript
const history = await client.message.getHistory('donald-trump');
1
Clear History
typescript
function clearHistory(): void
1
Clears the conversation history.
Example
typescript
client.message.clearHistory();
1
Types
Message Interface
typescript
interface Message {
id: string;
agentId: string;
content: string;
timestamp: Date;
type: 'user' | 'agent';
}
1
2
3
4
5
6
7
2
3
4
5
6
7
Error Handling
typescript
try {
await client.message.sendMessage(message);
} catch (error) {
if (error instanceof MessageDeliveryError) {
// Handle message delivery failure
} else if (error instanceof RateLimitError) {
// Handle rate limiting
} else {
// Handle other errors
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Best Practices
- Implement proper message queuing
- Handle rate limiting appropriately
- Store message history locally
- Implement retry logic for failed messages
- Monitor message delivery status