(query: string)
| 147 | } |
| 148 | |
| 149 | async runQuery(query: string): Promise<RunQueryResult | undefined> { |
| 150 | this.abortController = new AbortController(); |
| 151 | let finalAnswer: string | undefined; |
| 152 | |
| 153 | const startTime = Date.now(); |
| 154 | const item: HistoryItem = { |
| 155 | id: String(startTime), |
| 156 | query, |
| 157 | events: [], |
| 158 | answer: '', |
| 159 | status: 'processing', |
| 160 | startTime, |
| 161 | }; |
| 162 | this.historyValue = [...this.historyValue, item]; |
| 163 | this.inMemoryChatHistory.saveUserQuery(query); |
| 164 | this.errorValue = null; |
| 165 | this.workingStateValue = { status: 'thinking' }; |
| 166 | this.turnStartMsValue = startTime; |
| 167 | this.streamedCharsValue = 0; |
| 168 | this.streamModeValue = 'requesting'; |
| 169 | this.emitChange(); |
| 170 | |
| 171 | try { |
| 172 | const agent = await Agent.create({ |
| 173 | ...this.agentConfig, |
| 174 | signal: this.abortController.signal, |
| 175 | requestToolApproval: this.requestToolApproval, |
| 176 | requestUserInput: this.requestUserInput, |
| 177 | sessionApprovedTools: this.sessionApprovedTools, |
| 178 | messageQueue: defaultQueue, |
| 179 | }); |
| 180 | const stream = agent.run(query, this.inMemoryChatHistory); |
| 181 | for await (const event of stream) { |
| 182 | if (event.type === 'done') { |
| 183 | finalAnswer = (event as DoneEvent).answer; |
| 184 | } |
| 185 | await this.handleEvent(event); |
| 186 | } |
| 187 | |
| 188 | // Post-run: if messages arrived after the agent's last drain, start a new turn |
| 189 | if (!defaultQueue.isEmpty()) { |
| 190 | const remaining = defaultQueue.dequeueAll(); |
| 191 | const mergedText = remaining.map(m => m.text).join('\n\n'); |
| 192 | return this.runQuery(mergedText); |
| 193 | } |
| 194 | |
| 195 | if (finalAnswer) { |
| 196 | return { answer: finalAnswer }; |
| 197 | } |
| 198 | return undefined; |
| 199 | } catch (error) { |
| 200 | if (error instanceof Error && error.name === 'AbortError') { |
| 201 | this.markLastProcessing('interrupted'); |
| 202 | this.workingStateValue = { status: 'idle' }; |
| 203 | this.resetTurnStats(); |
| 204 | this.emitChange(); |
| 205 | return undefined; |
| 206 | } |
no test coverage detected