Elffuss BraidHub

kikocis/continue public

main · 3.0 KiBRawPermalink
1import crypto from "crypto";
2import { exec } from "node:child_process";
3import path from "node:path";
4import { IDE } from "..";
5
6export interface ModelInfo {
7 id: string;
8 size: number;
9 digest: string;
10}
11
12export async function isOllamaInstalled(): Promise<boolean> {
13 return new Promise((resolve, _reject) => {
14 const command =
15 process.platform === "win32" ? "where.exe ollama" : "which ollama";
16 exec(command, (error, _stdout, _stderr) => {
17 resolve(!error);
18 });
19 });
20}
21
22export async function startLocalOllama(ide: IDE): Promise<any> {
23 let startCommand: string | undefined;
24
25 switch (process.platform) {
26 case "darwin": //MacOS
27 startCommand = "open -a Ollama.app\n";
28 break;
29
30 case "win32": //Windows
31 startCommand = '& "ollama app.exe"\n';
32 break;
33
34 default: //Linux...
35 const start_script_path = path.resolve(__dirname, "./start_ollama.sh");
36 if (await ide.fileExists(`file:/${start_script_path}`)) {
37 startCommand = `set -e && chmod +x ${start_script_path} && ${start_script_path}\n`;
38 console.log(`Ollama Linux startup script at : ${start_script_path}`);
39 } else {
40 return ide.showToast(
41 "error",
42 `Cannot start Ollama: could not find ${start_script_path}!`,
43 );
44 }
45 }
46 if (startCommand) {
47 return ide.runCommand(startCommand, {
48 reuseTerminal: true,
49 terminalName: "Start Ollama",
50 });
51 }
52}
53
54export async function getRemoteModelInfo(
55 modelId: string,
56 signal?: AbortSignal,
57): Promise<ModelInfo | undefined> {
58 const start = Date.now();
59 const [modelName, tag = "latest"] = modelId.split(":");
60 const url = `https://registry.ollama.ai/v2/library/${modelName}/manifests/${tag}`;
61 try {
62 const sig = signal ? signal : AbortSignal.timeout(3000);
63 const response = await fetch(url, { signal: sig });
64
65 if (!response.ok) {
66 throw new Error(`Failed to fetch the model page: ${response.statusText}`);
67 }
68
69 // First, read the response body as an ArrayBuffer to compute the digest
70 const buffer = await response.arrayBuffer();
71 const digest = getDigest(buffer);
72
73 // Then, decode the ArrayBuffer into a string and parse it as JSON
74 const text = new TextDecoder().decode(buffer);
75 const manifest = JSON.parse(text) as {
76 config: { size: number };
77 layers: { size: number }[];
78 };
79 const modelSize =
80 manifest.config.size +
81 manifest.layers.reduce((sum, layer) => sum + layer.size, 0);
82
83 const data: ModelInfo = {
84 id: modelId,
85 size: modelSize,
86 digest,
87 };
88 // Cache the successful result
89 return data;
90 } catch (error) {
91 console.error(`Error fetching or parsing model info: ${error}`);
92 } finally {
93 const elapsed = Date.now() - start;
94 console.log(`Fetched remote information for ${modelId} in ${elapsed} ms`);
95 }
96 return undefined;
97}
98
99function getDigest(buffer: ArrayBuffer): string {
100 const hash = crypto.createHash("sha256");
101 hash.update(new Uint8Array(buffer));
102 return hash.digest("hex");
103}