@karmaniverous/jeeves-watcher 0.4.2 → 0.4.3
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/config.schema.json +38 -11
- package/dist/cjs/index.js +276 -167
- package/dist/cli/jeeves-watcher/index.js +277 -168
- package/dist/index.d.ts +10 -9
- package/dist/index.iife.js +277 -168
- package/dist/index.iife.min.js +1 -1
- package/dist/mjs/index.js +277 -168
- package/package.json +1 -1
|
@@ -3,12 +3,13 @@ import { Command } from '@commander-js/extra-typings';
|
|
|
3
3
|
import { dirname, resolve, relative, join, extname, basename } from 'node:path';
|
|
4
4
|
import { existsSync, statSync, readdirSync, readFileSync } from 'node:fs';
|
|
5
5
|
import ignore from 'ignore';
|
|
6
|
+
import { JsonMap, jsonMapMapSchema } from '@karmaniverous/jsonmap';
|
|
7
|
+
import { get, capitalize, title, camel, snake, dash, isEqual, omit } from 'radash';
|
|
6
8
|
import Handlebars from 'handlebars';
|
|
7
9
|
import dayjs from 'dayjs';
|
|
8
10
|
import { toMdast } from 'hast-util-to-mdast';
|
|
9
11
|
import { fromADF } from 'mdast-util-from-adf';
|
|
10
12
|
import { toMarkdown } from 'mdast-util-to-markdown';
|
|
11
|
-
import { capitalize, title, camel, snake, dash, isEqual, omit, get } from 'radash';
|
|
12
13
|
import rehypeParse from 'rehype-parse';
|
|
13
14
|
import { unified } from 'unified';
|
|
14
15
|
import chokidar from 'chokidar';
|
|
@@ -18,7 +19,6 @@ import picomatch from 'picomatch';
|
|
|
18
19
|
import { createHash } from 'node:crypto';
|
|
19
20
|
import { cosmiconfig } from 'cosmiconfig';
|
|
20
21
|
import { z, ZodError } from 'zod';
|
|
21
|
-
import { jsonMapMapSchema, JsonMap } from '@karmaniverous/jsonmap';
|
|
22
22
|
import { GoogleGenerativeAIEmbeddings } from '@langchain/google-genai';
|
|
23
23
|
import pino from 'pino';
|
|
24
24
|
import { v5 } from 'uuid';
|
|
@@ -242,6 +242,251 @@ class GitignoreFilter {
|
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
244
|
|
|
245
|
+
/**
|
|
246
|
+
* @module rules/templates
|
|
247
|
+
* Resolves template variables (`${path.to.value}`) in rule `set` objects against file attributes.
|
|
248
|
+
*/
|
|
249
|
+
/**
|
|
250
|
+
* Resolve `${template.vars}` in a value against the given attributes.
|
|
251
|
+
*
|
|
252
|
+
* @param value - The value to resolve.
|
|
253
|
+
* @param attributes - The file attributes for variable lookup.
|
|
254
|
+
* @returns The resolved value.
|
|
255
|
+
*/
|
|
256
|
+
function resolveTemplateVars(value, attributes) {
|
|
257
|
+
if (typeof value !== 'string')
|
|
258
|
+
return value;
|
|
259
|
+
return value.replace(/\$\{([^}]+)\}/g, (_match, varPath) => {
|
|
260
|
+
const current = get(attributes, varPath);
|
|
261
|
+
if (current === null || current === undefined)
|
|
262
|
+
return '';
|
|
263
|
+
return typeof current === 'string' ? current : JSON.stringify(current);
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Resolve all template variables in a `set` object.
|
|
268
|
+
*
|
|
269
|
+
* @param setObj - The key-value pairs to resolve.
|
|
270
|
+
* @param attributes - The file attributes for variable lookup.
|
|
271
|
+
* @returns The resolved key-value pairs.
|
|
272
|
+
*/
|
|
273
|
+
function resolveSet(setObj, attributes) {
|
|
274
|
+
const result = {};
|
|
275
|
+
for (const [key, value] of Object.entries(setObj)) {
|
|
276
|
+
result[key] = resolveTemplateVars(value, attributes);
|
|
277
|
+
}
|
|
278
|
+
return result;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* @module rules/apply
|
|
283
|
+
* Applies compiled inference rules to file attributes, producing merged metadata via template resolution and JsonMap transforms.
|
|
284
|
+
*/
|
|
285
|
+
/**
|
|
286
|
+
* Create the lib object for JsonMap transformations.
|
|
287
|
+
*
|
|
288
|
+
* @param configDir - Optional config directory for resolving relative file paths in lookups.
|
|
289
|
+
* @returns The lib object.
|
|
290
|
+
*/
|
|
291
|
+
function createJsonMapLib(configDir, customLib) {
|
|
292
|
+
// Cache loaded JSON files within a single applyRules invocation.
|
|
293
|
+
const jsonCache = new Map();
|
|
294
|
+
const loadJson = (filePath) => {
|
|
295
|
+
const resolvedPath = configDir ? resolve(configDir, filePath) : filePath;
|
|
296
|
+
if (!jsonCache.has(resolvedPath)) {
|
|
297
|
+
const raw = readFileSync(resolvedPath, 'utf-8');
|
|
298
|
+
jsonCache.set(resolvedPath, JSON.parse(raw));
|
|
299
|
+
}
|
|
300
|
+
return jsonCache.get(resolvedPath);
|
|
301
|
+
};
|
|
302
|
+
return {
|
|
303
|
+
split: (str, separator) => str.split(separator),
|
|
304
|
+
slice: (arr, start, end) => arr.slice(start, end),
|
|
305
|
+
join: (arr, separator) => arr.join(separator),
|
|
306
|
+
toLowerCase: (str) => str.toLowerCase(),
|
|
307
|
+
replace: (str, search, replacement) => str.replace(search, replacement),
|
|
308
|
+
get: (obj, path) => get(obj, path),
|
|
309
|
+
/**
|
|
310
|
+
* Load a JSON file (relative to configDir) and look up a value by key,
|
|
311
|
+
* optionally drilling into a sub-path.
|
|
312
|
+
*
|
|
313
|
+
* @param filePath - Path to a JSON file (resolved relative to configDir).
|
|
314
|
+
* @param key - Top-level key to look up.
|
|
315
|
+
* @param field - Optional dot-path into the looked-up entry.
|
|
316
|
+
* @returns The resolved value, or null if not found.
|
|
317
|
+
*/
|
|
318
|
+
lookupJson: (filePath, key, field) => {
|
|
319
|
+
const data = loadJson(filePath);
|
|
320
|
+
const entry = data[key];
|
|
321
|
+
if (entry === undefined || entry === null)
|
|
322
|
+
return null;
|
|
323
|
+
if (field)
|
|
324
|
+
return get(entry, field) ?? null;
|
|
325
|
+
return entry;
|
|
326
|
+
},
|
|
327
|
+
/**
|
|
328
|
+
* Map an array of keys through a JSON lookup file, collecting a specific
|
|
329
|
+
* field from each matching entry. Non-matching keys are silently skipped.
|
|
330
|
+
* Array-valued fields are flattened into the result.
|
|
331
|
+
*
|
|
332
|
+
* @param filePath - Path to a JSON file (resolved relative to configDir).
|
|
333
|
+
* @param keys - Array of top-level keys to look up.
|
|
334
|
+
* @param field - Dot-path into each looked-up entry.
|
|
335
|
+
* @returns Flat array of resolved values.
|
|
336
|
+
*/
|
|
337
|
+
mapLookup: (filePath, keys, field) => {
|
|
338
|
+
if (!Array.isArray(keys))
|
|
339
|
+
return [];
|
|
340
|
+
const data = loadJson(filePath);
|
|
341
|
+
const results = [];
|
|
342
|
+
for (const k of keys) {
|
|
343
|
+
if (typeof k !== 'string')
|
|
344
|
+
continue;
|
|
345
|
+
const entry = data[k];
|
|
346
|
+
if (entry === undefined || entry === null)
|
|
347
|
+
continue;
|
|
348
|
+
const val = get(entry, field);
|
|
349
|
+
if (val === undefined || val === null)
|
|
350
|
+
continue;
|
|
351
|
+
if (Array.isArray(val)) {
|
|
352
|
+
for (const item of val) {
|
|
353
|
+
results.push(item);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
results.push(val);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return results;
|
|
361
|
+
},
|
|
362
|
+
...customLib,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Load custom JsonMap lib functions from file paths.
|
|
367
|
+
*
|
|
368
|
+
* Each module should default-export an object of functions,
|
|
369
|
+
* or use named exports. Only function-valued exports are merged.
|
|
370
|
+
*
|
|
371
|
+
* @param paths - File paths to custom lib modules.
|
|
372
|
+
* @param configDir - Directory to resolve relative paths against.
|
|
373
|
+
* @returns The merged custom lib functions.
|
|
374
|
+
*/
|
|
375
|
+
async function loadCustomMapHelpers(paths, configDir) {
|
|
376
|
+
const merged = {};
|
|
377
|
+
for (const p of paths) {
|
|
378
|
+
const resolved = resolve(configDir, p);
|
|
379
|
+
const mod = (await import(resolved));
|
|
380
|
+
const fns = typeof mod.default === 'object' && mod.default !== null
|
|
381
|
+
? mod.default
|
|
382
|
+
: mod;
|
|
383
|
+
for (const [key, val] of Object.entries(fns)) {
|
|
384
|
+
if (typeof val === 'function') {
|
|
385
|
+
merged[key] = val;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return merged;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Apply compiled inference rules to file attributes, returning merged metadata and optional rendered content.
|
|
393
|
+
*
|
|
394
|
+
* Rules are evaluated in order; later rules override earlier ones.
|
|
395
|
+
* If a rule has a `map`, the JsonMap transformation is applied after `set` resolution,
|
|
396
|
+
* and map output overrides set output on conflict.
|
|
397
|
+
*
|
|
398
|
+
* @param compiledRules - The compiled rules to evaluate.
|
|
399
|
+
* @param attributes - The file attributes to match against.
|
|
400
|
+
* @param namedMaps - Optional record of named JsonMap definitions.
|
|
401
|
+
* @param logger - Optional logger for warnings (falls back to console.warn).
|
|
402
|
+
* @param templateEngine - Optional template engine for rendering content templates.
|
|
403
|
+
* @param configDir - Optional config directory for resolving .json map file paths.
|
|
404
|
+
* @returns The merged metadata and optional rendered content.
|
|
405
|
+
*/
|
|
406
|
+
async function applyRules(compiledRules, attributes, namedMaps, logger, templateEngine, configDir, customMapLib) {
|
|
407
|
+
// JsonMap's type definitions expect a generic JsonMapLib shape with unary functions.
|
|
408
|
+
// Our helper functions accept multiple args, which JsonMap supports at runtime.
|
|
409
|
+
const lib = createJsonMapLib(configDir, customMapLib);
|
|
410
|
+
let merged = {};
|
|
411
|
+
let renderedContent = null;
|
|
412
|
+
const log = logger ?? console;
|
|
413
|
+
for (const [ruleIndex, { rule, validate }] of compiledRules.entries()) {
|
|
414
|
+
if (validate(attributes)) {
|
|
415
|
+
// Apply set resolution
|
|
416
|
+
const setOutput = resolveSet(rule.set, attributes);
|
|
417
|
+
merged = { ...merged, ...setOutput };
|
|
418
|
+
// Apply map transformation if present
|
|
419
|
+
if (rule.map) {
|
|
420
|
+
let mapDef;
|
|
421
|
+
// Resolve map reference
|
|
422
|
+
if (typeof rule.map === 'string') {
|
|
423
|
+
if (rule.map.endsWith('.json') && configDir) {
|
|
424
|
+
// File path: load from .json file
|
|
425
|
+
try {
|
|
426
|
+
const mapPath = resolve(configDir, rule.map);
|
|
427
|
+
const raw = readFileSync(mapPath, 'utf-8');
|
|
428
|
+
mapDef = JSON.parse(raw);
|
|
429
|
+
}
|
|
430
|
+
catch (error) {
|
|
431
|
+
log.warn(`Failed to load map file "${rule.map}": ${error instanceof Error ? error.message : String(error)}`);
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
mapDef = namedMaps?.[rule.map];
|
|
437
|
+
if (!mapDef) {
|
|
438
|
+
log.warn(`Map reference "${rule.map}" not found in named maps. Skipping map transformation.`);
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
mapDef = rule.map;
|
|
445
|
+
}
|
|
446
|
+
// Execute JsonMap transformation
|
|
447
|
+
try {
|
|
448
|
+
const jsonMap = new JsonMap(mapDef, lib);
|
|
449
|
+
const mapOutput = await jsonMap.transform(attributes);
|
|
450
|
+
if (mapOutput &&
|
|
451
|
+
typeof mapOutput === 'object' &&
|
|
452
|
+
!Array.isArray(mapOutput)) {
|
|
453
|
+
merged = { ...merged, ...mapOutput };
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
log.warn(`JsonMap transformation did not return an object; skipping merge.`);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
catch (error) {
|
|
460
|
+
log.warn(`JsonMap transformation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
// Render template if present
|
|
464
|
+
if (rule.template && templateEngine) {
|
|
465
|
+
const templateKey = `rule-${String(ruleIndex)}`;
|
|
466
|
+
// Build template context: attributes (with json spread at top) + map output
|
|
467
|
+
const context = {
|
|
468
|
+
...(attributes.json ?? {}),
|
|
469
|
+
...attributes,
|
|
470
|
+
...merged,
|
|
471
|
+
};
|
|
472
|
+
try {
|
|
473
|
+
const result = templateEngine.render(templateKey, context);
|
|
474
|
+
if (result && result.trim()) {
|
|
475
|
+
renderedContent = result;
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
log.warn(`Template for rule ${String(ruleIndex)} rendered empty output. Falling back to raw content.`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
catch (error) {
|
|
482
|
+
log.warn(`Template render failed for rule ${String(ruleIndex)}: ${error instanceof Error ? error.message : String(error)}. Falling back to raw content.`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return { metadata: merged, renderedContent };
|
|
488
|
+
}
|
|
489
|
+
|
|
245
490
|
/**
|
|
246
491
|
* @module templates/helpers
|
|
247
492
|
* Registers built-in Handlebars helpers for content templates.
|
|
@@ -1187,6 +1432,17 @@ const jeevesWatcherConfigSchema = z.object({
|
|
|
1187
1432
|
})
|
|
1188
1433
|
.optional()
|
|
1189
1434
|
.describe('Custom Handlebars helper registration.'),
|
|
1435
|
+
/** Custom JsonMap lib function registration. */
|
|
1436
|
+
mapHelpers: z
|
|
1437
|
+
.object({
|
|
1438
|
+
/** File paths to custom lib modules (each exports functions to merge into the JsonMap lib). */
|
|
1439
|
+
paths: z
|
|
1440
|
+
.array(z.string())
|
|
1441
|
+
.optional()
|
|
1442
|
+
.describe('File paths to JS modules exporting functions to merge into the JsonMap lib.'),
|
|
1443
|
+
})
|
|
1444
|
+
.optional()
|
|
1445
|
+
.describe('Custom JsonMap lib function registration.'),
|
|
1190
1446
|
/** Logging configuration. */
|
|
1191
1447
|
logging: loggingConfigSchema.optional().describe('Logging configuration.'),
|
|
1192
1448
|
/** Timeout in milliseconds for graceful shutdown. */
|
|
@@ -1677,160 +1933,6 @@ async function extractText(filePath, extension) {
|
|
|
1677
1933
|
return extractPlaintext(filePath);
|
|
1678
1934
|
}
|
|
1679
1935
|
|
|
1680
|
-
/**
|
|
1681
|
-
* @module rules/templates
|
|
1682
|
-
* Resolves template variables (`${path.to.value}`) in rule `set` objects against file attributes.
|
|
1683
|
-
*/
|
|
1684
|
-
/**
|
|
1685
|
-
* Resolve `${template.vars}` in a value against the given attributes.
|
|
1686
|
-
*
|
|
1687
|
-
* @param value - The value to resolve.
|
|
1688
|
-
* @param attributes - The file attributes for variable lookup.
|
|
1689
|
-
* @returns The resolved value.
|
|
1690
|
-
*/
|
|
1691
|
-
function resolveTemplateVars(value, attributes) {
|
|
1692
|
-
if (typeof value !== 'string')
|
|
1693
|
-
return value;
|
|
1694
|
-
return value.replace(/\$\{([^}]+)\}/g, (_match, varPath) => {
|
|
1695
|
-
const current = get(attributes, varPath);
|
|
1696
|
-
if (current === null || current === undefined)
|
|
1697
|
-
return '';
|
|
1698
|
-
return typeof current === 'string' ? current : JSON.stringify(current);
|
|
1699
|
-
});
|
|
1700
|
-
}
|
|
1701
|
-
/**
|
|
1702
|
-
* Resolve all template variables in a `set` object.
|
|
1703
|
-
*
|
|
1704
|
-
* @param setObj - The key-value pairs to resolve.
|
|
1705
|
-
* @param attributes - The file attributes for variable lookup.
|
|
1706
|
-
* @returns The resolved key-value pairs.
|
|
1707
|
-
*/
|
|
1708
|
-
function resolveSet(setObj, attributes) {
|
|
1709
|
-
const result = {};
|
|
1710
|
-
for (const [key, value] of Object.entries(setObj)) {
|
|
1711
|
-
result[key] = resolveTemplateVars(value, attributes);
|
|
1712
|
-
}
|
|
1713
|
-
return result;
|
|
1714
|
-
}
|
|
1715
|
-
|
|
1716
|
-
/**
|
|
1717
|
-
* @module rules/apply
|
|
1718
|
-
* Applies compiled inference rules to file attributes, producing merged metadata via template resolution and JsonMap transforms.
|
|
1719
|
-
*/
|
|
1720
|
-
/**
|
|
1721
|
-
* Create the lib object for JsonMap transformations.
|
|
1722
|
-
*
|
|
1723
|
-
* @returns The lib object.
|
|
1724
|
-
*/
|
|
1725
|
-
function createJsonMapLib() {
|
|
1726
|
-
return {
|
|
1727
|
-
split: (str, separator) => str.split(separator),
|
|
1728
|
-
slice: (arr, start, end) => arr.slice(start, end),
|
|
1729
|
-
join: (arr, separator) => arr.join(separator),
|
|
1730
|
-
toLowerCase: (str) => str.toLowerCase(),
|
|
1731
|
-
replace: (str, search, replacement) => str.replace(search, replacement),
|
|
1732
|
-
get: (obj, path) => get(obj, path),
|
|
1733
|
-
};
|
|
1734
|
-
}
|
|
1735
|
-
/**
|
|
1736
|
-
* Apply compiled inference rules to file attributes, returning merged metadata and optional rendered content.
|
|
1737
|
-
*
|
|
1738
|
-
* Rules are evaluated in order; later rules override earlier ones.
|
|
1739
|
-
* If a rule has a `map`, the JsonMap transformation is applied after `set` resolution,
|
|
1740
|
-
* and map output overrides set output on conflict.
|
|
1741
|
-
*
|
|
1742
|
-
* @param compiledRules - The compiled rules to evaluate.
|
|
1743
|
-
* @param attributes - The file attributes to match against.
|
|
1744
|
-
* @param namedMaps - Optional record of named JsonMap definitions.
|
|
1745
|
-
* @param logger - Optional logger for warnings (falls back to console.warn).
|
|
1746
|
-
* @param templateEngine - Optional template engine for rendering content templates.
|
|
1747
|
-
* @param configDir - Optional config directory for resolving .json map file paths.
|
|
1748
|
-
* @returns The merged metadata and optional rendered content.
|
|
1749
|
-
*/
|
|
1750
|
-
async function applyRules(compiledRules, attributes, namedMaps, logger, templateEngine, configDir) {
|
|
1751
|
-
// JsonMap's type definitions expect a generic JsonMapLib shape with unary functions.
|
|
1752
|
-
// Our helper functions accept multiple args, which JsonMap supports at runtime.
|
|
1753
|
-
const lib = createJsonMapLib();
|
|
1754
|
-
let merged = {};
|
|
1755
|
-
let renderedContent = null;
|
|
1756
|
-
const log = logger ?? console;
|
|
1757
|
-
for (const [ruleIndex, { rule, validate }] of compiledRules.entries()) {
|
|
1758
|
-
if (validate(attributes)) {
|
|
1759
|
-
// Apply set resolution
|
|
1760
|
-
const setOutput = resolveSet(rule.set, attributes);
|
|
1761
|
-
merged = { ...merged, ...setOutput };
|
|
1762
|
-
// Apply map transformation if present
|
|
1763
|
-
if (rule.map) {
|
|
1764
|
-
let mapDef;
|
|
1765
|
-
// Resolve map reference
|
|
1766
|
-
if (typeof rule.map === 'string') {
|
|
1767
|
-
if (rule.map.endsWith('.json') && configDir) {
|
|
1768
|
-
// File path: load from .json file
|
|
1769
|
-
try {
|
|
1770
|
-
const mapPath = resolve(configDir, rule.map);
|
|
1771
|
-
const raw = readFileSync(mapPath, 'utf-8');
|
|
1772
|
-
mapDef = JSON.parse(raw);
|
|
1773
|
-
}
|
|
1774
|
-
catch (error) {
|
|
1775
|
-
log.warn(`Failed to load map file "${rule.map}": ${error instanceof Error ? error.message : String(error)}`);
|
|
1776
|
-
continue;
|
|
1777
|
-
}
|
|
1778
|
-
}
|
|
1779
|
-
else {
|
|
1780
|
-
mapDef = namedMaps?.[rule.map];
|
|
1781
|
-
if (!mapDef) {
|
|
1782
|
-
log.warn(`Map reference "${rule.map}" not found in named maps. Skipping map transformation.`);
|
|
1783
|
-
continue;
|
|
1784
|
-
}
|
|
1785
|
-
}
|
|
1786
|
-
}
|
|
1787
|
-
else {
|
|
1788
|
-
mapDef = rule.map;
|
|
1789
|
-
}
|
|
1790
|
-
// Execute JsonMap transformation
|
|
1791
|
-
try {
|
|
1792
|
-
const jsonMap = new JsonMap(mapDef, lib);
|
|
1793
|
-
const mapOutput = await jsonMap.transform(attributes);
|
|
1794
|
-
if (mapOutput &&
|
|
1795
|
-
typeof mapOutput === 'object' &&
|
|
1796
|
-
!Array.isArray(mapOutput)) {
|
|
1797
|
-
merged = { ...merged, ...mapOutput };
|
|
1798
|
-
}
|
|
1799
|
-
else {
|
|
1800
|
-
log.warn(`JsonMap transformation did not return an object; skipping merge.`);
|
|
1801
|
-
}
|
|
1802
|
-
}
|
|
1803
|
-
catch (error) {
|
|
1804
|
-
log.warn(`JsonMap transformation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
1805
|
-
}
|
|
1806
|
-
}
|
|
1807
|
-
// Render template if present
|
|
1808
|
-
if (rule.template && templateEngine) {
|
|
1809
|
-
const templateKey = `rule-${String(ruleIndex)}`;
|
|
1810
|
-
// Build template context: attributes (with json spread at top) + map output
|
|
1811
|
-
const context = {
|
|
1812
|
-
...(attributes.json ?? {}),
|
|
1813
|
-
...attributes,
|
|
1814
|
-
...merged,
|
|
1815
|
-
};
|
|
1816
|
-
try {
|
|
1817
|
-
const result = templateEngine.render(templateKey, context);
|
|
1818
|
-
if (result && result.trim()) {
|
|
1819
|
-
renderedContent = result;
|
|
1820
|
-
}
|
|
1821
|
-
else {
|
|
1822
|
-
log.warn(`Template for rule ${String(ruleIndex)} rendered empty output. Falling back to raw content.`);
|
|
1823
|
-
}
|
|
1824
|
-
}
|
|
1825
|
-
catch (error) {
|
|
1826
|
-
log.warn(`Template render failed for rule ${String(ruleIndex)}: ${error instanceof Error ? error.message : String(error)}. Falling back to raw content.`);
|
|
1827
|
-
}
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
}
|
|
1831
|
-
return { metadata: merged, renderedContent };
|
|
1832
|
-
}
|
|
1833
|
-
|
|
1834
1936
|
/**
|
|
1835
1937
|
* @module rules/attributes
|
|
1836
1938
|
* Builds file attribute objects for rule matching. Pure function: derives attributes from path, stats, and extracted data.
|
|
@@ -1921,14 +2023,14 @@ function compileRules(rules) {
|
|
|
1921
2023
|
* @param configDir - Optional config directory for resolving file paths.
|
|
1922
2024
|
* @returns The merged metadata and intermediate data.
|
|
1923
2025
|
*/
|
|
1924
|
-
async function buildMergedMetadata(filePath, compiledRules, metadataDir, maps, logger, templateEngine, configDir) {
|
|
2026
|
+
async function buildMergedMetadata(filePath, compiledRules, metadataDir, maps, logger, templateEngine, configDir, customMapLib) {
|
|
1925
2027
|
const ext = extname(filePath);
|
|
1926
2028
|
const stats = await stat(filePath);
|
|
1927
2029
|
// 1. Extract text and structured data
|
|
1928
2030
|
const extracted = await extractText(filePath, ext);
|
|
1929
2031
|
// 2. Build attributes + apply rules
|
|
1930
2032
|
const attributes = buildAttributes(filePath, stats, extracted.frontmatter, extracted.json);
|
|
1931
|
-
const { metadata: inferred, renderedContent } = await applyRules(compiledRules, attributes, maps, logger, templateEngine, configDir);
|
|
2033
|
+
const { metadata: inferred, renderedContent } = await applyRules(compiledRules, attributes, maps, logger, templateEngine, configDir, customMapLib);
|
|
1932
2034
|
// 3. Read enrichment metadata (merge, enrichment wins)
|
|
1933
2035
|
const enrichment = await readMetadata(filePath, metadataDir);
|
|
1934
2036
|
const metadata = {
|
|
@@ -2041,7 +2143,7 @@ class DocumentProcessor {
|
|
|
2041
2143
|
try {
|
|
2042
2144
|
const ext = extname(filePath);
|
|
2043
2145
|
// 1. Build merged metadata + extract text
|
|
2044
|
-
const { metadata, extracted, renderedContent } = await buildMergedMetadata(filePath, this.compiledRules, this.config.metadataDir, this.config.maps, this.logger, this.templateEngine, this.config.configDir);
|
|
2146
|
+
const { metadata, extracted, renderedContent } = await buildMergedMetadata(filePath, this.compiledRules, this.config.metadataDir, this.config.maps, this.logger, this.templateEngine, this.config.configDir, this.config.customMapLib);
|
|
2045
2147
|
// Use rendered template content if available, otherwise raw extracted text
|
|
2046
2148
|
const textToEmbed = renderedContent ?? extracted.text;
|
|
2047
2149
|
if (!textToEmbed.trim()) {
|
|
@@ -2155,7 +2257,7 @@ class DocumentProcessor {
|
|
|
2155
2257
|
return null;
|
|
2156
2258
|
}
|
|
2157
2259
|
// Build merged metadata (lightweight — no embedding)
|
|
2158
|
-
const { metadata } = await buildMergedMetadata(filePath, this.compiledRules, this.config.metadataDir, this.config.maps, this.logger, this.templateEngine, this.config.configDir);
|
|
2260
|
+
const { metadata } = await buildMergedMetadata(filePath, this.compiledRules, this.config.metadataDir, this.config.maps, this.logger, this.templateEngine, this.config.configDir, this.config.customMapLib);
|
|
2159
2261
|
// Update all chunk payloads
|
|
2160
2262
|
const totalChunks = getChunkCount(existingPayload);
|
|
2161
2263
|
const ids = chunkIds(filePath, totalChunks);
|
|
@@ -2169,21 +2271,20 @@ class DocumentProcessor {
|
|
|
2169
2271
|
}
|
|
2170
2272
|
}
|
|
2171
2273
|
/**
|
|
2172
|
-
* Update compiled inference rules
|
|
2173
|
-
*
|
|
2174
|
-
* @param compiledRules - The newly compiled rules.
|
|
2175
|
-
*/
|
|
2176
|
-
/**
|
|
2177
|
-
* Update compiled inference rules and optionally the template engine.
|
|
2274
|
+
* Update compiled inference rules, template engine, and custom map lib.
|
|
2178
2275
|
*
|
|
2179
2276
|
* @param compiledRules - The newly compiled rules.
|
|
2180
2277
|
* @param templateEngine - Optional updated template engine.
|
|
2278
|
+
* @param customMapLib - Optional updated custom JsonMap lib functions.
|
|
2181
2279
|
*/
|
|
2182
|
-
updateRules(compiledRules, templateEngine) {
|
|
2280
|
+
updateRules(compiledRules, templateEngine, customMapLib) {
|
|
2183
2281
|
this.compiledRules = compiledRules;
|
|
2184
2282
|
if (templateEngine) {
|
|
2185
2283
|
this.templateEngine = templateEngine;
|
|
2186
2284
|
}
|
|
2285
|
+
if (customMapLib !== undefined) {
|
|
2286
|
+
this.config = { ...this.config, customMapLib };
|
|
2287
|
+
}
|
|
2187
2288
|
this.logger.info({ rules: compiledRules.length }, 'Inference rules updated');
|
|
2188
2289
|
}
|
|
2189
2290
|
}
|
|
@@ -3063,12 +3164,17 @@ class JeevesWatcher {
|
|
|
3063
3164
|
const compiledRules = this.factories.compileRules(this.config.inferenceRules ?? []);
|
|
3064
3165
|
const configDir = this.configPath ? dirname(this.configPath) : '.';
|
|
3065
3166
|
const templateEngine = await buildTemplateEngine(this.config.inferenceRules ?? [], this.config.templates, this.config.templateHelpers?.paths, configDir);
|
|
3167
|
+
// Load custom JsonMap lib functions
|
|
3168
|
+
const customMapLib = this.config.mapHelpers?.paths?.length && configDir
|
|
3169
|
+
? await loadCustomMapHelpers(this.config.mapHelpers.paths, configDir)
|
|
3170
|
+
: undefined;
|
|
3066
3171
|
const processor = this.factories.createDocumentProcessor({
|
|
3067
3172
|
metadataDir: this.config.metadataDir ?? '.jeeves-metadata',
|
|
3068
3173
|
chunkSize: this.config.embedding.chunkSize,
|
|
3069
3174
|
chunkOverlap: this.config.embedding.chunkOverlap,
|
|
3070
3175
|
maps: this.config.maps,
|
|
3071
3176
|
configDir,
|
|
3177
|
+
customMapLib,
|
|
3072
3178
|
}, embeddingProvider, vectorStore, compiledRules, logger, templateEngine);
|
|
3073
3179
|
this.processor = processor;
|
|
3074
3180
|
this.queue = this.factories.createEventQueue({
|
|
@@ -3187,7 +3293,10 @@ class JeevesWatcher {
|
|
|
3187
3293
|
const compiledRules = this.factories.compileRules(newConfig.inferenceRules ?? []);
|
|
3188
3294
|
const reloadConfigDir = dirname(this.configPath);
|
|
3189
3295
|
const newTemplateEngine = await buildTemplateEngine(newConfig.inferenceRules ?? [], newConfig.templates, newConfig.templateHelpers?.paths, reloadConfigDir);
|
|
3190
|
-
|
|
3296
|
+
const newCustomMapLib = newConfig.mapHelpers?.paths?.length && reloadConfigDir
|
|
3297
|
+
? await loadCustomMapHelpers(newConfig.mapHelpers.paths, reloadConfigDir)
|
|
3298
|
+
: undefined;
|
|
3299
|
+
processor.updateRules(compiledRules, newTemplateEngine, newCustomMapLib);
|
|
3191
3300
|
logger.info({ configPath: this.configPath, rules: compiledRules.length }, 'Config reloaded');
|
|
3192
3301
|
}
|
|
3193
3302
|
catch (error) {
|
package/dist/index.d.ts
CHANGED
|
@@ -132,6 +132,9 @@ declare const jeevesWatcherConfigSchema: z.ZodObject<{
|
|
|
132
132
|
templateHelpers: z.ZodOptional<z.ZodObject<{
|
|
133
133
|
paths: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
134
134
|
}, z.core.$strip>>;
|
|
135
|
+
mapHelpers: z.ZodOptional<z.ZodObject<{
|
|
136
|
+
paths: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
137
|
+
}, z.core.$strip>>;
|
|
135
138
|
logging: z.ZodOptional<z.ZodObject<{
|
|
136
139
|
level: z.ZodOptional<z.ZodString>;
|
|
137
140
|
file: z.ZodOptional<z.ZodString>;
|
|
@@ -366,7 +369,7 @@ interface ApplyRulesResult {
|
|
|
366
369
|
* @param configDir - Optional config directory for resolving .json map file paths.
|
|
367
370
|
* @returns The merged metadata and optional rendered content.
|
|
368
371
|
*/
|
|
369
|
-
declare function applyRules(compiledRules: CompiledRule[], attributes: FileAttributes, namedMaps?: Record<string, JsonMapMap>, logger?: RuleLogger, templateEngine?: TemplateEngine, configDir?: string): Promise<ApplyRulesResult>;
|
|
372
|
+
declare function applyRules(compiledRules: CompiledRule[], attributes: FileAttributes, namedMaps?: Record<string, JsonMapMap>, logger?: RuleLogger, templateEngine?: TemplateEngine, configDir?: string, customMapLib?: Record<string, (...args: unknown[]) => unknown>): Promise<ApplyRulesResult>;
|
|
370
373
|
|
|
371
374
|
/**
|
|
372
375
|
* A point to upsert into the vector store.
|
|
@@ -516,6 +519,8 @@ interface ProcessorConfig {
|
|
|
516
519
|
maps?: Record<string, JsonMapMap>;
|
|
517
520
|
/** Config directory for resolving relative file paths. */
|
|
518
521
|
configDir?: string;
|
|
522
|
+
/** Custom JsonMap lib functions loaded from mapHelpers config. */
|
|
523
|
+
customMapLib?: Record<string, (...args: unknown[]) => unknown>;
|
|
519
524
|
}
|
|
520
525
|
/**
|
|
521
526
|
* Core document processing pipeline.
|
|
@@ -523,7 +528,7 @@ interface ProcessorConfig {
|
|
|
523
528
|
* Handles extracting text, computing embeddings, and syncing with the vector store.
|
|
524
529
|
*/
|
|
525
530
|
declare class DocumentProcessor {
|
|
526
|
-
private
|
|
531
|
+
private config;
|
|
527
532
|
private readonly embeddingProvider;
|
|
528
533
|
private readonly vectorStore;
|
|
529
534
|
private compiledRules;
|
|
@@ -570,17 +575,13 @@ declare class DocumentProcessor {
|
|
|
570
575
|
*/
|
|
571
576
|
processRulesUpdate(filePath: string): Promise<Record<string, unknown> | null>;
|
|
572
577
|
/**
|
|
573
|
-
* Update compiled inference rules
|
|
574
|
-
*
|
|
575
|
-
* @param compiledRules - The newly compiled rules.
|
|
576
|
-
*/
|
|
577
|
-
/**
|
|
578
|
-
* Update compiled inference rules and optionally the template engine.
|
|
578
|
+
* Update compiled inference rules, template engine, and custom map lib.
|
|
579
579
|
*
|
|
580
580
|
* @param compiledRules - The newly compiled rules.
|
|
581
581
|
* @param templateEngine - Optional updated template engine.
|
|
582
|
+
* @param customMapLib - Optional updated custom JsonMap lib functions.
|
|
582
583
|
*/
|
|
583
|
-
updateRules(compiledRules: CompiledRule[], templateEngine?: TemplateEngine): void;
|
|
584
|
+
updateRules(compiledRules: CompiledRule[], templateEngine?: TemplateEngine, customMapLib?: Record<string, (...args: unknown[]) => unknown>): void;
|
|
584
585
|
}
|
|
585
586
|
|
|
586
587
|
/**
|