feat(terminal): Move Standalone terminal Code to Typescript (#7927)

* feat(terminal): add shared terminal types and interfaces

Add shared terminal module with types and interfaces that enable
terminal management across VSCode, CLI, and JetBrains environments.

- Define ITerminal, ITerminalManager, and TerminalInfo interfaces
- Add TerminalProcessResultPromise for async command execution
- Include StandaloneTerminalOptions for non-VSCode environments
- Prepare module structure for standalone implementations

* feat(terminal): export standalone terminal implementations

Enable exports for standalone terminal classes that were previously
commented out as placeholders:
- StandaloneTerminal
- StandaloneTerminalManager
- StandaloneTerminalProcess
- StandaloneTerminalRegistry

These implementations are now ready for use outside the terminal module.

* fix: resolve TerminalInfo type incompatibility in settings update

- Remove unused TerminalInfo import from both updateSettings files
- Use `as any` cast to handle type mismatch between VSCode and standalone TerminalInfo
- Replace busyTerminals array with busyTerminalsCount to avoid type issues
- Add null-safe access when getting busy terminals length

* feat: import StandaloneTerminalManager from bundled cline-core

Replace standalone enhanced-terminal.js with import from the bundled
TypeScript version in cline-core.js. This consolidates terminal
management code and removes the need to separately include the
runtime file in the VS Code extension package.

- Re-export StandaloneTerminalManager from cline-core.ts
- Update vscode-impls.js to import from cline-core.js
- Remove .vscodeignore exception for enhanced-terminal.js

* feat: simplify standalone terminal manager initialization

Replace global injection pattern with environment variable detection
for determining terminal execution mode. The Task class now directly
instantiates StandaloneTerminalManager when IS_STANDALONE=true instead
of relying on a globally injected instance.

- Remove StandaloneTerminalManager re-export from cline-core.ts
- Simplify vscode-impls.js createTerminal to return stub object
- Use IS_STANDALONE env var for terminal manager selection in Task
- Remove global.standaloneTerminalManager injection pattern

* Fix Standalone build

* Fix Standalone build

* fix: use subagentTerminalOutputLineLimit in StandaloneTerminalManager.processOutput

Match the VSCode TerminalManager logic to properly use subagentTerminalOutputLineLimit (2000) for subagent commands instead of always falling back to terminalOutputLineLimit (500).

* feat: add TerminalManager to HostProvider for dependency injection

- Add TerminalManagerCreator type and createTerminalManager to HostProvider
- Extract ITerminalManager interface to shared/terminal/types for abstraction
- Refactor TerminalManager to implement ITerminalManager interface
- Create StandaloneTerminalManager for non-VSCode environments
- Update TerminalRegistry to use ITerminalManager via HostProvider
- Enable terminal management to work across different host environments

* feat: refactor terminal manager to use ITerminalManager interface

- Replace concrete TerminalManager/StandaloneTerminalManager types with ITerminalManager interface
- Use HostProvider.createTerminalManager() for host-agnostic terminal creation
- Simplify terminal execution mode logic in Task constructor
- Add dynamic imports for StandaloneTerminalManager when backgroundExec mode is used
- Improve logging for terminal manager selection
This commit is contained in:
Ara authored and GitHub committed 2025-12-07 05:10:25 -08:00
1 parent 34bb95e04e
commit e9136f60cc
18 files changed
+1292 -729

No files matched your search

+29 -67
View File
@@ -42,7 +42,6 @@ import { DiffViewProvider } from "@integrations/editor/DiffViewProvider"
import { formatContentBlockToMarkdown } from "@integrations/misc/export-markdown"
import { processFilesIntoText } from "@integrations/misc/extract-text"
import { showSystemNotification } from "@integrations/notifications"
import { TerminalManager } from "@integrations/terminal/TerminalManager"
import { TerminalProcessResultPromise } from "@integrations/terminal/TerminalProcess"
import { BrowserSession } from "@services/browser/BrowserSession"
import { UrlContentFetcher } from "@services/browser/UrlContentFetcher"
@@ -66,6 +65,7 @@ import { DEFAULT_LANGUAGE_SETTINGS, getLanguageKey, LanguageDisplay } from "@sha
import { CLINE_MCP_TOOL_IDENTIFIER } from "@shared/mcp"
import { USER_CONTENT_TAGS } from "@shared/messages/constants"
import { convertClineMessageToProto } from "@shared/proto-conversions/cline-message"
import { ITerminalManager } from "@shared/terminal/types"
import { ClineDefaultTool } from "@shared/tools"
import { ClineAskResponse } from "@shared/WebviewMessage"
import { isClaude4PlusModelFamily, isGPT5ModelFamily, isLocalModel, isNextGenModelFamily } from "@utils/model-utils"
@@ -135,12 +135,6 @@ type TaskParams = {
}
export class Task {
// Constants
private static readonly STANDALONE_TERMINAL_MODULE_PATH = path.join(
__dirname,
"../standalone/runtime-files/vscode/enhanced-terminal.js",
)
// Core task variables
readonly taskId: string
readonly ulid: string
@@ -200,7 +194,7 @@ export class Task {
// Service handlers
api: ApiHandler
terminalManager: TerminalManager
terminalManager: ITerminalManager
private urlContentFetcher: UrlContentFetcher
browserSession: BrowserSession
contextManager: ContextManager
@@ -288,38 +282,20 @@ export class Task {
this.clineIgnoreController = new ClineIgnoreController(cwd)
this.taskLockAcquired = taskLockAcquired
// TODO(ae) this is a hack to replace the terminal manager for standalone,
// until we have proper host bridge support for terminal execution. The
// standaloneTerminalManager is defined in the vscode-impls and injected
// during compilation of the standalone manager only, so this variable only
// exists in that case
// Determine terminal execution mode and create appropriate terminal manager
this.terminalExecutionMode = vscodeTerminalExecutionMode || "vscodeTerminal"
// First check if we're in standalone mode (original automatic detection)
if ((global as any).standaloneTerminalManager) {
this.terminalManager = (global as any).standaloneTerminalManager
this.terminalExecutionMode = "backgroundExec"
// When backgroundExec mode is selected, use StandaloneTerminalManager for hidden execution
// Otherwise, use the HostProvider's terminal manager (VSCode terminal in VSCode, standalone in CLI)
if (this.terminalExecutionMode === "backgroundExec") {
// Import StandaloneTerminalManager for background execution
const { StandaloneTerminalManager } = require("@shared/terminal/StandaloneTerminalManager")
this.terminalManager = new StandaloneTerminalManager()
Logger.info(`[Task ${taskId}] Using StandaloneTerminalManager for backgroundExec mode`)
} else {
// Not in standalone mode, use the configured mode (default to vscodeTerminal)
const terminalExecutionMode = vscodeTerminalExecutionMode || "vscodeTerminal"
this.terminalExecutionMode = terminalExecutionMode
if (terminalExecutionMode === "backgroundExec") {
try {
const { StandaloneTerminalManager } = require(Task.STANDALONE_TERMINAL_MODULE_PATH) as {
StandaloneTerminalManager?: new () => TerminalManager
}
if (StandaloneTerminalManager) {
this.terminalManager = new StandaloneTerminalManager()
} else {
this.terminalManager = new TerminalManager()
}
} catch (error) {
console.error("[DEBUG] Failed to load standalone terminal manager", error)
this.terminalManager = new TerminalManager()
}
} else {
this.terminalManager = new TerminalManager()
}
// Use the host-provided terminal manager (VSCode terminal in VSCode environment)
this.terminalManager = HostProvider.get().createTerminalManager()
Logger.info(`[Task ${taskId}] Using HostProvider terminal manager for vscodeTerminal mode`)
}
this.terminalManager.setShellIntegrationTimeout(shellIntegrationTimeout)
this.terminalManager.setTerminalReuseEnabled(terminalReuseEnabled ?? true)
@@ -1528,34 +1504,24 @@ export class Task {
Logger.info("Executing command in terminal: " + command)
let terminalManager: TerminalManager
if (isSubagent) {
// Create a background TerminalManager for CLI subagents
try {
const { StandaloneTerminalManager } = require(Task.STANDALONE_TERMINAL_MODULE_PATH) as {
StandaloneTerminalManager?: new () => TerminalManager
}
if (StandaloneTerminalManager) {
terminalManager = new StandaloneTerminalManager()
} else {
terminalManager = new TerminalManager()
}
} catch (error) {
console.error("[DEBUG] Failed to load standalone terminal manager for subagent", error)
terminalManager = new TerminalManager()
}
terminalManager.setShellIntegrationTimeout(this.terminalManager["shellIntegrationTimeout"] || 4000)
terminalManager.setTerminalReuseEnabled(this.terminalManager["terminalReuseEnabled"] ?? true)
terminalManager.setTerminalOutputLineLimit(this.terminalManager["terminalOutputLineLimit"] || 500)
terminalManager.setSubagentTerminalOutputLineLimit(this.terminalManager["subagentTerminalOutputLineLimit"] || 2000)
let terminalManager: ITerminalManager
if (isSubagent || this.terminalExecutionMode === "backgroundExec") {
// Use StandaloneTerminalManager for hidden background execution (subagents and backgroundExec mode)
const { StandaloneTerminalManager } = require("@shared/terminal/StandaloneTerminalManager")
terminalManager = new StandaloneTerminalManager()
Logger.info(
`[Task ${this.taskId}] Using StandaloneTerminalManager for ${isSubagent ? "subagent" : "backgroundExec"} command: ${command}`,
)
} else {
// Use the configured terminal manager for regular commands
// Use the configured terminal manager for regular commands (VSCode terminal)
terminalManager = this.terminalManager
}
const terminalInfo = await terminalManager.getOrCreateTerminal(this.cwd)
terminalInfo.terminal.show() // weird visual bug when creating new terminals (even manually) where there's an empty space at the top.
const process = terminalManager.runCommand(terminalInfo, command)
// Use `as any` to handle type incompatibility between VSCode's Thenable and Promise
// Both TerminalInfo types have the same runtime structure, the difference is purely TypeScript
const process = terminalManager.runCommand(terminalInfo as any, command)
// Track command execution for both terminal modes
this.controller.updateBackgroundCommandState(true, this.taskId)
@@ -1776,7 +1742,7 @@ export class Task {
// Process any output we captured before timeout
await setTimeoutPromise(50)
const result = this.terminalManager.processOutput(outputLines, undefined, false)
const result = terminalManager.processOutput(outputLines, undefined, isSubagent)
if (error.message === "COMMAND_TIMEOUT") {
return [
@@ -1808,11 +1774,7 @@ export class Task {
await setTimeoutPromise(50)
}
const result = terminalManager.processOutput(
outputLines,
isSubagent ? terminalManager["subagentTerminalOutputLineLimit"] : undefined,
isSubagent,
)
const result = terminalManager.processOutput(outputLines, undefined, isSubagent)
if (didCancelViaUi) {
return [
@@ -1900,7 +1862,7 @@ export class Task {
}
// Process the captured output to include in the cancellation message
const processedOutput = this.terminalManager.processOutput(outputLines, undefined, false)
const processedOutput = this.terminalManager.processOutput(outputLines, undefined, isSubagentCommand(command))
// Add cancellation information to the API conversation history
// This ensures the agent knows the command was cancelled in the next request