( prompt: string, inputFormat: 'text' | 'stream-json', )
| 1028 | } |
| 1029 | |
| 1030 | async function getInputPrompt( |
| 1031 | prompt: string, |
| 1032 | inputFormat: 'text' | 'stream-json', |
| 1033 | ): Promise<string | AsyncIterable<string>> { |
| 1034 | if ( |
| 1035 | !process.stdin.isTTY && |
| 1036 | // Input hijacking breaks MCP. |
| 1037 | !process.argv.includes('mcp') |
| 1038 | ) { |
| 1039 | if (inputFormat === 'stream-json') { |
| 1040 | return process.stdin; |
| 1041 | } |
| 1042 | process.stdin.setEncoding('utf8'); |
| 1043 | let data = ''; |
| 1044 | const onData = (chunk: string) => { |
| 1045 | data += chunk; |
| 1046 | }; |
| 1047 | process.stdin.on('data', onData); |
| 1048 | // If no data arrives in 3s, stop waiting and warn. Stdin is likely an |
| 1049 | // inherited pipe from a parent that isn't writing (subprocess spawned |
| 1050 | // without explicit stdin handling). 3s covers slow producers like curl, |
| 1051 | // jq on large files, python with import overhead. The warning makes |
| 1052 | // silent data loss visible for the rare producer that's slower still. |
| 1053 | const timedOut = await peekForStdinData(process.stdin, 3000); |
| 1054 | process.stdin.off('data', onData); |
| 1055 | if (timedOut) { |
| 1056 | process.stderr.write( |
| 1057 | 'Warning: no stdin data received in 3s, proceeding without it. ' + |
| 1058 | 'If piping from a slow command, redirect stdin explicitly: < /dev/null to skip, or wait longer.\n', |
| 1059 | ); |
| 1060 | } |
| 1061 | return [prompt, data].filter(Boolean).join('\n'); |
| 1062 | } |
| 1063 | return prompt; |
| 1064 | } |
| 1065 | |
| 1066 | async function run(): Promise<CommanderCommand> { |
| 1067 | profileCheckpoint('run_function_start'); |
no test coverage detected