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