UNPKG

termcode

Version:

Superior terminal AI coding agent with enterprise-grade security, intelligent error recovery, performance monitoring, and plugin system - Advanced Claude Code alternative

24 lines (23 loc) 908 B
import { promises as fs } from "node:fs"; import path from "node:path"; function dot(a, b) { let s = 0; for (let i = 0; i < Math.min(a.length, b.length); i++) s += a[i] * b[i]; return s; } export async function retrieve(repo, queryEmbedding, k = 12) { const p = path.resolve(repo, ".termcode-index.json"); const indexData = JSON.parse(await fs.readFile(p, "utf8")); // Handle both old format (array) and new format (object with chunks) const chunks = Array.isArray(indexData) ? indexData : indexData.chunks || []; // Filter chunks that have embeddings and calculate scores const chunksWithScores = chunks .filter((c) => c.embedding && Array.isArray(c.embedding)) .map((c) => ({ ...c, score: dot(c.embedding, queryEmbedding) })); chunksWithScores.sort((a, b) => b.score - a.score); return chunksWithScores.slice(0, k); }