| 170 | } |
| 171 | |
| 172 | async run( |
| 173 | input: ProcessInput, |
| 174 | params: Record<string, unknown>, |
| 175 | onProgress?: (percent: number, label: string) => void, |
| 176 | onLog?: (message: string) => void, |
| 177 | ): Promise<ProcessResult> { |
| 178 | return new Promise((resolve, reject) => { |
| 179 | const proc = spawn(this.pythonExe, [this.scriptPath], { |
| 180 | stdio: ['pipe', 'pipe', 'pipe'], |
| 181 | }) |
| 182 | |
| 183 | // Send input as a single JSON line on stdin |
| 184 | proc.stdin.write(JSON.stringify({ |
| 185 | input, |
| 186 | params, |
| 187 | nodeId: input.nodeId ?? '', |
| 188 | workspaceDir: this.workspaceDir, |
| 189 | tempDir: this.tempDir, |
| 190 | }) + '\n') |
| 191 | proc.stdin.end() |
| 192 | |
| 193 | let stdoutBuf = '' |
| 194 | let resolved = false |
| 195 | |
| 196 | proc.stdout.on('data', (chunk: Buffer) => { |
| 197 | stdoutBuf += chunk.toString() |
| 198 | const lines = stdoutBuf.split('\n') |
| 199 | stdoutBuf = lines.pop() ?? '' |
| 200 | |
| 201 | for (const line of lines) { |
| 202 | const trimmed = line.trim() |
| 203 | if (!trimmed) continue |
| 204 | try { |
| 205 | const msg = JSON.parse(trimmed) as { type: string; percent?: number; label?: string; message?: string; result?: ProcessResult } |
| 206 | if (msg.type === 'progress') { |
| 207 | onProgress?.(msg.percent ?? 0, msg.label ?? '') |
| 208 | } else if (msg.type === 'log') { |
| 209 | onLog?.(msg.message ?? '') |
| 210 | } else if (msg.type === 'done') { |
| 211 | resolved = true |
| 212 | resolve(msg.result ?? {}) |
| 213 | } else if (msg.type === 'error') { |
| 214 | resolved = true |
| 215 | reject(new Error(msg.message ?? 'Unknown error')) |
| 216 | } |
| 217 | } catch { |
| 218 | // Non-JSON stdout line — treat as a log message |
| 219 | onLog?.(trimmed) |
| 220 | } |
| 221 | } |
| 222 | }) |
| 223 | |
| 224 | let stderrBuf = '' |
| 225 | proc.stderr.on('data', (chunk: Buffer) => { |
| 226 | stderrBuf += chunk.toString() |
| 227 | }) |
| 228 | |
| 229 | proc.on('close', (code) => { |