@brainbox-labs/sdk 0.23.1 → 0.24.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/{sync-K4NBZBF3.js → chunk-QWOXEAQW.js} +99 -14
- package/dist/index.d.ts +48 -3
- package/dist/index.js +7 -2
- package/dist/sync-W5BVNQLX.js +39 -0
- package/package.json +1 -1
|
@@ -13,7 +13,6 @@ import {
|
|
|
13
13
|
import {
|
|
14
14
|
createBrainExecutor
|
|
15
15
|
} from "./chunk-GVWDS5TG.js";
|
|
16
|
-
import "./chunk-MLKGABMK.js";
|
|
17
16
|
|
|
18
17
|
// src/sync/index.ts
|
|
19
18
|
import * as nodePath2 from "path";
|
|
@@ -106,13 +105,58 @@ function executorLister(exec) {
|
|
|
106
105
|
};
|
|
107
106
|
}
|
|
108
107
|
|
|
108
|
+
// src/sync/batch.ts
|
|
109
|
+
var DEFAULT_MAX_BATCH_BYTES = 7e5;
|
|
110
|
+
var DEFAULT_MAX_BATCH_DOCS = 50;
|
|
111
|
+
function entryBytes(entry) {
|
|
112
|
+
return Buffer.byteLength(JSON.stringify(entry), "utf8");
|
|
113
|
+
}
|
|
114
|
+
function splitIntoBatches(entries, limits) {
|
|
115
|
+
const batches = [];
|
|
116
|
+
let current = [];
|
|
117
|
+
let currentBytes = 0;
|
|
118
|
+
const flush = () => {
|
|
119
|
+
if (current.length > 0) batches.push({ entries: current });
|
|
120
|
+
current = [];
|
|
121
|
+
currentBytes = 0;
|
|
122
|
+
};
|
|
123
|
+
for (const entry of entries) {
|
|
124
|
+
const bytes = entryBytes(entry);
|
|
125
|
+
if (bytes > limits.maxBytes) {
|
|
126
|
+
flush();
|
|
127
|
+
batches.push({ entries: [entry], oversize: true });
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (current.length > 0 && (currentBytes + bytes > limits.maxBytes || current.length >= limits.maxDocs)) flush();
|
|
131
|
+
current.push(entry);
|
|
132
|
+
currentBytes += bytes;
|
|
133
|
+
}
|
|
134
|
+
flush();
|
|
135
|
+
return batches;
|
|
136
|
+
}
|
|
137
|
+
|
|
109
138
|
// src/sync/index.ts
|
|
110
139
|
var DOC_GLOB = "**/*.{md,markdown,txt,html,htm,pdf,docx}";
|
|
140
|
+
var SERVER_WIDE_FAILURE = /\b(401|403)\b|ECONNREFUSED|ENOTFOUND|ETIMEDOUT|fetch failed/i;
|
|
111
141
|
var noopLogger = { isVerbose: false, debug: () => {
|
|
112
142
|
}, info: () => {
|
|
113
143
|
}, warn: () => {
|
|
114
144
|
}, error: () => {
|
|
115
145
|
} };
|
|
146
|
+
var SyncPartialFailureError = class extends Error {
|
|
147
|
+
result;
|
|
148
|
+
constructor(result) {
|
|
149
|
+
const failed = (result.batches ?? []).filter((b) => b.status === "failed");
|
|
150
|
+
const landed = (result.batches ?? []).filter((b) => b.status === "landed").reduce((n, b) => n + b.sourceKeys.length, 0);
|
|
151
|
+
const parts = [`${landed} doc(s) landed`];
|
|
152
|
+
for (const f of failed) parts.push(`batch ${f.batch} failed (${f.sourceKeys.length} doc(s)): ${f.error}`);
|
|
153
|
+
const notAttempted = (result.batches ?? []).filter((b) => b.status === "notAttempted").reduce((n, b) => n + b.sourceKeys.length, 0);
|
|
154
|
+
if (notAttempted > 0) parts.push(`${notAttempted} doc(s) not attempted`);
|
|
155
|
+
super(`[BrainBox] sync incomplete: ${parts.join("; ")}. Rerun to resume; if a failed batch had uploaded but its context then failed, the server may call it unchanged, so rerun with force.`);
|
|
156
|
+
this.name = "SyncPartialFailureError";
|
|
157
|
+
this.result = result;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
116
160
|
async function collect(opts, transform) {
|
|
117
161
|
const raw = [];
|
|
118
162
|
if (opts.dir) {
|
|
@@ -216,15 +260,53 @@ async function performSync(ctx, syncOpts, deps = {}) {
|
|
|
216
260
|
if (projectId === void 0) throw new Error("[BrainBox] sync: project was not resolved");
|
|
217
261
|
const sendKeys = new Set([...diff.added, ...diff.changed, ...opts.force ? diff.unchanged : []].map((d) => d.sourceKey));
|
|
218
262
|
const toSend = docs.filter((d) => sendKeys.has(d.local.sourceKey)).map((d) => d.entry);
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
263
|
+
const batches = splitIntoBatches(toSend, {
|
|
264
|
+
maxBytes: opts.maxBatchBytes ?? DEFAULT_MAX_BATCH_BYTES,
|
|
265
|
+
maxDocs: opts.maxBatchDocs ?? DEFAULT_MAX_BATCH_DOCS
|
|
266
|
+
});
|
|
267
|
+
if (batches.length > 0) {
|
|
268
|
+
result.batches = [];
|
|
269
|
+
const stages = [];
|
|
270
|
+
const emit = (e) => {
|
|
271
|
+
try {
|
|
272
|
+
opts.onProgress?.(e);
|
|
273
|
+
} catch {
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
let stopped = false;
|
|
277
|
+
for (const [i, { entries: batch, oversize }] of batches.entries()) {
|
|
278
|
+
const n = i + 1;
|
|
279
|
+
const sourceKeys = batch.map((e) => e.sourceKey);
|
|
280
|
+
const flag = oversize ? { oversize } : {};
|
|
281
|
+
if (stopped) {
|
|
282
|
+
result.batches.push({ batch: n, status: "notAttempted", sourceKeys, ...flag });
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
emit({ phase: "start", batch: n, of: batches.length, docs: batch.length, bytes: batch.reduce((t, e) => t + entryBytes(e), 0) });
|
|
286
|
+
try {
|
|
287
|
+
const ingest = await performIngest(ctx, {
|
|
288
|
+
docs: batch,
|
|
289
|
+
...opts.force ? { forceDocs: true } : {},
|
|
290
|
+
...opts.timeoutMs !== void 0 ? { timeoutMs: opts.timeoutMs } : {},
|
|
291
|
+
...opts.reviewOnly ? { reviewOnly: true } : {}
|
|
292
|
+
}, { orgId, projectId });
|
|
293
|
+
stages.push(...ingest.stages);
|
|
294
|
+
result.batches.push({ batch: n, status: "landed", sourceKeys, ...flag, ingest });
|
|
295
|
+
} catch (err) {
|
|
296
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
297
|
+
result.batches.push({ batch: n, status: "failed", sourceKeys, ...flag, error });
|
|
298
|
+
if (!oversize || SERVER_WIDE_FAILURE.test(error)) stopped = true;
|
|
299
|
+
emit({ phase: "failed", batch: n, of: batches.length, docs: batch.length, error });
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
emit({ phase: "done", batch: n, of: batches.length, docs: batch.length });
|
|
303
|
+
}
|
|
304
|
+
if (stages.length > 0) {
|
|
305
|
+
result.ingest = { stages };
|
|
306
|
+
result.skippedByServer = stages.flatMap((s) => s.unchanged ?? []);
|
|
307
|
+
}
|
|
227
308
|
}
|
|
309
|
+
if (result.batches?.some((b) => b.status !== "landed")) throw new SyncPartialFailureError(result);
|
|
228
310
|
if (opts.prune && result.pendingDeletes.length > 0) {
|
|
229
311
|
const pruned = await prune(api, orgId, projectId, result.pendingDeletes.map((d) => d.sourceKey));
|
|
230
312
|
result.archived = pruned.archived;
|
|
@@ -251,15 +333,18 @@ async function performPrune(ctx, sourceKeys, ids) {
|
|
|
251
333
|
}
|
|
252
334
|
return prune(api, ids.orgId, ids.projectId, sourceKeys);
|
|
253
335
|
}
|
|
336
|
+
|
|
254
337
|
export {
|
|
338
|
+
loadTransform,
|
|
255
339
|
applyTransform,
|
|
256
340
|
computeSourceVersion,
|
|
257
|
-
deriveSourceKey,
|
|
258
341
|
diffSources,
|
|
259
342
|
executorLister,
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
343
|
+
DEFAULT_MAX_BATCH_BYTES,
|
|
344
|
+
DEFAULT_MAX_BATCH_DOCS,
|
|
345
|
+
entryBytes,
|
|
346
|
+
splitIntoBatches,
|
|
347
|
+
SyncPartialFailureError,
|
|
263
348
|
performSync,
|
|
264
|
-
|
|
349
|
+
performPrune
|
|
265
350
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -209,8 +209,42 @@ interface SyncOptions {
|
|
|
209
209
|
dryRun?: boolean;
|
|
210
210
|
analysisDepth?: AnalysisDepth;
|
|
211
211
|
timeoutMs?: number;
|
|
212
|
-
/** Stop before activating the new version so a reviewer can curate first. */
|
|
212
|
+
/** Stop before activating the new version so a reviewer can curate first. With several batches this leaves one un-activated context per batch. */
|
|
213
213
|
reviewOnly?: boolean;
|
|
214
|
+
/** Serialized bytes per upload request (default 700_000). Docs are split into batches under this; a single doc over it is still sent, alone in its own batch, and the server decides. */
|
|
215
|
+
maxBatchBytes?: number;
|
|
216
|
+
/** Docs per upload request (default 50). */
|
|
217
|
+
maxBatchDocs?: number;
|
|
218
|
+
/** Called as each batch starts and finishes, so a caller can show progress. */
|
|
219
|
+
onProgress?: (event: SyncProgress) => void;
|
|
220
|
+
}
|
|
221
|
+
type SyncProgress = {
|
|
222
|
+
phase: 'start';
|
|
223
|
+
batch: number;
|
|
224
|
+
of: number;
|
|
225
|
+
docs: number;
|
|
226
|
+
bytes: number;
|
|
227
|
+
} | {
|
|
228
|
+
phase: 'done';
|
|
229
|
+
batch: number;
|
|
230
|
+
of: number;
|
|
231
|
+
docs: number;
|
|
232
|
+
} | {
|
|
233
|
+
phase: 'failed';
|
|
234
|
+
batch: number;
|
|
235
|
+
of: number;
|
|
236
|
+
docs: number;
|
|
237
|
+
error: string;
|
|
238
|
+
};
|
|
239
|
+
/** One upload request. `landed` batches were ingested; `failed` ones were not (their keys are safe to retry); `notAttempted` ones were never sent because an earlier batch failed. */
|
|
240
|
+
interface SyncBatchResult {
|
|
241
|
+
batch: number;
|
|
242
|
+
status: 'landed' | 'failed' | 'notAttempted';
|
|
243
|
+
sourceKeys: string[];
|
|
244
|
+
/** A single doc over `maxBatchBytes`, sent alone. If the server rejects it, the other batches still go. */
|
|
245
|
+
oversize?: true;
|
|
246
|
+
ingest?: IngestResult;
|
|
247
|
+
error?: string;
|
|
214
248
|
}
|
|
215
249
|
interface SyncEntry {
|
|
216
250
|
sourceKey: string;
|
|
@@ -241,8 +275,19 @@ interface SyncResult {
|
|
|
241
275
|
skippedByServer: string[];
|
|
242
276
|
/** Files a transform dropped (returned null). */
|
|
243
277
|
skippedByTransform: string[];
|
|
244
|
-
/** Absent for a dry run or when nothing needed ingesting. */
|
|
278
|
+
/** Absent for a dry run or when nothing needed ingesting. The stages of every batch that landed, in order. */
|
|
245
279
|
ingest?: IngestResult;
|
|
280
|
+
/** One entry per upload request, in order. Absent for a dry run or when nothing needed ingesting. */
|
|
281
|
+
batches?: SyncBatchResult[];
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Thrown after a sync in which a batch failed. Everything that did land
|
|
285
|
+
* stays landed: `result.batches` says which keys, and a rerun resumes because landed keys come back
|
|
286
|
+
* unchanged. Deletes were still computed but nothing was archived.
|
|
287
|
+
*/
|
|
288
|
+
declare class SyncPartialFailureError extends Error {
|
|
289
|
+
readonly result: SyncResult;
|
|
290
|
+
constructor(result: SyncResult);
|
|
246
291
|
}
|
|
247
292
|
interface PruneResult {
|
|
248
293
|
archived: string[];
|
|
@@ -473,4 +518,4 @@ declare class BrainBoxServe {
|
|
|
473
518
|
mcp(opts?: Omit<MCPOptions, 'apiKey' | 'project' | 'apiUrl'>): MCP;
|
|
474
519
|
}
|
|
475
520
|
|
|
476
|
-
export { BrainBox, type BrainBoxOptions, BrainBoxServe, BrainDispatchError, type BrainProjects, DocEntry, type IngestOptions, IngestPartialFailureError, type IngestPostgresOptions, type IngestResult, type IngestStageResult, type LocalGrepWikiArgs, type LocalGrepWikiResult, type PendingDelete, type ProjectView, type PruneResult, type SyncEntry, type SyncOptions, type SyncResult, type TenantView, isTenantKey };
|
|
521
|
+
export { BrainBox, type BrainBoxOptions, BrainBoxServe, BrainDispatchError, type BrainProjects, DocEntry, type IngestOptions, IngestPartialFailureError, type IngestPostgresOptions, type IngestResult, type IngestStageResult, type LocalGrepWikiArgs, type LocalGrepWikiResult, type PendingDelete, type ProjectView, type PruneResult, type SyncBatchResult, type SyncEntry, type SyncOptions, SyncPartialFailureError, type SyncProgress, type SyncResult, type TenantView, isTenantKey };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SyncPartialFailureError
|
|
3
|
+
} from "./chunk-QWOXEAQW.js";
|
|
1
4
|
import {
|
|
2
5
|
IngestPartialFailureError
|
|
3
6
|
} from "./chunk-XFLBPF4A.js";
|
|
7
|
+
import "./chunk-4J77FNJJ.js";
|
|
4
8
|
import {
|
|
5
9
|
BrainBoxClient,
|
|
6
10
|
MCP,
|
|
@@ -405,7 +409,7 @@ var BrainBox = class {
|
|
|
405
409
|
throw new Error("[BrainBox] brain.sync does not support tenant keys yet. Use a project or org key.");
|
|
406
410
|
}
|
|
407
411
|
const ctx = await this.ingestContext("sync");
|
|
408
|
-
const { performSync } = await import("./sync-
|
|
412
|
+
const { performSync } = await import("./sync-W5BVNQLX.js");
|
|
409
413
|
return performSync(ctx, opts);
|
|
410
414
|
}
|
|
411
415
|
/** Archive the synced pages for these `sourceKey`s (soft, reversible server-side). Unknown keys come back in `notFound`. */
|
|
@@ -414,7 +418,7 @@ var BrainBox = class {
|
|
|
414
418
|
throw new Error("[BrainBox] brain.prune does not support tenant keys yet. Use a project or org key.");
|
|
415
419
|
}
|
|
416
420
|
const ctx = await this.ingestContext("prune");
|
|
417
|
-
const { performPrune } = await import("./sync-
|
|
421
|
+
const { performPrune } = await import("./sync-W5BVNQLX.js");
|
|
418
422
|
return performPrune(ctx, sourceKeys);
|
|
419
423
|
}
|
|
420
424
|
/** Target for write-side calls: a tenant key's overlay project, else the handle's `project`. */
|
|
@@ -521,6 +525,7 @@ export {
|
|
|
521
525
|
BrainBoxServe,
|
|
522
526
|
BrainDispatchError,
|
|
523
527
|
IngestPartialFailureError,
|
|
528
|
+
SyncPartialFailureError,
|
|
524
529
|
isTenantKey,
|
|
525
530
|
serve_exports as serve
|
|
526
531
|
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_MAX_BATCH_BYTES,
|
|
3
|
+
DEFAULT_MAX_BATCH_DOCS,
|
|
4
|
+
SyncPartialFailureError,
|
|
5
|
+
applyTransform,
|
|
6
|
+
computeSourceVersion,
|
|
7
|
+
diffSources,
|
|
8
|
+
entryBytes,
|
|
9
|
+
executorLister,
|
|
10
|
+
loadTransform,
|
|
11
|
+
performPrune,
|
|
12
|
+
performSync,
|
|
13
|
+
splitIntoBatches
|
|
14
|
+
} from "./chunk-QWOXEAQW.js";
|
|
15
|
+
import "./chunk-XFLBPF4A.js";
|
|
16
|
+
import {
|
|
17
|
+
deriveSourceKey,
|
|
18
|
+
frontmatterSourceKey,
|
|
19
|
+
validateSourceKey
|
|
20
|
+
} from "./chunk-4J77FNJJ.js";
|
|
21
|
+
import "./chunk-GVWDS5TG.js";
|
|
22
|
+
import "./chunk-MLKGABMK.js";
|
|
23
|
+
export {
|
|
24
|
+
DEFAULT_MAX_BATCH_BYTES,
|
|
25
|
+
DEFAULT_MAX_BATCH_DOCS,
|
|
26
|
+
SyncPartialFailureError,
|
|
27
|
+
applyTransform,
|
|
28
|
+
computeSourceVersion,
|
|
29
|
+
deriveSourceKey,
|
|
30
|
+
diffSources,
|
|
31
|
+
entryBytes,
|
|
32
|
+
executorLister,
|
|
33
|
+
frontmatterSourceKey,
|
|
34
|
+
loadTransform,
|
|
35
|
+
performPrune,
|
|
36
|
+
performSync,
|
|
37
|
+
splitIntoBatches,
|
|
38
|
+
validateSourceKey
|
|
39
|
+
};
|