Skip to content

Commit e2c0410

Browse files
kwonah0claude
andcommitted
Add configurable code block formatting for shell agent output
- Add outputFormat config with useCodeBlock and codeBlockSyntax options - Dynamically apply code blocks based on configuration - Default to useCodeBlock: true with shell syntax for safety - Prevents markdown special character interpretation issues 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c86f584 commit e2c0410

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

dtui.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
"args": ["\"[SHELL RESPONSE]:\""],
77
"template": "{command} {args} \"{prompt}\"",
88
"timeout": 5000,
9-
"streaming": false
9+
"streaming": false,
10+
"outputFormat": {
11+
"useCodeBlock": true,
12+
"codeBlockSyntax": "shell"
13+
}
1014
}
1115
},
1216
"terminal": {

src/agents/ElectronShellAIAgent.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { AIAgent, AIAgentResult } from '../services/MockAIAgent';
2+
import { universalConfigService } from '../config/UniversalConfigService';
23

34
/**
45
* Electron Shell AI Agent that uses IPC to execute shell commands in main process
56
* This avoids Node.js module compatibility issues with Vite build
67
*/
78
export class ElectronShellAIAgent implements AIAgent {
89
name = 'Electron IPC Shell AI Agent';
10+
private outputConfig: { useCodeBlock: boolean; codeBlockSyntax: string } = {
11+
useCodeBlock: true,
12+
codeBlockSyntax: 'shell'
13+
};
914

1015
async executeCommand(_command: string): Promise<AIAgentResult> {
1116
return {
@@ -36,6 +41,16 @@ export class ElectronShellAIAgent implements AIAgent {
3641
async generateResponse(messages: any[]): Promise<string> {
3742
console.log('🎯 ElectronShellAIAgent.generateResponse called with messages:', messages);
3843

44+
// Load output format config
45+
const shellConfig = universalConfigService.get<any>('ai:shell');
46+
if (shellConfig?.outputFormat) {
47+
this.outputConfig = {
48+
useCodeBlock: shellConfig.outputFormat.useCodeBlock ?? true,
49+
codeBlockSyntax: shellConfig.outputFormat.codeBlockSyntax || 'shell'
50+
};
51+
}
52+
console.log('Using output config:', this.outputConfig);
53+
3954
// Convert message history to a single prompt
4055
const lastMessage = messages[messages.length - 1];
4156
const prompt = lastMessage?.content || '';
@@ -45,26 +60,36 @@ export class ElectronShellAIAgent implements AIAgent {
4560
const result = await this.query(prompt);
4661
console.log('ElectronShellAIAgent query result:', result);
4762

48-
// Format output for markdown rendering (with code blocks)
63+
// Format output based on config
4964
let output = '';
65+
const wrapInCodeBlock = (text: string) => {
66+
if (this.outputConfig.useCodeBlock) {
67+
return `\`\`\`${this.outputConfig.codeBlockSyntax}\n${text}\n\`\`\``;
68+
}
69+
return text;
70+
};
71+
5072
if (result.success) {
5173
const stdout = result.metadata?.stdout || result.content || 'Command completed successfully';
52-
output = `\`\`\`\n${stdout}\n\`\`\``;
74+
output = wrapInCodeBlock(stdout);
5375

5476
// Include stderr if present
5577
if (result.metadata?.stderr && result.metadata.stderr.trim()) {
56-
output += `\n\n**stderr:**\n\`\`\`\n${result.metadata.stderr.trim()}\n\`\`\``;
78+
const stderrFormatted = wrapInCodeBlock(result.metadata.stderr.trim());
79+
output += `\n\n**stderr:**\n${stderrFormatted}`;
5780
}
5881
} else {
5982
// For failures, show comprehensive error info in markdown format
6083
output = '**Command failed**\n\n';
6184

6285
if (result.metadata?.stderr && result.metadata.stderr.trim()) {
63-
output += `**stderr:**\n\`\`\`\n${result.metadata.stderr.trim()}\n\`\`\`\n\n`;
86+
const stderrFormatted = wrapInCodeBlock(result.metadata.stderr.trim());
87+
output += `**stderr:**\n${stderrFormatted}\n\n`;
6488
}
6589

6690
if (result.metadata?.stdout && result.metadata.stdout.trim()) {
67-
output += `**stdout:**\n\`\`\`\n${result.metadata.stdout.trim()}\n\`\`\`\n\n`;
91+
const stdoutFormatted = wrapInCodeBlock(result.metadata.stdout.trim());
92+
output += `**stdout:**\n${stdoutFormatted}\n\n`;
6893
}
6994

7095
if (result.metadata?.exitCode !== undefined) {

0 commit comments

Comments
 (0)