Improve piped input handling in headless mode - combine stdin with prompt args

This commit is contained in:
Nate 2025-07-30 09:25:34 -07:00
parent 8e44284927
commit 4e12633a41
2 changed files with 42 additions and 40 deletions

View file

@ -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);

View file

@ -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 = `<stdin>\n${stdinInput}\n</stdin>\n\n${prompt}`;
} else {
// Only stdin input, use as-is
prompt = stdinInput;
}
}
}