|
| 1 | +import * as http from "http" |
| 2 | +import { Logger } from "../../services/logging/Logger" |
| 3 | +import { WebviewProvider } from "../../core/webview" |
| 4 | + |
| 5 | +let testServer: http.Server | undefined |
| 6 | + |
| 7 | +/** |
| 8 | + * Creates and starts an HTTP server for test automation |
| 9 | + * @returns The created HTTP server instance |
| 10 | + */ |
| 11 | +export function createTestServer(): http.Server { |
| 12 | + const PORT = 9876 |
| 13 | + |
| 14 | + testServer = http.createServer((req, res) => { |
| 15 | + // Set CORS headers |
| 16 | + res.setHeader("Access-Control-Allow-Origin", "*") |
| 17 | + res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS") |
| 18 | + res.setHeader("Access-Control-Allow-Headers", "Content-Type") |
| 19 | + |
| 20 | + // Handle preflight requests |
| 21 | + if (req.method === "OPTIONS") { |
| 22 | + res.writeHead(204) |
| 23 | + res.end() |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + // Only handle POST requests to /task |
| 28 | + if (req.method !== "POST" || req.url !== "/task") { |
| 29 | + res.writeHead(404) |
| 30 | + res.end(JSON.stringify({ error: "Not found" })) |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + // Parse the request body |
| 35 | + let body = "" |
| 36 | + req.on("data", (chunk) => { |
| 37 | + body += chunk.toString() |
| 38 | + }) |
| 39 | + |
| 40 | + req.on("end", async () => { |
| 41 | + try { |
| 42 | + // Parse the JSON body |
| 43 | + const { task } = JSON.parse(body) |
| 44 | + |
| 45 | + if (!task) { |
| 46 | + res.writeHead(400) |
| 47 | + res.end(JSON.stringify({ error: "Missing task parameter" })) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + // Get a visible webview instance |
| 52 | + const visibleWebview = WebviewProvider.getVisibleInstance() |
| 53 | + if (!visibleWebview || !visibleWebview.controller) { |
| 54 | + res.writeHead(500) |
| 55 | + res.end(JSON.stringify({ error: "No active Cline instance found" })) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + // Initiate a new task |
| 60 | + Logger.log(`Test server initiating task: ${task}`) |
| 61 | + |
| 62 | + try { |
| 63 | + // Clear any existing task |
| 64 | + await visibleWebview.controller.clearTask() |
| 65 | + |
| 66 | + // Ensure we're in Act mode before initiating the task |
| 67 | + const { chatSettings } = await visibleWebview.controller.getStateToPostToWebview() |
| 68 | + if (chatSettings.mode === "plan") { |
| 69 | + // Switch to Act mode if currently in Plan mode |
| 70 | + await visibleWebview.controller.togglePlanActModeWithChatSettings({ mode: "act" }) |
| 71 | + } |
| 72 | + |
| 73 | + // Initiate the new task |
| 74 | + const taskId = await visibleWebview.controller.initClineWithTask(task) |
| 75 | + |
| 76 | + // Return success response with the task ID |
| 77 | + res.writeHead(200, { "Content-Type": "application/json" }) |
| 78 | + res.end(JSON.stringify({ success: true, taskId })) |
| 79 | + } catch (error) { |
| 80 | + Logger.log(`Error initiating task: ${error}`) |
| 81 | + res.writeHead(500) |
| 82 | + res.end(JSON.stringify({ error: `Failed to initiate task: ${error}` })) |
| 83 | + } |
| 84 | + } catch (error) { |
| 85 | + res.writeHead(400) |
| 86 | + res.end(JSON.stringify({ error: `Invalid JSON: ${error}` })) |
| 87 | + } |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + testServer.listen(PORT, () => { |
| 92 | + Logger.log(`Test server listening on port ${PORT}`) |
| 93 | + }) |
| 94 | + |
| 95 | + // Handle server errors |
| 96 | + testServer.on("error", (error) => { |
| 97 | + Logger.log(`Test server error: ${error}`) |
| 98 | + }) |
| 99 | + |
| 100 | + return testServer |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Shuts down the test server if it exists |
| 105 | + */ |
| 106 | +export function shutdownTestServer() { |
| 107 | + if (testServer) { |
| 108 | + testServer.close() |
| 109 | + Logger.log("Test server shut down") |
| 110 | + testServer = undefined |
| 111 | + } |
| 112 | +} |
0 commit comments