org-qmd 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/CHANGELOG.md +529 -0
- package/LICENSE +21 -0
- package/README.md +917 -0
- package/bin/qmd +32 -0
- package/dist/ast.d.ts +64 -0
- package/dist/ast.js +324 -0
- package/dist/cli/formatter.d.ts +120 -0
- package/dist/cli/formatter.js +350 -0
- package/dist/cli/qmd.d.ts +1 -0
- package/dist/cli/qmd.js +2820 -0
- package/dist/collections.d.ts +146 -0
- package/dist/collections.js +385 -0
- package/dist/db.d.ts +41 -0
- package/dist/db.js +75 -0
- package/dist/embedded-skills.d.ts +6 -0
- package/dist/embedded-skills.js +14 -0
- package/dist/index.d.ts +226 -0
- package/dist/index.js +234 -0
- package/dist/llm.d.ts +406 -0
- package/dist/llm.js +1174 -0
- package/dist/maintenance.d.ts +23 -0
- package/dist/maintenance.js +37 -0
- package/dist/mcp/server.d.ts +21 -0
- package/dist/mcp/server.js +653 -0
- package/dist/store.d.ts +993 -0
- package/dist/store.js +3806 -0
- package/package.json +101 -0
package/dist/cli/qmd.js
ADDED
|
@@ -0,0 +1,2820 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { openDatabase } from "../db.js";
|
|
3
|
+
import fastGlob from "fast-glob";
|
|
4
|
+
import { execSync, spawn as nodeSpawn } from "child_process";
|
|
5
|
+
import { fileURLToPath } from "url";
|
|
6
|
+
import { dirname, join as pathJoin, relative as relativePath } from "path";
|
|
7
|
+
import { parseArgs } from "util";
|
|
8
|
+
import { readFileSync, realpathSync, statSync, existsSync, unlinkSync, writeFileSync, openSync, closeSync, mkdirSync, lstatSync, rmSync, symlinkSync, readlinkSync } from "fs";
|
|
9
|
+
import { createInterface } from "readline/promises";
|
|
10
|
+
import { getPwd, getRealPath, homedir, resolve, enableProductionMode, searchFTS, extractSnippet, getContextForFile, getContextForPath, listCollections, removeCollection, renameCollection, findSimilarFiles, findDocumentByDocid, isDocid, matchFilesByGlob, getHashesNeedingEmbedding, clearAllEmbeddings, insertEmbedding, getStatus, hashContent, extractTitle, formatDocForEmbedding, chunkDocumentByTokens, clearCache, getCacheKey, getCachedResult, setCachedResult, getIndexHealth, parseVirtualPath, buildVirtualPath, isVirtualPath, resolveVirtualPath, toVirtualPath, insertContent, insertDocument, findActiveDocument, updateDocumentTitle, updateDocument, deactivateDocument, getActiveDocumentPaths, cleanupOrphanedContent, deleteLLMCache, deleteInactiveDocuments, cleanupOrphanedVectors, vacuumDatabase, getCollectionsWithoutContext, getTopLevelPathsWithoutContext, handelize, hybridQuery, vectorSearchQuery, structuredSearch, addLineNumbers, DEFAULT_EMBED_MODEL, DEFAULT_EMBED_MAX_BATCH_BYTES, DEFAULT_EMBED_MAX_DOCS_PER_BATCH, DEFAULT_RERANK_MODEL, DEFAULT_GLOB, DEFAULT_MULTI_GET_MAX_BYTES, createStore, getDefaultDbPath, reindexCollection, generateEmbeddings, syncConfigToDb, } from "../store.js";
|
|
11
|
+
import { disposeDefaultLlamaCpp, getDefaultLlamaCpp, withLLMSession, pullModels, DEFAULT_EMBED_MODEL_URI, DEFAULT_GENERATE_MODEL_URI, DEFAULT_RERANK_MODEL_URI, DEFAULT_MODEL_CACHE_DIR } from "../llm.js";
|
|
12
|
+
import { formatSearchResults, formatDocuments, escapeXml, escapeCSV, } from "./formatter.js";
|
|
13
|
+
import { getCollection as getCollectionFromYaml, listCollections as yamlListCollections, getDefaultCollectionNames, addContext as yamlAddContext, removeContext as yamlRemoveContext, removeCollection as yamlRemoveCollectionFn, renameCollection as yamlRenameCollectionFn, setGlobalContext, listAllContexts, setConfigIndexName, loadConfig, } from "../collections.js";
|
|
14
|
+
import { getEmbeddedQmdSkillContent, getEmbeddedQmdSkillFiles } from "../embedded-skills.js";
|
|
15
|
+
// Enable production mode - allows using default database path
|
|
16
|
+
// Tests must set INDEX_PATH or use createStore() with explicit path
|
|
17
|
+
enableProductionMode();
|
|
18
|
+
// =============================================================================
|
|
19
|
+
// Store/DB lifecycle (no legacy singletons in store.ts)
|
|
20
|
+
// =============================================================================
|
|
21
|
+
let store = null;
|
|
22
|
+
let storeDbPathOverride;
|
|
23
|
+
function getStore() {
|
|
24
|
+
if (!store) {
|
|
25
|
+
store = createStore(storeDbPathOverride);
|
|
26
|
+
// Sync YAML config into SQLite store_collections so store.ts reads from DB
|
|
27
|
+
try {
|
|
28
|
+
const config = loadConfig();
|
|
29
|
+
syncConfigToDb(store.db, config);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// Config may not exist yet — that's fine, DB works without it
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return store;
|
|
36
|
+
}
|
|
37
|
+
function getDb() {
|
|
38
|
+
return getStore().db;
|
|
39
|
+
}
|
|
40
|
+
/** Re-sync YAML config into SQLite after CLI mutations (add/remove/rename collection, context changes) */
|
|
41
|
+
function resyncConfig() {
|
|
42
|
+
const s = getStore();
|
|
43
|
+
try {
|
|
44
|
+
const config = loadConfig();
|
|
45
|
+
// Clear config hash to force re-sync
|
|
46
|
+
s.db.prepare(`DELETE FROM store_config WHERE key = 'config_hash'`).run();
|
|
47
|
+
syncConfigToDb(s.db, config);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// Config may not exist — that's fine
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function closeDb() {
|
|
54
|
+
if (store) {
|
|
55
|
+
store.close();
|
|
56
|
+
store = null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function getDbPath() {
|
|
60
|
+
return store?.dbPath ?? storeDbPathOverride ?? getDefaultDbPath();
|
|
61
|
+
}
|
|
62
|
+
function setIndexName(name) {
|
|
63
|
+
let normalizedName = name;
|
|
64
|
+
// Normalize relative paths to prevent malformed database paths
|
|
65
|
+
if (name && name.includes('/')) {
|
|
66
|
+
const { resolve } = require('path');
|
|
67
|
+
const { cwd } = require('process');
|
|
68
|
+
const absolutePath = resolve(cwd(), name);
|
|
69
|
+
// Replace path separators with underscores to create a valid filename
|
|
70
|
+
normalizedName = absolutePath.replace(/\//g, '_').replace(/^_/, '');
|
|
71
|
+
}
|
|
72
|
+
storeDbPathOverride = normalizedName ? getDefaultDbPath(normalizedName) : undefined;
|
|
73
|
+
// Reset open handle so next use opens the new index
|
|
74
|
+
closeDb();
|
|
75
|
+
}
|
|
76
|
+
function ensureVecTable(_db, dimensions) {
|
|
77
|
+
// Store owns the DB; ignore `_db` and ensure vec table on the active store
|
|
78
|
+
getStore().ensureVecTable(dimensions);
|
|
79
|
+
}
|
|
80
|
+
// Terminal colors (respects NO_COLOR env)
|
|
81
|
+
const useColor = !process.env.NO_COLOR && process.stdout.isTTY;
|
|
82
|
+
const c = {
|
|
83
|
+
reset: useColor ? "\x1b[0m" : "",
|
|
84
|
+
dim: useColor ? "\x1b[2m" : "",
|
|
85
|
+
bold: useColor ? "\x1b[1m" : "",
|
|
86
|
+
cyan: useColor ? "\x1b[36m" : "",
|
|
87
|
+
yellow: useColor ? "\x1b[33m" : "",
|
|
88
|
+
green: useColor ? "\x1b[32m" : "",
|
|
89
|
+
magenta: useColor ? "\x1b[35m" : "",
|
|
90
|
+
blue: useColor ? "\x1b[34m" : "",
|
|
91
|
+
};
|
|
92
|
+
// Terminal cursor control
|
|
93
|
+
const cursor = {
|
|
94
|
+
hide() { process.stderr.write('\x1b[?25l'); },
|
|
95
|
+
show() { process.stderr.write('\x1b[?25h'); },
|
|
96
|
+
};
|
|
97
|
+
// Ensure cursor is restored on exit
|
|
98
|
+
process.on('SIGINT', () => { cursor.show(); process.exit(130); });
|
|
99
|
+
process.on('SIGTERM', () => { cursor.show(); process.exit(143); });
|
|
100
|
+
// Terminal progress bar using OSC 9;4 escape sequence (TTY only)
|
|
101
|
+
const isTTY = process.stderr.isTTY;
|
|
102
|
+
const progress = {
|
|
103
|
+
set(percent) {
|
|
104
|
+
if (isTTY)
|
|
105
|
+
process.stderr.write(`\x1b]9;4;1;${Math.round(percent)}\x07`);
|
|
106
|
+
},
|
|
107
|
+
clear() {
|
|
108
|
+
if (isTTY)
|
|
109
|
+
process.stderr.write(`\x1b]9;4;0\x07`);
|
|
110
|
+
},
|
|
111
|
+
indeterminate() {
|
|
112
|
+
if (isTTY)
|
|
113
|
+
process.stderr.write(`\x1b]9;4;3\x07`);
|
|
114
|
+
},
|
|
115
|
+
error() {
|
|
116
|
+
if (isTTY)
|
|
117
|
+
process.stderr.write(`\x1b]9;4;2\x07`);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
// Format seconds into human-readable ETA
|
|
121
|
+
function formatETA(seconds) {
|
|
122
|
+
if (seconds < 60)
|
|
123
|
+
return `${Math.round(seconds)}s`;
|
|
124
|
+
if (seconds < 3600)
|
|
125
|
+
return `${Math.floor(seconds / 60)}m ${Math.round(seconds % 60)}s`;
|
|
126
|
+
return `${Math.floor(seconds / 3600)}h ${Math.floor((seconds % 3600) / 60)}m`;
|
|
127
|
+
}
|
|
128
|
+
// Check index health and print warnings/tips
|
|
129
|
+
function checkIndexHealth(db) {
|
|
130
|
+
const { needsEmbedding, totalDocs, daysStale } = getIndexHealth(db);
|
|
131
|
+
// Warn if many docs need embedding
|
|
132
|
+
if (needsEmbedding > 0) {
|
|
133
|
+
const pct = Math.round((needsEmbedding / totalDocs) * 100);
|
|
134
|
+
if (pct >= 10) {
|
|
135
|
+
process.stderr.write(`${c.yellow}Warning: ${needsEmbedding} documents (${pct}%) need embeddings. Run 'qmd embed' for better results.${c.reset}\n`);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
process.stderr.write(`${c.dim}Tip: ${needsEmbedding} documents need embeddings. Run 'qmd embed' to index them.${c.reset}\n`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// Check if most recent document update is older than 2 weeks
|
|
142
|
+
if (daysStale !== null && daysStale >= 14) {
|
|
143
|
+
process.stderr.write(`${c.dim}Tip: Index last updated ${daysStale} days ago. Run 'qmd update' to refresh.${c.reset}\n`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// Compute unique display path for a document
|
|
147
|
+
// Always include at least parent folder + filename, add more parent dirs until unique
|
|
148
|
+
function computeDisplayPath(filepath, collectionPath, existingPaths) {
|
|
149
|
+
// Get path relative to collection (include collection dir name)
|
|
150
|
+
const collectionDir = collectionPath.replace(/\/$/, '');
|
|
151
|
+
const collectionName = collectionDir.split('/').pop() || '';
|
|
152
|
+
let relativePath;
|
|
153
|
+
if (filepath.startsWith(collectionDir + '/')) {
|
|
154
|
+
// filepath is under collection: use collection name + relative path
|
|
155
|
+
relativePath = collectionName + filepath.slice(collectionDir.length);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
// Fallback: just use the filepath
|
|
159
|
+
relativePath = filepath;
|
|
160
|
+
}
|
|
161
|
+
const parts = relativePath.split('/').filter(p => p.length > 0);
|
|
162
|
+
// Always include at least parent folder + filename (minimum 2 parts if available)
|
|
163
|
+
// Then add more parent dirs until unique
|
|
164
|
+
const minParts = Math.min(2, parts.length);
|
|
165
|
+
for (let i = parts.length - minParts; i >= 0; i--) {
|
|
166
|
+
const candidate = parts.slice(i).join('/');
|
|
167
|
+
if (!existingPaths.has(candidate)) {
|
|
168
|
+
return candidate;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Absolute fallback: use full path (should be unique)
|
|
172
|
+
return filepath;
|
|
173
|
+
}
|
|
174
|
+
function formatTimeAgo(date) {
|
|
175
|
+
const seconds = Math.floor((Date.now() - date.getTime()) / 1000);
|
|
176
|
+
if (seconds < 60)
|
|
177
|
+
return `${seconds}s ago`;
|
|
178
|
+
const minutes = Math.floor(seconds / 60);
|
|
179
|
+
if (minutes < 60)
|
|
180
|
+
return `${minutes}m ago`;
|
|
181
|
+
const hours = Math.floor(minutes / 60);
|
|
182
|
+
if (hours < 24)
|
|
183
|
+
return `${hours}h ago`;
|
|
184
|
+
const days = Math.floor(hours / 24);
|
|
185
|
+
return `${days}d ago`;
|
|
186
|
+
}
|
|
187
|
+
function formatMs(ms) {
|
|
188
|
+
if (ms < 1000)
|
|
189
|
+
return `${ms}ms`;
|
|
190
|
+
return `${(ms / 1000).toFixed(1)}s`;
|
|
191
|
+
}
|
|
192
|
+
function formatBytes(bytes) {
|
|
193
|
+
if (bytes < 1024)
|
|
194
|
+
return `${bytes} B`;
|
|
195
|
+
if (bytes < 1024 * 1024)
|
|
196
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
197
|
+
if (bytes < 1024 * 1024 * 1024)
|
|
198
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
199
|
+
return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`;
|
|
200
|
+
}
|
|
201
|
+
async function showStatus() {
|
|
202
|
+
const dbPath = getDbPath();
|
|
203
|
+
const db = getDb();
|
|
204
|
+
// Collections are defined in YAML; no duplicate cleanup needed.
|
|
205
|
+
// Collections are defined in YAML; no duplicate cleanup needed.
|
|
206
|
+
// Index size
|
|
207
|
+
let indexSize = 0;
|
|
208
|
+
try {
|
|
209
|
+
const stat = statSync(dbPath).size;
|
|
210
|
+
indexSize = stat;
|
|
211
|
+
}
|
|
212
|
+
catch { }
|
|
213
|
+
// Collections info (from YAML + database stats)
|
|
214
|
+
const collections = listCollections(db);
|
|
215
|
+
// Overall stats
|
|
216
|
+
const totalDocs = db.prepare(`SELECT COUNT(*) as count FROM documents WHERE active = 1`).get();
|
|
217
|
+
const vectorCount = db.prepare(`SELECT COUNT(*) as count FROM content_vectors`).get();
|
|
218
|
+
const needsEmbedding = getHashesNeedingEmbedding(db);
|
|
219
|
+
// Most recent update across all collections
|
|
220
|
+
const mostRecent = db.prepare(`SELECT MAX(modified_at) as latest FROM documents WHERE active = 1`).get();
|
|
221
|
+
console.log(`${c.bold}QMD Status${c.reset}\n`);
|
|
222
|
+
console.log(`Index: ${dbPath}`);
|
|
223
|
+
console.log(`Size: ${formatBytes(indexSize)}`);
|
|
224
|
+
// MCP daemon status (check PID file liveness)
|
|
225
|
+
const mcpCacheDir = process.env.XDG_CACHE_HOME
|
|
226
|
+
? resolve(process.env.XDG_CACHE_HOME, "qmd")
|
|
227
|
+
: resolve(homedir(), ".cache", "qmd");
|
|
228
|
+
const mcpPidPath = resolve(mcpCacheDir, "mcp.pid");
|
|
229
|
+
if (existsSync(mcpPidPath)) {
|
|
230
|
+
const mcpPid = parseInt(readFileSync(mcpPidPath, "utf-8").trim());
|
|
231
|
+
try {
|
|
232
|
+
process.kill(mcpPid, 0);
|
|
233
|
+
console.log(`MCP: ${c.green}running${c.reset} (PID ${mcpPid})`);
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
unlinkSync(mcpPidPath);
|
|
237
|
+
// Stale PID file cleaned up silently
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
console.log("");
|
|
241
|
+
console.log(`${c.bold}Documents${c.reset}`);
|
|
242
|
+
console.log(` Total: ${totalDocs.count} files indexed`);
|
|
243
|
+
console.log(` Vectors: ${vectorCount.count} embedded`);
|
|
244
|
+
if (needsEmbedding > 0) {
|
|
245
|
+
console.log(` ${c.yellow}Pending: ${needsEmbedding} need embedding${c.reset} (run 'qmd embed')`);
|
|
246
|
+
}
|
|
247
|
+
if (mostRecent.latest) {
|
|
248
|
+
const lastUpdate = new Date(mostRecent.latest);
|
|
249
|
+
console.log(` Updated: ${formatTimeAgo(lastUpdate)}`);
|
|
250
|
+
}
|
|
251
|
+
// Get all contexts grouped by collection (from YAML)
|
|
252
|
+
const allContexts = listAllContexts();
|
|
253
|
+
const contextsByCollection = new Map();
|
|
254
|
+
for (const ctx of allContexts) {
|
|
255
|
+
// Group contexts by collection name
|
|
256
|
+
if (!contextsByCollection.has(ctx.collection)) {
|
|
257
|
+
contextsByCollection.set(ctx.collection, []);
|
|
258
|
+
}
|
|
259
|
+
contextsByCollection.get(ctx.collection).push({
|
|
260
|
+
path_prefix: ctx.path,
|
|
261
|
+
context: ctx.context
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
// AST chunking status
|
|
265
|
+
try {
|
|
266
|
+
const { getASTStatus } = await import("../ast.js");
|
|
267
|
+
const ast = await getASTStatus();
|
|
268
|
+
console.log(`\n${c.bold}AST Chunking${c.reset}`);
|
|
269
|
+
if (ast.available) {
|
|
270
|
+
const ok = ast.languages.filter(l => l.available).map(l => l.language);
|
|
271
|
+
const fail = ast.languages.filter(l => !l.available);
|
|
272
|
+
console.log(` Status: ${c.green}active${c.reset}`);
|
|
273
|
+
console.log(` Languages: ${ok.join(", ")}`);
|
|
274
|
+
if (fail.length > 0) {
|
|
275
|
+
for (const f of fail) {
|
|
276
|
+
console.log(` ${c.yellow}Unavailable: ${f.language} (${f.error})${c.reset}`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
console.log(` Status: ${c.yellow}unavailable${c.reset} (falling back to regex chunking)`);
|
|
282
|
+
for (const l of ast.languages) {
|
|
283
|
+
if (l.error)
|
|
284
|
+
console.log(` ${c.dim}${l.language}: ${l.error}${c.reset}`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
console.log(`\n${c.bold}AST Chunking${c.reset}`);
|
|
290
|
+
console.log(` Status: ${c.dim}not available${c.reset}`);
|
|
291
|
+
}
|
|
292
|
+
if (collections.length > 0) {
|
|
293
|
+
console.log(`\n${c.bold}Collections${c.reset}`);
|
|
294
|
+
for (const col of collections) {
|
|
295
|
+
const lastMod = col.last_modified ? formatTimeAgo(new Date(col.last_modified)) : "never";
|
|
296
|
+
const contexts = contextsByCollection.get(col.name) || [];
|
|
297
|
+
console.log(` ${c.cyan}${col.name}${c.reset} ${c.dim}(qmd://${col.name}/)${c.reset}`);
|
|
298
|
+
console.log(` ${c.dim}Pattern:${c.reset} ${col.glob_pattern}`);
|
|
299
|
+
console.log(` ${c.dim}Files:${c.reset} ${col.active_count} (updated ${lastMod})`);
|
|
300
|
+
if (contexts.length > 0) {
|
|
301
|
+
console.log(` ${c.dim}Contexts:${c.reset} ${contexts.length}`);
|
|
302
|
+
for (const ctx of contexts) {
|
|
303
|
+
// Handle both empty string and '/' as root context
|
|
304
|
+
const pathDisplay = (ctx.path_prefix === '' || ctx.path_prefix === '/') ? '/' : `/${ctx.path_prefix}`;
|
|
305
|
+
const contextPreview = ctx.context.length > 60
|
|
306
|
+
? ctx.context.substring(0, 57) + '...'
|
|
307
|
+
: ctx.context;
|
|
308
|
+
console.log(` ${c.dim}${pathDisplay}:${c.reset} ${contextPreview}`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
// Show examples of virtual paths
|
|
313
|
+
console.log(`\n${c.bold}Examples${c.reset}`);
|
|
314
|
+
console.log(` ${c.dim}# List files in a collection${c.reset}`);
|
|
315
|
+
if (collections.length > 0 && collections[0]) {
|
|
316
|
+
console.log(` qmd ls ${collections[0].name}`);
|
|
317
|
+
}
|
|
318
|
+
console.log(` ${c.dim}# Get a document${c.reset}`);
|
|
319
|
+
if (collections.length > 0 && collections[0]) {
|
|
320
|
+
console.log(` qmd get qmd://${collections[0].name}/path/to/file.md`);
|
|
321
|
+
}
|
|
322
|
+
console.log(` ${c.dim}# Search within a collection${c.reset}`);
|
|
323
|
+
if (collections.length > 0 && collections[0]) {
|
|
324
|
+
console.log(` qmd search "query" -c ${collections[0].name}`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
console.log(`\n${c.dim}No collections. Run 'qmd collection add .' to index markdown files.${c.reset}`);
|
|
329
|
+
}
|
|
330
|
+
// Models
|
|
331
|
+
{
|
|
332
|
+
// hf:org/repo/file.gguf → https://huggingface.co/org/repo
|
|
333
|
+
const hfLink = (uri) => {
|
|
334
|
+
const match = uri.match(/^hf:([^/]+\/[^/]+)\//);
|
|
335
|
+
return match ? `https://huggingface.co/${match[1]}` : uri;
|
|
336
|
+
};
|
|
337
|
+
console.log(`\n${c.bold}Models${c.reset}`);
|
|
338
|
+
console.log(` Embedding: ${hfLink(DEFAULT_EMBED_MODEL_URI)}`);
|
|
339
|
+
console.log(` Reranking: ${hfLink(DEFAULT_RERANK_MODEL_URI)}`);
|
|
340
|
+
console.log(` Generation: ${hfLink(DEFAULT_GENERATE_MODEL_URI)}`);
|
|
341
|
+
}
|
|
342
|
+
// Device / GPU info
|
|
343
|
+
try {
|
|
344
|
+
const llm = getDefaultLlamaCpp();
|
|
345
|
+
const device = await llm.getDeviceInfo();
|
|
346
|
+
console.log(`\n${c.bold}Device${c.reset}`);
|
|
347
|
+
if (device.gpu) {
|
|
348
|
+
console.log(` GPU: ${c.green}${device.gpu}${c.reset} (offloading: ${device.gpuOffloading ? 'yes' : 'no'})`);
|
|
349
|
+
if (device.gpuDevices.length > 0) {
|
|
350
|
+
// Deduplicate and count GPUs
|
|
351
|
+
const counts = new Map();
|
|
352
|
+
for (const name of device.gpuDevices) {
|
|
353
|
+
counts.set(name, (counts.get(name) || 0) + 1);
|
|
354
|
+
}
|
|
355
|
+
const deviceStr = Array.from(counts.entries())
|
|
356
|
+
.map(([name, count]) => count > 1 ? `${count}× ${name}` : name)
|
|
357
|
+
.join(', ');
|
|
358
|
+
console.log(` Devices: ${deviceStr}`);
|
|
359
|
+
}
|
|
360
|
+
if (device.vram) {
|
|
361
|
+
console.log(` VRAM: ${formatBytes(device.vram.free)} free / ${formatBytes(device.vram.total)} total`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
console.log(` GPU: ${c.yellow}none${c.reset} (running on CPU — models will be slow)`);
|
|
366
|
+
console.log(` ${c.dim}Tip: Install CUDA, Vulkan, or Metal support for GPU acceleration.${c.reset}`);
|
|
367
|
+
}
|
|
368
|
+
console.log(` CPU: ${device.cpuCores} math cores`);
|
|
369
|
+
}
|
|
370
|
+
catch {
|
|
371
|
+
// Don't fail status if LLM init fails
|
|
372
|
+
}
|
|
373
|
+
// Tips section
|
|
374
|
+
const tips = [];
|
|
375
|
+
// Check for collections without context
|
|
376
|
+
const collectionsWithoutContext = collections.filter(col => {
|
|
377
|
+
const contexts = contextsByCollection.get(col.name) || [];
|
|
378
|
+
return contexts.length === 0;
|
|
379
|
+
});
|
|
380
|
+
if (collectionsWithoutContext.length > 0) {
|
|
381
|
+
const names = collectionsWithoutContext.map(c => c.name).slice(0, 3).join(', ');
|
|
382
|
+
const more = collectionsWithoutContext.length > 3 ? ` +${collectionsWithoutContext.length - 3} more` : '';
|
|
383
|
+
tips.push(`Add context to collections for better search results: ${names}${more}`);
|
|
384
|
+
tips.push(` ${c.dim}qmd context add qmd://<name>/ "What this collection contains"${c.reset}`);
|
|
385
|
+
tips.push(` ${c.dim}qmd context add qmd://<name>/meeting-notes "Weekly team meeting notes"${c.reset}`);
|
|
386
|
+
}
|
|
387
|
+
// Check for collections without update commands
|
|
388
|
+
const collectionsWithoutUpdate = collections.filter(col => {
|
|
389
|
+
const yamlCol = getCollectionFromYaml(col.name);
|
|
390
|
+
return !yamlCol?.update;
|
|
391
|
+
});
|
|
392
|
+
if (collectionsWithoutUpdate.length > 0 && collections.length > 1) {
|
|
393
|
+
const names = collectionsWithoutUpdate.map(c => c.name).slice(0, 3).join(', ');
|
|
394
|
+
const more = collectionsWithoutUpdate.length > 3 ? ` +${collectionsWithoutUpdate.length - 3} more` : '';
|
|
395
|
+
tips.push(`Add update commands to keep collections fresh: ${names}${more}`);
|
|
396
|
+
tips.push(` ${c.dim}qmd collection update-cmd <name> 'git stash && git pull --rebase --ff-only && git stash pop'${c.reset}`);
|
|
397
|
+
}
|
|
398
|
+
if (tips.length > 0) {
|
|
399
|
+
console.log(`\n${c.bold}Tips${c.reset}`);
|
|
400
|
+
for (const tip of tips) {
|
|
401
|
+
console.log(` ${tip}`);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
closeDb();
|
|
405
|
+
}
|
|
406
|
+
async function updateCollections() {
|
|
407
|
+
const db = getDb();
|
|
408
|
+
const storeInstance = getStore();
|
|
409
|
+
// Collections are defined in YAML; no duplicate cleanup needed.
|
|
410
|
+
// Clear Ollama cache on update
|
|
411
|
+
clearCache(db);
|
|
412
|
+
const collections = listCollections(db);
|
|
413
|
+
if (collections.length === 0) {
|
|
414
|
+
console.log(`${c.dim}No collections found. Run 'qmd collection add .' to index markdown files.${c.reset}`);
|
|
415
|
+
closeDb();
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
console.log(`${c.bold}Updating ${collections.length} collection(s)...${c.reset}\n`);
|
|
419
|
+
for (let i = 0; i < collections.length; i++) {
|
|
420
|
+
const col = collections[i];
|
|
421
|
+
if (!col)
|
|
422
|
+
continue;
|
|
423
|
+
console.log(`${c.cyan}[${i + 1}/${collections.length}]${c.reset} ${c.bold}${col.name}${c.reset} ${c.dim}(${col.glob_pattern})${c.reset}`);
|
|
424
|
+
// Execute custom update command if specified in YAML
|
|
425
|
+
const yamlCol = getCollectionFromYaml(col.name);
|
|
426
|
+
if (yamlCol?.update) {
|
|
427
|
+
console.log(`${c.dim} Running update command: ${yamlCol.update}${c.reset}`);
|
|
428
|
+
try {
|
|
429
|
+
const proc = nodeSpawn("bash", ["-c", yamlCol.update], {
|
|
430
|
+
cwd: col.pwd,
|
|
431
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
432
|
+
});
|
|
433
|
+
const [output, errorOutput, exitCode] = await new Promise((resolve, reject) => {
|
|
434
|
+
let out = "";
|
|
435
|
+
let err = "";
|
|
436
|
+
proc.stdout?.on("data", (d) => { out += d.toString(); });
|
|
437
|
+
proc.stderr?.on("data", (d) => { err += d.toString(); });
|
|
438
|
+
proc.on("error", reject);
|
|
439
|
+
proc.on("close", (code) => resolve([out, err, code ?? 1]));
|
|
440
|
+
});
|
|
441
|
+
if (output.trim()) {
|
|
442
|
+
console.log(output.trim().split('\n').map(l => ` ${l}`).join('\n'));
|
|
443
|
+
}
|
|
444
|
+
if (errorOutput.trim()) {
|
|
445
|
+
console.log(errorOutput.trim().split('\n').map(l => ` ${l}`).join('\n'));
|
|
446
|
+
}
|
|
447
|
+
if (exitCode !== 0) {
|
|
448
|
+
console.log(`${c.yellow}✗ Update command failed with exit code ${exitCode}${c.reset}`);
|
|
449
|
+
process.exit(exitCode);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
catch (err) {
|
|
453
|
+
console.log(`${c.yellow}✗ Update command failed: ${err}${c.reset}`);
|
|
454
|
+
process.exit(1);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
const startTime = Date.now();
|
|
458
|
+
console.log(`Collection: ${col.pwd} (${col.glob_pattern})`);
|
|
459
|
+
progress.indeterminate();
|
|
460
|
+
const result = await reindexCollection(storeInstance, col.pwd, col.glob_pattern, col.name, {
|
|
461
|
+
ignorePatterns: yamlCol?.ignore,
|
|
462
|
+
onProgress: (info) => {
|
|
463
|
+
progress.set((info.current / info.total) * 100);
|
|
464
|
+
const elapsed = (Date.now() - startTime) / 1000;
|
|
465
|
+
const rate = info.current / elapsed;
|
|
466
|
+
const remaining = (info.total - info.current) / rate;
|
|
467
|
+
const eta = info.current > 2 ? ` ETA: ${formatETA(remaining)}` : "";
|
|
468
|
+
if (isTTY)
|
|
469
|
+
process.stderr.write(`\rIndexing: ${info.current}/${info.total}${eta} `);
|
|
470
|
+
},
|
|
471
|
+
});
|
|
472
|
+
progress.clear();
|
|
473
|
+
console.log(`\nIndexed: ${result.indexed} new, ${result.updated} updated, ${result.unchanged} unchanged, ${result.removed} removed`);
|
|
474
|
+
if (result.orphanedCleaned > 0) {
|
|
475
|
+
console.log(`Cleaned up ${result.orphanedCleaned} orphaned content hash(es)`);
|
|
476
|
+
}
|
|
477
|
+
console.log("");
|
|
478
|
+
}
|
|
479
|
+
// Check if any documents need embedding (show once at end)
|
|
480
|
+
const needsEmbedding = getHashesNeedingEmbedding(db);
|
|
481
|
+
closeDb();
|
|
482
|
+
console.log(`${c.green}✓ All collections updated.${c.reset}`);
|
|
483
|
+
if (needsEmbedding > 0) {
|
|
484
|
+
console.log(`\nRun 'qmd embed' to update embeddings (${needsEmbedding} unique hashes need vectors)`);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Detect which collection (if any) contains the given filesystem path.
|
|
489
|
+
* Returns { collectionId, collectionName, relativePath } or null if not in any collection.
|
|
490
|
+
*/
|
|
491
|
+
function detectCollectionFromPath(db, fsPath) {
|
|
492
|
+
const realPath = getRealPath(fsPath);
|
|
493
|
+
// Find collections that this path is under from YAML
|
|
494
|
+
const allCollections = yamlListCollections();
|
|
495
|
+
// Find longest matching path
|
|
496
|
+
let bestMatch = null;
|
|
497
|
+
for (const coll of allCollections) {
|
|
498
|
+
if (realPath.startsWith(coll.path + '/') || realPath === coll.path) {
|
|
499
|
+
if (!bestMatch || coll.path.length > bestMatch.path.length) {
|
|
500
|
+
bestMatch = { name: coll.name, path: coll.path };
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
if (!bestMatch)
|
|
505
|
+
return null;
|
|
506
|
+
// Calculate relative path
|
|
507
|
+
let relativePath = realPath;
|
|
508
|
+
if (relativePath.startsWith(bestMatch.path + '/')) {
|
|
509
|
+
relativePath = relativePath.slice(bestMatch.path.length + 1);
|
|
510
|
+
}
|
|
511
|
+
else if (relativePath === bestMatch.path) {
|
|
512
|
+
relativePath = '';
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
collectionName: bestMatch.name,
|
|
516
|
+
relativePath
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
async function contextAdd(pathArg, contextText) {
|
|
520
|
+
const db = getDb();
|
|
521
|
+
// Handle "/" as global context (applies to all collections)
|
|
522
|
+
if (pathArg === '/') {
|
|
523
|
+
setGlobalContext(contextText);
|
|
524
|
+
resyncConfig();
|
|
525
|
+
console.log(`${c.green}✓${c.reset} Set global context`);
|
|
526
|
+
console.log(`${c.dim}Context: ${contextText}${c.reset}`);
|
|
527
|
+
closeDb();
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
// Resolve path - defaults to current directory if not provided
|
|
531
|
+
let fsPath = pathArg || '.';
|
|
532
|
+
if (fsPath === '.' || fsPath === './') {
|
|
533
|
+
fsPath = getPwd();
|
|
534
|
+
}
|
|
535
|
+
else if (fsPath.startsWith('~/')) {
|
|
536
|
+
fsPath = homedir() + fsPath.slice(1);
|
|
537
|
+
}
|
|
538
|
+
else if (!fsPath.startsWith('/') && !fsPath.startsWith('qmd://')) {
|
|
539
|
+
fsPath = resolve(getPwd(), fsPath);
|
|
540
|
+
}
|
|
541
|
+
// Handle virtual paths (qmd://collection/path)
|
|
542
|
+
if (isVirtualPath(fsPath)) {
|
|
543
|
+
const parsed = parseVirtualPath(fsPath);
|
|
544
|
+
if (!parsed) {
|
|
545
|
+
console.error(`${c.yellow}Invalid virtual path: ${fsPath}${c.reset}`);
|
|
546
|
+
process.exit(1);
|
|
547
|
+
}
|
|
548
|
+
const coll = getCollectionFromYaml(parsed.collectionName);
|
|
549
|
+
if (!coll) {
|
|
550
|
+
console.error(`${c.yellow}Collection not found: ${parsed.collectionName}${c.reset}`);
|
|
551
|
+
process.exit(1);
|
|
552
|
+
}
|
|
553
|
+
yamlAddContext(parsed.collectionName, parsed.path, contextText);
|
|
554
|
+
resyncConfig();
|
|
555
|
+
const displayPath = parsed.path
|
|
556
|
+
? `qmd://${parsed.collectionName}/${parsed.path}`
|
|
557
|
+
: `qmd://${parsed.collectionName}/ (collection root)`;
|
|
558
|
+
console.log(`${c.green}✓${c.reset} Added context for: ${displayPath}`);
|
|
559
|
+
console.log(`${c.dim}Context: ${contextText}${c.reset}`);
|
|
560
|
+
closeDb();
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
// Detect collection from filesystem path
|
|
564
|
+
const detected = detectCollectionFromPath(db, fsPath);
|
|
565
|
+
if (!detected) {
|
|
566
|
+
console.error(`${c.yellow}Path is not in any indexed collection: ${fsPath}${c.reset}`);
|
|
567
|
+
console.error(`${c.dim}Run 'qmd status' to see indexed collections${c.reset}`);
|
|
568
|
+
process.exit(1);
|
|
569
|
+
}
|
|
570
|
+
yamlAddContext(detected.collectionName, detected.relativePath, contextText);
|
|
571
|
+
resyncConfig();
|
|
572
|
+
const displayPath = detected.relativePath ? `qmd://${detected.collectionName}/${detected.relativePath}` : `qmd://${detected.collectionName}/`;
|
|
573
|
+
console.log(`${c.green}✓${c.reset} Added context for: ${displayPath}`);
|
|
574
|
+
console.log(`${c.dim}Context: ${contextText}${c.reset}`);
|
|
575
|
+
closeDb();
|
|
576
|
+
}
|
|
577
|
+
function contextList() {
|
|
578
|
+
const db = getDb();
|
|
579
|
+
const allContexts = listAllContexts();
|
|
580
|
+
if (allContexts.length === 0) {
|
|
581
|
+
console.log(`${c.dim}No contexts configured. Use 'qmd context add' to add one.${c.reset}`);
|
|
582
|
+
closeDb();
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
console.log(`\n${c.bold}Configured Contexts${c.reset}\n`);
|
|
586
|
+
let lastCollection = '';
|
|
587
|
+
for (const ctx of allContexts) {
|
|
588
|
+
if (ctx.collection !== lastCollection) {
|
|
589
|
+
console.log(`${c.cyan}${ctx.collection}${c.reset}`);
|
|
590
|
+
lastCollection = ctx.collection;
|
|
591
|
+
}
|
|
592
|
+
const displayPath = ctx.path ? ` ${ctx.path}` : ' / (root)';
|
|
593
|
+
console.log(`${displayPath}`);
|
|
594
|
+
console.log(` ${c.dim}${ctx.context}${c.reset}`);
|
|
595
|
+
}
|
|
596
|
+
closeDb();
|
|
597
|
+
}
|
|
598
|
+
function contextRemove(pathArg) {
|
|
599
|
+
if (pathArg === '/') {
|
|
600
|
+
// Remove global context
|
|
601
|
+
setGlobalContext(undefined);
|
|
602
|
+
// Resync so SQLite store_config is updated
|
|
603
|
+
const s = getStore();
|
|
604
|
+
resyncConfig();
|
|
605
|
+
closeDb();
|
|
606
|
+
console.log(`${c.green}✓${c.reset} Removed global context`);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
// Handle virtual paths
|
|
610
|
+
if (isVirtualPath(pathArg)) {
|
|
611
|
+
const parsed = parseVirtualPath(pathArg);
|
|
612
|
+
if (!parsed) {
|
|
613
|
+
console.error(`${c.yellow}Invalid virtual path: ${pathArg}${c.reset}`);
|
|
614
|
+
process.exit(1);
|
|
615
|
+
}
|
|
616
|
+
const coll = getCollectionFromYaml(parsed.collectionName);
|
|
617
|
+
if (!coll) {
|
|
618
|
+
console.error(`${c.yellow}Collection not found: ${parsed.collectionName}${c.reset}`);
|
|
619
|
+
process.exit(1);
|
|
620
|
+
}
|
|
621
|
+
const success = yamlRemoveContext(coll.name, parsed.path);
|
|
622
|
+
if (!success) {
|
|
623
|
+
console.error(`${c.yellow}No context found for: ${pathArg}${c.reset}`);
|
|
624
|
+
process.exit(1);
|
|
625
|
+
}
|
|
626
|
+
console.log(`${c.green}✓${c.reset} Removed context for: ${pathArg}`);
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
// Handle filesystem paths
|
|
630
|
+
let fsPath = pathArg;
|
|
631
|
+
if (fsPath === '.' || fsPath === './') {
|
|
632
|
+
fsPath = getPwd();
|
|
633
|
+
}
|
|
634
|
+
else if (fsPath.startsWith('~/')) {
|
|
635
|
+
fsPath = homedir() + fsPath.slice(1);
|
|
636
|
+
}
|
|
637
|
+
else if (!fsPath.startsWith('/')) {
|
|
638
|
+
fsPath = resolve(getPwd(), fsPath);
|
|
639
|
+
}
|
|
640
|
+
const db = getDb();
|
|
641
|
+
const detected = detectCollectionFromPath(db, fsPath);
|
|
642
|
+
closeDb();
|
|
643
|
+
if (!detected) {
|
|
644
|
+
console.error(`${c.yellow}Path is not in any indexed collection: ${fsPath}${c.reset}`);
|
|
645
|
+
process.exit(1);
|
|
646
|
+
}
|
|
647
|
+
const success = yamlRemoveContext(detected.collectionName, detected.relativePath);
|
|
648
|
+
if (!success) {
|
|
649
|
+
console.error(`${c.yellow}No context found for: qmd://${detected.collectionName}/${detected.relativePath}${c.reset}`);
|
|
650
|
+
process.exit(1);
|
|
651
|
+
}
|
|
652
|
+
console.log(`${c.green}✓${c.reset} Removed context for: qmd://${detected.collectionName}/${detected.relativePath}`);
|
|
653
|
+
}
|
|
654
|
+
function getDocument(filename, fromLine, maxLines, lineNumbers) {
|
|
655
|
+
const db = getDb();
|
|
656
|
+
// Parse :linenum suffix from filename (e.g., "file.md:100")
|
|
657
|
+
let inputPath = filename;
|
|
658
|
+
const colonMatch = inputPath.match(/:(\d+)$/);
|
|
659
|
+
if (colonMatch && !fromLine) {
|
|
660
|
+
const matched = colonMatch[1];
|
|
661
|
+
if (matched) {
|
|
662
|
+
fromLine = parseInt(matched, 10);
|
|
663
|
+
inputPath = inputPath.slice(0, -colonMatch[0].length);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
// Handle docid lookup (#abc123, abc123, "#abc123", "abc123", etc.)
|
|
667
|
+
if (isDocid(inputPath)) {
|
|
668
|
+
const docidMatch = findDocumentByDocid(db, inputPath);
|
|
669
|
+
if (docidMatch) {
|
|
670
|
+
inputPath = docidMatch.filepath;
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
console.error(`Document not found: ${filename}`);
|
|
674
|
+
closeDb();
|
|
675
|
+
process.exit(1);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
let doc = null;
|
|
679
|
+
let virtualPath;
|
|
680
|
+
// Handle virtual paths (qmd://collection/path)
|
|
681
|
+
if (isVirtualPath(inputPath)) {
|
|
682
|
+
const parsed = parseVirtualPath(inputPath);
|
|
683
|
+
if (!parsed) {
|
|
684
|
+
console.error(`Invalid virtual path: ${inputPath}`);
|
|
685
|
+
closeDb();
|
|
686
|
+
process.exit(1);
|
|
687
|
+
}
|
|
688
|
+
// Try exact match on collection + path
|
|
689
|
+
doc = db.prepare(`
|
|
690
|
+
SELECT d.collection as collectionName, d.path, content.doc as body
|
|
691
|
+
FROM documents d
|
|
692
|
+
JOIN content ON content.hash = d.hash
|
|
693
|
+
WHERE d.collection = ? AND d.path = ? AND d.active = 1
|
|
694
|
+
`).get(parsed.collectionName, parsed.path);
|
|
695
|
+
if (!doc) {
|
|
696
|
+
// Try fuzzy match by path ending
|
|
697
|
+
doc = db.prepare(`
|
|
698
|
+
SELECT d.collection as collectionName, d.path, content.doc as body
|
|
699
|
+
FROM documents d
|
|
700
|
+
JOIN content ON content.hash = d.hash
|
|
701
|
+
WHERE d.collection = ? AND d.path LIKE ? AND d.active = 1
|
|
702
|
+
LIMIT 1
|
|
703
|
+
`).get(parsed.collectionName, `%${parsed.path}`);
|
|
704
|
+
}
|
|
705
|
+
virtualPath = inputPath;
|
|
706
|
+
}
|
|
707
|
+
else {
|
|
708
|
+
// Try to interpret as collection/path format first (before filesystem path)
|
|
709
|
+
// If path is relative (no / or ~ prefix), check if first component is a collection name
|
|
710
|
+
if (!inputPath.startsWith('/') && !inputPath.startsWith('~')) {
|
|
711
|
+
const parts = inputPath.split('/');
|
|
712
|
+
if (parts.length >= 2) {
|
|
713
|
+
const possibleCollection = parts[0];
|
|
714
|
+
const possiblePath = parts.slice(1).join('/');
|
|
715
|
+
// Check if this collection exists
|
|
716
|
+
const collExists = possibleCollection ? db.prepare(`
|
|
717
|
+
SELECT 1 FROM documents WHERE collection = ? AND active = 1 LIMIT 1
|
|
718
|
+
`).get(possibleCollection) : null;
|
|
719
|
+
if (collExists) {
|
|
720
|
+
// Try exact match on collection + path
|
|
721
|
+
doc = db.prepare(`
|
|
722
|
+
SELECT d.collection as collectionName, d.path, content.doc as body
|
|
723
|
+
FROM documents d
|
|
724
|
+
JOIN content ON content.hash = d.hash
|
|
725
|
+
WHERE d.collection = ? AND d.path = ? AND d.active = 1
|
|
726
|
+
`).get(possibleCollection || "", possiblePath || "");
|
|
727
|
+
if (!doc) {
|
|
728
|
+
// Try fuzzy match by path ending
|
|
729
|
+
doc = db.prepare(`
|
|
730
|
+
SELECT d.collection as collectionName, d.path, content.doc as body
|
|
731
|
+
FROM documents d
|
|
732
|
+
JOIN content ON content.hash = d.hash
|
|
733
|
+
WHERE d.collection = ? AND d.path LIKE ? AND d.active = 1
|
|
734
|
+
LIMIT 1
|
|
735
|
+
`).get(possibleCollection || "", `%${possiblePath}`);
|
|
736
|
+
}
|
|
737
|
+
if (doc) {
|
|
738
|
+
virtualPath = buildVirtualPath(doc.collectionName, doc.path);
|
|
739
|
+
// Skip the filesystem path handling below
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
// If not found as collection/path, handle as filesystem paths
|
|
745
|
+
if (!doc) {
|
|
746
|
+
let fsPath = inputPath;
|
|
747
|
+
// Expand ~ to home directory
|
|
748
|
+
if (fsPath.startsWith('~/')) {
|
|
749
|
+
fsPath = homedir() + fsPath.slice(1);
|
|
750
|
+
}
|
|
751
|
+
else if (!fsPath.startsWith('/')) {
|
|
752
|
+
// Relative path - resolve from current directory
|
|
753
|
+
fsPath = resolve(getPwd(), fsPath);
|
|
754
|
+
}
|
|
755
|
+
fsPath = getRealPath(fsPath);
|
|
756
|
+
// Try to detect which collection contains this path
|
|
757
|
+
const detected = detectCollectionFromPath(db, fsPath);
|
|
758
|
+
if (detected) {
|
|
759
|
+
// Found collection - query by collection name + relative path
|
|
760
|
+
doc = db.prepare(`
|
|
761
|
+
SELECT d.collection as collectionName, d.path, content.doc as body
|
|
762
|
+
FROM documents d
|
|
763
|
+
JOIN content ON content.hash = d.hash
|
|
764
|
+
WHERE d.collection = ? AND d.path = ? AND d.active = 1
|
|
765
|
+
`).get(detected.collectionName, detected.relativePath);
|
|
766
|
+
}
|
|
767
|
+
// Fuzzy match by filename (last component of path)
|
|
768
|
+
if (!doc) {
|
|
769
|
+
const filename = inputPath.split('/').pop() || inputPath;
|
|
770
|
+
doc = db.prepare(`
|
|
771
|
+
SELECT d.collection as collectionName, d.path, content.doc as body
|
|
772
|
+
FROM documents d
|
|
773
|
+
JOIN content ON content.hash = d.hash
|
|
774
|
+
WHERE d.path LIKE ? AND d.active = 1
|
|
775
|
+
LIMIT 1
|
|
776
|
+
`).get(`%${filename}`);
|
|
777
|
+
}
|
|
778
|
+
if (doc) {
|
|
779
|
+
virtualPath = buildVirtualPath(doc.collectionName, doc.path);
|
|
780
|
+
}
|
|
781
|
+
else {
|
|
782
|
+
virtualPath = inputPath;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
// Ensure doc is not null before proceeding
|
|
787
|
+
if (!doc) {
|
|
788
|
+
console.error(`Document not found: ${filename}`);
|
|
789
|
+
closeDb();
|
|
790
|
+
process.exit(1);
|
|
791
|
+
}
|
|
792
|
+
// Get context for this file
|
|
793
|
+
const context = getContextForPath(db, doc.collectionName, doc.path);
|
|
794
|
+
let output = doc.body;
|
|
795
|
+
const startLine = fromLine || 1;
|
|
796
|
+
// Apply line filtering if specified
|
|
797
|
+
if (fromLine !== undefined || maxLines !== undefined) {
|
|
798
|
+
const lines = output.split('\n');
|
|
799
|
+
const start = startLine - 1; // Convert to 0-indexed
|
|
800
|
+
const end = maxLines !== undefined ? start + maxLines : lines.length;
|
|
801
|
+
output = lines.slice(start, end).join('\n');
|
|
802
|
+
}
|
|
803
|
+
// Add line numbers if requested
|
|
804
|
+
if (lineNumbers) {
|
|
805
|
+
output = addLineNumbers(output, startLine);
|
|
806
|
+
}
|
|
807
|
+
// Output context header if exists
|
|
808
|
+
if (context) {
|
|
809
|
+
console.log(`Folder Context: ${context}\n---\n`);
|
|
810
|
+
}
|
|
811
|
+
console.log(output);
|
|
812
|
+
closeDb();
|
|
813
|
+
}
|
|
814
|
+
// Multi-get: fetch multiple documents by glob pattern or comma-separated list
|
|
815
|
+
function multiGet(pattern, maxLines, maxBytes = DEFAULT_MULTI_GET_MAX_BYTES, format = "cli") {
|
|
816
|
+
const db = getDb();
|
|
817
|
+
// Check if it's a comma-separated list or a glob pattern
|
|
818
|
+
const isCommaSeparated = pattern.includes(',') && !pattern.includes('*') && !pattern.includes('?');
|
|
819
|
+
let files;
|
|
820
|
+
if (isCommaSeparated) {
|
|
821
|
+
// Comma-separated list of files (can be virtual paths or relative paths)
|
|
822
|
+
const names = pattern.split(',').map(s => s.trim()).filter(Boolean);
|
|
823
|
+
files = [];
|
|
824
|
+
for (const name of names) {
|
|
825
|
+
let doc = null;
|
|
826
|
+
// Handle virtual paths
|
|
827
|
+
if (isVirtualPath(name)) {
|
|
828
|
+
const parsed = parseVirtualPath(name);
|
|
829
|
+
if (parsed) {
|
|
830
|
+
// Try exact match on collection + path
|
|
831
|
+
doc = db.prepare(`
|
|
832
|
+
SELECT
|
|
833
|
+
'qmd://' || d.collection || '/' || d.path as virtual_path,
|
|
834
|
+
LENGTH(content.doc) as body_length,
|
|
835
|
+
d.collection,
|
|
836
|
+
d.path
|
|
837
|
+
FROM documents d
|
|
838
|
+
JOIN content ON content.hash = d.hash
|
|
839
|
+
WHERE d.collection = ? AND d.path = ? AND d.active = 1
|
|
840
|
+
`).get(parsed.collectionName, parsed.path);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
else {
|
|
844
|
+
// Try exact match on path
|
|
845
|
+
doc = db.prepare(`
|
|
846
|
+
SELECT
|
|
847
|
+
'qmd://' || d.collection || '/' || d.path as virtual_path,
|
|
848
|
+
LENGTH(content.doc) as body_length,
|
|
849
|
+
d.collection,
|
|
850
|
+
d.path
|
|
851
|
+
FROM documents d
|
|
852
|
+
JOIN content ON content.hash = d.hash
|
|
853
|
+
WHERE d.path = ? AND d.active = 1
|
|
854
|
+
LIMIT 1
|
|
855
|
+
`).get(name);
|
|
856
|
+
// Try suffix match
|
|
857
|
+
if (!doc) {
|
|
858
|
+
doc = db.prepare(`
|
|
859
|
+
SELECT
|
|
860
|
+
'qmd://' || d.collection || '/' || d.path as virtual_path,
|
|
861
|
+
LENGTH(content.doc) as body_length,
|
|
862
|
+
d.collection,
|
|
863
|
+
d.path
|
|
864
|
+
FROM documents d
|
|
865
|
+
JOIN content ON content.hash = d.hash
|
|
866
|
+
WHERE d.path LIKE ? AND d.active = 1
|
|
867
|
+
LIMIT 1
|
|
868
|
+
`).get(`%${name}`);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
if (doc) {
|
|
872
|
+
files.push({
|
|
873
|
+
filepath: doc.virtual_path,
|
|
874
|
+
displayPath: doc.virtual_path,
|
|
875
|
+
bodyLength: doc.body_length,
|
|
876
|
+
collection: doc.collection,
|
|
877
|
+
path: doc.path
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
else {
|
|
881
|
+
console.error(`File not found: ${name}`);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
else {
|
|
886
|
+
// Glob pattern - matchFilesByGlob now returns virtual paths
|
|
887
|
+
files = matchFilesByGlob(db, pattern).map(f => ({
|
|
888
|
+
...f,
|
|
889
|
+
collection: undefined, // Will be fetched later if needed
|
|
890
|
+
path: undefined
|
|
891
|
+
}));
|
|
892
|
+
if (files.length === 0) {
|
|
893
|
+
console.error(`No files matched pattern: ${pattern}`);
|
|
894
|
+
closeDb();
|
|
895
|
+
process.exit(1);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
// Collect results for structured output
|
|
899
|
+
const results = [];
|
|
900
|
+
for (const file of files) {
|
|
901
|
+
// Parse virtual path to get collection info if not already available
|
|
902
|
+
let collection = file.collection;
|
|
903
|
+
let path = file.path;
|
|
904
|
+
if (!collection || !path) {
|
|
905
|
+
const parsed = parseVirtualPath(file.filepath);
|
|
906
|
+
if (parsed) {
|
|
907
|
+
collection = parsed.collectionName;
|
|
908
|
+
path = parsed.path;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
// Get context using collection-scoped function
|
|
912
|
+
const context = collection && path ? getContextForPath(db, collection, path) : null;
|
|
913
|
+
// Check size limit
|
|
914
|
+
if (file.bodyLength > maxBytes) {
|
|
915
|
+
results.push({
|
|
916
|
+
file: file.filepath,
|
|
917
|
+
displayPath: file.displayPath,
|
|
918
|
+
title: file.displayPath.split('/').pop() || file.displayPath,
|
|
919
|
+
body: "",
|
|
920
|
+
context,
|
|
921
|
+
skipped: true,
|
|
922
|
+
skipReason: `File too large (${Math.round(file.bodyLength / 1024)}KB > ${Math.round(maxBytes / 1024)}KB). Use 'qmd get ${file.displayPath}' to retrieve.`,
|
|
923
|
+
});
|
|
924
|
+
continue;
|
|
925
|
+
}
|
|
926
|
+
// Fetch document content using collection and path
|
|
927
|
+
if (!collection || !path)
|
|
928
|
+
continue;
|
|
929
|
+
const doc = db.prepare(`
|
|
930
|
+
SELECT content.doc as body, d.title
|
|
931
|
+
FROM documents d
|
|
932
|
+
JOIN content ON content.hash = d.hash
|
|
933
|
+
WHERE d.collection = ? AND d.path = ? AND d.active = 1
|
|
934
|
+
`).get(collection, path);
|
|
935
|
+
if (!doc)
|
|
936
|
+
continue;
|
|
937
|
+
let body = doc.body;
|
|
938
|
+
// Apply line limit if specified
|
|
939
|
+
if (maxLines !== undefined) {
|
|
940
|
+
const lines = body.split('\n');
|
|
941
|
+
body = lines.slice(0, maxLines).join('\n');
|
|
942
|
+
if (lines.length > maxLines) {
|
|
943
|
+
body += `\n\n[... truncated ${lines.length - maxLines} more lines]`;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
results.push({
|
|
947
|
+
file: file.filepath,
|
|
948
|
+
displayPath: file.displayPath,
|
|
949
|
+
title: doc.title || file.displayPath.split('/').pop() || file.displayPath,
|
|
950
|
+
body,
|
|
951
|
+
context,
|
|
952
|
+
skipped: false,
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
closeDb();
|
|
956
|
+
// Output based on format
|
|
957
|
+
if (format === "json") {
|
|
958
|
+
const output = results.map(r => ({
|
|
959
|
+
file: r.displayPath,
|
|
960
|
+
title: r.title,
|
|
961
|
+
...(r.context && { context: r.context }),
|
|
962
|
+
...(r.skipped ? { skipped: true, reason: r.skipReason } : { body: r.body }),
|
|
963
|
+
}));
|
|
964
|
+
console.log(JSON.stringify(output, null, 2));
|
|
965
|
+
}
|
|
966
|
+
else if (format === "csv") {
|
|
967
|
+
const escapeField = (val) => {
|
|
968
|
+
if (val === null || val === undefined)
|
|
969
|
+
return "";
|
|
970
|
+
const str = String(val);
|
|
971
|
+
if (str.includes(",") || str.includes('"') || str.includes("\n")) {
|
|
972
|
+
return `"${str.replace(/"/g, '""')}"`;
|
|
973
|
+
}
|
|
974
|
+
return str;
|
|
975
|
+
};
|
|
976
|
+
console.log("file,title,context,skipped,body");
|
|
977
|
+
for (const r of results) {
|
|
978
|
+
console.log([r.displayPath, r.title, r.context, r.skipped ? "true" : "false", r.skipped ? r.skipReason : r.body].map(escapeField).join(","));
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
else if (format === "files") {
|
|
982
|
+
for (const r of results) {
|
|
983
|
+
const ctx = r.context ? `,"${r.context.replace(/"/g, '""')}"` : "";
|
|
984
|
+
const status = r.skipped ? "[SKIPPED]" : "";
|
|
985
|
+
console.log(`${r.displayPath}${ctx}${status ? `,${status}` : ""}`);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
else if (format === "md") {
|
|
989
|
+
for (const r of results) {
|
|
990
|
+
console.log(`## ${r.displayPath}\n`);
|
|
991
|
+
if (r.title && r.title !== r.displayPath)
|
|
992
|
+
console.log(`**Title:** ${r.title}\n`);
|
|
993
|
+
if (r.context)
|
|
994
|
+
console.log(`**Context:** ${r.context}\n`);
|
|
995
|
+
if (r.skipped) {
|
|
996
|
+
console.log(`> ${r.skipReason}\n`);
|
|
997
|
+
}
|
|
998
|
+
else {
|
|
999
|
+
console.log("```");
|
|
1000
|
+
console.log(r.body);
|
|
1001
|
+
console.log("```\n");
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
else if (format === "xml") {
|
|
1006
|
+
console.log('<?xml version="1.0" encoding="UTF-8"?>');
|
|
1007
|
+
console.log("<documents>");
|
|
1008
|
+
for (const r of results) {
|
|
1009
|
+
console.log(" <document>");
|
|
1010
|
+
console.log(` <file>${escapeXml(r.displayPath)}</file>`);
|
|
1011
|
+
console.log(` <title>${escapeXml(r.title)}</title>`);
|
|
1012
|
+
if (r.context)
|
|
1013
|
+
console.log(` <context>${escapeXml(r.context)}</context>`);
|
|
1014
|
+
if (r.skipped) {
|
|
1015
|
+
console.log(` <skipped>true</skipped>`);
|
|
1016
|
+
console.log(` <reason>${escapeXml(r.skipReason || "")}</reason>`);
|
|
1017
|
+
}
|
|
1018
|
+
else {
|
|
1019
|
+
console.log(` <body>${escapeXml(r.body)}</body>`);
|
|
1020
|
+
}
|
|
1021
|
+
console.log(" </document>");
|
|
1022
|
+
}
|
|
1023
|
+
console.log("</documents>");
|
|
1024
|
+
}
|
|
1025
|
+
else {
|
|
1026
|
+
// CLI format (default)
|
|
1027
|
+
for (const r of results) {
|
|
1028
|
+
console.log(`\n${'='.repeat(60)}`);
|
|
1029
|
+
console.log(`File: ${r.displayPath}`);
|
|
1030
|
+
console.log(`${'='.repeat(60)}\n`);
|
|
1031
|
+
if (r.skipped) {
|
|
1032
|
+
console.log(`[SKIPPED: ${r.skipReason}]`);
|
|
1033
|
+
continue;
|
|
1034
|
+
}
|
|
1035
|
+
if (r.context) {
|
|
1036
|
+
console.log(`Folder Context: ${r.context}\n---\n`);
|
|
1037
|
+
}
|
|
1038
|
+
console.log(r.body);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
// List files in virtual file tree
|
|
1043
|
+
function listFiles(pathArg) {
|
|
1044
|
+
const db = getDb();
|
|
1045
|
+
if (!pathArg) {
|
|
1046
|
+
// No argument - list all collections
|
|
1047
|
+
const yamlCollections = yamlListCollections();
|
|
1048
|
+
if (yamlCollections.length === 0) {
|
|
1049
|
+
console.log("No collections found. Run 'qmd collection add .' to index files.");
|
|
1050
|
+
closeDb();
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
// Get file counts from database for each collection
|
|
1054
|
+
const collections = yamlCollections.map(coll => {
|
|
1055
|
+
const stats = db.prepare(`
|
|
1056
|
+
SELECT COUNT(*) as file_count
|
|
1057
|
+
FROM documents d
|
|
1058
|
+
WHERE d.collection = ? AND d.active = 1
|
|
1059
|
+
`).get(coll.name);
|
|
1060
|
+
return {
|
|
1061
|
+
name: coll.name,
|
|
1062
|
+
file_count: stats?.file_count || 0
|
|
1063
|
+
};
|
|
1064
|
+
});
|
|
1065
|
+
console.log(`${c.bold}Collections:${c.reset}\n`);
|
|
1066
|
+
for (const coll of collections) {
|
|
1067
|
+
console.log(` ${c.dim}qmd://${c.reset}${c.cyan}${coll.name}/${c.reset} ${c.dim}(${coll.file_count} files)${c.reset}`);
|
|
1068
|
+
}
|
|
1069
|
+
closeDb();
|
|
1070
|
+
return;
|
|
1071
|
+
}
|
|
1072
|
+
// Parse the path argument
|
|
1073
|
+
let collectionName;
|
|
1074
|
+
let pathPrefix = null;
|
|
1075
|
+
if (pathArg.startsWith('qmd://')) {
|
|
1076
|
+
// Virtual path format: qmd://collection/path
|
|
1077
|
+
const parsed = parseVirtualPath(pathArg);
|
|
1078
|
+
if (!parsed) {
|
|
1079
|
+
console.error(`Invalid virtual path: ${pathArg}`);
|
|
1080
|
+
closeDb();
|
|
1081
|
+
process.exit(1);
|
|
1082
|
+
}
|
|
1083
|
+
collectionName = parsed.collectionName;
|
|
1084
|
+
pathPrefix = parsed.path;
|
|
1085
|
+
}
|
|
1086
|
+
else {
|
|
1087
|
+
// Just collection name or collection/path
|
|
1088
|
+
const parts = pathArg.split('/');
|
|
1089
|
+
collectionName = parts[0] || '';
|
|
1090
|
+
if (parts.length > 1) {
|
|
1091
|
+
pathPrefix = parts.slice(1).join('/');
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
// Get the collection
|
|
1095
|
+
const coll = getCollectionFromYaml(collectionName);
|
|
1096
|
+
if (!coll) {
|
|
1097
|
+
console.error(`Collection not found: ${collectionName}`);
|
|
1098
|
+
console.error(`Run 'qmd ls' to see available collections.`);
|
|
1099
|
+
closeDb();
|
|
1100
|
+
process.exit(1);
|
|
1101
|
+
}
|
|
1102
|
+
// List files in the collection with size and modification time
|
|
1103
|
+
let query;
|
|
1104
|
+
let params;
|
|
1105
|
+
if (pathPrefix) {
|
|
1106
|
+
// List files under a specific path
|
|
1107
|
+
query = `
|
|
1108
|
+
SELECT d.path, d.title, d.modified_at, LENGTH(ct.doc) as size
|
|
1109
|
+
FROM documents d
|
|
1110
|
+
JOIN content ct ON d.hash = ct.hash
|
|
1111
|
+
WHERE d.collection = ? AND d.path LIKE ? AND d.active = 1
|
|
1112
|
+
ORDER BY d.path
|
|
1113
|
+
`;
|
|
1114
|
+
params = [coll.name, `${pathPrefix}%`];
|
|
1115
|
+
}
|
|
1116
|
+
else {
|
|
1117
|
+
// List all files in the collection
|
|
1118
|
+
query = `
|
|
1119
|
+
SELECT d.path, d.title, d.modified_at, LENGTH(ct.doc) as size
|
|
1120
|
+
FROM documents d
|
|
1121
|
+
JOIN content ct ON d.hash = ct.hash
|
|
1122
|
+
WHERE d.collection = ? AND d.active = 1
|
|
1123
|
+
ORDER BY d.path
|
|
1124
|
+
`;
|
|
1125
|
+
params = [coll.name];
|
|
1126
|
+
}
|
|
1127
|
+
const files = db.prepare(query).all(...params);
|
|
1128
|
+
if (files.length === 0) {
|
|
1129
|
+
if (pathPrefix) {
|
|
1130
|
+
console.log(`No files found under qmd://${collectionName}/${pathPrefix}`);
|
|
1131
|
+
}
|
|
1132
|
+
else {
|
|
1133
|
+
console.log(`No files found in collection: ${collectionName}`);
|
|
1134
|
+
}
|
|
1135
|
+
closeDb();
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
// Calculate max widths for alignment
|
|
1139
|
+
const maxSize = Math.max(...files.map(f => formatBytes(f.size).length));
|
|
1140
|
+
// Output in ls -l style
|
|
1141
|
+
for (const file of files) {
|
|
1142
|
+
const sizeStr = formatBytes(file.size).padStart(maxSize);
|
|
1143
|
+
const date = new Date(file.modified_at);
|
|
1144
|
+
const timeStr = formatLsTime(date);
|
|
1145
|
+
// Dim the qmd:// prefix, highlight the filename
|
|
1146
|
+
console.log(`${sizeStr} ${timeStr} ${c.dim}qmd://${collectionName}/${c.reset}${c.cyan}${file.path}${c.reset}`);
|
|
1147
|
+
}
|
|
1148
|
+
closeDb();
|
|
1149
|
+
}
|
|
1150
|
+
// Format date/time like ls -l
|
|
1151
|
+
function formatLsTime(date) {
|
|
1152
|
+
const now = new Date();
|
|
1153
|
+
const sixMonthsAgo = new Date(now.getTime() - 6 * 30 * 24 * 60 * 60 * 1000);
|
|
1154
|
+
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
1155
|
+
const month = months[date.getMonth()];
|
|
1156
|
+
const day = date.getDate().toString().padStart(2, ' ');
|
|
1157
|
+
// If file is older than 6 months, show year instead of time
|
|
1158
|
+
if (date < sixMonthsAgo) {
|
|
1159
|
+
const year = date.getFullYear();
|
|
1160
|
+
return `${month} ${day} ${year}`;
|
|
1161
|
+
}
|
|
1162
|
+
else {
|
|
1163
|
+
const hours = date.getHours().toString().padStart(2, '0');
|
|
1164
|
+
const minutes = date.getMinutes().toString().padStart(2, '0');
|
|
1165
|
+
return `${month} ${day} ${hours}:${minutes}`;
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
// Collection management commands
|
|
1169
|
+
function collectionList() {
|
|
1170
|
+
const db = getDb();
|
|
1171
|
+
const collections = listCollections(db);
|
|
1172
|
+
if (collections.length === 0) {
|
|
1173
|
+
console.log("No collections found. Run 'qmd collection add .' to create one.");
|
|
1174
|
+
closeDb();
|
|
1175
|
+
return;
|
|
1176
|
+
}
|
|
1177
|
+
console.log(`${c.bold}Collections (${collections.length}):${c.reset}\n`);
|
|
1178
|
+
for (const coll of collections) {
|
|
1179
|
+
const updatedAt = coll.last_modified ? new Date(coll.last_modified) : new Date();
|
|
1180
|
+
const timeAgo = formatTimeAgo(updatedAt);
|
|
1181
|
+
// Get YAML config to check includeByDefault
|
|
1182
|
+
const yamlColl = getCollectionFromYaml(coll.name);
|
|
1183
|
+
const excluded = yamlColl?.includeByDefault === false;
|
|
1184
|
+
const excludeTag = excluded ? ` ${c.yellow}[excluded]${c.reset}` : '';
|
|
1185
|
+
console.log(`${c.cyan}${coll.name}${c.reset} ${c.dim}(qmd://${coll.name}/)${c.reset}${excludeTag}`);
|
|
1186
|
+
console.log(` ${c.dim}Pattern:${c.reset} ${coll.glob_pattern}`);
|
|
1187
|
+
if (yamlColl?.ignore?.length) {
|
|
1188
|
+
console.log(` ${c.dim}Ignore:${c.reset} ${yamlColl.ignore.join(', ')}`);
|
|
1189
|
+
}
|
|
1190
|
+
console.log(` ${c.dim}Files:${c.reset} ${coll.active_count}`);
|
|
1191
|
+
console.log(` ${c.dim}Updated:${c.reset} ${timeAgo}`);
|
|
1192
|
+
console.log();
|
|
1193
|
+
}
|
|
1194
|
+
closeDb();
|
|
1195
|
+
}
|
|
1196
|
+
async function collectionAdd(pwd, globPattern, name) {
|
|
1197
|
+
// If name not provided, generate from pwd basename
|
|
1198
|
+
let collName = name;
|
|
1199
|
+
if (!collName) {
|
|
1200
|
+
const parts = pwd.split('/').filter(Boolean);
|
|
1201
|
+
collName = parts[parts.length - 1] || 'root';
|
|
1202
|
+
}
|
|
1203
|
+
// Check if collection with this name already exists in YAML
|
|
1204
|
+
const existing = getCollectionFromYaml(collName);
|
|
1205
|
+
if (existing) {
|
|
1206
|
+
console.error(`${c.yellow}Collection '${collName}' already exists.${c.reset}`);
|
|
1207
|
+
console.error(`Use a different name with --name <name>`);
|
|
1208
|
+
process.exit(1);
|
|
1209
|
+
}
|
|
1210
|
+
// Check if a collection with this pwd+glob already exists in YAML
|
|
1211
|
+
const allCollections = yamlListCollections();
|
|
1212
|
+
const existingPwdGlob = allCollections.find(c => c.path === pwd && c.pattern === globPattern);
|
|
1213
|
+
if (existingPwdGlob) {
|
|
1214
|
+
console.error(`${c.yellow}A collection already exists for this path and pattern:${c.reset}`);
|
|
1215
|
+
console.error(` Name: ${existingPwdGlob.name} (qmd://${existingPwdGlob.name}/)`);
|
|
1216
|
+
console.error(` Pattern: ${globPattern}`);
|
|
1217
|
+
console.error(`\nUse 'qmd update' to re-index it, or remove it first with 'qmd collection remove ${existingPwdGlob.name}'`);
|
|
1218
|
+
process.exit(1);
|
|
1219
|
+
}
|
|
1220
|
+
// Add to YAML config + sync to SQLite
|
|
1221
|
+
const { addCollection } = await import("../collections.js");
|
|
1222
|
+
addCollection(collName, pwd, globPattern);
|
|
1223
|
+
resyncConfig();
|
|
1224
|
+
// Create the collection and index files
|
|
1225
|
+
console.log(`Creating collection '${collName}'...`);
|
|
1226
|
+
const newColl = getCollectionFromYaml(collName);
|
|
1227
|
+
await indexFiles(pwd, globPattern, collName, false, newColl?.ignore);
|
|
1228
|
+
console.log(`${c.green}✓${c.reset} Collection '${collName}' created successfully`);
|
|
1229
|
+
}
|
|
1230
|
+
function collectionRemove(name) {
|
|
1231
|
+
// Check if collection exists in YAML
|
|
1232
|
+
const coll = getCollectionFromYaml(name);
|
|
1233
|
+
if (!coll) {
|
|
1234
|
+
console.error(`${c.yellow}Collection not found: ${name}${c.reset}`);
|
|
1235
|
+
console.error(`Run 'qmd collection list' to see available collections.`);
|
|
1236
|
+
process.exit(1);
|
|
1237
|
+
}
|
|
1238
|
+
const db = getDb();
|
|
1239
|
+
const result = removeCollection(db, name);
|
|
1240
|
+
// Also remove from YAML config
|
|
1241
|
+
yamlRemoveCollectionFn(name);
|
|
1242
|
+
closeDb();
|
|
1243
|
+
console.log(`${c.green}✓${c.reset} Removed collection '${name}'`);
|
|
1244
|
+
console.log(` Deleted ${result.deletedDocs} documents`);
|
|
1245
|
+
if (result.cleanedHashes > 0) {
|
|
1246
|
+
console.log(` Cleaned up ${result.cleanedHashes} orphaned content hashes`);
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
function collectionRename(oldName, newName) {
|
|
1250
|
+
// Check if old collection exists in YAML
|
|
1251
|
+
const coll = getCollectionFromYaml(oldName);
|
|
1252
|
+
if (!coll) {
|
|
1253
|
+
console.error(`${c.yellow}Collection not found: ${oldName}${c.reset}`);
|
|
1254
|
+
console.error(`Run 'qmd collection list' to see available collections.`);
|
|
1255
|
+
process.exit(1);
|
|
1256
|
+
}
|
|
1257
|
+
// Check if new name already exists in YAML
|
|
1258
|
+
const existing = getCollectionFromYaml(newName);
|
|
1259
|
+
if (existing) {
|
|
1260
|
+
console.error(`${c.yellow}Collection name already exists: ${newName}${c.reset}`);
|
|
1261
|
+
console.error(`Choose a different name or remove the existing collection first.`);
|
|
1262
|
+
process.exit(1);
|
|
1263
|
+
}
|
|
1264
|
+
const db = getDb();
|
|
1265
|
+
renameCollection(db, oldName, newName);
|
|
1266
|
+
// Also rename in YAML config
|
|
1267
|
+
yamlRenameCollectionFn(oldName, newName);
|
|
1268
|
+
closeDb();
|
|
1269
|
+
console.log(`${c.green}✓${c.reset} Renamed collection '${oldName}' to '${newName}'`);
|
|
1270
|
+
console.log(` Virtual paths updated: ${c.cyan}qmd://${oldName}/${c.reset} → ${c.cyan}qmd://${newName}/${c.reset}`);
|
|
1271
|
+
}
|
|
1272
|
+
async function indexFiles(pwd, globPattern = DEFAULT_GLOB, collectionName, suppressEmbedNotice = false, ignorePatterns) {
|
|
1273
|
+
const db = getDb();
|
|
1274
|
+
const resolvedPwd = pwd || getPwd();
|
|
1275
|
+
const now = new Date().toISOString();
|
|
1276
|
+
const excludeDirs = ["node_modules", ".git", ".cache", "vendor", "dist", "build"];
|
|
1277
|
+
// Clear Ollama cache on index
|
|
1278
|
+
clearCache(db);
|
|
1279
|
+
// Collection name must be provided (from YAML)
|
|
1280
|
+
if (!collectionName) {
|
|
1281
|
+
throw new Error("Collection name is required. Collections must be defined in ~/.config/qmd/index.yml");
|
|
1282
|
+
}
|
|
1283
|
+
console.log(`Collection: ${resolvedPwd} (${globPattern})`);
|
|
1284
|
+
progress.indeterminate();
|
|
1285
|
+
const allIgnore = [
|
|
1286
|
+
...excludeDirs.map(d => `**/${d}/**`),
|
|
1287
|
+
...(ignorePatterns || []),
|
|
1288
|
+
];
|
|
1289
|
+
const allFiles = await fastGlob(globPattern, {
|
|
1290
|
+
cwd: resolvedPwd,
|
|
1291
|
+
onlyFiles: true,
|
|
1292
|
+
followSymbolicLinks: false,
|
|
1293
|
+
dot: false,
|
|
1294
|
+
ignore: allIgnore,
|
|
1295
|
+
});
|
|
1296
|
+
// Filter hidden files/folders (dot: false handles top-level but not nested)
|
|
1297
|
+
const files = allFiles.filter(file => {
|
|
1298
|
+
const parts = file.split("/");
|
|
1299
|
+
return !parts.some(part => part.startsWith("."));
|
|
1300
|
+
});
|
|
1301
|
+
const total = files.length;
|
|
1302
|
+
const hasNoFiles = total === 0;
|
|
1303
|
+
if (hasNoFiles) {
|
|
1304
|
+
progress.clear();
|
|
1305
|
+
console.log("No files found matching pattern.");
|
|
1306
|
+
// Continue so the deactivation pass can mark previously indexed docs as inactive.
|
|
1307
|
+
}
|
|
1308
|
+
let indexed = 0, updated = 0, unchanged = 0, processed = 0;
|
|
1309
|
+
const seenPaths = new Set();
|
|
1310
|
+
const startTime = Date.now();
|
|
1311
|
+
for (const relativeFile of files) {
|
|
1312
|
+
const filepath = getRealPath(resolve(resolvedPwd, relativeFile));
|
|
1313
|
+
const path = handelize(relativeFile); // Normalize path for token-friendliness
|
|
1314
|
+
seenPaths.add(path);
|
|
1315
|
+
let content;
|
|
1316
|
+
try {
|
|
1317
|
+
content = readFileSync(filepath, "utf-8");
|
|
1318
|
+
}
|
|
1319
|
+
catch (err) {
|
|
1320
|
+
// Skip files that can't be read (e.g. iCloud evicted files returning EAGAIN)
|
|
1321
|
+
processed++;
|
|
1322
|
+
progress.set((processed / total) * 100);
|
|
1323
|
+
continue;
|
|
1324
|
+
}
|
|
1325
|
+
// Skip empty files - nothing useful to index
|
|
1326
|
+
if (!content.trim()) {
|
|
1327
|
+
processed++;
|
|
1328
|
+
continue;
|
|
1329
|
+
}
|
|
1330
|
+
const hash = await hashContent(content);
|
|
1331
|
+
const title = extractTitle(content, relativeFile);
|
|
1332
|
+
// Check if document exists in this collection with this path
|
|
1333
|
+
const existing = findActiveDocument(db, collectionName, path);
|
|
1334
|
+
if (existing) {
|
|
1335
|
+
if (existing.hash === hash) {
|
|
1336
|
+
// Hash unchanged, but check if title needs updating
|
|
1337
|
+
if (existing.title !== title) {
|
|
1338
|
+
updateDocumentTitle(db, existing.id, title, now);
|
|
1339
|
+
updated++;
|
|
1340
|
+
}
|
|
1341
|
+
else {
|
|
1342
|
+
unchanged++;
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
else {
|
|
1346
|
+
// Content changed - insert new content hash and update document
|
|
1347
|
+
insertContent(db, hash, content, now);
|
|
1348
|
+
const stat = statSync(filepath);
|
|
1349
|
+
updateDocument(db, existing.id, title, hash, stat ? new Date(stat.mtime).toISOString() : now);
|
|
1350
|
+
updated++;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
else {
|
|
1354
|
+
// New document - insert content and document
|
|
1355
|
+
indexed++;
|
|
1356
|
+
insertContent(db, hash, content, now);
|
|
1357
|
+
const stat = statSync(filepath);
|
|
1358
|
+
insertDocument(db, collectionName, path, title, hash, stat ? new Date(stat.birthtime).toISOString() : now, stat ? new Date(stat.mtime).toISOString() : now);
|
|
1359
|
+
}
|
|
1360
|
+
processed++;
|
|
1361
|
+
progress.set((processed / total) * 100);
|
|
1362
|
+
const elapsed = (Date.now() - startTime) / 1000;
|
|
1363
|
+
const rate = processed / elapsed;
|
|
1364
|
+
const remaining = (total - processed) / rate;
|
|
1365
|
+
const eta = processed > 2 ? ` ETA: ${formatETA(remaining)}` : "";
|
|
1366
|
+
if (isTTY)
|
|
1367
|
+
process.stderr.write(`\rIndexing: ${processed}/${total}${eta} `);
|
|
1368
|
+
}
|
|
1369
|
+
// Deactivate documents in this collection that no longer exist
|
|
1370
|
+
const allActive = getActiveDocumentPaths(db, collectionName);
|
|
1371
|
+
let removed = 0;
|
|
1372
|
+
for (const path of allActive) {
|
|
1373
|
+
if (!seenPaths.has(path)) {
|
|
1374
|
+
deactivateDocument(db, collectionName, path);
|
|
1375
|
+
removed++;
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
// Clean up orphaned content hashes (content not referenced by any document)
|
|
1379
|
+
const orphanedContent = cleanupOrphanedContent(db);
|
|
1380
|
+
// Check if vector index needs updating
|
|
1381
|
+
const needsEmbedding = getHashesNeedingEmbedding(db);
|
|
1382
|
+
progress.clear();
|
|
1383
|
+
console.log(`\nIndexed: ${indexed} new, ${updated} updated, ${unchanged} unchanged, ${removed} removed`);
|
|
1384
|
+
if (orphanedContent > 0) {
|
|
1385
|
+
console.log(`Cleaned up ${orphanedContent} orphaned content hash(es)`);
|
|
1386
|
+
}
|
|
1387
|
+
if (needsEmbedding > 0 && !suppressEmbedNotice) {
|
|
1388
|
+
console.log(`\nRun 'qmd embed' to update embeddings (${needsEmbedding} unique hashes need vectors)`);
|
|
1389
|
+
}
|
|
1390
|
+
closeDb();
|
|
1391
|
+
}
|
|
1392
|
+
function renderProgressBar(percent, width = 30) {
|
|
1393
|
+
const filled = Math.round((percent / 100) * width);
|
|
1394
|
+
const empty = width - filled;
|
|
1395
|
+
const bar = "█".repeat(filled) + "░".repeat(empty);
|
|
1396
|
+
return bar;
|
|
1397
|
+
}
|
|
1398
|
+
function parseEmbedBatchOption(name, value) {
|
|
1399
|
+
if (value === undefined)
|
|
1400
|
+
return undefined;
|
|
1401
|
+
const parsed = Number(value);
|
|
1402
|
+
if (!Number.isInteger(parsed) || parsed < 1) {
|
|
1403
|
+
throw new Error(`${name} must be a positive integer`);
|
|
1404
|
+
}
|
|
1405
|
+
return parsed;
|
|
1406
|
+
}
|
|
1407
|
+
function parseChunkStrategy(value) {
|
|
1408
|
+
if (value === undefined)
|
|
1409
|
+
return undefined;
|
|
1410
|
+
const s = String(value);
|
|
1411
|
+
if (s === "auto" || s === "regex")
|
|
1412
|
+
return s;
|
|
1413
|
+
throw new Error(`--chunk-strategy must be "auto" or "regex" (got "${s}")`);
|
|
1414
|
+
}
|
|
1415
|
+
async function vectorIndex(model = DEFAULT_EMBED_MODEL, force = false, batchOptions) {
|
|
1416
|
+
const storeInstance = getStore();
|
|
1417
|
+
const db = storeInstance.db;
|
|
1418
|
+
if (force) {
|
|
1419
|
+
console.log(`${c.yellow}Force re-indexing: clearing all vectors...${c.reset}`);
|
|
1420
|
+
}
|
|
1421
|
+
// Check if there's work to do before starting
|
|
1422
|
+
const hashesToEmbed = getHashesNeedingEmbedding(db);
|
|
1423
|
+
if (hashesToEmbed === 0 && !force) {
|
|
1424
|
+
console.log(`${c.green}✓ All content hashes already have embeddings.${c.reset}`);
|
|
1425
|
+
closeDb();
|
|
1426
|
+
return;
|
|
1427
|
+
}
|
|
1428
|
+
console.log(`${c.dim}Model: ${model}${c.reset}\n`);
|
|
1429
|
+
if (batchOptions?.maxDocsPerBatch !== undefined || batchOptions?.maxBatchBytes !== undefined) {
|
|
1430
|
+
const maxDocsPerBatch = batchOptions.maxDocsPerBatch ?? DEFAULT_EMBED_MAX_DOCS_PER_BATCH;
|
|
1431
|
+
const maxBatchBytes = batchOptions.maxBatchBytes ?? DEFAULT_EMBED_MAX_BATCH_BYTES;
|
|
1432
|
+
console.log(`${c.dim}Batch: ${maxDocsPerBatch} docs / ${formatBytes(maxBatchBytes)}${c.reset}\n`);
|
|
1433
|
+
}
|
|
1434
|
+
cursor.hide();
|
|
1435
|
+
progress.indeterminate();
|
|
1436
|
+
const startTime = Date.now();
|
|
1437
|
+
const result = await generateEmbeddings(storeInstance, {
|
|
1438
|
+
force,
|
|
1439
|
+
model,
|
|
1440
|
+
maxDocsPerBatch: batchOptions?.maxDocsPerBatch,
|
|
1441
|
+
maxBatchBytes: batchOptions?.maxBatchBytes,
|
|
1442
|
+
chunkStrategy: batchOptions?.chunkStrategy,
|
|
1443
|
+
onProgress: (info) => {
|
|
1444
|
+
if (info.totalBytes === 0)
|
|
1445
|
+
return;
|
|
1446
|
+
const percent = (info.bytesProcessed / info.totalBytes) * 100;
|
|
1447
|
+
progress.set(percent);
|
|
1448
|
+
const elapsed = (Date.now() - startTime) / 1000;
|
|
1449
|
+
const bytesPerSec = info.bytesProcessed / elapsed;
|
|
1450
|
+
const remainingBytes = info.totalBytes - info.bytesProcessed;
|
|
1451
|
+
const etaSec = remainingBytes / bytesPerSec;
|
|
1452
|
+
const bar = renderProgressBar(percent);
|
|
1453
|
+
const percentStr = percent.toFixed(0).padStart(3);
|
|
1454
|
+
const throughput = `${formatBytes(bytesPerSec)}/s`;
|
|
1455
|
+
const eta = elapsed > 2 ? formatETA(etaSec) : "...";
|
|
1456
|
+
const errStr = info.errors > 0 ? ` ${c.yellow}${info.errors} err${c.reset}` : "";
|
|
1457
|
+
if (isTTY)
|
|
1458
|
+
process.stderr.write(`\r${c.cyan}${bar}${c.reset} ${c.bold}${percentStr}%${c.reset} ${c.dim}${info.chunksEmbedded}/${info.totalChunks}${c.reset}${errStr} ${c.dim}${throughput} ETA ${eta}${c.reset} `);
|
|
1459
|
+
},
|
|
1460
|
+
});
|
|
1461
|
+
progress.clear();
|
|
1462
|
+
cursor.show();
|
|
1463
|
+
const totalTimeSec = result.durationMs / 1000;
|
|
1464
|
+
if (result.chunksEmbedded === 0 && result.docsProcessed === 0) {
|
|
1465
|
+
console.log(`${c.green}✓ No non-empty documents to embed.${c.reset}`);
|
|
1466
|
+
}
|
|
1467
|
+
else {
|
|
1468
|
+
console.log(`\r${c.green}${renderProgressBar(100)}${c.reset} ${c.bold}100%${c.reset} `);
|
|
1469
|
+
console.log(`\n${c.green}✓ Done!${c.reset} Embedded ${c.bold}${result.chunksEmbedded}${c.reset} chunks from ${c.bold}${result.docsProcessed}${c.reset} documents in ${c.bold}${formatETA(totalTimeSec)}${c.reset}`);
|
|
1470
|
+
if (result.errors > 0) {
|
|
1471
|
+
console.log(`${c.yellow}⚠ ${result.errors} chunks failed${c.reset}`);
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
closeDb();
|
|
1475
|
+
}
|
|
1476
|
+
// Sanitize a term for FTS5: remove punctuation except apostrophes
|
|
1477
|
+
function sanitizeFTS5Term(term) {
|
|
1478
|
+
// Remove all non-alphanumeric except apostrophes (for contractions like "don't")
|
|
1479
|
+
return term.replace(/[^\w']/g, '').trim();
|
|
1480
|
+
}
|
|
1481
|
+
// Build FTS5 query: phrase-aware with fallback to individual terms
|
|
1482
|
+
function buildFTS5Query(query) {
|
|
1483
|
+
// Sanitize the full query for phrase matching
|
|
1484
|
+
const sanitizedQuery = query.replace(/[^\w\s']/g, '').trim();
|
|
1485
|
+
const terms = query
|
|
1486
|
+
.split(/\s+/)
|
|
1487
|
+
.map(sanitizeFTS5Term)
|
|
1488
|
+
.filter(term => term.length >= 2); // Skip single chars and empty
|
|
1489
|
+
if (terms.length === 0)
|
|
1490
|
+
return "";
|
|
1491
|
+
if (terms.length === 1)
|
|
1492
|
+
return `"${terms[0].replace(/"/g, '""')}"`;
|
|
1493
|
+
// Strategy: exact phrase OR proximity match OR individual terms
|
|
1494
|
+
// Exact phrase matches rank highest, then close proximity, then any term
|
|
1495
|
+
const phrase = `"${sanitizedQuery.replace(/"/g, '""')}"`;
|
|
1496
|
+
const quotedTerms = terms.map(t => `"${t.replace(/"/g, '""')}"`);
|
|
1497
|
+
// FTS5 NEAR syntax: NEAR(term1 term2, distance)
|
|
1498
|
+
const nearPhrase = `NEAR(${quotedTerms.join(' ')}, 10)`;
|
|
1499
|
+
const orTerms = quotedTerms.join(' OR ');
|
|
1500
|
+
// Exact phrase > proximity > any term
|
|
1501
|
+
return `(${phrase}) OR (${nearPhrase}) OR (${orTerms})`;
|
|
1502
|
+
}
|
|
1503
|
+
// Normalize BM25 score to 0-1 range using sigmoid
|
|
1504
|
+
function normalizeBM25(score) {
|
|
1505
|
+
// BM25 scores are negative in SQLite (lower = better)
|
|
1506
|
+
// Typical range: -15 (excellent) to -2 (weak match)
|
|
1507
|
+
// Map to 0-1 where higher is better
|
|
1508
|
+
const absScore = Math.abs(score);
|
|
1509
|
+
// Sigmoid-ish normalization: maps ~2-15 range to ~0.1-0.95
|
|
1510
|
+
return 1 / (1 + Math.exp(-(absScore - 5) / 3));
|
|
1511
|
+
}
|
|
1512
|
+
// Highlight query terms in text (skip short words < 3 chars)
|
|
1513
|
+
function highlightTerms(text, query) {
|
|
1514
|
+
if (!useColor)
|
|
1515
|
+
return text;
|
|
1516
|
+
const terms = query.toLowerCase().split(/\s+/).filter(t => t.length >= 3);
|
|
1517
|
+
let result = text;
|
|
1518
|
+
for (const term of terms) {
|
|
1519
|
+
const regex = new RegExp(`(${term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
|
|
1520
|
+
result = result.replace(regex, `${c.yellow}${c.bold}$1${c.reset}`);
|
|
1521
|
+
}
|
|
1522
|
+
return result;
|
|
1523
|
+
}
|
|
1524
|
+
// Format score with color based on value
|
|
1525
|
+
function formatScore(score) {
|
|
1526
|
+
const pct = (score * 100).toFixed(0).padStart(3);
|
|
1527
|
+
if (!useColor)
|
|
1528
|
+
return `${pct}%`;
|
|
1529
|
+
if (score >= 0.7)
|
|
1530
|
+
return `${c.green}${pct}%${c.reset}`;
|
|
1531
|
+
if (score >= 0.4)
|
|
1532
|
+
return `${c.yellow}${pct}%${c.reset}`;
|
|
1533
|
+
return `${c.dim}${pct}%${c.reset}`;
|
|
1534
|
+
}
|
|
1535
|
+
function formatExplainNumber(value) {
|
|
1536
|
+
return value.toFixed(4);
|
|
1537
|
+
}
|
|
1538
|
+
// Shorten directory path for display - relative to $HOME (used for context paths, not documents)
|
|
1539
|
+
function shortPath(dirpath) {
|
|
1540
|
+
const home = homedir();
|
|
1541
|
+
if (dirpath.startsWith(home)) {
|
|
1542
|
+
return '~' + dirpath.slice(home.length);
|
|
1543
|
+
}
|
|
1544
|
+
return dirpath;
|
|
1545
|
+
}
|
|
1546
|
+
// Emit format-safe empty output for search commands.
|
|
1547
|
+
function printEmptySearchResults(format, reason = "no_results") {
|
|
1548
|
+
if (format === "json") {
|
|
1549
|
+
console.log("[]");
|
|
1550
|
+
return;
|
|
1551
|
+
}
|
|
1552
|
+
if (format === "csv") {
|
|
1553
|
+
console.log("docid,score,file,title,context,line,snippet");
|
|
1554
|
+
return;
|
|
1555
|
+
}
|
|
1556
|
+
if (format === "xml") {
|
|
1557
|
+
console.log("<results></results>");
|
|
1558
|
+
return;
|
|
1559
|
+
}
|
|
1560
|
+
if (format === "md" || format === "files") {
|
|
1561
|
+
return;
|
|
1562
|
+
}
|
|
1563
|
+
if (reason === "min_score") {
|
|
1564
|
+
console.log("No results found above minimum score threshold.");
|
|
1565
|
+
return;
|
|
1566
|
+
}
|
|
1567
|
+
console.log("No results found.");
|
|
1568
|
+
}
|
|
1569
|
+
function outputResults(results, query, opts) {
|
|
1570
|
+
const filtered = results.filter(r => r.score >= opts.minScore).slice(0, opts.limit);
|
|
1571
|
+
if (filtered.length === 0) {
|
|
1572
|
+
printEmptySearchResults(opts.format, "min_score");
|
|
1573
|
+
return;
|
|
1574
|
+
}
|
|
1575
|
+
// Helper to create qmd:// URI from displayPath
|
|
1576
|
+
const toQmdPath = (displayPath) => `qmd://${displayPath}`;
|
|
1577
|
+
if (opts.format === "json") {
|
|
1578
|
+
// JSON output for LLM consumption
|
|
1579
|
+
const output = filtered.map(row => {
|
|
1580
|
+
const docid = row.docid || (row.hash ? row.hash.slice(0, 6) : undefined);
|
|
1581
|
+
let body = opts.full ? row.body : undefined;
|
|
1582
|
+
let snippet = !opts.full ? extractSnippet(row.body, query, 300, row.chunkPos, undefined, opts.intent).snippet : undefined;
|
|
1583
|
+
if (opts.lineNumbers) {
|
|
1584
|
+
if (body)
|
|
1585
|
+
body = addLineNumbers(body);
|
|
1586
|
+
if (snippet)
|
|
1587
|
+
snippet = addLineNumbers(snippet);
|
|
1588
|
+
}
|
|
1589
|
+
return {
|
|
1590
|
+
...(docid && { docid: `#${docid}` }),
|
|
1591
|
+
score: Math.round(row.score * 100) / 100,
|
|
1592
|
+
file: toQmdPath(row.displayPath),
|
|
1593
|
+
title: row.title,
|
|
1594
|
+
...(row.context && { context: row.context }),
|
|
1595
|
+
...(body && { body }),
|
|
1596
|
+
...(snippet && { snippet }),
|
|
1597
|
+
...(opts.explain && row.explain && { explain: row.explain }),
|
|
1598
|
+
};
|
|
1599
|
+
});
|
|
1600
|
+
console.log(JSON.stringify(output, null, 2));
|
|
1601
|
+
}
|
|
1602
|
+
else if (opts.format === "files") {
|
|
1603
|
+
// Simple docid,score,filepath,context output
|
|
1604
|
+
for (const row of filtered) {
|
|
1605
|
+
const docid = row.docid || (row.hash ? row.hash.slice(0, 6) : "");
|
|
1606
|
+
const ctx = row.context ? `,"${row.context.replace(/"/g, '""')}"` : "";
|
|
1607
|
+
console.log(`#${docid},${row.score.toFixed(2)},${toQmdPath(row.displayPath)}${ctx}`);
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
else if (opts.format === "cli") {
|
|
1611
|
+
for (let i = 0; i < filtered.length; i++) {
|
|
1612
|
+
const row = filtered[i];
|
|
1613
|
+
if (!row)
|
|
1614
|
+
continue;
|
|
1615
|
+
const { line, snippet } = extractSnippet(row.body, query, 500, row.chunkPos, undefined, opts.intent);
|
|
1616
|
+
const docid = row.docid || (row.hash ? row.hash.slice(0, 6) : undefined);
|
|
1617
|
+
// Line 1: filepath with docid
|
|
1618
|
+
const path = toQmdPath(row.displayPath);
|
|
1619
|
+
// Only show :line if we actually found a term match in the snippet body (exclude header line).
|
|
1620
|
+
const snippetBody = snippet.split("\n").slice(1).join("\n").toLowerCase();
|
|
1621
|
+
const hasMatch = query.toLowerCase().split(/\s+/).some(t => t.length > 0 && snippetBody.includes(t));
|
|
1622
|
+
const lineInfo = hasMatch ? `:${line}` : "";
|
|
1623
|
+
const docidStr = docid ? ` ${c.dim}#${docid}${c.reset}` : "";
|
|
1624
|
+
console.log(`${c.cyan}${path}${c.dim}${lineInfo}${c.reset}${docidStr}`);
|
|
1625
|
+
// Line 2: Title (if available)
|
|
1626
|
+
if (row.title) {
|
|
1627
|
+
console.log(`${c.bold}Title: ${row.title}${c.reset}`);
|
|
1628
|
+
}
|
|
1629
|
+
// Line 3: Context (if available)
|
|
1630
|
+
if (row.context) {
|
|
1631
|
+
console.log(`${c.dim}Context: ${row.context}${c.reset}`);
|
|
1632
|
+
}
|
|
1633
|
+
// Line 4: Score
|
|
1634
|
+
const score = formatScore(row.score);
|
|
1635
|
+
console.log(`Score: ${c.bold}${score}${c.reset}`);
|
|
1636
|
+
if (opts.explain && row.explain) {
|
|
1637
|
+
const explain = row.explain;
|
|
1638
|
+
const ftsScores = explain.ftsScores.length > 0
|
|
1639
|
+
? explain.ftsScores.map(formatExplainNumber).join(", ")
|
|
1640
|
+
: "none";
|
|
1641
|
+
const vecScores = explain.vectorScores.length > 0
|
|
1642
|
+
? explain.vectorScores.map(formatExplainNumber).join(", ")
|
|
1643
|
+
: "none";
|
|
1644
|
+
const contribSummary = explain.rrf.contributions
|
|
1645
|
+
.slice()
|
|
1646
|
+
.sort((a, b) => b.rrfContribution - a.rrfContribution)
|
|
1647
|
+
.slice(0, 3)
|
|
1648
|
+
.map(c => `${c.source}/${c.queryType}#${c.rank}:${formatExplainNumber(c.rrfContribution)}`)
|
|
1649
|
+
.join(" | ");
|
|
1650
|
+
console.log(`${c.dim}Explain: fts=[${ftsScores}] vec=[${vecScores}]${c.reset}`);
|
|
1651
|
+
console.log(`${c.dim} RRF: total=${formatExplainNumber(explain.rrf.totalScore)} base=${formatExplainNumber(explain.rrf.baseScore)} bonus=${formatExplainNumber(explain.rrf.topRankBonus)} rank=${explain.rrf.rank}${c.reset}`);
|
|
1652
|
+
console.log(`${c.dim} Blend: ${Math.round(explain.rrf.weight * 100)}%*${formatExplainNumber(explain.rrf.positionScore)} + ${Math.round((1 - explain.rrf.weight) * 100)}%*${formatExplainNumber(explain.rerankScore)} = ${formatExplainNumber(explain.blendedScore)}${c.reset}`);
|
|
1653
|
+
if (contribSummary.length > 0) {
|
|
1654
|
+
console.log(`${c.dim} Top RRF contributions: ${contribSummary}${c.reset}`);
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
console.log();
|
|
1658
|
+
// Snippet with highlighting (diff-style header included)
|
|
1659
|
+
let displaySnippet = opts.lineNumbers ? addLineNumbers(snippet, line) : snippet;
|
|
1660
|
+
const highlighted = highlightTerms(displaySnippet, query);
|
|
1661
|
+
console.log(highlighted);
|
|
1662
|
+
// Double empty line between results
|
|
1663
|
+
if (i < filtered.length - 1)
|
|
1664
|
+
console.log('\n');
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
else if (opts.format === "md") {
|
|
1668
|
+
for (let i = 0; i < filtered.length; i++) {
|
|
1669
|
+
const row = filtered[i];
|
|
1670
|
+
if (!row)
|
|
1671
|
+
continue;
|
|
1672
|
+
const heading = row.title || row.displayPath;
|
|
1673
|
+
const docid = row.docid || (row.hash ? row.hash.slice(0, 6) : undefined);
|
|
1674
|
+
let content = opts.full ? row.body : extractSnippet(row.body, query, 500, row.chunkPos, undefined, opts.intent).snippet;
|
|
1675
|
+
if (opts.lineNumbers) {
|
|
1676
|
+
content = addLineNumbers(content);
|
|
1677
|
+
}
|
|
1678
|
+
const docidLine = docid ? `**docid:** \`#${docid}\`\n` : "";
|
|
1679
|
+
const contextLine = row.context ? `**context:** ${row.context}\n` : "";
|
|
1680
|
+
console.log(`---\n# ${heading}\n${docidLine}${contextLine}\n${content}\n`);
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
else if (opts.format === "xml") {
|
|
1684
|
+
for (const row of filtered) {
|
|
1685
|
+
const titleAttr = row.title ? ` title="${row.title.replace(/"/g, '"')}"` : "";
|
|
1686
|
+
const contextAttr = row.context ? ` context="${row.context.replace(/"/g, '"')}"` : "";
|
|
1687
|
+
const docid = row.docid || (row.hash ? row.hash.slice(0, 6) : "");
|
|
1688
|
+
let content = opts.full ? row.body : extractSnippet(row.body, query, 500, row.chunkPos, undefined, opts.intent).snippet;
|
|
1689
|
+
if (opts.lineNumbers) {
|
|
1690
|
+
content = addLineNumbers(content);
|
|
1691
|
+
}
|
|
1692
|
+
console.log(`<file docid="#${docid}" name="${toQmdPath(row.displayPath)}"${titleAttr}${contextAttr}>\n${content}\n</file>\n`);
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
else {
|
|
1696
|
+
// CSV format
|
|
1697
|
+
console.log("docid,score,file,title,context,line,snippet");
|
|
1698
|
+
for (const row of filtered) {
|
|
1699
|
+
const { line, snippet } = extractSnippet(row.body, query, 500, row.chunkPos, undefined, opts.intent);
|
|
1700
|
+
let content = opts.full ? row.body : snippet;
|
|
1701
|
+
if (opts.lineNumbers) {
|
|
1702
|
+
content = addLineNumbers(content, line);
|
|
1703
|
+
}
|
|
1704
|
+
const docid = row.docid || (row.hash ? row.hash.slice(0, 6) : "");
|
|
1705
|
+
const snippetText = content || "";
|
|
1706
|
+
console.log(`#${docid},${row.score.toFixed(4)},${escapeCSV(toQmdPath(row.displayPath))},${escapeCSV(row.title || "")},${escapeCSV(row.context || "")},${line},${escapeCSV(snippetText)}`);
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
// Resolve -c collection filter: supports single string, array, or undefined.
|
|
1711
|
+
// Returns validated collection names (exits on unknown collection).
|
|
1712
|
+
function resolveCollectionFilter(raw, useDefaults = false) {
|
|
1713
|
+
// If no filter specified and useDefaults is true, use default collections
|
|
1714
|
+
if (!raw && useDefaults) {
|
|
1715
|
+
return getDefaultCollectionNames();
|
|
1716
|
+
}
|
|
1717
|
+
if (!raw)
|
|
1718
|
+
return [];
|
|
1719
|
+
const names = Array.isArray(raw) ? raw : [raw];
|
|
1720
|
+
const validated = [];
|
|
1721
|
+
for (const name of names) {
|
|
1722
|
+
const coll = getCollectionFromYaml(name);
|
|
1723
|
+
if (!coll) {
|
|
1724
|
+
console.error(`Collection not found: ${name}`);
|
|
1725
|
+
closeDb();
|
|
1726
|
+
process.exit(1);
|
|
1727
|
+
}
|
|
1728
|
+
validated.push(name);
|
|
1729
|
+
}
|
|
1730
|
+
return validated;
|
|
1731
|
+
}
|
|
1732
|
+
// Post-filter results to only include files from specified collections.
|
|
1733
|
+
function filterByCollections(results, collectionNames) {
|
|
1734
|
+
if (collectionNames.length <= 1)
|
|
1735
|
+
return results;
|
|
1736
|
+
const prefixes = collectionNames.map(n => `qmd://${n}/`);
|
|
1737
|
+
return results.filter(r => {
|
|
1738
|
+
const path = r.filepath || r.file || '';
|
|
1739
|
+
return prefixes.some(p => path.startsWith(p));
|
|
1740
|
+
});
|
|
1741
|
+
}
|
|
1742
|
+
function parseStructuredQuery(query) {
|
|
1743
|
+
const rawLines = query.split('\n').map((line, idx) => ({
|
|
1744
|
+
raw: line,
|
|
1745
|
+
trimmed: line.trim(),
|
|
1746
|
+
number: idx + 1,
|
|
1747
|
+
})).filter(line => line.trimmed.length > 0);
|
|
1748
|
+
if (rawLines.length === 0)
|
|
1749
|
+
return null;
|
|
1750
|
+
const prefixRe = /^(lex|vec|hyde):\s*/i;
|
|
1751
|
+
const expandRe = /^expand:\s*/i;
|
|
1752
|
+
const intentRe = /^intent:\s*/i;
|
|
1753
|
+
const typed = [];
|
|
1754
|
+
let intent;
|
|
1755
|
+
for (const line of rawLines) {
|
|
1756
|
+
if (expandRe.test(line.trimmed)) {
|
|
1757
|
+
if (rawLines.length > 1) {
|
|
1758
|
+
throw new Error(`Line ${line.number} starts with expand:, but query documents cannot mix expand with typed lines. Submit a single expand query instead.`);
|
|
1759
|
+
}
|
|
1760
|
+
const text = line.trimmed.replace(expandRe, '').trim();
|
|
1761
|
+
if (!text) {
|
|
1762
|
+
throw new Error('expand: query must include text.');
|
|
1763
|
+
}
|
|
1764
|
+
return null; // treat as standalone expand query
|
|
1765
|
+
}
|
|
1766
|
+
// Parse intent: lines
|
|
1767
|
+
if (intentRe.test(line.trimmed)) {
|
|
1768
|
+
if (intent !== undefined) {
|
|
1769
|
+
throw new Error(`Line ${line.number}: only one intent: line is allowed per query document.`);
|
|
1770
|
+
}
|
|
1771
|
+
const text = line.trimmed.replace(intentRe, '').trim();
|
|
1772
|
+
if (!text) {
|
|
1773
|
+
throw new Error(`Line ${line.number}: intent: must include text.`);
|
|
1774
|
+
}
|
|
1775
|
+
intent = text;
|
|
1776
|
+
continue;
|
|
1777
|
+
}
|
|
1778
|
+
const match = line.trimmed.match(prefixRe);
|
|
1779
|
+
if (match) {
|
|
1780
|
+
const type = match[1].toLowerCase();
|
|
1781
|
+
const text = line.trimmed.slice(match[0].length).trim();
|
|
1782
|
+
if (!text) {
|
|
1783
|
+
throw new Error(`Line ${line.number} (${type}:) must include text.`);
|
|
1784
|
+
}
|
|
1785
|
+
if (/\r|\n/.test(text)) {
|
|
1786
|
+
throw new Error(`Line ${line.number} (${type}:) contains a newline. Keep each query on a single line.`);
|
|
1787
|
+
}
|
|
1788
|
+
typed.push({ type, query: text, line: line.number });
|
|
1789
|
+
continue;
|
|
1790
|
+
}
|
|
1791
|
+
if (rawLines.length === 1) {
|
|
1792
|
+
// Single plain line -> implicit expand
|
|
1793
|
+
return null;
|
|
1794
|
+
}
|
|
1795
|
+
throw new Error(`Line ${line.number} is missing a lex:/vec:/hyde:/intent: prefix. Each line in a query document must start with one.`);
|
|
1796
|
+
}
|
|
1797
|
+
// intent: alone is not a valid query — must have at least one search
|
|
1798
|
+
if (intent && typed.length === 0) {
|
|
1799
|
+
throw new Error('intent: cannot appear alone. Add at least one lex:, vec:, or hyde: line.');
|
|
1800
|
+
}
|
|
1801
|
+
return typed.length > 0 ? { searches: typed, intent } : null;
|
|
1802
|
+
}
|
|
1803
|
+
function search(query, opts) {
|
|
1804
|
+
const db = getDb();
|
|
1805
|
+
// Validate collection filter (supports multiple -c flags)
|
|
1806
|
+
// Use default collections if none specified
|
|
1807
|
+
const collectionNames = resolveCollectionFilter(opts.collection, true);
|
|
1808
|
+
const singleCollection = collectionNames.length === 1 ? collectionNames[0] : undefined;
|
|
1809
|
+
// Use large limit for --all, otherwise fetch more than needed and let outputResults filter
|
|
1810
|
+
const fetchLimit = opts.all ? 100000 : Math.max(50, opts.limit * 2);
|
|
1811
|
+
const results = filterByCollections(searchFTS(db, query, fetchLimit, singleCollection), collectionNames);
|
|
1812
|
+
// Add context to results
|
|
1813
|
+
const resultsWithContext = results.map(r => ({
|
|
1814
|
+
file: r.filepath,
|
|
1815
|
+
displayPath: r.displayPath,
|
|
1816
|
+
title: r.title,
|
|
1817
|
+
body: r.body || "",
|
|
1818
|
+
score: r.score,
|
|
1819
|
+
context: getContextForFile(db, r.filepath),
|
|
1820
|
+
hash: r.hash,
|
|
1821
|
+
docid: r.docid,
|
|
1822
|
+
}));
|
|
1823
|
+
closeDb();
|
|
1824
|
+
if (resultsWithContext.length === 0) {
|
|
1825
|
+
printEmptySearchResults(opts.format);
|
|
1826
|
+
return;
|
|
1827
|
+
}
|
|
1828
|
+
outputResults(resultsWithContext, query, opts);
|
|
1829
|
+
}
|
|
1830
|
+
// Log query expansion as a tree to stderr (CLI progress feedback)
|
|
1831
|
+
function logExpansionTree(originalQuery, expanded) {
|
|
1832
|
+
const lines = [];
|
|
1833
|
+
lines.push(`${c.dim}├─ ${originalQuery}${c.reset}`);
|
|
1834
|
+
for (const q of expanded) {
|
|
1835
|
+
let preview = q.query.replace(/\n/g, ' ');
|
|
1836
|
+
if (preview.length > 72)
|
|
1837
|
+
preview = preview.substring(0, 69) + '...';
|
|
1838
|
+
lines.push(`${c.dim}├─ ${q.type}: ${preview}${c.reset}`);
|
|
1839
|
+
}
|
|
1840
|
+
if (lines.length > 0) {
|
|
1841
|
+
lines[lines.length - 1] = lines[lines.length - 1].replace('├─', '└─');
|
|
1842
|
+
}
|
|
1843
|
+
for (const line of lines)
|
|
1844
|
+
process.stderr.write(line + '\n');
|
|
1845
|
+
}
|
|
1846
|
+
async function vectorSearch(query, opts, _model = DEFAULT_EMBED_MODEL) {
|
|
1847
|
+
const store = getStore();
|
|
1848
|
+
// Validate collection filter (supports multiple -c flags)
|
|
1849
|
+
// Use default collections if none specified
|
|
1850
|
+
const collectionNames = resolveCollectionFilter(opts.collection, true);
|
|
1851
|
+
const singleCollection = collectionNames.length === 1 ? collectionNames[0] : undefined;
|
|
1852
|
+
checkIndexHealth(store.db);
|
|
1853
|
+
await withLLMSession(async () => {
|
|
1854
|
+
let results = await vectorSearchQuery(store, query, {
|
|
1855
|
+
collection: singleCollection,
|
|
1856
|
+
limit: opts.all ? 500 : (opts.limit || 10),
|
|
1857
|
+
minScore: opts.minScore || 0.3,
|
|
1858
|
+
intent: opts.intent,
|
|
1859
|
+
hooks: {
|
|
1860
|
+
onExpand: (original, expanded) => {
|
|
1861
|
+
logExpansionTree(original, expanded);
|
|
1862
|
+
process.stderr.write(`${c.dim}Searching ${expanded.length + 1} vector queries...${c.reset}\n`);
|
|
1863
|
+
},
|
|
1864
|
+
},
|
|
1865
|
+
});
|
|
1866
|
+
// Post-filter for multi-collection
|
|
1867
|
+
if (collectionNames.length > 1) {
|
|
1868
|
+
results = results.filter(r => {
|
|
1869
|
+
const prefixes = collectionNames.map(n => `qmd://${n}/`);
|
|
1870
|
+
return prefixes.some(p => r.file.startsWith(p));
|
|
1871
|
+
});
|
|
1872
|
+
}
|
|
1873
|
+
closeDb();
|
|
1874
|
+
if (results.length === 0) {
|
|
1875
|
+
printEmptySearchResults(opts.format);
|
|
1876
|
+
return;
|
|
1877
|
+
}
|
|
1878
|
+
outputResults(results.map(r => ({
|
|
1879
|
+
file: r.file,
|
|
1880
|
+
displayPath: r.displayPath,
|
|
1881
|
+
title: r.title,
|
|
1882
|
+
body: r.body,
|
|
1883
|
+
score: r.score,
|
|
1884
|
+
context: r.context,
|
|
1885
|
+
docid: r.docid,
|
|
1886
|
+
})), query, { ...opts, limit: results.length });
|
|
1887
|
+
}, { maxDuration: 10 * 60 * 1000, name: 'vectorSearch' });
|
|
1888
|
+
}
|
|
1889
|
+
async function querySearch(query, opts, _embedModel = DEFAULT_EMBED_MODEL, _rerankModel = DEFAULT_RERANK_MODEL) {
|
|
1890
|
+
const store = getStore();
|
|
1891
|
+
// Validate collection filter (supports multiple -c flags)
|
|
1892
|
+
// Use default collections if none specified
|
|
1893
|
+
const collectionNames = resolveCollectionFilter(opts.collection, true);
|
|
1894
|
+
const singleCollection = collectionNames.length === 1 ? collectionNames[0] : undefined;
|
|
1895
|
+
checkIndexHealth(store.db);
|
|
1896
|
+
// Check for structured query syntax (lex:/vec:/hyde:/intent: prefixes)
|
|
1897
|
+
const parsed = parseStructuredQuery(query);
|
|
1898
|
+
// Intent can come from --intent flag or from intent: line in query document
|
|
1899
|
+
const intent = opts.intent || parsed?.intent;
|
|
1900
|
+
await withLLMSession(async () => {
|
|
1901
|
+
let results;
|
|
1902
|
+
if (parsed) {
|
|
1903
|
+
const structuredQueries = parsed.searches;
|
|
1904
|
+
// Structured search — user provided their own query expansions
|
|
1905
|
+
const typeLabels = structuredQueries.map(s => s.type).join('+');
|
|
1906
|
+
process.stderr.write(`${c.dim}Structured search: ${structuredQueries.length} queries (${typeLabels})${c.reset}\n`);
|
|
1907
|
+
if (intent) {
|
|
1908
|
+
process.stderr.write(`${c.dim}├─ intent: ${intent}${c.reset}\n`);
|
|
1909
|
+
}
|
|
1910
|
+
// Log each sub-query
|
|
1911
|
+
for (const s of structuredQueries) {
|
|
1912
|
+
let preview = s.query.replace(/\n/g, ' ');
|
|
1913
|
+
if (preview.length > 72)
|
|
1914
|
+
preview = preview.substring(0, 69) + '...';
|
|
1915
|
+
process.stderr.write(`${c.dim}├─ ${s.type}: ${preview}${c.reset}\n`);
|
|
1916
|
+
}
|
|
1917
|
+
process.stderr.write(`${c.dim}└─ Searching...${c.reset}\n`);
|
|
1918
|
+
results = await structuredSearch(store, structuredQueries, {
|
|
1919
|
+
collections: singleCollection ? [singleCollection] : undefined,
|
|
1920
|
+
limit: opts.all ? 500 : (opts.limit || 10),
|
|
1921
|
+
minScore: opts.minScore || 0,
|
|
1922
|
+
candidateLimit: opts.candidateLimit,
|
|
1923
|
+
skipRerank: opts.skipRerank,
|
|
1924
|
+
explain: !!opts.explain,
|
|
1925
|
+
intent,
|
|
1926
|
+
chunkStrategy: opts.chunkStrategy,
|
|
1927
|
+
hooks: {
|
|
1928
|
+
onEmbedStart: (count) => {
|
|
1929
|
+
process.stderr.write(`${c.dim}Embedding ${count} ${count === 1 ? 'query' : 'queries'}...${c.reset}`);
|
|
1930
|
+
},
|
|
1931
|
+
onEmbedDone: (ms) => {
|
|
1932
|
+
process.stderr.write(`${c.dim} (${formatMs(ms)})${c.reset}\n`);
|
|
1933
|
+
},
|
|
1934
|
+
onRerankStart: (chunkCount) => {
|
|
1935
|
+
process.stderr.write(`${c.dim}Reranking ${chunkCount} chunks...${c.reset}`);
|
|
1936
|
+
progress.indeterminate();
|
|
1937
|
+
},
|
|
1938
|
+
onRerankDone: (ms) => {
|
|
1939
|
+
progress.clear();
|
|
1940
|
+
process.stderr.write(`${c.dim} (${formatMs(ms)})${c.reset}\n`);
|
|
1941
|
+
},
|
|
1942
|
+
},
|
|
1943
|
+
});
|
|
1944
|
+
}
|
|
1945
|
+
else {
|
|
1946
|
+
// Standard hybrid query with automatic expansion
|
|
1947
|
+
results = await hybridQuery(store, query, {
|
|
1948
|
+
collection: singleCollection,
|
|
1949
|
+
limit: opts.all ? 500 : (opts.limit || 10),
|
|
1950
|
+
minScore: opts.minScore || 0,
|
|
1951
|
+
candidateLimit: opts.candidateLimit,
|
|
1952
|
+
skipRerank: opts.skipRerank,
|
|
1953
|
+
explain: !!opts.explain,
|
|
1954
|
+
intent,
|
|
1955
|
+
chunkStrategy: opts.chunkStrategy,
|
|
1956
|
+
hooks: {
|
|
1957
|
+
onStrongSignal: (score) => {
|
|
1958
|
+
process.stderr.write(`${c.dim}Strong BM25 signal (${score.toFixed(2)}) — skipping expansion${c.reset}\n`);
|
|
1959
|
+
},
|
|
1960
|
+
onExpandStart: () => {
|
|
1961
|
+
process.stderr.write(`${c.dim}Expanding query...${c.reset}`);
|
|
1962
|
+
},
|
|
1963
|
+
onExpand: (original, expanded, ms) => {
|
|
1964
|
+
process.stderr.write(`${c.dim} (${formatMs(ms)})${c.reset}\n`);
|
|
1965
|
+
logExpansionTree(original, expanded);
|
|
1966
|
+
process.stderr.write(`${c.dim}Searching ${expanded.length + 1} queries...${c.reset}\n`);
|
|
1967
|
+
},
|
|
1968
|
+
onEmbedStart: (count) => {
|
|
1969
|
+
process.stderr.write(`${c.dim}Embedding ${count} ${count === 1 ? 'query' : 'queries'}...${c.reset}`);
|
|
1970
|
+
},
|
|
1971
|
+
onEmbedDone: (ms) => {
|
|
1972
|
+
process.stderr.write(`${c.dim} (${formatMs(ms)})${c.reset}\n`);
|
|
1973
|
+
},
|
|
1974
|
+
onRerankStart: (chunkCount) => {
|
|
1975
|
+
process.stderr.write(`${c.dim}Reranking ${chunkCount} chunks...${c.reset}`);
|
|
1976
|
+
progress.indeterminate();
|
|
1977
|
+
},
|
|
1978
|
+
onRerankDone: (ms) => {
|
|
1979
|
+
progress.clear();
|
|
1980
|
+
process.stderr.write(`${c.dim} (${formatMs(ms)})${c.reset}\n`);
|
|
1981
|
+
},
|
|
1982
|
+
},
|
|
1983
|
+
});
|
|
1984
|
+
}
|
|
1985
|
+
// Post-filter for multi-collection
|
|
1986
|
+
if (collectionNames.length > 1) {
|
|
1987
|
+
results = results.filter(r => {
|
|
1988
|
+
const prefixes = collectionNames.map(n => `qmd://${n}/`);
|
|
1989
|
+
return prefixes.some(p => r.file.startsWith(p));
|
|
1990
|
+
});
|
|
1991
|
+
}
|
|
1992
|
+
closeDb();
|
|
1993
|
+
if (results.length === 0) {
|
|
1994
|
+
printEmptySearchResults(opts.format);
|
|
1995
|
+
return;
|
|
1996
|
+
}
|
|
1997
|
+
// Use first lex/vec query for output context, or original query
|
|
1998
|
+
const structuredQueries = parsed?.searches;
|
|
1999
|
+
const displayQuery = structuredQueries
|
|
2000
|
+
? (structuredQueries.find(s => s.type === 'lex')?.query || structuredQueries.find(s => s.type === 'vec')?.query || query)
|
|
2001
|
+
: query;
|
|
2002
|
+
// Map to CLI output format — use bestChunk for snippet display
|
|
2003
|
+
outputResults(results.map(r => ({
|
|
2004
|
+
file: r.file,
|
|
2005
|
+
displayPath: r.displayPath,
|
|
2006
|
+
title: r.title,
|
|
2007
|
+
body: r.bestChunk,
|
|
2008
|
+
chunkPos: r.bestChunkPos,
|
|
2009
|
+
score: r.score,
|
|
2010
|
+
context: r.context,
|
|
2011
|
+
docid: r.docid,
|
|
2012
|
+
explain: r.explain,
|
|
2013
|
+
})), displayQuery, { ...opts, limit: results.length });
|
|
2014
|
+
}, { maxDuration: 10 * 60 * 1000, name: 'querySearch' });
|
|
2015
|
+
}
|
|
2016
|
+
// Parse CLI arguments using util.parseArgs
|
|
2017
|
+
function parseCLI() {
|
|
2018
|
+
const { values, positionals } = parseArgs({
|
|
2019
|
+
args: process.argv.slice(2), // Skip node and script path
|
|
2020
|
+
options: {
|
|
2021
|
+
// Global options
|
|
2022
|
+
index: {
|
|
2023
|
+
type: "string",
|
|
2024
|
+
},
|
|
2025
|
+
context: {
|
|
2026
|
+
type: "string",
|
|
2027
|
+
},
|
|
2028
|
+
help: { type: "boolean", short: "h" },
|
|
2029
|
+
version: { type: "boolean", short: "v" },
|
|
2030
|
+
skill: { type: "boolean" },
|
|
2031
|
+
global: { type: "boolean" },
|
|
2032
|
+
yes: { type: "boolean" },
|
|
2033
|
+
// Search options
|
|
2034
|
+
n: { type: "string" },
|
|
2035
|
+
"min-score": { type: "string" },
|
|
2036
|
+
all: { type: "boolean" },
|
|
2037
|
+
full: { type: "boolean" },
|
|
2038
|
+
csv: { type: "boolean" },
|
|
2039
|
+
md: { type: "boolean" },
|
|
2040
|
+
xml: { type: "boolean" },
|
|
2041
|
+
files: { type: "boolean" },
|
|
2042
|
+
json: { type: "boolean" },
|
|
2043
|
+
explain: { type: "boolean" },
|
|
2044
|
+
collection: { type: "string", short: "c", multiple: true }, // Filter by collection(s)
|
|
2045
|
+
// Collection options
|
|
2046
|
+
name: { type: "string" }, // collection name
|
|
2047
|
+
mask: { type: "string" }, // glob pattern
|
|
2048
|
+
// Embed options
|
|
2049
|
+
force: { type: "boolean", short: "f" },
|
|
2050
|
+
"max-docs-per-batch": { type: "string" },
|
|
2051
|
+
"max-batch-mb": { type: "string" },
|
|
2052
|
+
// Update options
|
|
2053
|
+
pull: { type: "boolean" }, // git pull before update
|
|
2054
|
+
refresh: { type: "boolean" },
|
|
2055
|
+
// Get options
|
|
2056
|
+
l: { type: "string" }, // max lines
|
|
2057
|
+
from: { type: "string" }, // start line
|
|
2058
|
+
"max-bytes": { type: "string" }, // max bytes for multi-get
|
|
2059
|
+
"line-numbers": { type: "boolean" }, // add line numbers to output
|
|
2060
|
+
// Query options
|
|
2061
|
+
"candidate-limit": { type: "string", short: "C" },
|
|
2062
|
+
"no-rerank": { type: "boolean", default: false },
|
|
2063
|
+
intent: { type: "string" },
|
|
2064
|
+
// Chunking options
|
|
2065
|
+
"chunk-strategy": { type: "string" }, // "regex" (default) or "auto" (AST for code files)
|
|
2066
|
+
// MCP HTTP transport options
|
|
2067
|
+
http: { type: "boolean" },
|
|
2068
|
+
daemon: { type: "boolean" },
|
|
2069
|
+
port: { type: "string" },
|
|
2070
|
+
},
|
|
2071
|
+
allowPositionals: true,
|
|
2072
|
+
strict: false, // Allow unknown options to pass through
|
|
2073
|
+
});
|
|
2074
|
+
// Select index name (default: "index")
|
|
2075
|
+
const indexName = values.index;
|
|
2076
|
+
if (indexName) {
|
|
2077
|
+
setIndexName(indexName);
|
|
2078
|
+
setConfigIndexName(indexName);
|
|
2079
|
+
}
|
|
2080
|
+
// Determine output format
|
|
2081
|
+
let format = "cli";
|
|
2082
|
+
if (values.csv)
|
|
2083
|
+
format = "csv";
|
|
2084
|
+
else if (values.md)
|
|
2085
|
+
format = "md";
|
|
2086
|
+
else if (values.xml)
|
|
2087
|
+
format = "xml";
|
|
2088
|
+
else if (values.files)
|
|
2089
|
+
format = "files";
|
|
2090
|
+
else if (values.json)
|
|
2091
|
+
format = "json";
|
|
2092
|
+
// Default limit: 20 for --files/--json, 5 otherwise
|
|
2093
|
+
// --all means return all results (use very large limit)
|
|
2094
|
+
const defaultLimit = (format === "files" || format === "json") ? 20 : 5;
|
|
2095
|
+
const isAll = !!values.all;
|
|
2096
|
+
const opts = {
|
|
2097
|
+
format,
|
|
2098
|
+
full: !!values.full,
|
|
2099
|
+
limit: isAll ? 100000 : (values.n ? parseInt(String(values.n), 10) || defaultLimit : defaultLimit),
|
|
2100
|
+
minScore: values["min-score"] ? parseFloat(String(values["min-score"])) || 0 : 0,
|
|
2101
|
+
all: isAll,
|
|
2102
|
+
collection: values.collection,
|
|
2103
|
+
lineNumbers: !!values["line-numbers"],
|
|
2104
|
+
candidateLimit: values["candidate-limit"] ? parseInt(String(values["candidate-limit"]), 10) : undefined,
|
|
2105
|
+
skipRerank: !!values["no-rerank"],
|
|
2106
|
+
explain: !!values.explain,
|
|
2107
|
+
intent: values.intent,
|
|
2108
|
+
chunkStrategy: parseChunkStrategy(values["chunk-strategy"]),
|
|
2109
|
+
};
|
|
2110
|
+
return {
|
|
2111
|
+
command: positionals[0] || "",
|
|
2112
|
+
args: positionals.slice(1),
|
|
2113
|
+
query: positionals.slice(1).join(" "),
|
|
2114
|
+
opts,
|
|
2115
|
+
values,
|
|
2116
|
+
};
|
|
2117
|
+
}
|
|
2118
|
+
function getSkillInstallDir(globalInstall) {
|
|
2119
|
+
return globalInstall
|
|
2120
|
+
? resolve(homedir(), ".agents", "skills", "qmd")
|
|
2121
|
+
: resolve(getPwd(), ".agents", "skills", "qmd");
|
|
2122
|
+
}
|
|
2123
|
+
function getClaudeSkillLinkPath(globalInstall) {
|
|
2124
|
+
return globalInstall
|
|
2125
|
+
? resolve(homedir(), ".claude", "skills", "qmd")
|
|
2126
|
+
: resolve(getPwd(), ".claude", "skills", "qmd");
|
|
2127
|
+
}
|
|
2128
|
+
function pathExists(path) {
|
|
2129
|
+
try {
|
|
2130
|
+
lstatSync(path);
|
|
2131
|
+
return true;
|
|
2132
|
+
}
|
|
2133
|
+
catch {
|
|
2134
|
+
return false;
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
function removePath(path) {
|
|
2138
|
+
const stat = lstatSync(path);
|
|
2139
|
+
if (stat.isDirectory() && !stat.isSymbolicLink()) {
|
|
2140
|
+
rmSync(path, { recursive: true, force: true });
|
|
2141
|
+
}
|
|
2142
|
+
else {
|
|
2143
|
+
unlinkSync(path);
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
function showSkill() {
|
|
2147
|
+
console.log("QMD Skill (embedded)");
|
|
2148
|
+
console.log("");
|
|
2149
|
+
const content = getEmbeddedQmdSkillContent();
|
|
2150
|
+
process.stdout.write(content.endsWith("\n") ? content : content + "\n");
|
|
2151
|
+
}
|
|
2152
|
+
function writeEmbeddedSkill(targetDir, force) {
|
|
2153
|
+
if (pathExists(targetDir)) {
|
|
2154
|
+
if (!force) {
|
|
2155
|
+
throw new Error(`Skill already exists: ${targetDir} (use --force to replace it)`);
|
|
2156
|
+
}
|
|
2157
|
+
removePath(targetDir);
|
|
2158
|
+
}
|
|
2159
|
+
mkdirSync(targetDir, { recursive: true });
|
|
2160
|
+
for (const file of getEmbeddedQmdSkillFiles()) {
|
|
2161
|
+
const destination = resolve(targetDir, file.relativePath);
|
|
2162
|
+
mkdirSync(dirname(destination), { recursive: true });
|
|
2163
|
+
writeFileSync(destination, file.content, "utf-8");
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
function ensureClaudeSymlink(linkPath, targetDir, force) {
|
|
2167
|
+
const parentDir = dirname(linkPath);
|
|
2168
|
+
if (pathExists(parentDir)) {
|
|
2169
|
+
const resolvedTargetDir = realpathSync(dirname(targetDir));
|
|
2170
|
+
const resolvedLinkParent = realpathSync(parentDir);
|
|
2171
|
+
// If .claude/skills already resolves to the same directory as .agents/skills,
|
|
2172
|
+
// the skill is already visible to Claude and creating qmd -> qmd would loop.
|
|
2173
|
+
if (resolvedTargetDir === resolvedLinkParent) {
|
|
2174
|
+
return false;
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
const linkTarget = relativePath(parentDir, targetDir) || ".";
|
|
2178
|
+
mkdirSync(parentDir, { recursive: true });
|
|
2179
|
+
if (pathExists(linkPath)) {
|
|
2180
|
+
const stat = lstatSync(linkPath);
|
|
2181
|
+
if (stat.isSymbolicLink() && readlinkSync(linkPath) === linkTarget) {
|
|
2182
|
+
return true;
|
|
2183
|
+
}
|
|
2184
|
+
if (!force) {
|
|
2185
|
+
throw new Error(`Claude skill path already exists: ${linkPath} (use --force to replace it)`);
|
|
2186
|
+
}
|
|
2187
|
+
removePath(linkPath);
|
|
2188
|
+
}
|
|
2189
|
+
symlinkSync(linkTarget, linkPath, "dir");
|
|
2190
|
+
return true;
|
|
2191
|
+
}
|
|
2192
|
+
async function shouldCreateClaudeSymlink(linkPath, autoYes) {
|
|
2193
|
+
if (autoYes) {
|
|
2194
|
+
return true;
|
|
2195
|
+
}
|
|
2196
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
2197
|
+
console.log(`Tip: create a Claude symlink manually at ${linkPath}`);
|
|
2198
|
+
return false;
|
|
2199
|
+
}
|
|
2200
|
+
const rl = createInterface({
|
|
2201
|
+
input: process.stdin,
|
|
2202
|
+
output: process.stdout,
|
|
2203
|
+
});
|
|
2204
|
+
try {
|
|
2205
|
+
const answer = await rl.question(`Create a symlink in ${linkPath}? [y/N] `);
|
|
2206
|
+
const normalized = answer.trim().toLowerCase();
|
|
2207
|
+
return normalized === "y" || normalized === "yes";
|
|
2208
|
+
}
|
|
2209
|
+
finally {
|
|
2210
|
+
rl.close();
|
|
2211
|
+
}
|
|
2212
|
+
}
|
|
2213
|
+
async function installSkill(globalInstall, force, autoYes) {
|
|
2214
|
+
const installDir = getSkillInstallDir(globalInstall);
|
|
2215
|
+
writeEmbeddedSkill(installDir, force);
|
|
2216
|
+
console.log(`✓ Installed QMD skill to ${installDir}`);
|
|
2217
|
+
const claudeLinkPath = getClaudeSkillLinkPath(globalInstall);
|
|
2218
|
+
if (!(await shouldCreateClaudeSymlink(claudeLinkPath, autoYes))) {
|
|
2219
|
+
return;
|
|
2220
|
+
}
|
|
2221
|
+
const linked = ensureClaudeSymlink(claudeLinkPath, installDir, force);
|
|
2222
|
+
if (linked) {
|
|
2223
|
+
console.log(`✓ Linked Claude skill at ${claudeLinkPath}`);
|
|
2224
|
+
}
|
|
2225
|
+
else {
|
|
2226
|
+
console.log(`✓ Claude already sees the skill via ${dirname(claudeLinkPath)}`);
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
function showHelp() {
|
|
2230
|
+
console.log("qmd — Quick Markdown Search");
|
|
2231
|
+
console.log("");
|
|
2232
|
+
console.log("Usage:");
|
|
2233
|
+
console.log(" qmd <command> [options]");
|
|
2234
|
+
console.log("");
|
|
2235
|
+
console.log("Primary commands:");
|
|
2236
|
+
console.log(" qmd query <query> - Hybrid search with auto expansion + reranking (recommended)");
|
|
2237
|
+
console.log(" qmd query 'lex:..\\nvec:...' - Structured query document (you provide lex/vec/hyde lines)");
|
|
2238
|
+
console.log(" qmd search <query> - Full-text BM25 keywords (no LLM)");
|
|
2239
|
+
console.log(" qmd vsearch <query> - Vector similarity only");
|
|
2240
|
+
console.log(" qmd get <file>[:line] [-l N] - Show a single document, optional line slice");
|
|
2241
|
+
console.log(" qmd multi-get <pattern> - Batch fetch via glob or comma-separated list");
|
|
2242
|
+
console.log(" qmd skill show/install - Show or install the packaged QMD skill");
|
|
2243
|
+
console.log(" qmd mcp - Start the MCP server (stdio transport for AI agents)");
|
|
2244
|
+
console.log("");
|
|
2245
|
+
console.log("Collections & context:");
|
|
2246
|
+
console.log(" qmd collection add/list/remove/rename/show - Manage indexed folders");
|
|
2247
|
+
console.log(" qmd context add/list/rm - Attach human-written summaries");
|
|
2248
|
+
console.log(" qmd ls [collection[/path]] - Inspect indexed files");
|
|
2249
|
+
console.log("");
|
|
2250
|
+
console.log("Maintenance:");
|
|
2251
|
+
console.log(" qmd status - View index + collection health");
|
|
2252
|
+
console.log(" qmd update [--pull] - Re-index collections (optionally git pull first)");
|
|
2253
|
+
console.log(" qmd embed [-f] - Generate/refresh vector embeddings");
|
|
2254
|
+
console.log(" --max-docs-per-batch <n> - Cap docs loaded into memory per embedding batch");
|
|
2255
|
+
console.log(" --max-batch-mb <n> - Cap UTF-8 MB loaded into memory per embedding batch");
|
|
2256
|
+
console.log(" qmd cleanup - Clear caches, vacuum DB");
|
|
2257
|
+
console.log("");
|
|
2258
|
+
console.log("Query syntax (qmd query):");
|
|
2259
|
+
console.log(" QMD queries are either a single expand query (no prefix) or a multi-line");
|
|
2260
|
+
console.log(" document where every line is typed with lex:, vec:, or hyde:. This grammar");
|
|
2261
|
+
console.log(" matches the docs in docs/SYNTAX.md and is enforced in the CLI.");
|
|
2262
|
+
console.log("");
|
|
2263
|
+
const grammar = [
|
|
2264
|
+
`query = expand_query | query_document ;`,
|
|
2265
|
+
`expand_query = text | explicit_expand ;`,
|
|
2266
|
+
`explicit_expand= "expand:" text ;`,
|
|
2267
|
+
`query_document = [ intent_line ] { typed_line } ;`,
|
|
2268
|
+
`intent_line = "intent:" text newline ;`,
|
|
2269
|
+
`typed_line = type ":" text newline ;`,
|
|
2270
|
+
`type = "lex" | "vec" | "hyde" ;`,
|
|
2271
|
+
`text = quoted_phrase | plain_text ;`,
|
|
2272
|
+
`quoted_phrase = '"' { character } '"' ;`,
|
|
2273
|
+
`plain_text = { character } ;`,
|
|
2274
|
+
`newline = "\\n" ;`,
|
|
2275
|
+
];
|
|
2276
|
+
console.log(" Grammar:");
|
|
2277
|
+
for (const line of grammar) {
|
|
2278
|
+
console.log(` ${line}`);
|
|
2279
|
+
}
|
|
2280
|
+
console.log("");
|
|
2281
|
+
console.log(" Examples:");
|
|
2282
|
+
console.log(" qmd query \"how does auth work\" # single-line → implicit expand");
|
|
2283
|
+
console.log(" qmd query $'lex: CAP theorem\\nvec: consistency' # typed query document");
|
|
2284
|
+
console.log(" qmd query $'lex: \"exact matches\" sports -baseball' # phrase + negation lex search");
|
|
2285
|
+
console.log(" qmd query $'hyde: Hypothetical answer text' # hyde-only document");
|
|
2286
|
+
console.log("");
|
|
2287
|
+
console.log(" Constraints:");
|
|
2288
|
+
console.log(" - Standalone expand queries cannot mix with typed lines.");
|
|
2289
|
+
console.log(" - Query documents allow only lex:, vec:, or hyde: prefixes.");
|
|
2290
|
+
console.log(" - Each typed line must be single-line text with balanced quotes.");
|
|
2291
|
+
console.log("");
|
|
2292
|
+
console.log("AI agents & integrations:");
|
|
2293
|
+
console.log(" - Run `qmd mcp` to expose the MCP server (stdio) to agents/IDEs.");
|
|
2294
|
+
console.log(" - `qmd skill install` installs the QMD skill into ./.agents/skills/qmd.");
|
|
2295
|
+
console.log(" - Use `qmd skill install --global` for ~/.agents/skills/qmd.");
|
|
2296
|
+
console.log(" - `qmd --skill` is kept as an alias for `qmd skill show`.");
|
|
2297
|
+
console.log(" - Advanced: `qmd mcp --http ...` and `qmd mcp --http --daemon` are optional for custom transports.");
|
|
2298
|
+
console.log("");
|
|
2299
|
+
console.log("Global options:");
|
|
2300
|
+
console.log(" --index <name> - Use a named index (default: index)");
|
|
2301
|
+
console.log("");
|
|
2302
|
+
console.log("Search options:");
|
|
2303
|
+
console.log(" -n <num> - Max results (default 5, or 20 for --files/--json)");
|
|
2304
|
+
console.log(" --all - Return all matches (pair with --min-score)");
|
|
2305
|
+
console.log(" --min-score <num> - Minimum similarity score");
|
|
2306
|
+
console.log(" --full - Output full document instead of snippet");
|
|
2307
|
+
console.log(" -C, --candidate-limit <n> - Max candidates to rerank (default 40, lower = faster)");
|
|
2308
|
+
console.log(" --no-rerank - Skip LLM reranking (use RRF scores only, much faster on CPU)");
|
|
2309
|
+
console.log(" --line-numbers - Include line numbers in output");
|
|
2310
|
+
console.log(" --explain - Include retrieval score traces (query --json/CLI)");
|
|
2311
|
+
console.log(" --files | --json | --csv | --md | --xml - Output format");
|
|
2312
|
+
console.log(" -c, --collection <name> - Filter by one or more collections");
|
|
2313
|
+
console.log("");
|
|
2314
|
+
console.log("Embed/query options:");
|
|
2315
|
+
console.log(" --chunk-strategy <auto|regex> - Chunking mode (default: regex; auto uses AST for code files)");
|
|
2316
|
+
console.log("");
|
|
2317
|
+
console.log("Multi-get options:");
|
|
2318
|
+
console.log(" -l <num> - Maximum lines per file");
|
|
2319
|
+
console.log(" --max-bytes <num> - Skip files larger than N bytes (default 10240)");
|
|
2320
|
+
console.log(" --json/--csv/--md/--xml/--files - Same formats as search");
|
|
2321
|
+
console.log("");
|
|
2322
|
+
console.log(`Index: ${getDbPath()}`);
|
|
2323
|
+
}
|
|
2324
|
+
async function showVersion() {
|
|
2325
|
+
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
|
2326
|
+
const pkgPath = resolve(scriptDir, "..", "..", "package.json");
|
|
2327
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
2328
|
+
let commit = "";
|
|
2329
|
+
try {
|
|
2330
|
+
commit = execSync(`git -C ${scriptDir} rev-parse --short HEAD`, { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }).trim();
|
|
2331
|
+
}
|
|
2332
|
+
catch {
|
|
2333
|
+
// Not a git repo or git not available
|
|
2334
|
+
}
|
|
2335
|
+
const versionStr = commit ? `${pkg.version} (${commit})` : pkg.version;
|
|
2336
|
+
console.log(`qmd ${versionStr}`);
|
|
2337
|
+
}
|
|
2338
|
+
// Main CLI - only run if this is the main module
|
|
2339
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
2340
|
+
const argv1 = process.argv[1];
|
|
2341
|
+
const isMain = argv1 === __filename
|
|
2342
|
+
|| argv1?.endsWith("/qmd.ts")
|
|
2343
|
+
|| argv1?.endsWith("/qmd.js")
|
|
2344
|
+
|| (argv1 != null && realpathSync(argv1) === __filename);
|
|
2345
|
+
if (isMain) {
|
|
2346
|
+
const cli = parseCLI();
|
|
2347
|
+
if (cli.values.version) {
|
|
2348
|
+
await showVersion();
|
|
2349
|
+
process.exit(0);
|
|
2350
|
+
}
|
|
2351
|
+
if (cli.values.skill) {
|
|
2352
|
+
showSkill();
|
|
2353
|
+
process.exit(0);
|
|
2354
|
+
}
|
|
2355
|
+
if (cli.values.help && cli.command === "skill") {
|
|
2356
|
+
console.log("Usage: qmd skill <show|install> [options]");
|
|
2357
|
+
console.log("");
|
|
2358
|
+
console.log("Commands:");
|
|
2359
|
+
console.log(" show Print the packaged QMD skill");
|
|
2360
|
+
console.log(" install Install into ./.agents/skills/qmd");
|
|
2361
|
+
console.log("");
|
|
2362
|
+
console.log("Options:");
|
|
2363
|
+
console.log(" --global Install into ~/.agents/skills/qmd");
|
|
2364
|
+
console.log(" --yes Also create the .claude/skills/qmd symlink");
|
|
2365
|
+
console.log(" -f, --force Replace existing install or symlink");
|
|
2366
|
+
process.exit(0);
|
|
2367
|
+
}
|
|
2368
|
+
if (!cli.command || cli.values.help) {
|
|
2369
|
+
showHelp();
|
|
2370
|
+
process.exit(cli.values.help ? 0 : 1);
|
|
2371
|
+
}
|
|
2372
|
+
switch (cli.command) {
|
|
2373
|
+
case "context": {
|
|
2374
|
+
const subcommand = cli.args[0];
|
|
2375
|
+
if (!subcommand) {
|
|
2376
|
+
console.error("Usage: qmd context <add|list|rm>");
|
|
2377
|
+
console.error("");
|
|
2378
|
+
console.error("Commands:");
|
|
2379
|
+
console.error(" qmd context add [path] \"text\" - Add context (defaults to current dir)");
|
|
2380
|
+
console.error(" qmd context add / \"text\" - Add global context to all collections");
|
|
2381
|
+
console.error(" qmd context list - List all contexts");
|
|
2382
|
+
console.error(" qmd context rm <path> - Remove context");
|
|
2383
|
+
process.exit(1);
|
|
2384
|
+
}
|
|
2385
|
+
switch (subcommand) {
|
|
2386
|
+
case "add": {
|
|
2387
|
+
if (cli.args.length < 2) {
|
|
2388
|
+
console.error("Usage: qmd context add [path] \"text\"");
|
|
2389
|
+
console.error("");
|
|
2390
|
+
console.error("Examples:");
|
|
2391
|
+
console.error(" qmd context add \"Context for current directory\"");
|
|
2392
|
+
console.error(" qmd context add . \"Context for current directory\"");
|
|
2393
|
+
console.error(" qmd context add /subfolder \"Context for subfolder\"");
|
|
2394
|
+
console.error(" qmd context add / \"Global context for all collections\"");
|
|
2395
|
+
console.error("");
|
|
2396
|
+
console.error(" Using virtual paths:");
|
|
2397
|
+
console.error(" qmd context add qmd://journals/ \"Context for entire journals collection\"");
|
|
2398
|
+
console.error(" qmd context add qmd://journals/2024 \"Context for 2024 journals\"");
|
|
2399
|
+
process.exit(1);
|
|
2400
|
+
}
|
|
2401
|
+
let pathArg;
|
|
2402
|
+
let contextText;
|
|
2403
|
+
// Check if first arg looks like a path or if it's the context text
|
|
2404
|
+
const firstArg = cli.args[1] || '';
|
|
2405
|
+
const secondArg = cli.args[2];
|
|
2406
|
+
if (secondArg) {
|
|
2407
|
+
// Two args: path + context
|
|
2408
|
+
pathArg = firstArg;
|
|
2409
|
+
contextText = cli.args.slice(2).join(" ");
|
|
2410
|
+
}
|
|
2411
|
+
else {
|
|
2412
|
+
// One arg: context only (use current directory)
|
|
2413
|
+
pathArg = undefined;
|
|
2414
|
+
contextText = firstArg;
|
|
2415
|
+
}
|
|
2416
|
+
await contextAdd(pathArg, contextText);
|
|
2417
|
+
break;
|
|
2418
|
+
}
|
|
2419
|
+
case "list": {
|
|
2420
|
+
contextList();
|
|
2421
|
+
break;
|
|
2422
|
+
}
|
|
2423
|
+
case "rm":
|
|
2424
|
+
case "remove": {
|
|
2425
|
+
if (cli.args.length < 2 || !cli.args[1]) {
|
|
2426
|
+
console.error("Usage: qmd context rm <path>");
|
|
2427
|
+
console.error("Examples:");
|
|
2428
|
+
console.error(" qmd context rm /");
|
|
2429
|
+
console.error(" qmd context rm qmd://journals/2024");
|
|
2430
|
+
process.exit(1);
|
|
2431
|
+
}
|
|
2432
|
+
contextRemove(cli.args[1]);
|
|
2433
|
+
break;
|
|
2434
|
+
}
|
|
2435
|
+
default:
|
|
2436
|
+
console.error(`Unknown subcommand: ${subcommand}`);
|
|
2437
|
+
console.error("Available: add, list, rm");
|
|
2438
|
+
process.exit(1);
|
|
2439
|
+
}
|
|
2440
|
+
break;
|
|
2441
|
+
}
|
|
2442
|
+
case "get": {
|
|
2443
|
+
if (!cli.args[0]) {
|
|
2444
|
+
console.error("Usage: qmd get <filepath>[:line] [--from <line>] [-l <lines>] [--line-numbers]");
|
|
2445
|
+
process.exit(1);
|
|
2446
|
+
}
|
|
2447
|
+
const fromLine = cli.values.from ? parseInt(cli.values.from, 10) : undefined;
|
|
2448
|
+
const maxLines = cli.values.l ? parseInt(cli.values.l, 10) : undefined;
|
|
2449
|
+
getDocument(cli.args[0], fromLine, maxLines, cli.opts.lineNumbers);
|
|
2450
|
+
break;
|
|
2451
|
+
}
|
|
2452
|
+
case "multi-get": {
|
|
2453
|
+
if (!cli.args[0]) {
|
|
2454
|
+
console.error("Usage: qmd multi-get <pattern> [-l <lines>] [--max-bytes <bytes>] [--json|--csv|--md|--xml|--files]");
|
|
2455
|
+
console.error(" pattern: glob (e.g., 'journals/2025-05*.md') or comma-separated list");
|
|
2456
|
+
process.exit(1);
|
|
2457
|
+
}
|
|
2458
|
+
const maxLinesMulti = cli.values.l ? parseInt(cli.values.l, 10) : undefined;
|
|
2459
|
+
const maxBytes = cli.values["max-bytes"] ? parseInt(cli.values["max-bytes"], 10) : DEFAULT_MULTI_GET_MAX_BYTES;
|
|
2460
|
+
multiGet(cli.args[0], maxLinesMulti, maxBytes, cli.opts.format);
|
|
2461
|
+
break;
|
|
2462
|
+
}
|
|
2463
|
+
case "ls": {
|
|
2464
|
+
listFiles(cli.args[0]);
|
|
2465
|
+
break;
|
|
2466
|
+
}
|
|
2467
|
+
case "collection": {
|
|
2468
|
+
const subcommand = cli.args[0];
|
|
2469
|
+
switch (subcommand) {
|
|
2470
|
+
case "list": {
|
|
2471
|
+
collectionList();
|
|
2472
|
+
break;
|
|
2473
|
+
}
|
|
2474
|
+
case "add": {
|
|
2475
|
+
const pwd = cli.args[1] || getPwd();
|
|
2476
|
+
const resolvedPwd = pwd === '.' ? getPwd() : getRealPath(resolve(pwd));
|
|
2477
|
+
const globPattern = cli.values.mask || DEFAULT_GLOB;
|
|
2478
|
+
const name = cli.values.name;
|
|
2479
|
+
await collectionAdd(resolvedPwd, globPattern, name);
|
|
2480
|
+
break;
|
|
2481
|
+
}
|
|
2482
|
+
case "remove":
|
|
2483
|
+
case "rm": {
|
|
2484
|
+
if (!cli.args[1]) {
|
|
2485
|
+
console.error("Usage: qmd collection remove <name>");
|
|
2486
|
+
console.error(" Use 'qmd collection list' to see available collections");
|
|
2487
|
+
process.exit(1);
|
|
2488
|
+
}
|
|
2489
|
+
collectionRemove(cli.args[1]);
|
|
2490
|
+
break;
|
|
2491
|
+
}
|
|
2492
|
+
case "rename":
|
|
2493
|
+
case "mv": {
|
|
2494
|
+
if (!cli.args[1] || !cli.args[2]) {
|
|
2495
|
+
console.error("Usage: qmd collection rename <old-name> <new-name>");
|
|
2496
|
+
console.error(" Use 'qmd collection list' to see available collections");
|
|
2497
|
+
process.exit(1);
|
|
2498
|
+
}
|
|
2499
|
+
collectionRename(cli.args[1], cli.args[2]);
|
|
2500
|
+
break;
|
|
2501
|
+
}
|
|
2502
|
+
case "set-update":
|
|
2503
|
+
case "update-cmd": {
|
|
2504
|
+
const name = cli.args[1];
|
|
2505
|
+
const cmd = cli.args.slice(2).join(' ') || null;
|
|
2506
|
+
if (!name) {
|
|
2507
|
+
console.error("Usage: qmd collection update-cmd <name> [command]");
|
|
2508
|
+
console.error(" Set the command to run before indexing (e.g., 'git pull')");
|
|
2509
|
+
console.error(" Omit command to clear it");
|
|
2510
|
+
process.exit(1);
|
|
2511
|
+
}
|
|
2512
|
+
const { updateCollectionSettings, getCollection } = await import("../collections.js");
|
|
2513
|
+
const col = getCollection(name);
|
|
2514
|
+
if (!col) {
|
|
2515
|
+
console.error(`Collection not found: ${name}`);
|
|
2516
|
+
process.exit(1);
|
|
2517
|
+
}
|
|
2518
|
+
updateCollectionSettings(name, { update: cmd });
|
|
2519
|
+
if (cmd) {
|
|
2520
|
+
console.log(`✓ Set update command for '${name}': ${cmd}`);
|
|
2521
|
+
}
|
|
2522
|
+
else {
|
|
2523
|
+
console.log(`✓ Cleared update command for '${name}'`);
|
|
2524
|
+
}
|
|
2525
|
+
break;
|
|
2526
|
+
}
|
|
2527
|
+
case "include":
|
|
2528
|
+
case "exclude": {
|
|
2529
|
+
const name = cli.args[1];
|
|
2530
|
+
if (!name) {
|
|
2531
|
+
console.error(`Usage: qmd collection ${subcommand} <name>`);
|
|
2532
|
+
console.error(` ${subcommand === 'include' ? 'Include' : 'Exclude'} collection in default queries`);
|
|
2533
|
+
process.exit(1);
|
|
2534
|
+
}
|
|
2535
|
+
const { updateCollectionSettings, getCollection } = await import("../collections.js");
|
|
2536
|
+
const col = getCollection(name);
|
|
2537
|
+
if (!col) {
|
|
2538
|
+
console.error(`Collection not found: ${name}`);
|
|
2539
|
+
process.exit(1);
|
|
2540
|
+
}
|
|
2541
|
+
const include = subcommand === 'include';
|
|
2542
|
+
updateCollectionSettings(name, { includeByDefault: include });
|
|
2543
|
+
console.log(`✓ Collection '${name}' ${include ? 'included in' : 'excluded from'} default queries`);
|
|
2544
|
+
break;
|
|
2545
|
+
}
|
|
2546
|
+
case "show":
|
|
2547
|
+
case "info": {
|
|
2548
|
+
const name = cli.args[1];
|
|
2549
|
+
if (!name) {
|
|
2550
|
+
console.error("Usage: qmd collection show <name>");
|
|
2551
|
+
process.exit(1);
|
|
2552
|
+
}
|
|
2553
|
+
const { getCollection } = await import("../collections.js");
|
|
2554
|
+
const col = getCollection(name);
|
|
2555
|
+
if (!col) {
|
|
2556
|
+
console.error(`Collection not found: ${name}`);
|
|
2557
|
+
process.exit(1);
|
|
2558
|
+
}
|
|
2559
|
+
console.log(`Collection: ${name}`);
|
|
2560
|
+
console.log(` Path: ${col.path}`);
|
|
2561
|
+
console.log(` Pattern: ${col.pattern}`);
|
|
2562
|
+
console.log(` Include: ${col.includeByDefault !== false ? 'yes (default)' : 'no'}`);
|
|
2563
|
+
if (col.update) {
|
|
2564
|
+
console.log(` Update: ${col.update}`);
|
|
2565
|
+
}
|
|
2566
|
+
if (col.context) {
|
|
2567
|
+
const ctxCount = Object.keys(col.context).length;
|
|
2568
|
+
console.log(` Contexts: ${ctxCount}`);
|
|
2569
|
+
}
|
|
2570
|
+
break;
|
|
2571
|
+
}
|
|
2572
|
+
case "help":
|
|
2573
|
+
case undefined: {
|
|
2574
|
+
console.log("Usage: qmd collection <command> [options]");
|
|
2575
|
+
console.log("");
|
|
2576
|
+
console.log("Commands:");
|
|
2577
|
+
console.log(" list List all collections");
|
|
2578
|
+
console.log(" add <path> [--name NAME] Add a collection");
|
|
2579
|
+
console.log(" remove <name> Remove a collection");
|
|
2580
|
+
console.log(" rename <old> <new> Rename a collection");
|
|
2581
|
+
console.log(" show <name> Show collection details");
|
|
2582
|
+
console.log(" update-cmd <name> [cmd] Set pre-update command (e.g., 'git pull')");
|
|
2583
|
+
console.log(" include <name> Include in default queries");
|
|
2584
|
+
console.log(" exclude <name> Exclude from default queries");
|
|
2585
|
+
console.log("");
|
|
2586
|
+
console.log("Examples:");
|
|
2587
|
+
console.log(" qmd collection add ~/notes --name notes");
|
|
2588
|
+
console.log(" qmd collection update-cmd brain 'git pull'");
|
|
2589
|
+
console.log(" qmd collection exclude archive");
|
|
2590
|
+
process.exit(0);
|
|
2591
|
+
}
|
|
2592
|
+
default:
|
|
2593
|
+
console.error(`Unknown subcommand: ${subcommand}`);
|
|
2594
|
+
console.error("Run 'qmd collection help' for usage");
|
|
2595
|
+
process.exit(1);
|
|
2596
|
+
}
|
|
2597
|
+
break;
|
|
2598
|
+
}
|
|
2599
|
+
case "status":
|
|
2600
|
+
await showStatus();
|
|
2601
|
+
break;
|
|
2602
|
+
case "update":
|
|
2603
|
+
await updateCollections();
|
|
2604
|
+
break;
|
|
2605
|
+
case "embed":
|
|
2606
|
+
try {
|
|
2607
|
+
const maxDocsPerBatch = parseEmbedBatchOption("maxDocsPerBatch", cli.values["max-docs-per-batch"]);
|
|
2608
|
+
const maxBatchMb = parseEmbedBatchOption("maxBatchBytes", cli.values["max-batch-mb"]);
|
|
2609
|
+
const embedChunkStrategy = parseChunkStrategy(cli.values["chunk-strategy"]);
|
|
2610
|
+
await vectorIndex(DEFAULT_EMBED_MODEL, !!cli.values.force, {
|
|
2611
|
+
maxDocsPerBatch,
|
|
2612
|
+
maxBatchBytes: maxBatchMb === undefined ? undefined : maxBatchMb * 1024 * 1024,
|
|
2613
|
+
chunkStrategy: embedChunkStrategy,
|
|
2614
|
+
});
|
|
2615
|
+
}
|
|
2616
|
+
catch (error) {
|
|
2617
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
2618
|
+
process.exit(1);
|
|
2619
|
+
}
|
|
2620
|
+
break;
|
|
2621
|
+
case "pull": {
|
|
2622
|
+
const refresh = cli.values.refresh === undefined ? false : Boolean(cli.values.refresh);
|
|
2623
|
+
const models = [
|
|
2624
|
+
DEFAULT_EMBED_MODEL_URI,
|
|
2625
|
+
DEFAULT_GENERATE_MODEL_URI,
|
|
2626
|
+
DEFAULT_RERANK_MODEL_URI,
|
|
2627
|
+
];
|
|
2628
|
+
console.log(`${c.bold}Pulling models${c.reset}`);
|
|
2629
|
+
const results = await pullModels(models, {
|
|
2630
|
+
refresh,
|
|
2631
|
+
cacheDir: DEFAULT_MODEL_CACHE_DIR,
|
|
2632
|
+
});
|
|
2633
|
+
for (const result of results) {
|
|
2634
|
+
const size = formatBytes(result.sizeBytes);
|
|
2635
|
+
const note = result.refreshed ? "refreshed" : "cached/checked";
|
|
2636
|
+
console.log(`- ${result.model} -> ${result.path} (${size}, ${note})`);
|
|
2637
|
+
}
|
|
2638
|
+
break;
|
|
2639
|
+
}
|
|
2640
|
+
case "search":
|
|
2641
|
+
if (!cli.query) {
|
|
2642
|
+
console.error("Usage: qmd search [options] <query>");
|
|
2643
|
+
process.exit(1);
|
|
2644
|
+
}
|
|
2645
|
+
search(cli.query, cli.opts);
|
|
2646
|
+
break;
|
|
2647
|
+
case "vsearch":
|
|
2648
|
+
case "vector-search": // undocumented alias
|
|
2649
|
+
if (!cli.query) {
|
|
2650
|
+
console.error("Usage: qmd vsearch [options] <query>");
|
|
2651
|
+
process.exit(1);
|
|
2652
|
+
}
|
|
2653
|
+
// Default min-score for vector search is 0.3
|
|
2654
|
+
if (!cli.values["min-score"]) {
|
|
2655
|
+
cli.opts.minScore = 0.3;
|
|
2656
|
+
}
|
|
2657
|
+
await vectorSearch(cli.query, cli.opts);
|
|
2658
|
+
break;
|
|
2659
|
+
case "query":
|
|
2660
|
+
case "deep-search": // undocumented alias
|
|
2661
|
+
if (!cli.query) {
|
|
2662
|
+
console.error("Usage: qmd query [options] <query>");
|
|
2663
|
+
process.exit(1);
|
|
2664
|
+
}
|
|
2665
|
+
await querySearch(cli.query, cli.opts);
|
|
2666
|
+
break;
|
|
2667
|
+
case "mcp": {
|
|
2668
|
+
const sub = cli.args[0]; // stop | status | undefined
|
|
2669
|
+
// Cache dir for PID/log files — same dir as the index
|
|
2670
|
+
const cacheDir = process.env.XDG_CACHE_HOME
|
|
2671
|
+
? resolve(process.env.XDG_CACHE_HOME, "qmd")
|
|
2672
|
+
: resolve(homedir(), ".cache", "qmd");
|
|
2673
|
+
const pidPath = resolve(cacheDir, "mcp.pid");
|
|
2674
|
+
// Subcommands take priority over flags
|
|
2675
|
+
if (sub === "stop") {
|
|
2676
|
+
if (!existsSync(pidPath)) {
|
|
2677
|
+
console.log("Not running (no PID file).");
|
|
2678
|
+
process.exit(0);
|
|
2679
|
+
}
|
|
2680
|
+
const pid = parseInt(readFileSync(pidPath, "utf-8").trim());
|
|
2681
|
+
try {
|
|
2682
|
+
process.kill(pid, 0); // alive?
|
|
2683
|
+
process.kill(pid, "SIGTERM");
|
|
2684
|
+
unlinkSync(pidPath);
|
|
2685
|
+
console.log(`Stopped QMD MCP server (PID ${pid}).`);
|
|
2686
|
+
}
|
|
2687
|
+
catch {
|
|
2688
|
+
unlinkSync(pidPath);
|
|
2689
|
+
console.log("Cleaned up stale PID file (server was not running).");
|
|
2690
|
+
}
|
|
2691
|
+
process.exit(0);
|
|
2692
|
+
}
|
|
2693
|
+
if (cli.values.http) {
|
|
2694
|
+
const port = Number(cli.values.port) || 8181;
|
|
2695
|
+
if (cli.values.daemon) {
|
|
2696
|
+
// Guard: check if already running
|
|
2697
|
+
if (existsSync(pidPath)) {
|
|
2698
|
+
const existingPid = parseInt(readFileSync(pidPath, "utf-8").trim());
|
|
2699
|
+
try {
|
|
2700
|
+
process.kill(existingPid, 0); // alive?
|
|
2701
|
+
console.error(`Already running (PID ${existingPid}). Run 'qmd mcp stop' first.`);
|
|
2702
|
+
process.exit(1);
|
|
2703
|
+
}
|
|
2704
|
+
catch {
|
|
2705
|
+
// Stale PID file — continue
|
|
2706
|
+
}
|
|
2707
|
+
}
|
|
2708
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
2709
|
+
const logPath = resolve(cacheDir, "mcp.log");
|
|
2710
|
+
const logFd = openSync(logPath, "w"); // truncate — fresh log per daemon run
|
|
2711
|
+
const selfPath = fileURLToPath(import.meta.url);
|
|
2712
|
+
const spawnArgs = selfPath.endsWith(".ts")
|
|
2713
|
+
? ["--import", pathJoin(dirname(selfPath), "..", "..", "node_modules", "tsx", "dist", "esm", "index.mjs"), selfPath, "mcp", "--http", "--port", String(port)]
|
|
2714
|
+
: [selfPath, "mcp", "--http", "--port", String(port)];
|
|
2715
|
+
const child = nodeSpawn(process.execPath, spawnArgs, {
|
|
2716
|
+
stdio: ["ignore", logFd, logFd],
|
|
2717
|
+
detached: true,
|
|
2718
|
+
});
|
|
2719
|
+
child.unref();
|
|
2720
|
+
closeSync(logFd); // parent's copy; child inherited the fd
|
|
2721
|
+
writeFileSync(pidPath, String(child.pid));
|
|
2722
|
+
console.log(`Started on http://localhost:${port}/mcp (PID ${child.pid})`);
|
|
2723
|
+
console.log(`Logs: ${logPath}`);
|
|
2724
|
+
process.exit(0);
|
|
2725
|
+
}
|
|
2726
|
+
// Foreground HTTP mode — remove top-level cursor handlers so the
|
|
2727
|
+
// async cleanup handlers in startMcpHttpServer actually run.
|
|
2728
|
+
process.removeAllListeners("SIGTERM");
|
|
2729
|
+
process.removeAllListeners("SIGINT");
|
|
2730
|
+
const { startMcpHttpServer } = await import("../mcp/server.js");
|
|
2731
|
+
try {
|
|
2732
|
+
await startMcpHttpServer(port);
|
|
2733
|
+
}
|
|
2734
|
+
catch (e) {
|
|
2735
|
+
if (e?.code === "EADDRINUSE") {
|
|
2736
|
+
console.error(`Port ${port} already in use. Try a different port with --port.`);
|
|
2737
|
+
process.exit(1);
|
|
2738
|
+
}
|
|
2739
|
+
throw e;
|
|
2740
|
+
}
|
|
2741
|
+
}
|
|
2742
|
+
else {
|
|
2743
|
+
// Default: stdio transport
|
|
2744
|
+
const { startMcpServer } = await import("../mcp/server.js");
|
|
2745
|
+
await startMcpServer();
|
|
2746
|
+
}
|
|
2747
|
+
break;
|
|
2748
|
+
}
|
|
2749
|
+
case "skill": {
|
|
2750
|
+
const subcommand = cli.args[0];
|
|
2751
|
+
switch (subcommand) {
|
|
2752
|
+
case "show": {
|
|
2753
|
+
showSkill();
|
|
2754
|
+
break;
|
|
2755
|
+
}
|
|
2756
|
+
case "install": {
|
|
2757
|
+
try {
|
|
2758
|
+
await installSkill(Boolean(cli.values.global), Boolean(cli.values.force), Boolean(cli.values.yes));
|
|
2759
|
+
}
|
|
2760
|
+
catch (error) {
|
|
2761
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
2762
|
+
process.exit(1);
|
|
2763
|
+
}
|
|
2764
|
+
break;
|
|
2765
|
+
}
|
|
2766
|
+
case "help":
|
|
2767
|
+
case undefined: {
|
|
2768
|
+
console.log("Usage: qmd skill <show|install> [options]");
|
|
2769
|
+
console.log("");
|
|
2770
|
+
console.log("Commands:");
|
|
2771
|
+
console.log(" show Print the packaged QMD skill");
|
|
2772
|
+
console.log(" install Install into ./.agents/skills/qmd");
|
|
2773
|
+
console.log("");
|
|
2774
|
+
console.log("Options:");
|
|
2775
|
+
console.log(" --global Install into ~/.agents/skills/qmd");
|
|
2776
|
+
console.log(" --yes Also create the .claude/skills/qmd symlink");
|
|
2777
|
+
console.log(" -f, --force Replace existing install or symlink");
|
|
2778
|
+
process.exit(0);
|
|
2779
|
+
}
|
|
2780
|
+
default:
|
|
2781
|
+
console.error(`Unknown subcommand: ${subcommand}`);
|
|
2782
|
+
console.error("Run 'qmd skill help' for usage");
|
|
2783
|
+
process.exit(1);
|
|
2784
|
+
}
|
|
2785
|
+
break;
|
|
2786
|
+
}
|
|
2787
|
+
case "cleanup": {
|
|
2788
|
+
const db = getDb();
|
|
2789
|
+
// 1. Clear llm_cache
|
|
2790
|
+
const cacheCount = deleteLLMCache(db);
|
|
2791
|
+
console.log(`${c.green}✓${c.reset} Cleared ${cacheCount} cached API responses`);
|
|
2792
|
+
// 2. Remove orphaned vectors
|
|
2793
|
+
const orphanedVecs = cleanupOrphanedVectors(db);
|
|
2794
|
+
if (orphanedVecs > 0) {
|
|
2795
|
+
console.log(`${c.green}✓${c.reset} Removed ${orphanedVecs} orphaned embedding chunks`);
|
|
2796
|
+
}
|
|
2797
|
+
else {
|
|
2798
|
+
console.log(`${c.dim}No orphaned embeddings to remove${c.reset}`);
|
|
2799
|
+
}
|
|
2800
|
+
// 3. Remove inactive documents
|
|
2801
|
+
const inactiveDocs = deleteInactiveDocuments(db);
|
|
2802
|
+
if (inactiveDocs > 0) {
|
|
2803
|
+
console.log(`${c.green}✓${c.reset} Removed ${inactiveDocs} inactive document records`);
|
|
2804
|
+
}
|
|
2805
|
+
// 4. Vacuum to reclaim space
|
|
2806
|
+
vacuumDatabase(db);
|
|
2807
|
+
console.log(`${c.green}✓${c.reset} Database vacuumed`);
|
|
2808
|
+
closeDb();
|
|
2809
|
+
break;
|
|
2810
|
+
}
|
|
2811
|
+
default:
|
|
2812
|
+
console.error(`Unknown command: ${cli.command}`);
|
|
2813
|
+
console.error("Run 'qmd --help' for usage.");
|
|
2814
|
+
process.exit(1);
|
|
2815
|
+
}
|
|
2816
|
+
if (cli.command !== "mcp") {
|
|
2817
|
+
await disposeDefaultLlamaCpp();
|
|
2818
|
+
process.exit(0);
|
|
2819
|
+
}
|
|
2820
|
+
} // end if (main module)
|