Elffuss BraidHub

kikocis/continue public

main · 13.3 KiBRawPermalink
1const fs = require("fs");
2const path = require("path");
3
4const ncp = require("ncp").ncp;
5const { rimrafSync } = require("rimraf");
6
7const {
8 validateFilesPresent,
9 execCmdSync,
10 autodetectPlatformAndArch,
11} = require("../../../scripts/util/index");
12
13const { copySqlite, copyEsbuild } = require("./download-copy-sqlite-esbuild");
14const { generateAndCopyConfigYamlSchema } = require("./generate-copy-config");
15const { installAndCopyNodeModules } = require("./install-copy-nodemodule");
16const { npmInstall } = require("./npm-install");
17const { writeBuildTimestamp, continueDir } = require("./utils");
18
19// Clear folders that will be packaged to ensure clean slate
20rimrafSync(path.join(__dirname, "..", "bin"));
21rimrafSync(path.join(__dirname, "..", "out"));
22fs.mkdirSync(path.join(__dirname, "..", "out", "node_modules"), {
23 recursive: true,
24});
25const guiDist = path.join(__dirname, "..", "..", "..", "gui", "dist");
26if (!fs.existsSync(guiDist)) {
27 fs.mkdirSync(guiDist, { recursive: true });
28}
29
30const skipInstalls = process.env.SKIP_INSTALLS === "true";
31
32// Get the target to package for
33let target = undefined;
34const args = process.argv;
35if (args[2] === "--target") {
36 target = args[3];
37}
38
39let os;
40let arch;
41if (target) {
42 [os, arch] = target.split("-");
43} else {
44 [os, arch] = autodetectPlatformAndArch();
45}
46
47if (os === "alpine") {
48 os = "linux";
49}
50if (arch === "armhf") {
51 arch = "arm64";
52}
53target = `${os}-${arch}`;
54console.log("[info] Using target: ", target);
55
56const exe = os === "win32" ? ".exe" : "";
57
58const isArmTarget =
59 target === "darwin-arm64" ||
60 target === "linux-arm64" ||
61 target === "win32-arm64";
62
63const isWinTarget = target?.startsWith("win");
64const isLinuxTarget = target?.startsWith("linux");
65const isMacTarget = target?.startsWith("darwin");
66
67void (async () => {
68 const startTime = Date.now();
69 console.log(
70 `[info] Packaging extension for target ${target} - started at ${new Date().toISOString()}`,
71 );
72
73 // Make sure we have an initial timestamp file
74 writeBuildTimestamp();
75
76 if (!skipInstalls) {
77 const installStart = Date.now();
78 console.log(`[timer] Starting npm installs at ${new Date().toISOString()}`);
79 await Promise.all([generateAndCopyConfigYamlSchema(), npmInstall()]);
80 console.log(
81 `[timer] npm installs completed in ${Date.now() - installStart}ms`,
82 );
83 }
84
85 process.chdir(path.join(continueDir, "gui"));
86
87 // Copy over the dist folder to the JetBrains extension //
88 const intellijExtensionWebviewPath = path.join(
89 "..",
90 "extensions",
91 "intellij",
92 "src",
93 "main",
94 "resources",
95 "webview",
96 );
97
98 const indexHtmlPath = path.join(intellijExtensionWebviewPath, "index.html");
99 fs.copyFileSync(indexHtmlPath, "tmp_index.html");
100 rimrafSync(intellijExtensionWebviewPath);
101 fs.mkdirSync(intellijExtensionWebviewPath, { recursive: true });
102
103 const jetbrainsCopyStart = Date.now();
104 console.log(`[timer] Starting JetBrains copy at ${new Date().toISOString()}`);
105 await new Promise((resolve, reject) => {
106 ncp("dist", intellijExtensionWebviewPath, (error) => {
107 if (error) {
108 console.warn(
109 "[error] Error copying React app build to JetBrains extension: ",
110 error,
111 );
112 reject(error);
113 }
114 resolve();
115 });
116 });
117 console.log(
118 `[timer] JetBrains copy completed in ${Date.now() - jetbrainsCopyStart}ms`,
119 );
120
121 // Put back index.html
122 if (fs.existsSync(indexHtmlPath)) {
123 rimrafSync(indexHtmlPath);
124 }
125 fs.copyFileSync("tmp_index.html", indexHtmlPath);
126 fs.unlinkSync("tmp_index.html");
127
128 console.log("[info] Copied gui build to JetBrains extension");
129
130 // Then copy over the dist folder to the VSCode extension //
131 const vscodeGuiPath = path.join("../extensions/vscode/gui");
132 rimrafSync(vscodeGuiPath);
133 fs.mkdirSync(vscodeGuiPath, { recursive: true });
134 const vscodeCopyStart = Date.now();
135 console.log(`[timer] Starting VSCode copy at ${new Date().toISOString()}`);
136 await new Promise((resolve, reject) => {
137 ncp("dist", vscodeGuiPath, (error) => {
138 if (error) {
139 console.log(
140 "Error copying React app build to VSCode extension: ",
141 error,
142 );
143 reject(error);
144 } else {
145 console.log("Copied gui build to VSCode extension");
146 resolve();
147 }
148 });
149 });
150 console.log(
151 `[timer] VSCode copy completed in ${Date.now() - vscodeCopyStart}ms`,
152 );
153
154 if (!fs.existsSync(path.join("dist", "assets", "index.js"))) {
155 throw new Error("gui build did not produce index.js");
156 }
157 if (!fs.existsSync(path.join("dist", "assets", "index.css"))) {
158 throw new Error("gui build did not produce index.css");
159 }
160
161 // Copy over native / wasm modules //
162 process.chdir("../extensions/vscode");
163
164 fs.mkdirSync("bin", { recursive: true });
165
166 // onnxruntime-node
167 const onnxCopyStart = Date.now();
168 console.log(
169 `[timer] Starting onnxruntime copy at ${new Date().toISOString()}`,
170 );
171 await new Promise((resolve, reject) => {
172 ncp(
173 path.join(__dirname, "../../../core/node_modules/onnxruntime-node/bin"),
174 path.join(__dirname, "../bin"),
175 {
176 dereference: true,
177 },
178 (error) => {
179 if (error) {
180 console.warn("[info] Error copying onnxruntime-node files", error);
181 reject(error);
182 }
183 resolve();
184 },
185 );
186 });
187 console.log(
188 `[timer] onnxruntime copy completed in ${Date.now() - onnxCopyStart}ms`,
189 );
190 if (target) {
191 // If building for production, only need the binaries for current platform
192 try {
193 if (!target.startsWith("darwin")) {
194 rimrafSync(path.join(__dirname, "../bin/napi-v3/darwin"));
195 }
196 if (!target.startsWith("linux")) {
197 rimrafSync(path.join(__dirname, "../bin/napi-v3/linux"));
198 }
199 if (!target.startsWith("win")) {
200 rimrafSync(path.join(__dirname, "../bin/napi-v3/win32"));
201 }
202
203 // Also don't want to include cuda/shared/tensorrt binaries, they are too large
204 if (target.startsWith("linux")) {
205 const filesToRemove = [
206 "libonnxruntime_providers_cuda.so",
207 "libonnxruntime_providers_shared.so",
208 "libonnxruntime_providers_tensorrt.so",
209 ];
210 filesToRemove.forEach((file) => {
211 const filepath = path.join(
212 __dirname,
213 "../bin/napi-v3/linux/x64",
214 file,
215 );
216 if (fs.existsSync(filepath)) {
217 fs.rmSync(filepath);
218 }
219 });
220 }
221 } catch (e) {
222 console.warn("[info] Error removing unused binaries", e);
223 }
224 }
225 console.log("[info] Copied onnxruntime-node");
226
227 // tree-sitter-wasm
228 fs.mkdirSync("out", { recursive: true });
229
230 await new Promise((resolve, reject) => {
231 ncp(
232 path.join(__dirname, "../../../core/node_modules/tree-sitter-wasms/out"),
233 path.join(__dirname, "../out/tree-sitter-wasms"),
234 { dereference: true },
235 (error) => {
236 if (error) {
237 console.warn("[error] Error copying tree-sitter-wasm files", error);
238 reject(error);
239 } else {
240 resolve();
241 }
242 },
243 );
244 });
245
246 const filesToCopy = [
247 "../../../core/vendor/tree-sitter.wasm",
248 "../../../core/llm/llamaTokenizerWorkerPool.mjs",
249 "../../../core/llm/llamaTokenizer.mjs",
250 "../../../core/llm/tiktokenWorkerPool.mjs",
251 "../../../core/util/start_ollama.sh",
252 ];
253
254 for (const f of filesToCopy) {
255 fs.copyFileSync(
256 path.join(__dirname, f),
257 path.join(__dirname, "..", "out", path.basename(f)),
258 );
259 console.log(`[info] Copied ${path.basename(f)}`);
260 }
261
262 // tree-sitter tag query files
263 // ncp(
264 // path.join(
265 // __dirname,
266 // "../../../core/node_modules/llm-code-highlighter/dist/tag-qry",
267 // ),
268 // path.join(__dirname, "../out/tag-qry"),
269 // (error) => {
270 // if (error)
271 // console.warn("Error copying code-highlighter tag-qry files", error);
272 // },
273 // );
274
275 // textmate-syntaxes
276 await new Promise((resolve, reject) => {
277 ncp(
278 path.join(__dirname, "../textmate-syntaxes"),
279 path.join(__dirname, "../gui/textmate-syntaxes"),
280 (error) => {
281 if (error) {
282 console.warn("[error] Error copying textmate-syntaxes", error);
283 reject(error);
284 } else {
285 resolve();
286 }
287 },
288 );
289 });
290
291 if (!skipInstalls) {
292 // GitHub Actions doesn't support ARM, so we need to download pre-saved binaries
293 // 02/07/25 - the above comment is out of date, there is now support for ARM runners on GitHub Actions
294 if (isArmTarget) {
295 // lancedb binary
296 const packageToInstall = {
297 "darwin-arm64": "@lancedb/vectordb-darwin-arm64",
298 "linux-arm64": "@lancedb/vectordb-linux-arm64-gnu",
299 "win32-arm64": "@lancedb/vectordb-win32-arm64-msvc",
300 }[target];
301 console.log(
302 "[info] Downloading pre-built lancedb binary: " + packageToInstall,
303 );
304
305 await Promise.all([
306 copyEsbuild(target),
307 copySqlite(target),
308 installAndCopyNodeModules(packageToInstall, "@lancedb"),
309 ]);
310 } else {
311 // Download esbuild from npm in tmp and copy over
312 console.log("[info] npm installing esbuild binary");
313 await installAndCopyNodeModules("esbuild@0.17.19", "@esbuild");
314 }
315 }
316
317 console.log("[info] Copying sqlite node binding from core");
318 await new Promise((resolve, reject) => {
319 ncp(
320 path.join(__dirname, "../../../core/node_modules/sqlite3/build"),
321 path.join(__dirname, "../out/build"),
322 { dereference: true },
323 (error) => {
324 if (error) {
325 console.warn("[error] Error copying sqlite3 files", error);
326 reject(error);
327 } else {
328 resolve();
329 }
330 },
331 );
332 });
333
334 // Copied here as well for the VS Code test suite
335 await new Promise((resolve, reject) => {
336 ncp(
337 path.join(__dirname, "../../../core/node_modules/sqlite3/build"),
338 path.join(__dirname, "../out"),
339 { dereference: true },
340 (error) => {
341 if (error) {
342 console.warn("[error] Error copying sqlite3 files", error);
343 reject(error);
344 } else {
345 resolve();
346 }
347 },
348 );
349 });
350
351 // Copy node_modules for pre-built binaries
352 const NODE_MODULES_TO_COPY = [
353 "esbuild",
354 "@esbuild",
355 "@lancedb",
356 "@vscode/ripgrep",
357 "workerpool",
358 ];
359
360 fs.mkdirSync("out/node_modules", { recursive: true });
361
362 await Promise.all(
363 NODE_MODULES_TO_COPY.map(
364 (mod) =>
365 new Promise((resolve, reject) => {
366 fs.mkdirSync(`out/node_modules/${mod}`, { recursive: true });
367 ncp(
368 `node_modules/${mod}`,
369 `out/node_modules/${mod}`,
370 { dereference: true },
371 function (error) {
372 if (error) {
373 console.error(`[error] Error copying ${mod}`, error);
374 reject(error);
375 } else {
376 console.log(`[info] Copied ${mod}`);
377 resolve();
378 }
379 },
380 );
381 }),
382 ),
383 );
384
385 // delete esbuild/bin because platform-specific @esbuild is downloaded
386 fs.rmSync(`out/node_modules/esbuild/bin`, { recursive: true });
387
388 console.log(`[info] Copied ${NODE_MODULES_TO_COPY.join(", ")}`);
389
390 // Copy over any worker files
391 fs.cpSync(
392 "node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js",
393 "out/xhr-sync-worker.js",
394 );
395
396 // Validate the all of the necessary files are present
397 validateFilesPresent([
398 // Queries used to create the index for @code context provider
399 "tree-sitter/code-snippet-queries/c_sharp.scm",
400
401 // Queries used for @outline and @highlights context providers
402 "tag-qry/tree-sitter-c_sharp-tags.scm",
403
404 // onnx runtime bindngs
405 `bin/napi-v3/${os}/${arch}/onnxruntime_binding.node`,
406 `bin/napi-v3/${os}/${arch}/${
407 isMacTarget
408 ? "libonnxruntime.1.14.0.dylib"
409 : isLinuxTarget
410 ? "libonnxruntime.so.1.14.0"
411 : "onnxruntime.dll"
412 }`,
413
414 // Code/styling for the sidebar
415 "gui/assets/index.js",
416 "gui/assets/index.css",
417
418 // Tutorial
419 "media/move-chat-panel-right.md",
420 "continue_tutorial.py",
421 "config_schema.json",
422
423 // Embeddings model
424 "models/all-MiniLM-L6-v2/config.json",
425 "models/all-MiniLM-L6-v2/special_tokens_map.json",
426 "models/all-MiniLM-L6-v2/tokenizer_config.json",
427 "models/all-MiniLM-L6-v2/tokenizer.json",
428 "models/all-MiniLM-L6-v2/vocab.txt",
429 "models/all-MiniLM-L6-v2/onnx/model_quantized.onnx",
430
431 // node_modules (it's a bit confusing why this is necessary)
432 `node_modules/@vscode/ripgrep/bin/rg${exe}`,
433
434 // out directory (where the extension.js lives)
435 // "out/extension.js", This is generated afterward by vsce
436 // web-tree-sitter
437 "out/tree-sitter.wasm",
438 // Worker required by jsdom
439 "out/xhr-sync-worker.js",
440 // SQLite3 Node native module
441 "out/build/Release/node_sqlite3.node",
442
443 // out/node_modules (to be accessed by extension.js)
444 `out/node_modules/@vscode/ripgrep/bin/rg${exe}`,
445 `out/node_modules/@esbuild/${
446 target === "win32-arm64"
447 ? "esbuild.exe"
448 : target === "win32-x64"
449 ? "win32-x64/esbuild.exe"
450 : `${target}/bin/esbuild`
451 }`,
452 `out/node_modules/@lancedb/vectordb-${target}${isWinTarget ? "-msvc" : ""}${isLinuxTarget ? "-gnu" : ""}/index.node`,
453 `out/node_modules/esbuild/lib/main.js`,
454 ]);
455
456 console.log(
457 `[timer] Prepackage completed in ${Date.now() - startTime}ms - finished at ${new Date().toISOString()}`,
458 );
459 process.exit(0);
460})();