-
Notifications
You must be signed in to change notification settings - Fork 52
feat(invoke): add --prompt-file and stdin support for long prompts #974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aidandaly24
merged 2 commits into
aws:main
from
aidandaly24:feat/invoke-stdin-prompt-file
Apr 27, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { resolvePrompt } from '../resolve-prompt'; | ||
| import { randomUUID } from 'node:crypto'; | ||
| import { mkdtemp, rm, writeFile } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { Readable } from 'node:stream'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| describe('resolvePrompt', () => { | ||
| let dir: string; | ||
|
|
||
| beforeEach(async () => { | ||
| dir = await mkdtemp(join(tmpdir(), `resolve-prompt-${randomUUID()}-`)); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await rm(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('returns --prompt flag value when provided', async () => { | ||
| const result = await resolvePrompt({ flag: 'hello', stdinPiped: false }); | ||
| expect(result).toEqual({ success: true, prompt: 'hello' }); | ||
| }); | ||
|
|
||
| it('prefers --prompt flag over positional, file, and stdin', async () => { | ||
| const file = join(dir, 'p.txt'); | ||
| await writeFile(file, 'from-file'); | ||
| const result = await resolvePrompt( | ||
| { flag: 'from-flag', positional: 'from-positional', file, stdinPiped: true }, | ||
| Readable.from(['from-stdin']) | ||
| ); | ||
| expect(result).toEqual({ success: true, prompt: 'from-flag' }); | ||
| }); | ||
|
|
||
| it('prefers --prompt over positional', async () => { | ||
| const result = await resolvePrompt({ flag: 'from-flag', positional: 'from-positional', stdinPiped: false }); | ||
| expect(result).toEqual({ success: true, prompt: 'from-flag' }); | ||
| }); | ||
|
|
||
| it('falls back to positional when no flag', async () => { | ||
| const result = await resolvePrompt({ positional: 'from-positional', stdinPiped: false }); | ||
| expect(result).toEqual({ success: true, prompt: 'from-positional' }); | ||
| }); | ||
|
|
||
| it('reads from --prompt-file when no flag or positional', async () => { | ||
| const file = join(dir, 'p.txt'); | ||
| await writeFile(file, 'content from file\n'); | ||
| const result = await resolvePrompt({ file, stdinPiped: false }); | ||
| expect(result).toEqual({ success: true, prompt: 'content from file' }); | ||
| }); | ||
|
|
||
| it('strips only one trailing newline from file content', async () => { | ||
| const file = join(dir, 'p.txt'); | ||
| await writeFile(file, 'line1\nline2\n\n'); | ||
| const result = await resolvePrompt({ file, stdinPiped: false }); | ||
| expect(result.prompt).toBe('line1\nline2\n'); | ||
| }); | ||
|
|
||
| it('reads from stdin when piped and no other source', async () => { | ||
| const result = await resolvePrompt({ stdinPiped: true }, Readable.from(['piped input\n'])); | ||
| expect(result).toEqual({ success: true, prompt: 'piped input' }); | ||
| }); | ||
|
|
||
| it('errors when --prompt-file and stdin are both present', async () => { | ||
| const file = join(dir, 'p.txt'); | ||
| await writeFile(file, 'x'); | ||
| const result = await resolvePrompt({ file, stdinPiped: true }, Readable.from(['y'])); | ||
| expect(result.success).toBe(false); | ||
| expect(result.error).toContain('--prompt-file'); | ||
| expect(result.error).toContain('stdin'); | ||
| }); | ||
|
|
||
| it('returns failure when --prompt-file does not exist', async () => { | ||
| const result = await resolvePrompt({ file: join(dir, 'missing.txt'), stdinPiped: false }); | ||
| expect(result.success).toBe(false); | ||
| expect(result.error).toContain('Failed to read --prompt-file'); | ||
| }); | ||
|
|
||
| it('returns undefined prompt when no source is provided', async () => { | ||
| const result = await resolvePrompt({ stdinPiped: false }); | ||
| expect(result).toEqual({ success: true, prompt: undefined }); | ||
| }); | ||
|
|
||
| it('preserves empty-string flag (does not fall through)', async () => { | ||
| const result = await resolvePrompt({ flag: '', positional: 'ignored', stdinPiped: false }); | ||
| expect(result).toEqual({ success: true, prompt: '' }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { readFile } from 'node:fs/promises'; | ||
|
|
||
| export interface PromptSources { | ||
| /** Value from --prompt flag */ | ||
| flag?: string; | ||
| /** Value from positional argument */ | ||
| positional?: string; | ||
| /** Path from --prompt-file flag */ | ||
| file?: string; | ||
| /** True when stdin is piped (not a TTY) */ | ||
| stdinPiped: boolean; | ||
| } | ||
|
|
||
| export interface ResolvedPrompt { | ||
| success: boolean; | ||
| prompt?: string; | ||
| error?: string; | ||
| } | ||
|
|
||
| async function readPromptFile(path: string): Promise<ResolvedPrompt> { | ||
| try { | ||
| const content = await readFile(path, 'utf-8'); | ||
| return { success: true, prompt: content.replace(/\r?\n$/, '') }; | ||
| } catch (err) { | ||
| const message = err instanceof Error ? err.message : String(err); | ||
| return { success: false, error: `Failed to read --prompt-file '${path}': ${message}` }; | ||
| } | ||
| } | ||
|
|
||
| async function readStdin(stdin: NodeJS.ReadableStream): Promise<string> { | ||
| const chunks: Buffer[] = []; | ||
| for await (const chunk of stdin) { | ||
| chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk); | ||
| } | ||
| return Buffer.concat(chunks).toString('utf-8').trim(); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the effective prompt from multiple possible sources. | ||
| * | ||
| * Precedence (hybrid — backward compatible with existing --prompt/positional behavior): | ||
| * 1. --prompt flag | ||
| * 2. positional argument | ||
| * 3. --prompt-file | ||
| * 4. stdin (when piped) | ||
| * | ||
| * Collision rule: --prompt-file AND piped stdin together is an error, since silent | ||
| * precedence between two "bulk" sources would mask user mistakes (e.g. a CI pipeline | ||
| * accidentally piping data while also passing --prompt-file). | ||
| */ | ||
| export async function resolvePrompt( | ||
| sources: PromptSources, | ||
| stdin: NodeJS.ReadableStream = process.stdin | ||
| ): Promise<ResolvedPrompt> { | ||
| if (sources.flag !== undefined) return { success: true, prompt: sources.flag }; | ||
| if (sources.positional !== undefined) return { success: true, prompt: sources.positional }; | ||
|
|
||
| const stdinContent = sources.stdinPiped ? await readStdin(stdin) : ''; | ||
| const hasStdinContent = stdinContent.length > 0; | ||
|
|
||
| if (sources.file !== undefined && hasStdinContent) { | ||
| return { | ||
| success: false, | ||
| error: 'Cannot combine --prompt-file with piped stdin. Provide only one prompt source.', | ||
| }; | ||
| } | ||
| if (sources.file !== undefined) return readPromptFile(sources.file); | ||
| if (hasStdinContent) return { success: true, prompt: stdinContent }; | ||
| return { success: true, prompt: undefined }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.