@kb-labs/mind-entry 2.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +218 -0
- package/dist/cli/commands/init.d.ts +24 -0
- package/dist/cli/commands/init.js +114 -0
- package/dist/cli/commands/init.js.map +1 -0
- package/dist/cli/commands/rag-index.d.ts +20 -0
- package/dist/cli/commands/rag-index.js +363 -0
- package/dist/cli/commands/rag-index.js.map +1 -0
- package/dist/cli/commands/rag-query.d.ts +25 -0
- package/dist/cli/commands/rag-query.js +670 -0
- package/dist/cli/commands/rag-query.js.map +1 -0
- package/dist/cli/commands/verify.d.ts +15 -0
- package/dist/cli/commands/verify.js +92 -0
- package/dist/cli/commands/verify.js.map +1 -0
- package/dist/index.d.ts +592 -0
- package/dist/index.js +1499 -0
- package/dist/index.js.map +1 -0
- package/dist/manifest.v3.d.ts +52 -0
- package/dist/manifest.v3.js +180 -0
- package/dist/manifest.v3.js.map +1 -0
- package/package.json +61 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { defineCommand, useConfig, usePlatform, useLoader } from '@kb-labs/sdk';
|
|
2
|
+
import 'crypto';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { readFile } from 'fs/promises';
|
|
5
|
+
import { MindEngine } from '@kb-labs/mind-engine';
|
|
6
|
+
import '@kb-labs/mind-orchestrator';
|
|
7
|
+
|
|
8
|
+
// src/cli/commands/rag-index.ts
|
|
9
|
+
var MIND_PRODUCT_ID = "mind";
|
|
10
|
+
async function createMindRuntime(options) {
|
|
11
|
+
const config = await resolveConfig(options.cwd, options.config);
|
|
12
|
+
const platform = options.platform ?? usePlatform();
|
|
13
|
+
const service = {
|
|
14
|
+
index: async (scopeId) => {
|
|
15
|
+
const scope = resolveScope(config, scopeId);
|
|
16
|
+
const engine = createEngine(config, scope, options.cwd, options.runtime, platform, options.onProgress);
|
|
17
|
+
await engine.init();
|
|
18
|
+
const stats = await engine.index(resolveSources(config, scope), {
|
|
19
|
+
scope,
|
|
20
|
+
workspaceRoot: options.cwd
|
|
21
|
+
});
|
|
22
|
+
return stats;
|
|
23
|
+
},
|
|
24
|
+
query: async (queryOptions) => {
|
|
25
|
+
if (queryOptions.productId && queryOptions.productId !== MIND_PRODUCT_ID) {
|
|
26
|
+
throw new Error(`Unsupported productId "${queryOptions.productId}". Expected "${MIND_PRODUCT_ID}".`);
|
|
27
|
+
}
|
|
28
|
+
const scope = resolveScope(config, queryOptions.scopeId);
|
|
29
|
+
const sources = resolveSources(config, scope);
|
|
30
|
+
const engine = createEngine(config, scope, options.cwd, options.runtime, platform, options.onProgress);
|
|
31
|
+
await engine.init();
|
|
32
|
+
const result = await engine.query(
|
|
33
|
+
{
|
|
34
|
+
text: queryOptions.text,
|
|
35
|
+
intent: queryOptions.intent ?? "summary",
|
|
36
|
+
limit: queryOptions.limit,
|
|
37
|
+
profileId: queryOptions.profileId,
|
|
38
|
+
metadata: queryOptions.metadata
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
scope,
|
|
42
|
+
sources,
|
|
43
|
+
workspaceRoot: options.cwd,
|
|
44
|
+
limit: queryOptions.limit,
|
|
45
|
+
profile: queryOptions.profileId ? { id: queryOptions.profileId } : void 0
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
service,
|
|
53
|
+
config
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function createEngine(config, scope, cwd, runtime, platform, onProgress) {
|
|
57
|
+
const engineConfig = resolveEngineConfig(config, scope);
|
|
58
|
+
return new MindEngine(
|
|
59
|
+
{
|
|
60
|
+
id: engineConfig.id,
|
|
61
|
+
type: engineConfig.type,
|
|
62
|
+
options: {
|
|
63
|
+
...engineConfig.options ?? {},
|
|
64
|
+
_runtime: runtime,
|
|
65
|
+
platform: platform ?? void 0,
|
|
66
|
+
onProgress
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
workspaceRoot: cwd
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
function resolveSources(config, scope) {
|
|
75
|
+
if (!scope.sourceIds?.length) {
|
|
76
|
+
return config.sources;
|
|
77
|
+
}
|
|
78
|
+
const selected = config.sources.filter((source) => scope.sourceIds.includes(source.id));
|
|
79
|
+
if (!selected.length) {
|
|
80
|
+
throw new Error(`Scope "${scope.id}" does not reference existing sources.`);
|
|
81
|
+
}
|
|
82
|
+
return selected;
|
|
83
|
+
}
|
|
84
|
+
function resolveScope(config, scopeId) {
|
|
85
|
+
const scope = config.scopes.find((item) => item.id === scopeId);
|
|
86
|
+
if (!scope) {
|
|
87
|
+
throw new Error(`Scope "${scopeId}" is not defined in mind.scopes.`);
|
|
88
|
+
}
|
|
89
|
+
return scope;
|
|
90
|
+
}
|
|
91
|
+
function resolveEngineConfig(config, scope) {
|
|
92
|
+
const engineId = scope.defaultEngine ?? config.defaults?.fallbackEngineId ?? config.engines[0]?.id;
|
|
93
|
+
if (!engineId) {
|
|
94
|
+
throw new Error("No engines configured in mind config.");
|
|
95
|
+
}
|
|
96
|
+
const engine = config.engines.find((item) => item.id === engineId);
|
|
97
|
+
if (!engine) {
|
|
98
|
+
throw new Error(`Engine "${engineId}" referenced by scope "${scope.id}" does not exist.`);
|
|
99
|
+
}
|
|
100
|
+
return engine;
|
|
101
|
+
}
|
|
102
|
+
async function resolveConfig(cwd, provided) {
|
|
103
|
+
if (provided) {
|
|
104
|
+
return normalizeConfig(provided);
|
|
105
|
+
}
|
|
106
|
+
const configPath = await findConfigPath(cwd);
|
|
107
|
+
const raw = JSON.parse(await readFile(configPath, "utf8"));
|
|
108
|
+
return normalizeConfig(raw);
|
|
109
|
+
}
|
|
110
|
+
async function findConfigPath(cwd) {
|
|
111
|
+
const candidates = [
|
|
112
|
+
path.resolve(cwd, ".kb/kb.config.json"),
|
|
113
|
+
path.resolve(cwd, "kb.config.json")
|
|
114
|
+
];
|
|
115
|
+
for (const candidate of candidates) {
|
|
116
|
+
try {
|
|
117
|
+
await readFile(candidate, "utf8");
|
|
118
|
+
return candidate;
|
|
119
|
+
} catch {
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
throw new Error("No kb.config.json found. Expected .kb/kb.config.json or kb.config.json.");
|
|
123
|
+
}
|
|
124
|
+
function normalizeConfig(raw) {
|
|
125
|
+
const data = raw;
|
|
126
|
+
if (Array.isArray(data.profiles) && data.profiles.length > 0) {
|
|
127
|
+
const profile = data.profiles.find((p) => p.id === "default") ?? data.profiles[0];
|
|
128
|
+
const products = profile?.products;
|
|
129
|
+
const mindConfig = products?.[MIND_PRODUCT_ID];
|
|
130
|
+
if (!mindConfig) {
|
|
131
|
+
throw new Error("Config does not contain profiles[].products.mind section.");
|
|
132
|
+
}
|
|
133
|
+
return validateConfig(mindConfig);
|
|
134
|
+
}
|
|
135
|
+
if (data.mind && typeof data.mind === "object") {
|
|
136
|
+
return validateConfig(data.mind);
|
|
137
|
+
}
|
|
138
|
+
return validateConfig(data);
|
|
139
|
+
}
|
|
140
|
+
function validateConfig(config) {
|
|
141
|
+
if (!Array.isArray(config.sources) || !Array.isArray(config.scopes) || !Array.isArray(config.engines)) {
|
|
142
|
+
throw new Error("Invalid mind config: required arrays sources/scopes/engines are missing.");
|
|
143
|
+
}
|
|
144
|
+
return config;
|
|
145
|
+
}
|
|
146
|
+
function getAdapterName(service, fallback) {
|
|
147
|
+
if (!service) {
|
|
148
|
+
return fallback;
|
|
149
|
+
}
|
|
150
|
+
const name = service.constructor?.name || service.name || service.id;
|
|
151
|
+
if (name && name !== "Object" && name !== "Function") {
|
|
152
|
+
return name;
|
|
153
|
+
}
|
|
154
|
+
return fallback;
|
|
155
|
+
}
|
|
156
|
+
async function runRagIndex(options) {
|
|
157
|
+
const platform = options.platform ?? usePlatform();
|
|
158
|
+
const adapters = {
|
|
159
|
+
vectorStore: getAdapterName(platform?.vectorStore, "LocalVectorStore (fallback)"),
|
|
160
|
+
embeddings: getAdapterName(platform?.embeddings, "DeterministicEmbeddings (fallback)"),
|
|
161
|
+
storage: getAdapterName(platform?.storage, "MemoryStorage (fallback)"),
|
|
162
|
+
llm: getAdapterName(platform?.llm, "LocalStubLLM (fallback)"),
|
|
163
|
+
cache: getAdapterName(platform?.cache, "MemoryCache (fallback)")
|
|
164
|
+
};
|
|
165
|
+
let effectiveConfig = options.config;
|
|
166
|
+
if (options.include || options.exclude) {
|
|
167
|
+
let mindConfig = effectiveConfig;
|
|
168
|
+
if (mindConfig?.sources && Array.isArray(mindConfig.sources)) {
|
|
169
|
+
mindConfig = { ...mindConfig };
|
|
170
|
+
mindConfig.sources = mindConfig.sources.map((source) => {
|
|
171
|
+
const overriddenSource = { ...source };
|
|
172
|
+
if (options.include) {
|
|
173
|
+
overriddenSource.paths = [options.include];
|
|
174
|
+
}
|
|
175
|
+
if (options.exclude) {
|
|
176
|
+
overriddenSource.exclude = options.exclude.split(",").map((s) => s.trim());
|
|
177
|
+
}
|
|
178
|
+
return overriddenSource;
|
|
179
|
+
});
|
|
180
|
+
effectiveConfig = mindConfig;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const runtime = await createMindRuntime({
|
|
184
|
+
cwd: options.cwd,
|
|
185
|
+
config: effectiveConfig,
|
|
186
|
+
runtime: "runtime" in options ? options.runtime : void 0,
|
|
187
|
+
platform: options.platform
|
|
188
|
+
});
|
|
189
|
+
const allScopeIds = runtime.config.scopes?.map((scope) => scope.id) ?? [];
|
|
190
|
+
if (!allScopeIds.length) {
|
|
191
|
+
throw new Error("No mind scopes found. Update kb.config.json first.");
|
|
192
|
+
}
|
|
193
|
+
const scopeIds = options.scopeId ? allScopeIds.filter((scopeId) => scopeId === options.scopeId) : allScopeIds;
|
|
194
|
+
if (!scopeIds.length) {
|
|
195
|
+
throw new Error(
|
|
196
|
+
`Scope "${options.scopeId}" is not defined in mind.scopes.`
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
const originalSkipDedup = process.env.KB_SKIP_DEDUPLICATION;
|
|
200
|
+
if (options.skipDeduplication) {
|
|
201
|
+
process.env.KB_SKIP_DEDUPLICATION = "true";
|
|
202
|
+
}
|
|
203
|
+
const aggregatedStats = {
|
|
204
|
+
filesDiscovered: 0,
|
|
205
|
+
filesProcessed: 0,
|
|
206
|
+
filesSkipped: 0,
|
|
207
|
+
chunksStored: 0,
|
|
208
|
+
chunksUpdated: 0,
|
|
209
|
+
chunksSkipped: 0,
|
|
210
|
+
errorCount: 0,
|
|
211
|
+
durationMs: 0,
|
|
212
|
+
deletedFiles: 0,
|
|
213
|
+
deletedChunks: 0,
|
|
214
|
+
invalidChunks: 0
|
|
215
|
+
};
|
|
216
|
+
try {
|
|
217
|
+
for (const scopeId of scopeIds) {
|
|
218
|
+
const scopeStats = await runtime.service.index(scopeId);
|
|
219
|
+
if (scopeStats) {
|
|
220
|
+
aggregatedStats.filesDiscovered += scopeStats.filesDiscovered;
|
|
221
|
+
aggregatedStats.filesProcessed += scopeStats.filesProcessed;
|
|
222
|
+
aggregatedStats.filesSkipped += scopeStats.filesSkipped;
|
|
223
|
+
aggregatedStats.chunksStored += scopeStats.chunksStored;
|
|
224
|
+
aggregatedStats.chunksUpdated += scopeStats.chunksUpdated;
|
|
225
|
+
aggregatedStats.chunksSkipped += scopeStats.chunksSkipped;
|
|
226
|
+
aggregatedStats.errorCount += scopeStats.errorCount;
|
|
227
|
+
aggregatedStats.durationMs += scopeStats.durationMs;
|
|
228
|
+
aggregatedStats.deletedFiles = (aggregatedStats.deletedFiles ?? 0) + (scopeStats.deletedFiles ?? 0);
|
|
229
|
+
aggregatedStats.deletedChunks = (aggregatedStats.deletedChunks ?? 0) + (scopeStats.deletedChunks ?? 0);
|
|
230
|
+
aggregatedStats.invalidChunks = (aggregatedStats.invalidChunks ?? 0) + (scopeStats.invalidChunks ?? 0);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
} finally {
|
|
234
|
+
if (originalSkipDedup === void 0) {
|
|
235
|
+
delete process.env.KB_SKIP_DEDUPLICATION;
|
|
236
|
+
} else {
|
|
237
|
+
process.env.KB_SKIP_DEDUPLICATION = originalSkipDedup;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return { scopeIds, adapters, stats: aggregatedStats };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// src/cli/commands/rag-index.ts
|
|
244
|
+
var rag_index_default = defineCommand({
|
|
245
|
+
id: "mind:rag-index",
|
|
246
|
+
description: "Build Mind indexes",
|
|
247
|
+
handler: {
|
|
248
|
+
async execute(ctx, input) {
|
|
249
|
+
const startTime = Date.now();
|
|
250
|
+
const { flags } = input;
|
|
251
|
+
const mindConfig = await useConfig();
|
|
252
|
+
const cwd = flags.cwd || ctx.cwd;
|
|
253
|
+
const scopeId = flags.scope;
|
|
254
|
+
const include = flags.include;
|
|
255
|
+
const exclude = flags.exclude;
|
|
256
|
+
const skipDeduplication = flags.skipDeduplication;
|
|
257
|
+
const platform = usePlatform();
|
|
258
|
+
const loader = !flags.quiet && !flags.json ? useLoader("Building Mind RAG index...") : null;
|
|
259
|
+
loader?.start();
|
|
260
|
+
try {
|
|
261
|
+
const result = await runRagIndex({
|
|
262
|
+
cwd,
|
|
263
|
+
scopeId,
|
|
264
|
+
include,
|
|
265
|
+
exclude,
|
|
266
|
+
skipDeduplication,
|
|
267
|
+
config: mindConfig,
|
|
268
|
+
platform
|
|
269
|
+
});
|
|
270
|
+
const timing = Date.now() - startTime;
|
|
271
|
+
loader?.succeed(`Index built in ${(timing / 1e3).toFixed(1)}s`);
|
|
272
|
+
platform?.analytics?.track?.("mind.rag-index", {
|
|
273
|
+
scopeIds: result.scopeIds,
|
|
274
|
+
stats: result.stats
|
|
275
|
+
}).catch(() => {
|
|
276
|
+
});
|
|
277
|
+
if (flags.json) {
|
|
278
|
+
ctx.ui.json({
|
|
279
|
+
ok: true,
|
|
280
|
+
scopes: result.scopeIds,
|
|
281
|
+
stats: result.stats,
|
|
282
|
+
adapters: result.adapters,
|
|
283
|
+
timingMs: timing
|
|
284
|
+
});
|
|
285
|
+
} else if (!flags.quiet) {
|
|
286
|
+
const { stats } = result;
|
|
287
|
+
const percentage = stats.filesDiscovered > 0 ? (stats.filesProcessed / stats.filesDiscovered * 100).toFixed(1) : "0.0";
|
|
288
|
+
const chunksPerFile = stats.filesProcessed > 0 ? (stats.chunksStored / stats.filesProcessed).toFixed(2) : "0.00";
|
|
289
|
+
ctx.ui.success(
|
|
290
|
+
`Indexed ${stats.filesProcessed} files, ${stats.filesSkipped} skipped, ${stats.chunksStored} chunks, deleted ${stats.deletedFiles ?? 0} files/${stats.deletedChunks ?? 0} chunks`,
|
|
291
|
+
{
|
|
292
|
+
title: "Mind RAG Index",
|
|
293
|
+
sections: [
|
|
294
|
+
{
|
|
295
|
+
header: "Files",
|
|
296
|
+
items: [
|
|
297
|
+
`Discovered: ${stats.filesDiscovered}`,
|
|
298
|
+
`Processed: ${stats.filesProcessed} (${percentage}%)`,
|
|
299
|
+
`Skipped: ${stats.filesSkipped}`
|
|
300
|
+
]
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
header: "Chunks",
|
|
304
|
+
items: [
|
|
305
|
+
`Stored: ${stats.chunksStored}`,
|
|
306
|
+
`Updated: ${stats.chunksUpdated}`,
|
|
307
|
+
`Skipped: ${stats.chunksSkipped}`,
|
|
308
|
+
`Rate: ${chunksPerFile}/file`
|
|
309
|
+
]
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
header: "Cleanup",
|
|
313
|
+
items: [
|
|
314
|
+
`Deleted files: ${stats.deletedFiles ?? 0}`,
|
|
315
|
+
`Deleted chunks: ${stats.deletedChunks ?? 0}`,
|
|
316
|
+
`Invalid chunks: ${stats.invalidChunks ?? 0}`
|
|
317
|
+
]
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
header: "Health",
|
|
321
|
+
items: [
|
|
322
|
+
`Errors: ${stats.errorCount}`
|
|
323
|
+
]
|
|
324
|
+
}
|
|
325
|
+
],
|
|
326
|
+
timing
|
|
327
|
+
}
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
exitCode: 0,
|
|
332
|
+
ok: true,
|
|
333
|
+
scopes: result.scopeIds,
|
|
334
|
+
adapters: result.adapters
|
|
335
|
+
};
|
|
336
|
+
} catch (error) {
|
|
337
|
+
const timing = Date.now() - startTime;
|
|
338
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
339
|
+
loader?.fail(`Index build failed: ${message}`);
|
|
340
|
+
if (flags.json) {
|
|
341
|
+
ctx.ui.info(JSON.stringify({
|
|
342
|
+
ok: false,
|
|
343
|
+
error: message,
|
|
344
|
+
timingMs: timing
|
|
345
|
+
}));
|
|
346
|
+
} else if (!flags.quiet) {
|
|
347
|
+
ctx.ui.error(`Index build failed: ${message}`);
|
|
348
|
+
}
|
|
349
|
+
platform?.analytics?.track?.("mind.rag-index", {
|
|
350
|
+
error: true,
|
|
351
|
+
errorMessage: message,
|
|
352
|
+
timingMs: timing
|
|
353
|
+
}).catch(() => {
|
|
354
|
+
});
|
|
355
|
+
return { exitCode: 1, ok: false };
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
export { rag_index_default as default };
|
|
362
|
+
//# sourceMappingURL=rag-index.js.map
|
|
363
|
+
//# sourceMappingURL=rag-index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/runtime/mind-runtime.ts","../../../src/features/rag/service.ts","../../../src/cli/commands/rag-index.ts"],"names":["usePlatform"],"mappings":";;;;;;;;AAgBO,IAAM,eAAA,GAAkB,MAAA;AAyC/B,eAAsB,kBAAkB,OAAA,EAAmD;AACzF,EAAA,MAAM,SAAS,MAAM,aAAA,CAAc,OAAA,CAAQ,GAAA,EAAK,QAAQ,MAAM,CAAA;AAC9D,EAAA,MAAM,Q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,CAAA,oBAAA,EAAuB,OAAO,CAAA,CAAE,CAAA;AAE7C,QAAA,IAAI,MAAM,IAAA,EAAM;AACd,UAAA,GAAA,CAAI,EAAA,CAAG,IAAA,CAAK,IAAA,CAAK,SAAA,CAAU;AAAA,YACzB,EAAA,EAAI,KAAA;AAAA,YACJ,KAAA,EAAO,OAAA;AAAA,YACP,QAAA,EAAU;AAAA,WACX,CAAC,CAAA;AAAA,QACJ,CAAA,MAAA,IAAW,CAAC,KAAA,CAAM,KAAA,EAAO;AACvB,UAAA,GAAA,CAAI,EAAA,CAAG,KAAA,CAAM,CAAA,oBAAA,EAAuB,OAAO,CAAA,CAAE,CAAA;AAAA,QAC/C;AAGA,QAAA,QAAA,EAAU,SAAA,EAAW,QAAQ,gBAAA,EAAkB;AAAA,UAC7C,KAAA,EAAO,IAAA;AAAA,UACP,YAAA,EAAc,OAAA;AAAA,UACd,QAAA,EAAU;AAAA,SACX,CAAA,CAAE,KAAA,CAAM,MAAM;AAAA,QAAC,CAAC,CAAA;AAEjB,QAAA,OAAO,EAAE,QAAA,EAAU,CAAA,EAAG,EAAA,EAAI,KAAA,EAAM;AAAA,MAClC;AAAA,IACF;AAAA;AAEJ,CAAC","file":"rag-index.js","sourcesContent":["import { readFile } from 'node:fs/promises';\nimport * as path from 'node:path';\nimport { usePlatform, type PlatformServices } from '@kb-labs/sdk';\nimport { MindEngine } from '@kb-labs/mind-engine';\nimport type {\n MindConfigInput,\n MindEngineConfig,\n MindScopeConfig,\n MindSourceConfig,\n} from '@kb-labs/mind-contracts';\nimport type {\n MindIndexStats,\n MindIntent,\n MindQueryResult,\n} from '@kb-labs/mind-types';\n\nexport const MIND_PRODUCT_ID = 'mind';\n\nexport interface MindRuntimeService {\n index(scopeId: string): Promise<MindIndexStats>;\n query(options: {\n productId?: string;\n scopeId: string;\n text: string;\n intent?: MindIntent;\n limit?: number;\n profileId?: string;\n metadata?: Record<string, unknown>;\n }): Promise<MindQueryResult>;\n}\n\nexport interface MindRuntime {\n service: MindRuntimeService;\n config: MindConfigInput;\n}\n\nexport interface MindRuntimeOptions {\n cwd: string;\n config?: MindConfigInput | Record<string, unknown>;\n runtime?: {\n fetch?: typeof fetch;\n fs?: unknown;\n env?: (key: string) => string | undefined;\n log?: (\n level: 'debug' | 'info' | 'warn' | 'error',\n msg: string,\n meta?: Record<string, unknown>,\n ) => void;\n analytics?: {\n track(event: string, properties?: Record<string, unknown>): void;\n metric(name: string, value: number, tags?: Record<string, string>): void;\n };\n };\n platform?: PlatformServices;\n onProgress?: (event: { stage: string; details?: string; metadata?: Record<string, unknown>; timestamp: number }) => void;\n}\n\nexport async function createMindRuntime(options: MindRuntimeOptions): Promise<MindRuntime> {\n const config = await resolveConfig(options.cwd, options.config);\n const platform = options.platform ?? usePlatform();\n\n const service: MindRuntimeService = {\n index: async (scopeId: string) => {\n const scope = resolveScope(config, scopeId);\n const engine = createEngine(config, scope, options.cwd, options.runtime, platform, options.onProgress);\n await engine.init();\n const stats = await engine.index(resolveSources(config, scope), {\n scope,\n workspaceRoot: options.cwd,\n } as any);\n return stats as unknown as MindIndexStats;\n },\n query: async (queryOptions) => {\n if (queryOptions.productId && queryOptions.productId !== MIND_PRODUCT_ID) {\n throw new Error(`Unsupported productId \"${queryOptions.productId}\". Expected \"${MIND_PRODUCT_ID}\".`);\n }\n const scope = resolveScope(config, queryOptions.scopeId);\n const sources = resolveSources(config, scope);\n const engine = createEngine(config, scope, options.cwd, options.runtime, platform, options.onProgress);\n await engine.init();\n\n const result = await engine.query(\n {\n text: queryOptions.text,\n intent: queryOptions.intent ?? 'summary',\n limit: queryOptions.limit,\n profileId: queryOptions.profileId,\n metadata: queryOptions.metadata,\n } as any,\n {\n scope,\n sources,\n workspaceRoot: options.cwd,\n limit: queryOptions.limit,\n profile: queryOptions.profileId ? { id: queryOptions.profileId } : undefined,\n } as any,\n );\n\n return result as unknown as MindQueryResult;\n },\n };\n\n return {\n service,\n config,\n };\n}\n\nfunction createEngine(\n config: MindConfigInput,\n scope: MindScopeConfig,\n cwd: string,\n runtime: MindRuntimeOptions['runtime'],\n platform: PlatformServices | null | undefined,\n onProgress: MindRuntimeOptions['onProgress'],\n): any {\n const engineConfig = resolveEngineConfig(config, scope);\n return new MindEngine(\n {\n id: engineConfig.id,\n type: engineConfig.type,\n options: {\n ...(engineConfig.options ?? {}),\n _runtime: runtime,\n platform: platform ?? undefined,\n onProgress,\n },\n } as any,\n {\n workspaceRoot: cwd,\n } as any,\n );\n}\n\nfunction resolveSources(config: MindConfigInput, scope: MindScopeConfig): MindSourceConfig[] {\n if (!scope.sourceIds?.length) {\n return config.sources;\n }\n const selected = config.sources.filter((source: MindSourceConfig) => scope.sourceIds!.includes(source.id));\n if (!selected.length) {\n throw new Error(`Scope \"${scope.id}\" does not reference existing sources.`);\n }\n return selected;\n}\n\nfunction resolveScope(config: MindConfigInput, scopeId: string): MindScopeConfig {\n const scope = config.scopes.find((item: MindScopeConfig) => item.id === scopeId);\n if (!scope) {\n throw new Error(`Scope \"${scopeId}\" is not defined in mind.scopes.`);\n }\n return scope;\n}\n\nfunction resolveEngineConfig(config: MindConfigInput, scope: MindScopeConfig): MindEngineConfig {\n const engineId = scope.defaultEngine ?? config.defaults?.fallbackEngineId ?? config.engines[0]?.id;\n if (!engineId) {\n throw new Error('No engines configured in mind config.');\n }\n const engine = config.engines.find((item: MindEngineConfig) => item.id === engineId);\n if (!engine) {\n throw new Error(`Engine \"${engineId}\" referenced by scope \"${scope.id}\" does not exist.`);\n }\n return engine;\n}\n\nasync function resolveConfig(cwd: string, provided?: MindRuntimeOptions['config']): Promise<MindConfigInput> {\n if (provided) {\n return normalizeConfig(provided);\n }\n const configPath = await findConfigPath(cwd);\n const raw = JSON.parse(await readFile(configPath, 'utf8')) as Record<string, unknown>;\n return normalizeConfig(raw);\n}\n\nasync function findConfigPath(cwd: string): Promise<string> {\n const candidates = [\n path.resolve(cwd, '.kb/kb.config.json'),\n path.resolve(cwd, 'kb.config.json'),\n ];\n\n for (const candidate of candidates) {\n try {\n await readFile(candidate, 'utf8');\n return candidate;\n } catch {\n // continue\n }\n }\n\n throw new Error('No kb.config.json found. Expected .kb/kb.config.json or kb.config.json.');\n}\n\nfunction normalizeConfig(raw: MindRuntimeOptions['config']): MindConfigInput {\n const data = raw as Record<string, unknown>;\n\n // Canonical format: profiles[].products.mind\n if (Array.isArray(data.profiles) && data.profiles.length > 0) {\n const profile = (data.profiles as Array<Record<string, unknown>>).find((p) => p.id === 'default')\n ?? (data.profiles as Array<Record<string, unknown>>)[0];\n const products = profile?.products as Record<string, unknown> | undefined;\n const mindConfig = products?.[MIND_PRODUCT_ID] as MindConfigInput | undefined;\n if (!mindConfig) {\n throw new Error('Config does not contain profiles[].products.mind section.');\n }\n return validateConfig(mindConfig);\n }\n\n // Optional root-level format: { mind: {...} }\n if (data.mind && typeof data.mind === 'object') {\n return validateConfig(data.mind as MindConfigInput);\n }\n\n return validateConfig(data as unknown as MindConfigInput);\n}\n\nfunction validateConfig(config: MindConfigInput): MindConfigInput {\n if (!Array.isArray(config.sources) || !Array.isArray(config.scopes) || !Array.isArray(config.engines)) {\n throw new Error('Invalid mind config: required arrays sources/scopes/engines are missing.');\n }\n return config;\n}\n","import {\n usePlatform,\n useLLM,\n type PlatformServices,\n} from '@kb-labs/sdk';\nimport type {\n MindIntent,\n MindQueryResult,\n MindIndexStats,\n} from '@kb-labs/mind-types';\nimport { createHash } from 'node:crypto';\nimport * as path from 'node:path';\nimport {\n MIND_PRODUCT_ID,\n createMindRuntime,\n} from '../../runtime/mind-runtime';\nimport { loadManifest } from '@kb-labs/mind-engine';\nimport type {\n AgentQueryOrchestrator} from '@kb-labs/mind-orchestrator';\nimport {\n createAgentQueryOrchestrator\n} from '@kb-labs/mind-orchestrator';\n\ntype AgentQueryMode = 'instant' | 'auto' | 'thinking';\ntype AgentResponse = Record<string, unknown>;\ntype AgentErrorResponse = {\n error: { code: string; message: string; recoverable: boolean };\n meta: Record<string, unknown>;\n};\n\n/**\n * Global orchestrator instance for cache persistence across queries\n */\nlet globalOrchestrator: InstanceType<typeof AgentQueryOrchestrator> | null = null;\n\nexport interface RagIndexOptions {\n cwd: string;\n scopeId?: string;\n include?: string;\n exclude?: string;\n skipDeduplication?: boolean;\n platform?: PlatformServices;\n /**\n * Mind configuration (from ctx.config)\n * If provided, will be used instead of reading from file\n */\n config?: any;\n}\n\n/**\n * Information about which adapters were used during indexing\n */\nexport interface AdapterInfo {\n vectorStore: string;\n embeddings: string;\n storage: string;\n llm: string;\n cache: string;\n}\n\nexport interface RagIndexStats extends MindIndexStats {\n deletedFiles?: number;\n deletedChunks?: number;\n invalidChunks?: number;\n}\n\nexport interface RagIndexResult {\n scopeIds: string[];\n adapters: AdapterInfo;\n stats: RagIndexStats;\n}\n\nexport interface RagIndexOptionsWithRuntime extends RagIndexOptions {\n runtime?: Parameters<typeof createMindRuntime>[0]['runtime'];\n}\n\n/**\n * Get adapter name from platform service or fallback\n */\nfunction getAdapterName(service: any, fallback: string): string {\n if (!service) {return fallback;}\n // Try to get constructor name or class name\n const name = service.constructor?.name || service.name || service.id;\n if (name && name !== 'Object' && name !== 'Function') {\n return name;\n }\n return fallback;\n}\n\nexport async function runRagIndex(\n options: RagIndexOptions | RagIndexOptionsWithRuntime,\n): Promise<RagIndexResult> {\n // Use SDK's usePlatform() to get global platform singleton\n const platform = options.platform ?? usePlatform();\n\n // Collect adapter info - shows actual adapters being used\n const adapters: AdapterInfo = {\n vectorStore: getAdapterName(platform?.vectorStore, 'LocalVectorStore (fallback)'),\n embeddings: getAdapterName(platform?.embeddings, 'DeterministicEmbeddings (fallback)'),\n storage: getAdapterName(platform?.storage, 'MemoryStorage (fallback)'),\n llm: getAdapterName(platform?.llm, 'LocalStubLLM (fallback)'),\n cache: getAdapterName(platform?.cache, 'MemoryCache (fallback)'),\n };\n\n // If include/exclude provided, override paths in all sources (ESLint-style)\n let effectiveConfig = options.config;\n if (options.include || options.exclude) {\n // If config already provided (from useConfig), clone and modify it\n // If not provided, will be loaded by createMindRuntime\n let mindConfig = effectiveConfig;\n\n // Override paths/exclude in ALL sources (ESLint-style override)\n if (mindConfig?.sources && Array.isArray(mindConfig.sources)) {\n mindConfig = { ...mindConfig };\n mindConfig.sources = mindConfig.sources.map((source: any) => {\n const overriddenSource = { ...source };\n\n // --include overrides paths\n if (options.include) {\n overriddenSource.paths = [options.include];\n }\n\n // --exclude overrides exclude\n if (options.exclude) {\n overriddenSource.exclude = options.exclude.split(',').map(s => s.trim());\n }\n\n return overriddenSource;\n });\n effectiveConfig = mindConfig;\n }\n }\n\n const runtime = await createMindRuntime({\n cwd: options.cwd,\n config: effectiveConfig,\n runtime: 'runtime' in options ? options.runtime : undefined,\n platform: options.platform,\n });\n const allScopeIds = runtime.config.scopes?.map((scope: any) => scope.id) ?? [];\n if (!allScopeIds.length) {\n throw new Error('No mind scopes found. Update kb.config.json first.');\n }\n\n const scopeIds = options.scopeId\n ? allScopeIds.filter((scopeId: string) => scopeId === options.scopeId)\n : allScopeIds;\n\n if (!scopeIds.length) {\n throw new Error(\n `Scope \"${options.scopeId}\" is not defined in mind.scopes.`,\n );\n }\n\n // Set skip deduplication env var if requested\n const originalSkipDedup = process.env.KB_SKIP_DEDUPLICATION;\n if (options.skipDeduplication) {\n process.env.KB_SKIP_DEDUPLICATION = 'true';\n }\n\n // Aggregate stats across all scopes\n const aggregatedStats = {\n filesDiscovered: 0,\n filesProcessed: 0,\n filesSkipped: 0,\n chunksStored: 0,\n chunksUpdated: 0,\n chunksSkipped: 0,\n errorCount: 0,\n durationMs: 0,\n deletedFiles: 0,\n deletedChunks: 0,\n invalidChunks: 0,\n } satisfies RagIndexStats;\n\n try {\n for (const scopeId of scopeIds) {\n const scopeStats = await runtime.service.index(scopeId) as RagIndexStats;\n if (scopeStats) {\n aggregatedStats.filesDiscovered += scopeStats.filesDiscovered;\n aggregatedStats.filesProcessed += scopeStats.filesProcessed;\n aggregatedStats.filesSkipped += scopeStats.filesSkipped;\n aggregatedStats.chunksStored += scopeStats.chunksStored;\n aggregatedStats.chunksUpdated += scopeStats.chunksUpdated;\n aggregatedStats.chunksSkipped += scopeStats.chunksSkipped;\n aggregatedStats.errorCount += scopeStats.errorCount;\n aggregatedStats.durationMs += scopeStats.durationMs;\n aggregatedStats.deletedFiles = (aggregatedStats.deletedFiles ?? 0) + (scopeStats.deletedFiles ?? 0);\n aggregatedStats.deletedChunks = (aggregatedStats.deletedChunks ?? 0) + (scopeStats.deletedChunks ?? 0);\n aggregatedStats.invalidChunks = (aggregatedStats.invalidChunks ?? 0) + (scopeStats.invalidChunks ?? 0);\n }\n }\n } finally {\n // Restore original env var value\n if (originalSkipDedup === undefined) {\n delete process.env.KB_SKIP_DEDUPLICATION;\n } else {\n process.env.KB_SKIP_DEDUPLICATION = originalSkipDedup;\n }\n }\n\n // Invalidate query cache after re-indexing\n // This ensures fresh results after index update\n if (globalOrchestrator) {\n await globalOrchestrator.invalidateCache(scopeIds);\n }\n\n return { scopeIds, adapters, stats: aggregatedStats };\n}\n\nexport interface RagQueryOptions {\n cwd: string;\n scopeId?: string;\n text: string;\n intent?: MindIntent;\n limit?: number;\n profileId?: string;\n runtime?: Parameters<typeof createMindRuntime>[0]['runtime'];\n onProgress?: (stage: string, details?: string) => void;\n platform?: PlatformServices;\n /**\n * Mind configuration (from ctx.config)\n * If provided, will be used instead of reading from file\n */\n config?: any;\n}\n\nexport interface RagQueryResult {\n scopeId: string;\n result: MindQueryResult;\n}\n\nexport async function runRagQuery(\n options: RagQueryOptions,\n): Promise<RagQueryResult> {\n // Convert onProgress from (stage, details) to ProgressEvent format\n const onProgressEvent = options.onProgress\n ? (event: { stage: string; details?: string; metadata?: Record<string, unknown>; timestamp: number }) => {\n try {\n // Map engine stage names to human-readable messages\n const stageMap: Record<string, string> = {\n 'using_reasoning_engine': 'Using reasoning engine',\n 'reasoning_completed': 'Reasoning completed',\n 'analyzing_query_complexity': 'Analyzing query complexity',\n 'query_is_simple': 'Query is simple',\n 'planning_query': 'Planning query',\n 'query_plan_generated': 'Query plan generated',\n 'executing_subqueries': 'Executing subqueries',\n 'subqueries_completed': 'Subqueries completed',\n 'synthesizing_context': 'Synthesizing context',\n 'context_synthesis_completed': 'Context synthesis completed',\n 'generating_embedding': 'Generating embeddings',\n 'performing_hybrid_search': 'Performing hybrid search',\n 'searching_vector_store': 'Searching vector store',\n 'search_completed': 'Search completed',\n 'applying_popularity_boost': 'Applying popularity boost',\n 'applying_query_pattern_boost': 'Applying query pattern boost',\n 're_ranking_results': 'Re-ranking results',\n 're_ranking_completed': 'Re-ranking completed',\n 'compression_applied': 'Compression applied',\n 'saving_query_history': 'Saving query history',\n };\n \n const humanReadableStage = stageMap[event.stage] || event.stage;\n\n // Extract interesting details from metadata\n let enhancedDetails = event.details;\n if (event.metadata) {\n // Show subqueries if available\n if (event.metadata.subqueries && Array.isArray(event.metadata.subqueries)) {\n const subqueryList = event.metadata.subqueries.slice(0, 3).join(', ');\n const count = event.metadata.subqueries.length;\n enhancedDetails = `${count} subqueries: ${subqueryList}${count > 3 ? '...' : ''}`;\n }\n // Show result count if available\n else if (typeof event.metadata.resultCount === 'number') {\n enhancedDetails = `${event.metadata.resultCount} results`;\n }\n // Show chunk count if available\n else if (typeof event.metadata.chunkCount === 'number') {\n enhancedDetails = `${event.metadata.chunkCount} chunks`;\n }\n }\n\n if (options.onProgress) {\n options.onProgress(humanReadableStage, enhancedDetails);\n }\n } catch (error) {\n // Don't break query if progress callback fails\n // Error is silently ignored\n }\n }\n : undefined;\n\n // Wrap runtime to suppress INFO logs but allow WARN/ERROR\n const originalRuntime = options.runtime;\n \n const wrappedRuntime = originalRuntime ? {\n ...originalRuntime,\n log: (level: 'debug' | 'info' | 'warn' | 'error', message: string, meta?: any) => {\n // Suppress INFO and DEBUG logs - progress comes from explicit API now\n if (level === 'info' || level === 'debug') {\n return; // Don't output INFO/DEBUG logs\n }\n \n // Call original logger for warnings and errors only\n if (originalRuntime.log && (level === 'warn' || level === 'error')) {\n originalRuntime.log(level, message, meta);\n }\n },\n } : undefined;\n\n const runtime = await createMindRuntime({\n cwd: options.cwd,\n config: options.config,\n runtime: wrappedRuntime,\n onProgress: onProgressEvent,\n platform: options.platform,\n });\n \n options.onProgress?.('Initializing Mind runtime');\n\n const defaultScopeId = runtime.config.scopes?.[0]?.id;\n const scopeId = options.scopeId ?? defaultScopeId;\n if (!scopeId) {\n throw new Error(\n 'No mind scopes configured. Provide at least one scope in kb.config.json.',\n );\n }\n\n options.onProgress?.('Preparing query', `scope: ${scopeId}`);\n\n options.onProgress?.('Searching Mind index');\n\n const result = await runtime.service.query({\n productId: MIND_PRODUCT_ID,\n intent: options.intent ?? 'summary',\n scopeId,\n text: options.text,\n limit: options.limit,\n profileId: options.profileId,\n });\n\n options.onProgress?.('Processing results', `${result.chunks.length} chunks found`);\n\n return {\n scopeId,\n result,\n };\n}\n\n// === Agent-optimized RAG Query ===\n\nexport interface AgentRagQueryOptions {\n cwd: string;\n scopeId?: string;\n text: string;\n mode?: AgentQueryMode;\n indexRevision?: string;\n engineConfigHash?: string;\n sourcesDigest?: string;\n debug?: boolean;\n runtime?: Parameters<typeof createMindRuntime>[0]['runtime'];\n broker?: any; // StateBroker-like interface (duck typing to avoid circular deps)\n platform?: PlatformServices;\n /**\n * Mind configuration (from ctx.config)\n * If provided, will be used instead of reading from file\n */\n config?: any;\n}\n\nexport type AgentRagQueryResult = AgentResponse | AgentErrorResponse;\n\ninterface CacheContext {\n indexRevision?: string;\n engineConfigHash?: string;\n sourcesDigest?: string;\n}\n\ninterface ManifestCacheContext {\n found: boolean;\n indexRevision?: string;\n engineConfigHash?: string;\n sourcesDigest?: string;\n}\n\n/**\n * Run agent-optimized RAG query with orchestration.\n *\n * This function uses the orchestrator pipeline:\n * 1. Detect query complexity\n * 2. Decompose into sub-queries (auto/thinking modes)\n * 3. Gather chunks from mind-engine\n * 4. Check completeness (with retry in thinking mode)\n * 5. Synthesize agent-friendly response\n * 6. Compress if needed\n *\n * @returns AgentResponse | AgentErrorResponse - clean JSON for agents\n */\nexport async function runAgentRagQuery(\n options: AgentRagQueryOptions,\n): Promise<AgentRagQueryResult> {\n const platformBroker = options.platform?.cache\n ? {\n get: <T>(key: string) => options.platform!.cache!.get<T>(key),\n set: <T>(key: string, value: T, ttl?: number) => options.platform!.cache!.set(key, value, ttl),\n delete: (key: string) => options.platform!.cache!.delete(key),\n }\n : undefined;\n\n const ragLlm = useLLM({\n execution: {\n cache: {\n mode: 'prefer',\n scope: 'segments',\n },\n stream: {\n mode: 'prefer',\n fallbackToComplete: true,\n },\n },\n });\n\n // Always recreate orchestrator to use fresh LLM from useLLM()\n // This ensures analytics wrappers are always applied\n globalOrchestrator = createAgentQueryOrchestrator({\n llm: ragLlm, // Fresh LLM with analytics wrapper + cache/stream policy\n broker: options.broker ?? platformBroker, // Pass broker for persistent caching\n analyticsAdapter: options.platform?.analytics ?? null,\n config: {\n mode: options.mode ?? 'auto',\n autoDetectComplexity: true,\n },\n });\n\n const orchestrator = globalOrchestrator;\n\n // Create runtime\n const runtime = await createMindRuntime({\n cwd: options.cwd,\n config: options.config,\n runtime: options.runtime,\n platform: options.platform,\n });\n\n // Get scope ID\n const defaultScopeId = runtime.config.scopes?.[0]?.id;\n const scopeId = options.scopeId ?? defaultScopeId;\n\n if (!scopeId) {\n return {\n error: {\n code: 'KNOWLEDGE_SCOPE_NOT_FOUND',\n message: 'No mind scopes configured. Provide at least one scope in kb.config.json.',\n recoverable: false,\n },\n meta: {\n schemaVersion: 'agent-response-v1',\n requestId: `rq-${Date.now()}`,\n mode: options.mode ?? 'auto',\n timingMs: 0,\n cached: false,\n },\n };\n }\n\n const cacheContext = await resolveCacheContext({\n cwd: options.cwd,\n scopeId,\n config: runtime.config,\n providedIndexRevision: options.indexRevision,\n providedEngineConfigHash: options.engineConfigHash,\n providedSourcesDigest: options.sourcesDigest,\n });\n\n // Create query function for orchestrator with adaptive weights support\n const queryFn = async (queryOptions: {\n text: string;\n intent?: MindIntent;\n limit?: number;\n vectorWeight?: number;\n keywordWeight?: number;\n }) => {\n const result = await runtime.service.query({\n productId: MIND_PRODUCT_ID,\n intent: queryOptions.intent ?? 'search',\n scopeId,\n text: queryOptions.text,\n limit: queryOptions.limit,\n // Pass adaptive weights via metadata for mind-engine to use\n metadata: {\n agentMode: true,\n consumer: 'agent',\n mode: options.mode ?? 'auto',\n ...(queryOptions.vectorWeight !== undefined && queryOptions.keywordWeight !== undefined\n ? {\n vectorWeight: queryOptions.vectorWeight,\n keywordWeight: queryOptions.keywordWeight,\n }\n : {}),\n },\n });\n\n return {\n chunks: result.chunks,\n metadata: result.metadata ?? {},\n };\n };\n\n // Execute orchestrated query\n return orchestrator.query(\n {\n cwd: options.cwd,\n scopeId,\n text: options.text,\n mode: options.mode,\n indexRevision: cacheContext.indexRevision,\n engineConfigHash: cacheContext.engineConfigHash,\n sourcesDigest: cacheContext.sourcesDigest,\n debug: options.debug,\n },\n queryFn,\n );\n}\n\nasync function resolveCacheContext(options: {\n cwd: string;\n scopeId: string;\n config: any;\n providedIndexRevision?: string;\n providedEngineConfigHash?: string;\n providedSourcesDigest?: string;\n}): Promise<CacheContext> {\n const manifestContext = await readCacheContextFromManifest(options.cwd, options.config, options.scopeId);\n\n const indexRevision = options.providedIndexRevision\n ?? manifestContext.indexRevision;\n const engineConfigHash = options.providedEngineConfigHash\n ?? manifestContext.engineConfigHash\n ?? computeEngineConfigHash(options.config, options.scopeId);\n const sourcesDigest = options.providedSourcesDigest\n ?? manifestContext.sourcesDigest;\n\n return {\n indexRevision,\n engineConfigHash,\n sourcesDigest,\n };\n}\n\nfunction computeEngineConfigHash(config: any, scopeId: string): string | undefined {\n const scope = Array.isArray(config?.scopes)\n ? config.scopes.find((item: any) => item?.id === scopeId)\n : undefined;\n const engineId = scope?.defaultEngine\n ?? config?.defaults?.fallbackEngineId\n ?? config?.engines?.[0]?.id;\n const engine = Array.isArray(config?.engines)\n ? config.engines.find((item: any) => item?.id === engineId)\n : undefined;\n\n if (!engine) {\n return undefined;\n }\n\n const sanitized = {\n id: engine.id,\n type: engine.type,\n options: sanitizeEngineOptionsForHash(engine.options ?? {}),\n };\n\n return createHash('sha256').update(stableStringify(sanitized)).digest('hex');\n}\n\nasync function readCacheContextFromManifest(\n cwd: string,\n config: any,\n scopeId: string,\n): Promise<ManifestCacheContext> {\n const scope = Array.isArray(config?.scopes)\n ? config.scopes.find((item: any) => item?.id === scopeId)\n : undefined;\n const engineId = scope?.defaultEngine\n ?? config?.defaults?.fallbackEngineId\n ?? config?.engines?.[0]?.id;\n const engine = Array.isArray(config?.engines)\n ? config.engines.find((item: any) => item?.id === engineId)\n : undefined;\n const configuredIndexDir = typeof engine?.options?.indexDir === 'string'\n ? engine.options.indexDir\n : '.kb/mind/rag';\n\n const candidatePaths = [\n path.resolve(cwd, configuredIndexDir, scopeId, 'manifest.json'),\n path.resolve(cwd, configuredIndexDir, 'manifest.json'),\n path.resolve(cwd, '.kb/mind/indexes', scopeId, 'manifest.json'),\n path.resolve(cwd, '.kb/mind/rag', scopeId, 'manifest.json'),\n ];\n\n for (const manifestPath of candidatePaths) {\n try {\n const manifest = await loadManifest(manifestPath);\n const indexRevision = (manifest as { indexRevision?: unknown }).indexRevision;\n const engineConfigHash = (manifest as { engineConfigHash?: unknown }).engineConfigHash;\n const sourcesDigest = (manifest as { sourcesDigest?: unknown }).sourcesDigest;\n\n if (typeof indexRevision !== 'string' || indexRevision.length === 0) {\n throw new Error('missing indexRevision');\n }\n\n if (typeof engineConfigHash !== 'string' || engineConfigHash.length === 0) {\n throw new Error('missing engineConfigHash');\n }\n if (typeof sourcesDigest !== 'string' || sourcesDigest.length === 0) {\n throw new Error('missing sourcesDigest');\n }\n\n return {\n found: true,\n indexRevision,\n engineConfigHash,\n sourcesDigest,\n };\n } catch (error) {\n const code = (error as NodeJS.ErrnoException | undefined)?.code;\n if (code === 'ENOENT') {\n continue;\n }\n\n const message = error instanceof Error ? error.message : String(error);\n throw new Error(`Invalid index manifest at ${manifestPath}: ${message}`);\n }\n }\n\n return { found: false };\n}\n\nfunction sanitizeEngineOptionsForHash(options: Record<string, unknown>): Record<string, unknown> {\n const {\n _runtime: _runtimeIgnored,\n onProgress: _onProgressIgnored,\n platform: _platformIgnored,\n ...rest\n } = options as Record<string, unknown>;\n return rest;\n}\n\nfunction stableStringify(value: unknown): string {\n return JSON.stringify(sortObjectDeep(value));\n}\n\nfunction sortObjectDeep(value: unknown): unknown {\n if (Array.isArray(value)) {\n return value.map(sortObjectDeep);\n }\n if (value && typeof value === 'object') {\n const entries = Object.entries(value as Record<string, unknown>)\n .sort(([a], [b]) => a.localeCompare(b))\n .map(([key, val]) => [key, sortObjectDeep(val)] as const);\n return Object.fromEntries(entries);\n }\n return value;\n}\n","/**\n * Mind rag-index command - build Mind indexes (V3)\n */\n\nimport { defineCommand, useConfig, usePlatform, useLoader, type PluginContextV3 } from '@kb-labs/sdk';\nimport { runRagIndex } from '../../features/rag';\n\ninterface RagIndexInput {\n argv: string[];\n flags: {\n cwd?: string;\n scope?: string;\n include?: string;\n exclude?: string;\n skipDeduplication?: boolean;\n json?: boolean;\n quiet?: boolean;\n };\n}\n\ninterface RagIndexResult {\n exitCode: number;\n ok: boolean;\n scopes?: string[];\n adapters?: {\n vectorStore: string;\n embeddings: string;\n storage: string;\n llm: string;\n cache: string;\n };\n}\n\nexport default defineCommand({\n id: 'mind:rag-index',\n description: 'Build Mind indexes',\n\n handler: {\n async execute(ctx: PluginContextV3, input: RagIndexInput): Promise<RagIndexResult> {\n const startTime = Date.now();\n const { flags } = input;\n\n // Get Mind config using useConfig() helper\n const mindConfig = await useConfig();\n\n const cwd = flags.cwd || ctx.cwd;\n const scopeId = flags.scope;\n const include = flags.include;\n const exclude = flags.exclude;\n const skipDeduplication = flags.skipDeduplication;\n\n // Get platform for analytics (not passed to Mind - child process uses usePlatform())\n const platform = usePlatform();\n\n // Use loader for visual feedback (unless quiet/json mode)\n const loader = !flags.quiet && !flags.json ? useLoader('Building Mind RAG index...') : null;\n loader?.start();\n\n try {\n // Pass mindConfig from useConfig() - avoids reloading config in child process\n // IMPORTANT: Pass platform so Mind engine uses wrapped adapters with analytics tracking\n const result = await runRagIndex({\n cwd,\n scopeId,\n include,\n exclude,\n skipDeduplication,\n config: mindConfig,\n platform,\n });\n\n const timing = Date.now() - startTime;\n loader?.succeed(`Index built in ${(timing / 1000).toFixed(1)}s`);\n\n // Track analytics if available (runs in parent process)\n platform?.analytics?.track?.('mind.rag-index', {\n scopeIds: result.scopeIds,\n stats: result.stats,\n }).catch(() => {});\n\n if (flags.json) {\n ctx.ui.json({\n ok: true,\n scopes: result.scopeIds,\n stats: result.stats,\n adapters: result.adapters,\n timingMs: timing,\n });\n } else if (!flags.quiet) {\n const { stats } = result;\n const percentage = stats.filesDiscovered > 0\n ? ((stats.filesProcessed / stats.filesDiscovered) * 100).toFixed(1)\n : '0.0';\n const chunksPerFile = stats.filesProcessed > 0\n ? (stats.chunksStored / stats.filesProcessed).toFixed(2)\n : '0.00';\n\n ctx.ui.success(\n `Indexed ${stats.filesProcessed} files, ${stats.filesSkipped} skipped, ${stats.chunksStored} chunks, deleted ${stats.deletedFiles ?? 0} files/${stats.deletedChunks ?? 0} chunks`,\n {\n title: 'Mind RAG Index',\n sections: [\n {\n header: 'Files',\n items: [\n `Discovered: ${stats.filesDiscovered}`,\n `Processed: ${stats.filesProcessed} (${percentage}%)`,\n `Skipped: ${stats.filesSkipped}`,\n ],\n },\n {\n header: 'Chunks',\n items: [\n `Stored: ${stats.chunksStored}`,\n `Updated: ${stats.chunksUpdated}`,\n `Skipped: ${stats.chunksSkipped}`,\n `Rate: ${chunksPerFile}/file`,\n ],\n },\n {\n header: 'Cleanup',\n items: [\n `Deleted files: ${stats.deletedFiles ?? 0}`,\n `Deleted chunks: ${stats.deletedChunks ?? 0}`,\n `Invalid chunks: ${stats.invalidChunks ?? 0}`,\n ],\n },\n {\n header: 'Health',\n items: [\n `Errors: ${stats.errorCount}`,\n ],\n },\n ],\n timing,\n }\n );\n }\n\n return {\n exitCode: 0,\n ok: true,\n scopes: result.scopeIds,\n adapters: result.adapters,\n };\n } catch (error) {\n const timing = Date.now() - startTime;\n const message = error instanceof Error ? error.message : String(error);\n loader?.fail(`Index build failed: ${message}`);\n\n if (flags.json) {\n ctx.ui.info(JSON.stringify({\n ok: false,\n error: message,\n timingMs: timing,\n }));\n } else if (!flags.quiet) {\n ctx.ui.error(`Index build failed: ${message}`);\n }\n\n // Track analytics\n platform?.analytics?.track?.('mind.rag-index', {\n error: true,\n errorMessage: message,\n timingMs: timing,\n }).catch(() => {});\n\n return { exitCode: 1, ok: false };\n }\n },\n },\n});\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as _kb_labs_shared_command_kit from '@kb-labs/shared-command-kit';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Mind rag-query command - semantic RAG search (V3)
|
|
5
|
+
*/
|
|
6
|
+
interface RagQueryInput {
|
|
7
|
+
argv: string[];
|
|
8
|
+
flags: {
|
|
9
|
+
cwd?: string;
|
|
10
|
+
scope?: string;
|
|
11
|
+
text?: string;
|
|
12
|
+
intent?: string;
|
|
13
|
+
limit?: number;
|
|
14
|
+
profile?: string;
|
|
15
|
+
mode?: string;
|
|
16
|
+
format?: string;
|
|
17
|
+
json?: boolean;
|
|
18
|
+
quiet?: boolean;
|
|
19
|
+
agent?: boolean;
|
|
20
|
+
debug?: boolean;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
declare const _default: _kb_labs_shared_command_kit.CommandHandlerV3<unknown, RagQueryInput, any>;
|
|
24
|
+
|
|
25
|
+
export { _default as default };
|