`runTerminalCommand` function in Codebuff codebase.
In this article, we review runTerminalCommand function in Codebuff codebase. We will look at:
-
runTerminalCommand invoked in handleToolCall.
-
runTerminalCommand definition.
I study patterns used in an open source project found on Github Trending. For this week, I reviewed Codebuff codebase and wrote this article.
runTerminalCommand invoked in handleToolCall.
runTerminalCommand is invoked in codebuff/sdk/src/client.ts at L32. This is called in a function named handleToolCall.
This handleToolCall is invoked as shown below in codebuff/npm-app/src/client.ts.
// Handle backend-initiated tool call requests
this.webSocket.subscribe('tool-call-request', async (action) => {
const { requestId, toolName, input, userInputId } = action
...
try {
// Execute the tool call using existing tool handlers
const toolCall = {
toolCallId: requestId,
toolName,
input,
}
Spinner.get().stop()
const toolResult = await handleToolCall(toolCall as any)
Shadcn CLI uses ora to show spinner but looks like Codebuff has its own spinner logic written in npm-app/src/utils/spinner.ts.
runTerminalCommand definition.
Below is the function declaration of runTermincalCommand:
/* ------------------------------------------------------------------ */
/* PUBLIC API – runTerminalCommand */
/* ------------------------------------------------------------------ */
export const runTerminalCommand = async (
toolCallId: string,
command: string,
mode: 'user' | 'assistant',
processType: 'SYNC' | 'BACKGROUND',
timeoutSeconds: number,
cwd?: string,
stdoutFile?: string,
stderrFile?: string,
): Promise<CodebuffToolOutput<'run_terminal_command'>> => {
There are some variables initialized in this function’s definition and in the end, runTermincalCommand returns this:
/* sync mode --------------------------------------------------------- */
return new Promise((resolve) =>
runCommandChildProcess(
persistentProcess!,
modifiedCmd,
mode,
cwd!,
maybeTimeoutSeconds,
(v) => resolve(resolveCommand(v)),
),
)
runCommandChildProcess is the core command runner defined in the same file, npm-app/src/terminal/run-command.ts.
About me:
Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.
Email: ramu.narasinga@gmail.com
Want to learn from open-source? Solve challenges inspired by open-source projects.
References:
-
https://github.com/CodebuffAI/codebuff/blob/main/sdk/src/client.ts#L342
-
https://github.com/CodebuffAI/codebuff/blob/main/npm-app/src/terminal/run-command.ts#L366
-
https://github.com/CodebuffAI/codebuff/blob/main/npm-app/src/terminal/run-command.ts#L381
-
https://github.com/CodebuffAI/codebuff/blob/main/npm-app/src/terminal/run-command.ts#L175