| 116 | } |
| 117 | |
| 118 | async function waitForChildExit(child: ChildProcess, timeoutMs: number): Promise<boolean> { |
| 119 | if (child.exitCode != null || child.signalCode != null) { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | return new Promise<boolean>((resolve) => { |
| 124 | let timeout: ReturnType<typeof setTimeout>; |
| 125 | let settled = false; |
| 126 | const onExit = () => { |
| 127 | if (settled) { |
| 128 | return; |
| 129 | } |
| 130 | settled = true; |
| 131 | clearTimeout(timeout); |
| 132 | resolve(true); |
| 133 | }; |
| 134 | timeout = setTimeout(() => { |
| 135 | if (settled) { |
| 136 | return; |
| 137 | } |
| 138 | settled = true; |
| 139 | child.off("exit", onExit); |
| 140 | resolve(false); |
| 141 | }, timeoutMs); |
| 142 | child.once("exit", onExit); |
| 143 | if (child.exitCode != null || child.signalCode != null) { |
| 144 | onExit(); |
| 145 | } |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Convert tool parameters to JSON schema format for sending to CLI |