hafez 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +67 -9
- package/dist/apply.d.ts +30 -0
- package/dist/apply.js +582 -0
- package/dist/batch-ops.d.ts +470 -0
- package/dist/batch-ops.js +446 -0
- package/dist/cli/args.d.ts +19 -0
- package/dist/cli/args.js +53 -0
- package/dist/cli/batch-errors.js +6 -3
- package/dist/cli/commands.d.ts +24 -108
- package/dist/cli/commands.js +297 -313
- package/dist/cli/help-agent.d.ts +0 -5
- package/dist/cli/help-agent.js +44 -5
- package/dist/cli/index.d.ts +41 -0
- package/dist/cli/index.js +172 -172
- package/dist/cli/resolve-vault.js +2 -1
- package/dist/cli/schema-introspect.d.ts +1 -1
- package/dist/cli/schema-introspect.js +6 -22
- package/dist/cli/upgrade.d.ts +42 -0
- package/dist/cli/upgrade.js +305 -0
- package/dist/cli.js +0 -0
- package/dist/contracts.d.ts +55 -6
- package/dist/contracts.js +110 -23
- package/dist/dates.d.ts +10 -0
- package/dist/dates.js +27 -0
- package/dist/db.d.ts +27 -2
- package/dist/db.js +318 -56
- package/dist/digest.js +2 -0
- package/dist/document.d.ts +117 -0
- package/dist/document.js +578 -0
- package/dist/export-okf.js +48 -34
- package/dist/git.d.ts +7 -8
- package/dist/git.js +198 -63
- package/dist/index.d.ts +3 -4
- package/dist/index.js +302 -915
- package/dist/knowledge-index.d.ts +23 -1
- package/dist/knowledge-index.js +71 -13
- package/dist/merge.d.ts +1 -1
- package/dist/merge.js +106 -86
- package/dist/schema.js +4 -4
- package/dist/templates.d.ts +6 -0
- package/dist/templates.js +27 -27
- package/dist/types.d.ts +86 -12
- package/dist/vault.d.ts +51 -6
- package/dist/vault.js +114 -11
- package/package.json +2 -2
- package/skill/SKILL.md +51 -110
- package/dist/parser.d.ts +0 -18
- package/dist/parser.js +0 -68
- package/dist/query.d.ts +0 -24
- package/dist/query.js +0 -167
- package/dist/sections.d.ts +0 -54
- package/dist/sections.js +0 -297
package/dist/document.js
ADDED
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
// src/document.ts
|
|
2
|
+
// The Document module (see CONTEXT.md): owns the body of a vault file —
|
|
3
|
+
// structural sections, session-log entries, wiki-links. Every body read or
|
|
4
|
+
// mutation goes through the single structural-heading model defined here.
|
|
5
|
+
// Pure string→string.
|
|
6
|
+
//
|
|
7
|
+
// Which headings are structural is per-file knowledge: documentFor(ctx)
|
|
8
|
+
// derives the section set from the file's kind/type/subtype via contracts
|
|
9
|
+
// and returns the Document operations bound to it. There is deliberately no
|
|
10
|
+
// global fallback set — a session-only heading like '## Decisions' quoted
|
|
11
|
+
// inside an entity's Brief must NOT terminate that section.
|
|
12
|
+
import { ALL_CONTRACTS, SUBTYPE_SECTIONS, SESSION_SECTIONS, getContract } from './contracts.js';
|
|
13
|
+
import { localDay } from './dates.js';
|
|
14
|
+
const UNCHECKED_RE = /^- \[ \] (.+)$/gm;
|
|
15
|
+
// --- Set-independent helpers ---
|
|
16
|
+
const HEADING_RE = /^### (\d{4}-\d{2}-\d{2}) (?:—|--) (.+?)(?:\s+\[(\w+)\])?$/;
|
|
17
|
+
export function parseSessionLogHeading(line) {
|
|
18
|
+
const match = line.match(HEADING_RE);
|
|
19
|
+
if (!match)
|
|
20
|
+
return null;
|
|
21
|
+
return {
|
|
22
|
+
date: match[1],
|
|
23
|
+
agent: match[2],
|
|
24
|
+
type: match[3] || 'progress'
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export function formatSessionLogEntry(entry) {
|
|
28
|
+
const today = localDay();
|
|
29
|
+
const lines = [`### ${today} — ${entry.agent} [${entry.type}]`, `Summary: ${entry.summary}`];
|
|
30
|
+
if (entry.body)
|
|
31
|
+
lines.push(entry.body);
|
|
32
|
+
return lines.join('\n');
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Split a Session Log section's content into raw entry blocks, each beginning
|
|
36
|
+
* with a parseable entry heading. Non-entry text before the first heading is
|
|
37
|
+
* not an entry and is dropped. Document owns entry block splitting — merge's
|
|
38
|
+
* dedup/sort policy stays in merge.ts.
|
|
39
|
+
*/
|
|
40
|
+
export function splitLogEntries(content) {
|
|
41
|
+
return content
|
|
42
|
+
.split(/\n(?=### \d{4}-\d{2}-\d{2})/)
|
|
43
|
+
.map(b => b.trim())
|
|
44
|
+
.filter(b => b && parseSessionLogHeading(b.split('\n')[0]) !== null);
|
|
45
|
+
}
|
|
46
|
+
export function firstNonEmptyLine(s) {
|
|
47
|
+
return s?.split('\n').find(l => l.trim())?.trim() ?? '';
|
|
48
|
+
}
|
|
49
|
+
/** The one wiki-link syntax the vault recognizes: [[slug]] or
|
|
50
|
+
* [[slug|display text]] — slug in group 1, display text in group 2.
|
|
51
|
+
* Padding inside the brackets is Obsidian-idiomatic ([[ slug ]], [[slug | Label]])
|
|
52
|
+
* and is absorbed by the pattern, so both captures come out already trimmed —
|
|
53
|
+
* every consumer (Related coverage, mention harvesting, export) sees the bare slug.
|
|
54
|
+
* Non-global — wrap with `new RegExp(WIKI_LINK_RE.source, 'g')` for iteration. */
|
|
55
|
+
export const WIKI_LINK_RE = /\[\[\s*([^\]|]+?)\s*(?:\|\s*([^\]]+?)\s*)?\]\]/;
|
|
56
|
+
function escapeRegex(s) {
|
|
57
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Normalize text for matching: trim whitespace, replace smart quotes with
|
|
61
|
+
* straight equivalents, normalize em-dashes to regular dashes.
|
|
62
|
+
* Applied to both query and action text before comparison only — stored text is unchanged.
|
|
63
|
+
*/
|
|
64
|
+
function normalizeForMatch(s) {
|
|
65
|
+
return s
|
|
66
|
+
.trim()
|
|
67
|
+
.replace(/[‘’]/g, "'")
|
|
68
|
+
.replace(/[“”]/g, '"')
|
|
69
|
+
.replace(/—/g, '-');
|
|
70
|
+
}
|
|
71
|
+
const ENTITY_CONTRACTS = ALL_CONTRACTS.filter(c => c.kind === 'item');
|
|
72
|
+
function sectionNamesFor(ctx) {
|
|
73
|
+
if (ctx.kind === 'session')
|
|
74
|
+
return [...SESSION_SECTIONS];
|
|
75
|
+
if (ctx.kind === 'knowledge') {
|
|
76
|
+
const subtypeTable = SUBTYPE_SECTIONS;
|
|
77
|
+
const names = [...getContract('knowledge').defaultSections];
|
|
78
|
+
for (const s of Object.keys(subtypeTable))
|
|
79
|
+
names.push(...subtypeTable[s]);
|
|
80
|
+
return names;
|
|
81
|
+
}
|
|
82
|
+
return ENTITY_CONTRACTS.flatMap(c => [...c.defaultSections, ...c.optionalSections]);
|
|
83
|
+
}
|
|
84
|
+
/** The structural set a context binds — exposed for introspection and the
|
|
85
|
+
* contract-coverage tests; runtime consumers go through documentFor. */
|
|
86
|
+
export function structuralSectionsFor(ctx) {
|
|
87
|
+
return new Set([...sectionNamesFor(ctx), 'Related']);
|
|
88
|
+
}
|
|
89
|
+
const docCache = new Map();
|
|
90
|
+
/** The Document operations bound to one file's structural section set. */
|
|
91
|
+
export function documentFor(ctx) {
|
|
92
|
+
const key = ctx.kind;
|
|
93
|
+
let doc = docCache.get(key);
|
|
94
|
+
if (!doc) {
|
|
95
|
+
doc = makeDoc(new Set(structuralSectionsFor(ctx)));
|
|
96
|
+
docCache.set(key, doc);
|
|
97
|
+
}
|
|
98
|
+
return doc;
|
|
99
|
+
}
|
|
100
|
+
/** Bind from a parsed vault file — kind from Layout. */
|
|
101
|
+
export function documentForFile(parsed) {
|
|
102
|
+
return documentFor({ kind: parsed.kind });
|
|
103
|
+
}
|
|
104
|
+
// --- The bound implementation ---
|
|
105
|
+
function makeDoc(structuralSections) {
|
|
106
|
+
/**
|
|
107
|
+
* Find the next structural `## Heading` boundary after a given position.
|
|
108
|
+
* Skips `## ` headings that are not known structural sections (content headings).
|
|
109
|
+
* Returns the index of the `\n` before the `## `, or -1 if no structural heading found.
|
|
110
|
+
*/
|
|
111
|
+
function findNextStructuralHeading(body, from) {
|
|
112
|
+
const re = /\n## ([^\n]+)/g;
|
|
113
|
+
re.lastIndex = from;
|
|
114
|
+
let match;
|
|
115
|
+
while ((match = re.exec(body)) !== null) {
|
|
116
|
+
const headingName = match[1].trim();
|
|
117
|
+
if (structuralSections.has(headingName)) {
|
|
118
|
+
return match.index;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return -1;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Find a ## Heading section and its content.
|
|
125
|
+
* Returns { start, headingEnd, end, content } or null if not found.
|
|
126
|
+
* Matches heading exactly (e.g. "## Brief" won't match "## Briefing").
|
|
127
|
+
* Trailing whitespace on the heading line is tolerated — the splitter's
|
|
128
|
+
* boundary tokenizer trims headings, so anything it treats as a boundary
|
|
129
|
+
* must also be findable here (a hand-edited `## Session Log ` otherwise
|
|
130
|
+
* gets a duplicate section and archival never fires).
|
|
131
|
+
* Only treats known structural headings as section boundaries -- content
|
|
132
|
+
* inside a section may contain arbitrary `## ` headings without corruption.
|
|
133
|
+
*/
|
|
134
|
+
function findSection(body, heading) {
|
|
135
|
+
const re = new RegExp(`(^|\\n)(## ${escapeRegex(heading)}[ \\t]*)(?:\\n|$)`);
|
|
136
|
+
const match = re.exec(body);
|
|
137
|
+
if (!match)
|
|
138
|
+
return null;
|
|
139
|
+
const start = match.index + match[1].length;
|
|
140
|
+
const headingEnd = start + match[2].length;
|
|
141
|
+
const afterHeading = headingEnd + 1;
|
|
142
|
+
// Find the next structural ## heading or EOF. Scan from the heading's own
|
|
143
|
+
// trailing newline (afterHeading - 1), not past it — otherwise a structural
|
|
144
|
+
// heading on the immediately following line (no blank line between, as in
|
|
145
|
+
// hand-edited files) is invisible and mutators swallow the next section.
|
|
146
|
+
const nextHeading = findNextStructuralHeading(body, Math.max(0, afterHeading - 1));
|
|
147
|
+
const end = nextHeading === -1 ? body.length : nextHeading;
|
|
148
|
+
const rawContent = body.slice(afterHeading, end);
|
|
149
|
+
const content = rawContent.replace(/^\n+/, '').replace(/\n+$/, '');
|
|
150
|
+
return { start, headingEnd, end, content };
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Split a body into preamble + structural sections. Non-structural `## `
|
|
154
|
+
* headings stay inside the enclosing section's content — sections are
|
|
155
|
+
* atomic units of authored content, never split at user headings.
|
|
156
|
+
* Headings and content come back trimmed; reassemble with
|
|
157
|
+
* `## ${heading}\n\n${content}` joined by blank lines.
|
|
158
|
+
*/
|
|
159
|
+
function splitStructural(body) {
|
|
160
|
+
const marks = [];
|
|
161
|
+
const re = /(^|\n)## ([^\n]*)/g;
|
|
162
|
+
let m;
|
|
163
|
+
while ((m = re.exec(body)) !== null) {
|
|
164
|
+
const heading = m[2].trim();
|
|
165
|
+
if (!structuralSections.has(heading))
|
|
166
|
+
continue;
|
|
167
|
+
marks.push({ start: m.index + m[1].length, contentStart: m.index + m[0].length, heading });
|
|
168
|
+
}
|
|
169
|
+
if (marks.length === 0)
|
|
170
|
+
return { preamble: body.trim(), sections: [] };
|
|
171
|
+
const preamble = body.slice(0, marks[0].start).trim();
|
|
172
|
+
const sections = marks.map((mk, i) => ({
|
|
173
|
+
heading: mk.heading,
|
|
174
|
+
content: body.slice(mk.contentStart, i + 1 < marks.length ? marks[i + 1].start : body.length).trim(),
|
|
175
|
+
}));
|
|
176
|
+
return { preamble, sections };
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Replace a structural section's content, or create the section: before
|
|
180
|
+
* `insertBefore` when that section exists, at EOF otherwise.
|
|
181
|
+
*/
|
|
182
|
+
function setSection(body, heading, content, insertBefore) {
|
|
183
|
+
const existing = findSection(body, heading);
|
|
184
|
+
if (existing) {
|
|
185
|
+
return body.slice(0, existing.start) + `## ${heading}\n\n${content}\n` + body.slice(existing.end);
|
|
186
|
+
}
|
|
187
|
+
if (insertBefore) {
|
|
188
|
+
const target = findSection(body, insertBefore);
|
|
189
|
+
if (target) {
|
|
190
|
+
return body.slice(0, target.start) + `## ${heading}\n\n${content}\n\n` + body.slice(target.start);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return body + `\n\n## ${heading}\n\n${content}\n`;
|
|
194
|
+
}
|
|
195
|
+
/** Append content at the end of a structural section (creating it at EOF if absent). */
|
|
196
|
+
function appendToSection(body, heading, content) {
|
|
197
|
+
const section = findSection(body, heading);
|
|
198
|
+
if (!section)
|
|
199
|
+
return body + `\n\n## ${heading}\n\n${content}\n`;
|
|
200
|
+
if (section.end === body.length)
|
|
201
|
+
return body + '\n' + content + '\n';
|
|
202
|
+
return body.slice(0, section.end) + '\n' + content + body.slice(section.end);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Rename a structural section's heading in place, keeping its content.
|
|
206
|
+
* No-op when the section is absent. Any trailing whitespace on the old
|
|
207
|
+
* heading line is dropped with it.
|
|
208
|
+
*/
|
|
209
|
+
function renameSection(body, from, to) {
|
|
210
|
+
const section = findSection(body, from);
|
|
211
|
+
if (!section)
|
|
212
|
+
return body;
|
|
213
|
+
return body.slice(0, section.start) + `## ${to}` + body.slice(section.headingEnd);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Ensure a structural section exists: no-op when present, otherwise insert
|
|
217
|
+
* an empty section after `opts.after` (when that section exists) or at EOF.
|
|
218
|
+
*/
|
|
219
|
+
function ensureSection(body, heading, opts = {}) {
|
|
220
|
+
if (findSection(body, heading))
|
|
221
|
+
return body;
|
|
222
|
+
const block = `## ${heading}`;
|
|
223
|
+
if (opts.after) {
|
|
224
|
+
const anchor = findSection(body, opts.after);
|
|
225
|
+
if (anchor) {
|
|
226
|
+
const before = body.slice(0, anchor.end).replace(/\n+$/, '');
|
|
227
|
+
const after = body.slice(anchor.end).replace(/^\n+/, '');
|
|
228
|
+
return after ? `${before}\n\n${block}\n\n${after}` : `${before}\n\n${block}\n`;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return body.replace(/\n+$/, '') + `\n\n${block}\n`;
|
|
232
|
+
}
|
|
233
|
+
/** Insert a formatted session-log entry newest-first (creating the section at EOF if absent). */
|
|
234
|
+
function prependSessionLogEntry(body, entry) {
|
|
235
|
+
const section = findSection(body, 'Session Log');
|
|
236
|
+
if (!section)
|
|
237
|
+
return body + `\n\n## Session Log\n\n${entry}\n`;
|
|
238
|
+
const insertPos = section.headingEnd;
|
|
239
|
+
return body.slice(0, insertPos) + '\n\n' + entry + '\n' + body.slice(insertPos);
|
|
240
|
+
}
|
|
241
|
+
/** Body up to the real Session Log section — the `summary` read depth. */
|
|
242
|
+
function bodyBeforeSessionLog(body) {
|
|
243
|
+
const section = findSection(body, 'Session Log');
|
|
244
|
+
return section ? body.slice(0, section.start).trim() : body;
|
|
245
|
+
}
|
|
246
|
+
/** Entries inside the real Session Log section only — entry-shaped lines
|
|
247
|
+
* elsewhere in the body (e.g. quoted in a Brief) do not count. */
|
|
248
|
+
function countSessionLogEntries(body) {
|
|
249
|
+
const section = findSection(body, 'Session Log');
|
|
250
|
+
if (!section)
|
|
251
|
+
return 0;
|
|
252
|
+
return (section.content.match(/^### \d{4}-\d{2}-\d{2} (?:—|--)/gm) || []).length;
|
|
253
|
+
}
|
|
254
|
+
function parseSessionLog(body) {
|
|
255
|
+
const section = findSection(body, 'Session Log');
|
|
256
|
+
if (!section)
|
|
257
|
+
return [];
|
|
258
|
+
const entries = [];
|
|
259
|
+
const lines = section.content.split('\n');
|
|
260
|
+
let current = null;
|
|
261
|
+
let summaryLine = null;
|
|
262
|
+
for (const line of lines) {
|
|
263
|
+
const heading = parseSessionLogHeading(line);
|
|
264
|
+
if (heading) {
|
|
265
|
+
if (current && summaryLine) {
|
|
266
|
+
entries.push({ ...current, summary: summaryLine });
|
|
267
|
+
}
|
|
268
|
+
current = heading;
|
|
269
|
+
summaryLine = null;
|
|
270
|
+
}
|
|
271
|
+
else if (current && !summaryLine && line.startsWith('Summary: ')) {
|
|
272
|
+
summaryLine = line.slice('Summary: '.length);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (current && summaryLine) {
|
|
276
|
+
entries.push({ ...current, summary: summaryLine });
|
|
277
|
+
}
|
|
278
|
+
return entries;
|
|
279
|
+
}
|
|
280
|
+
function extractOldestSessionLogEntry(body) {
|
|
281
|
+
const section = findSection(body, 'Session Log');
|
|
282
|
+
if (!section)
|
|
283
|
+
return null;
|
|
284
|
+
// Work on the raw slice between the heading and the section's structural
|
|
285
|
+
// end, so the extracted entry can never swallow a following section
|
|
286
|
+
// (e.g. an auto-appended Related) into the archive.
|
|
287
|
+
const contentStart = section.headingEnd;
|
|
288
|
+
const region = body.slice(contentStart, section.end);
|
|
289
|
+
const entryStarts = [...region.matchAll(/^### \d{4}-\d{2}-\d{2} (?:—|--)/gm)];
|
|
290
|
+
if (entryStarts.length < 2)
|
|
291
|
+
return null;
|
|
292
|
+
// Entries are newest-first in the file, so the oldest is the last match
|
|
293
|
+
const oldestStart = entryStarts[entryStarts.length - 1].index;
|
|
294
|
+
const entry = region.slice(oldestStart).trim();
|
|
295
|
+
const remaining = body.slice(0, contentStart) + region.slice(0, oldestStart) + body.slice(section.end);
|
|
296
|
+
return { entry, remaining };
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Remove a section (heading + content) from body, cleaning up extra whitespace.
|
|
300
|
+
*/
|
|
301
|
+
function removeSection(body, heading) {
|
|
302
|
+
const section = findSection(body, heading);
|
|
303
|
+
if (!section)
|
|
304
|
+
return body;
|
|
305
|
+
const before = body.slice(0, section.start);
|
|
306
|
+
const after = body.slice(section.end);
|
|
307
|
+
// Clean up: avoid triple+ newlines at the splice point
|
|
308
|
+
const trimmedBefore = before.replace(/\n+$/, '');
|
|
309
|
+
const trimmedAfter = after.replace(/^\n+/, '');
|
|
310
|
+
if (!trimmedBefore)
|
|
311
|
+
return trimmedAfter;
|
|
312
|
+
if (!trimmedAfter)
|
|
313
|
+
return trimmedBefore;
|
|
314
|
+
return trimmedBefore + '\n\n' + trimmedAfter;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Insert a new section before a target heading, or at EOF.
|
|
318
|
+
* `before` is an array of headings to try inserting before (first found wins).
|
|
319
|
+
*/
|
|
320
|
+
function insertSectionBefore(body, heading, content, before) {
|
|
321
|
+
const sectionBlock = `## ${heading}\n\n${content}`;
|
|
322
|
+
for (const target of before) {
|
|
323
|
+
const targetSection = findSection(body, target);
|
|
324
|
+
if (targetSection) {
|
|
325
|
+
const insertPoint = targetSection.start;
|
|
326
|
+
const textBefore = body.slice(0, insertPoint).replace(/\n+$/, '');
|
|
327
|
+
const textAfter = body.slice(insertPoint);
|
|
328
|
+
if (!textBefore)
|
|
329
|
+
return sectionBlock + '\n\n' + textAfter;
|
|
330
|
+
return textBefore + '\n\n' + sectionBlock + '\n\n' + textAfter;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
// No target found — append at EOF
|
|
334
|
+
const trimmed = body.replace(/\n+$/, '');
|
|
335
|
+
return trimmed + '\n\n' + sectionBlock;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Insert a new section after a target heading (and its content), or fall back to inserting before fallbacks.
|
|
339
|
+
*/
|
|
340
|
+
function insertSectionAfter(body, heading, content, after, beforeFallbacks) {
|
|
341
|
+
const afterSection = findSection(body, after);
|
|
342
|
+
if (afterSection) {
|
|
343
|
+
const insertPoint = afterSection.end;
|
|
344
|
+
const textBefore = body.slice(0, insertPoint).replace(/\n+$/, '');
|
|
345
|
+
const textAfter = body.slice(insertPoint).replace(/^\n+/, '');
|
|
346
|
+
const sectionBlock = `## ${heading}\n\n${content}`;
|
|
347
|
+
if (!textAfter)
|
|
348
|
+
return textBefore + '\n\n' + sectionBlock;
|
|
349
|
+
return textBefore + '\n\n' + sectionBlock + '\n\n' + textAfter;
|
|
350
|
+
}
|
|
351
|
+
// After-target not found — fall back to insertBefore logic
|
|
352
|
+
return insertSectionBefore(body, heading, content, beforeFallbacks);
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Match an action by case-insensitive substring against unchecked items.
|
|
356
|
+
* Throws on 0 or >1 matches.
|
|
357
|
+
*/
|
|
358
|
+
function matchAction(body, query) {
|
|
359
|
+
const section = findSection(body, 'Next Actions');
|
|
360
|
+
if (!section)
|
|
361
|
+
throw new Error(`No matching action for "${query}"`);
|
|
362
|
+
const normalizedQuery = normalizeForMatch(query).toLowerCase();
|
|
363
|
+
const matches = [];
|
|
364
|
+
const allActions = [];
|
|
365
|
+
let m;
|
|
366
|
+
const re = new RegExp(UNCHECKED_RE.source, 'gm');
|
|
367
|
+
while ((m = re.exec(section.content)) !== null) {
|
|
368
|
+
allActions.push(m[1]);
|
|
369
|
+
if (normalizeForMatch(m[1]).toLowerCase().includes(normalizedQuery)) {
|
|
370
|
+
matches.push(m[1]);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (matches.length === 0) {
|
|
374
|
+
throw new Error(`No matching action for "${query}". Actions: ${JSON.stringify(allActions)}`);
|
|
375
|
+
}
|
|
376
|
+
if (matches.length > 1)
|
|
377
|
+
throw new Error(`Ambiguous match for "${query}": ${matches.join(', ')}`);
|
|
378
|
+
return matches[0];
|
|
379
|
+
}
|
|
380
|
+
// --- Brief functions ---
|
|
381
|
+
function getBrief(body) {
|
|
382
|
+
const section = findSection(body, 'Brief');
|
|
383
|
+
if (!section)
|
|
384
|
+
return null;
|
|
385
|
+
return section.content || null;
|
|
386
|
+
}
|
|
387
|
+
function setBrief(body, content) {
|
|
388
|
+
const existing = findSection(body, 'Brief');
|
|
389
|
+
if (existing) {
|
|
390
|
+
// Replace existing brief content
|
|
391
|
+
const updated = removeSection(body, 'Brief');
|
|
392
|
+
return setBrief(updated, content);
|
|
393
|
+
}
|
|
394
|
+
// Insert before: Next Actions > Current State > Session Log > EOF
|
|
395
|
+
return insertSectionBefore(body, 'Brief', content, ['Next Actions', 'Current State', 'Session Log']);
|
|
396
|
+
}
|
|
397
|
+
function removeBrief(body) {
|
|
398
|
+
return removeSection(body, 'Brief');
|
|
399
|
+
}
|
|
400
|
+
// --- Next Actions functions ---
|
|
401
|
+
function getNextActions(body) {
|
|
402
|
+
const section = findSection(body, 'Next Actions');
|
|
403
|
+
if (!section)
|
|
404
|
+
return [];
|
|
405
|
+
const actions = [];
|
|
406
|
+
let m;
|
|
407
|
+
const re = new RegExp(UNCHECKED_RE.source, 'gm');
|
|
408
|
+
while ((m = re.exec(section.content)) !== null) {
|
|
409
|
+
actions.push(m[1]);
|
|
410
|
+
}
|
|
411
|
+
return actions;
|
|
412
|
+
}
|
|
413
|
+
function addNextAction(body, action) {
|
|
414
|
+
const section = findSection(body, 'Next Actions');
|
|
415
|
+
if (section) {
|
|
416
|
+
// Add the new item at the end of unchecked items (before any checked items)
|
|
417
|
+
const lines = section.content.split('\n');
|
|
418
|
+
const lastUncheckedIdx = lines.reduce((last, line, i) => line.startsWith('- [ ] ') ? i : last, -1);
|
|
419
|
+
const insertIdx = lastUncheckedIdx === -1 ? 0 : lastUncheckedIdx + 1;
|
|
420
|
+
lines.splice(insertIdx, 0, `- [ ] ${action}`);
|
|
421
|
+
const newContent = lines.join('\n');
|
|
422
|
+
const before = body.slice(0, section.start).replace(/\n+$/, '');
|
|
423
|
+
const after = body.slice(section.end).replace(/^\n+/, '');
|
|
424
|
+
const sectionBlock = `## Next Actions\n\n${newContent}`;
|
|
425
|
+
const parts = [before, sectionBlock, after].filter(Boolean);
|
|
426
|
+
return parts.join('\n\n');
|
|
427
|
+
}
|
|
428
|
+
// No section — insert after Brief if present, otherwise before Session Log
|
|
429
|
+
return insertSectionAfter(body, 'Next Actions', `- [ ] ${action}`, 'Brief', ['Session Log']);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Edit the matched action's line inside the Next Actions section only.
|
|
433
|
+
* Matching was always section-scoped; editing must be too — an identical
|
|
434
|
+
* checkbox line quoted elsewhere in the body (a Brief restating a blocker)
|
|
435
|
+
* is authored content, not an action. Works on the section's raw slice and
|
|
436
|
+
* on whole lines, so action text is never interpreted as a replacement
|
|
437
|
+
* pattern ($&, $', $1 survive byte-identical).
|
|
438
|
+
*/
|
|
439
|
+
function editActionLine(body, matched, edit) {
|
|
440
|
+
const section = findSection(body, 'Next Actions');
|
|
441
|
+
if (!section)
|
|
442
|
+
return body;
|
|
443
|
+
const lines = body.slice(section.headingEnd, section.end).split('\n');
|
|
444
|
+
const idx = lines.indexOf(`- [ ] ${matched}`);
|
|
445
|
+
if (idx === -1)
|
|
446
|
+
return body;
|
|
447
|
+
edit(lines, idx);
|
|
448
|
+
return body.slice(0, section.headingEnd) + lines.join('\n') + body.slice(section.end);
|
|
449
|
+
}
|
|
450
|
+
function completeNextAction(body, query) {
|
|
451
|
+
const matched = matchAction(body, query);
|
|
452
|
+
// Replace the unchecked item with checked
|
|
453
|
+
const updatedBody = editActionLine(body, matched, (lines, i) => { lines[i] = `- [x] ${matched}`; });
|
|
454
|
+
// Check if any unchecked items remain
|
|
455
|
+
const remaining = getNextActions(updatedBody);
|
|
456
|
+
if (remaining.length === 0) {
|
|
457
|
+
return { matched, body: clearNextActions(updatedBody) };
|
|
458
|
+
}
|
|
459
|
+
return { matched, body: updatedBody };
|
|
460
|
+
}
|
|
461
|
+
function removeNextAction(body, query) {
|
|
462
|
+
const matched = matchAction(body, query);
|
|
463
|
+
// Remove the line
|
|
464
|
+
const updatedBody = editActionLine(body, matched, (lines, i) => { lines.splice(i, 1); });
|
|
465
|
+
// Check if any unchecked items remain
|
|
466
|
+
const remaining = getNextActions(updatedBody);
|
|
467
|
+
if (remaining.length === 0) {
|
|
468
|
+
return { matched, body: clearNextActions(updatedBody) };
|
|
469
|
+
}
|
|
470
|
+
return { matched, body: updatedBody };
|
|
471
|
+
}
|
|
472
|
+
function clearNextActions(body) {
|
|
473
|
+
return removeSection(body, 'Next Actions');
|
|
474
|
+
}
|
|
475
|
+
// --- Related section sync ---
|
|
476
|
+
function syncRelatedSection(body, frontmatter) {
|
|
477
|
+
const targets = [];
|
|
478
|
+
if (frontmatter.parent)
|
|
479
|
+
targets.push({ slug: frontmatter.parent, label: 'parent' });
|
|
480
|
+
for (const slug of frontmatter.related ?? [])
|
|
481
|
+
targets.push({ slug });
|
|
482
|
+
if (targets.length === 0) {
|
|
483
|
+
// No frontmatter links — remove Related section if present
|
|
484
|
+
const existing = findSection(body, 'Related');
|
|
485
|
+
if (!existing)
|
|
486
|
+
return body;
|
|
487
|
+
const before = body.slice(0, existing.start).replace(/\n+$/, '');
|
|
488
|
+
const after = body.slice(existing.end).replace(/^\n+/, '');
|
|
489
|
+
return [before, after].filter(Boolean).join('\n\n');
|
|
490
|
+
}
|
|
491
|
+
// Strip existing Related section before checking coverage
|
|
492
|
+
let authoredBody = body;
|
|
493
|
+
const existingRelated = findSection(body, 'Related');
|
|
494
|
+
if (existingRelated) {
|
|
495
|
+
const before = body.slice(0, existingRelated.start);
|
|
496
|
+
const after = body.slice(existingRelated.end);
|
|
497
|
+
authoredBody = (before + after).replace(/\n{3,}/g, '\n\n');
|
|
498
|
+
}
|
|
499
|
+
// Find which targets are NOT mentioned in authored body
|
|
500
|
+
const mentionedSlugs = new Set();
|
|
501
|
+
let match;
|
|
502
|
+
const re = new RegExp(WIKI_LINK_RE.source, 'g');
|
|
503
|
+
while ((match = re.exec(authoredBody)) !== null) {
|
|
504
|
+
mentionedSlugs.add(match[1]);
|
|
505
|
+
}
|
|
506
|
+
const uncovered = targets.filter(t => !mentionedSlugs.has(t.slug));
|
|
507
|
+
if (uncovered.length === 0) {
|
|
508
|
+
// All covered inline — return body without Related section
|
|
509
|
+
return authoredBody.replace(/\n+$/, '');
|
|
510
|
+
}
|
|
511
|
+
// Build Related section
|
|
512
|
+
const lines = uncovered.map(t => t.label ? `- [[${t.slug}]] (${t.label})` : `- [[${t.slug}]]`);
|
|
513
|
+
const section = `## Related\n\n${lines.join('\n')}`;
|
|
514
|
+
// Append at end
|
|
515
|
+
return authoredBody.replace(/\n+$/, '') + '\n\n' + section;
|
|
516
|
+
}
|
|
517
|
+
return {
|
|
518
|
+
findNextStructuralHeading,
|
|
519
|
+
findSection,
|
|
520
|
+
splitStructural,
|
|
521
|
+
setSection,
|
|
522
|
+
appendToSection,
|
|
523
|
+
renameSection,
|
|
524
|
+
ensureSection,
|
|
525
|
+
prependSessionLogEntry,
|
|
526
|
+
bodyBeforeSessionLog,
|
|
527
|
+
countSessionLogEntries,
|
|
528
|
+
parseSessionLog,
|
|
529
|
+
extractOldestSessionLogEntry,
|
|
530
|
+
getBrief,
|
|
531
|
+
setBrief,
|
|
532
|
+
removeBrief,
|
|
533
|
+
getNextActions,
|
|
534
|
+
addNextAction,
|
|
535
|
+
completeNextAction,
|
|
536
|
+
removeNextAction,
|
|
537
|
+
clearNextActions,
|
|
538
|
+
syncRelatedSection,
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
// --- Entity-bound convenience exports (public API) ---
|
|
542
|
+
// getBrief / getNextActions / parseSessionLog are read-only accessors on
|
|
543
|
+
// entity bodies; they bind the entity Document (Brief, Next Actions, and
|
|
544
|
+
// Session Log only exist on entity kinds).
|
|
545
|
+
const ENTITY_UNION_DOC = documentFor({ kind: 'entity' });
|
|
546
|
+
export function getBrief(body) {
|
|
547
|
+
return ENTITY_UNION_DOC.getBrief(body);
|
|
548
|
+
}
|
|
549
|
+
export function getNextActions(body) {
|
|
550
|
+
return ENTITY_UNION_DOC.getNextActions(body);
|
|
551
|
+
}
|
|
552
|
+
export function parseSessionLog(body) {
|
|
553
|
+
return ENTITY_UNION_DOC.parseSessionLog(body);
|
|
554
|
+
}
|
|
555
|
+
export function countSessionLogEntries(body) {
|
|
556
|
+
return ENTITY_UNION_DOC.countSessionLogEntries(body);
|
|
557
|
+
}
|
|
558
|
+
// --- Summary derivation ---
|
|
559
|
+
/**
|
|
560
|
+
* One-line summary for a doc: frontmatter description first, then the first
|
|
561
|
+
* non-empty line of the kind's primary section (entity: Brief; knowledge:
|
|
562
|
+
* Synthesis, falling back to Goal for plans; session: Summary).
|
|
563
|
+
* Shared by the vault index and the OKF export so the two surfaces agree.
|
|
564
|
+
*/
|
|
565
|
+
export function deriveSummary(kind, fm, body) {
|
|
566
|
+
if (fm.description)
|
|
567
|
+
return fm.description;
|
|
568
|
+
const doc = documentFor({ kind });
|
|
569
|
+
if (kind === 'entity')
|
|
570
|
+
return firstNonEmptyLine(doc.getBrief(body));
|
|
571
|
+
if (kind === 'knowledge') {
|
|
572
|
+
const synthesis = doc.findSection(body, 'Synthesis');
|
|
573
|
+
if (synthesis?.content)
|
|
574
|
+
return firstNonEmptyLine(synthesis.content);
|
|
575
|
+
return firstNonEmptyLine(doc.findSection(body, 'Goal')?.content);
|
|
576
|
+
}
|
|
577
|
+
return firstNonEmptyLine(doc.findSection(body, 'Summary')?.content);
|
|
578
|
+
}
|