MCPcopy Create free account
hub / github.com/cortexkit/magic-context / main

Function main

packages/plugin/scripts/test-semantic-search.ts:18–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16const query = process.argv[2] ?? "how does the historian work";
17
18async function main() {
19 const db = new Database(DB_PATH);
20 db.exec("PRAGMA journal_mode=WAL");
21
22 console.log(`Query: "${query}"\n`);
23
24 // Load model
25 await ensureEmbeddingModel();
26
27 // Embed query
28 const queryEmbedding = await embedText(query);
29 if (!queryEmbedding) {
30 console.error("Failed to embed query");
31 process.exit(1);
32 }
33
34 // Load all embeddings
35 // Use current repo's root commit hash as project identity
36 const proc = Bun.spawnSync(["git", "rev-list", "--max-parents=0", "HEAD"], { stdout: "pipe" });
37 const rootHash = new TextDecoder().decode(proc.stdout).trim().split("\n")[0] ?? "";
38 const projectPath = rootHash ? `git:${rootHash}` : "";
39 console.log(`Project: ${projectPath}`);
40
41 const allEmbeddings = loadAllEmbeddings(db, projectPath, getEmbeddingModelId());
42 console.log(`Loaded ${allEmbeddings.size} embeddings\n`);
43
44 // Compute similarities
45 const scored = Array.from(allEmbeddings.entries()).map(([memoryId, embedding]) => ({
46 memoryId,
47 similarity: cosineSimilarity(queryEmbedding, embedding.embedding),
48 }));
49
50 scored.sort((a, b) => b.similarity - a.similarity);
51
52 // Show top 10
53 console.log("Top 10 results:");
54 for (const result of scored.slice(0, 10)) {
55 const memory = db
56 .prepare("SELECT content, category FROM memories WHERE id = ?")
57 .get(result.memoryId) as { content: string; category: string };
58 console.log(` [${result.similarity.toFixed(4)}] (${memory.category}) ${memory.content.slice(0, 120)}...`);
59 }
60}
61
62main().catch(console.error);

Callers 1

Calls 8

ensureEmbeddingModelFunction · 0.90
embedTextFunction · 0.90
loadAllEmbeddingsFunction · 0.90
getEmbeddingModelIdFunction · 0.90
cosineSimilarityFunction · 0.85
errorMethod · 0.80
getMethod · 0.80
prepareMethod · 0.45

Tested by

no test coverage detected