@nusoft/nuos-build-catalogue 0.19.1 → 0.20.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/cli.js CHANGED
@@ -460,7 +460,17 @@ async function main() {
460
460
  // when the user switched machines and needs to pull the model
461
461
  // freshly. Same orchestrator that `init` calls internally.
462
462
  const { runLlmSetup } = await import('./setup/run-llm-setup.js');
463
+ const { ensureIndexBuilt } = await import('./setup/auto-index.js');
463
464
  const result = await runLlmSetup({ nonInteractive: false });
465
+ // After the LLM stack is ready, auto-build the search index when
466
+ // it isn't already present. Same helper init and install-protocols
467
+ // use — keeps the three commands aligned on "after this finishes
468
+ // the project is search-ready".
469
+ if (result.kind === 'already_ready' ||
470
+ result.kind === 'pulled_only' ||
471
+ result.kind === 'installed_and_pulled') {
472
+ await ensureIndexBuilt({});
473
+ }
464
474
  // Most failure paths emit guidance in-band; we exit non-zero only
465
475
  // when a pull actually failed (so CI scripting can branch on it).
466
476
  const exitCode = result.kind === 'pull_failed' || result.kind === 'install_failed' ? 1 : 0;
@@ -211,6 +211,7 @@ export async function cmdInit(prompt, options = {}) {
211
211
  // `nuos-catalogue setup-llm` later.
212
212
  if (!options.noLlm) {
213
213
  const { runLlmSetup } = await import('../setup/run-llm-setup.js');
214
+ const { ensureIndexBuilt } = await import('../setup/auto-index.js');
214
215
  await runLlmSetup({
215
216
  // The setup module writes its own progress directly to stderr; we
216
217
  // don't route through `prompt.print` because the in-place progress
@@ -224,6 +225,17 @@ export async function cmdInit(prompt, options = {}) {
224
225
  // so this is safe in unattended runs too.
225
226
  nonInteractive: false,
226
227
  });
228
+ // After LLM setup succeeds, auto-build the first search index. On a
229
+ // fresh project this is ~30s of starter-kit boilerplate; trivial,
230
+ // and finishing here means `search` works out of the box. When the
231
+ // LLM stack isn't ready, `ensureIndexBuilt` skips with a hint
232
+ // pointing back to setup-llm.
233
+ const indexResult = await ensureIndexBuilt({ cwd });
234
+ if (indexResult.kind === 'skipped_llm_not_ready') {
235
+ prompt.print('');
236
+ prompt.print(` · Skipping first-index build: ${indexResult.reason}.`);
237
+ prompt.print(` · ${indexResult.hint}`);
238
+ }
227
239
  }
228
240
  else {
229
241
  prompt.print('');
@@ -284,6 +296,16 @@ export async function cmdInstallProtocols(prompt, options = {}) {
284
296
  prompt.print('');
285
297
  prompt.print('Checking local semantic search (Ollama + qwen3-embedding:0.6b):');
286
298
  await reportLlmStatus((msg) => prompt.print(` ${msg}`));
299
+ // Auto-build the search index when the LLM is ready but the index
300
+ // isn't present yet (typical upgrade-path state: pre-0.19 install +
301
+ // someone just added docs/build/ content). When the index already
302
+ // exists this is a no-op; when the LLM isn't ready the helper skips
303
+ // with a hint that's already printed by reportLlmStatus.
304
+ const { ensureIndexBuilt } = await import('../setup/auto-index.js');
305
+ const indexResult = await ensureIndexBuilt({ cwd });
306
+ if (indexResult.kind === 'just_built') {
307
+ prompt.print(` ✓ Built search index (${indexResult.indexed} files, ${indexResult.chunks} chunks).`);
308
+ }
287
309
  return { output: '', exitCode: 0 };
288
310
  }
289
311
  /**
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Shared helper that runs the first search index build automatically
3
+ * from `init`, `install-protocols`, and `setup-llm`.
4
+ *
5
+ * Gated on the LLM stack being ready (Ollama + the configured embedding
6
+ * model). If the LLM isn't ready, this helper returns a `skipped_llm`
7
+ * result with a hint string the caller prints. The hint references
8
+ * `setup-llm` so the user has a clear path forward.
9
+ *
10
+ * Indexing on a fresh project takes ~30s — small enough that auto-
11
+ * running on first install is friendlier than asking. Subsequent calls
12
+ * are incremental via the per-file SHA hashes, so re-running on an
13
+ * existing index is cheap.
14
+ *
15
+ * @module setup/auto-index
16
+ */
17
+ /** Outcome of an auto-index attempt. */
18
+ export type AutoIndexResult = {
19
+ kind: 'already_built';
20
+ indexPath: string;
21
+ } | {
22
+ kind: 'just_built';
23
+ indexPath: string;
24
+ indexed: number;
25
+ chunks: number;
26
+ durationMs: number;
27
+ } | {
28
+ kind: 'skipped_llm_not_ready';
29
+ reason: string;
30
+ hint: string;
31
+ } | {
32
+ kind: 'skipped_no_catalogue';
33
+ } | {
34
+ kind: 'failed';
35
+ error: string;
36
+ };
37
+ export interface AutoIndexOptions {
38
+ /** Project root for path resolution. Defaults to `process.cwd()`. */
39
+ cwd?: string;
40
+ /** Output sink — defaults to process.stderr. */
41
+ out?: (text: string) => void;
42
+ /** Force a full reindex even if the index file already exists. */
43
+ force?: boolean;
44
+ }
45
+ /**
46
+ * Build the first search index when conditions allow. Idempotent: returns
47
+ * `already_built` and prints nothing when the index file exists (unless
48
+ * `force` is set). Returns `skipped_llm_not_ready` with a hint when the
49
+ * Ollama probe fails — the caller prints the hint and the user runs
50
+ * `setup-llm` to fix things.
51
+ *
52
+ * Never throws on user-facing failures.
53
+ */
54
+ export declare function ensureIndexBuilt(opts?: AutoIndexOptions): Promise<AutoIndexResult>;
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Shared helper that runs the first search index build automatically
3
+ * from `init`, `install-protocols`, and `setup-llm`.
4
+ *
5
+ * Gated on the LLM stack being ready (Ollama + the configured embedding
6
+ * model). If the LLM isn't ready, this helper returns a `skipped_llm`
7
+ * result with a hint string the caller prints. The hint references
8
+ * `setup-llm` so the user has a clear path forward.
9
+ *
10
+ * Indexing on a fresh project takes ~30s — small enough that auto-
11
+ * running on first install is friendlier than asking. Subsequent calls
12
+ * are incremental via the per-file SHA hashes, so re-running on an
13
+ * existing index is cheap.
14
+ *
15
+ * @module setup/auto-index
16
+ */
17
+ import { existsSync } from 'node:fs';
18
+ import { resolveBuildRoot, resolveCatalogueRoot, resolveHashPath, resolveIndexPath, } from '../path-resolution.js';
19
+ import { DEFAULT_OLLAMA_HOST, detectModelPresent, detectOllamaApi } from './ollama-detect.js';
20
+ import { DEFAULT_EMBEDDING_MODEL } from './run-llm-setup.js';
21
+ /**
22
+ * Build the first search index when conditions allow. Idempotent: returns
23
+ * `already_built` and prints nothing when the index file exists (unless
24
+ * `force` is set). Returns `skipped_llm_not_ready` with a hint when the
25
+ * Ollama probe fails — the caller prints the hint and the user runs
26
+ * `setup-llm` to fix things.
27
+ *
28
+ * Never throws on user-facing failures.
29
+ */
30
+ export async function ensureIndexBuilt(opts = {}) {
31
+ const cwd = opts.cwd ?? process.cwd();
32
+ const out = opts.out ?? ((text) => process.stderr.write(text));
33
+ // Resolve where the index file lives without forcing the LLM stack to
34
+ // load — path resolution is cheap and offline. When the project has
35
+ // no `docs/build/` yet (e.g. install-protocols invoked in a non-
36
+ // scaffolded directory), resolveBuildRoot throws — we treat that as a
37
+ // silent no-op, since there is nothing meaningful to index.
38
+ const ctx = { cwd, env: process.env };
39
+ let buildRoot;
40
+ let catalogueRoot;
41
+ let indexPath;
42
+ let hashPath;
43
+ try {
44
+ buildRoot = resolveBuildRoot(undefined, ctx);
45
+ catalogueRoot = resolveCatalogueRoot(undefined, ctx);
46
+ indexPath = resolveIndexPath(buildRoot, undefined, ctx);
47
+ hashPath = resolveHashPath(buildRoot, undefined, ctx);
48
+ }
49
+ catch {
50
+ return { kind: 'skipped_no_catalogue' };
51
+ }
52
+ // Fast path: index file already exists and we're not forcing a rebuild.
53
+ if (existsSync(indexPath) && !opts.force) {
54
+ return { kind: 'already_built', indexPath };
55
+ }
56
+ // Probe the LLM stack — index requires Ollama + the model. If either
57
+ // is missing, skip with a hint pointing at setup-llm.
58
+ const apiHost = process.env.NUOS_CATALOGUE_OLLAMA_HOST ?? DEFAULT_OLLAMA_HOST;
59
+ const modelId = process.env.NUOS_CATALOGUE_OLLAMA_MODEL ?? DEFAULT_EMBEDDING_MODEL;
60
+ const api = await detectOllamaApi(apiHost);
61
+ if (!api.reachable) {
62
+ return {
63
+ kind: 'skipped_llm_not_ready',
64
+ reason: `Ollama is not running at ${apiHost}`,
65
+ hint: 'Run `nuos-catalogue setup-llm` to set up local semantic search, then re-run `nuos-catalogue index`.',
66
+ };
67
+ }
68
+ const model = await detectModelPresent(apiHost, modelId);
69
+ if (!model.present) {
70
+ return {
71
+ kind: 'skipped_llm_not_ready',
72
+ reason: `${modelId} is not pulled`,
73
+ hint: 'Run `nuos-catalogue setup-llm` to pull the embedding model (~600 MB), then re-run `nuos-catalogue index`.',
74
+ };
75
+ }
76
+ // LLM is ready. Build the index. We import lazily so the cold-start
77
+ // path of `install-protocols` (where the index usually already exists)
78
+ // doesn't pay the embedder-loading cost.
79
+ out('Building search index for docs/build/ … (first run may take ~30 seconds)\n');
80
+ try {
81
+ const { selectEmbedderFromEnv } = await import('../embedder/select.js');
82
+ const { openStore } = await import('../store/open.js');
83
+ const { runIndex } = await import('../indexer/upsert.js');
84
+ const embedder = await selectEmbedderFromEnv();
85
+ const store = await openStore({ storagePath: indexPath, dimensions: embedder.dimensions });
86
+ try {
87
+ const report = await runIndex({
88
+ catalogueRoot,
89
+ hashFilePath: hashPath,
90
+ store,
91
+ embedder,
92
+ force: Boolean(opts.force),
93
+ dryRun: false,
94
+ });
95
+ out(`✓ Indexed ${report.indexed} file(s), ${report.chunks} chunks embedded in ` +
96
+ `${(report.durationMs / 1000).toFixed(1)}s\n`);
97
+ return {
98
+ kind: 'just_built',
99
+ indexPath,
100
+ indexed: report.indexed,
101
+ chunks: report.chunks,
102
+ durationMs: report.durationMs,
103
+ };
104
+ }
105
+ finally {
106
+ // Unload-after-use commitment — embedder releases the model.
107
+ await embedder.dispose();
108
+ }
109
+ }
110
+ catch (err) {
111
+ const message = err instanceof Error ? err.message : String(err);
112
+ out(`\n✗ Index build failed: ${message}\n`);
113
+ out('Re-run `nuos-catalogue index` manually to retry.\n');
114
+ return { kind: 'failed', error: message };
115
+ }
116
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nusoft/nuos-build-catalogue",
3
- "version": "0.19.1",
3
+ "version": "0.20.0",
4
4
  "description": "NuOS build-catalogue tooling: semantic search (WU 110) + migration runner that lifts markdown artefacts into JSON-backed workflow records (WU 111, Phase G).",
5
5
  "type": "module",
6
6
  "bin": {