@teammates/recall 0.1.0 → 0.1.1

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/src/indexer.ts CHANGED
@@ -1,203 +1,204 @@
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
- }
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/<name>/.index/
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 the index path for a specific teammate */
32
+ indexPath(teammate: string): string {
33
+ return path.join(this._config.teammatesDir, teammate, ".index");
34
+ }
35
+
36
+ /**
37
+ * Discover all teammate directories (folders containing SOUL.md).
38
+ */
39
+ async discoverTeammates(): Promise<string[]> {
40
+ const entries = await fs.readdir(this._config.teammatesDir, {
41
+ withFileTypes: true,
42
+ });
43
+ const teammates: string[] = [];
44
+ for (const entry of entries) {
45
+ if (!entry.isDirectory() || entry.name.startsWith(".")) continue;
46
+ const soulPath = path.join(
47
+ this._config.teammatesDir,
48
+ entry.name,
49
+ "SOUL.md"
50
+ );
51
+ try {
52
+ await fs.access(soulPath);
53
+ teammates.push(entry.name);
54
+ } catch {
55
+ // Not a teammate folder
56
+ }
57
+ }
58
+ return teammates;
59
+ }
60
+
61
+ /**
62
+ * Collect all indexable memory files for a teammate.
63
+ */
64
+ async collectFiles(teammate: string): Promise<TeammateFiles> {
65
+ const teammateDir = path.join(this._config.teammatesDir, teammate);
66
+ const files: TeammateFiles["files"] = [];
67
+
68
+ // MEMORIES.md
69
+ const memoriesPath = path.join(teammateDir, "MEMORIES.md");
70
+ try {
71
+ await fs.access(memoriesPath);
72
+ files.push({ uri: `${teammate}/MEMORIES.md`, absolutePath: memoriesPath });
73
+ } catch {
74
+ // No MEMORIES.md
75
+ }
76
+
77
+ // memory/*.md (daily logs)
78
+ const memoryDir = path.join(teammateDir, "memory");
79
+ try {
80
+ const memoryEntries = await fs.readdir(memoryDir);
81
+ for (const entry of memoryEntries) {
82
+ if (!entry.endsWith(".md")) continue;
83
+ files.push({
84
+ uri: `${teammate}/memory/${entry}`,
85
+ absolutePath: path.join(memoryDir, entry),
86
+ });
87
+ }
88
+ } catch {
89
+ // No memory/ directory
90
+ }
91
+
92
+ return { teammate, files };
93
+ }
94
+
95
+ /**
96
+ * Build or rebuild the index for a single teammate.
97
+ */
98
+ async indexTeammate(teammate: string): Promise<number> {
99
+ const { files } = await this.collectFiles(teammate);
100
+ if (files.length === 0) return 0;
101
+
102
+ const indexPath = this.indexPath(teammate);
103
+ const index = new LocalDocumentIndex({
104
+ folderPath: indexPath,
105
+ embeddings: this._embeddings,
106
+ });
107
+
108
+ // Recreate index from scratch
109
+ await index.createIndex({ version: 1, deleteIfExists: true });
110
+
111
+ let count = 0;
112
+ for (const file of files) {
113
+ const text = await fs.readFile(file.absolutePath, "utf-8");
114
+ if (text.trim().length === 0) continue;
115
+ await index.upsertDocument(file.uri, text, "md");
116
+ count++;
117
+ }
118
+
119
+ return count;
120
+ }
121
+
122
+ /**
123
+ * Build or rebuild indexes for all teammates.
124
+ */
125
+ async indexAll(): Promise<Map<string, number>> {
126
+ const teammates = await this.discoverTeammates();
127
+ const results = new Map<string, number>();
128
+ for (const teammate of teammates) {
129
+ const count = await this.indexTeammate(teammate);
130
+ results.set(teammate, count);
131
+ }
132
+ return results;
133
+ }
134
+
135
+ /**
136
+ * Upsert a single file into an existing teammate index.
137
+ * Creates the index if it doesn't exist yet.
138
+ */
139
+ async upsertFile(teammate: string, filePath: string): Promise<void> {
140
+ const teammateDir = path.join(this._config.teammatesDir, teammate);
141
+ const absolutePath = path.resolve(filePath);
142
+ const relativePath = path.relative(teammateDir, absolutePath);
143
+ const uri = `${teammate}/${relativePath.replace(/\\/g, "/")}`;
144
+
145
+ const text = await fs.readFile(absolutePath, "utf-8");
146
+ if (text.trim().length === 0) return;
147
+
148
+ const indexPath = this.indexPath(teammate);
149
+ const index = new LocalDocumentIndex({
150
+ folderPath: indexPath,
151
+ embeddings: this._embeddings,
152
+ });
153
+
154
+ if (!(await index.isIndexCreated())) {
155
+ await index.createIndex({ version: 1 });
156
+ }
157
+
158
+ await index.upsertDocument(uri, text, "md");
159
+ }
160
+
161
+ /**
162
+ * Sync a teammate's index with their current memory files.
163
+ * Upserts new/changed files without a full rebuild.
164
+ */
165
+ async syncTeammate(teammate: string): Promise<number> {
166
+ const { files } = await this.collectFiles(teammate);
167
+ if (files.length === 0) return 0;
168
+
169
+ const indexPath = this.indexPath(teammate);
170
+ const index = new LocalDocumentIndex({
171
+ folderPath: indexPath,
172
+ embeddings: this._embeddings,
173
+ });
174
+
175
+ if (!(await index.isIndexCreated())) {
176
+ // No index yet — do a full build
177
+ return this.indexTeammate(teammate);
178
+ }
179
+
180
+ // Upsert all files (Vectra handles dedup internally via URI)
181
+ let count = 0;
182
+ for (const file of files) {
183
+ const text = await fs.readFile(file.absolutePath, "utf-8");
184
+ if (text.trim().length === 0) continue;
185
+ await index.upsertDocument(file.uri, text, "md");
186
+ count++;
187
+ }
188
+
189
+ return count;
190
+ }
191
+
192
+ /**
193
+ * Sync indexes for all teammates.
194
+ */
195
+ async syncAll(): Promise<Map<string, number>> {
196
+ const teammates = await this.discoverTeammates();
197
+ const results = new Map<string, number>();
198
+ for (const teammate of teammates) {
199
+ const count = await this.syncTeammate(teammate);
200
+ results.set(teammate, count);
201
+ }
202
+ return results;
203
+ }
204
+ }
package/src/search.ts CHANGED
@@ -1,107 +1,99 @@
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
- export interface SearchOptions {
8
- /** Path to the .teammates directory */
9
- teammatesDir: string;
10
- /** Teammate name to search (searches all if omitted) */
11
- teammate?: string;
12
- /** Max results per teammate (default: 5) */
13
- maxResults?: number;
14
- /** Max chunks per document (default: 3) */
15
- maxChunks?: number;
16
- /** Max tokens per section (default: 500) */
17
- maxTokens?: number;
18
- /** Embedding model name */
19
- model?: string;
20
- /** Skip auto-sync before searching (default: false) */
21
- skipSync?: boolean;
22
- }
23
-
24
- export interface SearchResult {
25
- teammate: string;
26
- uri: string;
27
- text: string;
28
- score: number;
29
- }
30
-
31
- /**
32
- * Search teammate memories using semantic + keyword search.
33
- */
34
- export async function search(
35
- query: string,
36
- options: SearchOptions
37
- ): Promise<SearchResult[]> {
38
- const indexRoot = path.join(options.teammatesDir, ".index");
39
- const embeddings = new LocalEmbeddings(options.model);
40
- const maxResults = options.maxResults ?? 5;
41
- const maxChunks = options.maxChunks ?? 3;
42
- const maxTokens = options.maxTokens ?? 500;
43
-
44
- // Auto-sync: upsert any new/changed files before searching
45
- if (!options.skipSync) {
46
- const indexer = new Indexer({ teammatesDir: options.teammatesDir, model: options.model });
47
- if (options.teammate) {
48
- await indexer.syncTeammate(options.teammate);
49
- } else {
50
- await indexer.syncAll();
51
- }
52
- }
53
-
54
- // Determine which teammates to search
55
- let teammates: string[];
56
- if (options.teammate) {
57
- teammates = [options.teammate];
58
- } else {
59
- try {
60
- const entries = await fs.readdir(indexRoot, { withFileTypes: true });
61
- teammates = entries
62
- .filter((e) => e.isDirectory())
63
- .map((e) => e.name);
64
- } catch {
65
- return [];
66
- }
67
- }
68
-
69
- const allResults: SearchResult[] = [];
70
-
71
- for (const teammate of teammates) {
72
- const indexPath = path.join(indexRoot, teammate);
73
- try {
74
- await fs.access(indexPath);
75
- } catch {
76
- continue; // No index for this teammate
77
- }
78
-
79
- const index = new LocalDocumentIndex({
80
- folderPath: indexPath,
81
- embeddings,
82
- });
83
-
84
- if (!(await index.isIndexCreated())) continue;
85
-
86
- const docs = await index.queryDocuments(query, {
87
- maxDocuments: maxResults,
88
- maxChunks,
89
- });
90
-
91
- for (const doc of docs) {
92
- const sections = await doc.renderSections(maxTokens, 1);
93
- for (const section of sections) {
94
- allResults.push({
95
- teammate,
96
- uri: doc.uri,
97
- text: section.text,
98
- score: section.score,
99
- });
100
- }
101
- }
102
- }
103
-
104
- // Sort by score descending, return top results
105
- allResults.sort((a, b) => b.score - a.score);
106
- return allResults.slice(0, maxResults);
107
- }
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
+ export interface SearchOptions {
8
+ /** Path to the .teammates directory */
9
+ teammatesDir: string;
10
+ /** Teammate name to search (searches all if omitted) */
11
+ teammate?: string;
12
+ /** Max results per teammate (default: 5) */
13
+ maxResults?: number;
14
+ /** Max chunks per document (default: 3) */
15
+ maxChunks?: number;
16
+ /** Max tokens per section (default: 500) */
17
+ maxTokens?: number;
18
+ /** Embedding model name */
19
+ model?: string;
20
+ /** Skip auto-sync before searching (default: false) */
21
+ skipSync?: boolean;
22
+ }
23
+
24
+ export interface SearchResult {
25
+ teammate: string;
26
+ uri: string;
27
+ text: string;
28
+ score: number;
29
+ }
30
+
31
+ /**
32
+ * Search teammate memories using semantic + keyword search.
33
+ */
34
+ export async function search(
35
+ query: string,
36
+ options: SearchOptions
37
+ ): Promise<SearchResult[]> {
38
+ const embeddings = new LocalEmbeddings(options.model);
39
+ const indexer = new Indexer({ teammatesDir: options.teammatesDir, model: options.model });
40
+ const maxResults = options.maxResults ?? 5;
41
+ const maxChunks = options.maxChunks ?? 3;
42
+ const maxTokens = options.maxTokens ?? 500;
43
+
44
+ // Auto-sync: upsert any new/changed files before searching
45
+ if (!options.skipSync) {
46
+ if (options.teammate) {
47
+ await indexer.syncTeammate(options.teammate);
48
+ } else {
49
+ await indexer.syncAll();
50
+ }
51
+ }
52
+
53
+ // Determine which teammates to search
54
+ let teammates: string[];
55
+ if (options.teammate) {
56
+ teammates = [options.teammate];
57
+ } else {
58
+ teammates = await indexer.discoverTeammates();
59
+ }
60
+
61
+ const allResults: SearchResult[] = [];
62
+
63
+ for (const teammate of teammates) {
64
+ const indexPath = indexer.indexPath(teammate);
65
+ try {
66
+ await fs.access(indexPath);
67
+ } catch {
68
+ continue; // No index for this teammate
69
+ }
70
+
71
+ const index = new LocalDocumentIndex({
72
+ folderPath: indexPath,
73
+ embeddings,
74
+ });
75
+
76
+ if (!(await index.isIndexCreated())) continue;
77
+
78
+ const docs = await index.queryDocuments(query, {
79
+ maxDocuments: maxResults,
80
+ maxChunks,
81
+ });
82
+
83
+ for (const doc of docs) {
84
+ const sections = await doc.renderSections(maxTokens, 1);
85
+ for (const section of sections) {
86
+ allResults.push({
87
+ teammate,
88
+ uri: doc.uri,
89
+ text: section.text,
90
+ score: section.score,
91
+ });
92
+ }
93
+ }
94
+ }
95
+
96
+ // Sort by score descending, return top results
97
+ allResults.sort((a, b) => b.score - a.score);
98
+ return allResults.slice(0, maxResults);
99
+ }
package/tsconfig.json CHANGED
@@ -1,18 +1,18 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "Node16",
5
- "moduleResolution": "Node16",
6
- "lib": ["ES2022"],
7
- "outDir": "dist",
8
- "rootDir": "src",
9
- "strict": true,
10
- "declaration": true,
11
- "esModuleInterop": true,
12
- "skipLibCheck": true,
13
- "forceConsistentCasingInFileNames": true,
14
- "resolveJsonModule": true
15
- },
16
- "include": ["src"],
17
- "exclude": ["node_modules", "dist"]
18
- }
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "Node16",
5
+ "moduleResolution": "Node16",
6
+ "lib": ["ES2022"],
7
+ "outDir": "dist",
8
+ "rootDir": "src",
9
+ "strict": true,
10
+ "declaration": true,
11
+ "esModuleInterop": true,
12
+ "skipLibCheck": true,
13
+ "forceConsistentCasingInFileNames": true,
14
+ "resolveJsonModule": true
15
+ },
16
+ "include": ["src"],
17
+ "exclude": ["node_modules", "dist"]
18
+ }