MCPcopy Create free account
hub / github.com/TanStack/ai / drainStdout

Function drainStdout

packages/ai-sandbox/src/shell.ts:94–127  ·  view source on GitHub ↗

Feed the stdout async-iterable into the shared line queue.

()

Source from the content-addressed store, hash-verified

92
93 /** Feed the stdout async-iterable into the shared line queue. */
94 async function drainStdout(): Promise<void> {
95 let partial = ''
96 for await (const chunk of proc.stdout) {
97 partial += chunk
98 const parts = partial.split('\n')
99 // All but the last element are complete lines.
100 for (let i = 0; i < parts.length - 1; i++) {
101 const line = parts[i] as string
102 const resolver = pending.shift()
103 if (resolver !== undefined) {
104 resolver(line)
105 } else {
106 lineBuffer.push(line)
107 }
108 }
109 partial = parts[parts.length - 1] as string
110 }
111 // Flush any trailing partial line.
112 if (partial.length > 0) {
113 const line = partial
114 const resolver = pending.shift()
115 if (resolver !== undefined) {
116 resolver(line)
117 } else {
118 lineBuffer.push(line)
119 }
120 }
121 streamDone = true
122 // Resolve any remaining waiters with an empty sentinel so they unblock.
123 for (const resolver of pending) {
124 resolver('')
125 }
126 pending = []
127 }
128
129 // Start draining immediately; do NOT await — runs concurrently.
130 const drainPromise = drainStdout()

Callers 1

createBootstrapShellFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected