scai 0.1.63 โ 0.1.65
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 +30 -2
- package/dist/db/fileIndex.js +11 -101
- package/dist/fileRules/scoreFiles.js +71 -0
- package/dist/index.js +5 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -269,6 +269,34 @@ These features are experimental and subject to change:
|
|
|
269
269
|
|
|
270
270
|
<br>
|
|
271
271
|
|
|
272
|
+
## Commands
|
|
273
|
+
|
|
274
|
+
### `index`
|
|
275
|
+
The `index` command is used to manage and perform operations on the indexed files and repositories.
|
|
276
|
+
|
|
277
|
+
#### `scai index start`
|
|
278
|
+
|
|
279
|
+
Index supported files in the configured index directory.
|
|
280
|
+
|
|
281
|
+
```bash
|
|
282
|
+
scai index set <dir>
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Set and activate the index directory.
|
|
286
|
+
```bash
|
|
287
|
+
scai index list
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
List all indexed repositories.
|
|
291
|
+
```bash
|
|
292
|
+
scai index switch
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
Switch active repository (by key or indexDir). Run without input for an interactive list of repositories.
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
272
300
|
> **Note:** Indexing very large repositories (millions of lines) may take **hours or days**. Please be patient, and only index huge codebases if you are ok with some extra processing taking place on your computer.
|
|
273
301
|
|
|
274
302
|
The `scai index` command **automatically** starts a background daemon that continuously:
|
|
@@ -322,7 +350,7 @@ You can run it in two ways:
|
|
|
322
350
|
1. **Inline question**
|
|
323
351
|
|
|
324
352
|
```bash
|
|
325
|
-
scai ask "How does
|
|
353
|
+
scai ask "How does the controller work?"
|
|
326
354
|
```
|
|
327
355
|
|
|
328
356
|
2. **Interactive prompt**
|
|
@@ -335,7 +363,7 @@ You can run it in two ways:
|
|
|
335
363
|
, then type your question when prompted:
|
|
336
364
|
|
|
337
365
|
```
|
|
338
|
-
> How does
|
|
366
|
+
> How does the controller work?
|
|
339
367
|
```
|
|
340
368
|
|
|
341
369
|
</br>
|
package/dist/db/fileIndex.js
CHANGED
|
@@ -3,77 +3,46 @@ import path from 'path';
|
|
|
3
3
|
import { generateEmbedding } from '../lib/generateEmbedding.js';
|
|
4
4
|
import { sanitizeQueryForFts } from '../utils/sanitizeQuery.js';
|
|
5
5
|
import * as sqlTemplates from './sqlTemplates.js';
|
|
6
|
-
import { stringSimilarity } from 'string-similarity-js';
|
|
7
6
|
import { CANDIDATE_LIMIT } from '../constants.js';
|
|
8
7
|
import { getDbForRepo } from './client.js';
|
|
9
|
-
|
|
10
|
-
* ๐ Index a single file into the database.
|
|
11
|
-
*
|
|
12
|
-
* - Normalizes the file path to prevent OS-specific path bugs.
|
|
13
|
-
* - Stores metadata like summary, type, lastModified, etc.
|
|
14
|
-
* - Sets `embedding` to null initially โ computed later.
|
|
15
|
-
*/
|
|
8
|
+
import { scoreFiles } from '../fileRules/scoreFiles.js'; // ๐ NEW
|
|
16
9
|
export function indexFile(filePath, summary, type) {
|
|
17
10
|
const stats = fs.statSync(filePath);
|
|
18
11
|
const lastModified = stats.mtime.toISOString();
|
|
19
12
|
const indexedAt = new Date().toISOString();
|
|
20
13
|
const normalizedPath = path.normalize(filePath).replace(/\\/g, '/');
|
|
21
|
-
const fileName = path.basename(normalizedPath);
|
|
22
|
-
// Insert into files table
|
|
14
|
+
const fileName = path.basename(normalizedPath);
|
|
23
15
|
const db = getDbForRepo();
|
|
24
16
|
db.prepare(sqlTemplates.upsertFileTemplate).run({
|
|
25
17
|
path: normalizedPath,
|
|
26
|
-
filename: fileName,
|
|
18
|
+
filename: fileName,
|
|
27
19
|
summary,
|
|
28
20
|
type,
|
|
29
21
|
lastModified,
|
|
30
22
|
indexedAt,
|
|
31
23
|
embedding: null
|
|
32
24
|
});
|
|
33
|
-
// Insert into files_fts table for full-text search, including filename
|
|
34
25
|
db.prepare(`
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
`).run({
|
|
26
|
+
INSERT OR REPLACE INTO files_fts (rowid, filename, summary, path)
|
|
27
|
+
VALUES ((SELECT id FROM files WHERE path = :path), :filename, :summary, :path)
|
|
28
|
+
`).run({
|
|
38
29
|
path: normalizedPath,
|
|
39
30
|
filename: fileName,
|
|
40
|
-
summary
|
|
31
|
+
summary,
|
|
41
32
|
});
|
|
42
33
|
console.log(`๐ Indexed: ${normalizedPath}`);
|
|
43
34
|
}
|
|
44
|
-
/**
|
|
45
|
-
* ๐ FTS5 keyword-based search using SQLite's full-text index.
|
|
46
|
-
*
|
|
47
|
-
* Use this when you want:
|
|
48
|
-
* - A simple keyword search.
|
|
49
|
-
* - Fast fallback search that doesn't rely on embeddings.
|
|
50
|
-
*
|
|
51
|
-
* Returns a full `FileRow[]` (all known metadata, but no similarity score).
|
|
52
|
-
*/
|
|
53
35
|
export function queryFiles(safeQuery, limit = 10) {
|
|
54
36
|
console.log(`Executing search query: ${safeQuery}`);
|
|
55
37
|
const db = getDbForRepo();
|
|
56
|
-
|
|
38
|
+
return db.prepare(`
|
|
57
39
|
SELECT f.id, f.path, f.filename, f.summary, f.type, f.last_modified, f.indexed_at
|
|
58
40
|
FROM files f
|
|
59
41
|
JOIN files_fts fts ON f.id = fts.rowid
|
|
60
42
|
WHERE fts.files_fts MATCH ?
|
|
61
43
|
LIMIT ?
|
|
62
44
|
`).all(safeQuery, limit);
|
|
63
|
-
return results;
|
|
64
45
|
}
|
|
65
|
-
/**
|
|
66
|
-
* ๐ง Combined semantic + FTS search (Hybrid).
|
|
67
|
-
*
|
|
68
|
-
* 1. Convert user query to embedding vector using OpenAI / Ollama etc.
|
|
69
|
-
* 2. Perform a keyword-based FTS match to limit candidates.
|
|
70
|
-
* 3. For each candidate, compute cosine similarity between query + file embedding.
|
|
71
|
-
* 4. Blend the BM25 score and cosine similarity to produce a final score.
|
|
72
|
-
* 5. Return top K most relevant results.
|
|
73
|
-
*
|
|
74
|
-
* โ ๏ธ This returns a **lighter-weight type** than `FileRow` โ doesn't include id, timestamps, etc.
|
|
75
|
-
* Use a wrapper type like `SearchResultWithScore` in calling code if you need both.
|
|
76
|
-
*/
|
|
77
46
|
export async function searchFiles(query, topK = 5) {
|
|
78
47
|
console.log(`๐ง Searching for query: "${query}"`);
|
|
79
48
|
const embedding = await generateEmbedding(query);
|
|
@@ -93,70 +62,11 @@ export async function searchFiles(query, topK = 5) {
|
|
|
93
62
|
LIMIT ?
|
|
94
63
|
`).all(safeQuery, CANDIDATE_LIMIT);
|
|
95
64
|
console.log(`FTS search returned ${ftsResults.length} results`);
|
|
96
|
-
if (ftsResults.length === 0)
|
|
65
|
+
if (ftsResults.length === 0)
|
|
97
66
|
return [];
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const bm25Max = Math.max(...ftsResults.map(r => r.bm25Score));
|
|
101
|
-
const scored = ftsResults.map(result => {
|
|
102
|
-
let sim = 0;
|
|
103
|
-
let finalScore = 0;
|
|
104
|
-
const normalizedBm25 = 1 - ((result.bm25Score - bm25Min) / (bm25Max - bm25Min + 1e-5));
|
|
105
|
-
if (result.embedding) {
|
|
106
|
-
try {
|
|
107
|
-
const vector = JSON.parse(result.embedding);
|
|
108
|
-
sim = cosineSimilarity(embedding, vector);
|
|
109
|
-
}
|
|
110
|
-
catch (err) {
|
|
111
|
-
console.error(`โ Failed to parse embedding for ${result.path}:`, err);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
const terms = query.toLowerCase().split(/\s+/);
|
|
115
|
-
const path = result.path.toLowerCase();
|
|
116
|
-
const summary = (result.summary || '').toLowerCase();
|
|
117
|
-
let termMatches = 0;
|
|
118
|
-
for (const term of terms) {
|
|
119
|
-
if (path.includes(term) || summary.includes(term)) {
|
|
120
|
-
termMatches += 1;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
const matchRatio = termMatches / terms.length;
|
|
124
|
-
const termBoost = matchRatio >= 1 ? 1.0 : matchRatio >= 0.5 ? 0.5 : 0;
|
|
125
|
-
// ๐ง Final score with hybrid weighting (BM25 + Embedding + Term Boost)
|
|
126
|
-
finalScore = 0.4 * normalizedBm25 + 0.4 * sim + 0.2 * termBoost;
|
|
127
|
-
// โ
Fuzzy score using string-similarity-js
|
|
128
|
-
const fuzzyScore = stringSimilarity(query.toLowerCase(), `${path} ${summary}`);
|
|
129
|
-
finalScore += fuzzyScore * 10;
|
|
130
|
-
return {
|
|
131
|
-
id: result.id,
|
|
132
|
-
path: result.path,
|
|
133
|
-
summary: result.summary,
|
|
134
|
-
score: finalScore,
|
|
135
|
-
sim,
|
|
136
|
-
bm25: result.bm25Score,
|
|
137
|
-
};
|
|
138
|
-
});
|
|
139
|
-
const sorted = scored
|
|
140
|
-
.sort((a, b) => b.score - a.score)
|
|
141
|
-
.slice(0, topK);
|
|
142
|
-
console.log(`Returning top ${topK} results based on combined score`);
|
|
143
|
-
return sorted;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* ๐ข Cosine similarity between two vectors.
|
|
147
|
-
*/
|
|
148
|
-
function cosineSimilarity(a, b) {
|
|
149
|
-
const dot = a.reduce((sum, ai, i) => sum + ai * b[i], 0);
|
|
150
|
-
const magA = Math.sqrt(a.reduce((sum, ai) => sum + ai * ai, 0));
|
|
151
|
-
const magB = Math.sqrt(b.reduce((sum, bi) => sum + bi * bi, 0));
|
|
152
|
-
return dot / (magA * magB);
|
|
67
|
+
const scored = scoreFiles(query, embedding, ftsResults);
|
|
68
|
+
return scored.slice(0, topK);
|
|
153
69
|
}
|
|
154
|
-
/**
|
|
155
|
-
* ๐ง Get all extracted functions for a given list of file IDs.
|
|
156
|
-
*
|
|
157
|
-
* - Used in `askCmd.ts` to fetch code snippets from relevant files.
|
|
158
|
-
* - Grouped by file_id.
|
|
159
|
-
*/
|
|
160
70
|
export function getFunctionsForFiles(fileIds) {
|
|
161
71
|
if (!fileIds.length)
|
|
162
72
|
return {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { stringSimilarity } from 'string-similarity-js';
|
|
2
|
+
export function scoreFiles(query, embedding, candidates) {
|
|
3
|
+
const terms = query.toLowerCase().split(/\s+/);
|
|
4
|
+
const bm25Min = Math.min(...candidates.map(r => r.bm25Score));
|
|
5
|
+
const bm25Max = Math.max(...candidates.map(r => r.bm25Score));
|
|
6
|
+
return candidates.map(result => {
|
|
7
|
+
let finalScore = 0;
|
|
8
|
+
let sim = 0;
|
|
9
|
+
const path = result.path.toLowerCase();
|
|
10
|
+
const filename = result.filename.toLowerCase();
|
|
11
|
+
const summary = (result.summary || '').toLowerCase();
|
|
12
|
+
// ๐ฏ Normalize BM25
|
|
13
|
+
const normalizedBm25 = 1 - ((result.bm25Score - bm25Min) / (bm25Max - bm25Min + 1e-5));
|
|
14
|
+
// ๐ง Embedding similarity
|
|
15
|
+
if (result.embedding) {
|
|
16
|
+
try {
|
|
17
|
+
const vector = JSON.parse(result.embedding);
|
|
18
|
+
sim = cosineSimilarity(embedding, vector);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
sim = 0;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// ๐งฉ Match ratio
|
|
25
|
+
let matchRatio = 0;
|
|
26
|
+
let matchedTerms = 0;
|
|
27
|
+
for (const term of terms) {
|
|
28
|
+
if (path.includes(term) || summary.includes(term))
|
|
29
|
+
matchedTerms++;
|
|
30
|
+
}
|
|
31
|
+
matchRatio = matchedTerms / terms.length;
|
|
32
|
+
const termBoost = matchRatio >= 1 ? 1.0 : matchRatio >= 0.5 ? 0.5 : 0;
|
|
33
|
+
// ๐ช Path heuristics
|
|
34
|
+
const isHtml = path.endsWith('.html');
|
|
35
|
+
const isSrc = path.includes('/src/') || path.includes('/controls/');
|
|
36
|
+
const isDoc = path.includes('/docs/') || path.includes('/mvndist/');
|
|
37
|
+
const isExactMatch = filename === `${terms[0]}.js`;
|
|
38
|
+
let weight = 1;
|
|
39
|
+
if (isHtml)
|
|
40
|
+
weight *= 0.85;
|
|
41
|
+
if (isDoc)
|
|
42
|
+
weight *= 0.8;
|
|
43
|
+
if (isSrc)
|
|
44
|
+
weight *= 1.2;
|
|
45
|
+
if (isExactMatch)
|
|
46
|
+
weight *= 1.5;
|
|
47
|
+
// ๐งช Fuzzy score
|
|
48
|
+
const fuzzyScore = stringSimilarity(query, `${path} ${summary}`);
|
|
49
|
+
// ๐งฎ Final composite score
|
|
50
|
+
finalScore =
|
|
51
|
+
(0.4 * normalizedBm25) +
|
|
52
|
+
(0.4 * sim) +
|
|
53
|
+
(0.2 * termBoost) +
|
|
54
|
+
(fuzzyScore * 0.5); // scale fuzzy match moderately
|
|
55
|
+
finalScore *= weight;
|
|
56
|
+
return {
|
|
57
|
+
id: result.id,
|
|
58
|
+
path: result.path,
|
|
59
|
+
summary: result.summary,
|
|
60
|
+
score: finalScore,
|
|
61
|
+
sim,
|
|
62
|
+
bm25: result.bm25Score,
|
|
63
|
+
};
|
|
64
|
+
}).sort((a, b) => b.score - a.score);
|
|
65
|
+
}
|
|
66
|
+
function cosineSimilarity(a, b) {
|
|
67
|
+
const dot = a.reduce((sum, ai, i) => sum + ai * b[i], 0);
|
|
68
|
+
const magA = Math.sqrt(a.reduce((sum, ai) => sum + ai * ai, 0));
|
|
69
|
+
const magB = Math.sqrt(b.reduce((sum, bi) => sum + bi * bi, 0));
|
|
70
|
+
return dot / (magA * magB);
|
|
71
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -24,7 +24,7 @@ import { reviewPullRequestCmd } from "./commands/ReviewCmd.js";
|
|
|
24
24
|
import { promptForToken } from "./github/token.js";
|
|
25
25
|
import { validateGitHubTokenAgainstRepo } from "./github/githubAuthCheck.js";
|
|
26
26
|
import { checkGit } from "./commands/GitCmd.js";
|
|
27
|
-
import {
|
|
27
|
+
import { runInteractiveSwitch } from "./commands/SwitchCmd.js";
|
|
28
28
|
import { execSync } from "child_process";
|
|
29
29
|
import { fileURLToPath } from "url";
|
|
30
30
|
import { dirname, resolve } from "path";
|
|
@@ -166,15 +166,10 @@ index
|
|
|
166
166
|
Config.printAllRepos(); // ๐ simple and clean
|
|
167
167
|
});
|
|
168
168
|
index
|
|
169
|
-
.command('switch
|
|
170
|
-
.description('Switch active repository (by
|
|
171
|
-
.action((
|
|
172
|
-
|
|
173
|
-
runSwitchCommand(input);
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
runInteractiveSwitch();
|
|
177
|
-
}
|
|
169
|
+
.command('switch')
|
|
170
|
+
.description('Switch active repository (by interactive list only)')
|
|
171
|
+
.action(() => {
|
|
172
|
+
runInteractiveSwitch();
|
|
178
173
|
});
|
|
179
174
|
// This will help resolve the current directory in an ES Module
|
|
180
175
|
cmd
|