diff --git a/core/tools/implementations/runTerminalCommand.ts b/core/tools/implementations/runTerminalCommand.ts
index 785f4ee30..3d3ac85cb 100644
--- a/core/tools/implementations/runTerminalCommand.ts
+++ b/core/tools/implementations/runTerminalCommand.ts
@@ -10,6 +10,16 @@ import {
const asyncExec = util.promisify(childProcess.exec);
+// Add color-supporting environment variables
+const getColorEnv = () => ({
+ ...process.env,
+ FORCE_COLOR: "1",
+ COLORTERM: "truecolor",
+ TERM: "xterm-256color",
+ CLICOLOR: "1",
+ CLICOLOR_FORCE: "1",
+});
+
const ENABLED_FOR_REMOTES = [
"",
"local",
@@ -56,10 +66,11 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => {
}
}
- // Use spawn instead of exec to get streaming output
+ // Use spawn with color environment
const childProc = childProcess.spawn(args.command, {
cwd,
shell: true,
+ env: getColorEnv(), // Add enhanced environment for colors
});
childProc.stdout?.on("data", (data) => {
@@ -130,26 +141,7 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => {
return;
}
- if (!waitForCompletion) {
- // Already resolved, just update the UI with final output
- if (extras.onPartialOutput) {
- const status =
- code === 0 || !code
- ? "\nBackground command completed"
- : `\nBackground command failed with exit code ${code}`;
- extras.onPartialOutput({
- toolCallId,
- contextItems: [
- {
- name: "Terminal",
- description: "Terminal command output",
- content: terminalOutput,
- status: status,
- },
- ],
- });
- }
- } else {
+ if (waitForCompletion) {
// Normal completion, resolve now
if (code === 0) {
const status = "Command completed";
@@ -172,6 +164,25 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => {
},
]);
}
+ } else {
+ // Already resolved, just update the UI with final output
+ if (extras.onPartialOutput) {
+ const status =
+ code === 0 || !code
+ ? "\nBackground command completed"
+ : `\nBackground command failed with exit code ${code}`;
+ extras.onPartialOutput({
+ toolCallId,
+ contextItems: [
+ {
+ name: "Terminal",
+ description: "Terminal command output",
+ content: terminalOutput,
+ status: status,
+ },
+ ],
+ });
+ }
}
});
@@ -197,14 +208,43 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => {
const workspaceDirs = await extras.ide.getWorkspaceDirs();
const cwd = fileURLToPath(workspaceDirs[0]);
- if (!waitForCompletion) {
+ if (waitForCompletion) {
+ // Standard execution, waiting for completion
+ try {
+ // Use color environment for exec as well
+ const output = await asyncExec(args.command, {
+ cwd,
+ env: getColorEnv(),
+ });
+ const status = "Command completed";
+ return [
+ {
+ name: "Terminal",
+ description: "Terminal command output",
+ content: output.stdout ?? "",
+ status: status,
+ },
+ ];
+ } catch (error: any) {
+ const status = `Command failed with: ${error.message || error.toString()}`;
+ return [
+ {
+ name: "Terminal",
+ description: "Terminal command output",
+ content: error.stderr ?? error.toString(),
+ status: status,
+ },
+ ];
+ }
+ } else {
// For non-streaming but also not waiting for completion, use spawn
// but don't attach any listeners other than error
try {
- // Use spawn instead of exec but don't wait
+ // Use spawn with color environment
const childProc = childProcess.spawn(args.command, {
cwd,
shell: true,
+ env: getColorEnv(), // Add color environment
// Detach the process so it's not tied to the parent
detached: true,
// Redirect to /dev/null equivalent (works cross-platform)
@@ -246,30 +286,6 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => {
},
];
}
- } else {
- // Standard execution, waiting for completion
- try {
- const output = await asyncExec(args.command, { cwd });
- const status = "Command completed";
- return [
- {
- name: "Terminal",
- description: "Terminal command output",
- content: output.stdout ?? "",
- status: status,
- },
- ];
- } catch (error: any) {
- const status = `Command failed with: ${error.message || error.toString()}`;
- return [
- {
- name: "Terminal",
- description: "Terminal command output",
- content: error.stderr ?? error.toString(),
- status: status,
- },
- ];
- }
}
}
}
diff --git a/gui/package-lock.json b/gui/package-lock.json
index d6996309b..b895a9afc 100644
--- a/gui/package-lock.json
+++ b/gui/package-lock.json
@@ -25,10 +25,12 @@
"@tiptap/starter-kit": "^2.1.13",
"@tiptap/suggestion": "^2.1.13",
"@types/uuid": "^10.0.0",
+ "anser": "^2.3.2",
"clsx": "^2.1.1",
"core": "file:../core",
"dompurify": "^3.0.6",
"downshift": "^7.6.0",
+ "escape-carriage": "^1.3.1",
"lodash": "^4.17.21",
"lowlight": "^3.3.0",
"minisearch": "^7.0.2",
@@ -3737,6 +3739,12 @@
"url": "https://github.com/sponsors/epoberezkin"
}
},
+ "node_modules/anser": {
+ "version": "2.3.2",
+ "resolved": "https://registry.npmjs.org/anser/-/anser-2.3.2.tgz",
+ "integrity": "sha512-PMqBCBvrOVDRqLGooQb+z+t1Q0PiPyurUQeZRR5uHBOVZcW8B04KMmnT12USnhpNX2wCPagWzLVppQMUG3u0Dw==",
+ "license": "MIT"
+ },
"node_modules/ansi-regex": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
@@ -5200,6 +5208,12 @@
"node": ">=6"
}
},
+ "node_modules/escape-carriage": {
+ "version": "1.3.1",
+ "resolved": "https://registry.npmjs.org/escape-carriage/-/escape-carriage-1.3.1.tgz",
+ "integrity": "sha512-GwBr6yViW3ttx1kb7/Oh+gKQ1/TrhYwxKqVmg5gS+BK+Qe2KrOa/Vh7w3HPBvgGf0LfcDGoY9I6NHKoA5Hozhw==",
+ "license": "MIT"
+ },
"node_modules/escape-string-regexp": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
diff --git a/gui/package.json b/gui/package.json
index de6da1959..1f137f19b 100644
--- a/gui/package.json
+++ b/gui/package.json
@@ -34,10 +34,12 @@
"@tiptap/starter-kit": "^2.1.13",
"@tiptap/suggestion": "^2.1.13",
"@types/uuid": "^10.0.0",
+ "anser": "^2.3.2",
"clsx": "^2.1.1",
"core": "file:../core",
"dompurify": "^3.0.6",
"downshift": "^7.6.0",
+ "escape-carriage": "^1.3.1",
"lodash": "^4.17.21",
"lowlight": "^3.3.0",
"minisearch": "^7.0.2",
diff --git a/gui/src/components/ansiTerminal/Ansi.tsx b/gui/src/components/ansiTerminal/Ansi.tsx
new file mode 100644
index 000000000..35d4b0a43
--- /dev/null
+++ b/gui/src/components/ansiTerminal/Ansi.tsx
@@ -0,0 +1,279 @@
+import Anser, { AnserJsonEntry } from "anser";
+import { escapeCarriageReturn } from "escape-carriage";
+import * as React from "react";
+import styled from "styled-components";
+import {
+ defaultBorderRadius,
+ vscBackground,
+ vscEditorBackground,
+ vscForeground,
+} from "../../components";
+import { getFontSize } from "../../util";
+
+const AnsiSpan = styled.span<{
+ bg?: string;
+ fg?: string;
+ decoration?: string;
+}>`
+ ${({ bg }) => bg && `background-color: rgb(${bg});`}
+ ${({ fg }) => fg && `color: rgb(${fg});`}
+ ${({ decoration }) => {
+ switch (decoration) {
+ case "bold":
+ return "font-weight: bold;";
+ case "dim":
+ return "opacity: 0.5;";
+ case "italic":
+ return "font-style: italic;";
+ case "hidden":
+ return "visibility: hidden;";
+ case "strikethrough":
+ return "text-decoration: line-through;";
+ case "underline":
+ return "text-decoration: underline;";
+ case "blink":
+ return "text-decoration: blink;";
+ default:
+ return "";
+ }
+ }}
+`;
+
+const AnsiLink = styled.a`
+ color: var(--vscode-textLink-foreground, #3794ff);
+ text-decoration: none;
+ &:hover {
+ text-decoration: underline;
+ }
+`;
+
+// Using the same styled component structure as StyledMarkdown
+const StyledAnsi = styled.div<{
+ fontSize?: number;
+ whiteSpace: string;
+ bgColor: string;
+}>`
+ pre {
+ white-space: ${(props) => props.whiteSpace};
+ background-color: ${vscEditorBackground};
+ border-radius: ${defaultBorderRadius};
+ border: 1px solid
+ var(--vscode-editorWidget-border, rgba(127, 127, 127, 0.3));
+ max-width: calc(100vw - 24px);
+ overflow-x: scroll;
+ overflow-y: hidden;
+ padding: 8px;
+ }
+
+ code {
+ span.line:empty {
+ display: none;
+ }
+ word-wrap: break-word;
+ border-radius: ${defaultBorderRadius};
+ background-color: ${vscEditorBackground};
+ font-size: ${getFontSize() - 2}px;
+ font-family: var(--vscode-editor-font-family);
+ }
+
+ code:not(pre > code) {
+ font-family: var(--vscode-editor-font-family);
+ color: var(--vscode-input-placeholderForeground);
+ }
+
+ background-color: ${(props) => props.bgColor};
+ font-family:
+ var(--vscode-font-family),
+ system-ui,
+ -apple-system,
+ BlinkMacSystemFont,
+ "Segoe UI",
+ Roboto,
+ Oxygen,
+ Ubuntu,
+ Cantarell,
+ "Open Sans",
+ "Helvetica Neue",
+ sans-serif;
+ font-size: ${(props) => props.fontSize || getFontSize()}px;
+ padding-left: 8px;
+ padding-right: 8px;
+ color: ${vscForeground};
+ line-height: 1.5;
+
+ > *:last-child {
+ margin-bottom: 0;
+ }
+`;
+
+/**
+ * Converts ANSI strings into JSON output.
+ * @name ansiToJSON
+ * @function
+ * @param {String} input The input string.
+ * @param {boolean} use_classes If `true`, HTML classes will be appended
+ * to the HTML output.
+ * @return {Array} The parsed input.
+ */
+function ansiToJSON(
+ input: string,
+ use_classes: boolean = false,
+): AnserJsonEntry[] {
+ input = escapeCarriageReturn(fixBackspace(input));
+ return Anser.ansiToJson(input, {
+ json: true,
+ remove_empty: true,
+ use_classes,
+ });
+}
+
+/**
+ * Create a class string.
+ * @name createClass
+ * @function
+ * @param {AnserJsonEntry} bundle
+ * @return {String} class name(s)
+ */
+function createClass(bundle: AnserJsonEntry): string | null {
+ let classNames: string = "";
+
+ if (bundle.bg) {
+ classNames += `${bundle.bg}-bg `;
+ }
+ if (bundle.fg) {
+ classNames += `${bundle.fg}-fg `;
+ }
+ if (bundle.decoration) {
+ classNames += `ansi-${bundle.decoration} `;
+ }
+
+ if (classNames === "") {
+ return null;
+ }
+
+ classNames = classNames.substring(0, classNames.length - 1);
+ return classNames;
+}
+
+/**
+ * Converts an Anser bundle into a React Node.
+ * @param linkify whether links should be converting into clickable anchor tags.
+ * @param useClasses should render the span with a class instead of style.
+ * @param bundle Anser output.
+ * @param key
+ */
+
+function convertBundleIntoReact(
+ linkify: boolean,
+ useClasses: boolean,
+ bundle: AnserJsonEntry,
+ key: number,
+): JSX.Element {
+ const className = useClasses ? createClass(bundle) : null;
+ // Convert bundle.decoration to string or undefined (not null) to match the prop type
+ const decorationProp = bundle.decoration
+ ? String(bundle.decoration)
+ : undefined;
+
+ if (!linkify) {
+ return (
+
+ {bundle.content}
+
+ );
+ }
+
+ const content: React.ReactNode[] = [];
+ const linkRegex =
+ /(\s|^)(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/g;
+
+ let index = 0;
+ let match: RegExpExecArray | null;
+ while ((match = linkRegex.exec(bundle.content)) !== null) {
+ const [, pre, url] = match;
+
+ const startIndex = match.index + pre.length;
+ if (startIndex > index) {
+ content.push(bundle.content.substring(index, startIndex));
+ }
+
+ // Make sure the href we generate from the link is fully qualified. We assume http
+ // if it starts with a www because many sites don't support https
+ const href = url.startsWith("www.") ? `http://${url}` : url;
+
+ content.push(
+
+ {url}
+ ,
+ );
+
+ index = linkRegex.lastIndex;
+ }
+
+ if (index < bundle.content.length) {
+ content.push(bundle.content.substring(index));
+ }
+
+ return (
+
+ {content}
+
+ );
+}
+
+declare interface Props {
+ children?: string;
+ linkify?: boolean;
+ className?: string;
+ useClasses?: boolean;
+}
+
+export default function Ansi(props: Props): JSX.Element {
+ const { className, useClasses, children, linkify } = props;
+
+ // Create the ANSI content
+ const ansiContent = ansiToJSON(children ?? "", useClasses ?? false).map(
+ (bundle, i) =>
+ convertBundleIntoReact(linkify ?? false, useClasses ?? false, bundle, i),
+ );
+
+ return (
+
+
+ {ansiContent}
+
+
+ );
+}
+
+// This is copied from the Jupyter Classic source code
+// notebook/static/base/js/utils.js to handle \b in a way
+// that is **compatible with Jupyter classic**. One can
+// argue that this behavior is questionable:
+// https://stackoverflow.com/questions/55440152/multiple-b-doesnt-work-as-expected-in-jupyter#
+function fixBackspace(txt: string) {
+ let tmp = txt;
+ do {
+ txt = tmp;
+ // Cancel out anything-but-newline followed by backspace
+ tmp = txt.replace(/[^\n]\x08/gm, "");
+ } while (tmp.length < txt.length);
+ return txt;
+}
diff --git a/gui/src/pages/gui/ToolCallDiv/RunTerminalCommand.tsx b/gui/src/pages/gui/ToolCallDiv/RunTerminalCommand.tsx
index c78ec3495..0b3bf1b9d 100644
--- a/gui/src/pages/gui/ToolCallDiv/RunTerminalCommand.tsx
+++ b/gui/src/pages/gui/ToolCallDiv/RunTerminalCommand.tsx
@@ -1,11 +1,10 @@
-import { AnyAction, ThunkDispatch } from "@reduxjs/toolkit";
import { ToolCallState } from "core";
-import { Fragment } from "react";
-import { useDispatch } from "react-redux";
import styled from "styled-components";
+import { vscForeground } from "../../../components";
+import Ansi from "../../../components/ansiTerminal/Ansi";
import StyledMarkdownPreview from "../../../components/StyledMarkdownPreview";
-import { moveTerminalProcessToBackground } from "../../../redux/thunks/moveTerminalProcessToBackground";
import { useAppDispatch } from "../../../redux/hooks";
+import { moveTerminalProcessToBackground } from "../../../redux/thunks/moveTerminalProcessToBackground";
interface RunTerminalCommandToolCallProps {
command: string;
@@ -15,13 +14,12 @@ interface RunTerminalCommandToolCallProps {
const CommandStatus = styled.div`
font-size: 12px;
- color: #666;
+ color: var(--vscode-descriptionForeground, ${vscForeground}88);
margin-top: 8px;
- padding-left: 8px;
- padding-right: 8px;
- padding-bottom: 8px;
display: flex;
align-items: center;
+ padding-left: 8px;
+ padding-right: 8px;
`;
const StatusIcon = styled.span<{
@@ -33,21 +31,32 @@ const StatusIcon = styled.span<{
margin-right: 8px;
background-color: ${(props) =>
props.status === "running"
- ? "#4caf50"
+ ? "var(--vscode-testing-runAction, #4caf50)"
: props.status === "completed"
- ? "#4caf50"
+ ? "var(--vscode-testing-iconPassed, #4caf50)"
: props.status === "background"
- ? "#2196f3"
- : "#f44336"};
+ ? "var(--vscode-statusBarItem-prominentBackground, #2196f3)"
+ : "var(--vscode-testing-iconFailed, #f44336)"};
${(props) =>
props.status === "running" ? "animation: pulse 1.5s infinite;" : ""}
`;
-// Removed unused styled components
+// Waiting message styled for consistency
+const WaitingMessage = styled.div`
+ padding: 8px;
+ padding-left: 16px;
+ padding-right: 16px;
+ margin-top: 8px;
+`;
+
+// For consistency with the rest of the styled components
+const AnsiWrapper = styled.div`
+ margin-top: 8px;
+`;
const BackgroundLink = styled.a`
font-size: 12px;
- color: #0077cc;
+ color: var(--vscode-textLink-foreground, #3794ff);
margin-left: 12px;
cursor: pointer;
text-decoration: none;
@@ -57,6 +66,11 @@ const BackgroundLink = styled.a`
}
`;
+// Just spacing between terminal components and the next toolcall
+const TerminalContainer = styled.div`
+ margin-bottom: 16px;
+`;
+
export function RunTerminalCommand(props: RunTerminalCommandToolCallProps) {
const dispatch = useAppDispatch();
@@ -82,16 +96,24 @@ export function RunTerminalCommand(props: RunTerminalCommandToolCallProps) {
}
return (
-
+
+ {/* Command */}
+ {/* Terminal output with ANSI support */}
+ {isRunning && !hasOutput && (
+ Waiting for output...
+ )}
+ {hasOutput && (
+
+ {terminalContent}
+
+ )}
+
+ {/* Status information */}
{(statusMessage || isRunning) && (
@@ -112,6 +134,6 @@ export function RunTerminalCommand(props: RunTerminalCommandToolCallProps) {
)}
)}
-
+
);
}