xonecas.io
64875
Mc

mini-coder / commit 3e4ee5c

feat(cli): support stdin one-shot prompts in non-tty mode

xonecas2026-03-14 3e4ee5c27910e493b7deaedf3181ef860b07e910 parent 18abdac

3 files changed

src/cli/stdin-prompt.test.ts+41−0
@@ -0,0 +1,41 @@
1+import { describe, expect, test } from "bun:test";
2+import { Readable } from "node:stream";
3+import { resolvePromptInput } from "./stdin-prompt.ts";
4+
5+describe("resolvePromptInput", () => {
6+ test("keeps explicit prompt argument", async () => {
7+ const prompt = await resolvePromptInput("hello", {
8+ stdin: Readable.from(["ignored"]),
9+ stdinIsTTY: false,
10+ });
11+
12+ expect(prompt).toBe("hello");
13+ });
14+
15+ test("reads piped stdin when no prompt argument", async () => {
16+ const prompt = await resolvePromptInput(null, {
17+ stdin: Readable.from([" explain\nthis file\n "]),
18+ stdinIsTTY: false,
19+ });
20+
21+ expect(prompt).toBe("explain\nthis file");
22+ });
23+
24+ test("returns null for empty piped input", async () => {
25+ const prompt = await resolvePromptInput(null, {
26+ stdin: Readable.from(["\n \t \n"]),
27+ stdinIsTTY: false,
28+ });
29+
30+ expect(prompt).toBeNull();
31+ });
32+
33+ test("returns null when stdin is a tty", async () => {
34+ const prompt = await resolvePromptInput(null, {
35+ stdin: Readable.from(["ignored"]),
36+ stdinIsTTY: true,
37+ });
38+
39+ expect(prompt).toBeNull();
40+ });
41+});
src/cli/stdin-prompt.ts+33−0
@@ -0,0 +1,33 @@
1+import type { Readable } from "node:stream";
2+
3+function chunkToString(chunk: unknown): string {
4+ if (typeof chunk === "string") return chunk;
5+ if (chunk instanceof Uint8Array) return Buffer.from(chunk).toString("utf8");
6+ return String(chunk);
7+}
8+
9+async function readStreamText(stream: Readable): Promise<string> {
10+ let text = "";
11+ for await (const chunk of stream) {
12+ text += chunkToString(chunk);
13+ }
14+ return text;
15+}
16+
17+export async function resolvePromptInput(
18+ promptArg: string | null,
19+ opts?: {
20+ stdin?: Readable;
21+ stdinIsTTY?: boolean;
22+ },
23+): Promise<string | null> {
24+ if (promptArg) return promptArg;
25+
26+ const stdinIsTTY = opts?.stdinIsTTY ?? process.stdin.isTTY;
27+ if (stdinIsTTY) return null;
28+
29+ const stdin = opts?.stdin ?? process.stdin;
30+ const piped = await readStreamText(stdin);
31+ const trimmed = piped.trim();
32+ return trimmed.length > 0 ? trimmed : null;
33+}
src/index.ts+17−4
@@ -19,6 +19,8 @@ import {
1919 renderError,
2020 writeln,
2121 } from "./cli/output.ts";
22+import { resolvePromptInput } from "./cli/stdin-prompt.ts";
23+import { terminal } from "./cli/terminal-io.ts";
2224 import { initApiLog } from "./llm-api/api-log.ts";
2325 import {
2426 initModelInfoCache,
@@ -64,6 +66,17 @@ async function main(): Promise<void> {
6466 process.exit(0);
6567 }
6668
69+ const prompt = await resolvePromptInput(args.prompt);
70+ if (!prompt && !terminal.isTTY) {
71+ renderError(
72+ new Error(
73+ "No prompt provided. Pass a prompt argument or pipe text on stdin.",
74+ ),
75+ "input",
76+ );
77+ process.exit(1);
78+ }
79+
6780 // Determine session
6881 let sessionId: string | undefined;
6982 if (args.resumeLast) {
@@ -120,7 +133,7 @@ async function main(): Promise<void> {
120133 });
121134
122135 const { text: resolvedText, images: refImages } = await resolveFileRefs(
123- args.prompt ?? "",
136+ prompt ?? "",
124137 parentCwd,
125138 );
126139 const result = await runner.processUserInput(resolvedText, refImages);
@@ -145,7 +158,7 @@ async function main(): Promise<void> {
145158 return;
146159 }
147160
148- if (!args.prompt) {
161+ if (!prompt) {
149162 // Only show banner for interactive sessions, not piped/one-shot
150163 renderBanner(model, args.cwd);
151164 }
@@ -168,9 +181,9 @@ async function main(): Promise<void> {
168181
169182 const { runner, cmdCtx } = await initAgent(agentOpts);
170183
171- if (args.prompt) {
184+ if (prompt) {
172185 const { text: resolvedText, images: refImages } = await resolveFileRefs(
173- args.prompt,
186+ prompt,
174187 args.cwd,
175188 );
176189 await runner.processUserInput(resolvedText, refImages);