UNPKG

scai

Version:

> **AI-powered CLI for local code analysis, commit message suggestions, and natural-language queries.** 100% local, private, GDPR-friendly, made in Denmark/EU with ā¤ļø.

97 lines (96 loc) • 3.7 kB
// context.ts import { readConfig, Config } from "./config.js"; import { normalizePath } from "./utils/contentUtils.js"; import { getDbForRepo, getDbPathForRepo } from "./db/client.js"; import fs from "fs"; import chalk from "chalk"; import { execSync } from "child_process"; function modelExists(model) { try { const output = execSync("ollama list", { encoding: "utf-8" }); return output .split("\n") .map(line => line.trim()) .filter(Boolean) .some(line => line.toLowerCase().startsWith(model.toLowerCase() + " ") || line.toLowerCase() === model.toLowerCase()); } catch (err) { console.error(chalk.red("āŒ Failed to check models with `ollama list`")); return false; } } export async function updateContext() { const cwd = normalizePath(process.cwd()); const cfg = readConfig(); // šŸ”Ž Look up existing repo by indexDir let repoKey = Object.keys(cfg.repos || {}).find((key) => normalizePath(cfg.repos[key]?.indexDir || "") === cwd); if (!repoKey) { const err = new Error(); err.message = chalk.red("āŒ Current directory is not in your list of indexed repos. See the 'scai index list' command.") + "\n" + chalk.blueBright(" ↳ To get the full benefit of an indexed repo, navigate to your repository root folder.") + "\n\n" + chalk.greenBright(" Run ") + chalk.cyan("scai index set") + chalk.greenBright(" command. This enables Scai to index the repo, find git credentials etc.\n"); throw err; } const activeRepoChanged = cfg.activeRepo !== repoKey; if (activeRepoChanged) { const switched = Config.setActiveRepo(repoKey); if (switched === false) { // ā— If switching is denied by lock, stop updateContext completely return false; } } const repoCfg = cfg.repos[repoKey]; let ok = true; if (activeRepoChanged) { console.log(chalk.yellow("\nšŸ” Updating context...\n")); console.log(`āœ… Active repo: ${chalk.green(repoKey)}`); console.log(`āœ… Index dir: ${chalk.cyan(repoCfg.indexDir || cwd)}`); } // šŸ”‘ Token check const token = repoCfg.githubToken || cfg.githubToken; if (!token) { console.log(`ā„¹ļø No GitHub token found. You can set one with: ${chalk.bold(chalk.bgGreen("scai auth set"))}`); } else if (activeRepoChanged) { console.log(`āœ… GitHub token present`); } // šŸ’¾ DB check const dbPath = getDbPathForRepo(); if (!fs.existsSync(dbPath)) { console.log(chalk.yellow(`šŸ“¦ Initializing DB at ${dbPath}`)); try { getDbForRepo(); } catch { ok = false; } } else if (activeRepoChanged) { console.log(chalk.green("āœ… Database present")); } // 🧠 Model check const model = cfg.model; if (!model) { console.log(chalk.red("āŒ No model configured.") + "\nāž”ļø Set one with: " + chalk.bold(chalk.bgGreen("scai config set-model <model>"))); ok = false; } else if (!modelExists(model)) { console.log(chalk.red(`āŒ Model '${model}' not installed in Ollama.`) + "\nāž”ļø Install with: " + chalk.bold(chalk.yellow(`ollama pull ${model}`)) + " or choose another with: " + chalk.bold(chalk.yellow("scai config set-model <model>"))); ok = false; } if (!ok) { console.log(chalk.bold.red("\nāš ļø Repositoriy context not set correctly\n")); } return ok; }