From d2ef83bc60f0dd295fc6150030357bf914d0169f Mon Sep 17 00:00:00 2001 From: jlove29 Date: Sat, 19 Apr 2025 11:07:39 +0100 Subject: [PATCH] Add direct execution of shell commands --- packages/cli/src/ui/hooks/useGeminiStream.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/cli/src/ui/hooks/useGeminiStream.ts b/packages/cli/src/ui/hooks/useGeminiStream.ts index b4746fe6..37e62d93 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.ts +++ b/packages/cli/src/ui/hooks/useGeminiStream.ts @@ -4,6 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +import { exec } from 'child_process'; import { useState, useRef, useCallback, useEffect } from 'react'; import { useInput } from 'ink'; import { GeminiClient } from '../../core/gemini-client.js'; @@ -16,6 +17,8 @@ import { import { globalConfig } from '../../config/config.js'; import { getErrorMessage, isNodeError } from '../../utils/errors.js'; +const allowlistedCommands = ['ls']; // TODO: make this configurable + const addHistoryItem = ( setHistory: React.Dispatch>, itemData: Omit, @@ -106,6 +109,21 @@ export const useGeminiStream = ( { type: 'user', text: trimmedQuery }, userMessageTimestamp, ); + + const maybeCommand = trimmedQuery.split(/\s+/)[0]; + if (allowlistedCommands.includes(maybeCommand)) { + exec(trimmedQuery, (error, stdout, stderr) => { + const timestamp = getNextMessageId(userMessageTimestamp); + // TODO: handle stderr, error + addHistoryItem( + setHistory, + { type: 'info', text: stdout }, + timestamp, + ); + }); + return; + } + } else if ( // HACK to detect errored function responses. typeof query === 'object' &&