@brainbox-labs/sdk 0.24.0 → 0.26.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.
@@ -75,7 +75,7 @@ async function performIngest(ctx, opts, ids) {
75
75
  const [{ introspectDatabaseSchema }, { parseSqlInputs }, { expandDocPatterns, inlineText }, { connectAndProfile }] = await Promise.all([
76
76
  import("./introspect-KQOOQEZM.js"),
77
77
  import("./sql-parser-TB26PFXS.js"),
78
- import("./docs-loader-VA4KBG6Y.js"),
78
+ import("./docs-loader-NW6KY4Z3.js"),
79
79
  import("./column-profiler-FJCAQQAS.js")
80
80
  ]);
81
81
  const api = new BrainBoxApiClient(ctx.apiUrl, ctx.apiKey, noopLogger);
@@ -63,6 +63,9 @@ function validateSourceKey(raw, where = "sourceKey") {
63
63
  function normalizeNamespace(ns) {
64
64
  return (ns ?? "").trim().replace(/^\/+|\/+$/g, "");
65
65
  }
66
+ function serverNamespace(ns) {
67
+ return (ns ?? "").split("/").map((seg) => seg.replace(/[^a-zA-Z0-9.\-_]/g, "-")).filter((seg) => seg !== "").join("/");
68
+ }
66
69
  function deriveSourceKey(relPath, namespace) {
67
70
  const posix = relPath.split("\\").join("/").replace(/^\.\//, "").replace(/^\/+/, "");
68
71
  const ns = normalizeNamespace(namespace);
@@ -184,6 +187,7 @@ export {
184
187
  convertedName,
185
188
  validateSourceKey,
186
189
  normalizeNamespace,
190
+ serverNamespace,
187
191
  deriveSourceKey,
188
192
  frontmatterSourceKey,
189
193
  loadDocFile,
@@ -2,14 +2,15 @@ import {
2
2
  lookupIds,
3
3
  performIngest,
4
4
  resolveIds
5
- } from "./chunk-XFLBPF4A.js";
5
+ } from "./chunk-3DMBWVAH.js";
6
6
  import {
7
7
  deriveSourceKey,
8
8
  frontmatterSourceKey,
9
9
  loadDocFile,
10
10
  normalizeNamespace,
11
+ serverNamespace,
11
12
  validateSourceKey
12
- } from "./chunk-4J77FNJJ.js";
13
+ } from "./chunk-KZ2QGVO3.js";
13
14
  import {
14
15
  createBrainExecutor
15
16
  } from "./chunk-GVWDS5TG.js";
@@ -81,23 +82,31 @@ function diffSources(local, remote) {
81
82
  }
82
83
  var MAX_LIST_PAGES = 500;
83
84
  var LIST_PAGE_LIMIT = 50;
84
- var SERVER_SCAN_WINDOW = 1e3;
85
+ var LEGACY_SCAN_WINDOW = 1e3;
85
86
  function executorLister(exec) {
86
87
  return {
87
- async listSources(namespace) {
88
- const want = normalizeNamespace(namespace);
88
+ async listSources(namespace, opts) {
89
+ const want = serverNamespace(namespace);
89
90
  const out = [];
90
91
  let seen = 0;
92
+ let serverTruncated = false;
91
93
  let cursor;
92
94
  for (let i = 0; i < MAX_LIST_PAGES; i++) {
93
95
  const args = { kind: "raw-source", limit: LIST_PAGE_LIMIT, ...want !== "" ? { namespace: want } : {}, ...cursor ? { cursor } : {} };
94
96
  const res = await exec("list_wiki_pages", args);
95
97
  seen += res.pages?.length ?? 0;
98
+ if (res.truncated) serverTruncated = true;
96
99
  for (const p of res.pages ?? []) {
97
- if (!p.sourceKey || normalizeNamespace(p.namespace) !== want) continue;
100
+ if (!p.sourceKey) continue;
101
+ const ns = normalizeNamespace(p.namespace);
102
+ if (ns !== want && !(opts?.subtree && ns.startsWith(`${want}/`))) continue;
98
103
  out.push({ sourceKey: p.sourceKey, title: p.title, pageId: p.pageId, sourceVersion: p.sourceVersion, namespace: p.namespace, analysisDepth: p.analysisDepth });
99
104
  }
100
- if (!res.nextCursor) return { sources: out, truncated: seen >= SERVER_SCAN_WINDOW };
105
+ if (!res.nextCursor) {
106
+ if (serverTruncated) return { sources: out, truncated: true, truncatedReason: "the server reported that its scan window filled" };
107
+ if (res.complete) return { sources: out, truncated: false };
108
+ return seen >= LEGACY_SCAN_WINDOW ? { sources: out, truncated: true, truncatedReason: `the server sent no completeness flag and ${seen} pages were listed, as many as its ${LEGACY_SCAN_WINDOW}-doc scan window` } : { sources: out, truncated: false };
109
+ }
101
110
  cursor = res.nextCursor;
102
111
  }
103
112
  throw new Error("[BrainBox] sync: too many raw-source pages to list; narrow with a namespace");
@@ -164,7 +173,10 @@ async function collect(opts, transform) {
164
173
  const files = (await glob(DOC_GLOB, { cwd: root, nodir: true, ignore: ["**/node_modules/**"] })).sort();
165
174
  for (const rel of files) {
166
175
  const loaded = await loadDocFile(nodePath2.join(root, rel));
167
- raw.push({ rel: rel.split(nodePath2.sep).join("/"), content: loaded.content, type: loaded.type, entry: { name: rel } });
176
+ const posixRel = rel.split(nodePath2.sep).join("/");
177
+ const sub = nodePath2.posix.dirname(posixRel);
178
+ const ns = opts.flat || sub === "." ? void 0 : `${opts.namespace}/${sub}`;
179
+ raw.push({ rel: posixRel, content: loaded.content, type: loaded.type, entry: { name: rel }, ...ns !== void 0 && { ns } });
168
180
  }
169
181
  }
170
182
  for (const e of opts.sources ?? []) {
@@ -203,7 +215,7 @@ async function collect(opts, transform) {
203
215
  if (prior !== void 0) throw new Error(`[BrainBox] sync: "${prior}" and "${r.rel}" both map to sourceKey "${sourceKey}"`);
204
216
  seen.set(sourceKey, r.rel);
205
217
  const sourceVersion = r.entry.sourceVersion ?? computeSourceVersion(content, transform);
206
- const namespace = r.entry.namespace ?? opts.namespace;
218
+ const namespace = serverNamespace(r.entry.namespace ?? r.ns ?? opts.namespace);
207
219
  const analysisDepth = r.entry.analysisDepth ?? opts.analysisDepth;
208
220
  docs.push({
209
221
  // Server's resolveDepth: per-source ?? request default ?? 'shallow'; the diff compares against this.
@@ -238,14 +250,14 @@ async function performSync(ctx, syncOpts, deps = {}) {
238
250
  const api = new BrainBoxApiClient(ctx.apiUrl, ctx.apiKey, noopLogger);
239
251
  const { orgId, projectId } = opts.dryRun ? await lookupIds(api, ctx) : await resolveIds(api, ctx);
240
252
  const projectMissing = projectId === void 0;
241
- const listing = projectId === void 0 ? { sources: [], truncated: false } : await (deps.remote ?? executorLister(createBrainExecutor(ctx.apiUrl, ctx.apiKey, projectId))).listSources(opts.namespace);
253
+ const listing = projectId === void 0 ? { sources: [], truncated: false } : await (deps.remote ?? executorLister(createBrainExecutor(ctx.apiUrl, ctx.apiKey, projectId))).listSources(opts.namespace, { subtree: !opts.flat });
242
254
  const diff = diffSources(docs.map((d) => d.local), listing.sources);
243
255
  if (listing.truncated) diff.deleted = [];
244
256
  const result = {
245
257
  dryRun: opts.dryRun ?? false,
246
258
  namespace: opts.namespace,
247
259
  ...projectMissing && { projectMissing: true },
248
- ...listing.truncated && { listingTruncated: true },
260
+ ...listing.truncated && { listingTruncated: true, ...listing.truncatedReason && { listingTruncatedReason: listing.truncatedReason } },
249
261
  added: diff.added.map(toEntry),
250
262
  changed: diff.changed.map(toEntry),
251
263
  unchanged: diff.unchanged.map(toEntry),
@@ -2,7 +2,7 @@ import {
2
2
  expandDocPatterns,
3
3
  inlineText,
4
4
  loadDocFile
5
- } from "./chunk-4J77FNJJ.js";
5
+ } from "./chunk-KZ2QGVO3.js";
6
6
  import "./chunk-MLKGABMK.js";
7
7
  export {
8
8
  expandDocPatterns,
package/dist/index.d.ts CHANGED
@@ -197,8 +197,14 @@ interface SyncOptions {
197
197
  * ONE sync root: deletes are computed against every keyed raw-source page in exactly this
198
198
  * namespace, so pages written there by anything else (brain.ingest with a sourceKey, another
199
199
  * folder's sync) show up as pending deletes. Synced-in namespaces are read-only locally.
200
+ *
201
+ * Unless `flat`, subfolders map to nested namespaces (`kb` + `eng/runbooks/x.md` -> `kb/eng/runbooks`) and the
202
+ * sync root owns the whole subtree: `kb` and `kb/**`. Don't sync `kb` and `kb/eng` from two roots: each would see
203
+ * the other's pages as pending deletes.
200
204
  */
201
205
  namespace?: string;
206
+ /** Put every page of `dir` in `namespace` itself, ignoring subfolders (the pre-0.25 behaviour). Keys are the same either way. */
207
+ flat?: boolean;
202
208
  /** Transform script: a name under `.brainbox/transforms/` in the current working directory, or a path relative to it. Its content hash is folded into sourceVersion. */
203
209
  transform?: string;
204
210
  /** Pre-approve archiving docs that no longer exist locally. Default false: they come back as `pendingDeletes`. */
@@ -262,6 +268,8 @@ interface SyncResult {
262
268
  projectMissing?: boolean;
263
269
  /** The server may have cut the listing short, so deletes were not computed (nothing is reported or archived). */
264
270
  listingTruncated?: boolean;
271
+ /** Why the listing was treated as possibly incomplete; set with `listingTruncated`. */
272
+ listingTruncatedReason?: string;
265
273
  added: SyncEntry[];
266
274
  changed: SyncEntry[];
267
275
  /** Same key + version + namespace as the Brain. Not sent unless `force`. */
@@ -480,9 +488,11 @@ declare class BrainBox {
480
488
  * with {@link BrainBox.prune}. Targets the handle's `project` like {@link BrainBox.ingest};
481
489
  * tenant keys are not supported yet.
482
490
  *
483
- * `namespace` defaults to the synced dir's basename and is owned by this one sync root: deletes
484
- * are computed against every keyed raw-source page in exactly that namespace, so other writers
485
- * into it show up as pending deletes.
491
+ * `namespace` defaults to the synced dir's basename. Subfolders map to nested namespaces
492
+ * (`kb` + `eng/x.md` -> `kb/eng`) unless `flat: true`. The sync root owns the namespace and
493
+ * everything under it (exactly the namespace when `flat`): deletes are computed against every
494
+ * keyed raw-source page there, so other writers, or another root synced to a nested namespace
495
+ * such as `kb/eng`, show up as pending deletes.
486
496
  */
487
497
  sync(opts: SyncOptions): Promise<SyncResult>;
488
498
  /** Archive the synced pages for these `sourceKey`s (soft, reversible server-side). Unknown keys come back in `notFound`. */
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  SyncPartialFailureError
3
- } from "./chunk-QWOXEAQW.js";
3
+ } from "./chunk-SUTD4AF4.js";
4
4
  import {
5
5
  IngestPartialFailureError
6
- } from "./chunk-XFLBPF4A.js";
7
- import "./chunk-4J77FNJJ.js";
6
+ } from "./chunk-3DMBWVAH.js";
7
+ import "./chunk-KZ2QGVO3.js";
8
8
  import {
9
9
  BrainBoxClient,
10
10
  MCP,
@@ -390,7 +390,7 @@ var BrainBox = class {
390
390
  */
391
391
  async ingest(opts) {
392
392
  const ctx = await this.ingestContext("ingest");
393
- const { performIngest } = await import("./ingest-DICRS7WW.js");
393
+ const { performIngest } = await import("./ingest-F253IAHY.js");
394
394
  return performIngest(ctx, opts);
395
395
  }
396
396
  /**
@@ -400,16 +400,18 @@ var BrainBox = class {
400
400
  * with {@link BrainBox.prune}. Targets the handle's `project` like {@link BrainBox.ingest};
401
401
  * tenant keys are not supported yet.
402
402
  *
403
- * `namespace` defaults to the synced dir's basename and is owned by this one sync root: deletes
404
- * are computed against every keyed raw-source page in exactly that namespace, so other writers
405
- * into it show up as pending deletes.
403
+ * `namespace` defaults to the synced dir's basename. Subfolders map to nested namespaces
404
+ * (`kb` + `eng/x.md` -> `kb/eng`) unless `flat: true`. The sync root owns the namespace and
405
+ * everything under it (exactly the namespace when `flat`): deletes are computed against every
406
+ * keyed raw-source page there, so other writers, or another root synced to a nested namespace
407
+ * such as `kb/eng`, show up as pending deletes.
406
408
  */
407
409
  async sync(opts) {
408
410
  if (isTenantKey(this.options.apiKey)) {
409
411
  throw new Error("[BrainBox] brain.sync does not support tenant keys yet. Use a project or org key.");
410
412
  }
411
413
  const ctx = await this.ingestContext("sync");
412
- const { performSync } = await import("./sync-W5BVNQLX.js");
414
+ const { performSync } = await import("./sync-G2GH3PEJ.js");
413
415
  return performSync(ctx, opts);
414
416
  }
415
417
  /** Archive the synced pages for these `sourceKey`s (soft, reversible server-side). Unknown keys come back in `notFound`. */
@@ -418,7 +420,7 @@ var BrainBox = class {
418
420
  throw new Error("[BrainBox] brain.prune does not support tenant keys yet. Use a project or org key.");
419
421
  }
420
422
  const ctx = await this.ingestContext("prune");
421
- const { performPrune } = await import("./sync-W5BVNQLX.js");
423
+ const { performPrune } = await import("./sync-G2GH3PEJ.js");
422
424
  return performPrune(ctx, sourceKeys);
423
425
  }
424
426
  /** Target for write-side calls: a tenant key's overlay project, else the handle's `project`. */
@@ -15,7 +15,7 @@ import {
15
15
  htmlToMarkdown,
16
16
  inlineText,
17
17
  needsConversion
18
- } from "../chunk-4J77FNJJ.js";
18
+ } from "../chunk-KZ2QGVO3.js";
19
19
  import {
20
20
  introspectTrinoSchema,
21
21
  introspectTrinoSchemaWithConnection,
@@ -3,7 +3,7 @@ import {
3
3
  lookupIds,
4
4
  performIngest,
5
5
  resolveIds
6
- } from "./chunk-XFLBPF4A.js";
6
+ } from "./chunk-3DMBWVAH.js";
7
7
  import "./chunk-MLKGABMK.js";
8
8
  export {
9
9
  IngestPartialFailureError,
@@ -11,13 +11,13 @@ import {
11
11
  performPrune,
12
12
  performSync,
13
13
  splitIntoBatches
14
- } from "./chunk-QWOXEAQW.js";
15
- import "./chunk-XFLBPF4A.js";
14
+ } from "./chunk-SUTD4AF4.js";
15
+ import "./chunk-3DMBWVAH.js";
16
16
  import {
17
17
  deriveSourceKey,
18
18
  frontmatterSourceKey,
19
19
  validateSourceKey
20
- } from "./chunk-4J77FNJJ.js";
20
+ } from "./chunk-KZ2QGVO3.js";
21
21
  import "./chunk-GVWDS5TG.js";
22
22
  import "./chunk-MLKGABMK.js";
23
23
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brainbox-labs/sdk",
3
- "version": "0.24.0",
3
+ "version": "0.26.0",
4
4
  "description": "BrainBox JavaScript/TypeScript SDK — MCP server + API client",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -32,7 +32,7 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@brainbox-labs/api": "^0.11.0",
35
- "@brainbox-labs/types": "^1.34.0",
35
+ "@brainbox-labs/types": "^1.36.0",
36
36
  "@modelcontextprotocol/sdk": "^1.27.1",
37
37
  "dotenv": "^16.4.5",
38
38
  "express": "^5.0.0",