| 1 | import { readFileSync } from "fs"; |
| 2 | import { dirname, join } from "path"; |
| 3 | import { fileURLToPath } from "url"; |
| 4 | |
| 5 | import node_machine_id from "node-machine-id"; |
| 6 | |
| 7 | import { isAuthenticatedConfig, loadAuthConfig } from "./auth/workos.js"; |
| 8 | import { logger } from "./util/logger.js"; |
| 9 | |
| 10 | export function getVersion(): string { |
| 11 | try { |
| 12 | const __filename = fileURLToPath(import.meta.url); |
| 13 | const __dirname = dirname(__filename); |
| 14 | const packageJsonPath = join(__dirname, "../package.json"); |
| 15 | const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8")); |
| 16 | return packageJson.version; |
| 17 | } catch { |
| 18 | console.warn("Warning: Could not read version from package.json"); |
| 19 | return "unknown"; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | function getEventUserId(): string { |
| 24 | const authConfig = loadAuthConfig(); |
| 25 | |
| 26 | if (isAuthenticatedConfig(authConfig)) { |
| 27 | return authConfig.userId; |
| 28 | } |
| 29 | |
| 30 | // Fall back to unique machine id if not signed in |
| 31 | return node_machine_id.machineIdSync(); |
| 32 | } |
| 33 | |
| 34 | // Singleton to cache the latest version result |
| 35 | let latestVersionCache: Promise<string | null> | null = null; |
| 36 | |
| 37 | export async function getLatestVersion( |
| 38 | signal?: AbortSignal, |
| 39 | ): Promise<string | null> { |
| 40 | // Return cached promise if it exists |
| 41 | if (latestVersionCache) { |
| 42 | return latestVersionCache; |
| 43 | } |
| 44 | |
| 45 | // Create and cache the promise |
| 46 | latestVersionCache = (async () => { |
| 47 | try { |
| 48 | const id = getEventUserId(); |
| 49 | const response = await fetch( |
| 50 | `https://api.continue.dev/cn/info?id=${encodeURIComponent(id)}`, |
| 51 | { signal }, |
| 52 | ); |
| 53 | if (!response.ok) { |
| 54 | throw new Error(`HTTP error! status: ${response.status}`); |
| 55 | } |
| 56 | const data = await response.json(); |
| 57 | return data.version; |
| 58 | } catch (error) { |
| 59 | if (error instanceof Error && error.name === "AbortError") { |
| 60 | // Request was aborted, don't log |
| 61 | return null; |
| 62 | } |
| 63 | logger?.debug( |
| 64 | "Warning: Could not fetch latest version from api.continue.dev", |
| 65 | ); |
| 66 | return null; |
| 67 | } |
| 68 | })(); |
| 69 | |
| 70 | return latestVersionCache; |
| 71 | } |
| 72 | |
| 73 | getLatestVersion() |
| 74 | .then((version) => { |
| 75 | if (version) { |
| 76 | logger?.info(`Latest version: ${version}`); |
| 77 | } |
| 78 | }) |
| 79 | .catch((error) => { |
| 80 | logger?.debug( |
| 81 | `Warning: Could not fetch latest version from api.continue.dev: ${error}`, |
| 82 | ); |
| 83 | }); |
| 84 | |
| 85 | export function compareVersions( |
| 86 | current: string, |
| 87 | latest: string, |
| 88 | ): "newer" | "same" | "older" { |
| 89 | if (current === "unknown" || latest === "unknown") { |
| 90 | return "same"; |
| 91 | } |
| 92 | |
| 93 | // Simple semantic version comparison |
| 94 | const parseVersion = (version: string) => { |
| 95 | const parts = version |
| 96 | .replace(/^v/, "") |
| 97 | .split(".") |
| 98 | .map((part) => parseInt(part, 10)); |
| 99 | return { major: parts[0] || 0, minor: parts[1] || 0, patch: parts[2] || 0 }; |
| 100 | }; |
| 101 | |
| 102 | const currentParts = parseVersion(current); |
| 103 | const latestParts = parseVersion(latest); |
| 104 | |
| 105 | if (currentParts.major > latestParts.major) return "newer"; |
| 106 | if (currentParts.major < latestParts.major) return "older"; |
| 107 | if (currentParts.minor > latestParts.minor) return "newer"; |
| 108 | if (currentParts.minor < latestParts.minor) return "older"; |
| 109 | if (currentParts.patch > latestParts.patch) return "newer"; |
| 110 | if (currentParts.patch < latestParts.patch) return "older"; |
| 111 | |
| 112 | return "same"; |
| 113 | } |