Elffuss BraidHub

kikocis/continue public

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