包文件→ .shtukbase/packages/taskstart/hooks/brief.ts
hooks/brief.ts
58 行, 3.0 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // TS-R01 (SessionStart). The agent's first context in the project: what the project is, which packages stand, the
- // norms that do not let through (MUST, mode enforce), the work in progress and the questions still open for the owner.
- // Its stdout is added to the agent's context by Claude Code. Short on purpose: names and paths, not page bodies.
- import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
- import { join } from 'node:path';
- import { parseYaml, pystr, splitFrontMatter } from '../../collections/checks/lib/pages.ts';
- import { projectRoot, readPayload } from '../../hooks/lib/hooklib.ts';
- const MAX = 25;
- const { p } = readPayload();
- if (!p) process.exit(0);
- const root = projectRoot(p);
- const head = (file: string) => parseYaml(splitFrontMatter(readFileSync(file, 'utf8'))[0]);
- /** Records of a folder (recursively): [address, front matter]. */
- function records(dir: string): [string, Record<string, any>][] {
- const out: [string, Record<string, any>][] = [];
- const walk = (rel: string) => {
- const full = join(root, rel);
- if (!existsSync(full)) return;
- for (const n of readdirSync(full).sort()) {
- if (n.startsWith('.')) continue;
- const r = `${rel}/${n}`;
- if (statSync(join(root, r)).isDirectory()) walk(r);
- else if (n.endsWith('.md')) out.push([`/${r}`, head(join(root, r))]);
- }
- };
- walk(dir);
- return out;
- }
- const st = (h: Record<string, any>) => pystr(h.status ?? '').trim().toLowerCase();
- const cap = (xs: string[]) => (xs.length > MAX ? [...xs.slice(0, MAX), `… and ${xs.length - MAX} more`] : xs);
- const lines: string[] = [];
- let lock: Record<string, any> = {};
- try {
- lock = (Bun.YAML.parse(readFileSync(join(root, '.shtukbase', 'packages.lock'), 'utf8')) as Record<string, any>) ?? {};
- } catch {}
- const pkgs = Object.entries(lock.packages ?? {}).map(([id, v]: [string, any]) => `${id} ${v?.version ?? ''}`.trim());
- lines.push(`Project "${lock.name ?? lock.project ?? root}" (Shtukbase). Packages: ${pkgs.join(', ') || 'none'}.`);
- lines.push('Norms are the pages in /norms/rules; the check of all norms is shtuk check. Write collection records with a head and a title.');
- const must = records('norms/rules').filter(([, h]) => st(h) === 'active' && pystr(h.level) === 'MUST' && pystr(h.mode) === 'enforce');
- if (must.length) {
- lines.push('', `Enforcing norms (MUST), ${must.length}:`);
- lines.push(...cap(must.map(([a, h]) => `- ${pystr(h.ruleId ?? a)}: ${pystr(h.requirement ?? h.title ?? '')}`)));
- }
- const doing = records('work/tasks').filter(([, h]) => st(h) === 'doing');
- if (doing.length) {
- lines.push('', `Tasks in progress, ${doing.length}:`);
- lines.push(...cap(doing.map(([a, h]) => `- ${a}: ${pystr(h.title ?? '')}`)));
- }
- const open = records('knowledge/questions').filter(([, h]) => st(h) === 'open');
- if (open.length) {
- lines.push('', `Open questions to the owner, ${open.length} (do not decide for the owner, the answer goes into the question record):`);
- lines.push(...cap(open.map(([a, h]) => `- ${a}: ${pystr(h.title ?? '')}`)));
- }
- console.log(lines.join('\n'));