MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / runBashCommand

Function runBashCommand

cli/src/commands/router.ts:38–213  ·  view source on GitHub ↗
(command: string)

Source from the content-addressed store, hash-verified

36 * Uses ghost mode when streaming or chain in progress, otherwise adds directly to chat history.
37 */
38export function runBashCommand(command: string) {
39 const {
40 streamingAgents,
41 isChainInProgress,
42 setMessages,
43 addPendingBashMessage,
44 updatePendingBashMessage,
45 } = useChatStore.getState()
46
47 const ghost = streamingAgents.size > 0 || isChainInProgress
48 const id = crypto.randomUUID()
49 const commandCwd = process.cwd()
50 const startTime = Date.now()
51
52 if (ghost) {
53 // Ghost mode: add to pending messages
54 addPendingBashMessage({
55 id,
56 command,
57 stdout: '',
58 stderr: '',
59 exitCode: 0,
60 isRunning: true,
61 startTime: Date.now(),
62 cwd: commandCwd,
63 })
64 } else {
65 // Direct mode: add to chat history with placeholder output (user + assistant)
66 const { assistantMessage } = buildBashHistoryMessages({
67 command,
68 cwd: commandCwd,
69 toolCallId: id,
70 output: '...',
71 })
72 setMessages((prev) => [...prev, assistantMessage])
73 }
74
75 runTerminalCommand({
76 command,
77 process_type: 'SYNC',
78 cwd: commandCwd,
79 timeout_seconds: -1,
80 env: getSystemProcessEnv(),
81 })
82 .then(([{ value }]) => {
83 const stdout = 'stdout' in value ? value.stdout || '' : ''
84 const stderr = 'stderr' in value ? value.stderr || '' : ''
85 const exitCode = 'exitCode' in value ? value.exitCode ?? 0 : 0
86
87 // Track terminal command completion
88 const durationMs = Date.now() - startTime
89 trackEvent(AnalyticsEvent.TERMINAL_COMMAND_COMPLETED, {
90 command: command.split(' ')[0], // Just the command name, not args
91 exitCode,
92 success: exitCode === 0,
93 ghost,
94 durationMs,
95 hasStdout: stdout.length > 0,

Callers 2

routeUserPromptFunction · 0.85

Calls 5

buildBashHistoryMessagesFunction · 0.90
runTerminalCommandFunction · 0.90
getSystemProcessEnvFunction · 0.90
trackEventFunction · 0.90

Tested by

no test coverage detected