包文件→ .shtukbase/packages/content/hooks/approve-guard.ts
hooks/approve-guard.ts
31 行, 1.9 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // CNT-R01 (PreToolUse on Write, Edit, MultiEdit). In /content/drafts and /content/ideas an agent may write the text
- // and the editor's fields, but not what a person decides: the states approved and published, and the fields
- // approvedBy, approvedOn, publishedOn, url stay as they were.
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { parseYaml, pystr, splitFrontMatter } from '../../collections/checks/lib/pages.ts';
- import { BLOCK, contentAfter, projectPath, projectRoot, readPayload } from '../../hooks/lib/hooklib.ts';
- const FOLDERS = ['content/drafts/', 'content/ideas/'];
- const PERSON_FIELDS = ['approvedBy', 'approvedOn', 'publishedOn', 'url'];
- const PERSON_STATES = ['approved', 'published'];
- const { p } = readPayload();
- if (!p) process.exit(0);
- const root = projectRoot(p);
- const rel = projectPath(root, p.tool_input?.file_path);
- if (!rel || !FOLDERS.some((f) => rel.startsWith(f))) process.exit(0);
- const after = contentAfter(root, String(p.tool_name ?? ''), p.tool_input ?? {});
- if (after === null) process.exit(0);
- const fmOf = (t: string) => parseYaml(splitFrontMatter(t)[0]);
- const v = (x: unknown) => pystr(x ?? '').trim();
- const st = (x: unknown) => v(x).replace(/^\/+|\/+$/g, '').split('/').pop()!.toLowerCase();
- const before = existsSync(join(root, rel)) ? fmOf(readFileSync(join(root, rel), 'utf8')) : {};
- const now = fmOf(after);
- const changed = PERSON_FIELDS.filter((f) => v(before[f]) !== v(now[f]));
- const state = st(now.status);
- if (PERSON_STATES.includes(state) && st(before.status) !== state) changed.push(`status: ${state}`);
- if (changed.length) {
- process.stderr.write(`Not written (norm CNT-R01): ${changed.join(', ')} in ${rel} is set only by a person. Leave it as it was; the text and the editor fields (verdict, editorNotes) may be written, an agent sets only the states draft and review on a draft and drafted on an idea.\n`);
- process.exit(BLOCK);
- }