This repository was archived by the owner on May 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebase-map.ts
More file actions
67 lines (59 loc) · 2.34 KB
/
Copy pathcodebase-map.ts
File metadata and controls
67 lines (59 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type { Database } from "bun:sqlite";
import type { FileEntry } from "../config/types.ts";
export class CodebaseMap {
constructor(private db: Database) {}
update(path: string, purpose: string, agent?: string): void {
this.db.prepare(
`INSERT INTO codebase_map (file_path, purpose, last_agent, updated_at)
VALUES (?, ?, ?, datetime('now'))
ON CONFLICT(file_path) DO UPDATE SET
purpose = excluded.purpose,
last_agent = excluded.last_agent,
updated_at = datetime('now')`,
).run(path, purpose, agent ?? null);
}
get(path: string): FileEntry | null {
const row = this.db.prepare(
`SELECT * FROM codebase_map WHERE file_path = ?`,
).get(path) as Record<string, unknown> | null;
if (!row) return null;
return this.mapEntry(row);
}
search(query: string, limit = 10): FileEntry[] {
const pattern = `%${query}%`;
const rows = this.db.prepare(
`SELECT * FROM codebase_map WHERE file_path LIKE ? OR purpose LIKE ? ORDER BY updated_at DESC LIMIT ?`,
).all(pattern, pattern, limit) as Record<string, unknown>[];
return rows.map((r) => this.mapEntry(r));
}
listByAgent(agentName: string): FileEntry[] {
const rows = this.db.prepare(
`SELECT * FROM codebase_map WHERE last_agent = ? ORDER BY updated_at DESC`,
).all(agentName) as Record<string, unknown>[];
return rows.map((r) => this.mapEntry(r));
}
buildContext(paths: string[]): string {
if (paths.length === 0) return "";
const entries = paths
.map((p) => this.get(p))
.filter((e): e is FileEntry => e != null);
return entries.map((e) => `${e.path}: ${e.purpose}`).join("\n");
}
getProjectOverview(limit = 50): string {
const rows = this.db.prepare(
`SELECT * FROM codebase_map ORDER BY updated_at DESC LIMIT ?`,
).all(limit) as Record<string, unknown>[];
const entries = rows.map((r) => this.mapEntry(r));
if (entries.length === 0) return "No codebase map entries yet.";
return `Project files (${entries.length}):\n` +
entries.map((e) => ` ${e.path} — ${e.purpose}`).join("\n");
}
private mapEntry(row: Record<string, unknown>): FileEntry {
return {
path: row.file_path as string,
purpose: row.purpose as string,
lastAgent: row.last_agent as string | undefined,
lastUpdated: row.updated_at as string,
};
}
}