sensemaking 0.12.2 → 0.13.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.
Files changed (245) hide show
  1. package/dist/cjs/cli/download.js +4 -4
  2. package/dist/cjs/cli/download.js.map +1 -1
  3. package/dist/cjs/cli/init.js +4 -4
  4. package/dist/cjs/cli/init.js.map +1 -1
  5. package/dist/cjs/cli/map.js +4 -4
  6. package/dist/cjs/cli/map.js.map +1 -1
  7. package/dist/cjs/cli/named.js +2 -2
  8. package/dist/cjs/cli/named.js.map +1 -1
  9. package/dist/cjs/cli/path.js +6 -6
  10. package/dist/cjs/cli/path.js.map +1 -1
  11. package/dist/cjs/cli/peek.js +4 -4
  12. package/dist/cjs/cli/peek.js.map +1 -1
  13. package/dist/cjs/cli/related.js +4 -4
  14. package/dist/cjs/cli/related.js.map +1 -1
  15. package/dist/cjs/cli/search.js +4 -4
  16. package/dist/cjs/cli/search.js.map +1 -1
  17. package/dist/cjs/cli/shared.d.cts +2 -2
  18. package/dist/cjs/cli/shared.d.ts +2 -2
  19. package/dist/cjs/cli/shared.js +5 -5
  20. package/dist/cjs/cli/shared.js.map +1 -1
  21. package/dist/cjs/cli/status.js +16 -16
  22. package/dist/cjs/cli/status.js.map +1 -1
  23. package/dist/cjs/cli/types.d.cts +1 -1
  24. package/dist/cjs/cli/types.d.ts +1 -1
  25. package/dist/cjs/cli.js +3 -3
  26. package/dist/cjs/cli.js.map +1 -1
  27. package/dist/cjs/commands/index.d.cts +10 -0
  28. package/dist/cjs/commands/index.d.ts +10 -0
  29. package/dist/cjs/commands/index.js +42 -0
  30. package/dist/cjs/commands/index.js.map +1 -0
  31. package/dist/cjs/commands/map.d.cts +21 -0
  32. package/dist/cjs/commands/map.d.ts +21 -0
  33. package/dist/cjs/commands/map.js +90 -0
  34. package/dist/cjs/commands/map.js.map +1 -0
  35. package/dist/cjs/commands/peek.d.cts +20 -0
  36. package/dist/cjs/commands/peek.d.ts +20 -0
  37. package/dist/cjs/commands/peek.js +175 -0
  38. package/dist/cjs/commands/peek.js.map +1 -0
  39. package/dist/cjs/commands/related.d.cts +6 -0
  40. package/dist/cjs/commands/related.d.ts +6 -0
  41. package/dist/cjs/commands/related.js +232 -0
  42. package/dist/cjs/commands/related.js.map +1 -0
  43. package/dist/cjs/commands/scope.d.cts +10 -0
  44. package/dist/cjs/commands/scope.d.ts +10 -0
  45. package/dist/cjs/commands/scope.js +124 -0
  46. package/dist/cjs/commands/scope.js.map +1 -0
  47. package/dist/cjs/commands/search.d.cts +12 -0
  48. package/dist/cjs/commands/search.d.ts +12 -0
  49. package/dist/cjs/{commands.js → commands/search.js} +20 -358
  50. package/dist/cjs/commands/search.js.map +1 -0
  51. package/dist/cjs/commands/status.d.cts +9 -0
  52. package/dist/cjs/commands/status.d.ts +9 -0
  53. package/dist/cjs/commands/status.js +25 -0
  54. package/dist/cjs/commands/status.js.map +1 -0
  55. package/dist/cjs/config/access.d.cts +19 -0
  56. package/dist/cjs/config/access.d.ts +19 -0
  57. package/dist/cjs/config/access.js +149 -0
  58. package/dist/cjs/config/access.js.map +1 -0
  59. package/dist/cjs/config/index.d.cts +5 -0
  60. package/dist/cjs/config/index.d.ts +5 -0
  61. package/dist/cjs/config/index.js +77 -0
  62. package/dist/cjs/config/index.js.map +1 -0
  63. package/dist/cjs/config/load.d.cts +8 -0
  64. package/dist/cjs/config/load.d.ts +8 -0
  65. package/dist/cjs/config/load.js +448 -0
  66. package/dist/cjs/config/load.js.map +1 -0
  67. package/dist/cjs/config/resolve.d.cts +6 -0
  68. package/dist/cjs/config/resolve.d.ts +6 -0
  69. package/dist/cjs/config/resolve.js +50 -0
  70. package/dist/cjs/config/resolve.js.map +1 -0
  71. package/dist/{esm/config.d.ts → cjs/config/types.d.cts} +2 -32
  72. package/dist/cjs/{config.d.cts → config/types.d.ts} +2 -32
  73. package/dist/cjs/config/types.js +39 -0
  74. package/dist/cjs/config/types.js.map +1 -0
  75. package/dist/cjs/config/validate.d.cts +4 -0
  76. package/dist/cjs/config/validate.d.ts +4 -0
  77. package/dist/cjs/config/validate.js +363 -0
  78. package/dist/cjs/config/validate.js.map +1 -0
  79. package/dist/cjs/db/index.d.cts +4 -0
  80. package/dist/cjs/db/index.d.ts +4 -0
  81. package/dist/cjs/db/index.js +42 -0
  82. package/dist/cjs/db/index.js.map +1 -0
  83. package/dist/cjs/{db.d.cts → db/open.d.cts} +2 -8
  84. package/dist/cjs/{db.d.ts → db/open.d.ts} +2 -8
  85. package/dist/cjs/db/open.js +223 -0
  86. package/dist/cjs/db/open.js.map +1 -0
  87. package/dist/cjs/db/reconcile.d.cts +9 -0
  88. package/dist/cjs/db/reconcile.d.ts +9 -0
  89. package/dist/cjs/{db.js → db/reconcile.js} +24 -261
  90. package/dist/cjs/db/reconcile.js.map +1 -0
  91. package/dist/cjs/db/shared.d.cts +5 -0
  92. package/dist/cjs/db/shared.d.ts +5 -0
  93. package/dist/cjs/db/shared.js +45 -0
  94. package/dist/cjs/db/shared.js.map +1 -0
  95. package/dist/cjs/features/embed.d.cts +1 -1
  96. package/dist/cjs/features/embed.d.ts +1 -1
  97. package/dist/cjs/features/embed.js +4 -3
  98. package/dist/cjs/features/embed.js.map +1 -1
  99. package/dist/cjs/features/index.d.cts +1 -1
  100. package/dist/cjs/features/index.d.ts +1 -1
  101. package/dist/cjs/features/index.js +4 -2
  102. package/dist/cjs/features/index.js.map +1 -1
  103. package/dist/cjs/features/links.js +38 -20
  104. package/dist/cjs/features/links.js.map +1 -1
  105. package/dist/cjs/features/tags.d.cts +2 -0
  106. package/dist/cjs/features/tags.d.ts +2 -0
  107. package/dist/cjs/features/tags.js +188 -0
  108. package/dist/cjs/features/tags.js.map +1 -0
  109. package/dist/cjs/features/types.d.cts +2 -2
  110. package/dist/cjs/features/types.d.ts +2 -2
  111. package/dist/cjs/index.d.cts +6 -6
  112. package/dist/cjs/index.d.ts +6 -6
  113. package/dist/cjs/index.js +15 -15
  114. package/dist/cjs/index.js.map +1 -1
  115. package/dist/cjs/scan.d.cts +3 -1
  116. package/dist/cjs/scan.d.ts +3 -1
  117. package/dist/cjs/scan.js +9 -6
  118. package/dist/cjs/scan.js.map +1 -1
  119. package/dist/cjs/sql-functions.d.cts +2 -0
  120. package/dist/cjs/sql-functions.d.ts +2 -0
  121. package/dist/cjs/sql-functions.js +64 -0
  122. package/dist/cjs/sql-functions.js.map +1 -0
  123. package/dist/cjs/watch.d.cts +1 -1
  124. package/dist/cjs/watch.d.ts +1 -1
  125. package/dist/cjs/watch.js +12 -12
  126. package/dist/cjs/watch.js.map +1 -1
  127. package/dist/esm/cli/download.js +1 -1
  128. package/dist/esm/cli/download.js.map +1 -1
  129. package/dist/esm/cli/init.js +1 -1
  130. package/dist/esm/cli/init.js.map +1 -1
  131. package/dist/esm/cli/map.js +1 -1
  132. package/dist/esm/cli/map.js.map +1 -1
  133. package/dist/esm/cli/named.js +1 -1
  134. package/dist/esm/cli/named.js.map +1 -1
  135. package/dist/esm/cli/path.js +1 -1
  136. package/dist/esm/cli/path.js.map +1 -1
  137. package/dist/esm/cli/peek.js +1 -1
  138. package/dist/esm/cli/peek.js.map +1 -1
  139. package/dist/esm/cli/related.js +1 -1
  140. package/dist/esm/cli/related.js.map +1 -1
  141. package/dist/esm/cli/search.js +1 -1
  142. package/dist/esm/cli/search.js.map +1 -1
  143. package/dist/esm/cli/shared.d.ts +2 -2
  144. package/dist/esm/cli/shared.js +2 -2
  145. package/dist/esm/cli/shared.js.map +1 -1
  146. package/dist/esm/cli/status.js +3 -3
  147. package/dist/esm/cli/status.js.map +1 -1
  148. package/dist/esm/cli/types.d.ts +1 -1
  149. package/dist/esm/cli/types.js.map +1 -1
  150. package/dist/esm/cli.js +1 -1
  151. package/dist/esm/cli.js.map +1 -1
  152. package/dist/esm/commands/index.d.ts +10 -0
  153. package/dist/esm/commands/index.js +8 -0
  154. package/dist/esm/commands/index.js.map +1 -0
  155. package/dist/esm/commands/map.d.ts +21 -0
  156. package/dist/esm/commands/map.js +56 -0
  157. package/dist/esm/commands/map.js.map +1 -0
  158. package/dist/esm/commands/peek.d.ts +20 -0
  159. package/dist/esm/commands/peek.js +63 -0
  160. package/dist/esm/commands/peek.js.map +1 -0
  161. package/dist/esm/commands/related.d.ts +6 -0
  162. package/dist/esm/commands/related.js +47 -0
  163. package/dist/esm/commands/related.js.map +1 -0
  164. package/dist/esm/commands/scope.d.ts +10 -0
  165. package/dist/esm/commands/scope.js +60 -0
  166. package/dist/esm/commands/scope.js.map +1 -0
  167. package/dist/esm/commands/search.d.ts +12 -0
  168. package/dist/esm/commands/search.js +258 -0
  169. package/dist/esm/commands/search.js.map +1 -0
  170. package/dist/esm/commands/status.d.ts +9 -0
  171. package/dist/esm/commands/status.js +16 -0
  172. package/dist/esm/commands/status.js.map +1 -0
  173. package/dist/esm/config/access.d.ts +19 -0
  174. package/dist/esm/config/access.js +89 -0
  175. package/dist/esm/config/access.js.map +1 -0
  176. package/dist/esm/config/index.d.ts +5 -0
  177. package/dist/esm/config/index.js +4 -0
  178. package/dist/esm/config/index.js.map +1 -0
  179. package/dist/esm/config/load.d.ts +8 -0
  180. package/dist/esm/config/load.js +242 -0
  181. package/dist/esm/config/load.js.map +1 -0
  182. package/dist/esm/config/resolve.d.ts +6 -0
  183. package/dist/esm/config/resolve.js +35 -0
  184. package/dist/esm/config/resolve.js.map +1 -0
  185. package/dist/{cjs/config.d.ts → esm/config/types.d.ts} +2 -32
  186. package/dist/esm/config/types.js +15 -0
  187. package/dist/esm/config/types.js.map +1 -0
  188. package/dist/esm/config/validate.d.ts +4 -0
  189. package/dist/esm/config/validate.js +226 -0
  190. package/dist/esm/config/validate.js.map +1 -0
  191. package/dist/esm/db/index.d.ts +4 -0
  192. package/dist/esm/db/index.js +5 -0
  193. package/dist/esm/db/index.js.map +1 -0
  194. package/dist/esm/{db.d.ts → db/open.d.ts} +2 -8
  195. package/dist/esm/db/open.js +160 -0
  196. package/dist/esm/db/open.js.map +1 -0
  197. package/dist/esm/db/reconcile.d.ts +9 -0
  198. package/dist/esm/{db.js → db/reconcile.js} +12 -216
  199. package/dist/esm/db/reconcile.js.map +1 -0
  200. package/dist/esm/db/shared.d.ts +5 -0
  201. package/dist/esm/db/shared.js +20 -0
  202. package/dist/esm/db/shared.js.map +1 -0
  203. package/dist/esm/features/embed.d.ts +1 -1
  204. package/dist/esm/features/embed.js +2 -1
  205. package/dist/esm/features/embed.js.map +1 -1
  206. package/dist/esm/features/index.d.ts +1 -1
  207. package/dist/esm/features/index.js +3 -1
  208. package/dist/esm/features/index.js.map +1 -1
  209. package/dist/esm/features/links.js +36 -21
  210. package/dist/esm/features/links.js.map +1 -1
  211. package/dist/esm/features/tags.d.ts +2 -0
  212. package/dist/esm/features/tags.js +80 -0
  213. package/dist/esm/features/tags.js.map +1 -0
  214. package/dist/esm/features/types.d.ts +2 -2
  215. package/dist/esm/features/types.js.map +1 -1
  216. package/dist/esm/index.d.ts +6 -6
  217. package/dist/esm/index.js +3 -3
  218. package/dist/esm/index.js.map +1 -1
  219. package/dist/esm/scan.d.ts +3 -1
  220. package/dist/esm/scan.js +5 -2
  221. package/dist/esm/scan.js.map +1 -1
  222. package/dist/esm/sql-functions.d.ts +2 -0
  223. package/dist/esm/sql-functions.js +47 -0
  224. package/dist/esm/sql-functions.js.map +1 -0
  225. package/dist/esm/watch.d.ts +1 -1
  226. package/dist/esm/watch.js +2 -2
  227. package/dist/esm/watch.js.map +1 -1
  228. package/package.json +2 -1
  229. package/schema.json +4 -0
  230. package/skills/sense/SKILL.md +3 -3
  231. package/skills/sense-bases/EXAMPLES.md +103 -0
  232. package/skills/sense-bases/SKILL.md +137 -0
  233. package/skills/sense-setup/SKILL.md +1 -1
  234. package/dist/cjs/commands.d.cts +0 -58
  235. package/dist/cjs/commands.d.ts +0 -58
  236. package/dist/cjs/commands.js.map +0 -1
  237. package/dist/cjs/config.js +0 -919
  238. package/dist/cjs/config.js.map +0 -1
  239. package/dist/cjs/db.js.map +0 -1
  240. package/dist/esm/commands.d.ts +0 -58
  241. package/dist/esm/commands.js +0 -486
  242. package/dist/esm/commands.js.map +0 -1
  243. package/dist/esm/config.js +0 -599
  244. package/dist/esm/config.js.map +0 -1
  245. package/dist/esm/db.js.map +0 -1
@@ -1,599 +0,0 @@
1
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
2
- import { dirname, join, resolve } from 'node:path';
3
- import { SenseError } from './errors.js';
4
- export const CONFIG_FILENAME = 'sense.config.json';
5
- export const STATE_DIR = '.sense';
6
- // Highest sense.config.json `version` this build understands. Older versions auto-migrate on load.
7
- export const SUPPORTED_CONFIG_VERSION = 4;
8
- // v1 and v2 were `scan`/`find`-shaped; v3 introduced `presets`. Only the former need the
9
- // legacy shape check before migrating.
10
- const FIRST_PRESET_VERSION = 3;
11
- // Each feature owns its tables, parse-time extraction, and reconcile step; commands degrade when one is off.
12
- // links/sections/rank are opt-out toggles in the top-level `features` block; embed is not a
13
- // member of that block -- it is on exactly when an `embed` block names a model.
14
- const FEATURE_NAMES = [
15
- 'links',
16
- 'sections',
17
- 'rank',
18
- 'embed'
19
- ];
20
- // v1 -> v2 migration only: the features v2 introduced as opt-out (embed was opt-in then).
21
- const V2_OPT_OUT_NAMES = [
22
- 'links',
23
- 'sections',
24
- 'rank'
25
- ];
26
- export const DEFAULT_EMBED_MODEL = 'minishlab/potion-retrieval-32M';
27
- export function presetNames(cfg) {
28
- return Object.keys(cfg.presets);
29
- }
30
- // Whether the tree names a model at all.
31
- export function embedEnabled(cfg) {
32
- var _cfg_embed;
33
- return typeof ((_cfg_embed = cfg.embed) === null || _cfg_embed === void 0 ? void 0 : _cfg_embed.model) === 'string' && cfg.embed.model.length > 0;
34
- }
35
- // A preset's own vector participation: absent or true means on, false means off.
36
- export function presetSemanticEnabled(cfg, name) {
37
- var _cfg_presets_name;
38
- return ((_cfg_presets_name = cfg.presets[name]) === null || _cfg_presets_name === void 0 ? void 0 : _cfg_presets_name.semantic) !== false;
39
- }
40
- // Whether embedding needs to run: a model is named AND some preset wants vectors.
41
- export function anyPresetEmbeds(cfg) {
42
- return embedEnabled(cfg) && presetNames(cfg).some((name)=>presetSemanticEnabled(cfg, name));
43
- }
44
- // Opt-out features (default on): absent block or key means enabled. `rank` additionally
45
- // requires `links`. `embed` is derived, not a features-block member: on iff `embed` names a model.
46
- export function featureEnabled(cfg, name) {
47
- var _cfg_features;
48
- if (name === 'embed') return anyPresetEmbeds(cfg);
49
- const enabled = ((_cfg_features = cfg.features) === null || _cfg_features === void 0 ? void 0 : _cfg_features[name]) !== false;
50
- if (name === 'rank') return enabled && featureEnabled(cfg, 'links');
51
- return enabled;
52
- }
53
- export function enabledFeatures(cfg) {
54
- return FEATURE_NAMES.filter((name)=>featureEnabled(cfg, name));
55
- }
56
- // Feature states, so "off" and "empty" stay distinguishable in output. `embed` is excluded:
57
- // it is not a features-block toggle, and `status` reports it on its own line.
58
- export function featureStates(cfg) {
59
- const reported = FEATURE_NAMES.filter((name)=>name !== 'embed');
60
- return {
61
- on: reported.filter((name)=>featureEnabled(cfg, name)),
62
- off: reported.filter((name)=>!featureEnabled(cfg, name))
63
- };
64
- }
65
- // Resolved embed provider settings, or null when the config names no model. There is no
66
- // fallback to DEFAULT_EMBED_MODEL: that constant is a template `init` and the v4 migration
67
- // write into the file, never an implicit runtime default.
68
- export function embedConfig(cfg) {
69
- var _e_type;
70
- if (!embedEnabled(cfg)) return null;
71
- const e = cfg.embed;
72
- return {
73
- model: e.model,
74
- type: (_e_type = e.type) !== null && _e_type !== void 0 ? _e_type : 'static',
75
- url: e.url,
76
- key: e.key
77
- };
78
- }
79
- // The configured FTS5 tokenizer, or undefined for the built-in default. Undefined rather than
80
- // the default string so featureSignature can leave the segment off entirely for trees that
81
- // never set it, which is what keeps existing indexes from rebuilding on upgrade.
82
- export function contentTokenize(cfg) {
83
- var _cfg_content;
84
- const tokenize = (_cfg_content = cfg.content) === null || _cfg_content === void 0 ? void 0 : _cfg_content.tokenize;
85
- return tokenize === undefined || tokenize.trim() === '' ? undefined : tokenize.trim();
86
- }
87
- // Cache key over everything indexing derives from, so any change to those inputs rebuilds.
88
- export function featureSignature(cfg) {
89
- const globalPart = enabledFeatures(cfg).filter((name)=>name !== 'embed').join(',');
90
- const e = embedConfig(cfg);
91
- const embedPart = e ? `embed:${e.type}:${e.model}` : 'embed:off';
92
- // One keyed segment per preset so a rebuild notice can name exactly which preset moved.
93
- const presetsPart = [
94
- ...presetNames(cfg)
95
- ].sort().map((name)=>{
96
- var _p_exclude;
97
- const p = cfg.presets[name];
98
- const include = [
99
- ...p.include
100
- ].sort().join('+');
101
- const exclude = [
102
- ...(_p_exclude = p.exclude) !== null && _p_exclude !== void 0 ? _p_exclude : []
103
- ].sort().join('+');
104
- return `preset:${name}:${include}:${exclude}:${p.semantic === false ? 'off' : 'on'}`;
105
- }).join('|');
106
- const tokenize = contentTokenize(cfg);
107
- const parts = [
108
- `features:${globalPart}`,
109
- embedPart
110
- ];
111
- if (tokenize !== undefined) parts.push(`tokenize:${tokenize}`);
112
- parts.push(presetsPart);
113
- return parts.join('|');
114
- }
115
- // Looks up a declared preset by name, defaulting to `default`. Throws naming every declared
116
- // preset when an explicit name is not declared -- the `default` fallback is never unknown
117
- // itself (validateConfig requires it).
118
- export function resolvePreset(cfg, name) {
119
- const presetName = name !== null && name !== void 0 ? name : 'default';
120
- const preset = cfg.presets[presetName];
121
- if (!preset) throw new SenseError('PRESET_UNKNOWN', `unknown preset "${presetName}"; declared presets: ${presetNames(cfg).join(', ')}`);
122
- return {
123
- name: presetName,
124
- preset
125
- };
126
- }
127
- // Precedence, per field: built-ins <- named preset (or `default`) <- caller overrides.
128
- // `opts` arrives with any saved-query/CLI merge already resolved (src/cli/named.ts).
129
- export function resolveSearch(cfg, opts = {}) {
130
- var _ref, _opts_k, _opts_where, _opts_include, _opts_exclude;
131
- const { name: presetName, preset } = resolvePreset(cfg, opts.preset);
132
- const k = (_ref = (_opts_k = opts.k) !== null && _opts_k !== void 0 ? _opts_k : preset.k) !== null && _ref !== void 0 ? _ref : 10;
133
- const where = (_opts_where = opts.where) !== null && _opts_where !== void 0 ? _opts_where : preset.where;
134
- // Each overrides its own side only, so neither clears the other.
135
- const include = (_opts_include = opts.include) !== null && _opts_include !== void 0 ? _opts_include : preset.include;
136
- // --no-exclude widens past the preset's exclusions; an explicit --exclude alongside it is
137
- // still the scope for this command, since it says what to leave out rather than what to keep.
138
- const exclude = (_opts_exclude = opts.exclude) !== null && _opts_exclude !== void 0 ? _opts_exclude : opts.noExclude ? undefined : preset.exclude;
139
- return {
140
- presetName,
141
- k,
142
- where,
143
- include,
144
- exclude,
145
- semantic: preset.semantic !== false
146
- };
147
- }
148
- // Pure per-version steps, chained by loadConfig. Intermediate shapes predate the Config type,
149
- // so steps are loosely typed and only the final result is cast back.
150
- const MIGRATIONS = {
151
- // v1 -> v2: features block introduced, opt-out features enabled (matches the old implicit
152
- // behavior of `links` etc. not existing). embed stays absent -- opt-in.
153
- 1: (cfg)=>({
154
- ...cfg,
155
- version: 2,
156
- features: Object.fromEntries(V2_OPT_OUT_NAMES.map((name)=>[
157
- name,
158
- true
159
- ]))
160
- }),
161
- // v2 -> v3: mechanical-minimal, by decision -- only what keeps an existing config loading.
162
- // No restructuring, no preset inference, no query rewriting; real trees get hand-migrated
163
- // separately to actually use presets.
164
- 2: (cfg)=>{
165
- var _prevDefaults_find;
166
- const { scan, defaults, features, checks, queries, ...rest } = cfg;
167
- const scanInclude = scan.include;
168
- const nextQueries = {};
169
- for (const [name, value] of Object.entries(queries !== null && queries !== void 0 ? queries : {})){
170
- if (typeof value === 'string') {
171
- nextQueries[name] = value;
172
- continue;
173
- }
174
- const entry = value;
175
- // Loosely typed on purpose: this step emits the v3 shape, which still had `semantic`.
176
- // The v3 -> v4 step below strips it.
177
- const search = {
178
- search: entry.find
179
- };
180
- if (entry.k !== undefined) search.k = entry.k;
181
- if (entry.where !== undefined) search.where = entry.where;
182
- // semantic: true was opt-in; it is now the default, so it drops. semantic: false is kept.
183
- if (entry.semantic === false) search.semantic = false;
184
- nextQueries[name] = search;
185
- }
186
- const prevDefaults = defaults;
187
- const defaultWhere = prevDefaults === null || prevDefaults === void 0 ? void 0 : (_prevDefaults_find = prevDefaults.find) === null || _prevDefaults_find === void 0 ? void 0 : _prevDefaults_find.where;
188
- // features.embed false becomes semantic:false on every preset produced here.
189
- const prevFeatures = features !== null && features !== void 0 ? features : {};
190
- const embedWasOff = prevFeatures.embed === false;
191
- // Object form carried provider settings (model/type/url/key), not a toggle -- they move
192
- // to the v3 top-level `embed` block verbatim; dropping them would silently switch an
193
- // api tree back to the built-in static model.
194
- const embedProvider = typeof prevFeatures.embed === 'object' && prevFeatures.embed !== null ? prevFeatures.embed : undefined;
195
- const { embed: _embed, ...restFeatures } = prevFeatures;
196
- const defaultPreset = {
197
- include: scanInclude
198
- };
199
- if (defaultWhere !== undefined) defaultPreset.where = defaultWhere;
200
- if (embedWasOff) defaultPreset.semantic = false;
201
- // checks (assertions over saved queries) was removed in v3; its queries still run under
202
- // `queries`, just without the pass/fail assertion -- reachable now only as a saved query.
203
- if (checks !== undefined) {
204
- console.error('sense: v2 "checks" was removed in v3 (sense check no longer asserts on saved queries); its queries are carried over under "queries" -- a returned row set is now the reader\'s judgment');
205
- }
206
- const result = {
207
- ...rest,
208
- version: 3,
209
- presets: {
210
- default: defaultPreset
211
- },
212
- queries: nextQueries
213
- };
214
- if (embedProvider !== undefined) result.embed = embedProvider;
215
- if (Object.keys(restFeatures).length > 0) result.features = restFeatures;
216
- return result;
217
- },
218
- // v3 -> v4: write the model into the file, wrap bare-string queries as { sql }, drop a saved
219
- // search's `semantic` (its preset decides). Same effective model, so nothing re-embeds.
220
- 3: (cfg)=>{
221
- var _cfg_queries, _cfg_embed;
222
- const presets = cfg.presets;
223
- const embedded = Object.values(presets).some((p)=>p.semantic !== false);
224
- const nextQueries = {};
225
- for (const [name, value] of Object.entries((_cfg_queries = cfg.queries) !== null && _cfg_queries !== void 0 ? _cfg_queries : {})){
226
- // v3's bare string meant SQL by inference; v4 makes every entry name its verb.
227
- if (typeof value === 'string') {
228
- nextQueries[name] = {
229
- sql: value
230
- };
231
- continue;
232
- }
233
- if (typeof value !== 'object' || value === null || !('search' in value)) {
234
- nextQueries[name] = value;
235
- continue;
236
- }
237
- const { semantic: _semantic, ...rest } = value;
238
- nextQueries[name] = rest;
239
- }
240
- const prev = (_cfg_embed = cfg.embed) !== null && _cfg_embed !== void 0 ? _cfg_embed : {};
241
- const result = {
242
- ...cfg,
243
- version: 4,
244
- queries: nextQueries
245
- };
246
- if (embedded) {
247
- var _prev_model, _prev_type;
248
- result.embed = {
249
- model: (_prev_model = prev.model) !== null && _prev_model !== void 0 ? _prev_model : DEFAULT_EMBED_MODEL,
250
- type: (_prev_type = prev.type) !== null && _prev_type !== void 0 ? _prev_type : 'static',
251
- ...prev.url !== undefined ? {
252
- url: prev.url
253
- } : {},
254
- ...prev.key !== undefined ? {
255
- key: prev.key
256
- } : {}
257
- };
258
- } else {
259
- delete result.embed;
260
- // Dropping settings silently would look like a bug in a tree that had configured an
261
- // api endpoint and then turned vectors off; say it, since v4 has no way to carry
262
- // provider settings for a tree that does not embed.
263
- if (Object.keys(prev).length > 0) console.error('sense: every preset has "semantic": false, so v4 drops the "embed" block (vectors stay off); re-add it, or set a preset\'s semantic back to true, to turn them on');
264
- }
265
- return result;
266
- }
267
- };
268
- export function migrateConfig(cfg) {
269
- var _cfg_version;
270
- const from = (_cfg_version = cfg.version) !== null && _cfg_version !== void 0 ? _cfg_version : 1;
271
- let current = cfg;
272
- for(let v = from; v < SUPPORTED_CONFIG_VERSION; v++){
273
- const step = MIGRATIONS[v];
274
- if (!step) throw new SenseError('CONFIG_VERSION_UNSUPPORTED', `no migration from config version ${v}`);
275
- current = step(current);
276
- }
277
- return {
278
- cfg: current,
279
- from
280
- };
281
- }
282
- function starterConfig() {
283
- return {
284
- $schema: 'https://unpkg.com/sensemaking/schema.json',
285
- version: SUPPORTED_CONFIG_VERSION,
286
- presets: {
287
- default: {
288
- include: [
289
- '**/*.md'
290
- ],
291
- k: 10
292
- },
293
- large: {
294
- include: [
295
- '**/*.md'
296
- ],
297
- k: 20
298
- }
299
- },
300
- // Written out rather than defaulted in code: this line is what makes `sense download`
301
- // fetch 124 MB, so it belongs where it can be read and changed.
302
- embed: {
303
- model: DEFAULT_EMBED_MODEL,
304
- type: 'static'
305
- },
306
- queries: {}
307
- };
308
- }
309
- // Refuses to overwrite an existing config.
310
- export function initConfig(dir) {
311
- const configPath = join(dir, CONFIG_FILENAME);
312
- if (existsSync(configPath)) {
313
- throw new SenseError('CONFIG_EXISTS', `${CONFIG_FILENAME} already exists in ${dir}`);
314
- }
315
- writeFileSync(configPath, `${JSON.stringify(starterConfig(), null, 2)}\n`);
316
- return configPath;
317
- }
318
- export function findConfigPath(startDir) {
319
- let dir = resolve(startDir);
320
- for(;;){
321
- const candidate = join(dir, CONFIG_FILENAME);
322
- if (existsSync(candidate)) return candidate;
323
- const parent = dirname(dir);
324
- if (parent === dir) return null;
325
- dir = parent;
326
- }
327
- }
328
- // Shape check for hand-edited files, so a typo names itself instead of surfacing as a
329
- // TypeError. Unknown top-level keys warn (forward compat); unknown keys inside a block error.
330
- const KNOWN_KEYS = new Set([
331
- '$schema',
332
- 'version',
333
- 'presets',
334
- 'features',
335
- 'embed',
336
- 'content',
337
- 'queries'
338
- ]);
339
- const KNOWN_PRESET_KEYS = new Set([
340
- 'include',
341
- 'exclude',
342
- 'k',
343
- 'semantic',
344
- 'where'
345
- ]);
346
- const KNOWN_FEATURE_KEYS = new Set([
347
- 'links',
348
- 'sections',
349
- 'rank'
350
- ]);
351
- const KNOWN_EMBED_KEYS = new Set([
352
- 'model',
353
- 'type',
354
- 'url',
355
- 'key'
356
- ]);
357
- const KNOWN_CONTENT_KEYS = new Set([
358
- 'tokenize'
359
- ]);
360
- const SAVED_SEARCH_KEYS = new Set([
361
- 'search',
362
- 'preset',
363
- 'include',
364
- 'exclude',
365
- 'where',
366
- 'k'
367
- ]);
368
- function unknownConfigKeys(cfg) {
369
- return Object.keys(cfg).filter((k)=>!KNOWN_KEYS.has(k));
370
- }
371
- function isNonEmptyStringArray(value) {
372
- return Array.isArray(value) && value.length > 0 && value.every((g)=>typeof g === 'string' && g.length > 0);
373
- }
374
- // Pre-v3 shape check, just enough for migrateConfig to run safely (it reads scan.include
375
- // directly). The full v3 shape is checked by validateConfig once migration has produced it.
376
- function validateLegacyScan(parsed, configPath) {
377
- const cfg = typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed) ? parsed : null;
378
- const scan = cfg === null || cfg === void 0 ? void 0 : cfg.scan;
379
- if (!cfg || !scan || !isNonEmptyStringArray(scan.include)) {
380
- throw new SenseError('CONFIG_INVALID', `${configPath}: scan.include must be a non-empty array of glob strings`);
381
- }
382
- }
383
- // Only the shape is checked here. Whether the linked SQLite accepts the tokenizer is settled
384
- // by probing it in db.ts, so this never has to carry a table of which version added what.
385
- function validateContentBlock(value, configPath) {
386
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
387
- throw new SenseError('CONFIG_INVALID', `${configPath}: content must be an object of { tokenize?: string }`);
388
- }
389
- const block = value;
390
- const unknown = Object.keys(block).filter((k)=>!KNOWN_CONTENT_KEYS.has(k));
391
- if (unknown.length > 0) {
392
- throw new SenseError('CONFIG_INVALID', `${configPath}: content has unknown key(s) ${unknown.join(', ')}; content takes tokenize`);
393
- }
394
- if (block.tokenize !== undefined && (typeof block.tokenize !== 'string' || block.tokenize.trim() === '')) {
395
- throw new SenseError('CONFIG_INVALID', `${configPath}: content.tokenize must be a non-empty string, e.g. "trigram" or "unicode61 tokenchars '-_'"`);
396
- }
397
- }
398
- function validateFeaturesBlock(value, configPath) {
399
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
400
- throw new SenseError('CONFIG_INVALID', `${configPath}: features must be an object of name -> boolean`);
401
- }
402
- const block = value;
403
- const unknown = Object.keys(block).filter((k)=>!KNOWN_FEATURE_KEYS.has(k));
404
- if (unknown.length > 0) {
405
- throw new SenseError('CONFIG_INVALID', `${configPath}: features has unknown key(s) ${unknown.join(', ')}; embed is not a features key -- see the top-level "embed" block`);
406
- }
407
- for (const [name, v] of Object.entries(block)){
408
- if (typeof v !== 'boolean') {
409
- throw new SenseError('CONFIG_INVALID', `${configPath}: features.${name} must be a boolean`);
410
- }
411
- }
412
- }
413
- function validateEmbedBlock(value, configPath) {
414
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
415
- throw new SenseError('CONFIG_INVALID', `${configPath}: embed must be an object of { model?, type?: "static"|"api", url?, key? }`);
416
- }
417
- const embed = value;
418
- const unknown = Object.keys(embed).filter((k)=>!KNOWN_EMBED_KEYS.has(k));
419
- if (unknown.length > 0) {
420
- throw new SenseError('CONFIG_INVALID', `${configPath}: embed has unknown key(s) ${unknown.join(', ')}`);
421
- }
422
- if (embed.model !== undefined && typeof embed.model !== 'string') {
423
- throw new SenseError('CONFIG_INVALID', `${configPath}: embed.model must be a string`);
424
- }
425
- // The block exists to name a model; provider settings without one would read as "vectors
426
- // configured" while embedding stays off, which is the ambiguity v4 removed.
427
- if (embed.model === undefined || embed.model === '') {
428
- throw new SenseError('CONFIG_INVALID', `${configPath}: embed.model is required when the "embed" block is present (it is what turns vectors on); remove the block to index without vectors`);
429
- }
430
- if (embed.type !== undefined && embed.type !== 'static' && embed.type !== 'api') {
431
- throw new SenseError('CONFIG_INVALID', `${configPath}: embed.type must be "static" or "api"`);
432
- }
433
- if (embed.url !== undefined && typeof embed.url !== 'string') {
434
- throw new SenseError('CONFIG_INVALID', `${configPath}: embed.url must be a string`);
435
- }
436
- if (embed.key !== undefined && typeof embed.key !== 'string') {
437
- throw new SenseError('CONFIG_INVALID', `${configPath}: embed.key must be a string`);
438
- }
439
- }
440
- function validatePreset(name, value, configPath) {
441
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
442
- throw new SenseError('CONFIG_INVALID', `${configPath}: presets.${name} must be an object`);
443
- }
444
- const preset = value;
445
- const unknown = Object.keys(preset).filter((k)=>!KNOWN_PRESET_KEYS.has(k));
446
- if (unknown.length > 0) {
447
- throw new SenseError('CONFIG_INVALID', `${configPath}: presets.${name} has unknown key(s) ${unknown.join(', ')}; a preset takes include, exclude, k, semantic, where`);
448
- }
449
- if (!isNonEmptyStringArray(preset.include)) {
450
- throw new SenseError('CONFIG_INVALID', `${configPath}: presets.${name}.include must be a non-empty array of glob strings`);
451
- }
452
- if (preset.exclude !== undefined && !isNonEmptyStringArray(preset.exclude)) {
453
- throw new SenseError('CONFIG_INVALID', `${configPath}: presets.${name}.exclude must be a non-empty array of glob strings`);
454
- }
455
- if (preset.semantic !== undefined && typeof preset.semantic !== 'boolean') {
456
- throw new SenseError('CONFIG_INVALID', `${configPath}: presets.${name}.semantic must be a boolean`);
457
- }
458
- if (preset.k !== undefined && (typeof preset.k !== 'number' || !Number.isInteger(preset.k) || preset.k <= 0)) {
459
- throw new SenseError('CONFIG_INVALID', `${configPath}: presets.${name}.k must be a positive integer`);
460
- }
461
- if (preset.where !== undefined && typeof preset.where !== 'string') {
462
- throw new SenseError('CONFIG_INVALID', `${configPath}: presets.${name}.where must be a SQL condition string`);
463
- }
464
- }
465
- // A queries.<name> entry: { sql } or a saved search { search, preset?, include?, exclude?, where?, k? }.
466
- function validateSavedQuery(name, value, configPath) {
467
- // A bare string used to mean SQL. It now fails rather than being inferred: an entry says
468
- // which of the two verbs it runs, the same choice the CLI makes explicit.
469
- if (typeof value === 'string') {
470
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name} must say which verb it runs: { "sql": ${JSON.stringify(value.length > 40 ? `${value.slice(0, 37)}...` : value)} } to run it as SQL, or { "search": "..." } for a ranked search`);
471
- }
472
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
473
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name} must be { sql } or { search, preset?, include?, exclude?, where?, k? }`);
474
- }
475
- const entry = value;
476
- if ('sql' in entry) {
477
- const unknown = Object.keys(entry).filter((k)=>k !== 'sql');
478
- if (unknown.length > 0) {
479
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name} has unknown key(s) ${unknown.join(', ')}; a { sql } query takes only sql`);
480
- }
481
- if (typeof entry.sql !== 'string' || entry.sql.trim() === '') {
482
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name}.sql must be a non-empty string`);
483
- }
484
- return;
485
- }
486
- if ('search' in entry) {
487
- const unknown = Object.keys(entry).filter((k)=>!SAVED_SEARCH_KEYS.has(k));
488
- if (unknown.length > 0) {
489
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name} has unknown key(s) ${unknown.join(', ')}; a saved search takes search, preset, include, exclude, where, k`);
490
- }
491
- // A saved entry saves a question; a scope without a question is just flags.
492
- if (typeof entry.search !== 'string' || entry.search.trim() === '') {
493
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name}.search must be non-empty text`);
494
- }
495
- if (entry.preset !== undefined && (typeof entry.preset !== 'string' || entry.preset.length === 0)) {
496
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name}.preset must be a preset name`);
497
- }
498
- if (entry.include !== undefined && !isNonEmptyStringArray(entry.include)) {
499
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name}.include must be a non-empty array of glob strings`);
500
- }
501
- if (entry.exclude !== undefined && !isNonEmptyStringArray(entry.exclude)) {
502
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name}.exclude must be a non-empty array of glob strings`);
503
- }
504
- if (entry.where !== undefined && typeof entry.where !== 'string') {
505
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name}.where must be a SQL condition string`);
506
- }
507
- if (entry.k !== undefined && (typeof entry.k !== 'number' || !Number.isInteger(entry.k) || entry.k <= 0)) {
508
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name}.k must be a positive integer`);
509
- }
510
- return;
511
- }
512
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries.${name} must be { sql } or { search, preset?, include?, exclude?, where?, k? }`);
513
- }
514
- function validateConfig(parsed, configPath) {
515
- if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
516
- throw new SenseError('CONFIG_INVALID', `${configPath}: config must be a JSON object`);
517
- }
518
- const cfg = parsed;
519
- // `checks` is rejected by name, not warned: silence would hide that assertions are gone.
520
- if (cfg.checks !== undefined) {
521
- throw new SenseError('CONFIG_INVALID', `${configPath}: checks was removed in v3 -- sense check no longer asserts on saved queries; a returned row set is the reader's judgment`);
522
- }
523
- const presets = cfg.presets;
524
- if (!presets || typeof presets !== 'object' || Array.isArray(presets) || Object.keys(presets).length === 0) {
525
- throw new SenseError('CONFIG_INVALID', `${configPath}: presets must be a non-empty object of preset name -> { include, exclude?, k?, semantic?, where? }`);
526
- }
527
- for (const [name, value] of Object.entries(presets)){
528
- validatePreset(name, value, configPath);
529
- }
530
- if (presets.default === undefined) {
531
- throw new SenseError('CONFIG_INVALID', `${configPath}: presets must include a "default" preset`);
532
- }
533
- if (cfg.queries === undefined) cfg.queries = {};
534
- if (typeof cfg.queries !== 'object' || cfg.queries === null || Array.isArray(cfg.queries)) {
535
- throw new SenseError('CONFIG_INVALID', `${configPath}: queries must be an object of name -> { sql } or { search }`);
536
- }
537
- const queries = cfg.queries;
538
- for (const [name, value] of Object.entries(queries)){
539
- validateSavedQuery(name, value, configPath);
540
- }
541
- if (cfg.features !== undefined) {
542
- validateFeaturesBlock(cfg.features, configPath);
543
- }
544
- if (cfg.embed !== undefined) {
545
- validateEmbedBlock(cfg.embed, configPath);
546
- }
547
- if (cfg.content !== undefined) {
548
- validateContentBlock(cfg.content, configPath);
549
- }
550
- return cfg;
551
- }
552
- export function loadConfig(explicitPath) {
553
- let configPath;
554
- if (explicitPath) {
555
- configPath = resolve(process.cwd(), explicitPath);
556
- if (!existsSync(configPath)) throw new SenseError('CONFIG_NOT_FOUND', `config not found: ${configPath}`);
557
- } else {
558
- const found = findConfigPath(process.cwd());
559
- if (!found) {
560
- throw new SenseError('CONFIG_NOT_FOUND', `could not find ${CONFIG_FILENAME} in ${process.cwd()} or any parent directory`);
561
- }
562
- configPath = found;
563
- }
564
- const raw = readFileSync(configPath, 'utf8');
565
- const parsed = JSON.parse(raw);
566
- // Ahead of both the version gate and legacy/current shape checks: a non-object file (or
567
- // an array -- typeof [] is also 'object') is malformed at every version, so it gets one
568
- // error regardless of which branch would otherwise run.
569
- if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
570
- throw new SenseError('CONFIG_INVALID', `${configPath}: config must be a JSON object`);
571
- }
572
- // Version gate before shape validation: a config written by a newer sense should fail
573
- // with "requires a newer sense", not with shape errors its own version may not have.
574
- const version = typeof parsed === 'object' && parsed !== null && typeof parsed.version === 'number' ? parsed.version : 1;
575
- if (version > SUPPORTED_CONFIG_VERSION) {
576
- throw new SenseError('CONFIG_VERSION_UNSUPPORTED', `config version ${version} requires a newer sense`);
577
- }
578
- // validateConfig only knows the current shape, so scan-shaped files (pre-v3) get a minimal
579
- // check, migrate, then the full one on the result.
580
- let cfg;
581
- let migratedFrom;
582
- if (version < SUPPORTED_CONFIG_VERSION) {
583
- if (version < FIRST_PRESET_VERSION) validateLegacyScan(parsed, configPath);
584
- const result = migrateConfig(parsed);
585
- cfg = validateConfig(result.cfg, configPath);
586
- migratedFrom = result.from;
587
- writeFileSync(configPath, `${JSON.stringify(cfg, null, 2)}\n`);
588
- } else {
589
- cfg = validateConfig(parsed, configPath);
590
- }
591
- const unknownKeys = unknownConfigKeys(cfg);
592
- return {
593
- ...cfg,
594
- baseDir: dirname(configPath),
595
- configPath,
596
- migratedFrom,
597
- unknownKeys: unknownKeys.length > 0 ? unknownKeys : undefined
598
- };
599
- }