| 1 | import { exec, spawn } from "child_process"; |
| 2 | import { promisify } from "util"; |
| 3 | |
| 4 | import { GlobalContext } from "core/util/GlobalContext.js"; |
| 5 | |
| 6 | import { logger } from "src/util/logger.js"; |
| 7 | |
| 8 | import { compareVersions, getLatestVersion, getVersion } from "../version.js"; |
| 9 | |
| 10 | import { BaseService } from "./BaseService.js"; |
| 11 | import { serviceContainer } from "./ServiceContainer.js"; |
| 12 | import { UpdateServiceState, UpdateStatus } from "./types.js"; |
| 13 | const execAsync = promisify(exec); |
| 14 | |
| 15 | /** |
| 16 | * Service for checking and performing CLI updates |
| 17 | */ |
| 18 | export class UpdateService extends BaseService<UpdateServiceState> { |
| 19 | constructor() { |
| 20 | super("update", { |
| 21 | autoUpdate: true, |
| 22 | isAutoUpdate: true, |
| 23 | status: UpdateStatus.IDLE, |
| 24 | message: "", |
| 25 | error: null, |
| 26 | isUpdateAvailable: false, |
| 27 | latestVersion: null, |
| 28 | currentVersion: getVersion(), |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Initialize the update service |
| 34 | */ |
| 35 | async doInitialize(headless?: boolean) { |
| 36 | // Don't automatically check in tests/headless |
| 37 | if (!headless && process.env.NODE_ENV !== "test") { |
| 38 | void this.checkAndAutoUpdate(); |
| 39 | } |
| 40 | |
| 41 | return this.currentState; |
| 42 | } |
| 43 | |
| 44 | private async checkAndAutoUpdate() { |
| 45 | // First get auto update setting from global context |
| 46 | const globalContext = new GlobalContext(); |
| 47 | const autoUpdate = globalContext.get("autoUpdateCli") ?? true; |
| 48 | this.setState({ |
| 49 | autoUpdate, |
| 50 | }); |
| 51 | |
| 52 | try { |
| 53 | // Check for updates |
| 54 | this.setState({ |
| 55 | status: UpdateStatus.CHECKING, |
| 56 | message: "Checking for updates", |
| 57 | }); |
| 58 | |
| 59 | const latestVersion = await getLatestVersion(); |
| 60 | this.setState({ |
| 61 | latestVersion, |
| 62 | }); |
| 63 | |
| 64 | if (!latestVersion) { |
| 65 | this.setState({ |
| 66 | status: UpdateStatus.IDLE, |
| 67 | message: "Continue CLI", |
| 68 | isUpdateAvailable: false, |
| 69 | }); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | const comparison = compareVersions( |
| 74 | this.currentState.currentVersion, |
| 75 | latestVersion, |
| 76 | ); |
| 77 | const isUpdateAvailable = comparison === "older"; |
| 78 | this.setState({ |
| 79 | isUpdateAvailable, |
| 80 | }); |
| 81 | |
| 82 | if (this.currentState.currentVersion === "0.0.0-dev") { |
| 83 | this.setState({ |
| 84 | status: UpdateStatus.IDLE, |
| 85 | message: `Continue CLI`, |
| 86 | isUpdateAvailable, |
| 87 | latestVersion, |
| 88 | }); |
| 89 | return; // Uncomment to test auto-update behavior in dev |
| 90 | } |
| 91 | |
| 92 | // If update is available, automatically update |
| 93 | if ( |
| 94 | autoUpdate && |
| 95 | isUpdateAvailable && |
| 96 | this.currentState.status !== "updating" && |
| 97 | !process.env.CONTINUE_CLI_AUTO_UPDATED //Already auto updated, preventing sequential auto-update |
| 98 | ) { |
| 99 | await this.performUpdate(true); |
| 100 | } else { |
| 101 | this.setState({ |
| 102 | status: UpdateStatus.IDLE, |
| 103 | message: isUpdateAvailable |
| 104 | ? `Update available: v${latestVersion}` |
| 105 | : `Continue CLI v${this.currentState.currentVersion}`, |
| 106 | isUpdateAvailable, |
| 107 | latestVersion, |
| 108 | }); |
| 109 | } |
| 110 | } catch (error: any) { |
| 111 | logger.error("Error checking for updates:", error); |
| 112 | this.setState({ |
| 113 | status: UpdateStatus.ERROR, |
| 114 | message: `Continue CLI v${this.currentState.currentVersion}`, |
| 115 | error, |
| 116 | }); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public async setAutoUpdate(value: boolean) { |
| 121 | const globalContext = new GlobalContext(); |
| 122 | globalContext.update("autoUpdateCli", value); |
| 123 | this.setState({ |
| 124 | autoUpdate: value, |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | // TODO this is a hack because our service state update code is broken |
| 129 | // Currently all things that need update use serviceContainer.set manually |
| 130 | // Rather than actually using the stateChanged event |
| 131 | setState(newState: Partial<UpdateServiceState>): void { |
| 132 | super.setState(newState); |
| 133 | serviceContainer.set("update", this.currentState); |
| 134 | } |
| 135 | |
| 136 | async performUpdate(isAutoUpdate?: boolean) { |
| 137 | if (this.currentState.status === "updating") { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | try { |
| 142 | this.setState({ |
| 143 | isAutoUpdate, |
| 144 | status: UpdateStatus.UPDATING, |
| 145 | message: `${isAutoUpdate ? "Auto-updating" : "Updating"} to v${this.currentState.latestVersion}`, |
| 146 | }); |
| 147 | |
| 148 | // Install the update |
| 149 | const { stdout, stderr } = await execAsync("npm i -g @continuedev/cli"); |
| 150 | logger.debug("Update output:", { stdout, stderr }); |
| 151 | |
| 152 | if (stderr) { |
| 153 | const errLines = stderr.split("\n"); |
| 154 | for (const line of errLines) { |
| 155 | const lower = line.toLowerCase().trim(); |
| 156 | if ( |
| 157 | !line || |
| 158 | lower.includes("debugger") || |
| 159 | lower.includes("npm warn") |
| 160 | ) { |
| 161 | continue; |
| 162 | } |
| 163 | this.setState({ |
| 164 | status: UpdateStatus.ERROR, |
| 165 | message: `Error updating to v${this.currentState.latestVersion}`, |
| 166 | error: new Error(stderr), |
| 167 | }); |
| 168 | return; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | this.setState({ |
| 173 | status: UpdateStatus.UPDATED, |
| 174 | message: `${isAutoUpdate ? "Auto-updated to" : "Restart for"} v${this.currentState.latestVersion}`, |
| 175 | isUpdateAvailable: false, |
| 176 | }); |
| 177 | if (isAutoUpdate) { |
| 178 | this.restartCLI(); |
| 179 | } |
| 180 | } catch (error: any) { |
| 181 | logger.error("Error updating CLI:", error); |
| 182 | this.setState({ |
| 183 | status: UpdateStatus.ERROR, |
| 184 | message: isAutoUpdate ? "Auto-update failed" : "Update failed", |
| 185 | error, |
| 186 | }); |
| 187 | setTimeout(() => { |
| 188 | this.setState({ |
| 189 | status: UpdateStatus.IDLE, |
| 190 | message: `/update to v${this.currentState.latestVersion}`, |
| 191 | }); |
| 192 | }, 4000); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | private restartCLI(): void { |
| 197 | try { |
| 198 | const entryPoint = process.argv[1]; |
| 199 | const cliArgs = process.argv.slice(2); |
| 200 | const nodeExecutable = process.execPath; |
| 201 | |
| 202 | logger.debug( |
| 203 | `Preparing for CLI restart with: ${nodeExecutable} ${entryPoint} ${cliArgs.join( |
| 204 | " ", |
| 205 | )}`, |
| 206 | ); |
| 207 | |
| 208 | // Halt/clean up parent cn process |
| 209 | try { |
| 210 | // Remove all input listeners |
| 211 | global.clearTimeout = () => {}; |
| 212 | global.clearInterval = () => {}; |
| 213 | process.stdin.removeAllListeners(); |
| 214 | process.stdin.pause(); |
| 215 | // console.clear(); // Don't want to clear things that were in console before cn started |
| 216 | } catch (e) { |
| 217 | logger.debug("Error cleaning up terminal:", e); |
| 218 | } |
| 219 | |
| 220 | // Spawn a new detached cn process |
| 221 | const child = spawn(nodeExecutable, [entryPoint, ...cliArgs], { |
| 222 | detached: true, |
| 223 | stdio: "inherit", |
| 224 | env: { |
| 225 | ...process.env, |
| 226 | CONTINUE_CLI_AUTO_UPDATED: "true", |
| 227 | }, |
| 228 | }); |
| 229 | |
| 230 | // I did not find a way on existing to avoid a bug where next process has input glitches without leaving parent in place |
| 231 | // So instead of existing, parent will exit when child exits |
| 232 | // process.exit(0); |
| 233 | child.on("exit", (code) => { |
| 234 | process.exit(code); |
| 235 | }); |
| 236 | child.unref(); |
| 237 | } catch (error) { |
| 238 | logger.error("Failed to restart CLI:", error); |
| 239 | } |
| 240 | } |
| 241 | } |