Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

💄 style(tool-ui): render ANSI escape codes in RunCommand output (#15516)

Arvin Xu28f0117932f6
diff --git a/packages/shared-tool-ui/package.json b/packages/shared-tool-ui/package.json
index 8661526620b..6fbb8e635e3 100644
--- a/packages/shared-tool-ui/package.json
+++ b/packages/shared-tool-ui/package.json
@@ -11,7 +11,8 @@
"./styles": "./src/styles.ts"
},
"dependencies": {
- "@lobechat/tool-runtime": "workspace:*"
+ "@lobechat/tool-runtime": "workspace:*",
+ "anser": "^2.3.5"
},
"devDependencies": {
"@lobechat/types": "workspace:*"
diff --git a/packages/shared-tool-ui/src/Render/RunCommand/AnsiOutput.tsx b/packages/shared-tool-ui/src/Render/RunCommand/AnsiOutput.tsx
new file mode 100644
index 00000000000..9b20faac261
--- /dev/null
+++ b/packages/shared-tool-ui/src/Render/RunCommand/AnsiOutput.tsx
@@ -0,0 +1,77 @@
+'use client';
+
+import Anser from 'anser';
+import { createStaticStyles, cssVar } from 'antd-style';
+import { memo, useMemo } from 'react';
+
+const styles = createStaticStyles(({ css }) => ({
+ pre: css`
+ overflow: auto;
+
+ max-height: 200px;
+ margin: 0;
+ padding: 8px;
+ border-radius: 6px;
+
+ font-family: ${cssVar.fontFamilyCode};
+ font-size: 12px;
+ line-height: 1.6;
+ color: ${cssVar.colorText};
+ word-break: break-word;
+ white-space: pre-wrap;
+
+ background: ${cssVar.colorFillTertiary};
+ `,
+}));
+
+interface AnsiOutputProps {
+ text: string;
+}
+
+const AnsiOutput = memo<AnsiOutputProps>(({ text }) => {
+ const segments = useMemo(
+ () =>
+ Anser.ansiToJson(text, {
+ json: true,
+ remove_empty: true,
+ use_classes: false,
+ }),
+ [text],
+ );
+
+ return (
+ <pre className={styles.pre}>
+ {segments.map((seg, i) => {
+ const decorations = seg.decorations ?? [];
+ const isDim = decorations.includes('dim');
+ const isBold = decorations.includes('bold');
+ const isItalic = decorations.includes('italic');
+ const isUnderline = decorations.includes('underline');
+ const isStrike = decorations.includes('strikethrough');
+
+ return (
+ <span
+ key={i}
+ style={{
+ background: seg.bg ? `rgb(${seg.bg})` : undefined,
+ color: seg.fg ? `rgb(${seg.fg})` : undefined,
+ fontStyle: isItalic ? 'italic' : undefined,
+ fontWeight: isBold ? 600 : undefined,
+ opacity: isDim ? 0.6 : undefined,
+ textDecoration:
+ [isUnderline && 'underline', isStrike && 'line-through']
+ .filter(Boolean)
+ .join(' ') || undefined,
+ }}
+ >
+ {seg.content}
+ </span>
+ );
+ })}
+ </pre>
+ );
+});
+
+AnsiOutput.displayName = 'AnsiOutput';
+
+export default AnsiOutput;
diff --git a/packages/shared-tool-ui/src/Render/RunCommand/index.tsx b/packages/shared-tool-ui/src/Render/RunCommand/index.tsx
index b95768551a4..15c4b062aee 100644
--- a/packages/shared-tool-ui/src/Render/RunCommand/index.tsx
+++ b/packages/shared-tool-ui/src/Render/RunCommand/index.tsx
@@ -6,6 +6,8 @@ import { Block, Flexbox, Highlighter } from '@lobehub/ui';
import { createStaticStyles } from 'antd-style';
import { memo } from 'react';
+import AnsiOutput from './AnsiOutput';
+
const styles = createStaticStyles(({ css }) => ({
container: css`
overflow: hidden;
@@ -36,22 +38,8 @@ const RunCommand = memo<BuiltinRenderProps<RunCommandArgs, RunCommandState>>(
>
{args?.command || ''}
</Highlighter>
- {output && (
- <Highlighter
- wrap
- language={'text'}
- showLanguage={false}
- style={{ maxHeight: 200, overflow: 'auto', paddingInline: 8 }}
- variant={'filled'}
- >
- {output}
- </Highlighter>
- )}
- {pluginState?.stderr && (
- <Highlighter wrap language={'text'} showLanguage={false} variant={'filled'}>
- {pluginState.stderr}
- </Highlighter>
- )}
+ {output && <AnsiOutput text={output} />}
+ {pluginState?.stderr && <AnsiOutput text={pluginState.stderr} />}
</Block>
</Flexbox>
);