Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 42 additions & 33 deletions src/core/Cline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
addLineNumbers,
stripLineNumbers,
everyLineHasLineNumbers,
truncateOutput,
} from "../integrations/misc/extract-text"
import { TerminalManager, ExitCodeDetails } from "../integrations/terminal/TerminalManager"
import { UrlContentFetcher } from "../services/browser/UrlContentFetcher"
Expand Down Expand Up @@ -55,11 +54,12 @@ import { HistoryItem } from "../shared/HistoryItem"
import { ClineAskResponse } from "../shared/WebviewMessage"
import { GlobalFileNames } from "../shared/globalFileNames"
import { defaultModeSlug, getModeBySlug, getFullModeDetails } from "../shared/modes"
import { EXPERIMENT_IDS, experiments as Experiments, ExperimentId } from "../shared/experiments"
import { calculateApiCost } from "../utils/cost"
import { fileExistsAtPath } from "../utils/fs"
import { arePathsEqual, getReadablePath } from "../utils/path"
import { parseMentions } from "./mentions"
import { RooIgnoreController, LOCK_TEXT_SYMBOL } from "./ignore/RooIgnoreController"
import { RooIgnoreController } from "./ignore/RooIgnoreController"
import { AssistantMessageContent, parseAssistantMessage, ToolParamName, ToolUseName } from "./assistant-message"
import { formatResponse } from "./prompts/responses"
import { SYSTEM_PROMPT } from "./prompts/system"
Expand All @@ -70,7 +70,7 @@ import { BrowserSession } from "../services/browser/BrowserSession"
import { McpHub } from "../services/mcp/McpHub"
import crypto from "crypto"
import { insertGroups } from "./diff/insert-groups"
import { EXPERIMENT_IDS, experiments as Experiments, ExperimentId } from "../shared/experiments"
import { OutputBuilder } from "../integrations/terminal/OutputBuilder"

const cwd =
vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") // may or may not exist but fs checking existence would immediately ask for permission which would be bad UX, need to come up with a better solution
Expand Down Expand Up @@ -912,44 +912,52 @@ export class Cline {
// Tools

async executeCommandTool(command: string): Promise<[boolean, ToolResponse]> {
const { terminalOutputLimit } = (await this.providerRef.deref()?.getState()) ?? {}

const terminalInfo = await this.terminalManager.getOrCreateTerminal(cwd)
terminalInfo.terminal.show() // weird visual bug when creating new terminals (even manually) where there's an empty space at the top.
const process = this.terminalManager.runCommand(terminalInfo, command)
// Weird visual bug when creating new terminals (even manually) where
// there's an empty space at the top.
terminalInfo.terminal.show()
const process = this.terminalManager.runCommand(terminalInfo, command, terminalOutputLimit)

let userFeedback: { text?: string; images?: string[] } | undefined
let didContinue = false
const sendCommandOutput = async (line: string): Promise<void> => {

const sendCommandOutput = async (line: string) => {
try {
const { response, text, images } = await this.ask("command_output", line)

if (response === "yesButtonClicked") {
// proceed while running
// Proceed while running.
} else {
userFeedback = { text, images }
}

didContinue = true
process.continue() // continue past the await
process.continue() // Continue past the await.
} catch {
// This can only happen if this ask promise was ignored, so ignore this error
// This can only happen if this ask promise was ignored, so ignore this error.
}
}

let lines: string[] = []
let completed = false
let exitDetails: ExitCodeDetails | undefined

let builder = new OutputBuilder({ maxSize: terminalOutputLimit })
let output: string | undefined = undefined

process.on("line", (line) => {
lines.push(line)
builder.append(line)

if (!didContinue) {
sendCommandOutput(line)
} else {
this.say("command_output", line)
}
})

let completed = false
let exitDetails: ExitCodeDetails | undefined
process.once("completed", (output?: string) => {
// Use provided output if available, otherwise keep existing result.
if (output) {
lines = output.split("\n")
}
process.once("completed", (buffer?: string) => {
output = buffer
completed = true
})

Expand All @@ -965,19 +973,17 @@ export class Cline {

await process

// Wait for a short delay to ensure all messages are sent to the webview
// Wait for a short delay to ensure all messages are sent to the webview.
// This delay allows time for non-awaited promises to be created and
// for their associated messages to be sent to the webview, maintaining
// the correct order of messages (although the webview is smart about
// grouping command_output messages despite any gaps anyways)
// grouping command_output messages despite any gaps anyways).
await delay(50)

const { terminalOutputLineLimit } = (await this.providerRef.deref()?.getState()) ?? {}
const output = truncateOutput(lines.join("\n"), terminalOutputLineLimit)
const result = output.trim()
const result = output || builder.content

if (userFeedback) {
await this.say("user_feedback", userFeedback.text, userFeedback.images)

return [
true,
formatResponse.toolResult(
Expand All @@ -991,25 +997,28 @@ export class Cline {

if (completed) {
let exitStatus = "No exit code available"

if (exitDetails !== undefined) {
if (exitDetails.signal) {
exitStatus = `Process terminated by signal ${exitDetails.signal} (${exitDetails.signalName})`

if (exitDetails.coreDumpPossible) {
exitStatus += " - core dump possible"
}
} else {
exitStatus = `Exit code: ${exitDetails.exitCode}`
}
}

return [false, `Command executed. ${exitStatus}${result.length > 0 ? `\nOutput:\n${result}` : ""}`]
} else {
return [
false,
`Command is still running in the user's terminal.${
result.length > 0 ? `\nHere's the output so far:\n${result}` : ""
}\n\nYou will be updated on the terminal status and new output in the future.`,
]
}

return [
false,
`Command is still running in the user's terminal.${
result.length > 0 ? `\nHere's the output so far:\n${result}` : ""
}\n\nYou will be updated on the terminal status and new output in the future.`,
]
}

async *attemptApiRequest(previousApiReqIndex: number, retryAttempt: number = 0): ApiStream {
Expand Down Expand Up @@ -3495,7 +3504,7 @@ export class Cline {
terminalDetails += "\n\n# Actively Running Terminals"
for (const busyTerminal of busyTerminals) {
terminalDetails += `\n## Original command: \`${busyTerminal.lastCommand}\``
const newOutput = this.terminalManager.getUnretrievedOutput(busyTerminal.id)
const newOutput = this.terminalManager.readLine(busyTerminal.id)
if (newOutput) {
terminalDetails += `\n### New Output\n${newOutput}`
} else {
Expand All @@ -3507,7 +3516,7 @@ export class Cline {
if (inactiveTerminals.length > 0) {
const inactiveTerminalOutputs = new Map<number, string>()
for (const inactiveTerminal of inactiveTerminals) {
const newOutput = this.terminalManager.getUnretrievedOutput(inactiveTerminal.id)
const newOutput = this.terminalManager.readLine(inactiveTerminal.id)
if (newOutput) {
inactiveTerminalOutputs.set(inactiveTerminal.id, newOutput)
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/mentions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import * as vscode from "vscode"
import * as path from "path"
import { openFile } from "../../integrations/misc/open-file"
import { UrlContentFetcher } from "../../services/browser/UrlContentFetcher"
import { mentionRegexGlobal, formatGitSuggestion, type MentionSuggestion } from "../../shared/context-mentions"
import { mentionRegexGlobal } from "../../shared/context-mentions"
import fs from "fs/promises"
import { extractTextFromFile } from "../../integrations/misc/extract-text"
import { isBinaryFile } from "isbinaryfile"
import { diagnosticsToProblemsString } from "../../integrations/diagnostics"
import { getCommitInfo, getWorkingState } from "../../utils/git"
import { getLatestTerminalOutput } from "../../integrations/terminal/get-latest-output"
import { getLatestTerminalOutput } from "../../integrations/terminal/getLatestTerminalOutput"

export async function openMention(mention?: string): Promise<void> {
if (!mention) {
Expand Down
17 changes: 8 additions & 9 deletions src/core/webview/ClineProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import simpleGit from "simple-git"
import { setPanel } from "../../activate/registerCommands"

import { ApiConfiguration, ApiProvider, ModelInfo, API_CONFIG_KEYS } from "../../shared/api"
import { CheckpointStorage } from "../../shared/checkpoints"
import { findLast } from "../../shared/array"
import { CustomSupportPrompts, supportPrompt } from "../../shared/support-prompt"
import { supportPrompt } from "../../shared/support-prompt"
import { GlobalFileNames } from "../../shared/globalFileNames"
import { SecretKey, GlobalStateKey, SECRET_KEYS, GLOBAL_STATE_KEYS } from "../../shared/globalState"
import { HistoryItem } from "../../shared/HistoryItem"
import { ApiConfigMeta, ExtensionMessage } from "../../shared/ExtensionMessage"
import { checkoutDiffPayloadSchema, checkoutRestorePayloadSchema, WebviewMessage } from "../../shared/WebviewMessage"
import { Mode, CustomModePrompts, PromptComponent, defaultModeSlug, ModeConfig } from "../../shared/modes"
import { Mode, PromptComponent, defaultModeSlug, ModeConfig } from "../../shared/modes"
import { checkExistKey } from "../../shared/checkExistApiConfig"
import { EXPERIMENT_IDS, experiments as Experiments, experimentDefault, ExperimentId } from "../../shared/experiments"
import { TERMINAL_OUTPUT_LIMIT } from "../../shared/terminal"
import { downloadTask } from "../../integrations/misc/export-markdown"
import { openFile, openImage } from "../../integrations/misc/open-file"
import { selectImages } from "../../integrations/misc/process-images"
Expand Down Expand Up @@ -1215,7 +1215,6 @@ export class ClineProvider implements vscode.WebviewViewProvider {
await this.postStateToWebview()
break
case "checkpointStorage":
console.log(`[ClineProvider] checkpointStorage: ${message.text}`)
const checkpointStorage = message.text ?? "task"
await this.updateGlobalState("checkpointStorage", checkpointStorage)
await this.postStateToWebview()
Expand Down Expand Up @@ -1249,8 +1248,8 @@ export class ClineProvider implements vscode.WebviewViewProvider {
await this.updateGlobalState("writeDelayMs", message.value)
await this.postStateToWebview()
break
case "terminalOutputLineLimit":
await this.updateGlobalState("terminalOutputLineLimit", message.value)
case "terminalOutputLimit":
await this.updateGlobalState("terminalOutputLimit", message.value)
await this.postStateToWebview()
break
case "mode":
Expand Down Expand Up @@ -2134,7 +2133,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
screenshotQuality,
preferredLanguage,
writeDelayMs,
terminalOutputLineLimit,
terminalOutputLimit,
fuzzyMatchThreshold,
mcpEnabled,
enableMcpServerCreation,
Expand Down Expand Up @@ -2186,7 +2185,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
screenshotQuality: screenshotQuality ?? 75,
preferredLanguage: preferredLanguage ?? "English",
writeDelayMs: writeDelayMs ?? 1000,
terminalOutputLineLimit: terminalOutputLineLimit ?? 500,
terminalOutputLimit: terminalOutputLimit ?? TERMINAL_OUTPUT_LIMIT,
fuzzyMatchThreshold: fuzzyMatchThreshold ?? 1.0,
mcpEnabled: mcpEnabled ?? true,
enableMcpServerCreation: enableMcpServerCreation ?? true,
Expand Down Expand Up @@ -2334,7 +2333,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
screenshotQuality: stateValues.screenshotQuality ?? 75,
fuzzyMatchThreshold: stateValues.fuzzyMatchThreshold ?? 1.0,
writeDelayMs: stateValues.writeDelayMs ?? 1000,
terminalOutputLineLimit: stateValues.terminalOutputLineLimit ?? 500,
terminalOutputLimit: stateValues.terminalOutputLimit ?? TERMINAL_OUTPUT_LIMIT,
mode: stateValues.mode ?? defaultModeSlug,
preferredLanguage:
stateValues.preferredLanguage ??
Expand Down
66 changes: 1 addition & 65 deletions src/integrations/misc/__tests__/extract-text.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addLineNumbers, everyLineHasLineNumbers, stripLineNumbers, truncateOutput } from "../extract-text"
import { addLineNumbers, everyLineHasLineNumbers, stripLineNumbers } from "../extract-text"

describe("addLineNumbers", () => {
it("should add line numbers starting from 1 by default", () => {
Expand Down Expand Up @@ -101,67 +101,3 @@ describe("stripLineNumbers", () => {
expect(stripLineNumbers(input)).toBe(expected)
})
})

describe("truncateOutput", () => {
it("returns original content when no line limit provided", () => {
const content = "line1\nline2\nline3"
expect(truncateOutput(content)).toBe(content)
})

it("returns original content when lines are under limit", () => {
const content = "line1\nline2\nline3"
expect(truncateOutput(content, 5)).toBe(content)
})

it("truncates content with 20/80 split when over limit", () => {
// Create 25 lines of content
const lines = Array.from({ length: 25 }, (_, i) => `line${i + 1}`)
const content = lines.join("\n")

// Set limit to 10 lines
const result = truncateOutput(content, 10)

// Should keep:
// - First 2 lines (20% of 10)
// - Last 8 lines (80% of 10)
// - Omission indicator in between
const expectedLines = [
"line1",
"line2",
"",
"[...15 lines omitted...]",
"",
"line18",
"line19",
"line20",
"line21",
"line22",
"line23",
"line24",
"line25",
]
expect(result).toBe(expectedLines.join("\n"))
})

it("handles empty content", () => {
expect(truncateOutput("", 10)).toBe("")
})

it("handles single line content", () => {
expect(truncateOutput("single line", 10)).toBe("single line")
})

it("handles windows-style line endings", () => {
// Create content with windows line endings
const lines = Array.from({ length: 15 }, (_, i) => `line${i + 1}`)
const content = lines.join("\r\n")

const result = truncateOutput(content, 5)

// Should keep first line (20% of 5 = 1) and last 4 lines (80% of 5 = 4)
// Split result by either \r\n or \n to normalize line endings
const resultLines = result.split(/\r?\n/)
const expectedLines = ["line1", "", "[...10 lines omitted...]", "", "line12", "line13", "line14", "line15"]
expect(resultLines).toEqual(expectedLines)
})
})
34 changes: 0 additions & 34 deletions src/integrations/misc/extract-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,37 +89,3 @@ export function stripLineNumbers(content: string): string {
const lineEnding = content.includes("\r\n") ? "\r\n" : "\n"
return processedLines.join(lineEnding)
}

/**
* Truncates multi-line output while preserving context from both the beginning and end.
* When truncation is needed, it keeps 20% of the lines from the start and 80% from the end,
* with a clear indicator of how many lines were omitted in between.
*
* @param content The multi-line string to truncate
* @param lineLimit Optional maximum number of lines to keep. If not provided or 0, returns the original content
* @returns The truncated string with an indicator of omitted lines, or the original content if no truncation needed
*
* @example
* // With 10 line limit on 25 lines of content:
* // - Keeps first 2 lines (20% of 10)
* // - Keeps last 8 lines (80% of 10)
* // - Adds "[...15 lines omitted...]" in between
*/
export function truncateOutput(content: string, lineLimit?: number): string {
if (!lineLimit) {
return content
}

const lines = content.split("\n")
if (lines.length <= lineLimit) {
return content
}

const beforeLimit = Math.floor(lineLimit * 0.2) // 20% of lines before
const afterLimit = lineLimit - beforeLimit // remaining 80% after
return [
...lines.slice(0, beforeLimit),
`\n[...${lines.length - lineLimit} lines omitted...]\n`,
...lines.slice(-afterLimit),
].join("\n")
}
Loading