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
JavaScript
// 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;
}