@zenera/rag 1.1.6 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -2
- package/dist/command.js +41 -727
- package/dist/common/embedder.d.ts +3 -0
- package/dist/common/embedder.js +64 -0
- package/dist/common/locate.d.ts +18 -0
- package/dist/{schema → common}/locate.js +42 -28
- package/dist/common/manifest.d.ts +50 -0
- package/dist/common/manifest.js +62 -0
- package/dist/common/progress.d.ts +57 -0
- package/dist/common/progress.js +155 -0
- package/dist/common/prose.d.ts +13 -0
- package/dist/common/prose.js +56 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +6 -4
- package/dist/schema/build.js +6 -2
- package/dist/schema/command.d.ts +3 -0
- package/dist/schema/command.js +819 -0
- package/dist/schema/files.d.ts +5 -27
- package/dist/schema/files.js +9 -29
- package/dist/schema/lookup.d.ts +1 -1
- package/dist/schema/lookup.js +1 -1
- package/dist/{present.d.ts → schema/present.d.ts} +5 -5
- package/dist/{present.js → schema/present.js} +2 -2
- package/dist/{query.d.ts → schema/query.d.ts} +1 -1
- package/dist/schema/readme.d.ts +6 -0
- package/dist/schema/readme.js +122 -0
- package/dist/{repl.d.ts → schema/repl.d.ts} +1 -1
- package/dist/schema/search.js +2 -1
- package/dist/schema/tools.d.ts +1 -1
- package/dist/schema/tools.js +97 -6
- package/dist/schema/trace.d.ts +52 -0
- package/dist/schema/trace.js +144 -0
- package/package.json +3 -3
- package/dist/schema/locate.d.ts +0 -20
- package/dist/schema/progress.d.ts +0 -26
- package/dist/schema/progress.js +0 -316
- /package/dist/{schema → common}/match.d.ts +0 -0
- /package/dist/{schema → common}/match.js +0 -0
- /package/dist/{query.js → schema/query.js} +0 -0
- /package/dist/{repl.js → schema/repl.js} +0 -0
package/dist/schema/locate.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/** The name a new index is given. Nothing searches for it; only `index --out` writes it. */
|
|
2
|
-
export declare const DEFAULT_DIR = "./schema-db";
|
|
3
|
-
export declare const DIR_ENV = "ZEN_SCHEMA_DB";
|
|
4
|
-
/** How the directory was arrived at, which is what decides whether to say so. */
|
|
5
|
-
export type DirSource = 'flag' | 'env' | 'found' | 'default';
|
|
6
|
-
export interface Located {
|
|
7
|
-
dir: string;
|
|
8
|
-
from: DirSource;
|
|
9
|
-
}
|
|
10
|
-
export interface LocateOptions {
|
|
11
|
-
env?: NodeJS.ProcessEnv;
|
|
12
|
-
/** do not climb above this; the home directory, or the root, by default */
|
|
13
|
-
ceiling?: string;
|
|
14
|
-
}
|
|
15
|
-
export declare function locateIndex(cwd: string, flag?: string, options?: LocateOptions): Located;
|
|
16
|
-
/** Where a new index goes: the same environment variable, minus the search. */
|
|
17
|
-
export declare function outputDir(cwd: string, flag?: string, env?: NodeJS.ProcessEnv): string;
|
|
18
|
-
/** An index is a directory with a manifest in it; nothing else is asserted here. */
|
|
19
|
-
export declare function isIndex(dir: string): boolean;
|
|
20
|
-
//# sourceMappingURL=locate.d.ts.map
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { Counts, Manifest } from './files.ts';
|
|
2
|
-
export declare const LOCK_FILE = ".lock";
|
|
3
|
-
export declare const README_FILE = "README.md";
|
|
4
|
-
export type Phase = 'reading' | 'graph' | 'embedding' | 'writing';
|
|
5
|
-
export interface BuildPlan {
|
|
6
|
-
/** where the index goes */
|
|
7
|
-
dir: string;
|
|
8
|
-
files: readonly string[];
|
|
9
|
-
/** the embedding reference as it was typed */
|
|
10
|
-
embedding: string;
|
|
11
|
-
indexer: string;
|
|
12
|
-
}
|
|
13
|
-
export interface Journal {
|
|
14
|
-
phase(name: Phase): void;
|
|
15
|
-
/** what the documents turned out to hold, once they have been read */
|
|
16
|
-
read(counts: Counts): void;
|
|
17
|
-
progress(done: number, total: number): void;
|
|
18
|
-
finish(manifest: Manifest): void;
|
|
19
|
-
fail(reason: unknown): void;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Takes the directory, or refuses it. Two builds writing one index would
|
|
23
|
-
* interleave their LanceDB writes and leave a store neither of them describes.
|
|
24
|
-
*/
|
|
25
|
-
export declare function beginBuild(plan: BuildPlan): Journal;
|
|
26
|
-
//# sourceMappingURL=progress.d.ts.map
|
package/dist/schema/progress.js
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
import { CliError, EXIT } from '@zenera/cli/lib';
|
|
2
|
-
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
-
import { hostname } from 'node:os';
|
|
4
|
-
import { basename, join } from 'node:path';
|
|
5
|
-
// ---------------------------------------------------------------------------
|
|
6
|
-
// A build that says what it is doing
|
|
7
|
-
//
|
|
8
|
-
// Indexing a large document is minutes of silence with a directory slowly
|
|
9
|
-
// filling up, and the directory is the only thing a second person — or a second
|
|
10
|
-
// process, or an agent reading the tree — ever sees. So the build writes two
|
|
11
|
-
// files into it and keeps them true:
|
|
12
|
-
//
|
|
13
|
-
// .lock who is building this, right now. Gone when nothing is.
|
|
14
|
-
// README.md a progress report while it runs, and a description of what the
|
|
15
|
-
// index holds once it does not.
|
|
16
|
-
//
|
|
17
|
-
// Nothing in either file is an absolute path. A project's `assets/` directory
|
|
18
|
-
// is mounted at `/assets` inside an agent's sandbox, so an index built there is
|
|
19
|
-
// read under a name this process never sees; a host path would be a lie there.
|
|
20
|
-
// ---------------------------------------------------------------------------
|
|
21
|
-
export const LOCK_FILE = '.lock';
|
|
22
|
-
export const README_FILE = 'README.md';
|
|
23
|
-
/** The floor on how often README.md is rewritten. */
|
|
24
|
-
const INTERVAL_MS = 5000;
|
|
25
|
-
const PHASES = {
|
|
26
|
-
reading: 'reading the documents',
|
|
27
|
-
graph: 'building the graph',
|
|
28
|
-
embedding: 'embedding',
|
|
29
|
-
writing: 'writing the store',
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* Takes the directory, or refuses it. Two builds writing one index would
|
|
33
|
-
* interleave their LanceDB writes and leave a store neither of them describes.
|
|
34
|
-
*/
|
|
35
|
-
export function beginBuild(plan) {
|
|
36
|
-
const documents = plan.files.map((file) => basename(file));
|
|
37
|
-
const started = Date.now();
|
|
38
|
-
const lock = {
|
|
39
|
-
pid: process.pid,
|
|
40
|
-
host: hostname(),
|
|
41
|
-
startedAt: new Date(started).toISOString(),
|
|
42
|
-
indexer: plan.indexer,
|
|
43
|
-
embedding: plan.embedding,
|
|
44
|
-
documents,
|
|
45
|
-
};
|
|
46
|
-
mkdirSync(plan.dir, { recursive: true });
|
|
47
|
-
claim(join(plan.dir, LOCK_FILE), lock);
|
|
48
|
-
let phase = 'reading';
|
|
49
|
-
let counts;
|
|
50
|
-
let done = 0;
|
|
51
|
-
let total = 0;
|
|
52
|
-
let wroteAt = 0;
|
|
53
|
-
let closed = false;
|
|
54
|
-
const write = (body) => {
|
|
55
|
-
// Through a temp name, so a reader never catches half a file.
|
|
56
|
-
const target = join(plan.dir, README_FILE);
|
|
57
|
-
const temp = `${target}.tmp`;
|
|
58
|
-
writeFileSync(temp, body);
|
|
59
|
-
renameSync(temp, target);
|
|
60
|
-
wroteAt = Date.now();
|
|
61
|
-
};
|
|
62
|
-
const report = () => write(building({
|
|
63
|
-
documents,
|
|
64
|
-
embedding: plan.embedding,
|
|
65
|
-
started,
|
|
66
|
-
phase,
|
|
67
|
-
done,
|
|
68
|
-
total,
|
|
69
|
-
counts,
|
|
70
|
-
}));
|
|
71
|
-
const maybe = () => {
|
|
72
|
-
if (!closed && Date.now() - wroteAt >= INTERVAL_MS) {
|
|
73
|
-
report();
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
// The first embedding request can block for ten seconds or more, so a purely
|
|
77
|
-
// event-driven throttle would leave the elapsed line frozen through it.
|
|
78
|
-
const ticker = setInterval(maybe, INTERVAL_MS);
|
|
79
|
-
ticker.unref();
|
|
80
|
-
const close = (body) => {
|
|
81
|
-
if (closed) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
closed = true;
|
|
85
|
-
clearInterval(ticker);
|
|
86
|
-
write(body);
|
|
87
|
-
rmSync(join(plan.dir, LOCK_FILE), { force: true });
|
|
88
|
-
};
|
|
89
|
-
report();
|
|
90
|
-
return {
|
|
91
|
-
phase(name) {
|
|
92
|
-
phase = name;
|
|
93
|
-
maybe();
|
|
94
|
-
},
|
|
95
|
-
read(seen) {
|
|
96
|
-
counts = seen;
|
|
97
|
-
total = seen.entities;
|
|
98
|
-
maybe();
|
|
99
|
-
},
|
|
100
|
-
progress(at, of) {
|
|
101
|
-
done = at;
|
|
102
|
-
total = of;
|
|
103
|
-
maybe();
|
|
104
|
-
},
|
|
105
|
-
finish(manifest) {
|
|
106
|
-
close(complete(plan.dir, manifest, Date.now() - started));
|
|
107
|
-
},
|
|
108
|
-
fail(reason) {
|
|
109
|
-
close(failed({ documents, phase, reason, started }));
|
|
110
|
-
},
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* `wx` makes the create and the check one operation, so two builds racing for
|
|
115
|
-
* the same directory cannot both win. A lock whose process is gone is stale by
|
|
116
|
-
* definition and is taken rather than respected — a crashed build must not make
|
|
117
|
-
* a directory permanently unbuildable.
|
|
118
|
-
*/
|
|
119
|
-
function claim(path, lock) {
|
|
120
|
-
const body = `${JSON.stringify(lock, null, 4)}\n`;
|
|
121
|
-
try {
|
|
122
|
-
writeFileSync(path, body, { flag: 'wx' });
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
catch (err) {
|
|
126
|
-
if (err.code !== 'EEXIST') {
|
|
127
|
-
throw err;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
const held = readLock(path);
|
|
131
|
-
if (held && held.host === hostname() && alive(held.pid)) {
|
|
132
|
-
throw new CliError(`this index is already being built (pid ${held.pid}, since ${held.startedAt})`, EXIT.failed, `wait for it, or build elsewhere with --out`);
|
|
133
|
-
}
|
|
134
|
-
writeFileSync(path, body);
|
|
135
|
-
}
|
|
136
|
-
function readLock(path) {
|
|
137
|
-
try {
|
|
138
|
-
return JSON.parse(readFileSync(path, 'utf8'));
|
|
139
|
-
}
|
|
140
|
-
catch {
|
|
141
|
-
return undefined;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* `kill(pid, 0)` sends no signal and only asks whether the process exists.
|
|
146
|
-
* EPERM means it exists and belongs to someone else, which still counts.
|
|
147
|
-
*/
|
|
148
|
-
function alive(pid) {
|
|
149
|
-
try {
|
|
150
|
-
process.kill(pid, 0);
|
|
151
|
-
return true;
|
|
152
|
-
}
|
|
153
|
-
catch (err) {
|
|
154
|
-
return err.code === 'EPERM';
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
function building(state) {
|
|
158
|
-
const now = Date.now();
|
|
159
|
-
const rows = [
|
|
160
|
-
['documents', state.documents.join(', ')],
|
|
161
|
-
['embedding', state.embedding],
|
|
162
|
-
[
|
|
163
|
-
'started',
|
|
164
|
-
`${new Date(state.started).toISOString()} (${duration(now - state.started)} ago)`,
|
|
165
|
-
],
|
|
166
|
-
['step', PHASES[state.phase]],
|
|
167
|
-
];
|
|
168
|
-
if (state.counts) {
|
|
169
|
-
rows.push(['found', entities(state.counts)]);
|
|
170
|
-
}
|
|
171
|
-
if (state.total > 0) {
|
|
172
|
-
const percent = Math.round((state.done / state.total) * 100);
|
|
173
|
-
rows.push(['embedded', `${state.done} of ${state.total} · ${percent}%`]);
|
|
174
|
-
}
|
|
175
|
-
rows.push(['updated', new Date(now).toISOString()]);
|
|
176
|
-
return [
|
|
177
|
-
'# Schema index — being built',
|
|
178
|
-
'',
|
|
179
|
-
'A searchable index of the API documents named below, written by `zen rag schema index`.',
|
|
180
|
-
'**It is incomplete. Nothing should read it yet.**',
|
|
181
|
-
'',
|
|
182
|
-
...fields(rows),
|
|
183
|
-
'',
|
|
184
|
-
`These lines are refreshed at most every ${INTERVAL_MS / 1000} seconds while the build runs, and`,
|
|
185
|
-
'the whole file is replaced by a description of the index when it finishes. If it still says',
|
|
186
|
-
'"being built" and `.lock` names no living process, the build died part way.',
|
|
187
|
-
'',
|
|
188
|
-
].join('\n');
|
|
189
|
-
}
|
|
190
|
-
function complete(dir, manifest, ms) {
|
|
191
|
-
const titles = manifest.sources.map((s) => s.title).filter(Boolean);
|
|
192
|
-
const what = titles.length > 0 ? titles.join(', ') : basename(dir);
|
|
193
|
-
return [
|
|
194
|
-
`# Schema index — ${what}`,
|
|
195
|
-
'',
|
|
196
|
-
`A searchable index of ${plural(manifest.sources.length, 'API document')}, built with`,
|
|
197
|
-
`${manifest.embedding.ref} (${manifest.embedding.dimensions}d) in ${duration(ms)}.`,
|
|
198
|
-
'Ask it for the operations and types behind a question and it answers with a subgraph:',
|
|
199
|
-
'the endpoints that match, the schemas they carry, and the fields inside those — printed',
|
|
200
|
-
'as text, Mermaid, TypeScript or OpenAPI.',
|
|
201
|
-
'',
|
|
202
|
-
'## What it covers',
|
|
203
|
-
'',
|
|
204
|
-
...sourceTable(manifest.sources),
|
|
205
|
-
'',
|
|
206
|
-
`${entities(manifest.counts)},`,
|
|
207
|
-
`${searched(manifest.indexes)}.`,
|
|
208
|
-
'',
|
|
209
|
-
'## Files',
|
|
210
|
-
'',
|
|
211
|
-
...fields([
|
|
212
|
-
['manifest.json', 'what this index is and what built it — read this first'],
|
|
213
|
-
['graph.json', 'the nodes and edges: operations, types, fields'],
|
|
214
|
-
['schemas.json', 'the JSON Schema of every type'],
|
|
215
|
-
['operations.json', 'every operation, with its parameters and responses'],
|
|
216
|
-
...(manifest.sources.some((s) => s.path)
|
|
217
|
-
? [['sources/', 'the documents themselves, bundled, exactly as indexed']]
|
|
218
|
-
: []),
|
|
219
|
-
['lance/', 'the LanceDB table: the search text, the vectors, the filter columns'],
|
|
220
|
-
]),
|
|
221
|
-
'',
|
|
222
|
-
'## Asking it something',
|
|
223
|
-
'',
|
|
224
|
-
'From this directory:',
|
|
225
|
-
'',
|
|
226
|
-
'```',
|
|
227
|
-
'zen rag schema search --dir . --all "how do I cancel a subscription"',
|
|
228
|
-
'```',
|
|
229
|
-
'',
|
|
230
|
-
`Built by ${manifest.indexer} on ${manifest.createdAt}.`,
|
|
231
|
-
'',
|
|
232
|
-
].join('\n');
|
|
233
|
-
}
|
|
234
|
-
function failed(state) {
|
|
235
|
-
return [
|
|
236
|
-
'# Schema index — failed',
|
|
237
|
-
'',
|
|
238
|
-
'This index was not finished and what is here is incomplete. Nothing should read it;',
|
|
239
|
-
'build it again with `zen rag schema index`.',
|
|
240
|
-
'',
|
|
241
|
-
...fields([
|
|
242
|
-
['documents', state.documents.join(', ')],
|
|
243
|
-
['step', PHASES[state.phase]],
|
|
244
|
-
['reason', message(state.reason)],
|
|
245
|
-
['started', new Date(state.started).toISOString()],
|
|
246
|
-
[
|
|
247
|
-
'failed',
|
|
248
|
-
`${new Date().toISOString()} (after ${duration(Date.now() - state.started)})`,
|
|
249
|
-
],
|
|
250
|
-
]),
|
|
251
|
-
'',
|
|
252
|
-
].join('\n');
|
|
253
|
-
}
|
|
254
|
-
// ---------------------------------------------------------------------------
|
|
255
|
-
// Small renderings
|
|
256
|
-
// ---------------------------------------------------------------------------
|
|
257
|
-
/** An indented block, which markdown renders verbatim and an agent reads as a table. */
|
|
258
|
-
function fields(rows) {
|
|
259
|
-
const width = Math.max(...rows.map((r) => (r[0] ?? '').length));
|
|
260
|
-
return rows.map(([name, value]) => ` ${(name ?? '').padEnd(width)} ${value ?? ''}`);
|
|
261
|
-
}
|
|
262
|
-
const HEADERS = ['document', 'dialect', 'paths', 'operations', 'schemas', 'fields'];
|
|
263
|
-
function sourceTable(sources) {
|
|
264
|
-
const rows = sources.map((s) => [
|
|
265
|
-
s.file,
|
|
266
|
-
s.dialect,
|
|
267
|
-
String(s.paths),
|
|
268
|
-
String(s.methods),
|
|
269
|
-
String(s.types),
|
|
270
|
-
String(s.properties),
|
|
271
|
-
]);
|
|
272
|
-
const widths = HEADERS.map((h, i) => Math.max(h.length, ...rows.map((r) => (r[i] ?? '').length)));
|
|
273
|
-
const line = (cells) => `| ${cells.map((c, i) => c.padEnd(widths[i] ?? 0)).join(' | ')} |`;
|
|
274
|
-
return [
|
|
275
|
-
line(HEADERS),
|
|
276
|
-
`| ${widths.map((w) => '-'.repeat(w)).join(' | ')} |`,
|
|
277
|
-
...rows.map(line),
|
|
278
|
-
];
|
|
279
|
-
}
|
|
280
|
-
function entities(counts) {
|
|
281
|
-
return (`${plural(counts.entities, 'entity', 'entities')}: ` +
|
|
282
|
-
`${counts.methods} operations, ${counts.types} schemas, ${counts.properties} fields`);
|
|
283
|
-
}
|
|
284
|
-
function searched(indexes) {
|
|
285
|
-
if (!indexes.fts && !indexes.vector) {
|
|
286
|
-
return 'scanned flat: neither index was built';
|
|
287
|
-
}
|
|
288
|
-
if (!indexes.vector) {
|
|
289
|
-
// Below a couple of thousand rows an IVF index has nothing to train on.
|
|
290
|
-
return 'searched by full text and by vector, the latter as a flat scan';
|
|
291
|
-
}
|
|
292
|
-
return indexes.fts
|
|
293
|
-
? 'searched by full text and by vector'
|
|
294
|
-
: 'searched by vector, with no full-text index';
|
|
295
|
-
}
|
|
296
|
-
function plural(n, one, many = `${one}s`) {
|
|
297
|
-
return `${n} ${n === 1 ? one : many}`;
|
|
298
|
-
}
|
|
299
|
-
function duration(ms) {
|
|
300
|
-
const seconds = Math.round(ms / 1000);
|
|
301
|
-
if (seconds < 1) {
|
|
302
|
-
return 'under a second';
|
|
303
|
-
}
|
|
304
|
-
if (seconds < 60) {
|
|
305
|
-
return `${seconds}s`;
|
|
306
|
-
}
|
|
307
|
-
const minutes = Math.floor(seconds / 60);
|
|
308
|
-
return minutes < 60
|
|
309
|
-
? `${minutes}m${seconds % 60}s`
|
|
310
|
-
: `${Math.floor(minutes / 60)}h${minutes % 60}m`;
|
|
311
|
-
}
|
|
312
|
-
function message(reason) {
|
|
313
|
-
const text = reason instanceof Error ? reason.message : String(reason);
|
|
314
|
-
return text.split('\n')[0]?.trim() || 'no reason given';
|
|
315
|
-
}
|
|
316
|
-
//# sourceMappingURL=progress.js.map
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|