MCPcopy Create free account
hub / github.com/Noumena-Network/code / getStdinOverride

Function getStdinOverride

src/utils/renderOptions.ts:26–79  ·  view source on GitHub ↗

* Gets a ReadStream for /dev/tty when stdin is piped. * This allows interactive Ink rendering even when stdin is a pipe. * Result is cached for the lifetime of the process.

()

Source from the content-addressed store, hash-verified

24 * Result is cached for the lifetime of the process.
25 */
26function getStdinOverride(): ReadStream | undefined {
27 // Return cached result if already computed
28 if (cachedStdinOverride !== null) {
29 return cachedStdinOverride
30 }
31
32 // No override needed if stdin is already a TTY
33 if (process.stdin.isTTY) {
34 cachedStdinOverride = undefined
35 return undefined
36 }
37
38 // PTY test harnesses and some supervisors provide a valid stdin stream even
39 // when a compiled runtime misreports process.stdin.isTTY. Do not steal input
40 // from the harness by reopening /dev/tty in those explicitly marked cases.
41 if (isStdinOverrideDisabled()) {
42 cachedStdinOverride = undefined
43 return undefined
44 }
45
46 // Skip in CI environments
47 if (isEnvTruthy(process.env.CI)) {
48 cachedStdinOverride = undefined
49 return undefined
50 }
51
52 // Skip if running MCP (input hijacking breaks MCP)
53 if (process.argv.includes('mcp')) {
54 cachedStdinOverride = undefined
55 return undefined
56 }
57
58 // No /dev/tty on Windows
59 if (process.platform === 'win32') {
60 cachedStdinOverride = undefined
61 return undefined
62 }
63
64 // Try to open /dev/tty as an alternative input source
65 try {
66 const ttyFd = openSync('/dev/tty', 'r')
67 const ttyStream = new ReadStream(ttyFd)
68 // Explicitly set isTTY to true since we know /dev/tty is a TTY.
69 // This is needed because some runtimes (like Bun's compiled binaries)
70 // may not correctly detect isTTY on ReadStream created from a file descriptor.
71 ttyStream.isTTY = true
72 cachedStdinOverride = ttyStream
73 return cachedStdinOverride
74 } catch (err) {
75 logError(err as Error)
76 cachedStdinOverride = undefined
77 return undefined
78 }
79}
80
81/**
82 * Returns base render options for Ink, including stdin override when needed.

Callers 1

getBaseRenderOptionsFunction · 0.85

Calls 3

isEnvTruthyFunction · 0.90
isStdinOverrideDisabledFunction · 0.85
logErrorFunction · 0.70

Tested by

no test coverage detected