centaurus-cli 3.1.3 → 3.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/dist/cli-adapter.js +685 -153
  2. package/dist/cli-adapter.js.map +1 -1
  3. package/dist/config/defaultConfig.js +1 -4
  4. package/dist/config/defaultConfig.js.map +1 -1
  5. package/dist/config/models.js +4 -0
  6. package/dist/config/models.js.map +1 -1
  7. package/dist/config/slash-commands.js +66 -2
  8. package/dist/config/slash-commands.js.map +1 -1
  9. package/dist/config/types.js +4 -4
  10. package/dist/config/types.js.map +1 -1
  11. package/dist/index.js +36 -0
  12. package/dist/index.js.map +1 -1
  13. package/dist/services/ai-context-injector.js +109 -0
  14. package/dist/services/ai-context-injector.js.map +1 -1
  15. package/dist/services/api-client.js.map +1 -1
  16. package/dist/services/background-task-manager.js +59 -0
  17. package/dist/services/background-task-manager.js.map +1 -1
  18. package/dist/services/local-chat-storage.js +2 -0
  19. package/dist/services/local-chat-storage.js.map +1 -1
  20. package/dist/services/skill-storage.js +141 -0
  21. package/dist/services/skill-storage.js.map +1 -0
  22. package/dist/services/sub-agent-manager.js +49 -8
  23. package/dist/services/sub-agent-manager.js.map +1 -1
  24. package/dist/services/warpify-detector.js +17 -5
  25. package/dist/services/warpify-detector.js.map +1 -1
  26. package/dist/tools/background-command.js +5 -2
  27. package/dist/tools/background-command.js.map +1 -1
  28. package/dist/tools/command.js +367 -109
  29. package/dist/tools/command.js.map +1 -1
  30. package/dist/tools/file-ops.js +23 -6
  31. package/dist/tools/file-ops.js.map +1 -1
  32. package/dist/tools/plan-mode.js +184 -336
  33. package/dist/tools/plan-mode.js.map +1 -1
  34. package/dist/tools/sub-agent.js +24 -5
  35. package/dist/tools/sub-agent.js.map +1 -1
  36. package/dist/tools/todo-list.js +157 -0
  37. package/dist/tools/todo-list.js.map +1 -0
  38. package/dist/types/skill.js +30 -0
  39. package/dist/types/skill.js.map +1 -0
  40. package/dist/ui/components/App.js +956 -162
  41. package/dist/ui/components/App.js.map +1 -1
  42. package/dist/ui/components/AuthScreen.js +3 -1
  43. package/dist/ui/components/AuthScreen.js.map +1 -1
  44. package/dist/ui/components/AuthWelcomeScreen.js +3 -1
  45. package/dist/ui/components/AuthWelcomeScreen.js.map +1 -1
  46. package/dist/ui/components/CodeBlock.js +3 -1
  47. package/dist/ui/components/CodeBlock.js.map +1 -1
  48. package/dist/ui/components/CompactShellPreview.js +44 -0
  49. package/dist/ui/components/CompactShellPreview.js.map +1 -0
  50. package/dist/ui/components/ConfigViewer.js +3 -1
  51. package/dist/ui/components/ConfigViewer.js.map +1 -1
  52. package/dist/ui/components/ConfirmPrompt.js +3 -1
  53. package/dist/ui/components/ConfirmPrompt.js.map +1 -1
  54. package/dist/ui/components/ConnectionStatusMessage.js +3 -1
  55. package/dist/ui/components/ConnectionStatusMessage.js.map +1 -1
  56. package/dist/ui/components/DetailedPlanReviewScreen.js +84 -74
  57. package/dist/ui/components/DetailedPlanReviewScreen.js.map +1 -1
  58. package/dist/ui/components/DiffViewer.js +6 -3
  59. package/dist/ui/components/DiffViewer.js.map +1 -1
  60. package/dist/ui/components/FileCreationPreview.js.map +1 -1
  61. package/dist/ui/components/FileTagAutocomplete.js +4 -2
  62. package/dist/ui/components/FileTagAutocomplete.js.map +1 -1
  63. package/dist/ui/components/InputBox.js +243 -40
  64. package/dist/ui/components/InputBox.js.map +1 -1
  65. package/dist/ui/components/InteractiveShell.js +5 -3
  66. package/dist/ui/components/InteractiveShell.js.map +1 -1
  67. package/dist/ui/components/KeyboardHelp.js +4 -1
  68. package/dist/ui/components/KeyboardHelp.js.map +1 -1
  69. package/dist/ui/components/LoadingIndicator.js +3 -1
  70. package/dist/ui/components/LoadingIndicator.js.map +1 -1
  71. package/dist/ui/components/MCPAddScreen.js +63 -13
  72. package/dist/ui/components/MCPAddScreen.js.map +1 -1
  73. package/dist/ui/components/MarkdownRenderer.js +3 -1
  74. package/dist/ui/components/MarkdownRenderer.js.map +1 -1
  75. package/dist/ui/components/MessageDisplay.js +9 -7
  76. package/dist/ui/components/MessageDisplay.js.map +1 -1
  77. package/dist/ui/components/ModelPicker.js +170 -0
  78. package/dist/ui/components/ModelPicker.js.map +1 -0
  79. package/dist/ui/components/MonitorModeAIPanel.js +3 -1
  80. package/dist/ui/components/MonitorModeAIPanel.js.map +1 -1
  81. package/dist/ui/components/PlanAcceptedMessage.js +12 -6
  82. package/dist/ui/components/PlanAcceptedMessage.js.map +1 -1
  83. package/dist/ui/components/PlanQuestionMessage.js +37 -0
  84. package/dist/ui/components/PlanQuestionMessage.js.map +1 -0
  85. package/dist/ui/components/PlanQuestionScreen.js +138 -0
  86. package/dist/ui/components/PlanQuestionScreen.js.map +1 -0
  87. package/dist/ui/components/PlanReviewScreen.js +7 -9
  88. package/dist/ui/components/PlanReviewScreen.js.map +1 -1
  89. package/dist/ui/components/RulesEditorScreen.js +65 -28
  90. package/dist/ui/components/RulesEditorScreen.js.map +1 -1
  91. package/dist/ui/components/SelectPrompt.js +3 -1
  92. package/dist/ui/components/SelectPrompt.js.map +1 -1
  93. package/dist/ui/components/SkillCreatorScreen.js +217 -0
  94. package/dist/ui/components/SkillCreatorScreen.js.map +1 -0
  95. package/dist/ui/components/SlashCommandAutocomplete.js +4 -2
  96. package/dist/ui/components/SlashCommandAutocomplete.js.map +1 -1
  97. package/dist/ui/components/StatusBar.js +4 -2
  98. package/dist/ui/components/StatusBar.js.map +1 -1
  99. package/dist/ui/components/StreamingMessageDisplay.js +5 -3
  100. package/dist/ui/components/StreamingMessageDisplay.js.map +1 -1
  101. package/dist/ui/components/SubAgentListScreen.js +65 -0
  102. package/dist/ui/components/SubAgentListScreen.js.map +1 -0
  103. package/dist/ui/components/SubAgentViewScreen.js +123 -0
  104. package/dist/ui/components/SubAgentViewScreen.js.map +1 -0
  105. package/dist/ui/components/TaskCompletedMessage.js +40 -8
  106. package/dist/ui/components/TaskCompletedMessage.js.map +1 -1
  107. package/dist/ui/components/TaskProgressIndicator.js +6 -4
  108. package/dist/ui/components/TaskProgressIndicator.js.map +1 -1
  109. package/dist/ui/components/TextEditor.js +297 -0
  110. package/dist/ui/components/TextEditor.js.map +1 -0
  111. package/dist/ui/components/TodoListMessage.js +59 -0
  112. package/dist/ui/components/TodoListMessage.js.map +1 -0
  113. package/dist/ui/components/ToolExecutionMessage.js +134 -84
  114. package/dist/ui/components/ToolExecutionMessage.js.map +1 -1
  115. package/dist/ui/components/ToolExecutionStatus.js +3 -1
  116. package/dist/ui/components/ToolExecutionStatus.js.map +1 -1
  117. package/dist/ui/components/WelcomeBanner.js +33 -33
  118. package/dist/ui/components/WelcomeBanner.js.map +1 -1
  119. package/dist/ui/components/WorkflowCreatorScreen.js +5 -3
  120. package/dist/ui/components/WorkflowCreatorScreen.js.map +1 -1
  121. package/dist/ui/theme.js +97 -0
  122. package/dist/ui/theme.js.map +1 -0
  123. package/dist/ui/utils/chat-history-limit.js +247 -0
  124. package/dist/ui/utils/chat-history-limit.js.map +1 -0
  125. package/dist/utils/chat-formatter.js +22 -9
  126. package/dist/utils/chat-formatter.js.map +1 -1
  127. package/dist/utils/input-classifier.js +11 -1
  128. package/dist/utils/input-classifier.js.map +1 -1
  129. package/dist/utils/output-truncation.js +175 -0
  130. package/dist/utils/output-truncation.js.map +1 -0
  131. package/dist/utils/rule-reference-resolver.js +3 -3
  132. package/dist/utils/rule-reference-resolver.js.map +1 -1
  133. package/dist/utils/tunnel-commands-manager.js +134 -0
  134. package/dist/utils/tunnel-commands-manager.js.map +1 -0
  135. package/package.json +91 -90
  136. package/postinstall.js +4 -11
  137. package/dist/ui/components/MultiLineInput.js +0 -255
  138. package/dist/ui/components/MultiLineInput.js.map +0 -1
@@ -1,4 +1,5 @@
1
1
  import stripAnsi from "strip-ansi";
2
+ import { matchTunnelableCommand } from "../utils/tunnel-commands-manager.js";
2
3
  const SSH_COMMAND_PATTERNS = [
3
4
  // ssh user@host, ssh -p port user@host, ssh -i key user@host
4
5
  /^ssh\s+(?:(?:-\w+\s+\S+\s+)*)?(?:(\S+)@)?(\S+)/i
@@ -15,8 +16,8 @@ const DOCKER_COMMAND_PATTERNS = [
15
16
  // docker run -it image bash
16
17
  /^docker\s+run\s+(?:(?:-\w+(?:\s+\S+)?\s+)*)?(\S+)/i
17
18
  ];
18
- function detectWarpifySession(command, output) {
19
- const commandSession = detectFromCommand(command);
19
+ function detectWarpifySession(command, output, customTunnelCommands = []) {
20
+ const commandSession = detectFromCommand(command, customTunnelCommands);
20
21
  if (commandSession.type !== "none") {
21
22
  return commandSession;
22
23
  }
@@ -32,7 +33,7 @@ function detectWarpifySession(command, output) {
32
33
  confidence: "high"
33
34
  };
34
35
  }
35
- function detectFromCommand(command) {
36
+ function detectFromCommand(command, customTunnelCommands = []) {
36
37
  if (!command) {
37
38
  return { type: "none", detectedFrom: "none", confidence: "high" };
38
39
  }
@@ -74,6 +75,15 @@ function detectFromCommand(command) {
74
75
  };
75
76
  }
76
77
  }
78
+ const matchedTunnelCommand = matchTunnelableCommand(trimmedCommand, customTunnelCommands);
79
+ if (matchedTunnelCommand) {
80
+ return {
81
+ type: "custom",
82
+ connectionString: matchedTunnelCommand,
83
+ detectedFrom: "command",
84
+ confidence: "high"
85
+ };
86
+ }
77
87
  return { type: "none", detectedFrom: "none", confidence: "high" };
78
88
  }
79
89
  function detectFromOutput(output) {
@@ -120,6 +130,8 @@ function getSessionDescription(session) {
120
130
  return session.connectionString ? `WSL: ${session.connectionString}` : "WSL session detected";
121
131
  case "docker":
122
132
  return session.connectionString ? `Docker container: ${session.connectionString}` : "Docker session detected";
133
+ case "custom":
134
+ return session.connectionString ? `Custom tunnel command: ${session.connectionString}` : "Custom tunnel command detected";
123
135
  default:
124
136
  return "No remote session detected";
125
137
  }
@@ -131,8 +143,8 @@ class WarpifyDetector {
131
143
  * @param command - The shell command that started the session
132
144
  * @param output - Optional PTY output for fallback detection
133
145
  */
134
- detect(command, output) {
135
- const session = detectWarpifySession(command, output);
146
+ detect(command, output, customTunnelCommands = []) {
147
+ const session = detectWarpifySession(command, output, customTunnelCommands);
136
148
  this.lastDetection = session;
137
149
  return session;
138
150
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/services/warpify-detector.ts"],"sourcesContent":["/**\r\n * Warpify Detector Service\r\n * \r\n * Detects if the current PTY session has an active warpifiable remote session\r\n * (SSH, WSL, Docker) by analyzing:\r\n * 1. The command that started the shell (most reliable)\r\n * 2. Terminal output patterns (fallback)\r\n */\r\n\r\nimport stripAnsi from 'strip-ansi';\r\n\r\nexport type WarpifySessionType = 'ssh' | 'wsl' | 'docker' | 'none';\r\n\r\nexport interface WarpifySession {\r\n type: WarpifySessionType;\r\n connectionString?: string; // e.g., \"user@hostname\" for SSH, \"Ubuntu\" for WSL\r\n detectedFrom: 'command' | 'prompt' | 'env' | 'none';\r\n confidence: 'high' | 'medium' | 'low';\r\n}\r\n\r\n/**\r\n * SSH command patterns - detect based on the shell command\r\n */\r\nconst SSH_COMMAND_PATTERNS = [\r\n // ssh user@host, ssh -p port user@host, ssh -i key user@host\r\n /^ssh\\s+(?:(?:-\\w+\\s+\\S+\\s+)*)?(?:(\\S+)@)?(\\S+)/i,\r\n];\r\n\r\n/**\r\n * WSL command patterns\r\n */\r\nconst WSL_COMMAND_PATTERNS = [\r\n // wsl, wsl -d distro, wsl --distribution distro\r\n /^wsl(?:\\s+(?:-d|--distribution)\\s+(\\S+))?/i,\r\n // bash (when on Windows, often means WSL)\r\n /^bash$/i,\r\n];\r\n\r\n/**\r\n * Docker command patterns\r\n */\r\nconst DOCKER_COMMAND_PATTERNS = [\r\n // docker exec -it container bash\r\n /^docker\\s+exec\\s+(?:(?:-\\w+\\s+)*)?(\\S+)/i,\r\n // docker run -it image bash\r\n /^docker\\s+run\\s+(?:(?:-\\w+(?:\\s+\\S+)?\\s+)*)?(\\S+)/i,\r\n];\r\n\r\n/**\r\n * Detect warpifiable session - PRIMARY METHOD\r\n * Uses the command that started the shell for reliable detection\r\n * \r\n * @param command - The command that started the shell session (e.g., \"ssh rohan@localhost\")\r\n * @param output - Optional PTY output for fallback detection\r\n */\r\nexport function detectWarpifySession(command: string, output?: string): WarpifySession {\r\n // First, try command-based detection (most reliable)\r\n const commandSession = detectFromCommand(command);\r\n if (commandSession.type !== 'none') {\r\n return commandSession;\r\n }\r\n\r\n // Fallback: try output-based detection\r\n if (output) {\r\n const outputSession = detectFromOutput(output);\r\n if (outputSession.type !== 'none') {\r\n return outputSession;\r\n }\r\n }\r\n\r\n return {\r\n type: 'none',\r\n detectedFrom: 'none',\r\n confidence: 'high',\r\n };\r\n}\r\n\r\n/**\r\n * Detect session type from the shell command\r\n */\r\nfunction detectFromCommand(command: string): WarpifySession {\r\n if (!command) {\r\n return { type: 'none', detectedFrom: 'none', confidence: 'high' };\r\n }\r\n\r\n const trimmedCommand = command.trim();\r\n\r\n // Check for SSH command\r\n for (const pattern of SSH_COMMAND_PATTERNS) {\r\n const match = trimmedCommand.match(pattern);\r\n if (match) {\r\n const user = match[1] || 'user';\r\n const host = match[2] || 'remote';\r\n return {\r\n type: 'ssh',\r\n connectionString: `${user}@${host}`,\r\n detectedFrom: 'command',\r\n confidence: 'high',\r\n };\r\n }\r\n }\r\n\r\n // Check for WSL command\r\n for (const pattern of WSL_COMMAND_PATTERNS) {\r\n const match = trimmedCommand.match(pattern);\r\n if (match) {\r\n const distro = match[1] || 'Ubuntu';\r\n return {\r\n type: 'wsl',\r\n connectionString: distro,\r\n detectedFrom: 'command',\r\n confidence: 'high',\r\n };\r\n }\r\n }\r\n\r\n // Check for Docker command\r\n for (const pattern of DOCKER_COMMAND_PATTERNS) {\r\n const match = trimmedCommand.match(pattern);\r\n if (match) {\r\n const container = match[1]?.substring(0, 12) || 'container';\r\n return {\r\n type: 'docker',\r\n connectionString: container,\r\n detectedFrom: 'command',\r\n confidence: 'high',\r\n };\r\n }\r\n }\r\n\r\n return { type: 'none', detectedFrom: 'none', confidence: 'high' };\r\n}\r\n\r\n/**\r\n * Detect session from PTY output (fallback method)\r\n * Used when the command was not an obvious SSH/WSL/Docker command\r\n */\r\nfunction detectFromOutput(output: string): WarpifySession {\r\n // Strip ANSI codes for reliable pattern matching\r\n const cleanOutput = stripAnsi(output);\r\n const recentOutput = getRecentLines(cleanOutput, 30);\r\n\r\n // Check for SSH environment variables (very reliable)\r\n if (/SSH_CLIENT=|SSH_CONNECTION=|SSH_TTY=/.test(recentOutput)) {\r\n // Try to extract connection info from prompt\r\n const promptMatch = recentOutput.match(/([a-zA-Z0-9_-]+)@([a-zA-Z0-9._-]+)/);\r\n return {\r\n type: 'ssh',\r\n connectionString: promptMatch ? `${promptMatch[1]}@${promptMatch[2]}` : undefined,\r\n detectedFrom: 'env',\r\n confidence: 'high',\r\n };\r\n }\r\n\r\n // Check for WSL indicators\r\n if (/\\/mnt\\/[a-z]\\//.test(recentOutput) || /microsoft-standard-WSL/i.test(recentOutput)) {\r\n const promptMatch = recentOutput.match(/([a-zA-Z0-9_-]+)@([a-zA-Z0-9._-]+)/);\r\n return {\r\n type: 'wsl',\r\n connectionString: promptMatch?.[2] || 'WSL',\r\n detectedFrom: 'prompt',\r\n confidence: 'high',\r\n };\r\n }\r\n\r\n // Check for Docker indicators\r\n if (/docker|container/i.test(recentOutput) || /^[a-f0-9]{12}:/.test(recentOutput)) {\r\n const containerMatch = recentOutput.match(/@([a-f0-9]{12})/);\r\n return {\r\n type: 'docker',\r\n connectionString: containerMatch?.[1] || 'container',\r\n detectedFrom: 'prompt',\r\n confidence: 'medium',\r\n };\r\n }\r\n\r\n return { type: 'none', detectedFrom: 'none', confidence: 'high' };\r\n}\r\n\r\n/**\r\n * Get the last N lines of output\r\n */\r\nfunction getRecentLines(output: string, n: number): string {\r\n const lines = output.split('\\n');\r\n return lines.slice(-n).join('\\n');\r\n}\r\n\r\n/**\r\n * Get a human-readable description of the detected session\r\n */\r\nexport function getSessionDescription(session: WarpifySession): string {\r\n switch (session.type) {\r\n case 'ssh':\r\n return session.connectionString\r\n ? `SSH session: ${session.connectionString}`\r\n : 'SSH session detected';\r\n case 'wsl':\r\n return session.connectionString\r\n ? `WSL: ${session.connectionString}`\r\n : 'WSL session detected';\r\n case 'docker':\r\n return session.connectionString\r\n ? `Docker container: ${session.connectionString}`\r\n : 'Docker session detected';\r\n default:\r\n return 'No remote session detected';\r\n }\r\n}\r\n\r\n/**\r\n * WarpifyDetector class for stateful detection\r\n */\r\nexport class WarpifyDetector {\r\n private lastDetection: WarpifySession | null = null;\r\n\r\n /**\r\n * Detect warpifiable session\r\n * @param command - The shell command that started the session\r\n * @param output - Optional PTY output for fallback detection\r\n */\r\n detect(command: string, output?: string): WarpifySession {\r\n const session = detectWarpifySession(command, output);\r\n this.lastDetection = session;\r\n return session;\r\n }\r\n\r\n getLastDetection(): WarpifySession | null {\r\n return this.lastDetection;\r\n }\r\n\r\n reset(): void {\r\n this.lastDetection = null;\r\n }\r\n}\r\n\r\nexport const warpifyDetector = new WarpifyDetector();\r\n"],"mappings":"AASA,OAAO,eAAe;AActB,MAAM,uBAAuB;AAAA;AAAA,EAEzB;AACJ;AAKA,MAAM,uBAAuB;AAAA;AAAA,EAEzB;AAAA;AAAA,EAEA;AACJ;AAKA,MAAM,0BAA0B;AAAA;AAAA,EAE5B;AAAA;AAAA,EAEA;AACJ;AASO,SAAS,qBAAqB,SAAiB,QAAiC;AAEnF,QAAM,iBAAiB,kBAAkB,OAAO;AAChD,MAAI,eAAe,SAAS,QAAQ;AAChC,WAAO;AAAA,EACX;AAGA,MAAI,QAAQ;AACR,UAAM,gBAAgB,iBAAiB,MAAM;AAC7C,QAAI,cAAc,SAAS,QAAQ;AAC/B,aAAO;AAAA,IACX;AAAA,EACJ;AAEA,SAAO;AAAA,IACH,MAAM;AAAA,IACN,cAAc;AAAA,IACd,YAAY;AAAA,EAChB;AACJ;AAKA,SAAS,kBAAkB,SAAiC;AACxD,MAAI,CAAC,SAAS;AACV,WAAO,EAAE,MAAM,QAAQ,cAAc,QAAQ,YAAY,OAAO;AAAA,EACpE;A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AAK,gBAAgB;AAAA,EACzB;AACJ;AAEO,MAAM,kBAAkB,IAAI,gBAAgB;","names":[]}
1
+ {"version":3,"sources":["../../src/services/warpify-detector.ts"],"sourcesContent":["/**\r\n * Warpify Detector Service\r\n * \r\n * Detects if the current PTY session has an active warpifiable remote session\r\n * (SSH, WSL, Docker) by analyzing:\r\n * 1. The command that started the shell (most reliable)\r\n * 2. Terminal output patterns (fallback)\r\n */\r\n\r\nimport stripAnsi from 'strip-ansi';\r\nimport { matchTunnelableCommand } from '../utils/tunnel-commands-manager.js';\r\n\r\nexport type WarpifySessionType = 'ssh' | 'wsl' | 'docker' | 'custom' | 'none';\r\n\r\nexport interface WarpifySession {\r\n type: WarpifySessionType;\r\n connectionString?: string; // e.g., \"user@hostname\" for SSH, \"Ubuntu\" for WSL\r\n detectedFrom: 'command' | 'prompt' | 'env' | 'none';\r\n confidence: 'high' | 'medium' | 'low';\r\n}\r\n\r\n/**\r\n * SSH command patterns - detect based on the shell command\r\n */\r\nconst SSH_COMMAND_PATTERNS = [\r\n // ssh user@host, ssh -p port user@host, ssh -i key user@host\r\n /^ssh\\s+(?:(?:-\\w+\\s+\\S+\\s+)*)?(?:(\\S+)@)?(\\S+)/i,\r\n];\r\n\r\n/**\r\n * WSL command patterns\r\n */\r\nconst WSL_COMMAND_PATTERNS = [\r\n // wsl, wsl -d distro, wsl --distribution distro\r\n /^wsl(?:\\s+(?:-d|--distribution)\\s+(\\S+))?/i,\r\n // bash (when on Windows, often means WSL)\r\n /^bash$/i,\r\n];\r\n\r\n/**\r\n * Docker command patterns\r\n */\r\nconst DOCKER_COMMAND_PATTERNS = [\r\n // docker exec -it container bash\r\n /^docker\\s+exec\\s+(?:(?:-\\w+\\s+)*)?(\\S+)/i,\r\n // docker run -it image bash\r\n /^docker\\s+run\\s+(?:(?:-\\w+(?:\\s+\\S+)?\\s+)*)?(\\S+)/i,\r\n];\r\n\r\n/**\r\n * Detect warpifiable session - PRIMARY METHOD\r\n * Uses the command that started the shell for reliable detection\r\n * \r\n * @param command - The command that started the shell session (e.g., \"ssh rohan@localhost\")\r\n * @param output - Optional PTY output for fallback detection\r\n */\r\nexport function detectWarpifySession(command: string, output?: string, customTunnelCommands: string[] = []): WarpifySession {\r\n // First, try command-based detection (most reliable)\r\n const commandSession = detectFromCommand(command, customTunnelCommands);\r\n if (commandSession.type !== 'none') {\r\n return commandSession;\r\n }\r\n\r\n // Fallback: try output-based detection\r\n if (output) {\r\n const outputSession = detectFromOutput(output);\r\n if (outputSession.type !== 'none') {\r\n return outputSession;\r\n }\r\n }\r\n\r\n return {\r\n type: 'none',\r\n detectedFrom: 'none',\r\n confidence: 'high',\r\n };\r\n}\r\n\r\n/**\r\n * Detect session type from the shell command\r\n */\r\nfunction detectFromCommand(command: string, customTunnelCommands: string[] = []): WarpifySession {\r\n if (!command) {\r\n return { type: 'none', detectedFrom: 'none', confidence: 'high' };\r\n }\r\n\r\n const trimmedCommand = command.trim();\r\n\r\n // Check for SSH command\r\n for (const pattern of SSH_COMMAND_PATTERNS) {\r\n const match = trimmedCommand.match(pattern);\r\n if (match) {\r\n const user = match[1] || 'user';\r\n const host = match[2] || 'remote';\r\n return {\r\n type: 'ssh',\r\n connectionString: `${user}@${host}`,\r\n detectedFrom: 'command',\r\n confidence: 'high',\r\n };\r\n }\r\n }\r\n\r\n // Check for WSL command\r\n for (const pattern of WSL_COMMAND_PATTERNS) {\r\n const match = trimmedCommand.match(pattern);\r\n if (match) {\r\n const distro = match[1] || 'Ubuntu';\r\n return {\r\n type: 'wsl',\r\n connectionString: distro,\r\n detectedFrom: 'command',\r\n confidence: 'high',\r\n };\r\n }\r\n }\r\n\r\n // Check for Docker command\r\n for (const pattern of DOCKER_COMMAND_PATTERNS) {\r\n const match = trimmedCommand.match(pattern);\r\n if (match) {\r\n const container = match[1]?.substring(0, 12) || 'container';\r\n return {\r\n type: 'docker',\r\n connectionString: container,\r\n detectedFrom: 'command',\r\n confidence: 'high',\r\n };\r\n }\r\n }\r\n\r\n const matchedTunnelCommand = matchTunnelableCommand(trimmedCommand, customTunnelCommands);\r\n if (matchedTunnelCommand) {\r\n return {\r\n type: 'custom',\r\n connectionString: matchedTunnelCommand,\r\n detectedFrom: 'command',\r\n confidence: 'high',\r\n };\r\n }\r\n\r\n return { type: 'none', detectedFrom: 'none', confidence: 'high' };\r\n}\r\n\r\n/**\r\n * Detect session from PTY output (fallback method)\r\n * Used when the command was not an obvious SSH/WSL/Docker command\r\n */\r\nfunction detectFromOutput(output: string): WarpifySession {\r\n // Strip ANSI codes for reliable pattern matching\r\n const cleanOutput = stripAnsi(output);\r\n const recentOutput = getRecentLines(cleanOutput, 30);\r\n\r\n // Check for SSH environment variables (very reliable)\r\n if (/SSH_CLIENT=|SSH_CONNECTION=|SSH_TTY=/.test(recentOutput)) {\r\n // Try to extract connection info from prompt\r\n const promptMatch = recentOutput.match(/([a-zA-Z0-9_-]+)@([a-zA-Z0-9._-]+)/);\r\n return {\r\n type: 'ssh',\r\n connectionString: promptMatch ? `${promptMatch[1]}@${promptMatch[2]}` : undefined,\r\n detectedFrom: 'env',\r\n confidence: 'high',\r\n };\r\n }\r\n\r\n // Check for WSL indicators\r\n if (/\\/mnt\\/[a-z]\\//.test(recentOutput) || /microsoft-standard-WSL/i.test(recentOutput)) {\r\n const promptMatch = recentOutput.match(/([a-zA-Z0-9_-]+)@([a-zA-Z0-9._-]+)/);\r\n return {\r\n type: 'wsl',\r\n connectionString: promptMatch?.[2] || 'WSL',\r\n detectedFrom: 'prompt',\r\n confidence: 'high',\r\n };\r\n }\r\n\r\n // Check for Docker indicators\r\n if (/docker|container/i.test(recentOutput) || /^[a-f0-9]{12}:/.test(recentOutput)) {\r\n const containerMatch = recentOutput.match(/@([a-f0-9]{12})/);\r\n return {\r\n type: 'docker',\r\n connectionString: containerMatch?.[1] || 'container',\r\n detectedFrom: 'prompt',\r\n confidence: 'medium',\r\n };\r\n }\r\n\r\n return { type: 'none', detectedFrom: 'none', confidence: 'high' };\r\n}\r\n\r\n/**\r\n * Get the last N lines of output\r\n */\r\nfunction getRecentLines(output: string, n: number): string {\r\n const lines = output.split('\\n');\r\n return lines.slice(-n).join('\\n');\r\n}\r\n\r\n/**\r\n * Get a human-readable description of the detected session\r\n */\r\nexport function getSessionDescription(session: WarpifySession): string {\r\n switch (session.type) {\r\n case 'ssh':\r\n return session.connectionString\r\n ? `SSH session: ${session.connectionString}`\r\n : 'SSH session detected';\r\n case 'wsl':\r\n return session.connectionString\r\n ? `WSL: ${session.connectionString}`\r\n : 'WSL session detected';\r\n case 'docker':\r\n return session.connectionString\r\n ? `Docker container: ${session.connectionString}`\r\n : 'Docker session detected';\r\n case 'custom':\r\n return session.connectionString\r\n ? `Custom tunnel command: ${session.connectionString}`\r\n : 'Custom tunnel command detected';\r\n default:\r\n return 'No remote session detected';\r\n }\r\n}\r\n\r\n/**\r\n * WarpifyDetector class for stateful detection\r\n */\r\nexport class WarpifyDetector {\r\n private lastDetection: WarpifySession | null = null;\r\n\r\n /**\r\n * Detect warpifiable session\r\n * @param command - The shell command that started the session\r\n * @param output - Optional PTY output for fallback detection\r\n */\r\n detect(command: string, output?: string, customTunnelCommands: string[] = []): WarpifySession {\r\n const session = detectWarpifySession(command, output, customTunnelCommands);\r\n this.lastDetection = session;\r\n return session;\r\n }\r\n\r\n getLastDetection(): WarpifySession | null {\r\n return this.lastDetection;\r\n }\r\n\r\n reset(): void {\r\n this.lastDetection = null;\r\n }\r\n}\r\n\r\nexport const warpifyDetector = new WarpifyDetector();\r\n"],"mappings":"AASA,OAAO,eAAe;AACtB,SAAS,8BAA8B;AAcvC,MAAM,uBAAuB;AAAA;AAAA,EAEzB;AACJ;AAKA,MAAM,uBAAuB;AAAA;AAAA,EAEzB;AAAA;AAAA,EAEA;AACJ;AAKA,MAAM,0BAA0B;AAAA;AAAA,EAE5B;AAAA;AAAA,EAEA;AACJ;AASO,SAAS,qBAAqB,SAAiB,QAAiB,uBAAiC,CAAC,GAAmB;AAExH,QAAM,iBAAiB,kB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,EAAE,KAAK,IAAI;AACpC;AAKO,SAAS,sBAAsB,SAAiC;AACnE,UAAQ,QAAQ,MAAM;AAAA,IAClB,KAAK;AACD,aAAO,QAAQ,mBACT,gBAAgB,QAAQ,gBAAgB,KACxC;AAAA,IACV,KAAK;AACD,aAAO,QAAQ,mBACT,QAAQ,QAAQ,gBAAgB,KAChC;AAAA,IACV,KAAK;AACD,aAAO,QAAQ,mBACT,qBAAqB,QAAQ,gBAAgB,KAC7C;AAAA,IACV,KAAK;AACD,aAAO,QAAQ,mBACT,0BAA0B,QAAQ,gBAAgB,KAClD;AAAA,IACV;AACI,aAAO;AAAA,EACf;AACJ;AAKO,MAAM,gBAAgB;AAAA,EACjB,gBAAuC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAO/C,OAAO,SAAiB,QAAiB,uBAAiC,CAAC,GAAmB;AAC1F,UAAM,UAAU,qBAAqB,SAAS,QAAQ,oBAAoB;AAC1E,SAAK,gBAAgB;AACrB,WAAO;AAAA,EACX;AAAA,EAEA,mBAA0C;AACtC,WAAO,KAAK;AAAA,EAChB;AAAA,EAEA,QAAc;AACV,SAAK,gBAAgB;AAAA,EACzB;AACJ;AAEO,MAAM,kBAAkB,IAAI,gBAAgB;","names":[]}
@@ -1,5 +1,7 @@
1
1
  import { BackgroundTaskManager } from "../services/background-task-manager.js";
2
2
  import { runSSHCommand, runLocalPty } from "../utils/editor-utils.js";
3
+ import { processTerminalOutput } from "../utils/terminal-output.js";
4
+ import stripAnsi from "strip-ansi";
3
5
  import * as path from "path";
4
6
  function resolveBackgroundCommandCwd(currentContext, contextCwd, commandCwd) {
5
7
  if (currentContext.type === "local") {
@@ -158,8 +160,9 @@ IMPORTANT:
158
160
  Available tasks:
159
161
  ${taskList}`);
160
162
  }
161
- const fullOutput = task.output;
162
- const lines = fullOutput.split("\n");
163
+ const rawOutput = task.output;
164
+ const cleanOutput = stripAnsi(processTerminalOutput(rawOutput)).replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, "");
165
+ const lines = cleanOutput.split("\n");
163
166
  const outputToShow = lines.slice(-output_lines).join("\n");
164
167
  const truncated = lines.length > output_lines;
165
168
  const durationMs = Date.now() - task.startTime.getTime();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/tools/background-command.ts"],"sourcesContent":["/**\r\n * Background Command Tool\r\n * \r\n * Allows the AI to execute commands in the background (non-blocking),\r\n * check their status, and kill/terminate them.\r\n * \r\n * Uses the existing BackgroundTaskManager singleton.\r\n */\r\n\r\nimport { Tool } from './types.js';\r\nimport { BackgroundTaskManager } from '../services/background-task-manager.js';\r\nimport { runSSHCommand, runLocalPty } from '../utils/editor-utils.js';\r\nimport { ContextManager } from '../context/context-manager.js';\r\nimport * as path from 'path';\r\n\r\nfunction resolveBackgroundCommandCwd(currentContext: any, contextCwd: string, commandCwd: string): string {\r\n if (currentContext.type === 'local') {\r\n return path.resolve(contextCwd, commandCwd);\r\n }\r\n\r\n const remoteBase = currentContext.metadata?.workingDirectory || contextCwd || '~';\r\n return path.posix.isAbsolute(commandCwd)\r\n ? commandCwd\r\n : path.posix.join(remoteBase, commandCwd);\r\n}\r\n\r\n\r\nexport const backgroundCommandTool: Tool = {\r\n schema: {\r\n name: 'background_command',\r\n description: `Execute shell commands in the background, check their status, or kill them. This tool is NON-BLOCKING - when you start a command, the tool returns immediately with a task ID while the command continues running in the background.\r\n\r\nUSE CASES:\r\n- Running development servers (npm run dev, python -m http.server, etc.)\r\n- Running long-running processes (builds, tests, watchers)\r\n- Running multiple commands concurrently\r\n\r\nACTIONS:\r\n1. \"start\" - Start a new background command. Returns a task_id immediately.\r\n2. \"status\" - Check the status and output of a background task. Use this to monitor progress.\r\n3. \"kill\" - Terminate a running background task.\r\n4. \"wait\" - Pause execution for a specified duration. Use this instead of repeatedly checking status.\r\n\r\nIMPORTANT:\r\n- After starting a task, you can continue with other work while it runs.\r\n- Use \"status\" periodically to check on long-running tasks.\r\n- Use \"wait\" to pause before checking status again (e.g., wait 30 seconds for a build to complete).\r\n- Always clean up tasks with \"kill\" when they're no longer needed.\r\n- The task_id returned from \"start\" must be saved to use with \"status\" or \"kill\".`,\r\n parameters: {\r\n type: 'object',\r\n properties: {\r\n reason_text: {\r\n type: 'string',\r\n description: 'REQUIRED: A brief explanation of what you are doing with this background command.',\r\n },\r\n action: {\r\n type: 'string',\r\n enum: ['start', 'status', 'kill', 'wait'],\r\n description: 'The action to perform: \"start\" to run a new command, \"status\" to check a task, \"kill\" to terminate a task, \"wait\" to pause execution.',\r\n },\r\n command: {\r\n type: 'string',\r\n description: 'The shell command to execute. REQUIRED when action is \"start\".',\r\n },\r\n cwd: {\r\n type: 'string',\r\n description: 'The working directory for the command. REQUIRED when action is \"start\".',\r\n },\r\n task_id: {\r\n type: 'string',\r\n description: 'The task ID to check or kill. REQUIRED when action is \"status\" or \"kill\".',\r\n },\r\n output_lines: {\r\n type: 'integer',\r\n description: 'Number of lines of output to return (from the end). Default: 50. Only used with \"status\" action.',\r\n },\r\n wait_seconds: {\r\n type: 'integer',\r\n description: 'Number of seconds to wait. REQUIRED when action is \"wait\". Use this to pause before checking task status again.',\r\n },\r\n },\r\n required: ['reason_text', 'action'],\r\n },\r\n },\r\n async execute(args, context) {\r\n const { action, command, cwd, task_id, output_lines = 50, wait_seconds } = args;\r\n\r\n switch (action) {\r\n case 'start': {\r\n // Validate required parameters for start\r\n if (!command) {\r\n throw new Error('Missing required parameter \"command\" for action \"start\"');\r\n }\r\n if (!cwd) {\r\n throw new Error('Missing required parameter \"cwd\" for action \"start\"');\r\n }\r\n\r\n // Check for remote context\r\n const contextManager = context.contextManager as ContextManager;\r\n const activeContext = contextManager.getCurrentContext();\r\n\r\n // Get the full context stack to handle nested environments\r\n // If getContextStack is not available (older version), fall back to checking current context\r\n let stack: any[] = [];\r\n if (typeof contextManager.getContextStack === 'function') {\r\n stack = contextManager.getContextStack();\r\n } else {\r\n // Fallback for safety\r\n const current = contextManager.getCurrentContext();\r\n if (current) stack = [current];\r\n }\r\n\r\n let currentCommand = command;\r\n\r\n // Iterate from the target context (top of stack) down to the root\r\n for (let i = stack.length - 1; i >= 0; i--) {\r\n const ctx = stack[i];\r\n const isTarget = i === stack.length - 1;\r\n\r\n // Determine the working directory to use for this execution layer\r\n // If this is the target layer, use the user-provided cwd\r\n // If this is an intermediate layer, use its tracked working directory\r\n const executionCwd = isTarget ? cwd : ctx.metadata.workingDirectory;\r\n\r\n // 1. Check if this context has a direct protocol executor (e.g. active SSH client)\r\n if (ctx.type === 'ssh' && ctx.handler?.client) {\r\n const sshClient = ctx.handler.client;\r\n\r\n // Start task tracking\r\n const remoteTask = BackgroundTaskManager.startRemoteTask(\r\n command, // Original command for display\r\n cwd, // Original CWD for display\r\n ctx.type // Context type\r\n );\r\n\r\n // Execute via SSH PTY\r\n const sshPty = runSSHCommand(\r\n sshClient,\r\n currentCommand, // The accumulated (possibly wrapped) command\r\n executionCwd,\r\n remoteTask.onData,\r\n remoteTask.onExit\r\n );\r\n remoteTask.setRemotePty(sshPty);\r\n\r\n return formatStartResponse(remoteTask.id, command, cwd, ctx.type);\r\n }\r\n\r\n // 2. Check if this is the local root context\r\n if (ctx.type === 'local') {\r\n // Start task tracking\r\n const remoteTask = BackgroundTaskManager.startRemoteTask(\r\n command,\r\n cwd,\r\n 'local'\r\n );\r\n\r\n // Execute via Local PTY (which might be running a wrapped command like \"wsl ...\" or \"docker ...\")\r\n const localPty = runLocalPty(\r\n currentCommand,\r\n executionCwd, // Use the local CWD\r\n remoteTask.onData,\r\n remoteTask.onExit\r\n );\r\n remoteTask.setRemotePty(localPty);\r\n\r\n return formatStartResponse(remoteTask.id, command, cwd, 'local');\r\n }\r\n\r\n // 3. Otherwise, WRAP the command for the next iteration (parent)\r\n if (ctx.type === 'docker') {\r\n const containerId = ctx.metadata.containerId;\r\n if (!containerId) throw new Error('Docker context missing containerId');\r\n currentCommand = buildDockerCommand(containerId, executionCwd, currentCommand);\r\n } else if (ctx.type === 'wsl') {\r\n const distro = ctx.metadata.distroName || 'Ubuntu';\r\n currentCommand = buildWSLCommand(distro, executionCwd, currentCommand);\r\n } else if (ctx.type === 'ssh') {\r\n // Fallback for SSH without accessible client (e.g. wrapped command)\r\n currentCommand = buildSSHCommand(ctx.metadata, executionCwd, currentCommand);\r\n } else {\r\n // Unknown context type, try to pass through or error?\r\n // For now we error to be safe, or just wrap in sh -c?\r\n throw new Error(`Unsupported context type for wrapping: ${ctx.type}`);\r\n }\r\n }\r\n\r\n throw new Error('Failed to execute command: Reached end of context stack without execution.');\r\n }\r\n\r\n case 'status': {\r\n // Validate required parameters for status\r\n if (!task_id) {\r\n throw new Error('Missing required parameter \"task_id\" for action \"status\"');\r\n }\r\n\r\n const task = BackgroundTaskManager.getTask(task_id);\r\n if (!task) {\r\n // List all available tasks to help the AI\r\n const allTasks = BackgroundTaskManager.getAllTasks();\r\n if (allTasks.length === 0) {\r\n throw new Error(`Task \"${task_id}\" not found. There are no background tasks currently tracked.`);\r\n }\r\n const taskList = allTasks.map(t => `- ${t.id} (${t.isRunning ? 'running' : 'completed'}): ${t.command}`).join('\\n');\r\n throw new Error(`Task \"${task_id}\" not found.\\n\\nAvailable tasks:\\n${taskList}`);\r\n }\r\n\r\n // Get the output (last N lines)\r\n const fullOutput = task.output;\r\n const lines = fullOutput.split('\\n');\r\n const outputToShow = lines.slice(-output_lines).join('\\n');\r\n const truncated = lines.length > output_lines;\r\n\r\n const durationMs = Date.now() - task.startTime.getTime();\r\n const durationSec = Math.round(durationMs / 1000);\r\n\r\n let statusText = '';\r\n if (task.isRunning) {\r\n statusText = `**Status:** šŸ”„ Running (${durationSec}s)`;\r\n } else if (task.exitCode === 0) {\r\n statusText = `**Status:** āœ… Completed successfully (exit code: 0)`;\r\n } else if (task.exitCode !== undefined) {\r\n statusText = `**Status:** āŒ Failed (exit code: ${task.exitCode})`;\r\n } else if (task.error) {\r\n statusText = `**Status:** āŒ Error: ${task.error}`;\r\n } else {\r\n statusText = `**Status:** ā¹ļø Stopped`;\r\n }\r\n\r\n const remoteLabel = task.remoteContext ? ` [${task.remoteContext}]` : '';\r\n\r\n return `Background Task Status${remoteLabel}\r\n\r\n**Task ID:** ${task_id}\r\n**Command:** ${task.command}\r\n**Working Directory:** ${task.cwd}\r\n${statusText}\r\n\r\n**Output${truncated ? ` (last ${output_lines} lines)` : ''}:**\r\n\\`\\`\\`\r\n${outputToShow || '(no output yet)'}\r\n\\`\\`\\``;\r\n }\r\n\r\n case 'kill': {\r\n // Validate required parameters for kill\r\n if (!task_id) {\r\n throw new Error('Missing required parameter \"task_id\" for action \"kill\"');\r\n }\r\n\r\n const task = BackgroundTaskManager.getTask(task_id);\r\n if (!task) {\r\n // Try to find if it was recently killed or doesn't exist\r\n throw new Error(`Task \"${task_id}\" not found.`);\r\n }\r\n\r\n if (!task.isRunning) {\r\n return `Task \"${task_id}\" is not running (already completed or stopped). No action taken.`;\r\n }\r\n\r\n // For remote tasks, we might need to do special cleanup, \r\n // but BackgroundTaskManager.cancelTask() handles calling the pty.kill()\r\n const success = BackgroundTaskManager.cancelTask(task_id);\r\n if (success) {\r\n return `Background task \"${task_id}\" has been terminated.\r\n\r\n**Command:** ${task.command}\r\n**Status:** ā¹ļø Killed by request`;\r\n } else {\r\n throw new Error(`Failed to kill task \"${task_id}\". The task may have already completed.`);\r\n }\r\n }\r\n\r\n case 'wait': {\r\n // Validate required parameters for wait\r\n if (!wait_seconds) {\r\n throw new Error('Missing required parameter \"wait_seconds\" for action \"wait\"');\r\n }\r\n\r\n if (wait_seconds < 1 || wait_seconds > 300) {\r\n throw new Error('wait_seconds must be between 1 and 300 (5 minutes)');\r\n }\r\n\r\n // Create a promise that resolves after the specified duration\r\n await new Promise(resolve => setTimeout(resolve, wait_seconds * 1000));\r\n\r\n return `Waited for ${wait_seconds} second${wait_seconds !== 1 ? 's' : ''}.\r\n\r\nā±ļø The execution has been paused for the requested duration. You can now continue with the next action.`;\r\n }\r\n\r\n default:\r\n throw new Error(`Unknown action \"${action}\". Valid actions are: \"start\", \"status\", \"kill\", \"wait\".`);\r\n }\r\n },\r\n};\r\n\r\n/**\r\n * Format the successful start response\r\n */\r\nfunction formatStartResponse(taskId: string, command: string, cwd: string, contextType: string): string {\r\n return `Background task started successfully in ${contextType}.\r\n\r\n**Task ID:** ${taskId}\r\n**Command:** ${command}\r\n**Working Directory:** ${cwd}\r\n**Context:** ${contextType}\r\n\r\nThe command is now running in the background. Use action=\"status\" with task_id=\"${taskId}\" to check progress, or action=\"kill\" to terminate it.`;\r\n}\r\n\r\n/**\r\n * Helper to escape a command for use in \"sh -c\" or similar\r\n */\r\nfunction escapeCmd(cmd: string): string {\r\n // Escape backslashes first, then quotes\r\n return cmd.replace(/\\\\/g, '\\\\\\\\').replace(/\"/g, '\\\\\"');\r\n}\r\n\r\n/**\r\n * Build a Docker execution command\r\n */\r\nfunction buildDockerCommand(containerId: string, cwd: string, command: string): string {\r\n // docker exec -w <cwd> <id> sh -c \"<cmd>\"\r\n return `docker exec -w \"${cwd}\" ${containerId} sh -c \"${escapeCmd(command)}\"`;\r\n}\r\n\r\n/**\r\n * Build a WSL execution command\r\n */\r\nfunction buildWSLCommand(distro: string, cwd: string, command: string): string {\r\n // wsl -d <distro> -- bash -c \"cd <cwd> && <cmd>\"\r\n return `wsl -d ${distro} -- bash -c \"cd \\\\\"${cwd}\\\\\" && ${escapeCmd(command)}\"`;\r\n}\r\n\r\n/**\r\n * Build an SSH execution command (for wrapping)\r\n */\r\nfunction buildSSHCommand(metadata: any, cwd: string, command: string): string {\r\n const portFlag = metadata.port ? `-p ${metadata.port}` : '';\r\n const userHost = `${metadata.username}@${metadata.hostname}`;\r\n // ssh -p <port> user@host \"cd <cwd> && <cmd>\"\r\n return `ssh ${portFlag} ${userHost} \"cd \\\\\"${cwd}\\\\\" && ${escapeCmd(command)}\"`;\r\n}\r\n"],"mappings":"AAUA,SAAS,6BAA6B;AACtC,SAAS,eAAe,mBAAmB;AAE3C,YAAY,UAAU;AAEtB,SAAS,4BAA4B,gBAAqB,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,aAAW,WAAW,SAAS,eAAe,GAAI,CAAC;AAErE,eAAO,cAAc,YAAY,UAAU,iBAAiB,IAAI,MAAM,EAAE;AAAA;AAAA;AAAA,MAG5E;AAAA,MAEA;AACI,cAAM,IAAI,MAAM,mBAAmB,MAAM,0DAA0D;AAAA,IAC3G;AAAA,EACJ;AACJ;AAKA,SAAS,oBAAoB,QAAgB,SAAiB,KAAa,aAA6B;AACpG,SAAO,2CAA2C,WAAW;AAAA;AAAA,eAElD,MAAM;AAAA,eACN,OAAO;AAAA,yBACG,GAAG;AAAA,eACb,WAAW;AAAA;AAAA,kFAEwD,MAAM;AACxF;AAKA,SAAS,UAAU,KAAqB;AAEpC,SAAO,IAAI,QAAQ,OAAO,MAAM,EAAE,QAAQ,MAAM,KAAK;AACzD;AAKA,SAAS,mBAAmB,aAAqB,KAAa,SAAyB;AAEnF,SAAO,mBAAmB,GAAG,KAAK,WAAW,WAAW,UAAU,OAAO,CAAC;AAC9E;AAKA,SAAS,gBAAgB,QAAgB,KAAa,SAAyB;AAE3E,SAAO,UAAU,MAAM,sBAAsB,GAAG,UAAU,UAAU,OAAO,CAAC;AAChF;AAKA,SAAS,gBAAgB,UAAe,KAAa,SAAyB;AAC1E,QAAM,WAAW,SAAS,OAAO,MAAM,SAAS,IAAI,KAAK;AACzD,QAAM,WAAW,GAAG,SAAS,QAAQ,IAAI,SAAS,QAAQ;AAE1D,SAAO,OAAO,QAAQ,IAAI,QAAQ,WAAW,GAAG,UAAU,UAAU,OAAO,CAAC;AAChF;","names":[]}
1
+ {"version":3,"sources":["../../src/tools/background-command.ts"],"sourcesContent":["/**\r\n * Background Command Tool\r\n * \r\n * Allows the AI to execute commands in the background (non-blocking),\r\n * check their status, and kill/terminate them.\r\n * \r\n * Uses the existing BackgroundTaskManager singleton.\r\n */\r\n\r\nimport { Tool } from './types.js';\r\nimport { BackgroundTaskManager } from '../services/background-task-manager.js';\r\nimport { runSSHCommand, runLocalPty } from '../utils/editor-utils.js';\r\nimport { ContextManager } from '../context/context-manager.js';\r\nimport { processTerminalOutput } from '../utils/terminal-output.js';\r\nimport stripAnsi from 'strip-ansi';\r\nimport * as path from 'path';\r\n\r\nfunction resolveBackgroundCommandCwd(currentContext: any, contextCwd: string, commandCwd: string): string {\r\n if (currentContext.type === 'local') {\r\n return path.resolve(contextCwd, commandCwd);\r\n }\r\n\r\n const remoteBase = currentContext.metadata?.workingDirectory || contextCwd || '~';\r\n return path.posix.isAbsolute(commandCwd)\r\n ? commandCwd\r\n : path.posix.join(remoteBase, commandCwd);\r\n}\r\n\r\n\r\nexport const backgroundCommandTool: Tool = {\r\n schema: {\r\n name: 'background_command',\r\n description: `Execute shell commands in the background, check their status, or kill them. This tool is NON-BLOCKING - when you start a command, the tool returns immediately with a task ID while the command continues running in the background.\r\n\r\nUSE CASES:\r\n- Running development servers (npm run dev, python -m http.server, etc.)\r\n- Running long-running processes (builds, tests, watchers)\r\n- Running multiple commands concurrently\r\n\r\nACTIONS:\r\n1. \"start\" - Start a new background command. Returns a task_id immediately.\r\n2. \"status\" - Check the status and output of a background task. Use this to monitor progress.\r\n3. \"kill\" - Terminate a running background task.\r\n4. \"wait\" - Pause execution for a specified duration. Use this instead of repeatedly checking status.\r\n\r\nIMPORTANT:\r\n- After starting a task, you can continue with other work while it runs.\r\n- Use \"status\" periodically to check on long-running tasks.\r\n- Use \"wait\" to pause before checking status again (e.g., wait 30 seconds for a build to complete).\r\n- Always clean up tasks with \"kill\" when they're no longer needed.\r\n- The task_id returned from \"start\" must be saved to use with \"status\" or \"kill\".`,\r\n parameters: {\r\n type: 'object',\r\n properties: {\r\n reason_text: {\r\n type: 'string',\r\n description: 'REQUIRED: A brief explanation of what you are doing with this background command.',\r\n },\r\n action: {\r\n type: 'string',\r\n enum: ['start', 'status', 'kill', 'wait'],\r\n description: 'The action to perform: \"start\" to run a new command, \"status\" to check a task, \"kill\" to terminate a task, \"wait\" to pause execution.',\r\n },\r\n command: {\r\n type: 'string',\r\n description: 'The shell command to execute. REQUIRED when action is \"start\".',\r\n },\r\n cwd: {\r\n type: 'string',\r\n description: 'The working directory for the command. REQUIRED when action is \"start\".',\r\n },\r\n task_id: {\r\n type: 'string',\r\n description: 'The task ID to check or kill. REQUIRED when action is \"status\" or \"kill\".',\r\n },\r\n output_lines: {\r\n type: 'integer',\r\n description: 'Number of lines of output to return (from the end). Default: 50. Only used with \"status\" action.',\r\n },\r\n wait_seconds: {\r\n type: 'integer',\r\n description: 'Number of seconds to wait. REQUIRED when action is \"wait\". Use this to pause before checking task status again.',\r\n },\r\n },\r\n required: ['reason_text', 'action'],\r\n },\r\n },\r\n async execute(args, context) {\r\n const { action, command, cwd, task_id, output_lines = 50, wait_seconds } = args;\r\n\r\n switch (action) {\r\n case 'start': {\r\n // Validate required parameters for start\r\n if (!command) {\r\n throw new Error('Missing required parameter \"command\" for action \"start\"');\r\n }\r\n if (!cwd) {\r\n throw new Error('Missing required parameter \"cwd\" for action \"start\"');\r\n }\r\n\r\n // Check for remote context\r\n const contextManager = context.contextManager as ContextManager;\r\n const activeContext = contextManager.getCurrentContext();\r\n\r\n // Get the full context stack to handle nested environments\r\n // If getContextStack is not available (older version), fall back to checking current context\r\n let stack: any[] = [];\r\n if (typeof contextManager.getContextStack === 'function') {\r\n stack = contextManager.getContextStack();\r\n } else {\r\n // Fallback for safety\r\n const current = contextManager.getCurrentContext();\r\n if (current) stack = [current];\r\n }\r\n\r\n let currentCommand = command;\r\n\r\n // Iterate from the target context (top of stack) down to the root\r\n for (let i = stack.length - 1; i >= 0; i--) {\r\n const ctx = stack[i];\r\n const isTarget = i === stack.length - 1;\r\n\r\n // Determine the working directory to use for this execution layer\r\n // If this is the target layer, use the user-provided cwd\r\n // If this is an intermediate layer, use its tracked working directory\r\n const executionCwd = isTarget ? cwd : ctx.metadata.workingDirectory;\r\n\r\n // 1. Check if this context has a direct protocol executor (e.g. active SSH client)\r\n if (ctx.type === 'ssh' && ctx.handler?.client) {\r\n const sshClient = ctx.handler.client;\r\n\r\n // Start task tracking\r\n const remoteTask = BackgroundTaskManager.startRemoteTask(\r\n command, // Original command for display\r\n cwd, // Original CWD for display\r\n ctx.type // Context type\r\n );\r\n\r\n // Execute via SSH PTY\r\n const sshPty = runSSHCommand(\r\n sshClient,\r\n currentCommand, // The accumulated (possibly wrapped) command\r\n executionCwd,\r\n remoteTask.onData,\r\n remoteTask.onExit\r\n );\r\n remoteTask.setRemotePty(sshPty);\r\n\r\n return formatStartResponse(remoteTask.id, command, cwd, ctx.type);\r\n }\r\n\r\n // 2. Check if this is the local root context\r\n if (ctx.type === 'local') {\r\n // Start task tracking\r\n const remoteTask = BackgroundTaskManager.startRemoteTask(\r\n command,\r\n cwd,\r\n 'local'\r\n );\r\n\r\n // Execute via Local PTY (which might be running a wrapped command like \"wsl ...\" or \"docker ...\")\r\n const localPty = runLocalPty(\r\n currentCommand,\r\n executionCwd, // Use the local CWD\r\n remoteTask.onData,\r\n remoteTask.onExit\r\n );\r\n remoteTask.setRemotePty(localPty);\r\n\r\n return formatStartResponse(remoteTask.id, command, cwd, 'local');\r\n }\r\n\r\n // 3. Otherwise, WRAP the command for the next iteration (parent)\r\n if (ctx.type === 'docker') {\r\n const containerId = ctx.metadata.containerId;\r\n if (!containerId) throw new Error('Docker context missing containerId');\r\n currentCommand = buildDockerCommand(containerId, executionCwd, currentCommand);\r\n } else if (ctx.type === 'wsl') {\r\n const distro = ctx.metadata.distroName || 'Ubuntu';\r\n currentCommand = buildWSLCommand(distro, executionCwd, currentCommand);\r\n } else if (ctx.type === 'ssh') {\r\n // Fallback for SSH without accessible client (e.g. wrapped command)\r\n currentCommand = buildSSHCommand(ctx.metadata, executionCwd, currentCommand);\r\n } else {\r\n // Unknown context type, try to pass through or error?\r\n // For now we error to be safe, or just wrap in sh -c?\r\n throw new Error(`Unsupported context type for wrapping: ${ctx.type}`);\r\n }\r\n }\r\n\r\n throw new Error('Failed to execute command: Reached end of context stack without execution.');\r\n }\r\n\r\n case 'status': {\r\n // Validate required parameters for status\r\n if (!task_id) {\r\n throw new Error('Missing required parameter \"task_id\" for action \"status\"');\r\n }\r\n\r\n const task = BackgroundTaskManager.getTask(task_id);\r\n if (!task) {\r\n // List all available tasks to help the AI\r\n const allTasks = BackgroundTaskManager.getAllTasks();\r\n if (allTasks.length === 0) {\r\n throw new Error(`Task \"${task_id}\" not found. There are no background tasks currently tracked.`);\r\n }\r\n const taskList = allTasks.map(t => `- ${t.id} (${t.isRunning ? 'running' : 'completed'}): ${t.command}`).join('\\n');\r\n throw new Error(`Task \"${task_id}\" not found.\\n\\nAvailable tasks:\\n${taskList}`);\r\n }\r\n\r\n // Get the output (last N lines) — sanitize to strip ANSI escape sequences\r\n // and control characters that would corrupt the terminal if rendered\r\n const rawOutput = task.output;\r\n const cleanOutput = stripAnsi(processTerminalOutput(rawOutput))\r\n .replace(/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F\\x7F]/g, '');\r\n const lines = cleanOutput.split('\\n');\r\n const outputToShow = lines.slice(-output_lines).join('\\n');\r\n const truncated = lines.length > output_lines;\r\n\r\n const durationMs = Date.now() - task.startTime.getTime();\r\n const durationSec = Math.round(durationMs / 1000);\r\n\r\n let statusText = '';\r\n if (task.isRunning) {\r\n statusText = `**Status:** šŸ”„ Running (${durationSec}s)`;\r\n } else if (task.exitCode === 0) {\r\n statusText = `**Status:** āœ… Completed successfully (exit code: 0)`;\r\n } else if (task.exitCode !== undefined) {\r\n statusText = `**Status:** āŒ Failed (exit code: ${task.exitCode})`;\r\n } else if (task.error) {\r\n statusText = `**Status:** āŒ Error: ${task.error}`;\r\n } else {\r\n statusText = `**Status:** ā¹ļø Stopped`;\r\n }\r\n\r\n const remoteLabel = task.remoteContext ? ` [${task.remoteContext}]` : '';\r\n\r\n return `Background Task Status${remoteLabel}\r\n\r\n**Task ID:** ${task_id}\r\n**Command:** ${task.command}\r\n**Working Directory:** ${task.cwd}\r\n${statusText}\r\n\r\n**Output${truncated ? ` (last ${output_lines} lines)` : ''}:**\r\n\\`\\`\\`\r\n${outputToShow || '(no output yet)'}\r\n\\`\\`\\``;\r\n }\r\n\r\n case 'kill': {\r\n // Validate required parameters for kill\r\n if (!task_id) {\r\n throw new Error('Missing required parameter \"task_id\" for action \"kill\"');\r\n }\r\n\r\n const task = BackgroundTaskManager.getTask(task_id);\r\n if (!task) {\r\n // Try to find if it was recently killed or doesn't exist\r\n throw new Error(`Task \"${task_id}\" not found.`);\r\n }\r\n\r\n if (!task.isRunning) {\r\n return `Task \"${task_id}\" is not running (already completed or stopped). No action taken.`;\r\n }\r\n\r\n // For remote tasks, we might need to do special cleanup, \r\n // but BackgroundTaskManager.cancelTask() handles calling the pty.kill()\r\n const success = BackgroundTaskManager.cancelTask(task_id);\r\n if (success) {\r\n return `Background task \"${task_id}\" has been terminated.\r\n\r\n**Command:** ${task.command}\r\n**Status:** ā¹ļø Killed by request`;\r\n } else {\r\n throw new Error(`Failed to kill task \"${task_id}\". The task may have already completed.`);\r\n }\r\n }\r\n\r\n case 'wait': {\r\n // Validate required parameters for wait\r\n if (!wait_seconds) {\r\n throw new Error('Missing required parameter \"wait_seconds\" for action \"wait\"');\r\n }\r\n\r\n if (wait_seconds < 1 || wait_seconds > 300) {\r\n throw new Error('wait_seconds must be between 1 and 300 (5 minutes)');\r\n }\r\n\r\n // Create a promise that resolves after the specified duration\r\n await new Promise(resolve => setTimeout(resolve, wait_seconds * 1000));\r\n\r\n return `Waited for ${wait_seconds} second${wait_seconds !== 1 ? 's' : ''}.\r\n\r\nā±ļø The execution has been paused for the requested duration. You can now continue with the next action.`;\r\n }\r\n\r\n default:\r\n throw new Error(`Unknown action \"${action}\". Valid actions are: \"start\", \"status\", \"kill\", \"wait\".`);\r\n }\r\n },\r\n};\r\n\r\n/**\r\n * Format the successful start response\r\n */\r\nfunction formatStartResponse(taskId: string, command: string, cwd: string, contextType: string): string {\r\n return `Background task started successfully in ${contextType}.\r\n\r\n**Task ID:** ${taskId}\r\n**Command:** ${command}\r\n**Working Directory:** ${cwd}\r\n**Context:** ${contextType}\r\n\r\nThe command is now running in the background. Use action=\"status\" with task_id=\"${taskId}\" to check progress, or action=\"kill\" to terminate it.`;\r\n}\r\n\r\n/**\r\n * Helper to escape a command for use in \"sh -c\" or similar\r\n */\r\nfunction escapeCmd(cmd: string): string {\r\n // Escape backslashes first, then quotes\r\n return cmd.replace(/\\\\/g, '\\\\\\\\').replace(/\"/g, '\\\\\"');\r\n}\r\n\r\n/**\r\n * Build a Docker execution command\r\n */\r\nfunction buildDockerCommand(containerId: string, cwd: string, command: string): string {\r\n // docker exec -w <cwd> <id> sh -c \"<cmd>\"\r\n return `docker exec -w \"${cwd}\" ${containerId} sh -c \"${escapeCmd(command)}\"`;\r\n}\r\n\r\n/**\r\n * Build a WSL execution command\r\n */\r\nfunction buildWSLCommand(distro: string, cwd: string, command: string): string {\r\n // wsl -d <distro> -- bash -c \"cd <cwd> && <cmd>\"\r\n return `wsl -d ${distro} -- bash -c \"cd \\\\\"${cwd}\\\\\" && ${escapeCmd(command)}\"`;\r\n}\r\n\r\n/**\r\n * Build an SSH execution command (for wrapping)\r\n */\r\nfunction buildSSHCommand(metadata: any, cwd: string, command: string): string {\r\n const portFlag = metadata.port ? `-p ${metadata.port}` : '';\r\n const userHost = `${metadata.username}@${metadata.hostname}`;\r\n // ssh -p <port> user@host \"cd <cwd> && <cmd>\"\r\n return `ssh ${portFlag} ${userHost} \"cd \\\\\"${cwd}\\\\\" && ${escapeCmd(command)}\"`;\r\n}\r\n"],"mappings":"AAUA,SAAS,6BAA6B;AACtC,SAAS,eAAe,mBAAmB;AAE3C,SAAS,6BAA6B;AACtC,OAAO,eAAe;AACtB,YAAY,UAAU;AAEtB,SAAS,4BAA4B,gBAAqB,YAAoB,YAA4B;AACtG,MAAI,eAAe,SAAS,SAAS;AACjC,WAAO,KAAK,QAAQ,YAAY,UAAU;AAAA,EAC9C;AAEA,QAAM,aAAa,eAAe,UAAU,oBAAoB,cAAc;AAC9E,SAAO,KAAK,MAAM,WAAW,UAAU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