| 104 | }); |
| 105 | |
| 106 | const severPostMidCall = async (): Promise<void> => { |
| 107 | await toolCallOpened; |
| 108 | await delay(SEVER_GRACE_MS); |
| 109 | const hop = toolCallHop; |
| 110 | if (!hop) return; |
| 111 | // Destroy BOTH ends so every fetch client (undici and bun) observes a hard |
| 112 | // stream error: the client-facing response socket (what the SDK's reader is |
| 113 | // attached to) AND the upstream to the origin. |
| 114 | for (const destroy of [ |
| 115 | () => hop.res.socket?.destroy(), |
| 116 | () => hop.res.destroy(), |
| 117 | () => hop.upstream.socket?.destroy(), |
| 118 | () => hop.upstream.destroy(), |
| 119 | ]) { |
| 120 | try { |
| 121 | destroy(); |
| 122 | } catch { |
| 123 | /* already gone */ |
| 124 | } |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | return new Promise((resolve) => { |
| 129 | server.listen(0, "127.0.0.1", () => { |