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

Function run

packages/plugin/scripts/embedding-baseline.ts:252–401  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

250}
251
252async function run() {
253 const args = parseArgs();
254
255 // 1. Load embedding config from user's magic-context.jsonc so we match
256 // whatever provider is currently active for the plugin.
257 // Run the raw input through the same Zod schema the plugin uses so we
258 // get the canonical discriminated-union shape initializeEmbedding expects.
259 const rawConfig = await loadEmbeddingConfig(args.configPath);
260 const parsed = EmbeddingConfigSchema.safeParse(rawConfig);
261 if (!parsed.success) {
262 console.error(
263 `[baseline] embedding config failed validation: ${parsed.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ")}`,
264 );
265 process.exit(1);
266 }
267 const embeddingConfig = parsed.data;
268 console.log("[baseline] embedding config:", embeddingConfig);
269 initializeEmbedding(embeddingConfig);
270
271 const modelReady = await ensureEmbeddingModel();
272 if (!modelReady) {
273 console.error("[baseline] embedding provider failed to initialize. Aborting.");
274 process.exit(1);
275 }
276
277 const modelId = getEmbeddingModelId();
278 console.log(`[baseline] model_id: ${modelId}`);
279
280 // 2. Open the plugin's live context DB (read-only) and load memories + embeddings
281 // for the current project.
282 const dbPath = join(homedir(), ".local/share/opencode/storage/plugin/magic-context/context.db");
283 if (!existsSync(dbPath)) {
284 console.error(`[baseline] plugin DB not found at ${dbPath}`);
285 process.exit(1);
286 }
287 const db = new Database(dbPath, { readonly: true });
288
289 const projectIdentity = resolveProjectIdentity(process.cwd());
290 console.log(`[baseline] project_identity: ${projectIdentity}`);
291
292 const memories = getMemoriesByProject(db, projectIdentity, ["active", "permanent"]);
293 const embeddings = loadAllEmbeddings(db, projectIdentity, modelId);
294 console.log(
295 `[baseline] memories_considered=${memories.length} embeddings_loaded=${embeddings.size}`,
296 );
297
298 if (embeddings.size === 0) {
299 console.error(
300 "[baseline] no embeddings in DB for current project. Did the background embed sweep run yet?",
301 );
302 process.exit(1);
303 }
304
305 const memoriesById = new Map(memories.map((m) => [m.id, m]));
306
307 // 3. Embed queries and score.
308 const queryLatencies: number[] = [];
309 let embeddingDim: number | null = null;

Callers 2

prepareBaselineFunction · 0.70
withRetryFunction · 0.50

Calls 14

initializeEmbeddingFunction · 0.90
ensureEmbeddingModelFunction · 0.90
getEmbeddingModelIdFunction · 0.90
resolveProjectIdentityFunction · 0.90
getMemoriesByProjectFunction · 0.90
loadAllEmbeddingsFunction · 0.90
embedTextFunction · 0.90
cosineSimilarityFunction · 0.90
loadEmbeddingConfigFunction · 0.85
errorMethod · 0.80
getMethod · 0.80
parseArgsFunction · 0.70

Tested by

no test coverage detected