Agent (LLM)
The Agent
class is the core abstraction for AI conversation handling in Micdrop. It provides a standardized interface for integrating various AI providers into real-time voice conversations.
Available Implementations
- OpenaiAgent from @micdrop/openai
- MistralAgent from @micdrop/mistral
- MockAgent for testing
Overview
The Agent
class is an abstract base class that extends EventEmitter
(from eventemitter3
) and manages:
- Conversation history with role-based messages (system, user, assistant)
- Streaming response generation with both stream and promise interfaces
- Event emission for conversation state changes
- Cancellation and cleanup mechanisms
- Integration with logging systems
export abstract class Agent<
Options extends AgentOptions = AgentOptions,
> extends EventEmitter<AgentEvents> {
public logger?: Logger
public conversation: MicdropConversation
constructor(protected options: Options)
// Generate a streaming response with both stream and promise interfaces
abstract answer(): Readable
// Cancel the current answer generation process
abstract cancel(): void
}
Architecture
Core Interfaces
export interface AgentOptions {
systemPrompt: string
}
export interface AgentEvents {
Message: [MicdropConversationMessage]
CancelLastUserMessage: []
CancelLastAssistantMessage: []
SkipAnswer: []
EndCall: []
}
export interface MicdropConversationMessage {
role: 'system' | 'user' | 'assistant'
content: string
metadata?: any
}
Conversation Management
The Agent maintains a conversation history as an array of messages:
public conversation: MicdropConversation
The conversation is initialized with the system prompt:
constructor(protected options: Options) {
super()
this.conversation = [{ role: 'system', content: options.systemPrompt }]
}
Abstract Methods
answer(): Readable
Generates a streaming response based on the current conversation history.
Return Value: A Readable
stream that emits text chunks in real-time
Implementation Requirements:
- Handle cancellation via the
cancel()
method - Add the complete response to conversation history when finished with
addAssistantMessage()
- Emit appropriate events during processing
cancel(): void
Cancels the current answer generation process. Should:
- Abort any ongoing API requests
- Clean up resources
- Stop the answer stream
Public Methods
addUserMessage(text: string)
Adds a user message to the conversation history and emits a Message
event.
agent.addUserMessage('Hello, how are you?')
addAssistantMessage(text: string)
Adds an assistant message to the conversation history and emits a Message
event.
agent.addAssistantMessage("I'm doing well, thank you!")
destroy()
Cleans up the agent instance:
- Removes all event listeners
- Calls
cancel()
to stop ongoing operations - Logs destruction
agent.destroy()
Protected Methods
addMessage(role: 'user' | 'assistant' | 'system', text: string)
Internal method to add any type of message to the conversation and emit the Message
event.
endCall()
Emits the EndCall
event to signal that the conversation should end.
cancelLastUserMessage()
Removes the last user message from the conversation history (if it exists) and emits the CancelLastUserMessage
event.
cancelLastAssistantMessage()
Removes the last assistant message from the conversation history (if it exists) and emits the CancelLastAssistantMessage
event.
skipAnswer()
Emits the SkipAnswer
event to indicate the agent is skipping the current response.
log(...message: any[])
Logs messages using the attached logger if available.
Events
The Agent emits the following events:
Message
Emitted when a new message is added to the conversation.
agent.on('Message', (message: MicdropConversationMessage) => {
console.log(`New ${message.role} message: ${message.content}`)
})
CancelLastUserMessage
Emitted when the last user message is cancelled.
agent.on('CancelLastUserMessage', () => {
console.log('Last user message was cancelled')
})
CancelLastAssistantMessage
Emitted when the last assistant message is cancelled.
agent.on('CancelLastAssistantMessage', () => {
console.log('Last assistant message was cancelled')
})