| 1 | const fs = require("fs"); |
| 2 | const path = require("path"); |
| 3 | |
| 4 | const ncp = require("ncp").ncp; |
| 5 | const { rimrafSync } = require("rimraf"); |
| 6 | |
| 7 | const { execCmdSync } = require("../../../scripts/util/index"); |
| 8 | |
| 9 | const continueDir = path.join(__dirname, "..", "..", ".."); |
| 10 | |
| 11 | function copyTokenizers() { |
| 12 | fs.copyFileSync( |
| 13 | path.join(__dirname, "../../../core/llm/llamaTokenizerWorkerPool.mjs"), |
| 14 | path.join(__dirname, "../out/llamaTokenizerWorkerPool.mjs"), |
| 15 | ); |
| 16 | console.log("[info] Copied llamaTokenizerWorkerPool"); |
| 17 | |
| 18 | fs.copyFileSync( |
| 19 | path.join(__dirname, "../../../core/llm/llamaTokenizer.mjs"), |
| 20 | path.join(__dirname, "../out/llamaTokenizer.mjs"), |
| 21 | ); |
| 22 | console.log("[info] Copied llamaTokenizer"); |
| 23 | } |
| 24 | |
| 25 | async function buildGui(isGhAction) { |
| 26 | // Make sure we are in the right directory |
| 27 | if (!process.cwd().endsWith("gui")) { |
| 28 | process.chdir(path.join(continueDir, "gui")); |
| 29 | } |
| 30 | if (isGhAction) { |
| 31 | execCmdSync("npm run build"); |
| 32 | } |
| 33 | |
| 34 | // Copy over the dist folder to the JetBrains extension // |
| 35 | const intellijExtensionWebviewPath = path.join( |
| 36 | "..", |
| 37 | "extensions", |
| 38 | "intellij", |
| 39 | "src", |
| 40 | "main", |
| 41 | "resources", |
| 42 | "webview", |
| 43 | ); |
| 44 | |
| 45 | const indexHtmlPath = path.join(intellijExtensionWebviewPath, "index.html"); |
| 46 | fs.copyFileSync(indexHtmlPath, "tmp_index.html"); |
| 47 | rimrafSync(intellijExtensionWebviewPath); |
| 48 | fs.mkdirSync(intellijExtensionWebviewPath, { recursive: true }); |
| 49 | |
| 50 | await new Promise((resolve, reject) => { |
| 51 | ncp("dist", intellijExtensionWebviewPath, (error) => { |
| 52 | if (error) { |
| 53 | console.warn( |
| 54 | "[error] Error copying React app build to JetBrains extension: ", |
| 55 | error, |
| 56 | ); |
| 57 | reject(error); |
| 58 | } |
| 59 | resolve(); |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | // Put back index.html |
| 64 | if (fs.existsSync(indexHtmlPath)) { |
| 65 | rimrafSync(indexHtmlPath); |
| 66 | } |
| 67 | fs.copyFileSync("tmp_index.html", indexHtmlPath); |
| 68 | fs.unlinkSync("tmp_index.html"); |
| 69 | |
| 70 | console.log("[info] Copied gui build to JetBrains extension"); |
| 71 | |
| 72 | // Then copy over the dist folder to the VSCode extension // |
| 73 | const vscodeGuiPath = path.join("../extensions/vscode/gui"); |
| 74 | fs.mkdirSync(vscodeGuiPath, { recursive: true }); |
| 75 | await new Promise((resolve, reject) => { |
| 76 | ncp("dist", vscodeGuiPath, (error) => { |
| 77 | if (error) { |
| 78 | console.log( |
| 79 | "Error copying React app build to VSCode extension: ", |
| 80 | error, |
| 81 | ); |
| 82 | reject(error); |
| 83 | } else { |
| 84 | console.log("Copied gui build to VSCode extension"); |
| 85 | resolve(); |
| 86 | } |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | if (!fs.existsSync(path.join("dist", "assets", "index.js"))) { |
| 91 | throw new Error("gui build did not produce index.js"); |
| 92 | } |
| 93 | if (!fs.existsSync(path.join("dist", "assets", "index.css"))) { |
| 94 | throw new Error("gui build did not produce index.css"); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | async function copyOnnxRuntimeFromNodeModules(target) { |
| 99 | process.chdir(path.join(continueDir, "extensions", "vscode")); |
| 100 | fs.mkdirSync("bin", { recursive: true }); |
| 101 | |
| 102 | await new Promise((resolve, reject) => { |
| 103 | ncp( |
| 104 | path.join(__dirname, "../../../core/node_modules/onnxruntime-node/bin"), |
| 105 | path.join(__dirname, "../bin"), |
| 106 | { |
| 107 | dereference: true, |
| 108 | }, |
| 109 | (error) => { |
| 110 | if (error) { |
| 111 | console.warn("[info] Error copying onnxruntime-node files", error); |
| 112 | reject(error); |
| 113 | } |
| 114 | resolve(); |
| 115 | }, |
| 116 | ); |
| 117 | }); |
| 118 | if (target) { |
| 119 | // If building for production, only need the binaries for current platform |
| 120 | try { |
| 121 | if (!target.startsWith("darwin")) { |
| 122 | rimrafSync(path.join(__dirname, "../bin/napi-v3/darwin")); |
| 123 | } |
| 124 | if (!target.startsWith("linux")) { |
| 125 | rimrafSync(path.join(__dirname, "../bin/napi-v3/linux")); |
| 126 | } |
| 127 | if (!target.startsWith("win")) { |
| 128 | rimrafSync(path.join(__dirname, "../bin/napi-v3/win32")); |
| 129 | } |
| 130 | |
| 131 | // Also don't want to include cuda/shared/tensorrt binaries, they are too large |
| 132 | if (target.startsWith("linux")) { |
| 133 | const filesToRemove = [ |
| 134 | "libonnxruntime_providers_cuda.so", |
| 135 | "libonnxruntime_providers_shared.so", |
| 136 | "libonnxruntime_providers_tensorrt.so", |
| 137 | ]; |
| 138 | filesToRemove.forEach((file) => { |
| 139 | const filepath = path.join( |
| 140 | __dirname, |
| 141 | "../bin/napi-v3/linux/x64", |
| 142 | file, |
| 143 | ); |
| 144 | if (fs.existsSync(filepath)) { |
| 145 | fs.rmSync(filepath); |
| 146 | } |
| 147 | }); |
| 148 | } |
| 149 | } catch (e) { |
| 150 | console.warn("[info] Error removing unused binaries", e); |
| 151 | } |
| 152 | } |
| 153 | console.log("[info] Copied onnxruntime-node"); |
| 154 | } |
| 155 | |
| 156 | async function copyTreeSitterWasms() { |
| 157 | process.chdir(path.join(continueDir, "extensions", "vscode")); |
| 158 | fs.mkdirSync("out", { recursive: true }); |
| 159 | |
| 160 | await new Promise((resolve, reject) => { |
| 161 | ncp( |
| 162 | path.join(__dirname, "../../../core/node_modules/tree-sitter-wasms/out"), |
| 163 | path.join(__dirname, "../out/tree-sitter-wasms"), |
| 164 | { dereference: true }, |
| 165 | (error) => { |
| 166 | if (error) { |
| 167 | console.warn("[error] Error copying tree-sitter-wasm files", error); |
| 168 | reject(error); |
| 169 | } else { |
| 170 | resolve(); |
| 171 | } |
| 172 | }, |
| 173 | ); |
| 174 | }); |
| 175 | |
| 176 | fs.copyFileSync( |
| 177 | path.join(__dirname, "../../../core/vendor/tree-sitter.wasm"), |
| 178 | path.join(__dirname, "../out/tree-sitter.wasm"), |
| 179 | ); |
| 180 | console.log("[info] Copied tree-sitter wasms"); |
| 181 | } |
| 182 | |
| 183 | async function copyTreeSitterTagQryFiles() { |
| 184 | // ncp( |
| 185 | // path.join( |
| 186 | // __dirname, |
| 187 | // "../../../core/node_modules/llm-code-highlighter/dist/tag-qry", |
| 188 | // ), |
| 189 | // path.join(__dirname, "../out/tag-qry"), |
| 190 | // (error) => { |
| 191 | // if (error) |
| 192 | // console.warn("Error copying code-highlighter tag-qry files", error); |
| 193 | // }, |
| 194 | // ); |
| 195 | } |
| 196 | |
| 197 | async function copyNodeModules() { |
| 198 | // Copy node_modules for pre-built binaries |
| 199 | process.chdir(path.join(continueDir, "extensions", "vscode")); |
| 200 | |
| 201 | const NODE_MODULES_TO_COPY = [ |
| 202 | "esbuild", |
| 203 | "@esbuild", |
| 204 | "@lancedb", |
| 205 | "@vscode/ripgrep", |
| 206 | "workerpool", |
| 207 | ]; |
| 208 | fs.mkdirSync("out/node_modules", { recursive: true }); |
| 209 | |
| 210 | await Promise.all( |
| 211 | NODE_MODULES_TO_COPY.map( |
| 212 | (mod) => |
| 213 | new Promise((resolve, reject) => { |
| 214 | fs.mkdirSync(`out/node_modules/${mod}`, { recursive: true }); |
| 215 | ncp( |
| 216 | `node_modules/${mod}`, |
| 217 | `out/node_modules/${mod}`, |
| 218 | { dereference: true }, |
| 219 | function (error) { |
| 220 | if (error) { |
| 221 | console.error(`[error] Error copying ${mod}`, error); |
| 222 | reject(error); |
| 223 | } else { |
| 224 | console.log(`[info] Copied ${mod}`); |
| 225 | resolve(); |
| 226 | } |
| 227 | }, |
| 228 | ); |
| 229 | }), |
| 230 | ), |
| 231 | ); |
| 232 | |
| 233 | // delete esbuild/bin because platform-specific @esbuild is downloaded |
| 234 | fs.rmdirSync(`out/node_modules/esbuild/bin`, { recursive: true }); |
| 235 | |
| 236 | console.log(`[info] Copied ${NODE_MODULES_TO_COPY.join(", ")}`); |
| 237 | } |
| 238 | |
| 239 | // async function downloadEsbuildBinary(isGhAction, isArm, target) { |
| 240 | // process.chdir(path.join(continueDir, "extensions", "vscode")); |
| 241 | |
| 242 | // if (isGhAction && isArm) { |
| 243 | // // Download and unzip esbuild |
| 244 | // console.log("[info] Downloading pre-built esbuild binary"); |
| 245 | // rimrafSync("node_modules/@esbuild"); |
| 246 | // fs.mkdirSync("node_modules/@esbuild", { recursive: true }); |
| 247 | // execCmdSync( |
| 248 | // `curl -o node_modules/@esbuild/esbuild.zip https://continue-server-binaries.s3.us-west-1.amazonaws.com/${target}/esbuild.zip`, |
| 249 | // ); |
| 250 | // execCmdSync(`cd node_modules/@esbuild && unzip esbuild.zip`); |
| 251 | // fs.unlinkSync("node_modules/@esbuild/esbuild.zip"); |
| 252 | // } else { |
| 253 | // // Download esbuild from npm in tmp and copy over |
| 254 | // console.log("npm installing esbuild binary"); |
| 255 | // await installNodeModuleInTempDirAndCopyToCurrent( |
| 256 | // "esbuild@0.17.19", |
| 257 | // "@esbuild", |
| 258 | // ); |
| 259 | // } |
| 260 | // } |
| 261 | |
| 262 | async function downloadEsbuildBinary(target) { |
| 263 | console.log("[info] Downloading pre-built esbuild binary"); |
| 264 | rimrafSync("out/node_modules/@esbuild"); |
| 265 | fs.mkdirSync(`out/node_modules/@esbuild/${target}/bin`, { recursive: true }); |
| 266 | fs.mkdirSync(`out/tmp`, { recursive: true }); |
| 267 | const downloadUrl = { |
| 268 | "darwin-arm64": |
| 269 | "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.17.19.tgz", |
| 270 | "linux-arm64": |
| 271 | "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.17.19.tgz", |
| 272 | "win32-arm64": |
| 273 | "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.17.19.tgz", |
| 274 | "linux-x64": |
| 275 | "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.17.19.tgz", |
| 276 | "darwin-x64": |
| 277 | "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.17.19.tgz", |
| 278 | "win32-x64": |
| 279 | "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.17.19.tgz", |
| 280 | }[target]; |
| 281 | execCmdSync(`curl -L -o out/tmp/esbuild.tgz ${downloadUrl}`); |
| 282 | execCmdSync("cd out/tmp && tar -xvzf esbuild.tgz"); |
| 283 | // Copy the installed package back to the current directory |
| 284 | let tmpPath = "out/tmp/package/bin"; |
| 285 | let outPath = `out/node_modules/@esbuild/${target}/bin`; |
| 286 | if (target.startsWith("win")) { |
| 287 | tmpPath = "out/tmp/package"; |
| 288 | outPath = `out/node_modules/@esbuild/${target}`; |
| 289 | } |
| 290 | |
| 291 | await new Promise((resolve, reject) => { |
| 292 | ncp( |
| 293 | path.join(tmpPath), |
| 294 | path.join(outPath), |
| 295 | { dereference: true }, |
| 296 | (error) => { |
| 297 | if (error) { |
| 298 | console.error(`[error] Error copying esbuild package`, error); |
| 299 | reject(error); |
| 300 | } else { |
| 301 | resolve(); |
| 302 | } |
| 303 | }, |
| 304 | ); |
| 305 | }); |
| 306 | rimrafSync("out/tmp"); |
| 307 | } |
| 308 | |
| 309 | async function downloadSqliteBinary(target) { |
| 310 | console.log("[info] Downloading pre-built sqlite3 binary"); |
| 311 | rimrafSync("../../core/node_modules/sqlite3/build"); |
| 312 | const downloadUrl = { |
| 313 | "darwin-arm64": |
| 314 | "https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v6-darwin-arm64.tar.gz", |
| 315 | "linux-arm64": |
| 316 | "https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v3-linux-arm64.tar.gz", |
| 317 | "win32-arm64": |
| 318 | "https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v6-win32-arm64.tar.gz", |
| 319 | "linux-x64": |
| 320 | "https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v3-linux-x64.tar.gz", |
| 321 | "darwin-x64": |
| 322 | "https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v6-darwin-x64.tar.gz", |
| 323 | "win32-x64": |
| 324 | "https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v3-win32-x64.tar.gz", |
| 325 | }[target]; |
| 326 | execCmdSync( |
| 327 | `curl -L -o ../../core/node_modules/sqlite3/build.tar.gz ${downloadUrl}`, |
| 328 | ); |
| 329 | execCmdSync("cd ../../core/node_modules/sqlite3 && tar -xvzf build.tar.gz"); |
| 330 | fs.unlinkSync("../../core/node_modules/sqlite3/build.tar.gz"); |
| 331 | } |
| 332 | |
| 333 | async function copySqliteBinary() { |
| 334 | process.chdir(path.join(continueDir, "extensions", "vscode")); |
| 335 | console.log("[info] Copying sqlite node binding from core"); |
| 336 | await new Promise((resolve, reject) => { |
| 337 | ncp( |
| 338 | path.join(__dirname, "../../../core/node_modules/sqlite3/build"), |
| 339 | path.join(__dirname, "../out/build"), |
| 340 | { dereference: true }, |
| 341 | (error) => { |
| 342 | if (error) { |
| 343 | console.warn("[error] Error copying sqlite3 files", error); |
| 344 | reject(error); |
| 345 | } else { |
| 346 | resolve(); |
| 347 | } |
| 348 | }, |
| 349 | ); |
| 350 | }); |
| 351 | } |
| 352 | |
| 353 | async function downloadRipgrepBinary(target) { |
| 354 | console.log("[info] Downloading pre-built ripgrep binary"); |
| 355 | rimrafSync("node_modules/@vscode/ripgrep/bin"); |
| 356 | fs.mkdirSync("node_modules/@vscode/ripgrep/bin", { recursive: true }); |
| 357 | 4; |
| 358 | const downloadUrl = { |
| 359 | "darwin-arm64": |
| 360 | "https://github.com/microsoft/ripgrep-prebuilt/releases/download/v13.0.0-10/ripgrep-v13.0.0-10-aarch64-apple-darwin.tar.gz", |
| 361 | "linux-arm64": |
| 362 | "https://github.com/microsoft/ripgrep-prebuilt/releases/download/v13.0.0-10/ripgrep-v13.0.0-10-aarch64-unknown-linux-gnu.tar.gz", |
| 363 | "win32-arm64": |
| 364 | "https://github.com/microsoft/ripgrep-prebuilt/releases/download/v13.0.0-10/ripgrep-v13.0.0-10-aarch64-pc-windows-msvc.zip", |
| 365 | "linux-x64": |
| 366 | "https://github.com/microsoft/ripgrep-prebuilt/releases/download/v13.0.0-10/ripgrep-v13.0.0-10-x86_64-unknown-linux-musl.tar.gz", |
| 367 | "darwin-x64": |
| 368 | "https://github.com/microsoft/ripgrep-prebuilt/releases/download/v13.0.0-10/ripgrep-v13.0.0-10-x86_64-apple-darwin.tar.gz", |
| 369 | "win32-x64": |
| 370 | "https://github.com/microsoft/ripgrep-prebuilt/releases/download/v13.0.0-10/ripgrep-v13.0.0-10-x86_64-pc-windows-msvc.zip", |
| 371 | }[target]; |
| 372 | |
| 373 | if (target.startsWith("win")) { |
| 374 | execCmdSync( |
| 375 | `curl -L -o node_modules/@vscode/ripgrep/bin/build.zip ${downloadUrl}`, |
| 376 | ); |
| 377 | execCmdSync("cd node_modules/@vscode/ripgrep/bin && unzip build.zip"); |
| 378 | fs.unlinkSync("node_modules/@vscode/ripgrep/bin/build.zip"); |
| 379 | } else { |
| 380 | execCmdSync( |
| 381 | `curl -L -o node_modules/@vscode/ripgrep/bin/build.tar.gz ${downloadUrl}`, |
| 382 | ); |
| 383 | execCmdSync( |
| 384 | "cd node_modules/@vscode/ripgrep/bin && tar -xvzf build.tar.gz", |
| 385 | ); |
| 386 | fs.unlinkSync("node_modules/@vscode/ripgrep/bin/build.tar.gz"); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | async function installNodeModuleInTempDirAndCopyToCurrent(packageName, toCopy) { |
| 391 | console.log(`Copying ${packageName} to ${toCopy}`); |
| 392 | // This is a way to install only one package without npm trying to install all the dependencies |
| 393 | // Create a temporary directory for installing the package |
| 394 | const adjustedName = packageName.replace(/@/g, "").replace("/", "-"); |
| 395 | |
| 396 | const tempDir = `/tmp/continue-node_modules-${adjustedName}`; |
| 397 | const currentDir = process.cwd(); |
| 398 | |
| 399 | // Remove the dir we will be copying to |
| 400 | rimrafSync(`node_modules/${toCopy}`); |
| 401 | |
| 402 | // Ensure the temporary directory exists |
| 403 | fs.mkdirSync(tempDir, { recursive: true }); |
| 404 | |
| 405 | try { |
| 406 | // Move to the temporary directory |
| 407 | process.chdir(tempDir); |
| 408 | |
| 409 | // Initialize a new package.json and install the package |
| 410 | execCmdSync(`npm init -y && npm i -f ${packageName} --no-save`); |
| 411 | |
| 412 | console.log( |
| 413 | `Contents of: ${packageName}`, |
| 414 | fs.readdirSync(path.join(tempDir, "node_modules", toCopy)), |
| 415 | ); |
| 416 | |
| 417 | // Without this it seems the file isn't completely written to disk |
| 418 | // Ideally we validate file integrity in the validation at the end |
| 419 | await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 420 | |
| 421 | // Copy the installed package back to the current directory |
| 422 | await new Promise((resolve, reject) => { |
| 423 | ncp( |
| 424 | path.join(tempDir, "node_modules", toCopy), |
| 425 | path.join(currentDir, "node_modules", toCopy), |
| 426 | { dereference: true }, |
| 427 | (error) => { |
| 428 | if (error) { |
| 429 | console.error( |
| 430 | `[error] Error copying ${packageName} package`, |
| 431 | error, |
| 432 | ); |
| 433 | reject(error); |
| 434 | } else { |
| 435 | resolve(); |
| 436 | } |
| 437 | }, |
| 438 | ); |
| 439 | }); |
| 440 | } finally { |
| 441 | // Clean up the temporary directory |
| 442 | // rimrafSync(tempDir); |
| 443 | |
| 444 | // Return to the original directory |
| 445 | process.chdir(currentDir); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | async function copyScripts() { |
| 450 | process.chdir(path.join(continueDir, "extensions", "vscode")); |
| 451 | console.log("[info] Copying scripts from core"); |
| 452 | fs.copyFileSync( |
| 453 | path.join(__dirname, "../../../core/util/start_ollama.sh"), |
| 454 | path.join(__dirname, "../out/start_ollama.sh"), |
| 455 | ); |
| 456 | console.log("[info] Copied script files"); |
| 457 | } |
| 458 | |
| 459 | // We can't simply touch one of our files to trigger a rebuild, because |
| 460 | // esbuild doesn't always use modifications times to detect changes - |
| 461 | // for example, if it finds a file changed within the last 3 seconds, |
| 462 | // it will fall back to full-contents-comparison for that file |
| 463 | // |
| 464 | // So to facilitate development workflows, we always include a timestamp string |
| 465 | // in the build |
| 466 | function writeBuildTimestamp() { |
| 467 | fs.writeFileSync( |
| 468 | path.join(continueDir, "extensions/vscode", "src/.buildTimestamp.ts"), |
| 469 | `export default "${new Date().toISOString()}";\n`, |
| 470 | ); |
| 471 | } |
| 472 | |
| 473 | module.exports = { |
| 474 | continueDir, |
| 475 | buildGui, |
| 476 | copyOnnxRuntimeFromNodeModules, |
| 477 | copyTreeSitterWasms, |
| 478 | copyTreeSitterTagQryFiles, |
| 479 | copyNodeModules, |
| 480 | downloadEsbuildBinary, |
| 481 | copySqliteBinary, |
| 482 | installNodeModuleInTempDirAndCopyToCurrent, |
| 483 | downloadSqliteBinary, |
| 484 | downloadRipgrepBinary, |
| 485 | copyTokenizers, |
| 486 | copyScripts, |
| 487 | writeBuildTimestamp, |
| 488 | }; |