| 28 | } |
| 29 | |
| 30 | export class MockAgent { |
| 31 | public messages: Message[] = []; |
| 32 | public state: Record<string, any> = {}; |
| 33 | public agentId?: string; |
| 34 | public threadId?: string; |
| 35 | public addMessages = vi.fn((messages: Message[]) => { |
| 36 | this.messages.push(...messages); |
| 37 | }); |
| 38 | public addMessage = vi.fn((message: Message) => { |
| 39 | this.messages.push(message); |
| 40 | // Also track on parent if this is a clone |
| 41 | if (this._parentAgent) { |
| 42 | this._parentAgent.addMessage(message); |
| 43 | } |
| 44 | }); |
| 45 | public abortRun = vi.fn(); |
| 46 | public clone = vi.fn(() => this._cloneImpl()); |
| 47 | |
| 48 | private newMessages: Message[]; |
| 49 | private error?: Error | string; |
| 50 | private runAgentDelay: number; |
| 51 | public runAgentCallback?: (input: any) => void; |
| 52 | public runAgentCalls: any[] = []; |
| 53 | private _parentAgent?: MockAgent; |
| 54 | |
| 55 | constructor(options: MockAgentOptions = {}) { |
| 56 | this.messages = options.messages || []; |
| 57 | this.newMessages = options.newMessages || []; |
| 58 | this.error = options.error; |
| 59 | this.runAgentDelay = options.runAgentDelay || 0; |
| 60 | this.runAgentCallback = options.runAgentCallback; |
| 61 | this.agentId = options.agentId; |
| 62 | this.threadId = options.threadId; |
| 63 | this.state = options.state || {}; |
| 64 | } |
| 65 | |
| 66 | async runAgent( |
| 67 | input: any, |
| 68 | subscriber?: any, |
| 69 | ): Promise<{ newMessages: Message[] }> { |
| 70 | this.runAgentCalls.push(input); |
| 71 | // Also track on parent if this is a clone |
| 72 | if (this._parentAgent) { |
| 73 | this._parentAgent.runAgentCalls.push(input); |
| 74 | } |
| 75 | |
| 76 | if (this.runAgentCallback) { |
| 77 | this.runAgentCallback(input); |
| 78 | } |
| 79 | |
| 80 | if (this.runAgentDelay > 0) { |
| 81 | await new Promise((resolve) => setTimeout(resolve, this.runAgentDelay)); |
| 82 | } |
| 83 | |
| 84 | if (this.error) { |
| 85 | throw this.error; |
| 86 | } |
| 87 |
nothing calls this directly
no test coverage detected
searching dependent graphs…