| 82 | } |
| 83 | |
| 84 | export async function createAgentSessionHarness( |
| 85 | options: AgentSessionHarnessOptions |
| 86 | ): Promise<AgentSessionHarness> { |
| 87 | const testHistory = options.historyService ? undefined : await createTestHistoryService(); |
| 88 | const historyService = options.historyService ?? testHistory!.historyService; |
| 89 | const config = options.config ?? testHistory?.config ?? createAgentSessionTestConfig(); |
| 90 | const cleanup = testHistory?.cleanup ?? (() => Promise.resolve()); |
| 91 | const { aiEmitter, aiService } = options.aiService |
| 92 | ? { aiEmitter: options.aiEmitter ?? new EventEmitter(), aiService: options.aiService } |
| 93 | : createMockAiService({ |
| 94 | emitter: options.aiEmitter, |
| 95 | overrides: options.aiServiceOverrides, |
| 96 | }); |
| 97 | const initStateManager = |
| 98 | options.initStateManager ?? createMockInitStateManager(options.initStateManagerOverrides); |
| 99 | const backgroundProcessManager = |
| 100 | options.backgroundProcessManager ?? |
| 101 | createMockBackgroundProcessManager(options.backgroundProcessManagerOverrides); |
| 102 | |
| 103 | const session = new AgentSession({ |
| 104 | workspaceId: options.workspaceId, |
| 105 | config, |
| 106 | historyService, |
| 107 | aiService, |
| 108 | initStateManager, |
| 109 | backgroundProcessManager, |
| 110 | onCompactionComplete: options.onCompactionComplete, |
| 111 | }); |
| 112 | |
| 113 | const events: WorkspaceChatMessage[] = []; |
| 114 | if (options.captureEvents) { |
| 115 | session.onChatEvent(({ message }) => { |
| 116 | events.push(message); |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | return { |
| 121 | session, |
| 122 | config, |
| 123 | historyService, |
| 124 | cleanup, |
| 125 | aiEmitter, |
| 126 | aiService, |
| 127 | initStateManager, |
| 128 | backgroundProcessManager, |
| 129 | events, |
| 130 | }; |
| 131 | } |