|
146 | 146 |
|
147 | 147 | globalThis.WebSocket = class extends NativeWebSocket { |
148 | 148 | send(data) { |
149 | | - super.send(data); |
150 | | - if (!options.enabled || typeof data !== "string") return; |
| 149 | + if (!options.enabled || typeof data !== "string") return super.send(data); |
151 | 150 | try { |
152 | | - const { value } = parseFrame(data); |
| 151 | + const { value, ndjson } = parseFrame(data); |
| 152 | + let changed = false; |
153 | 153 | for (const request of Array.isArray(value) ? value : [value]) { |
154 | 154 | if (request?._tag === "Request" && request.tag === "orchestration.subscribeShell") { |
| 155 | + const afterSequence = request.payload?.afterSequence; |
| 156 | + // Foreground resumes reuse this socket's client cursor. A new RPC |
| 157 | + // session owns a new socket and reloads the authoritative HTTP shell. |
| 158 | + const offset = |
| 159 | + typeof afterSequence === "number" ? (this.latestShellStream?.offset ?? 0) : 0; |
| 160 | + if (offset !== 0) { |
| 161 | + request.payload.afterSequence -= offset; |
| 162 | + changed = true; |
| 163 | + } |
155 | 164 | const stream = { |
156 | | - sequence: shellSequence, |
157 | | - offset: 0, |
| 165 | + sequence: afterSequence ?? shellSequence, |
| 166 | + offset, |
158 | 167 | emit: (node) => |
159 | 168 | this.replayDeliver({ |
160 | 169 | _tag: "Chunk", |
|
163 | 172 | }), |
164 | 173 | }; |
165 | 174 | this.shellStreams.set(request.id, stream); |
| 175 | + this.latestShellStream = stream; |
166 | 176 | latestShellStream = stream; |
167 | 177 | if (delayedThread && !delayedStream) delayedStream = stream; |
168 | 178 | } else if ( |
|
172 | 182 | this.threadRequests.add(request.id); |
173 | 183 | } |
174 | 184 | } |
| 185 | + if (changed) |
| 186 | + data = ndjson |
| 187 | + ? value.map((item) => JSON.stringify(item)).join("\n") + "\n" |
| 188 | + : JSON.stringify(value); |
175 | 189 | } catch { |
176 | 190 | // Non-JSON socket traffic belongs to the native protocol handler. |
177 | 191 | } |
| 192 | + super.send(data); |
178 | 193 | } |
| 194 | + latestShellStream = null; |
179 | 195 | shellStreams = new Map(); |
180 | 196 | threadRequests = new Set(); |
181 | 197 | replayDeliver; |
|
0 commit comments