From 4e12633a418c8f22e00715ed5c77278f7ad8f384 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Jul 2025 09:25:34 -0700 Subject: [PATCH] Improve piped input handling in headless mode - combine stdin with prompt args --- src/e2e/headless-simple.test.ts | 72 ++++++++++++++++----------------- src/index.ts | 10 ++++- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/e2e/headless-simple.test.ts b/src/e2e/headless-simple.test.ts index 5bf838eea..e191fb44d 100644 --- a/src/e2e/headless-simple.test.ts +++ b/src/e2e/headless-simple.test.ts @@ -3,6 +3,18 @@ import { createTestContext, cleanupTestContext, runCLI, createTestConfig } from describe("E2E: Headless Mode (Simple)", () => { let context: any; + const testConfig = `name: Test Assistant +version: 1.0.0 +schema: v1 +models: + - model: gpt-4 + provider: openai + apiKey: test-key + roles: + - chat`; + + const testEnv = { OPENAI_API_KEY: "test-key" }; + beforeEach(async () => { context = await createTestContext(); }); @@ -39,72 +51,56 @@ no models here`); }); it("should handle missing prompt in headless mode", async () => { - await createTestConfig(context, `name: Test Assistant -version: 1.0.0 -schema: v1 -models: - - model: gpt-4 - provider: openai - apiKey: test-key - roles: - - chat`); + await createTestConfig(context, testConfig); const result = await runCLI(context, { args: ["-p", "--config", context.configPath], - env: { OPENAI_API_KEY: "test-key" }, + env: testEnv, expectError: true, }); - // Should error when no prompt provided in headless mode expect(result.exitCode).not.toBe(0); }); it("should accept piped input with -p flag", async () => { - await createTestConfig(context, `name: Test Assistant -version: 1.0.0 -schema: v1 -models: - - model: gpt-4 - provider: openai - apiKey: test-key - roles: - - chat`); + await createTestConfig(context, testConfig); const result = await runCLI(context, { args: ["-p", "--config", context.configPath], - env: { OPENAI_API_KEY: "test-key" }, + env: testEnv, input: "Hello from piped input", expectError: true, }); - // Should not show "You: " prompt when input is piped expect(result.stderr).not.toContain("You:"); expect(result.stdout).not.toContain("You:"); - - // Will fail because of missing API key but should consume the piped input expect(result.exitCode).not.toBe(0); }); - it("should not interfere with normal prompt argument", async () => { - await createTestConfig(context, `name: Test Assistant -version: 1.0.0 -schema: v1 -models: - - model: gpt-4 - provider: openai - apiKey: test-key - roles: - - chat`); + it("should combine piped input with prompt argument", async () => { + await createTestConfig(context, testConfig); const result = await runCLI(context, { args: ["-p", "--config", context.configPath, "Command line argument"], - env: { OPENAI_API_KEY: "test-key" }, - input: "Piped input that should be ignored", + env: testEnv, + input: "Piped input to be combined", + expectError: true, + }); + + expect(result.stderr).not.toContain("You:"); + expect(result.stdout).not.toContain("You:"); + expect(result.exitCode).not.toBe(0); + }); + + it("should work with only prompt argument", async () => { + await createTestConfig(context, testConfig); + + const result = await runCLI(context, { + args: ["-p", "--config", context.configPath, "Only command line argument"], + env: testEnv, expectError: true, }); - // Should use command line argument over piped input - // Will fail because of missing API key but should not show "You:" prompt expect(result.stderr).not.toContain("You:"); expect(result.stdout).not.toContain("You:"); expect(result.exitCode).not.toBe(0); diff --git a/src/index.ts b/src/index.ts index 99210cb8e..5cdf34db5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -78,10 +78,16 @@ addCommonOptions(program) } // Check for piped input when using -p flag - if (options.print && !prompt) { + if (options.print) { const stdinInput = readStdinSync(); if (stdinInput) { - prompt = stdinInput; + if (prompt) { + // Combine stdin and prompt argument - stdin comes first in XML block + prompt = `\n${stdinInput}\n\n\n${prompt}`; + } else { + // Only stdin input, use as-is + prompt = stdinInput; + } } }