()
| 171 | } |
| 172 | |
| 173 | const connect = () => { |
| 174 | if (closed) return |
| 175 | |
| 176 | const connection = resolveEditorConnection(directory, port, editor.connection) |
| 177 | if (!connection) { |
| 178 | if (!zedTerminal) { |
| 179 | setStore("status", "disabled") |
| 180 | scheduleReconnect() |
| 181 | return |
| 182 | } |
| 183 | if (!editor.selection) { |
| 184 | setStore("status", "disabled") |
| 185 | scheduleReconnect() |
| 186 | return |
| 187 | } |
| 188 | |
| 189 | zedSelection ??= editor |
| 190 | .selection(directory) |
| 191 | .then((result) => { |
| 192 | if (closed || socket) return |
| 193 | if (!isRecord(result) || result.type === "unavailable") return |
| 194 | const decoded = result.type === "selection" ? decodeEditorSelection(result.selection) : Option.none() |
| 195 | const selection = Option.getOrUndefined(decoded) |
| 196 | const key = editorSelectionKey(selection) |
| 197 | if (key !== lastZedSelectionKey) { |
| 198 | lastZedSelectionKey = key |
| 199 | setSelection(selection) |
| 200 | setStore("status", selection ? "connected" : "disabled") |
| 201 | } |
| 202 | }) |
| 203 | .catch(() => { |
| 204 | // Keep the last known Zed selection for transient polling failures. |
| 205 | }) |
| 206 | .finally(() => { |
| 207 | zedSelection = undefined |
| 208 | }) |
| 209 | scheduleZedPoll() |
| 210 | return |
| 211 | } |
| 212 | |
| 213 | setStore("status", "connecting") |
| 214 | const current = openEditorSocket(connection, WebSocketImpl) |
| 215 | socket = current |
| 216 | |
| 217 | current.addEventListener("open", () => { |
| 218 | if (socket !== current) { |
| 219 | current.close() |
| 220 | return |
| 221 | } |
| 222 | |
| 223 | attempt = 0 |
| 224 | setStore("status", "connected") |
| 225 | request("initialize", { |
| 226 | protocolVersion: MCP_PROTOCOL_VERSION, |
| 227 | capabilities: {}, |
| 228 | clientInfo: { name: "opencode", version: "0.0.0" }, |
| 229 | }) |
| 230 | }) |
no test coverage detected