From 9bc9c6e6c5b389fc72f9fcdb4452a50c37dc903e Mon Sep 17 00:00:00 2001 From: Allen Hutchison Date: Tue, 22 Apr 2025 18:32:03 -0700 Subject: [PATCH] Question flag (#125) --- packages/cli/src/config/config.ts | 8 ++++++++ packages/cli/src/gemini.ts | 11 ++++++++--- packages/cli/src/ui/hooks/useGeminiStream.ts | 2 +- packages/server/src/config/config.ts | 8 ++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/config/config.ts b/packages/cli/src/config/config.ts index 42275834..09513bae 100644 --- a/packages/cli/src/config/config.ts +++ b/packages/cli/src/config/config.ts @@ -20,6 +20,7 @@ interface CliArgs { target_dir: string | undefined; model: string | undefined; debug_mode: boolean | undefined; + question: string | undefined; } function parseArguments(): CliArgs { @@ -42,6 +43,12 @@ function parseArguments(): CliArgs { description: 'Whether to run in debug mode. Defaults to false.', default: false, }) + .option('question', { + alias: 'q', + type: 'string', + description: + 'The question to pass to the command when using piped input.', + }) .help() .alias('h', 'help') .strict().argv; @@ -71,6 +78,7 @@ export function loadCliConfig(): Config { argv.model || DEFAULT_GEMINI_MODEL, argv.target_dir || process.cwd(), argv.debug_mode || false, + argv.question || '', // TODO: load passthroughCommands from .env file ); } diff --git a/packages/cli/src/gemini.ts b/packages/cli/src/gemini.ts index 56a44e30..0579f059 100644 --- a/packages/cli/src/gemini.ts +++ b/packages/cli/src/gemini.ts @@ -13,9 +13,10 @@ import { GeminiClient } from '@gemini-code/server'; async function main() { const config = loadCliConfig(); + let input = config.getQuestion(); - // Render UI, passing necessary config values and initial input - if (process.stdin.isTTY) { + // Render UI, passing necessary config values. Check that there is no command line question. + if (process.stdin.isTTY && input?.length === 0) { render( React.createElement(App, { config, @@ -24,7 +25,11 @@ async function main() { return; } - const input = await readStdin(); + // If not a TTY, read from stdin + // This is for cases where the user pipes input directly into the command + if (!process.stdin.isTTY) { + input += await readStdin(); + } if (!input) { console.error('No input provided via stdin.'); process.exit(1); diff --git a/packages/cli/src/ui/hooks/useGeminiStream.ts b/packages/cli/src/ui/hooks/useGeminiStream.ts index 27b68577..364661b6 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.ts +++ b/packages/cli/src/ui/hooks/useGeminiStream.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { exec as _exec, exec } from 'child_process'; +import { exec as _exec } from 'child_process'; import { useState, useRef, useCallback, useEffect } from 'react'; import { useInput } from 'ink'; import { diff --git a/packages/server/src/config/config.ts b/packages/server/src/config/config.ts index e842cd29..2cb05318 100644 --- a/packages/server/src/config/config.ts +++ b/packages/server/src/config/config.ts @@ -26,6 +26,7 @@ export class Config { private targetDir: string; private toolRegistry: ToolRegistry; private debugMode: boolean; + private question: string | undefined; private passthroughCommands: string[]; constructor( @@ -33,12 +34,14 @@ export class Config { model: string, targetDir: string, debugMode: boolean, + question: string, passthroughCommands?: string[], ) { this.apiKey = apiKey; this.model = model; this.targetDir = targetDir; this.debugMode = debugMode; + this.question = question; this.passthroughCommands = passthroughCommands || DEFAULT_PASSTHROUGH_COMMANDS; @@ -64,6 +67,9 @@ export class Config { getDebugMode(): boolean { return this.debugMode; } + getQuestion(): string | undefined { + return this.question; + } getPassthroughCommands(): string[] { return this.passthroughCommands; @@ -98,6 +104,7 @@ export function createServerConfig( model: string, targetDir: string, debugMode: boolean, + question: string, passthroughCommands?: string[], ): Config { return new Config( @@ -105,6 +112,7 @@ export function createServerConfig( model, path.resolve(targetDir), debugMode, + question, passthroughCommands, ); }