/** * Agent Loop - Manual Agent Loop Control * * Provides utilities for manual control of agent execution loops. */ import type { Message } from './types'; export interface AgentLoopConfig { /** Model to use */ model: string; /** System prompt */ system?: string; /** Available tools */ tools?: Record; /** Maximum steps (default: 10) */ maxSteps?: number; /** Stop condition */ stopWhen?: StopCondition; /** On step finish callback */ onStepFinish?: (step: AgentStep) => void | Promise; /** On tool call callback (for approval) */ onToolCall?: (toolCall: ToolCallInfo) => Promise; } export interface AgentTool { description: string; parameters: any; execute: (args: any) => Promise; } export interface AgentStep { stepNumber: number; text: string; toolCalls: ToolCallInfo[]; toolResults: ToolResultInfo[]; usage: { promptTokens: number; completionTokens: number; totalTokens: number; }; finishReason: string; } export interface ToolCallInfo { toolCallId: string; toolName: string; args: any; } export interface ToolResultInfo { toolCallId: string; toolName: string; result: any; isError?: boolean; } export type StopCondition = { type: 'stepCount'; count: number; } | { type: 'noToolCalls'; } | { type: 'custom'; check: (step: AgentStep) => boolean; }; export interface AgentLoopResult { text: string; steps: AgentStep[]; totalUsage: { promptTokens: number; completionTokens: number; totalTokens: number; }; finishReason: string; } /** * Create a manual agent loop for fine-grained control. * * @example Basic usage * ```typescript * const loop = createAgentLoop({ * model: 'gpt-4o', * system: 'You are a helpful assistant', * tools: { * search: { * description: 'Search the web', * parameters: z.object({ query: z.string() }), * execute: async ({ query }) => searchWeb(query) * } * }, * maxSteps: 5 * }); * * const result = await loop.run('Find information about AI'); * ``` * * @example With approval * ```typescript * const loop = createAgentLoop({ * model: 'gpt-4o', * tools: { ... }, * onToolCall: async (toolCall) => { * const approved = await askUserForApproval(toolCall); * return approved; * } * }); * ``` * * @example Step-by-step control * ```typescript * const loop = createAgentLoop({ model: 'gpt-4o', tools: { ... } }); * * // Initialize with a prompt * loop.addMessage({ role: 'user', content: 'Hello' }); * * // Run one step at a time * while (!loop.isComplete()) { * const step = await loop.step(); * console.log('Step:', step); * * // Optionally modify messages or tools between steps * if (needsMoreContext) { * loop.addMessage({ role: 'user', content: 'Additional context...' }); * } * } * * const result = loop.getResult(); * ``` */ export declare function createAgentLoop(config: AgentLoopConfig): AgentLoop; export declare class AgentLoop { private config; private messages; private steps; private currentStep; private complete; private totalUsage; constructor(config: AgentLoopConfig); /** * Add a message to the conversation. */ addMessage(message: Message): void; /** * Get all messages in the conversation. */ getMessages(): Message[]; /** * Check if the loop is complete. */ isComplete(): boolean; /** * Run a single step of the agent loop. */ step(): Promise; /** * Run the full agent loop until completion. */ run(prompt: string): Promise; /** * Get the final result. */ getResult(): AgentLoopResult; /** * Check if the loop should stop. */ private shouldStop; /** * Reset the agent loop. */ reset(): void; } /** * Create a stop condition that stops after N steps. */ export declare function stopAfterSteps(count: number): StopCondition; /** * Create a stop condition that stops when no tool calls are made. */ export declare function stopWhenNoToolCalls(): StopCondition; /** * Create a custom stop condition. */ export declare function stopWhen(check: (step: AgentStep) => boolean): StopCondition;