|
| 1 | +import { runTerminalCommand } from '@codebuff/sdk' |
| 2 | + |
1 | 3 | import { handleInitializationFlowLocally } from './init' |
2 | 4 |
|
3 | 5 | import type { MultilineInputHandle } from '../components/multiline-input' |
4 | | -import type { ChatMessage } from '../types/chat' |
| 6 | +import type { ChatMessage, ContentBlock } from '../types/chat' |
5 | 7 | import type { SendMessageFn } from '../types/contracts/send-message' |
6 | 8 | import type { User } from '../utils/auth' |
7 | 9 | import type { AgentMode } from '../utils/constants' |
@@ -62,6 +64,67 @@ export function routeUserPrompt(params: { |
62 | 64 | let postUserMessage: Parameters<SendMessageFn>[0]['postUserMessage'] = |
63 | 65 | undefined |
64 | 66 |
|
| 67 | + if (trimmed.startsWith('!')) { |
| 68 | + const toolCallId = crypto.randomUUID() |
| 69 | + const resultBlock: ContentBlock = { |
| 70 | + type: 'tool', |
| 71 | + toolName: 'run_terminal_command', |
| 72 | + toolCallId, |
| 73 | + input: { command: trimmed.slice(1).trim() }, |
| 74 | + output: '', |
| 75 | + } |
| 76 | + |
| 77 | + setMessages((prev) => [ |
| 78 | + ...prev, |
| 79 | + { |
| 80 | + id: `user-${Date.now()}`, |
| 81 | + variant: 'user', |
| 82 | + content: trimmed, |
| 83 | + timestamp: new Date().toISOString(), |
| 84 | + }, |
| 85 | + { |
| 86 | + id: `sys-${Date.now()}`, |
| 87 | + variant: 'ai', |
| 88 | + content: '', |
| 89 | + blocks: [resultBlock], |
| 90 | + timestamp: new Date().toISOString(), |
| 91 | + }, |
| 92 | + ]) |
| 93 | + |
| 94 | + runTerminalCommand({ |
| 95 | + command: trimmed.slice(1).trim(), |
| 96 | + process_type: 'SYNC', |
| 97 | + cwd: process.cwd(), |
| 98 | + timeout_seconds: -1, |
| 99 | + env: process.env, |
| 100 | + }).then(([{ value }]) => { |
| 101 | + setMessages((prev) => { |
| 102 | + const output = 'stdout' in value ? value.stdout : '' |
| 103 | + return prev.map((msg) => { |
| 104 | + if (!msg.blocks) { |
| 105 | + return msg |
| 106 | + } |
| 107 | + return { |
| 108 | + ...msg, |
| 109 | + blocks: msg.blocks.map((block) => |
| 110 | + 'toolCallId' in block && block.toolCallId === toolCallId |
| 111 | + ? { |
| 112 | + ...block, |
| 113 | + output, |
| 114 | + } |
| 115 | + : block, |
| 116 | + ), |
| 117 | + } |
| 118 | + }) |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + saveToHistory(trimmed) |
| 123 | + setInputValue('') |
| 124 | + |
| 125 | + return |
| 126 | + } |
| 127 | + |
65 | 128 | const normalized = trimmed.startsWith('/') ? trimmed.slice(1) : trimmed |
66 | 129 | const cmd = normalized.split(/\s+/)[0].toLowerCase() |
67 | 130 | if (cmd === 'login' || cmd === 'signin') { |
|
0 commit comments