@teammates/recall 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +156 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +249 -0
- package/dist/embeddings.d.ts +12 -0
- package/dist/embeddings.js +35 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/indexer.d.ts +54 -0
- package/dist/indexer.js +172 -0
- package/dist/search.d.ts +26 -0
- package/dist/search.js +75 -0
- package/package.json +34 -0
- package/src/cli.ts +275 -0
- package/src/embeddings.ts +43 -0
- package/src/index.ts +3 -0
- package/src/indexer.ts +203 -0
- package/src/search.ts +107 -0
- package/tsconfig.json +18 -0
package/dist/search.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface SearchOptions {
|
|
2
|
+
/** Path to the .teammates directory */
|
|
3
|
+
teammatesDir: string;
|
|
4
|
+
/** Teammate name to search (searches all if omitted) */
|
|
5
|
+
teammate?: string;
|
|
6
|
+
/** Max results per teammate (default: 5) */
|
|
7
|
+
maxResults?: number;
|
|
8
|
+
/** Max chunks per document (default: 3) */
|
|
9
|
+
maxChunks?: number;
|
|
10
|
+
/** Max tokens per section (default: 500) */
|
|
11
|
+
maxTokens?: number;
|
|
12
|
+
/** Embedding model name */
|
|
13
|
+
model?: string;
|
|
14
|
+
/** Skip auto-sync before searching (default: false) */
|
|
15
|
+
skipSync?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface SearchResult {
|
|
18
|
+
teammate: string;
|
|
19
|
+
uri: string;
|
|
20
|
+
text: string;
|
|
21
|
+
score: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Search teammate memories using semantic + keyword search.
|
|
25
|
+
*/
|
|
26
|
+
export declare function search(query: string, options: SearchOptions): Promise<SearchResult[]>;
|
package/dist/search.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { LocalDocumentIndex } from "vectra";
|
|
2
|
+
import { LocalEmbeddings } from "./embeddings.js";
|
|
3
|
+
import { Indexer } from "./indexer.js";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import * as fs from "node:fs/promises";
|
|
6
|
+
/**
|
|
7
|
+
* Search teammate memories using semantic + keyword search.
|
|
8
|
+
*/
|
|
9
|
+
export async function search(query, options) {
|
|
10
|
+
const indexRoot = path.join(options.teammatesDir, ".index");
|
|
11
|
+
const embeddings = new LocalEmbeddings(options.model);
|
|
12
|
+
const maxResults = options.maxResults ?? 5;
|
|
13
|
+
const maxChunks = options.maxChunks ?? 3;
|
|
14
|
+
const maxTokens = options.maxTokens ?? 500;
|
|
15
|
+
// Auto-sync: upsert any new/changed files before searching
|
|
16
|
+
if (!options.skipSync) {
|
|
17
|
+
const indexer = new Indexer({ teammatesDir: options.teammatesDir, model: options.model });
|
|
18
|
+
if (options.teammate) {
|
|
19
|
+
await indexer.syncTeammate(options.teammate);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
await indexer.syncAll();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// Determine which teammates to search
|
|
26
|
+
let teammates;
|
|
27
|
+
if (options.teammate) {
|
|
28
|
+
teammates = [options.teammate];
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
try {
|
|
32
|
+
const entries = await fs.readdir(indexRoot, { withFileTypes: true });
|
|
33
|
+
teammates = entries
|
|
34
|
+
.filter((e) => e.isDirectory())
|
|
35
|
+
.map((e) => e.name);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const allResults = [];
|
|
42
|
+
for (const teammate of teammates) {
|
|
43
|
+
const indexPath = path.join(indexRoot, teammate);
|
|
44
|
+
try {
|
|
45
|
+
await fs.access(indexPath);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
continue; // No index for this teammate
|
|
49
|
+
}
|
|
50
|
+
const index = new LocalDocumentIndex({
|
|
51
|
+
folderPath: indexPath,
|
|
52
|
+
embeddings,
|
|
53
|
+
});
|
|
54
|
+
if (!(await index.isIndexCreated()))
|
|
55
|
+
continue;
|
|
56
|
+
const docs = await index.queryDocuments(query, {
|
|
57
|
+
maxDocuments: maxResults,
|
|
58
|
+
maxChunks,
|
|
59
|
+
});
|
|
60
|
+
for (const doc of docs) {
|
|
61
|
+
const sections = await doc.renderSections(maxTokens, 1);
|
|
62
|
+
for (const section of sections) {
|
|
63
|
+
allResults.push({
|
|
64
|
+
teammate,
|
|
65
|
+
uri: doc.uri,
|
|
66
|
+
text: section.text,
|
|
67
|
+
score: section.score,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Sort by score descending, return top results
|
|
73
|
+
allResults.sort((a, b) => b.score - a.score);
|
|
74
|
+
return allResults.slice(0, maxResults);
|
|
75
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@teammates/recall",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Local semantic memory search for teammates. Indexes MEMORIES.md and daily logs using Vectra + transformers.js.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"teammates-recall": "dist/cli.js"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"dev": "tsc --watch"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"teammates",
|
|
17
|
+
"ai",
|
|
18
|
+
"memory",
|
|
19
|
+
"vector-search",
|
|
20
|
+
"embeddings"
|
|
21
|
+
],
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@huggingface/transformers": "^3.0.0",
|
|
25
|
+
"vectra": "^0.9.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^20.0.0",
|
|
29
|
+
"typescript": "^5.5.0"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=20.0.0"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import * as fs from "node:fs/promises";
|
|
5
|
+
import { Indexer } from "./indexer.js";
|
|
6
|
+
import { search } from "./search.js";
|
|
7
|
+
|
|
8
|
+
const HELP = `
|
|
9
|
+
teammates-recall — Semantic memory search for teammates
|
|
10
|
+
|
|
11
|
+
Usage:
|
|
12
|
+
teammates-recall index [options] Full rebuild of all indexes
|
|
13
|
+
teammates-recall sync [options] Sync new/changed files into indexes
|
|
14
|
+
teammates-recall add <file> [options] Add a single file to a teammate's index
|
|
15
|
+
teammates-recall search <query> [options] Search teammate memories (auto-syncs)
|
|
16
|
+
teammates-recall status [options] Show index status
|
|
17
|
+
|
|
18
|
+
Options:
|
|
19
|
+
--dir <path> Path to .teammates directory (default: ./.teammates)
|
|
20
|
+
--teammate <name> Limit to a specific teammate
|
|
21
|
+
--results <n> Max results (default: 5)
|
|
22
|
+
--model <name> Embedding model (default: Xenova/all-MiniLM-L6-v2)
|
|
23
|
+
--no-sync Skip auto-sync before search
|
|
24
|
+
--json Output as JSON
|
|
25
|
+
--help Show this help
|
|
26
|
+
`.trim();
|
|
27
|
+
|
|
28
|
+
interface Args {
|
|
29
|
+
command: string;
|
|
30
|
+
query: string;
|
|
31
|
+
file: string;
|
|
32
|
+
dir: string;
|
|
33
|
+
teammate?: string;
|
|
34
|
+
results: number;
|
|
35
|
+
model?: string;
|
|
36
|
+
json: boolean;
|
|
37
|
+
sync: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function parseArgs(argv: string[]): Args {
|
|
41
|
+
const args: Args = {
|
|
42
|
+
command: "",
|
|
43
|
+
query: "",
|
|
44
|
+
file: "",
|
|
45
|
+
dir: "./.teammates",
|
|
46
|
+
results: 5,
|
|
47
|
+
json: false,
|
|
48
|
+
sync: true,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
let i = 0;
|
|
52
|
+
// Skip node and script path
|
|
53
|
+
while (i < argv.length && (argv[i].includes("node") || argv[i].includes("teammates-recall") || argv[i].endsWith(".js"))) {
|
|
54
|
+
i++;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (i < argv.length && !argv[i].startsWith("-")) {
|
|
58
|
+
args.command = argv[i++];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// For search, next non-flag arg is the query; for add, it's the file path
|
|
62
|
+
if (args.command === "search" && i < argv.length && !argv[i].startsWith("-")) {
|
|
63
|
+
args.query = argv[i++];
|
|
64
|
+
} else if (args.command === "add" && i < argv.length && !argv[i].startsWith("-")) {
|
|
65
|
+
args.file = argv[i++];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
while (i < argv.length) {
|
|
69
|
+
const arg = argv[i++];
|
|
70
|
+
switch (arg) {
|
|
71
|
+
case "--dir":
|
|
72
|
+
args.dir = argv[i++];
|
|
73
|
+
break;
|
|
74
|
+
case "--teammate":
|
|
75
|
+
args.teammate = argv[i++];
|
|
76
|
+
break;
|
|
77
|
+
case "--results":
|
|
78
|
+
args.results = parseInt(argv[i++], 10);
|
|
79
|
+
break;
|
|
80
|
+
case "--model":
|
|
81
|
+
args.model = argv[i++];
|
|
82
|
+
break;
|
|
83
|
+
case "--no-sync":
|
|
84
|
+
args.sync = false;
|
|
85
|
+
break;
|
|
86
|
+
case "--json":
|
|
87
|
+
args.json = true;
|
|
88
|
+
break;
|
|
89
|
+
case "--help":
|
|
90
|
+
case "-h":
|
|
91
|
+
console.log(HELP);
|
|
92
|
+
process.exit(0);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return args;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function resolveTeammatesDir(dir: string): Promise<string> {
|
|
100
|
+
const resolved = path.resolve(dir);
|
|
101
|
+
try {
|
|
102
|
+
await fs.access(resolved);
|
|
103
|
+
return resolved;
|
|
104
|
+
} catch {
|
|
105
|
+
console.error(`Error: .teammates directory not found at ${resolved}`);
|
|
106
|
+
process.exit(1);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function cmdIndex(args: Args): Promise<void> {
|
|
111
|
+
const teammatesDir = await resolveTeammatesDir(args.dir);
|
|
112
|
+
const indexer = new Indexer({ teammatesDir, model: args.model });
|
|
113
|
+
|
|
114
|
+
if (args.teammate) {
|
|
115
|
+
console.error(`Indexing ${args.teammate}...`);
|
|
116
|
+
const count = await indexer.indexTeammate(args.teammate);
|
|
117
|
+
if (args.json) {
|
|
118
|
+
console.log(JSON.stringify({ teammate: args.teammate, files: count }));
|
|
119
|
+
} else {
|
|
120
|
+
console.log(`Indexed ${count} files for ${args.teammate}`);
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
console.error("Indexing all teammates...");
|
|
124
|
+
const results = await indexer.indexAll();
|
|
125
|
+
if (args.json) {
|
|
126
|
+
const obj = Object.fromEntries(results);
|
|
127
|
+
console.log(JSON.stringify(obj));
|
|
128
|
+
} else {
|
|
129
|
+
for (const [teammate, count] of results) {
|
|
130
|
+
console.log(` ${teammate}: ${count} files`);
|
|
131
|
+
}
|
|
132
|
+
console.log(`Done.`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function cmdSync(args: Args): Promise<void> {
|
|
138
|
+
const teammatesDir = await resolveTeammatesDir(args.dir);
|
|
139
|
+
const indexer = new Indexer({ teammatesDir, model: args.model });
|
|
140
|
+
|
|
141
|
+
if (args.teammate) {
|
|
142
|
+
console.error(`Syncing ${args.teammate}...`);
|
|
143
|
+
const count = await indexer.syncTeammate(args.teammate);
|
|
144
|
+
if (args.json) {
|
|
145
|
+
console.log(JSON.stringify({ teammate: args.teammate, files: count }));
|
|
146
|
+
} else {
|
|
147
|
+
console.log(`Synced ${count} files for ${args.teammate}`);
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
console.error("Syncing all teammates...");
|
|
151
|
+
const results = await indexer.syncAll();
|
|
152
|
+
if (args.json) {
|
|
153
|
+
const obj = Object.fromEntries(results);
|
|
154
|
+
console.log(JSON.stringify(obj));
|
|
155
|
+
} else {
|
|
156
|
+
for (const [teammate, count] of results) {
|
|
157
|
+
console.log(` ${teammate}: ${count} files`);
|
|
158
|
+
}
|
|
159
|
+
console.log(`Done.`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function cmdAdd(args: Args): Promise<void> {
|
|
165
|
+
if (!args.file) {
|
|
166
|
+
console.error("Error: add requires a file path argument");
|
|
167
|
+
console.error("Usage: teammates-recall add <file> --teammate <name>");
|
|
168
|
+
process.exit(1);
|
|
169
|
+
}
|
|
170
|
+
if (!args.teammate) {
|
|
171
|
+
console.error("Error: add requires --teammate <name>");
|
|
172
|
+
process.exit(1);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const teammatesDir = await resolveTeammatesDir(args.dir);
|
|
176
|
+
const indexer = new Indexer({ teammatesDir, model: args.model });
|
|
177
|
+
await indexer.upsertFile(args.teammate, args.file);
|
|
178
|
+
|
|
179
|
+
if (args.json) {
|
|
180
|
+
console.log(JSON.stringify({ teammate: args.teammate, file: args.file, status: "ok" }));
|
|
181
|
+
} else {
|
|
182
|
+
console.log(`Added ${args.file} to ${args.teammate}'s index`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function cmdSearch(args: Args): Promise<void> {
|
|
187
|
+
if (!args.query) {
|
|
188
|
+
console.error("Error: search requires a query argument");
|
|
189
|
+
console.error("Usage: teammates-recall search <query> [options]");
|
|
190
|
+
process.exit(1);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const teammatesDir = await resolveTeammatesDir(args.dir);
|
|
194
|
+
const results = await search(args.query, {
|
|
195
|
+
teammatesDir,
|
|
196
|
+
teammate: args.teammate,
|
|
197
|
+
maxResults: args.results,
|
|
198
|
+
model: args.model,
|
|
199
|
+
skipSync: !args.sync,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
if (args.json) {
|
|
203
|
+
console.log(JSON.stringify(results, null, 2));
|
|
204
|
+
} else {
|
|
205
|
+
if (results.length === 0) {
|
|
206
|
+
console.log("No results found.");
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
for (const result of results) {
|
|
210
|
+
console.log(`--- ${result.teammate} | ${result.uri} (score: ${result.score.toFixed(3)}) ---`);
|
|
211
|
+
console.log(result.text);
|
|
212
|
+
console.log();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function cmdStatus(args: Args): Promise<void> {
|
|
218
|
+
const teammatesDir = await resolveTeammatesDir(args.dir);
|
|
219
|
+
const indexer = new Indexer({ teammatesDir, model: args.model });
|
|
220
|
+
const teammates = await indexer.discoverTeammates();
|
|
221
|
+
|
|
222
|
+
const status: Record<string, { memoryFiles: number; indexed: boolean }> = {};
|
|
223
|
+
|
|
224
|
+
for (const teammate of teammates) {
|
|
225
|
+
const { files } = await indexer.collectFiles(teammate);
|
|
226
|
+
const indexPath = path.join(indexer.indexRoot, teammate);
|
|
227
|
+
let indexed = false;
|
|
228
|
+
try {
|
|
229
|
+
await fs.access(indexPath);
|
|
230
|
+
indexed = true;
|
|
231
|
+
} catch {
|
|
232
|
+
// Not indexed
|
|
233
|
+
}
|
|
234
|
+
status[teammate] = { memoryFiles: files.length, indexed };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (args.json) {
|
|
238
|
+
console.log(JSON.stringify(status, null, 2));
|
|
239
|
+
} else {
|
|
240
|
+
for (const [teammate, info] of Object.entries(status)) {
|
|
241
|
+
const tag = info.indexed ? "indexed" : "not indexed";
|
|
242
|
+
console.log(` ${teammate}: ${info.memoryFiles} memory files (${tag})`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function main(): Promise<void> {
|
|
248
|
+
const args = parseArgs(process.argv);
|
|
249
|
+
|
|
250
|
+
switch (args.command) {
|
|
251
|
+
case "index":
|
|
252
|
+
await cmdIndex(args);
|
|
253
|
+
break;
|
|
254
|
+
case "sync":
|
|
255
|
+
await cmdSync(args);
|
|
256
|
+
break;
|
|
257
|
+
case "add":
|
|
258
|
+
await cmdAdd(args);
|
|
259
|
+
break;
|
|
260
|
+
case "search":
|
|
261
|
+
await cmdSearch(args);
|
|
262
|
+
break;
|
|
263
|
+
case "status":
|
|
264
|
+
await cmdStatus(args);
|
|
265
|
+
break;
|
|
266
|
+
default:
|
|
267
|
+
console.log(HELP);
|
|
268
|
+
process.exit(args.command ? 1 : 0);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
main().catch((err) => {
|
|
273
|
+
console.error(err.message);
|
|
274
|
+
process.exit(1);
|
|
275
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { EmbeddingsModel, EmbeddingsResponse } from "vectra";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_MODEL = "Xenova/all-MiniLM-L6-v2";
|
|
4
|
+
const MAX_TOKENS = 256;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Local embeddings using transformers.js. No API keys, no network after first model download.
|
|
8
|
+
*/
|
|
9
|
+
export class LocalEmbeddings implements EmbeddingsModel {
|
|
10
|
+
readonly maxTokens = MAX_TOKENS;
|
|
11
|
+
|
|
12
|
+
private _model: string;
|
|
13
|
+
private _extractor: any | null = null;
|
|
14
|
+
|
|
15
|
+
constructor(model?: string) {
|
|
16
|
+
this._model = model ?? DEFAULT_MODEL;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async createEmbeddings(
|
|
20
|
+
inputs: string | string[]
|
|
21
|
+
): Promise<EmbeddingsResponse> {
|
|
22
|
+
try {
|
|
23
|
+
const extractor = await this._getExtractor();
|
|
24
|
+
const texts = Array.isArray(inputs) ? inputs : [inputs];
|
|
25
|
+
const output = await extractor(texts, {
|
|
26
|
+
pooling: "mean",
|
|
27
|
+
normalize: true,
|
|
28
|
+
});
|
|
29
|
+
const embeddings: number[][] = output.tolist();
|
|
30
|
+
return { status: "success", output: embeddings };
|
|
31
|
+
} catch (err: any) {
|
|
32
|
+
return { status: "error", message: err.message };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private async _getExtractor(): Promise<any> {
|
|
37
|
+
if (!this._extractor) {
|
|
38
|
+
const { pipeline } = await import("@huggingface/transformers");
|
|
39
|
+
this._extractor = await pipeline("feature-extraction", this._model);
|
|
40
|
+
}
|
|
41
|
+
return this._extractor;
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/index.ts
ADDED
package/src/indexer.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { LocalDocumentIndex } from "vectra";
|
|
2
|
+
import { LocalEmbeddings } from "./embeddings.js";
|
|
3
|
+
import * as fs from "node:fs/promises";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
|
|
6
|
+
export interface IndexerConfig {
|
|
7
|
+
/** Path to the .teammates directory */
|
|
8
|
+
teammatesDir: string;
|
|
9
|
+
/** Embedding model name (default: Xenova/all-MiniLM-L6-v2) */
|
|
10
|
+
model?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface TeammateFiles {
|
|
14
|
+
teammate: string;
|
|
15
|
+
files: { uri: string; absolutePath: string }[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Indexes teammate memory files (MEMORIES.md + memory/*.md) into Vectra.
|
|
20
|
+
* One index per teammate, stored at .teammates/.index/<name>/
|
|
21
|
+
*/
|
|
22
|
+
export class Indexer {
|
|
23
|
+
private _config: IndexerConfig;
|
|
24
|
+
private _embeddings: LocalEmbeddings;
|
|
25
|
+
|
|
26
|
+
constructor(config: IndexerConfig) {
|
|
27
|
+
this._config = config;
|
|
28
|
+
this._embeddings = new LocalEmbeddings(config.model);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get indexRoot(): string {
|
|
32
|
+
return path.join(this._config.teammatesDir, ".index");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Discover all teammate directories (folders containing SOUL.md).
|
|
37
|
+
*/
|
|
38
|
+
async discoverTeammates(): Promise<string[]> {
|
|
39
|
+
const entries = await fs.readdir(this._config.teammatesDir, {
|
|
40
|
+
withFileTypes: true,
|
|
41
|
+
});
|
|
42
|
+
const teammates: string[] = [];
|
|
43
|
+
for (const entry of entries) {
|
|
44
|
+
if (!entry.isDirectory() || entry.name.startsWith(".")) continue;
|
|
45
|
+
const soulPath = path.join(
|
|
46
|
+
this._config.teammatesDir,
|
|
47
|
+
entry.name,
|
|
48
|
+
"SOUL.md"
|
|
49
|
+
);
|
|
50
|
+
try {
|
|
51
|
+
await fs.access(soulPath);
|
|
52
|
+
teammates.push(entry.name);
|
|
53
|
+
} catch {
|
|
54
|
+
// Not a teammate folder
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return teammates;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Collect all indexable memory files for a teammate.
|
|
62
|
+
*/
|
|
63
|
+
async collectFiles(teammate: string): Promise<TeammateFiles> {
|
|
64
|
+
const teammateDir = path.join(this._config.teammatesDir, teammate);
|
|
65
|
+
const files: TeammateFiles["files"] = [];
|
|
66
|
+
|
|
67
|
+
// MEMORIES.md
|
|
68
|
+
const memoriesPath = path.join(teammateDir, "MEMORIES.md");
|
|
69
|
+
try {
|
|
70
|
+
await fs.access(memoriesPath);
|
|
71
|
+
files.push({ uri: `${teammate}/MEMORIES.md`, absolutePath: memoriesPath });
|
|
72
|
+
} catch {
|
|
73
|
+
// No MEMORIES.md
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// memory/*.md (daily logs)
|
|
77
|
+
const memoryDir = path.join(teammateDir, "memory");
|
|
78
|
+
try {
|
|
79
|
+
const memoryEntries = await fs.readdir(memoryDir);
|
|
80
|
+
for (const entry of memoryEntries) {
|
|
81
|
+
if (!entry.endsWith(".md")) continue;
|
|
82
|
+
files.push({
|
|
83
|
+
uri: `${teammate}/memory/${entry}`,
|
|
84
|
+
absolutePath: path.join(memoryDir, entry),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
} catch {
|
|
88
|
+
// No memory/ directory
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { teammate, files };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Build or rebuild the index for a single teammate.
|
|
96
|
+
*/
|
|
97
|
+
async indexTeammate(teammate: string): Promise<number> {
|
|
98
|
+
const { files } = await this.collectFiles(teammate);
|
|
99
|
+
if (files.length === 0) return 0;
|
|
100
|
+
|
|
101
|
+
const indexPath = path.join(this.indexRoot, teammate);
|
|
102
|
+
const index = new LocalDocumentIndex({
|
|
103
|
+
folderPath: indexPath,
|
|
104
|
+
embeddings: this._embeddings,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Recreate index from scratch
|
|
108
|
+
await index.createIndex({ version: 1, deleteIfExists: true });
|
|
109
|
+
|
|
110
|
+
let count = 0;
|
|
111
|
+
for (const file of files) {
|
|
112
|
+
const text = await fs.readFile(file.absolutePath, "utf-8");
|
|
113
|
+
if (text.trim().length === 0) continue;
|
|
114
|
+
await index.upsertDocument(file.uri, text, "md");
|
|
115
|
+
count++;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return count;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Build or rebuild indexes for all teammates.
|
|
123
|
+
*/
|
|
124
|
+
async indexAll(): Promise<Map<string, number>> {
|
|
125
|
+
const teammates = await this.discoverTeammates();
|
|
126
|
+
const results = new Map<string, number>();
|
|
127
|
+
for (const teammate of teammates) {
|
|
128
|
+
const count = await this.indexTeammate(teammate);
|
|
129
|
+
results.set(teammate, count);
|
|
130
|
+
}
|
|
131
|
+
return results;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Upsert a single file into an existing teammate index.
|
|
136
|
+
* Creates the index if it doesn't exist yet.
|
|
137
|
+
*/
|
|
138
|
+
async upsertFile(teammate: string, filePath: string): Promise<void> {
|
|
139
|
+
const teammateDir = path.join(this._config.teammatesDir, teammate);
|
|
140
|
+
const absolutePath = path.resolve(filePath);
|
|
141
|
+
const relativePath = path.relative(teammateDir, absolutePath);
|
|
142
|
+
const uri = `${teammate}/${relativePath.replace(/\\/g, "/")}`;
|
|
143
|
+
|
|
144
|
+
const text = await fs.readFile(absolutePath, "utf-8");
|
|
145
|
+
if (text.trim().length === 0) return;
|
|
146
|
+
|
|
147
|
+
const indexPath = path.join(this.indexRoot, teammate);
|
|
148
|
+
const index = new LocalDocumentIndex({
|
|
149
|
+
folderPath: indexPath,
|
|
150
|
+
embeddings: this._embeddings,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
if (!(await index.isIndexCreated())) {
|
|
154
|
+
await index.createIndex({ version: 1 });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
await index.upsertDocument(uri, text, "md");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Sync a teammate's index with their current memory files.
|
|
162
|
+
* Upserts new/changed files without a full rebuild.
|
|
163
|
+
*/
|
|
164
|
+
async syncTeammate(teammate: string): Promise<number> {
|
|
165
|
+
const { files } = await this.collectFiles(teammate);
|
|
166
|
+
if (files.length === 0) return 0;
|
|
167
|
+
|
|
168
|
+
const indexPath = path.join(this.indexRoot, teammate);
|
|
169
|
+
const index = new LocalDocumentIndex({
|
|
170
|
+
folderPath: indexPath,
|
|
171
|
+
embeddings: this._embeddings,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
if (!(await index.isIndexCreated())) {
|
|
175
|
+
// No index yet — do a full build
|
|
176
|
+
return this.indexTeammate(teammate);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Upsert all files (Vectra handles dedup internally via URI)
|
|
180
|
+
let count = 0;
|
|
181
|
+
for (const file of files) {
|
|
182
|
+
const text = await fs.readFile(file.absolutePath, "utf-8");
|
|
183
|
+
if (text.trim().length === 0) continue;
|
|
184
|
+
await index.upsertDocument(file.uri, text, "md");
|
|
185
|
+
count++;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return count;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Sync indexes for all teammates.
|
|
193
|
+
*/
|
|
194
|
+
async syncAll(): Promise<Map<string, number>> {
|
|
195
|
+
const teammates = await this.discoverTeammates();
|
|
196
|
+
const results = new Map<string, number>();
|
|
197
|
+
for (const teammate of teammates) {
|
|
198
|
+
const count = await this.syncTeammate(teammate);
|
|
199
|
+
results.set(teammate, count);
|
|
200
|
+
}
|
|
201
|
+
return results;
|
|
202
|
+
}
|
|
203
|
+
}
|