@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
package/dist/mjs/index.js
CHANGED
|
@@ -2,10 +2,11 @@ import Fastify from 'fastify';
|
|
|
2
2
|
import { readdir, stat, rm, readFile, mkdir, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { resolve, dirname, join, relative, extname, basename } from 'node:path';
|
|
4
4
|
import picomatch from 'picomatch';
|
|
5
|
-
import { omit, capitalize, title, camel, snake, dash, isEqual
|
|
5
|
+
import { omit, get, capitalize, title, camel, snake, dash, isEqual } from 'radash';
|
|
6
6
|
import { createHash } from 'node:crypto';
|
|
7
7
|
import { existsSync, statSync, readdirSync, readFileSync } from 'node:fs';
|
|
8
8
|
import ignore from 'ignore';
|
|
9
|
+
import { JsonMap, jsonMapMapSchema } from '@karmaniverous/jsonmap';
|
|
9
10
|
import Handlebars from 'handlebars';
|
|
10
11
|
import dayjs from 'dayjs';
|
|
11
12
|
import { toMdast } from 'hast-util-to-mdast';
|
|
@@ -16,7 +17,6 @@ import { unified } from 'unified';
|
|
|
16
17
|
import chokidar from 'chokidar';
|
|
17
18
|
import { cosmiconfig } from 'cosmiconfig';
|
|
18
19
|
import { z, ZodError } from 'zod';
|
|
19
|
-
import { jsonMapMapSchema, JsonMap } from '@karmaniverous/jsonmap';
|
|
20
20
|
import { GoogleGenerativeAIEmbeddings } from '@langchain/google-genai';
|
|
21
21
|
import pino from 'pino';
|
|
22
22
|
import { v5 } from 'uuid';
|
|
@@ -636,6 +636,251 @@ class GitignoreFilter {
|
|
|
636
636
|
}
|
|
637
637
|
}
|
|
638
638
|
|
|
639
|
+
/**
|
|
640
|
+
* @module rules/templates
|
|
641
|
+
* Resolves template variables (`${path.to.value}`) in rule `set` objects against file attributes.
|
|
642
|
+
*/
|
|
643
|
+
/**
|
|
644
|
+
* Resolve `${template.vars}` in a value against the given attributes.
|
|
645
|
+
*
|
|
646
|
+
* @param value - The value to resolve.
|
|
647
|
+
* @param attributes - The file attributes for variable lookup.
|
|
648
|
+
* @returns The resolved value.
|
|
649
|
+
*/
|
|
650
|
+
function resolveTemplateVars(value, attributes) {
|
|
651
|
+
if (typeof value !== 'string')
|
|
652
|
+
return value;
|
|
653
|
+
return value.replace(/\$\{([^}]+)\}/g, (_match, varPath) => {
|
|
654
|
+
const current = get(attributes, varPath);
|
|
655
|
+
if (current === null || current === undefined)
|
|
656
|
+
return '';
|
|
657
|
+
return typeof current === 'string' ? current : JSON.stringify(current);
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Resolve all template variables in a `set` object.
|
|
662
|
+
*
|
|
663
|
+
* @param setObj - The key-value pairs to resolve.
|
|
664
|
+
* @param attributes - The file attributes for variable lookup.
|
|
665
|
+
* @returns The resolved key-value pairs.
|
|
666
|
+
*/
|
|
667
|
+
function resolveSet(setObj, attributes) {
|
|
668
|
+
const result = {};
|
|
669
|
+
for (const [key, value] of Object.entries(setObj)) {
|
|
670
|
+
result[key] = resolveTemplateVars(value, attributes);
|
|
671
|
+
}
|
|
672
|
+
return result;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* @module rules/apply
|
|
677
|
+
* Applies compiled inference rules to file attributes, producing merged metadata via template resolution and JsonMap transforms.
|
|
678
|
+
*/
|
|
679
|
+
/**
|
|
680
|
+
* Create the lib object for JsonMap transformations.
|
|
681
|
+
*
|
|
682
|
+
* @param configDir - Optional config directory for resolving relative file paths in lookups.
|
|
683
|
+
* @returns The lib object.
|
|
684
|
+
*/
|
|
685
|
+
function createJsonMapLib(configDir, customLib) {
|
|
686
|
+
// Cache loaded JSON files within a single applyRules invocation.
|
|
687
|
+
const jsonCache = new Map();
|
|
688
|
+
const loadJson = (filePath) => {
|
|
689
|
+
const resolvedPath = configDir ? resolve(configDir, filePath) : filePath;
|
|
690
|
+
if (!jsonCache.has(resolvedPath)) {
|
|
691
|
+
const raw = readFileSync(resolvedPath, 'utf-8');
|
|
692
|
+
jsonCache.set(resolvedPath, JSON.parse(raw));
|
|
693
|
+
}
|
|
694
|
+
return jsonCache.get(resolvedPath);
|
|
695
|
+
};
|
|
696
|
+
return {
|
|
697
|
+
split: (str, separator) => str.split(separator),
|
|
698
|
+
slice: (arr, start, end) => arr.slice(start, end),
|
|
699
|
+
join: (arr, separator) => arr.join(separator),
|
|
700
|
+
toLowerCase: (str) => str.toLowerCase(),
|
|
701
|
+
replace: (str, search, replacement) => str.replace(search, replacement),
|
|
702
|
+
get: (obj, path) => get(obj, path),
|
|
703
|
+
/**
|
|
704
|
+
* Load a JSON file (relative to configDir) and look up a value by key,
|
|
705
|
+
* optionally drilling into a sub-path.
|
|
706
|
+
*
|
|
707
|
+
* @param filePath - Path to a JSON file (resolved relative to configDir).
|
|
708
|
+
* @param key - Top-level key to look up.
|
|
709
|
+
* @param field - Optional dot-path into the looked-up entry.
|
|
710
|
+
* @returns The resolved value, or null if not found.
|
|
711
|
+
*/
|
|
712
|
+
lookupJson: (filePath, key, field) => {
|
|
713
|
+
const data = loadJson(filePath);
|
|
714
|
+
const entry = data[key];
|
|
715
|
+
if (entry === undefined || entry === null)
|
|
716
|
+
return null;
|
|
717
|
+
if (field)
|
|
718
|
+
return get(entry, field) ?? null;
|
|
719
|
+
return entry;
|
|
720
|
+
},
|
|
721
|
+
/**
|
|
722
|
+
* Map an array of keys through a JSON lookup file, collecting a specific
|
|
723
|
+
* field from each matching entry. Non-matching keys are silently skipped.
|
|
724
|
+
* Array-valued fields are flattened into the result.
|
|
725
|
+
*
|
|
726
|
+
* @param filePath - Path to a JSON file (resolved relative to configDir).
|
|
727
|
+
* @param keys - Array of top-level keys to look up.
|
|
728
|
+
* @param field - Dot-path into each looked-up entry.
|
|
729
|
+
* @returns Flat array of resolved values.
|
|
730
|
+
*/
|
|
731
|
+
mapLookup: (filePath, keys, field) => {
|
|
732
|
+
if (!Array.isArray(keys))
|
|
733
|
+
return [];
|
|
734
|
+
const data = loadJson(filePath);
|
|
735
|
+
const results = [];
|
|
736
|
+
for (const k of keys) {
|
|
737
|
+
if (typeof k !== 'string')
|
|
738
|
+
continue;
|
|
739
|
+
const entry = data[k];
|
|
740
|
+
if (entry === undefined || entry === null)
|
|
741
|
+
continue;
|
|
742
|
+
const val = get(entry, field);
|
|
743
|
+
if (val === undefined || val === null)
|
|
744
|
+
continue;
|
|
745
|
+
if (Array.isArray(val)) {
|
|
746
|
+
for (const item of val) {
|
|
747
|
+
results.push(item);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
else {
|
|
751
|
+
results.push(val);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return results;
|
|
755
|
+
},
|
|
756
|
+
...customLib,
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Load custom JsonMap lib functions from file paths.
|
|
761
|
+
*
|
|
762
|
+
* Each module should default-export an object of functions,
|
|
763
|
+
* or use named exports. Only function-valued exports are merged.
|
|
764
|
+
*
|
|
765
|
+
* @param paths - File paths to custom lib modules.
|
|
766
|
+
* @param configDir - Directory to resolve relative paths against.
|
|
767
|
+
* @returns The merged custom lib functions.
|
|
768
|
+
*/
|
|
769
|
+
async function loadCustomMapHelpers(paths, configDir) {
|
|
770
|
+
const merged = {};
|
|
771
|
+
for (const p of paths) {
|
|
772
|
+
const resolved = resolve(configDir, p);
|
|
773
|
+
const mod = (await import(resolved));
|
|
774
|
+
const fns = typeof mod.default === 'object' && mod.default !== null
|
|
775
|
+
? mod.default
|
|
776
|
+
: mod;
|
|
777
|
+
for (const [key, val] of Object.entries(fns)) {
|
|
778
|
+
if (typeof val === 'function') {
|
|
779
|
+
merged[key] = val;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
return merged;
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Apply compiled inference rules to file attributes, returning merged metadata and optional rendered content.
|
|
787
|
+
*
|
|
788
|
+
* Rules are evaluated in order; later rules override earlier ones.
|
|
789
|
+
* If a rule has a `map`, the JsonMap transformation is applied after `set` resolution,
|
|
790
|
+
* and map output overrides set output on conflict.
|
|
791
|
+
*
|
|
792
|
+
* @param compiledRules - The compiled rules to evaluate.
|
|
793
|
+
* @param attributes - The file attributes to match against.
|
|
794
|
+
* @param namedMaps - Optional record of named JsonMap definitions.
|
|
795
|
+
* @param logger - Optional logger for warnings (falls back to console.warn).
|
|
796
|
+
* @param templateEngine - Optional template engine for rendering content templates.
|
|
797
|
+
* @param configDir - Optional config directory for resolving .json map file paths.
|
|
798
|
+
* @returns The merged metadata and optional rendered content.
|
|
799
|
+
*/
|
|
800
|
+
async function applyRules(compiledRules, attributes, namedMaps, logger, templateEngine, configDir, customMapLib) {
|
|
801
|
+
// JsonMap's type definitions expect a generic JsonMapLib shape with unary functions.
|
|
802
|
+
// Our helper functions accept multiple args, which JsonMap supports at runtime.
|
|
803
|
+
const lib = createJsonMapLib(configDir, customMapLib);
|
|
804
|
+
let merged = {};
|
|
805
|
+
let renderedContent = null;
|
|
806
|
+
const log = logger ?? console;
|
|
807
|
+
for (const [ruleIndex, { rule, validate }] of compiledRules.entries()) {
|
|
808
|
+
if (validate(attributes)) {
|
|
809
|
+
// Apply set resolution
|
|
810
|
+
const setOutput = resolveSet(rule.set, attributes);
|
|
811
|
+
merged = { ...merged, ...setOutput };
|
|
812
|
+
// Apply map transformation if present
|
|
813
|
+
if (rule.map) {
|
|
814
|
+
let mapDef;
|
|
815
|
+
// Resolve map reference
|
|
816
|
+
if (typeof rule.map === 'string') {
|
|
817
|
+
if (rule.map.endsWith('.json') && configDir) {
|
|
818
|
+
// File path: load from .json file
|
|
819
|
+
try {
|
|
820
|
+
const mapPath = resolve(configDir, rule.map);
|
|
821
|
+
const raw = readFileSync(mapPath, 'utf-8');
|
|
822
|
+
mapDef = JSON.parse(raw);
|
|
823
|
+
}
|
|
824
|
+
catch (error) {
|
|
825
|
+
log.warn(`Failed to load map file "${rule.map}": ${error instanceof Error ? error.message : String(error)}`);
|
|
826
|
+
continue;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
else {
|
|
830
|
+
mapDef = namedMaps?.[rule.map];
|
|
831
|
+
if (!mapDef) {
|
|
832
|
+
log.warn(`Map reference "${rule.map}" not found in named maps. Skipping map transformation.`);
|
|
833
|
+
continue;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
else {
|
|
838
|
+
mapDef = rule.map;
|
|
839
|
+
}
|
|
840
|
+
// Execute JsonMap transformation
|
|
841
|
+
try {
|
|
842
|
+
const jsonMap = new JsonMap(mapDef, lib);
|
|
843
|
+
const mapOutput = await jsonMap.transform(attributes);
|
|
844
|
+
if (mapOutput &&
|
|
845
|
+
typeof mapOutput === 'object' &&
|
|
846
|
+
!Array.isArray(mapOutput)) {
|
|
847
|
+
merged = { ...merged, ...mapOutput };
|
|
848
|
+
}
|
|
849
|
+
else {
|
|
850
|
+
log.warn(`JsonMap transformation did not return an object; skipping merge.`);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
catch (error) {
|
|
854
|
+
log.warn(`JsonMap transformation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
// Render template if present
|
|
858
|
+
if (rule.template && templateEngine) {
|
|
859
|
+
const templateKey = `rule-${String(ruleIndex)}`;
|
|
860
|
+
// Build template context: attributes (with json spread at top) + map output
|
|
861
|
+
const context = {
|
|
862
|
+
...(attributes.json ?? {}),
|
|
863
|
+
...attributes,
|
|
864
|
+
...merged,
|
|
865
|
+
};
|
|
866
|
+
try {
|
|
867
|
+
const result = templateEngine.render(templateKey, context);
|
|
868
|
+
if (result && result.trim()) {
|
|
869
|
+
renderedContent = result;
|
|
870
|
+
}
|
|
871
|
+
else {
|
|
872
|
+
log.warn(`Template for rule ${String(ruleIndex)} rendered empty output. Falling back to raw content.`);
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
catch (error) {
|
|
876
|
+
log.warn(`Template render failed for rule ${String(ruleIndex)}: ${error instanceof Error ? error.message : String(error)}. Falling back to raw content.`);
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
return { metadata: merged, renderedContent };
|
|
882
|
+
}
|
|
883
|
+
|
|
639
884
|
/**
|
|
640
885
|
* @module templates/helpers
|
|
641
886
|
* Registers built-in Handlebars helpers for content templates.
|
|
@@ -1164,6 +1409,17 @@ const jeevesWatcherConfigSchema = z.object({
|
|
|
1164
1409
|
})
|
|
1165
1410
|
.optional()
|
|
1166
1411
|
.describe('Custom Handlebars helper registration.'),
|
|
1412
|
+
/** Custom JsonMap lib function registration. */
|
|
1413
|
+
mapHelpers: z
|
|
1414
|
+
.object({
|
|
1415
|
+
/** File paths to custom lib modules (each exports functions to merge into the JsonMap lib). */
|
|
1416
|
+
paths: z
|
|
1417
|
+
.array(z.string())
|
|
1418
|
+
.optional()
|
|
1419
|
+
.describe('File paths to JS modules exporting functions to merge into the JsonMap lib.'),
|
|
1420
|
+
})
|
|
1421
|
+
.optional()
|
|
1422
|
+
.describe('Custom JsonMap lib function registration.'),
|
|
1167
1423
|
/** Logging configuration. */
|
|
1168
1424
|
logging: loggingConfigSchema.optional().describe('Logging configuration.'),
|
|
1169
1425
|
/** Timeout in milliseconds for graceful shutdown. */
|
|
@@ -1654,160 +1910,6 @@ async function extractText(filePath, extension) {
|
|
|
1654
1910
|
return extractPlaintext(filePath);
|
|
1655
1911
|
}
|
|
1656
1912
|
|
|
1657
|
-
/**
|
|
1658
|
-
* @module rules/templates
|
|
1659
|
-
* Resolves template variables (`${path.to.value}`) in rule `set` objects against file attributes.
|
|
1660
|
-
*/
|
|
1661
|
-
/**
|
|
1662
|
-
* Resolve `${template.vars}` in a value against the given attributes.
|
|
1663
|
-
*
|
|
1664
|
-
* @param value - The value to resolve.
|
|
1665
|
-
* @param attributes - The file attributes for variable lookup.
|
|
1666
|
-
* @returns The resolved value.
|
|
1667
|
-
*/
|
|
1668
|
-
function resolveTemplateVars(value, attributes) {
|
|
1669
|
-
if (typeof value !== 'string')
|
|
1670
|
-
return value;
|
|
1671
|
-
return value.replace(/\$\{([^}]+)\}/g, (_match, varPath) => {
|
|
1672
|
-
const current = get(attributes, varPath);
|
|
1673
|
-
if (current === null || current === undefined)
|
|
1674
|
-
return '';
|
|
1675
|
-
return typeof current === 'string' ? current : JSON.stringify(current);
|
|
1676
|
-
});
|
|
1677
|
-
}
|
|
1678
|
-
/**
|
|
1679
|
-
* Resolve all template variables in a `set` object.
|
|
1680
|
-
*
|
|
1681
|
-
* @param setObj - The key-value pairs to resolve.
|
|
1682
|
-
* @param attributes - The file attributes for variable lookup.
|
|
1683
|
-
* @returns The resolved key-value pairs.
|
|
1684
|
-
*/
|
|
1685
|
-
function resolveSet(setObj, attributes) {
|
|
1686
|
-
const result = {};
|
|
1687
|
-
for (const [key, value] of Object.entries(setObj)) {
|
|
1688
|
-
result[key] = resolveTemplateVars(value, attributes);
|
|
1689
|
-
}
|
|
1690
|
-
return result;
|
|
1691
|
-
}
|
|
1692
|
-
|
|
1693
|
-
/**
|
|
1694
|
-
* @module rules/apply
|
|
1695
|
-
* Applies compiled inference rules to file attributes, producing merged metadata via template resolution and JsonMap transforms.
|
|
1696
|
-
*/
|
|
1697
|
-
/**
|
|
1698
|
-
* Create the lib object for JsonMap transformations.
|
|
1699
|
-
*
|
|
1700
|
-
* @returns The lib object.
|
|
1701
|
-
*/
|
|
1702
|
-
function createJsonMapLib() {
|
|
1703
|
-
return {
|
|
1704
|
-
split: (str, separator) => str.split(separator),
|
|
1705
|
-
slice: (arr, start, end) => arr.slice(start, end),
|
|
1706
|
-
join: (arr, separator) => arr.join(separator),
|
|
1707
|
-
toLowerCase: (str) => str.toLowerCase(),
|
|
1708
|
-
replace: (str, search, replacement) => str.replace(search, replacement),
|
|
1709
|
-
get: (obj, path) => get(obj, path),
|
|
1710
|
-
};
|
|
1711
|
-
}
|
|
1712
|
-
/**
|
|
1713
|
-
* Apply compiled inference rules to file attributes, returning merged metadata and optional rendered content.
|
|
1714
|
-
*
|
|
1715
|
-
* Rules are evaluated in order; later rules override earlier ones.
|
|
1716
|
-
* If a rule has a `map`, the JsonMap transformation is applied after `set` resolution,
|
|
1717
|
-
* and map output overrides set output on conflict.
|
|
1718
|
-
*
|
|
1719
|
-
* @param compiledRules - The compiled rules to evaluate.
|
|
1720
|
-
* @param attributes - The file attributes to match against.
|
|
1721
|
-
* @param namedMaps - Optional record of named JsonMap definitions.
|
|
1722
|
-
* @param logger - Optional logger for warnings (falls back to console.warn).
|
|
1723
|
-
* @param templateEngine - Optional template engine for rendering content templates.
|
|
1724
|
-
* @param configDir - Optional config directory for resolving .json map file paths.
|
|
1725
|
-
* @returns The merged metadata and optional rendered content.
|
|
1726
|
-
*/
|
|
1727
|
-
async function applyRules(compiledRules, attributes, namedMaps, logger, templateEngine, configDir) {
|
|
1728
|
-
// JsonMap's type definitions expect a generic JsonMapLib shape with unary functions.
|
|
1729
|
-
// Our helper functions accept multiple args, which JsonMap supports at runtime.
|
|
1730
|
-
const lib = createJsonMapLib();
|
|
1731
|
-
let merged = {};
|
|
1732
|
-
let renderedContent = null;
|
|
1733
|
-
const log = logger ?? console;
|
|
1734
|
-
for (const [ruleIndex, { rule, validate }] of compiledRules.entries()) {
|
|
1735
|
-
if (validate(attributes)) {
|
|
1736
|
-
// Apply set resolution
|
|
1737
|
-
const setOutput = resolveSet(rule.set, attributes);
|
|
1738
|
-
merged = { ...merged, ...setOutput };
|
|
1739
|
-
// Apply map transformation if present
|
|
1740
|
-
if (rule.map) {
|
|
1741
|
-
let mapDef;
|
|
1742
|
-
// Resolve map reference
|
|
1743
|
-
if (typeof rule.map === 'string') {
|
|
1744
|
-
if (rule.map.endsWith('.json') && configDir) {
|
|
1745
|
-
// File path: load from .json file
|
|
1746
|
-
try {
|
|
1747
|
-
const mapPath = resolve(configDir, rule.map);
|
|
1748
|
-
const raw = readFileSync(mapPath, 'utf-8');
|
|
1749
|
-
mapDef = JSON.parse(raw);
|
|
1750
|
-
}
|
|
1751
|
-
catch (error) {
|
|
1752
|
-
log.warn(`Failed to load map file "${rule.map}": ${error instanceof Error ? error.message : String(error)}`);
|
|
1753
|
-
continue;
|
|
1754
|
-
}
|
|
1755
|
-
}
|
|
1756
|
-
else {
|
|
1757
|
-
mapDef = namedMaps?.[rule.map];
|
|
1758
|
-
if (!mapDef) {
|
|
1759
|
-
log.warn(`Map reference "${rule.map}" not found in named maps. Skipping map transformation.`);
|
|
1760
|
-
continue;
|
|
1761
|
-
}
|
|
1762
|
-
}
|
|
1763
|
-
}
|
|
1764
|
-
else {
|
|
1765
|
-
mapDef = rule.map;
|
|
1766
|
-
}
|
|
1767
|
-
// Execute JsonMap transformation
|
|
1768
|
-
try {
|
|
1769
|
-
const jsonMap = new JsonMap(mapDef, lib);
|
|
1770
|
-
const mapOutput = await jsonMap.transform(attributes);
|
|
1771
|
-
if (mapOutput &&
|
|
1772
|
-
typeof mapOutput === 'object' &&
|
|
1773
|
-
!Array.isArray(mapOutput)) {
|
|
1774
|
-
merged = { ...merged, ...mapOutput };
|
|
1775
|
-
}
|
|
1776
|
-
else {
|
|
1777
|
-
log.warn(`JsonMap transformation did not return an object; skipping merge.`);
|
|
1778
|
-
}
|
|
1779
|
-
}
|
|
1780
|
-
catch (error) {
|
|
1781
|
-
log.warn(`JsonMap transformation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
1782
|
-
}
|
|
1783
|
-
}
|
|
1784
|
-
// Render template if present
|
|
1785
|
-
if (rule.template && templateEngine) {
|
|
1786
|
-
const templateKey = `rule-${String(ruleIndex)}`;
|
|
1787
|
-
// Build template context: attributes (with json spread at top) + map output
|
|
1788
|
-
const context = {
|
|
1789
|
-
...(attributes.json ?? {}),
|
|
1790
|
-
...attributes,
|
|
1791
|
-
...merged,
|
|
1792
|
-
};
|
|
1793
|
-
try {
|
|
1794
|
-
const result = templateEngine.render(templateKey, context);
|
|
1795
|
-
if (result && result.trim()) {
|
|
1796
|
-
renderedContent = result;
|
|
1797
|
-
}
|
|
1798
|
-
else {
|
|
1799
|
-
log.warn(`Template for rule ${String(ruleIndex)} rendered empty output. Falling back to raw content.`);
|
|
1800
|
-
}
|
|
1801
|
-
}
|
|
1802
|
-
catch (error) {
|
|
1803
|
-
log.warn(`Template render failed for rule ${String(ruleIndex)}: ${error instanceof Error ? error.message : String(error)}. Falling back to raw content.`);
|
|
1804
|
-
}
|
|
1805
|
-
}
|
|
1806
|
-
}
|
|
1807
|
-
}
|
|
1808
|
-
return { metadata: merged, renderedContent };
|
|
1809
|
-
}
|
|
1810
|
-
|
|
1811
1913
|
/**
|
|
1812
1914
|
* @module rules/attributes
|
|
1813
1915
|
* Builds file attribute objects for rule matching. Pure function: derives attributes from path, stats, and extracted data.
|
|
@@ -1898,14 +2000,14 @@ function compileRules(rules) {
|
|
|
1898
2000
|
* @param configDir - Optional config directory for resolving file paths.
|
|
1899
2001
|
* @returns The merged metadata and intermediate data.
|
|
1900
2002
|
*/
|
|
1901
|
-
async function buildMergedMetadata(filePath, compiledRules, metadataDir, maps, logger, templateEngine, configDir) {
|
|
2003
|
+
async function buildMergedMetadata(filePath, compiledRules, metadataDir, maps, logger, templateEngine, configDir, customMapLib) {
|
|
1902
2004
|
const ext = extname(filePath);
|
|
1903
2005
|
const stats = await stat(filePath);
|
|
1904
2006
|
// 1. Extract text and structured data
|
|
1905
2007
|
const extracted = await extractText(filePath, ext);
|
|
1906
2008
|
// 2. Build attributes + apply rules
|
|
1907
2009
|
const attributes = buildAttributes(filePath, stats, extracted.frontmatter, extracted.json);
|
|
1908
|
-
const { metadata: inferred, renderedContent } = await applyRules(compiledRules, attributes, maps, logger, templateEngine, configDir);
|
|
2010
|
+
const { metadata: inferred, renderedContent } = await applyRules(compiledRules, attributes, maps, logger, templateEngine, configDir, customMapLib);
|
|
1909
2011
|
// 3. Read enrichment metadata (merge, enrichment wins)
|
|
1910
2012
|
const enrichment = await readMetadata(filePath, metadataDir);
|
|
1911
2013
|
const metadata = {
|
|
@@ -2018,7 +2120,7 @@ class DocumentProcessor {
|
|
|
2018
2120
|
try {
|
|
2019
2121
|
const ext = extname(filePath);
|
|
2020
2122
|
// 1. Build merged metadata + extract text
|
|
2021
|
-
const { metadata, extracted, renderedContent } = await buildMergedMetadata(filePath, this.compiledRules, this.config.metadataDir, this.config.maps, this.logger, this.templateEngine, this.config.configDir);
|
|
2123
|
+
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);
|
|
2022
2124
|
// Use rendered template content if available, otherwise raw extracted text
|
|
2023
2125
|
const textToEmbed = renderedContent ?? extracted.text;
|
|
2024
2126
|
if (!textToEmbed.trim()) {
|
|
@@ -2132,7 +2234,7 @@ class DocumentProcessor {
|
|
|
2132
2234
|
return null;
|
|
2133
2235
|
}
|
|
2134
2236
|
// Build merged metadata (lightweight — no embedding)
|
|
2135
|
-
const { metadata } = await buildMergedMetadata(filePath, this.compiledRules, this.config.metadataDir, this.config.maps, this.logger, this.templateEngine, this.config.configDir);
|
|
2237
|
+
const { metadata } = await buildMergedMetadata(filePath, this.compiledRules, this.config.metadataDir, this.config.maps, this.logger, this.templateEngine, this.config.configDir, this.config.customMapLib);
|
|
2136
2238
|
// Update all chunk payloads
|
|
2137
2239
|
const totalChunks = getChunkCount(existingPayload);
|
|
2138
2240
|
const ids = chunkIds(filePath, totalChunks);
|
|
@@ -2146,21 +2248,20 @@ class DocumentProcessor {
|
|
|
2146
2248
|
}
|
|
2147
2249
|
}
|
|
2148
2250
|
/**
|
|
2149
|
-
* Update compiled inference rules
|
|
2150
|
-
*
|
|
2151
|
-
* @param compiledRules - The newly compiled rules.
|
|
2152
|
-
*/
|
|
2153
|
-
/**
|
|
2154
|
-
* Update compiled inference rules and optionally the template engine.
|
|
2251
|
+
* Update compiled inference rules, template engine, and custom map lib.
|
|
2155
2252
|
*
|
|
2156
2253
|
* @param compiledRules - The newly compiled rules.
|
|
2157
2254
|
* @param templateEngine - Optional updated template engine.
|
|
2255
|
+
* @param customMapLib - Optional updated custom JsonMap lib functions.
|
|
2158
2256
|
*/
|
|
2159
|
-
updateRules(compiledRules, templateEngine) {
|
|
2257
|
+
updateRules(compiledRules, templateEngine, customMapLib) {
|
|
2160
2258
|
this.compiledRules = compiledRules;
|
|
2161
2259
|
if (templateEngine) {
|
|
2162
2260
|
this.templateEngine = templateEngine;
|
|
2163
2261
|
}
|
|
2262
|
+
if (customMapLib !== undefined) {
|
|
2263
|
+
this.config = { ...this.config, customMapLib };
|
|
2264
|
+
}
|
|
2164
2265
|
this.logger.info({ rules: compiledRules.length }, 'Inference rules updated');
|
|
2165
2266
|
}
|
|
2166
2267
|
}
|
|
@@ -3040,12 +3141,17 @@ class JeevesWatcher {
|
|
|
3040
3141
|
const compiledRules = this.factories.compileRules(this.config.inferenceRules ?? []);
|
|
3041
3142
|
const configDir = this.configPath ? dirname(this.configPath) : '.';
|
|
3042
3143
|
const templateEngine = await buildTemplateEngine(this.config.inferenceRules ?? [], this.config.templates, this.config.templateHelpers?.paths, configDir);
|
|
3144
|
+
// Load custom JsonMap lib functions
|
|
3145
|
+
const customMapLib = this.config.mapHelpers?.paths?.length && configDir
|
|
3146
|
+
? await loadCustomMapHelpers(this.config.mapHelpers.paths, configDir)
|
|
3147
|
+
: undefined;
|
|
3043
3148
|
const processor = this.factories.createDocumentProcessor({
|
|
3044
3149
|
metadataDir: this.config.metadataDir ?? '.jeeves-metadata',
|
|
3045
3150
|
chunkSize: this.config.embedding.chunkSize,
|
|
3046
3151
|
chunkOverlap: this.config.embedding.chunkOverlap,
|
|
3047
3152
|
maps: this.config.maps,
|
|
3048
3153
|
configDir,
|
|
3154
|
+
customMapLib,
|
|
3049
3155
|
}, embeddingProvider, vectorStore, compiledRules, logger, templateEngine);
|
|
3050
3156
|
this.processor = processor;
|
|
3051
3157
|
this.queue = this.factories.createEventQueue({
|
|
@@ -3164,7 +3270,10 @@ class JeevesWatcher {
|
|
|
3164
3270
|
const compiledRules = this.factories.compileRules(newConfig.inferenceRules ?? []);
|
|
3165
3271
|
const reloadConfigDir = dirname(this.configPath);
|
|
3166
3272
|
const newTemplateEngine = await buildTemplateEngine(newConfig.inferenceRules ?? [], newConfig.templates, newConfig.templateHelpers?.paths, reloadConfigDir);
|
|
3167
|
-
|
|
3273
|
+
const newCustomMapLib = newConfig.mapHelpers?.paths?.length && reloadConfigDir
|
|
3274
|
+
? await loadCustomMapHelpers(newConfig.mapHelpers.paths, reloadConfigDir)
|
|
3275
|
+
: undefined;
|
|
3276
|
+
processor.updateRules(compiledRules, newTemplateEngine, newCustomMapLib);
|
|
3168
3277
|
logger.info({ configPath: this.configPath, rules: compiledRules.length }, 'Config reloaded');
|
|
3169
3278
|
}
|
|
3170
3279
|
catch (error) {
|
package/package.json
CHANGED