UNPKG

@continue-reasoning/mini-agent

Version:

A platform-agnostic AI agent framework for building autonomous AI agents with tool execution capabilities

184 lines 6.22 kB
/** * @fileoverview BaseAgent Implementation * * This file provides the BaseAgent class that connects all the interfaces * and implements the core agent workflow. It coordinates between IChat, * IToolScheduler, and AgentEvent system to provide a complete agent experience. */ import { IAgent, IAgentConfig, IAgentStatus, IChat, IToolScheduler, ITokenUsage, AgentEvent, EventHandler, MessageItem, ITool } from './interfaces'; import { ILogger } from './logger'; /** * BaseAgent implementation that connects all core interfaces * * This class provides the main agent functionality by coordinating: * - IChat: For conversation management and streaming responses * - IToolScheduler: For tool execution and management * - AgentEvent: For event emission and monitoring * * The agent follows this workflow: * 1. Receive user input * 2. Send to chat for LLM processing * 3. Extract tool calls from response * 4. Execute tools via scheduler * 5. Integrate results back into conversation * 6. Emit events throughout the process * * Key features: * - Streaming-first approach for real-time responses * - Comprehensive event emission for monitoring * - Automatic tool call extraction and execution * - Proper error handling and state management * - Thread-safe operation with abort signal support * * @example * ```typescript * const agent = new BaseAgent(config, chat, toolScheduler); * agent.onEvent('logger', (event) => console.log(event)); * * const abortController = new AbortController(); * for await (const event of agent.process('Hello', 'session-1', abortController.signal)) { * console.log(event); * } * ``` */ export declare abstract class BaseAgent implements IAgent { protected agentConfig: IAgentConfig; protected chat: IChat<any>; protected toolScheduler: IToolScheduler; /** Map of event handler IDs to their handler functions */ private eventHandlers; /** Current conversation turn number, incremented for each user input */ private currentTurn; /** Flag indicating if the agent is currently processing a request */ private isRunning; /** Timestamp of the last status update */ private lastUpdateTime; /** Logger instance for this agent */ protected logger: ILogger; /** * Constructor for BaseAgent * * @param config - Agent configuration including model, working directory, etc. * @param chat - Chat instance for conversation management * @param toolScheduler - Tool scheduler for executing tool calls */ constructor(agentConfig: IAgentConfig, chat: IChat<any>, toolScheduler: IToolScheduler); registerTool(tool: ITool): void; removeTool(toolName: string): boolean; getToolList(): ITool[]; getTool(toolName: string): ITool | undefined; /** * Set up internal event handlers */ private setupEventHandlers; /** * Main processing method - handles complete conversation flow * * This is the primary entry point for processing user input. It orchestrates * the entire conversation flow including: * - User input processing * - LLM response generation (streaming) * - Tool call extraction and execution * - Result integration * - Event emission * * The method is designed to be thread-safe and respects abort signals for * graceful cancellation. * * @param userInput - The user's input text * @param sessionId - Unique identifier for this conversation session * @param abortSignal - Signal to abort the processing if needed * @returns AsyncGenerator that yields AgentEvent objects * * @example * ```typescript * const abortController = new AbortController(); * for await (const event of agent.process('Hello', 'session-1', abortController.signal)) { * if (event.type === AgentEventType.AssistantMessage) { * console.log(event.data); * } * } * ``` */ process(userInput: string, sessionId: string, abortSignal: AbortSignal): AsyncGenerator<AgentEvent>; /** * Process one turn of conversation * * This method processes a single turn of conversation, handling: * - LLM response generation (streaming) * - Tool call extraction and execution * - Event emission * * @param sessionId - Unique identifier for this conversation session * @param chatMessage - The chat message to process * @param abortSignal - Signal to abort the processing if needed * @returns AsyncGenerator that yields AgentEvent objects */ processOneTurn(sessionId: string, chatMessage: MessageItem, abortSignal: AbortSignal): AsyncGenerator<AgentEvent>; /** * Create user content from input * * Converts user input string into a ConversationContent object * with proper metadata and formatting. * * @param userInput - The user's input text * @param sessionId - Current session identifier * @returns ConversationContent object representing the user input * * @private */ private createUserContent; /** * Register event handler */ onEvent(id: string, handler: EventHandler): void; /** * Remove event handler */ offEvent(id: string): void; /** * Create and emit event */ private createEvent; /** * Create error event */ private createErrorEvent; /** * Emit event to all handlers */ private emitEvent; /** * Get the underlying chat instance */ getChat(): IChat<any>; /** * Get the tool scheduler instance */ getToolScheduler(): IToolScheduler; /** * Get current token usage */ getTokenUsage(): ITokenUsage; /** * Clear conversation history */ clearHistory(): void; /** * Set system prompt */ setSystemPrompt(systemPrompt: string): void; /** * Get current system prompt */ getSystemPrompt(): string | undefined; /** * Get current agent status */ getStatus(): IAgentStatus; /** * Generate unique prompt ID */ private generatePromptId; } //# sourceMappingURL=baseAgent.d.ts.map