|
| 1 | +import { z } from "zod"; |
| 2 | + |
| 3 | +export function registerTools(server, processManager, wsControlServer, phoenixDesktopPath) { |
| 4 | + server.tool( |
| 5 | + "start_phoenix", |
| 6 | + "Start the Phoenix Code desktop app (Electron). Launches npm run serve:electron in the phoenix-desktop directory.", |
| 7 | + {}, |
| 8 | + async () => { |
| 9 | + try { |
| 10 | + if (processManager.isRunning()) { |
| 11 | + return { |
| 12 | + content: [{ |
| 13 | + type: "text", |
| 14 | + text: JSON.stringify({ |
| 15 | + success: false, |
| 16 | + error: "Phoenix is already running", |
| 17 | + pid: processManager.getPid() |
| 18 | + }) |
| 19 | + }] |
| 20 | + }; |
| 21 | + } |
| 22 | + const result = await processManager.start(phoenixDesktopPath); |
| 23 | + return { |
| 24 | + content: [{ |
| 25 | + type: "text", |
| 26 | + text: JSON.stringify({ |
| 27 | + success: true, |
| 28 | + pid: result.pid, |
| 29 | + wsPort: wsControlServer.getPort() |
| 30 | + }) |
| 31 | + }] |
| 32 | + }; |
| 33 | + } catch (err) { |
| 34 | + return { |
| 35 | + content: [{ |
| 36 | + type: "text", |
| 37 | + text: JSON.stringify({ success: false, error: err.message }) |
| 38 | + }] |
| 39 | + }; |
| 40 | + } |
| 41 | + } |
| 42 | + ); |
| 43 | + |
| 44 | + server.tool( |
| 45 | + "stop_phoenix", |
| 46 | + "Stop the running Phoenix Code desktop app.", |
| 47 | + {}, |
| 48 | + async () => { |
| 49 | + try { |
| 50 | + const result = await processManager.stop(); |
| 51 | + return { |
| 52 | + content: [{ |
| 53 | + type: "text", |
| 54 | + text: JSON.stringify(result) |
| 55 | + }] |
| 56 | + }; |
| 57 | + } catch (err) { |
| 58 | + return { |
| 59 | + content: [{ |
| 60 | + type: "text", |
| 61 | + text: JSON.stringify({ success: false, error: err.message }) |
| 62 | + }] |
| 63 | + }; |
| 64 | + } |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + server.tool( |
| 69 | + "get_terminal_logs", |
| 70 | + "Get stdout/stderr output from the Electron process. By default returns new logs since last call; set clear=true to get all logs and clear the buffer.", |
| 71 | + { clear: z.boolean().default(false).describe("If true, return all logs and clear the buffer. If false, return only new logs since last read.") }, |
| 72 | + async ({ clear }) => { |
| 73 | + let logs; |
| 74 | + if (clear) { |
| 75 | + logs = processManager.getTerminalLogs(false); |
| 76 | + processManager.clearTerminalLogs(); |
| 77 | + } else { |
| 78 | + logs = processManager.getTerminalLogs(true); |
| 79 | + } |
| 80 | + const text = logs.map(e => `[${e.stream}] ${e.text}`).join(""); |
| 81 | + return { |
| 82 | + content: [{ |
| 83 | + type: "text", |
| 84 | + text: text || "(no terminal logs)" |
| 85 | + }] |
| 86 | + }; |
| 87 | + } |
| 88 | + ); |
| 89 | + |
| 90 | + server.tool( |
| 91 | + "get_browser_console_logs", |
| 92 | + "Get console logs forwarded from the Phoenix browser runtime via WebSocket. By default returns new logs since last call; set clear=true to get all logs and clear the buffer.", |
| 93 | + { |
| 94 | + clear: z.boolean().default(false).describe("If true, return all logs and clear the buffer. If false, return only new logs since last read."), |
| 95 | + instance: z.string().optional().describe("Target a specific Phoenix instance by name (e.g. 'Phoenix-a3f2'). Required when multiple instances are connected.") |
| 96 | + }, |
| 97 | + async ({ clear, instance }) => { |
| 98 | + let logs; |
| 99 | + if (clear) { |
| 100 | + logs = wsControlServer.getBrowserLogs(false, instance); |
| 101 | + if (logs && logs.error) { |
| 102 | + return { |
| 103 | + content: [{ type: "text", text: JSON.stringify(logs) }] |
| 104 | + }; |
| 105 | + } |
| 106 | + const clearResult = wsControlServer.clearBrowserLogs(instance); |
| 107 | + if (clearResult && clearResult.error) { |
| 108 | + return { |
| 109 | + content: [{ type: "text", text: JSON.stringify(clearResult) }] |
| 110 | + }; |
| 111 | + } |
| 112 | + } else { |
| 113 | + logs = wsControlServer.getBrowserLogs(true, instance); |
| 114 | + if (logs && logs.error) { |
| 115 | + return { |
| 116 | + content: [{ type: "text", text: JSON.stringify(logs) }] |
| 117 | + }; |
| 118 | + } |
| 119 | + } |
| 120 | + return { |
| 121 | + content: [{ |
| 122 | + type: "text", |
| 123 | + text: JSON.stringify(logs.length > 0 ? logs : "(no browser logs)") |
| 124 | + }] |
| 125 | + }; |
| 126 | + } |
| 127 | + ); |
| 128 | + |
| 129 | + server.tool( |
| 130 | + "take_screenshot", |
| 131 | + "Take a screenshot of the Phoenix Code app window. Returns a PNG image.", |
| 132 | + { |
| 133 | + selector: z.string().optional().describe("Optional CSS selector to capture a specific element"), |
| 134 | + instance: z.string().optional().describe("Target a specific Phoenix instance by name (e.g. 'Phoenix-a3f2'). Required when multiple instances are connected.") |
| 135 | + }, |
| 136 | + async ({ selector, instance }) => { |
| 137 | + try { |
| 138 | + const base64Data = await wsControlServer.requestScreenshot(selector, instance); |
| 139 | + return { |
| 140 | + content: [{ |
| 141 | + type: "image", |
| 142 | + data: base64Data, |
| 143 | + mimeType: "image/png" |
| 144 | + }] |
| 145 | + }; |
| 146 | + } catch (err) { |
| 147 | + return { |
| 148 | + content: [{ |
| 149 | + type: "text", |
| 150 | + text: JSON.stringify({ error: err.message }) |
| 151 | + }] |
| 152 | + }; |
| 153 | + } |
| 154 | + } |
| 155 | + ); |
| 156 | + |
| 157 | + server.tool( |
| 158 | + "reload_phoenix", |
| 159 | + "Reload the Phoenix Code app. Closes all open files (prompting to save unsaved changes) then reloads the app.", |
| 160 | + { |
| 161 | + instance: z.string().optional().describe("Target a specific Phoenix instance by name (e.g. 'Phoenix-a3f2'). Required when multiple instances are connected.") |
| 162 | + }, |
| 163 | + async ({ instance }) => { |
| 164 | + try { |
| 165 | + const result = await wsControlServer.requestReload(false, instance); |
| 166 | + return { |
| 167 | + content: [{ |
| 168 | + type: "text", |
| 169 | + text: JSON.stringify({ success: true, message: "Phoenix is reloading" }) |
| 170 | + }] |
| 171 | + }; |
| 172 | + } catch (err) { |
| 173 | + return { |
| 174 | + content: [{ |
| 175 | + type: "text", |
| 176 | + text: JSON.stringify({ error: err.message }) |
| 177 | + }] |
| 178 | + }; |
| 179 | + } |
| 180 | + } |
| 181 | + ); |
| 182 | + |
| 183 | + server.tool( |
| 184 | + "force_reload_phoenix", |
| 185 | + "Force reload the Phoenix Code app without saving. Closes all open files without saving unsaved changes, then reloads the app.", |
| 186 | + { |
| 187 | + instance: z.string().optional().describe("Target a specific Phoenix instance by name (e.g. 'Phoenix-a3f2'). Required when multiple instances are connected.") |
| 188 | + }, |
| 189 | + async ({ instance }) => { |
| 190 | + try { |
| 191 | + const result = await wsControlServer.requestReload(true, instance); |
| 192 | + return { |
| 193 | + content: [{ |
| 194 | + type: "text", |
| 195 | + text: JSON.stringify({ success: true, message: "Phoenix is force reloading (unsaved changes discarded)" }) |
| 196 | + }] |
| 197 | + }; |
| 198 | + } catch (err) { |
| 199 | + return { |
| 200 | + content: [{ |
| 201 | + type: "text", |
| 202 | + text: JSON.stringify({ error: err.message }) |
| 203 | + }] |
| 204 | + }; |
| 205 | + } |
| 206 | + } |
| 207 | + ); |
| 208 | + |
| 209 | + server.tool( |
| 210 | + "get_phoenix_status", |
| 211 | + "Check the status of the Phoenix process and WebSocket connection.", |
| 212 | + {}, |
| 213 | + async () => { |
| 214 | + return { |
| 215 | + content: [{ |
| 216 | + type: "text", |
| 217 | + text: JSON.stringify({ |
| 218 | + processRunning: processManager.isRunning(), |
| 219 | + pid: processManager.getPid(), |
| 220 | + wsConnected: wsControlServer.isClientConnected(), |
| 221 | + connectedInstances: wsControlServer.getConnectedInstances(), |
| 222 | + wsPort: wsControlServer.getPort() |
| 223 | + }) |
| 224 | + }] |
| 225 | + }; |
| 226 | + } |
| 227 | + ); |
| 228 | +} |
0 commit comments