()
| 1571 | } |
| 1572 | |
| 1573 | public async condenseContext(): Promise<void> { |
| 1574 | // CRITICAL: Flush any pending tool results before condensing |
| 1575 | // to ensure tool_use/tool_result pairs are complete in history |
| 1576 | await this.flushPendingToolResultsToHistory() |
| 1577 | |
| 1578 | const systemPrompt = await this.getSystemPrompt() |
| 1579 | |
| 1580 | // Get condensing configuration |
| 1581 | const state = await this.providerRef.deref()?.getState() |
| 1582 | const customCondensingPrompt = state?.customSupportPrompts?.CONDENSE |
| 1583 | const { mode, apiConfiguration } = state ?? {} |
| 1584 | |
| 1585 | const { contextTokens: prevContextTokens } = this.getTokenUsage() |
| 1586 | |
| 1587 | // Build tools for condensing metadata (same tools used for normal API calls) |
| 1588 | const provider = this.providerRef.deref() |
| 1589 | let allTools: import("openai").default.Chat.ChatCompletionTool[] = [] |
| 1590 | if (provider) { |
| 1591 | const modelInfo = this.api.getModel().info |
| 1592 | const toolsResult = await buildNativeToolsArrayWithRestrictions({ |
| 1593 | provider, |
| 1594 | cwd: this.cwd, |
| 1595 | mode, |
| 1596 | customModes: state?.customModes, |
| 1597 | experiments: state?.experiments, |
| 1598 | apiConfiguration, |
| 1599 | disabledTools: state?.disabledTools, |
| 1600 | modelInfo, |
| 1601 | includeAllToolsWithRestrictions: false, |
| 1602 | }) |
| 1603 | allTools = toolsResult.tools |
| 1604 | } |
| 1605 | |
| 1606 | // Build metadata with tools and taskId for the condensing API call |
| 1607 | const metadata: ApiHandlerCreateMessageMetadata = { |
| 1608 | mode, |
| 1609 | taskId: this.taskId, |
| 1610 | ...(this.currentRequestAbortController?.signal |
| 1611 | ? { |
| 1612 | abortSignal: this.currentRequestAbortController.signal, |
| 1613 | } |
| 1614 | : {}), |
| 1615 | ...(allTools.length > 0 |
| 1616 | ? { |
| 1617 | tools: allTools, |
| 1618 | tool_choice: "auto", |
| 1619 | parallelToolCalls: true, |
| 1620 | } |
| 1621 | : {}), |
| 1622 | } |
| 1623 | // Generate environment details to include in the condensed summary |
| 1624 | const environmentDetails = await getEnvironmentDetails(this, true) |
| 1625 | |
| 1626 | const filesReadByRoo = await this.getFilesReadByRooSafely("condenseContext") |
| 1627 | |
| 1628 | const { |
| 1629 | messages, |
| 1630 | summary, |
no test coverage detected