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

Function main

packages/plugin/scripts/backfill-embeddings.ts:29–134  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

27}
28
29async function main() {
30 const directory = getArg("--directory") ?? process.cwd();
31 const projectFilter = getArg("--project");
32 const forceProjectPath = process.argv.includes("--force-project-path");
33 const projectIdentity = resolveProjectIdentity(directory);
34
35 if (projectFilter && projectFilter !== projectIdentity && !forceProjectPath) {
36 console.error(
37 `--project ${projectFilter} does not match identity for --directory ${directory}: ${projectIdentity}. ` +
38 "Pass --force-project-path to override.",
39 );
40 process.exit(1);
41 }
42
43 const db = new Database(DB_PATH);
44 db.exec("PRAGMA journal_mode=WAL");
45 const config = loadPluginConfig(directory);
46 registerProjectEmbedding(
47 db,
48 projectIdentity,
49 config.embedding,
50 {
51 memoryEnabled: config.memory.enabled,
52 gitCommitEnabled: config.memory.git_commit_indexing.enabled,
53 },
54 directory,
55 );
56
57 const snapshot = getProjectEmbeddingSnapshot(projectIdentity);
58 if (!snapshot?.enabled) {
59 console.error("Embedding is disabled for this project.");
60 db.close();
61 process.exit(1);
62 }
63
64 // Find memories without embeddings for the current model (optionally filtered to one project).
65 const effectiveProject = projectFilter ?? projectIdentity;
66 const query = effectiveProject
67 ? `SELECT m.id, m.content, m.category, m.project_path
68 FROM memories m
69 LEFT JOIN memory_embeddings me ON me.memory_id = m.id AND me.model_id = ?
70 WHERE m.status != 'deleted' AND me.memory_id IS NULL AND m.project_path = ?`
71 : `SELECT m.id, m.content, m.category, m.project_path
72 FROM memories m
73 LEFT JOIN memory_embeddings me ON me.memory_id = m.id AND me.model_id = ?
74 WHERE m.status != 'deleted' AND me.memory_id IS NULL`;
75 const stmt = db.prepare(query);
76 const allMemories = (
77 effectiveProject ? stmt.all(snapshot.modelId, effectiveProject) : stmt.all(snapshot.modelId)
78 ) as Array<{ id: number; content: string; category: string; project_path: string }>;
79
80 console.log(
81 `Found ${allMemories.length} memories without embeddings${effectiveProject ? ` in project ${effectiveProject}` : ""}`,
82 );
83
84 if (allMemories.length === 0) {
85 console.log("Nothing to do.");
86 db.close();

Callers 1

Calls 12

resolveProjectIdentityFunction · 0.90
loadPluginConfigFunction · 0.90
registerProjectEmbeddingFunction · 0.90
embedBatchForProjectFunction · 0.90
saveEmbeddingFunction · 0.90
getArgFunction · 0.85
errorMethod · 0.80
getMethod · 0.80
warnMethod · 0.65
closeMethod · 0.45
prepareMethod · 0.45

Tested by

no test coverage detected