| 1 | import * as os from "node:os"; |
| 2 | |
| 3 | import * as vscode from "vscode"; |
| 4 | |
| 5 | function charIsEscapedAtIndex(index: number, str: string): boolean { |
| 6 | if (index === 0) { |
| 7 | return false; |
| 8 | } |
| 9 | if (str[index - 1] !== "\\") { |
| 10 | return false; |
| 11 | } |
| 12 | return !charIsEscapedAtIndex(index - 1, str); |
| 13 | } |
| 14 | |
| 15 | export function convertSingleToDoubleQuoteJSON(json: string): string { |
| 16 | const singleQuote = "'"; |
| 17 | const doubleQuote = '"'; |
| 18 | const isQuote = (char: string) => |
| 19 | char === doubleQuote || char === singleQuote; |
| 20 | |
| 21 | let newJson = ""; |
| 22 | let insideString = false; |
| 23 | let enclosingQuoteType = doubleQuote; |
| 24 | for (let i = 0; i < json.length; i++) { |
| 25 | if (insideString) { |
| 26 | if (json[i] === enclosingQuoteType && !charIsEscapedAtIndex(i, json)) { |
| 27 | // Close string with a double quote |
| 28 | insideString = false; |
| 29 | newJson += doubleQuote; |
| 30 | } else if (json[i] === singleQuote) { |
| 31 | if (charIsEscapedAtIndex(i, json)) { |
| 32 | // Unescape single quote |
| 33 | newJson = newJson.slice(0, -1); |
| 34 | } |
| 35 | newJson += singleQuote; |
| 36 | } else if (json[i] === doubleQuote) { |
| 37 | if (!charIsEscapedAtIndex(i, json)) { |
| 38 | // Escape double quote |
| 39 | newJson += "\\"; |
| 40 | } |
| 41 | newJson += doubleQuote; |
| 42 | } else { |
| 43 | newJson += json[i]; |
| 44 | } |
| 45 | } else { |
| 46 | if (isQuote(json[i])) { |
| 47 | insideString = true; |
| 48 | enclosingQuoteType = json[i]; |
| 49 | newJson += doubleQuote; |
| 50 | } else { |
| 51 | newJson += json[i]; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return newJson; |
| 57 | } |
| 58 | |
| 59 | export function debounced(delay: number, fn: (...args: any[]) => void) { |
| 60 | let timerId: NodeJS.Timeout | null; |
| 61 | return (...args: any[]) => { |
| 62 | if (timerId) { |
| 63 | clearTimeout(timerId); |
| 64 | } |
| 65 | timerId = setTimeout(() => { |
| 66 | fn(...args); |
| 67 | timerId = null; |
| 68 | }, delay); |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | type Platform = "mac" | "linux" | "windows" | "unknown"; |
| 73 | type Architecture = "x64" | "arm64" | "unknown"; |
| 74 | |
| 75 | export function getPlatform(): Platform { |
| 76 | const platform = os.platform(); |
| 77 | if (platform === "darwin") { |
| 78 | return "mac"; |
| 79 | } else if (platform === "linux") { |
| 80 | return "linux"; |
| 81 | } else if (platform === "win32") { |
| 82 | return "windows"; |
| 83 | } else { |
| 84 | return "unknown"; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | export function getArchitecture(): Architecture { |
| 89 | const arch = os.arch(); |
| 90 | if (arch === "x64" || arch === "ia32") { |
| 91 | return "x64"; |
| 92 | } else if (arch === "arm64" || arch === "arm") { |
| 93 | return "arm64"; |
| 94 | } else { |
| 95 | return "unknown"; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | export function isUnsupportedPlatform(): { |
| 100 | isUnsupported: boolean; |
| 101 | reason?: string; |
| 102 | } { |
| 103 | const platform = getPlatform(); |
| 104 | const arch = getArchitecture(); |
| 105 | |
| 106 | if (platform === "windows" && arch === "arm64") { |
| 107 | return { |
| 108 | isUnsupported: true, |
| 109 | reason: |
| 110 | "Windows ARM64 is not currently supported due to missing native dependencies (sqlite3, onnxruntime). Please use the extension on Windows x64, macOS, or Linux instead.", |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | // if (platform === "unknown" || arch === "unknown") { |
| 115 | // return { |
| 116 | // isUnsupported: true, |
| 117 | // reason: `Unsupported platform combination: ${os.platform()}-${os.arch()}. Continue extension supports Windows x64, macOS (Intel/Apple Silicon), and Linux (x64/ARM64).`, |
| 118 | // }; |
| 119 | // } |
| 120 | |
| 121 | return { isUnsupported: false }; |
| 122 | } |
| 123 | |
| 124 | export function getAltOrOption() { |
| 125 | if (getPlatform() === "mac") { |
| 126 | return "⌥"; |
| 127 | } else { |
| 128 | return "Alt"; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | export function getMetaKeyLabel() { |
| 133 | const platform = getPlatform(); |
| 134 | switch (platform) { |
| 135 | case "mac": |
| 136 | return "⌘"; |
| 137 | case "linux": |
| 138 | case "windows": |
| 139 | return "Ctrl"; |
| 140 | default: |
| 141 | return "Ctrl"; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | export function getMetaKeyName() { |
| 146 | const platform = getPlatform(); |
| 147 | switch (platform) { |
| 148 | case "mac": |
| 149 | return "Cmd"; |
| 150 | case "linux": |
| 151 | case "windows": |
| 152 | return "Ctrl"; |
| 153 | default: |
| 154 | return "Ctrl"; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | export function getExtensionVersion(): string { |
| 159 | const extension = vscode.extensions.getExtension("continue.continue"); |
| 160 | return extension?.packageJSON.version || "0.1.0"; |
| 161 | } |
| 162 | |
| 163 | export function getvsCodeUriScheme(): string { |
| 164 | return vscode.env.uriScheme; |
| 165 | } |
| 166 | |
| 167 | export function isExtensionPrerelease(): boolean { |
| 168 | const extensionVersion = getExtensionVersion(); |
| 169 | const versionParts = extensionVersion.split("."); |
| 170 | if (versionParts.length >= 2) { |
| 171 | const minorVersion = parseInt(versionParts[1], 10); |
| 172 | if (!isNaN(minorVersion)) { |
| 173 | return minorVersion % 2 !== 0; |
| 174 | } |
| 175 | } |
| 176 | return false; |
| 177 | } |