@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/dist/index.js ADDED
@@ -0,0 +1,1499 @@
1
+ import { combinePermissions, kbPlatformPreset, defineCommand, useConfig as useConfig$1, usePlatform, useLoader, useLLM } from '@kb-labs/sdk';
2
+ import { readFile } from 'fs/promises';
3
+ import * as path2 from 'path';
4
+ import { join } from 'path';
5
+ import { loadManifest, MindEngine } from '@kb-labs/mind-engine';
6
+ import { createHash } from 'crypto';
7
+ import { isAgentError, createAgentQueryOrchestrator } from '@kb-labs/mind-orchestrator';
8
+ import { defaultMindSyncConfig } from '@kb-labs/mind-contracts';
9
+ import { z } from 'zod';
10
+ import { initMindStructure } from '@kb-labs/mind-indexer';
11
+ import { promises } from 'fs';
12
+
13
+ // src/manifest.v3.ts
14
+ var pluginPermissions = combinePermissions().with(kbPlatformPreset).withEnv([
15
+ "NODE_ENV",
16
+ "OPENAI_API_KEY",
17
+ "QDRANT_URL",
18
+ "QDRANT_API_KEY",
19
+ "EMBEDDING_PROVIDER",
20
+ "VECTOR_STORE_TYPE"
21
+ ]).withFs({
22
+ mode: "readWrite",
23
+ allow: [
24
+ ".kb/mind/**",
25
+ // Mind index data
26
+ ".kb/cache/**"
27
+ // Cache directory
28
+ ]
29
+ }).withFs({
30
+ mode: "read",
31
+ allow: [
32
+ "package.json",
33
+ "**/package.json",
34
+ "config/**",
35
+ "**/*.ts",
36
+ "**/*.tsx",
37
+ "**/*.js",
38
+ "**/*.jsx",
39
+ "**/*.md"
40
+ ]
41
+ }).withNetwork({
42
+ fetch: [
43
+ "https://api.openai.com/*",
44
+ // OpenAI embeddings/LLM
45
+ "http://localhost:6333/*",
46
+ // Qdrant vector store (local)
47
+ "http://127.0.0.1:6333/*",
48
+ "https://*.qdrant.io/*"
49
+ // Qdrant cloud
50
+ ]
51
+ }).withPlatform({
52
+ llm: true,
53
+ // LLM for query orchestration
54
+ embeddings: true,
55
+ // Embedding generation
56
+ vectorStore: { collections: ["mind:"] },
57
+ // Vector DB with mind: namespace
58
+ cache: true,
59
+ // State caching
60
+ analytics: true,
61
+ // Analytics tracking
62
+ storage: true
63
+ // Artifact storage
64
+ }).withQuotas({
65
+ timeoutMs: 12e5,
66
+ // 20 minutes for indexing
67
+ memoryMb: 4096,
68
+ // 4GB for large codebases
69
+ cpuMs: 6e5
70
+ // 10 minutes CPU time
71
+ }).build();
72
+ var manifest = {
73
+ schema: "kb.plugin/3",
74
+ id: "@kb-labs/mind",
75
+ version: "0.1.0",
76
+ display: {
77
+ name: "Mind",
78
+ description: "AI-powered code search and RAG system for semantic codebase understanding.",
79
+ tags: ["search", "rag", "ai", "semantic", "mind-index"]
80
+ },
81
+ // Configuration section in kb.config.json
82
+ configSection: "mind",
83
+ // Platform requirements
84
+ platform: {
85
+ requires: ["llm", "embeddings", "vectorStore", "cache", "storage"],
86
+ optional: ["analytics", "logger"]
87
+ },
88
+ // ✅ PERMISSIONS DEFINED ONCE FOR ENTIRE PLUGIN (Manifest-First)
89
+ // All commands, routes, and actions inherit these permissions
90
+ permissions: pluginPermissions,
91
+ // CLI commands (V3 structure with cli wrapper)
92
+ cli: {
93
+ commands: [
94
+ {
95
+ id: "mind:init",
96
+ group: "mind",
97
+ describe: "Initialize mind workspace",
98
+ handler: "./cli/commands/init.js#default",
99
+ handlerPath: "./cli/commands/init.js"
100
+ },
101
+ {
102
+ id: "mind:verify",
103
+ group: "mind",
104
+ describe: "Verify workspace consistency",
105
+ handler: "./cli/commands/verify.js#default",
106
+ handlerPath: "./cli/commands/verify.js"
107
+ },
108
+ {
109
+ id: "mind:rag-index",
110
+ group: "mind",
111
+ describe: "Build Mind indexes",
112
+ handler: "./cli/commands/rag-index.js#default",
113
+ handlerPath: "./cli/commands/rag-index.js"
114
+ },
115
+ {
116
+ id: "mind:rag-query",
117
+ group: "mind",
118
+ describe: "Run semantic RAG query",
119
+ handler: "./cli/commands/rag-query.js#default",
120
+ handlerPath: "./cli/commands/rag-query.js"
121
+ },
122
+ // Sync commands (5 separate commands instead of subcommands)
123
+ {
124
+ id: "mind:sync-add",
125
+ group: "mind",
126
+ describe: "Add document to sync",
127
+ handler: "./cli/commands/sync-add.js#default",
128
+ handlerPath: "./cli/commands/sync-add.js"
129
+ },
130
+ {
131
+ id: "mind:sync-update",
132
+ group: "mind",
133
+ describe: "Update synced document",
134
+ handler: "./cli/commands/sync-update.js#default",
135
+ handlerPath: "./cli/commands/sync-update.js"
136
+ },
137
+ {
138
+ id: "mind:sync-delete",
139
+ group: "mind",
140
+ describe: "Delete synced document",
141
+ handler: "./cli/commands/sync-delete.js#default",
142
+ handlerPath: "./cli/commands/sync-delete.js"
143
+ },
144
+ {
145
+ id: "mind:sync-list",
146
+ group: "mind",
147
+ describe: "List synced documents",
148
+ handler: "./cli/commands/sync-list.js#default",
149
+ handlerPath: "./cli/commands/sync-list.js"
150
+ },
151
+ {
152
+ id: "mind:sync-status",
153
+ group: "mind",
154
+ describe: "Show sync status",
155
+ handler: "./cli/commands/sync-status.js#default",
156
+ handlerPath: "./cli/commands/sync-status.js"
157
+ }
158
+ ]
159
+ },
160
+ // Scheduled jobs (inherit permissions from manifest)
161
+ actions: [
162
+ {
163
+ id: "auto-index",
164
+ handler: "./handlers/auto-index.js#run",
165
+ schedule: "0 * * * *",
166
+ // Every hour
167
+ description: "Automatically index Mind RAG database",
168
+ enabled: false
169
+ // Disabled by default
170
+ }
171
+ ],
172
+ // Artifacts
173
+ artifacts: [
174
+ {
175
+ id: "mind.index.json",
176
+ pathTemplate: ".kb/mind/index/index.json",
177
+ description: "Mind RAG index metadata."
178
+ },
179
+ {
180
+ id: "mind.cache.json",
181
+ pathTemplate: ".kb/cache/mind-*.json",
182
+ description: "Mind query cache files."
183
+ }
184
+ ]
185
+ };
186
+ var MIND_PRODUCT_ID = "mind";
187
+ async function createMindRuntime(options) {
188
+ const config = await resolveConfig(options.cwd, options.config);
189
+ const platform = options.platform ?? usePlatform();
190
+ const service = {
191
+ index: async (scopeId) => {
192
+ const scope = resolveScope(config, scopeId);
193
+ const engine = createEngine(config, scope, options.cwd, options.runtime, platform, options.onProgress);
194
+ await engine.init();
195
+ const stats = await engine.index(resolveSources(config, scope), {
196
+ scope,
197
+ workspaceRoot: options.cwd
198
+ });
199
+ return stats;
200
+ },
201
+ query: async (queryOptions) => {
202
+ if (queryOptions.productId && queryOptions.productId !== MIND_PRODUCT_ID) {
203
+ throw new Error(`Unsupported productId "${queryOptions.productId}". Expected "${MIND_PRODUCT_ID}".`);
204
+ }
205
+ const scope = resolveScope(config, queryOptions.scopeId);
206
+ const sources = resolveSources(config, scope);
207
+ const engine = createEngine(config, scope, options.cwd, options.runtime, platform, options.onProgress);
208
+ await engine.init();
209
+ const result = await engine.query(
210
+ {
211
+ text: queryOptions.text,
212
+ intent: queryOptions.intent ?? "summary",
213
+ limit: queryOptions.limit,
214
+ profileId: queryOptions.profileId,
215
+ metadata: queryOptions.metadata
216
+ },
217
+ {
218
+ scope,
219
+ sources,
220
+ workspaceRoot: options.cwd,
221
+ limit: queryOptions.limit,
222
+ profile: queryOptions.profileId ? { id: queryOptions.profileId } : void 0
223
+ }
224
+ );
225
+ return result;
226
+ }
227
+ };
228
+ return {
229
+ service,
230
+ config
231
+ };
232
+ }
233
+ function createEngine(config, scope, cwd, runtime, platform, onProgress) {
234
+ const engineConfig = resolveEngineConfig(config, scope);
235
+ return new MindEngine(
236
+ {
237
+ id: engineConfig.id,
238
+ type: engineConfig.type,
239
+ options: {
240
+ ...engineConfig.options ?? {},
241
+ _runtime: runtime,
242
+ platform: platform ?? void 0,
243
+ onProgress
244
+ }
245
+ },
246
+ {
247
+ workspaceRoot: cwd
248
+ }
249
+ );
250
+ }
251
+ function resolveSources(config, scope) {
252
+ if (!scope.sourceIds?.length) {
253
+ return config.sources;
254
+ }
255
+ const selected = config.sources.filter((source) => scope.sourceIds.includes(source.id));
256
+ if (!selected.length) {
257
+ throw new Error(`Scope "${scope.id}" does not reference existing sources.`);
258
+ }
259
+ return selected;
260
+ }
261
+ function resolveScope(config, scopeId) {
262
+ const scope = config.scopes.find((item) => item.id === scopeId);
263
+ if (!scope) {
264
+ throw new Error(`Scope "${scopeId}" is not defined in mind.scopes.`);
265
+ }
266
+ return scope;
267
+ }
268
+ function resolveEngineConfig(config, scope) {
269
+ const engineId = scope.defaultEngine ?? config.defaults?.fallbackEngineId ?? config.engines[0]?.id;
270
+ if (!engineId) {
271
+ throw new Error("No engines configured in mind config.");
272
+ }
273
+ const engine = config.engines.find((item) => item.id === engineId);
274
+ if (!engine) {
275
+ throw new Error(`Engine "${engineId}" referenced by scope "${scope.id}" does not exist.`);
276
+ }
277
+ return engine;
278
+ }
279
+ async function resolveConfig(cwd, provided) {
280
+ if (provided) {
281
+ return normalizeConfig(provided);
282
+ }
283
+ const configPath = await findConfigPath(cwd);
284
+ const raw = JSON.parse(await readFile(configPath, "utf8"));
285
+ return normalizeConfig(raw);
286
+ }
287
+ async function findConfigPath(cwd) {
288
+ const candidates = [
289
+ path2.resolve(cwd, ".kb/kb.config.json"),
290
+ path2.resolve(cwd, "kb.config.json")
291
+ ];
292
+ for (const candidate of candidates) {
293
+ try {
294
+ await readFile(candidate, "utf8");
295
+ return candidate;
296
+ } catch {
297
+ }
298
+ }
299
+ throw new Error("No kb.config.json found. Expected .kb/kb.config.json or kb.config.json.");
300
+ }
301
+ function normalizeConfig(raw) {
302
+ const data = raw;
303
+ if (Array.isArray(data.profiles) && data.profiles.length > 0) {
304
+ const profile = data.profiles.find((p) => p.id === "default") ?? data.profiles[0];
305
+ const products = profile?.products;
306
+ const mindConfig = products?.[MIND_PRODUCT_ID];
307
+ if (!mindConfig) {
308
+ throw new Error("Config does not contain profiles[].products.mind section.");
309
+ }
310
+ return validateConfig(mindConfig);
311
+ }
312
+ if (data.mind && typeof data.mind === "object") {
313
+ return validateConfig(data.mind);
314
+ }
315
+ return validateConfig(data);
316
+ }
317
+ function validateConfig(config) {
318
+ if (!Array.isArray(config.sources) || !Array.isArray(config.scopes) || !Array.isArray(config.engines)) {
319
+ throw new Error("Invalid mind config: required arrays sources/scopes/engines are missing.");
320
+ }
321
+ return config;
322
+ }
323
+ var globalOrchestrator = null;
324
+ function getAdapterName(service, fallback) {
325
+ if (!service) {
326
+ return fallback;
327
+ }
328
+ const name = service.constructor?.name || service.name || service.id;
329
+ if (name && name !== "Object" && name !== "Function") {
330
+ return name;
331
+ }
332
+ return fallback;
333
+ }
334
+ async function runRagIndex(options) {
335
+ const platform = options.platform ?? usePlatform();
336
+ const adapters = {
337
+ vectorStore: getAdapterName(platform?.vectorStore, "LocalVectorStore (fallback)"),
338
+ embeddings: getAdapterName(platform?.embeddings, "DeterministicEmbeddings (fallback)"),
339
+ storage: getAdapterName(platform?.storage, "MemoryStorage (fallback)"),
340
+ llm: getAdapterName(platform?.llm, "LocalStubLLM (fallback)"),
341
+ cache: getAdapterName(platform?.cache, "MemoryCache (fallback)")
342
+ };
343
+ let effectiveConfig = options.config;
344
+ if (options.include || options.exclude) {
345
+ let mindConfig = effectiveConfig;
346
+ if (mindConfig?.sources && Array.isArray(mindConfig.sources)) {
347
+ mindConfig = { ...mindConfig };
348
+ mindConfig.sources = mindConfig.sources.map((source) => {
349
+ const overriddenSource = { ...source };
350
+ if (options.include) {
351
+ overriddenSource.paths = [options.include];
352
+ }
353
+ if (options.exclude) {
354
+ overriddenSource.exclude = options.exclude.split(",").map((s) => s.trim());
355
+ }
356
+ return overriddenSource;
357
+ });
358
+ effectiveConfig = mindConfig;
359
+ }
360
+ }
361
+ const runtime = await createMindRuntime({
362
+ cwd: options.cwd,
363
+ config: effectiveConfig,
364
+ runtime: "runtime" in options ? options.runtime : void 0,
365
+ platform: options.platform
366
+ });
367
+ const allScopeIds = runtime.config.scopes?.map((scope) => scope.id) ?? [];
368
+ if (!allScopeIds.length) {
369
+ throw new Error("No mind scopes found. Update kb.config.json first.");
370
+ }
371
+ const scopeIds = options.scopeId ? allScopeIds.filter((scopeId) => scopeId === options.scopeId) : allScopeIds;
372
+ if (!scopeIds.length) {
373
+ throw new Error(
374
+ `Scope "${options.scopeId}" is not defined in mind.scopes.`
375
+ );
376
+ }
377
+ const originalSkipDedup = process.env.KB_SKIP_DEDUPLICATION;
378
+ if (options.skipDeduplication) {
379
+ process.env.KB_SKIP_DEDUPLICATION = "true";
380
+ }
381
+ const aggregatedStats = {
382
+ filesDiscovered: 0,
383
+ filesProcessed: 0,
384
+ filesSkipped: 0,
385
+ chunksStored: 0,
386
+ chunksUpdated: 0,
387
+ chunksSkipped: 0,
388
+ errorCount: 0,
389
+ durationMs: 0,
390
+ deletedFiles: 0,
391
+ deletedChunks: 0,
392
+ invalidChunks: 0
393
+ };
394
+ try {
395
+ for (const scopeId of scopeIds) {
396
+ const scopeStats = await runtime.service.index(scopeId);
397
+ if (scopeStats) {
398
+ aggregatedStats.filesDiscovered += scopeStats.filesDiscovered;
399
+ aggregatedStats.filesProcessed += scopeStats.filesProcessed;
400
+ aggregatedStats.filesSkipped += scopeStats.filesSkipped;
401
+ aggregatedStats.chunksStored += scopeStats.chunksStored;
402
+ aggregatedStats.chunksUpdated += scopeStats.chunksUpdated;
403
+ aggregatedStats.chunksSkipped += scopeStats.chunksSkipped;
404
+ aggregatedStats.errorCount += scopeStats.errorCount;
405
+ aggregatedStats.durationMs += scopeStats.durationMs;
406
+ aggregatedStats.deletedFiles = (aggregatedStats.deletedFiles ?? 0) + (scopeStats.deletedFiles ?? 0);
407
+ aggregatedStats.deletedChunks = (aggregatedStats.deletedChunks ?? 0) + (scopeStats.deletedChunks ?? 0);
408
+ aggregatedStats.invalidChunks = (aggregatedStats.invalidChunks ?? 0) + (scopeStats.invalidChunks ?? 0);
409
+ }
410
+ }
411
+ } finally {
412
+ if (originalSkipDedup === void 0) {
413
+ delete process.env.KB_SKIP_DEDUPLICATION;
414
+ } else {
415
+ process.env.KB_SKIP_DEDUPLICATION = originalSkipDedup;
416
+ }
417
+ }
418
+ if (globalOrchestrator) {
419
+ await globalOrchestrator.invalidateCache(scopeIds);
420
+ }
421
+ return { scopeIds, adapters, stats: aggregatedStats };
422
+ }
423
+ async function runRagQuery(options) {
424
+ const onProgressEvent = options.onProgress ? (event) => {
425
+ try {
426
+ const stageMap = {
427
+ "using_reasoning_engine": "Using reasoning engine",
428
+ "reasoning_completed": "Reasoning completed",
429
+ "analyzing_query_complexity": "Analyzing query complexity",
430
+ "query_is_simple": "Query is simple",
431
+ "planning_query": "Planning query",
432
+ "query_plan_generated": "Query plan generated",
433
+ "executing_subqueries": "Executing subqueries",
434
+ "subqueries_completed": "Subqueries completed",
435
+ "synthesizing_context": "Synthesizing context",
436
+ "context_synthesis_completed": "Context synthesis completed",
437
+ "generating_embedding": "Generating embeddings",
438
+ "performing_hybrid_search": "Performing hybrid search",
439
+ "searching_vector_store": "Searching vector store",
440
+ "search_completed": "Search completed",
441
+ "applying_popularity_boost": "Applying popularity boost",
442
+ "applying_query_pattern_boost": "Applying query pattern boost",
443
+ "re_ranking_results": "Re-ranking results",
444
+ "re_ranking_completed": "Re-ranking completed",
445
+ "compression_applied": "Compression applied",
446
+ "saving_query_history": "Saving query history"
447
+ };
448
+ const humanReadableStage = stageMap[event.stage] || event.stage;
449
+ let enhancedDetails = event.details;
450
+ if (event.metadata) {
451
+ if (event.metadata.subqueries && Array.isArray(event.metadata.subqueries)) {
452
+ const subqueryList = event.metadata.subqueries.slice(0, 3).join(", ");
453
+ const count = event.metadata.subqueries.length;
454
+ enhancedDetails = `${count} subqueries: ${subqueryList}${count > 3 ? "..." : ""}`;
455
+ } else if (typeof event.metadata.resultCount === "number") {
456
+ enhancedDetails = `${event.metadata.resultCount} results`;
457
+ } else if (typeof event.metadata.chunkCount === "number") {
458
+ enhancedDetails = `${event.metadata.chunkCount} chunks`;
459
+ }
460
+ }
461
+ if (options.onProgress) {
462
+ options.onProgress(humanReadableStage, enhancedDetails);
463
+ }
464
+ } catch (error) {
465
+ }
466
+ } : void 0;
467
+ const originalRuntime = options.runtime;
468
+ const wrappedRuntime = originalRuntime ? {
469
+ ...originalRuntime,
470
+ log: (level, message, meta) => {
471
+ if (level === "info" || level === "debug") {
472
+ return;
473
+ }
474
+ if (originalRuntime.log && (level === "warn" || level === "error")) {
475
+ originalRuntime.log(level, message, meta);
476
+ }
477
+ }
478
+ } : void 0;
479
+ const runtime = await createMindRuntime({
480
+ cwd: options.cwd,
481
+ config: options.config,
482
+ runtime: wrappedRuntime,
483
+ onProgress: onProgressEvent,
484
+ platform: options.platform
485
+ });
486
+ options.onProgress?.("Initializing Mind runtime");
487
+ const defaultScopeId = runtime.config.scopes?.[0]?.id;
488
+ const scopeId = options.scopeId ?? defaultScopeId;
489
+ if (!scopeId) {
490
+ throw new Error(
491
+ "No mind scopes configured. Provide at least one scope in kb.config.json."
492
+ );
493
+ }
494
+ options.onProgress?.("Preparing query", `scope: ${scopeId}`);
495
+ options.onProgress?.("Searching Mind index");
496
+ const result = await runtime.service.query({
497
+ productId: MIND_PRODUCT_ID,
498
+ intent: options.intent ?? "summary",
499
+ scopeId,
500
+ text: options.text,
501
+ limit: options.limit,
502
+ profileId: options.profileId
503
+ });
504
+ options.onProgress?.("Processing results", `${result.chunks.length} chunks found`);
505
+ return {
506
+ scopeId,
507
+ result
508
+ };
509
+ }
510
+ async function runAgentRagQuery(options) {
511
+ const platformBroker = options.platform?.cache ? {
512
+ get: (key) => options.platform.cache.get(key),
513
+ set: (key, value, ttl) => options.platform.cache.set(key, value, ttl),
514
+ delete: (key) => options.platform.cache.delete(key)
515
+ } : void 0;
516
+ const ragLlm = useLLM({
517
+ execution: {
518
+ cache: {
519
+ mode: "prefer",
520
+ scope: "segments"
521
+ },
522
+ stream: {
523
+ mode: "prefer",
524
+ fallbackToComplete: true
525
+ }
526
+ }
527
+ });
528
+ globalOrchestrator = createAgentQueryOrchestrator({
529
+ llm: ragLlm,
530
+ // Fresh LLM with analytics wrapper + cache/stream policy
531
+ broker: options.broker ?? platformBroker,
532
+ // Pass broker for persistent caching
533
+ analyticsAdapter: options.platform?.analytics ?? null,
534
+ config: {
535
+ mode: options.mode ?? "auto",
536
+ autoDetectComplexity: true
537
+ }
538
+ });
539
+ const orchestrator = globalOrchestrator;
540
+ const runtime = await createMindRuntime({
541
+ cwd: options.cwd,
542
+ config: options.config,
543
+ runtime: options.runtime,
544
+ platform: options.platform
545
+ });
546
+ const defaultScopeId = runtime.config.scopes?.[0]?.id;
547
+ const scopeId = options.scopeId ?? defaultScopeId;
548
+ if (!scopeId) {
549
+ return {
550
+ error: {
551
+ code: "KNOWLEDGE_SCOPE_NOT_FOUND",
552
+ message: "No mind scopes configured. Provide at least one scope in kb.config.json.",
553
+ recoverable: false
554
+ },
555
+ meta: {
556
+ schemaVersion: "agent-response-v1",
557
+ requestId: `rq-${Date.now()}`,
558
+ mode: options.mode ?? "auto",
559
+ timingMs: 0,
560
+ cached: false
561
+ }
562
+ };
563
+ }
564
+ const cacheContext = await resolveCacheContext({
565
+ cwd: options.cwd,
566
+ scopeId,
567
+ config: runtime.config,
568
+ providedIndexRevision: options.indexRevision,
569
+ providedEngineConfigHash: options.engineConfigHash,
570
+ providedSourcesDigest: options.sourcesDigest
571
+ });
572
+ const queryFn = async (queryOptions) => {
573
+ const result = await runtime.service.query({
574
+ productId: MIND_PRODUCT_ID,
575
+ intent: queryOptions.intent ?? "search",
576
+ scopeId,
577
+ text: queryOptions.text,
578
+ limit: queryOptions.limit,
579
+ // Pass adaptive weights via metadata for mind-engine to use
580
+ metadata: {
581
+ agentMode: true,
582
+ consumer: "agent",
583
+ mode: options.mode ?? "auto",
584
+ ...queryOptions.vectorWeight !== void 0 && queryOptions.keywordWeight !== void 0 ? {
585
+ vectorWeight: queryOptions.vectorWeight,
586
+ keywordWeight: queryOptions.keywordWeight
587
+ } : {}
588
+ }
589
+ });
590
+ return {
591
+ chunks: result.chunks,
592
+ metadata: result.metadata ?? {}
593
+ };
594
+ };
595
+ return orchestrator.query(
596
+ {
597
+ cwd: options.cwd,
598
+ scopeId,
599
+ text: options.text,
600
+ mode: options.mode,
601
+ indexRevision: cacheContext.indexRevision,
602
+ engineConfigHash: cacheContext.engineConfigHash,
603
+ sourcesDigest: cacheContext.sourcesDigest,
604
+ debug: options.debug
605
+ },
606
+ queryFn
607
+ );
608
+ }
609
+ async function resolveCacheContext(options) {
610
+ const manifestContext = await readCacheContextFromManifest(options.cwd, options.config, options.scopeId);
611
+ const indexRevision = options.providedIndexRevision ?? manifestContext.indexRevision;
612
+ const engineConfigHash = options.providedEngineConfigHash ?? manifestContext.engineConfigHash ?? computeEngineConfigHash(options.config, options.scopeId);
613
+ const sourcesDigest = options.providedSourcesDigest ?? manifestContext.sourcesDigest;
614
+ return {
615
+ indexRevision,
616
+ engineConfigHash,
617
+ sourcesDigest
618
+ };
619
+ }
620
+ function computeEngineConfigHash(config, scopeId) {
621
+ const scope = Array.isArray(config?.scopes) ? config.scopes.find((item) => item?.id === scopeId) : void 0;
622
+ const engineId = scope?.defaultEngine ?? config?.defaults?.fallbackEngineId ?? config?.engines?.[0]?.id;
623
+ const engine = Array.isArray(config?.engines) ? config.engines.find((item) => item?.id === engineId) : void 0;
624
+ if (!engine) {
625
+ return void 0;
626
+ }
627
+ const sanitized = {
628
+ id: engine.id,
629
+ type: engine.type,
630
+ options: sanitizeEngineOptionsForHash(engine.options ?? {})
631
+ };
632
+ return createHash("sha256").update(stableStringify(sanitized)).digest("hex");
633
+ }
634
+ async function readCacheContextFromManifest(cwd, config, scopeId) {
635
+ const scope = Array.isArray(config?.scopes) ? config.scopes.find((item) => item?.id === scopeId) : void 0;
636
+ const engineId = scope?.defaultEngine ?? config?.defaults?.fallbackEngineId ?? config?.engines?.[0]?.id;
637
+ const engine = Array.isArray(config?.engines) ? config.engines.find((item) => item?.id === engineId) : void 0;
638
+ const configuredIndexDir = typeof engine?.options?.indexDir === "string" ? engine.options.indexDir : ".kb/mind/rag";
639
+ const candidatePaths = [
640
+ path2.resolve(cwd, configuredIndexDir, scopeId, "manifest.json"),
641
+ path2.resolve(cwd, configuredIndexDir, "manifest.json"),
642
+ path2.resolve(cwd, ".kb/mind/indexes", scopeId, "manifest.json"),
643
+ path2.resolve(cwd, ".kb/mind/rag", scopeId, "manifest.json")
644
+ ];
645
+ for (const manifestPath of candidatePaths) {
646
+ try {
647
+ const manifest2 = await loadManifest(manifestPath);
648
+ const indexRevision = manifest2.indexRevision;
649
+ const engineConfigHash = manifest2.engineConfigHash;
650
+ const sourcesDigest = manifest2.sourcesDigest;
651
+ if (typeof indexRevision !== "string" || indexRevision.length === 0) {
652
+ throw new Error("missing indexRevision");
653
+ }
654
+ if (typeof engineConfigHash !== "string" || engineConfigHash.length === 0) {
655
+ throw new Error("missing engineConfigHash");
656
+ }
657
+ if (typeof sourcesDigest !== "string" || sourcesDigest.length === 0) {
658
+ throw new Error("missing sourcesDigest");
659
+ }
660
+ return {
661
+ found: true,
662
+ indexRevision,
663
+ engineConfigHash,
664
+ sourcesDigest
665
+ };
666
+ } catch (error) {
667
+ const code = error?.code;
668
+ if (code === "ENOENT") {
669
+ continue;
670
+ }
671
+ const message = error instanceof Error ? error.message : String(error);
672
+ throw new Error(`Invalid index manifest at ${manifestPath}: ${message}`);
673
+ }
674
+ }
675
+ return { found: false };
676
+ }
677
+ function sanitizeEngineOptionsForHash(options) {
678
+ const {
679
+ _runtime: _runtimeIgnored,
680
+ onProgress: _onProgressIgnored,
681
+ platform: _platformIgnored,
682
+ ...rest
683
+ } = options;
684
+ return rest;
685
+ }
686
+ function stableStringify(value) {
687
+ return JSON.stringify(sortObjectDeep(value));
688
+ }
689
+ function sortObjectDeep(value) {
690
+ if (Array.isArray(value)) {
691
+ return value.map(sortObjectDeep);
692
+ }
693
+ if (value && typeof value === "object") {
694
+ const entries = Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, val]) => [key, sortObjectDeep(val)]);
695
+ return Object.fromEntries(entries);
696
+ }
697
+ return value;
698
+ }
699
+ function useConfig(ctx) {
700
+ if (!ctx.config) {
701
+ throw new Error("Mind configuration not found in context. Ensure ctx.config is loaded.");
702
+ }
703
+ const config = ctx.config;
704
+ if (!config.sources || !Array.isArray(config.sources)) {
705
+ throw new Error("Invalid mind config: missing or invalid sources");
706
+ }
707
+ if (!config.scopes || !Array.isArray(config.scopes)) {
708
+ throw new Error("Invalid mind config: missing or invalid scopes");
709
+ }
710
+ if (!config.engines || !Array.isArray(config.engines)) {
711
+ throw new Error("Invalid mind config: missing or invalid engines");
712
+ }
713
+ return config;
714
+ }
715
+ function tryUseConfig(ctx) {
716
+ try {
717
+ return useConfig(ctx);
718
+ } catch {
719
+ return null;
720
+ }
721
+ }
722
+ function useSyncConfig(ctx) {
723
+ const config = useConfig(ctx);
724
+ if (!config.sync) {
725
+ return { ...defaultMindSyncConfig };
726
+ }
727
+ return config.sync;
728
+ }
729
+
730
+ // src/infra/analytics/events.ts
731
+ var ANALYTICS_PREFIX = {
732
+ QUERY: "mind.query",
733
+ FEED: "mind.feed",
734
+ UPDATE: "mind.update",
735
+ INIT: "mind.init",
736
+ PACK: "mind.pack",
737
+ VERIFY: "mind.verify"
738
+ };
739
+ var ANALYTICS_SUFFIX = {
740
+ STARTED: "started",
741
+ FINISHED: "finished"
742
+ };
743
+ var ANALYTICS_EVENTS = {
744
+ // Query events
745
+ QUERY_STARTED: `${ANALYTICS_PREFIX.QUERY}.${ANALYTICS_SUFFIX.STARTED}`,
746
+ QUERY_FINISHED: `${ANALYTICS_PREFIX.QUERY}.${ANALYTICS_SUFFIX.FINISHED}`,
747
+ // Feed events
748
+ FEED_STARTED: `${ANALYTICS_PREFIX.FEED}.${ANALYTICS_SUFFIX.STARTED}`,
749
+ FEED_FINISHED: `${ANALYTICS_PREFIX.FEED}.${ANALYTICS_SUFFIX.FINISHED}`,
750
+ // Update events
751
+ UPDATE_STARTED: `${ANALYTICS_PREFIX.UPDATE}.${ANALYTICS_SUFFIX.STARTED}`,
752
+ UPDATE_FINISHED: `${ANALYTICS_PREFIX.UPDATE}.${ANALYTICS_SUFFIX.FINISHED}`,
753
+ // Init events
754
+ INIT_STARTED: `${ANALYTICS_PREFIX.INIT}.${ANALYTICS_SUFFIX.STARTED}`,
755
+ INIT_FINISHED: `${ANALYTICS_PREFIX.INIT}.${ANALYTICS_SUFFIX.FINISHED}`,
756
+ // Pack events
757
+ PACK_STARTED: `${ANALYTICS_PREFIX.PACK}.${ANALYTICS_SUFFIX.STARTED}`,
758
+ PACK_FINISHED: `${ANALYTICS_PREFIX.PACK}.${ANALYTICS_SUFFIX.FINISHED}`,
759
+ // Verify events
760
+ VERIFY_STARTED: `${ANALYTICS_PREFIX.VERIFY}.${ANALYTICS_SUFFIX.STARTED}`,
761
+ VERIFY_FINISHED: `${ANALYTICS_PREFIX.VERIFY}.${ANALYTICS_SUFFIX.FINISHED}`
762
+ };
763
+ var ANALYTICS_ACTOR = {
764
+ type: "agent",
765
+ id: "mind-cli"
766
+ };
767
+ var InitInputSchema = z.object({
768
+ cwd: z.string().optional(),
769
+ force: z.boolean().optional().default(false),
770
+ json: z.boolean().optional().default(false),
771
+ verbose: z.boolean().optional().default(false),
772
+ quiet: z.boolean().optional().default(false)
773
+ });
774
+ var InitOutputSchema = z.object({
775
+ ok: z.boolean(),
776
+ mindDir: z.string(),
777
+ cwd: z.string()
778
+ });
779
+ var UpdateInputSchema = z.object({
780
+ cwd: z.string().optional(),
781
+ since: z.string().optional(),
782
+ timeBudget: z.number().optional(),
783
+ json: z.boolean().optional().default(false),
784
+ verbose: z.boolean().optional().default(false),
785
+ quiet: z.boolean().optional().default(false)
786
+ });
787
+ var UpdateOutputSchema = z.object({
788
+ ok: z.boolean(),
789
+ updated: z.number(),
790
+ duration: z.number()
791
+ });
792
+ var PackInputSchema = z.object({
793
+ cwd: z.string().optional(),
794
+ intent: z.string(),
795
+ product: z.string().optional(),
796
+ preset: z.string().optional(),
797
+ budget: z.number().optional(),
798
+ withBundle: z.boolean().optional().default(false),
799
+ out: z.string().optional(),
800
+ seed: z.number().optional(),
801
+ json: z.boolean().optional().default(false),
802
+ verbose: z.boolean().optional().default(false),
803
+ quiet: z.boolean().optional().default(false)
804
+ });
805
+ var PackOutputSchema = z.object({
806
+ ok: z.boolean(),
807
+ packPath: z.string(),
808
+ size: z.number()
809
+ });
810
+ var FeedInputSchema = z.object({
811
+ cwd: z.string().optional(),
812
+ intent: z.string().optional(),
813
+ product: z.string().optional(),
814
+ preset: z.string().optional(),
815
+ budget: z.number().optional(),
816
+ withBundle: z.boolean().optional().default(false),
817
+ since: z.string().optional(),
818
+ timeBudget: z.number().optional(),
819
+ noUpdate: z.boolean().optional().default(false),
820
+ out: z.string().optional(),
821
+ seed: z.number().optional(),
822
+ json: z.boolean().optional().default(false),
823
+ verbose: z.boolean().optional().default(false),
824
+ quiet: z.boolean().optional().default(false)
825
+ });
826
+ var FeedOutputSchema = z.object({
827
+ ok: z.boolean(),
828
+ packPath: z.string(),
829
+ updated: z.number(),
830
+ duration: z.number()
831
+ });
832
+ var QueryInputSchema = z.object({
833
+ cwd: z.string().optional(),
834
+ query: z.enum(["impact", "scope", "exports", "externals", "chain", "meta", "docs"]),
835
+ file: z.string().optional(),
836
+ path: z.string().optional(),
837
+ scope: z.string().optional(),
838
+ product: z.string().optional(),
839
+ tag: z.string().optional(),
840
+ type: z.string().optional(),
841
+ filter: z.string().optional(),
842
+ limit: z.number().optional().default(500),
843
+ depth: z.number().optional().default(5),
844
+ cacheMode: z.enum(["ci", "local"]).optional().default("local"),
845
+ cacheTtl: z.number().optional().default(60),
846
+ noCache: z.boolean().optional().default(false),
847
+ paths: z.enum(["id", "absolute"]).optional().default("id"),
848
+ aiMode: z.boolean().optional().default(false),
849
+ toon: z.boolean().optional().default(false),
850
+ toonSidecar: z.boolean().optional().default(false),
851
+ json: z.boolean().optional().default(false),
852
+ compact: z.boolean().optional().default(false),
853
+ quiet: z.boolean().optional().default(false)
854
+ });
855
+ var QueryOutputSchema = z.object({
856
+ ok: z.boolean(),
857
+ query: z.string(),
858
+ result: z.any(),
859
+ toonPath: z.string().optional()
860
+ });
861
+ var VerifyInputSchema = z.object({
862
+ cwd: z.string().optional(),
863
+ json: z.boolean().optional().default(false),
864
+ quiet: z.boolean().optional().default(false)
865
+ });
866
+ var VerifyOutputSchema = z.object({
867
+ ok: z.boolean(),
868
+ consistent: z.boolean(),
869
+ errors: z.array(z.object({
870
+ file: z.string(),
871
+ message: z.string()
872
+ }))
873
+ });
874
+
875
+ // src/cli/utils.ts
876
+ var colors = {
877
+ red: (text) => `\x1B[31m${text}\x1B[0m`,
878
+ green: (text) => `\x1B[32m${text}\x1B[0m`,
879
+ yellow: (text) => `\x1B[33m${text}\x1B[0m`,
880
+ blue: (text) => `\x1B[34m${text}\x1B[0m`,
881
+ cyan: (text) => `\x1B[36m${text}\x1B[0m`,
882
+ gray: (text) => `\x1B[90m${text}\x1B[0m`,
883
+ bold: (text) => `\x1B[1m${text}\x1B[0m`,
884
+ dim: (text) => `\x1B[2m${text}\x1B[0m`
885
+ };
886
+ var safeColors = colors;
887
+ var safeSymbols = {
888
+ check: "\u2713",
889
+ cross: "\u2717",
890
+ arrow: "\u2192",
891
+ bullet: "\u2022",
892
+ info: "\u2139",
893
+ warning: "\u26A0",
894
+ error: "\u2717"
895
+ };
896
+ var TimingTracker = class {
897
+ startTime;
898
+ constructor() {
899
+ this.startTime = Date.now();
900
+ }
901
+ getElapsed() {
902
+ return Date.now() - this.startTime;
903
+ }
904
+ getElapsedMs() {
905
+ return this.getElapsed();
906
+ }
907
+ };
908
+ function formatTiming(ms) {
909
+ if (ms < 1e3) {
910
+ return `${ms}ms`;
911
+ }
912
+ return `${(ms / 1e3).toFixed(1)}s`;
913
+ }
914
+ function box(textOrTitle, maybeLines = []) {
915
+ const lines = Array.isArray(maybeLines) ? maybeLines : typeof maybeLines === "string" ? maybeLines.split("\n") : [];
916
+ const rows = [textOrTitle, ...lines, ""];
917
+ return rows.map((line) => line && line.length > 0 ? `\u2502 ${line}` : "\u2502").join("\n");
918
+ }
919
+ function keyValue(arg1, arg2) {
920
+ const format = (key, value) => {
921
+ const label = process.env.NO_COLOR ? key : colors.cyan(key);
922
+ return `${label}: ${value}`;
923
+ };
924
+ if (typeof arg1 === "string" && arg2 !== void 0) {
925
+ return format(arg1, arg2);
926
+ }
927
+ if (arg1 && typeof arg1 === "object") {
928
+ return Object.entries(arg1).map(([key, value]) => format(key, value));
929
+ }
930
+ return "";
931
+ }
932
+ function createSpinner(text) {
933
+ const frames = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
934
+ let frame = 0;
935
+ const interval = setInterval(() => {
936
+ process.stdout.write(`\r${frames[frame]} ${text}`);
937
+ frame = (frame + 1) % frames.length;
938
+ }, 100);
939
+ return {
940
+ stop: (finalText) => {
941
+ clearInterval(interval);
942
+ process.stdout.write(`\r${finalText || text}
943
+ `);
944
+ }
945
+ };
946
+ }
947
+ var init_default = defineCommand({
948
+ id: "mind:init",
949
+ description: "Initialize mind workspace",
950
+ // ❌ NO permissions here - they are in manifest.v3.ts!
951
+ // Permissions are manifest-wide in V3
952
+ handler: {
953
+ async execute(ctx, input) {
954
+ const startTime = Date.now();
955
+ const { flags } = input;
956
+ const cwd = flags.cwd || ctx.cwd;
957
+ ctx.trace?.addEvent?.("mind.init.start", {
958
+ cwd,
959
+ command: "mind:init",
960
+ force: flags.force
961
+ });
962
+ ctx.trace?.addEvent?.("mind.init.initializing", { cwd, force: flags.force });
963
+ const mindDir = await initMindStructure({
964
+ cwd,
965
+ force: flags.force,
966
+ log: (entry) => {
967
+ if (!flags.quiet && !flags.json) {
968
+ ctx.ui.info(`Init: ${entry.msg || entry}`);
969
+ }
970
+ ctx.trace?.addEvent?.("mind.init.step", { msg: entry.msg || entry });
971
+ }
972
+ });
973
+ ctx.trace?.addEvent?.("mind.init.complete", {
974
+ mindDir,
975
+ cwd
976
+ });
977
+ const artifacts = [];
978
+ const artifactPatterns = [
979
+ { name: "Index", pattern: "index.json", description: "Main Mind index" },
980
+ { name: "API Index", pattern: "api-index.json", description: "API index" },
981
+ { name: "Dependencies", pattern: "deps.json", description: "Dependencies graph" },
982
+ { name: "Recent Diff", pattern: "recent-diff.json", description: "Recent changes diff" }
983
+ ];
984
+ for (const { name, pattern, description } of artifactPatterns) {
985
+ const artifactPath = join(mindDir, pattern);
986
+ try {
987
+ const stats = await promises.stat(artifactPath);
988
+ artifacts.push({
989
+ name,
990
+ path: artifactPath,
991
+ size: stats.size,
992
+ modified: stats.mtime,
993
+ description
994
+ });
995
+ } catch {
996
+ }
997
+ }
998
+ ctx.trace?.addEvent?.("mind.init.artifacts", {
999
+ mindDir,
1000
+ artifactsCount: artifacts.length
1001
+ });
1002
+ const timing = Date.now() - startTime;
1003
+ if (flags.json) {
1004
+ ctx.ui.info(JSON.stringify({
1005
+ ok: true,
1006
+ summary: {
1007
+ Workspace: mindDir,
1008
+ Status: "Initialized"
1009
+ },
1010
+ artifacts,
1011
+ timingMs: timing,
1012
+ data: {
1013
+ mindDir,
1014
+ cwd
1015
+ }
1016
+ }));
1017
+ } else if (!flags.quiet) {
1018
+ const artifactItems = [];
1019
+ for (const artifact of artifacts) {
1020
+ artifactItems.push(`\u2713 ${artifact.name}: ${artifact.description}`);
1021
+ }
1022
+ const sections = [
1023
+ {
1024
+ header: "Summary",
1025
+ items: [
1026
+ `Workspace: ${mindDir}`,
1027
+ `Status: Initialized`
1028
+ ]
1029
+ }
1030
+ ];
1031
+ if (artifacts.length > 0) {
1032
+ sections.push({
1033
+ header: "Created Artifacts",
1034
+ items: artifactItems
1035
+ });
1036
+ }
1037
+ ctx.ui.success("Mind workspace initialized", {
1038
+ title: "Mind Init",
1039
+ sections,
1040
+ timing
1041
+ });
1042
+ }
1043
+ return {
1044
+ exitCode: 0,
1045
+ mindDir,
1046
+ artifacts
1047
+ };
1048
+ }
1049
+ }
1050
+ });
1051
+ var rag_index_default = defineCommand({
1052
+ id: "mind:rag-index",
1053
+ description: "Build Mind indexes",
1054
+ handler: {
1055
+ async execute(ctx, input) {
1056
+ const startTime = Date.now();
1057
+ const { flags } = input;
1058
+ const mindConfig = await useConfig$1();
1059
+ const cwd = flags.cwd || ctx.cwd;
1060
+ const scopeId = flags.scope;
1061
+ const include = flags.include;
1062
+ const exclude = flags.exclude;
1063
+ const skipDeduplication = flags.skipDeduplication;
1064
+ const platform = usePlatform();
1065
+ const loader = !flags.quiet && !flags.json ? useLoader("Building Mind RAG index...") : null;
1066
+ loader?.start();
1067
+ try {
1068
+ const result = await runRagIndex({
1069
+ cwd,
1070
+ scopeId,
1071
+ include,
1072
+ exclude,
1073
+ skipDeduplication,
1074
+ config: mindConfig,
1075
+ platform
1076
+ });
1077
+ const timing = Date.now() - startTime;
1078
+ loader?.succeed(`Index built in ${(timing / 1e3).toFixed(1)}s`);
1079
+ platform?.analytics?.track?.("mind.rag-index", {
1080
+ scopeIds: result.scopeIds,
1081
+ stats: result.stats
1082
+ }).catch(() => {
1083
+ });
1084
+ if (flags.json) {
1085
+ ctx.ui.json({
1086
+ ok: true,
1087
+ scopes: result.scopeIds,
1088
+ stats: result.stats,
1089
+ adapters: result.adapters,
1090
+ timingMs: timing
1091
+ });
1092
+ } else if (!flags.quiet) {
1093
+ const { stats } = result;
1094
+ const percentage = stats.filesDiscovered > 0 ? (stats.filesProcessed / stats.filesDiscovered * 100).toFixed(1) : "0.0";
1095
+ const chunksPerFile = stats.filesProcessed > 0 ? (stats.chunksStored / stats.filesProcessed).toFixed(2) : "0.00";
1096
+ ctx.ui.success(
1097
+ `Indexed ${stats.filesProcessed} files, ${stats.filesSkipped} skipped, ${stats.chunksStored} chunks, deleted ${stats.deletedFiles ?? 0} files/${stats.deletedChunks ?? 0} chunks`,
1098
+ {
1099
+ title: "Mind RAG Index",
1100
+ sections: [
1101
+ {
1102
+ header: "Files",
1103
+ items: [
1104
+ `Discovered: ${stats.filesDiscovered}`,
1105
+ `Processed: ${stats.filesProcessed} (${percentage}%)`,
1106
+ `Skipped: ${stats.filesSkipped}`
1107
+ ]
1108
+ },
1109
+ {
1110
+ header: "Chunks",
1111
+ items: [
1112
+ `Stored: ${stats.chunksStored}`,
1113
+ `Updated: ${stats.chunksUpdated}`,
1114
+ `Skipped: ${stats.chunksSkipped}`,
1115
+ `Rate: ${chunksPerFile}/file`
1116
+ ]
1117
+ },
1118
+ {
1119
+ header: "Cleanup",
1120
+ items: [
1121
+ `Deleted files: ${stats.deletedFiles ?? 0}`,
1122
+ `Deleted chunks: ${stats.deletedChunks ?? 0}`,
1123
+ `Invalid chunks: ${stats.invalidChunks ?? 0}`
1124
+ ]
1125
+ },
1126
+ {
1127
+ header: "Health",
1128
+ items: [
1129
+ `Errors: ${stats.errorCount}`
1130
+ ]
1131
+ }
1132
+ ],
1133
+ timing
1134
+ }
1135
+ );
1136
+ }
1137
+ return {
1138
+ exitCode: 0,
1139
+ ok: true,
1140
+ scopes: result.scopeIds,
1141
+ adapters: result.adapters
1142
+ };
1143
+ } catch (error) {
1144
+ const timing = Date.now() - startTime;
1145
+ const message = error instanceof Error ? error.message : String(error);
1146
+ loader?.fail(`Index build failed: ${message}`);
1147
+ if (flags.json) {
1148
+ ctx.ui.info(JSON.stringify({
1149
+ ok: false,
1150
+ error: message,
1151
+ timingMs: timing
1152
+ }));
1153
+ } else if (!flags.quiet) {
1154
+ ctx.ui.error(`Index build failed: ${message}`);
1155
+ }
1156
+ platform?.analytics?.track?.("mind.rag-index", {
1157
+ error: true,
1158
+ errorMessage: message,
1159
+ timingMs: timing
1160
+ }).catch(() => {
1161
+ });
1162
+ return { exitCode: 1, ok: false };
1163
+ }
1164
+ }
1165
+ }
1166
+ });
1167
+ var VALID_INTENTS = ["summary", "search", "similar", "nav"];
1168
+ var VALID_MODES = ["instant", "auto", "thinking"];
1169
+ var VALID_FORMATS = ["text", "json", "json-pretty"];
1170
+ function isValidIntent(intent) {
1171
+ return VALID_INTENTS.includes(intent);
1172
+ }
1173
+ function isValidMode(mode) {
1174
+ return VALID_MODES.includes(mode);
1175
+ }
1176
+ function isValidFormat(format) {
1177
+ return VALID_FORMATS.includes(format);
1178
+ }
1179
+ function truncateText(text, limit) {
1180
+ if (text.length <= limit) {
1181
+ return text;
1182
+ }
1183
+ return `${text.slice(0, limit - 3)}...`;
1184
+ }
1185
+ var rag_query_default = defineCommand({
1186
+ id: "mind:rag-query",
1187
+ description: "Run semantic RAG query on Mind index",
1188
+ handler: {
1189
+ async execute(ctx, input) {
1190
+ const startTime = Date.now();
1191
+ const { flags } = input;
1192
+ const cwd = flags.cwd || ctx.cwd;
1193
+ const scopeId = flags.scope;
1194
+ const intent = flags.intent && isValidIntent(flags.intent) ? flags.intent : void 0;
1195
+ const text = flags.text?.trim() || "";
1196
+ const limit = flags.limit ? Math.max(1, flags.limit) : void 0;
1197
+ const profileId = flags.profile;
1198
+ const platform = usePlatform();
1199
+ const mode = flags.mode && isValidMode(flags.mode) ? flags.mode : "auto";
1200
+ let format = flags.format && isValidFormat(flags.format) ? flags.format : "text";
1201
+ if (flags.json && format === "text") {
1202
+ format = "json";
1203
+ }
1204
+ if (!text) {
1205
+ if (flags.agent) {
1206
+ console.log(JSON.stringify({
1207
+ error: {
1208
+ code: "INVALID_QUERY",
1209
+ message: 'Provide --text "<query>" to run rag:query.',
1210
+ recoverable: false
1211
+ },
1212
+ meta: {
1213
+ schemaVersion: "agent-response-v1",
1214
+ requestId: `rq-${Date.now()}`,
1215
+ mode,
1216
+ timingMs: 0,
1217
+ cached: false
1218
+ }
1219
+ }));
1220
+ ctx.trace?.addEvent?.("mind.rag-query.invalid", { reason: "missing-text" });
1221
+ return { exitCode: 1, ok: false };
1222
+ }
1223
+ ctx.ui.error('Provide --text "<query>" to run rag:query.');
1224
+ ctx.ui.info('Use: kb mind rag-query --text "your query"');
1225
+ ctx.ui.info("Add --scope to search in specific scope");
1226
+ ctx.ui.info("Add --intent to specify intent (summary, search, similar, nav)");
1227
+ ctx.trace?.addEvent?.("mind.rag-query.invalid", { reason: "missing-text" });
1228
+ return { exitCode: 1, ok: false };
1229
+ }
1230
+ if (flags.agent) {
1231
+ try {
1232
+ const result = await runAgentRagQuery({
1233
+ cwd,
1234
+ scopeId,
1235
+ text,
1236
+ mode,
1237
+ debug: flags.debug,
1238
+ broker: void 0,
1239
+ // Gracefully falls back to in-memory
1240
+ platform
1241
+ // Pass platform for analytics adapter
1242
+ });
1243
+ platform?.analytics?.track?.("mind.rag-query", {
1244
+ mode,
1245
+ agent: true,
1246
+ scopeId,
1247
+ intent
1248
+ }).catch(() => {
1249
+ });
1250
+ console.log(JSON.stringify(result));
1251
+ if (isAgentError(result)) {
1252
+ return { exitCode: 1, ok: false, result };
1253
+ }
1254
+ return { exitCode: 0, ok: true, result };
1255
+ } catch (error) {
1256
+ const message = error instanceof Error ? error.message : String(error);
1257
+ console.log(JSON.stringify({
1258
+ error: {
1259
+ code: "ENGINE_ERROR",
1260
+ message,
1261
+ recoverable: true
1262
+ },
1263
+ meta: {
1264
+ schemaVersion: "agent-response-v1",
1265
+ requestId: `rq-${Date.now()}`,
1266
+ mode,
1267
+ timingMs: Date.now() - startTime,
1268
+ cached: false
1269
+ }
1270
+ }));
1271
+ ctx.trace?.addEvent?.("mind.rag-query.agent.error", { error: message });
1272
+ return { exitCode: 1, ok: false };
1273
+ }
1274
+ }
1275
+ if (!flags.quiet && format === "text") {
1276
+ ctx.ui.info("Initializing Mind RAG query...");
1277
+ }
1278
+ const formatElapsedTime = (ms) => {
1279
+ const seconds = Math.floor(ms / 1e3);
1280
+ if (seconds < 60) {
1281
+ return `${seconds}s`;
1282
+ }
1283
+ const minutes = Math.floor(seconds / 60);
1284
+ const remainingSeconds = seconds % 60;
1285
+ return `${minutes}m ${remainingSeconds}s`;
1286
+ };
1287
+ let currentStage = "Initializing";
1288
+ let lastProgressTime = Date.now();
1289
+ try {
1290
+ const result = await runRagQuery({
1291
+ cwd,
1292
+ scopeId,
1293
+ text,
1294
+ intent,
1295
+ limit,
1296
+ profileId,
1297
+ platform,
1298
+ // Pass platform for analytics adapter
1299
+ runtime: void 0,
1300
+ // Runtime context not available in CLI
1301
+ onProgress: (stage, details) => {
1302
+ if (flags.quiet || format === "json" || format === "json-pretty") {
1303
+ return;
1304
+ }
1305
+ currentStage = details ? `${stage}: ${details}` : stage;
1306
+ const now = Date.now();
1307
+ if (now - lastProgressTime >= 1e3) {
1308
+ const elapsed = now - startTime;
1309
+ const elapsedStr = formatElapsedTime(elapsed);
1310
+ ctx.ui.info(`${currentStage} [${elapsedStr}]`);
1311
+ lastProgressTime = now;
1312
+ }
1313
+ }
1314
+ });
1315
+ const timing = Date.now() - startTime;
1316
+ ctx.trace?.addEvent?.("mind.rag-query.complete", {
1317
+ mode,
1318
+ scopeId,
1319
+ chunks: result.result.chunks.length,
1320
+ timingMs: timing
1321
+ });
1322
+ if (format === "json" || format === "json-pretty") {
1323
+ if (result.result.metadata?.jsonResponse) {
1324
+ const jsonResponse = result.result.metadata.jsonResponse;
1325
+ if (format === "json-pretty") {
1326
+ ctx.ui.info(JSON.stringify(jsonResponse, null, 2));
1327
+ } else {
1328
+ ctx.ui.info(JSON.stringify(jsonResponse));
1329
+ }
1330
+ } else {
1331
+ ctx.ui.info(JSON.stringify({
1332
+ ok: true,
1333
+ scopeId: result.scopeId,
1334
+ intent: result.result.query.intent,
1335
+ chunks: result.result.chunks,
1336
+ contextText: result.result.contextText
1337
+ }));
1338
+ }
1339
+ } else if (!flags.quiet) {
1340
+ const topChunk = result.result.chunks[0];
1341
+ const sections = [
1342
+ {
1343
+ header: "Summary",
1344
+ items: [
1345
+ `Scope: ${result.scopeId}`,
1346
+ `Intent: ${result.result.query.intent}`,
1347
+ `Chunks returned: ${result.result.chunks.length}`
1348
+ ]
1349
+ }
1350
+ ];
1351
+ if (topChunk) {
1352
+ sections.push({
1353
+ header: "Top chunk",
1354
+ items: [
1355
+ `${topChunk.path} #${topChunk.span.startLine}-${topChunk.span.endLine}`,
1356
+ truncateText(topChunk.text, 400)
1357
+ ]
1358
+ });
1359
+ } else {
1360
+ sections.push({
1361
+ items: ["No matching chunks found."]
1362
+ });
1363
+ }
1364
+ if (result.result.contextText && result.result.contextText.length > 0) {
1365
+ const contextPreview = result.result.contextText.length > 2e3 ? result.result.contextText.substring(0, 2e3) + "..." : result.result.contextText;
1366
+ sections.push({
1367
+ header: `Synthesized context (${result.result.contextText.length} chars)`,
1368
+ items: [contextPreview]
1369
+ });
1370
+ }
1371
+ ctx.ui.success("Query completed", {
1372
+ title: "Mind RAG Query",
1373
+ sections,
1374
+ timing
1375
+ });
1376
+ }
1377
+ platform?.analytics?.track?.("mind.rag-query", {
1378
+ mode,
1379
+ agent: false,
1380
+ scopeId,
1381
+ intent
1382
+ }).catch(() => {
1383
+ });
1384
+ return { exitCode: 0, ok: true, result };
1385
+ } catch (error) {
1386
+ const timing = Date.now() - startTime;
1387
+ const message = error instanceof Error ? error.message : String(error);
1388
+ ctx.trace?.addEvent?.("mind.rag-query.error", { error: message, timingMs: timing });
1389
+ if (format === "json" || format === "json-pretty") {
1390
+ ctx.ui.info(JSON.stringify({
1391
+ ok: false,
1392
+ error: message,
1393
+ timingMs: timing
1394
+ }));
1395
+ } else if (!flags.quiet) {
1396
+ ctx.ui.error(`Query failed: ${message}`);
1397
+ }
1398
+ platform?.analytics?.track?.("mind.rag-query", {
1399
+ mode,
1400
+ agent: false,
1401
+ scopeId,
1402
+ intent,
1403
+ error: true
1404
+ }).catch(() => {
1405
+ });
1406
+ return { exitCode: 1, ok: false };
1407
+ }
1408
+ }
1409
+ }
1410
+ });
1411
+ var verify_default = defineCommand({
1412
+ id: "mind:verify",
1413
+ description: "Check Mind platform services readiness",
1414
+ handler: {
1415
+ async execute(ctx, input) {
1416
+ const startTime = Date.now();
1417
+ const { flags } = input;
1418
+ ctx.trace?.addEvent?.("mind.verify.start", { command: "mind:verify" });
1419
+ const platform = usePlatform();
1420
+ const services = [];
1421
+ if (!platform) {
1422
+ const result2 = {
1423
+ exitCode: 1,
1424
+ ok: false,
1425
+ services,
1426
+ issues: ["platform context is missing"],
1427
+ meta: { timingMs: Date.now() - startTime }
1428
+ };
1429
+ if (flags.json) {
1430
+ ctx.ui.info(JSON.stringify(result2));
1431
+ } else {
1432
+ ctx.ui.error("Platform services are not available in this context");
1433
+ }
1434
+ ctx.trace?.addEvent?.("mind.verify.failed", { reason: "no-platform" });
1435
+ return result2;
1436
+ }
1437
+ const check = (name, required, available, configured, message) => {
1438
+ services.push({ service: name, required, available, configured, message });
1439
+ };
1440
+ const has = (key) => Boolean(platform[key]);
1441
+ const isConfigured = (svc) => platform.isConfigured?.(svc) ?? has(svc);
1442
+ check("vectorStore", true, has("vectorStore"), isConfigured("vectorStore"));
1443
+ check("embeddings", true, has("embeddings"), isConfigured("embeddings"));
1444
+ check("llm", false, has("llm"), isConfigured("llm"));
1445
+ check("cache", false, has("cache"), true);
1446
+ check("storage", false, has("storage"), true);
1447
+ check("analytics", false, has("analytics"), true);
1448
+ const requiredOk = services.filter((s) => s.required).every((s) => s.available && s.configured);
1449
+ const issues = services.filter((s) => s.required && (!s.available || !s.configured)).map((s) => `${s.service} is missing or not configured`);
1450
+ const timing = Date.now() - startTime;
1451
+ ctx.trace?.addEvent?.("mind.verify.complete", { ok: requiredOk, issues: issues.length });
1452
+ const result = {
1453
+ exitCode: requiredOk ? 0 : 1,
1454
+ ok: requiredOk,
1455
+ services,
1456
+ issues,
1457
+ meta: { timingMs: timing }
1458
+ };
1459
+ if (flags.json) {
1460
+ ctx.ui.info(JSON.stringify(result));
1461
+ } else if (!flags.quiet) {
1462
+ const sections = [
1463
+ {
1464
+ header: "Services",
1465
+ items: services.map((s) => {
1466
+ const status = s.available && s.configured ? "\u2713" : "\u26A0";
1467
+ return `${status} ${s.service}: ${s.available ? "available" : "missing"}${s.configured ? "" : " (not configured)"}`;
1468
+ })
1469
+ }
1470
+ ];
1471
+ if (issues.length) {
1472
+ sections.push({
1473
+ header: "Issues",
1474
+ items: issues.map((i) => `\u26A0 ${i}`)
1475
+ });
1476
+ }
1477
+ if (requiredOk) {
1478
+ ctx.ui.success("Mind platform services verified", {
1479
+ title: "Mind Verify - Platform",
1480
+ sections,
1481
+ timing
1482
+ });
1483
+ } else {
1484
+ ctx.ui.error("Mind platform services have issues");
1485
+ ctx.ui.success("Verification Details", {
1486
+ title: "Mind Verify - Platform",
1487
+ sections,
1488
+ timing
1489
+ });
1490
+ }
1491
+ }
1492
+ return result;
1493
+ }
1494
+ }
1495
+ });
1496
+
1497
+ export { ANALYTICS_ACTOR, ANALYTICS_EVENTS, ANALYTICS_PREFIX, ANALYTICS_SUFFIX, FeedInputSchema, FeedOutputSchema, InitInputSchema, InitOutputSchema, MIND_PRODUCT_ID, PackInputSchema, PackOutputSchema, QueryInputSchema, QueryOutputSchema, TimingTracker, UpdateInputSchema, UpdateOutputSchema, VerifyInputSchema, VerifyOutputSchema, box, colors, createMindRuntime, createSpinner, formatTiming, keyValue, manifest, runAgentRagQuery, init_default as runInitCommand, runRagIndex, rag_index_default as runRagIndexCommand, runRagQuery, rag_query_default as runRagQueryCommand, verify_default as runVerifyCommand, safeColors, safeSymbols, tryUseConfig, useConfig, useSyncConfig };
1498
+ //# sourceMappingURL=index.js.map
1499
+ //# sourceMappingURL=index.js.map