archivo del paquete→ .shtukbase/packages/agents/lib/factlib.ts
lib/factlib.ts
112 líneas, 4.6 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // The shared part of the factory packages (agents, automations, runs, alarms): record types, field helpers,
- // times, links that must lead to a page, and files that must exist. Read-only, Bun only, no dependencies.
- // The parsing of pages is the library of the package collections; the packages runs, automations and alarms
- // require agents and import this file from `.shtukbase/packages/agents/lib/factlib.ts`.
- import { existsSync, statSync } from 'node:fs';
- import { dirname, isAbsolute, join, resolve } from 'node:path';
- import { type Project, type Rec, TYPES, TYPE_LABEL, normPath, pystr } from '../../collections/checks/lib/speclib.ts';
- export * from '../../collections/checks/lib/speclib.ts';
- Object.assign(TYPES, {
- AGENT: ['Worker', 'Assistant', 'Specialist'],
- FACTION: ['Action'],
- AUTOMATION: ['Automation'],
- RUN: ['Run'],
- ALARM: ['Alarm'],
- });
- Object.assign(TYPE_LABEL, {
- AGENT: 'agents', FACTION: 'actions', AUTOMATION: 'automations', RUN: 'runs', ALARM: 'alarms',
- });
- /** Page types an agent description may have (the node reads the same three). */
- export const AGENT_TYPES = ['Worker', 'Assistant', 'Specialist'];
- /** Is the value filled: a list with a non-blank item, a map with a key, or a non-blank scalar. */
- export function filled(v: unknown): boolean {
- if (v === null || v === undefined || v === false) return false;
- if (Array.isArray(v)) return v.some((x) => filled(x));
- if (v instanceof Date) return true;
- if (typeof v === 'object') return Object.keys(v as object).length > 0;
- return pystr(v).trim() !== '';
- }
- /** Is the field of the record filled. */
- export function has(rec: Rec, field: string): boolean {
- return filled(rec.fm[field]);
- }
- /** A scalar as trimmed text ('' for nothing). */
- export function text(v: unknown): string {
- if (v === null || v === undefined) return '';
- if (v instanceof Date) return v.toISOString();
- return pystr(v).trim();
- }
- /** A map value or null. */
- export function map(v: unknown): Record<string, any> | null {
- return v && typeof v === 'object' && !Array.isArray(v) && !(v instanceof Date) ? (v as Record<string, any>) : null;
- }
- /** An ISO date or date-time → milliseconds, or null when it is not one. A bare date is midnight UTC. */
- export function time(v: unknown): number | null {
- if (v instanceof Date) return v.getTime();
- const s = text(v);
- if (!/^\d{4}-\d{2}-\d{2}([T ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?)?$/.test(s)) return null;
- const iso = s.length === 10 ? s + 'T00:00:00Z' : s.replace(' ', 'T');
- const ms = Date.parse(/(Z|[+-]\d{2}:?\d{2})$/.test(iso) ? iso : iso + 'Z');
- return Number.isNaN(ms) ? null : ms;
- }
- /** A positive whole number, or null. */
- export function count(v: unknown): number | null {
- const n = typeof v === 'number' ? v : /^\d+$/.test(text(v)) ? Number(text(v)) : NaN;
- return Number.isInteger(n) && n > 0 ? n : null;
- }
- /** A link value (path, `[[x]]`, `[t](x)`) → the project page `/a/b.md` it names, or null. */
- export function target(rec: Rec, v: unknown): string | null {
- return normPath(v, null, rec.id);
- }
- /** Does the project page exist (`/a/b.md`, a folder page `/a/b/index.md` counts too). */
- export function pageExists(p: Project, page: string): boolean {
- const f = join(p.root, page);
- return existsSync(f) || existsSync(f.replace(/\.md$/, '/index.md'));
- }
- /** The nearest folder at or above `from` that holds `.git`, or null. */
- export function repoRoot(from: string): string | null {
- let d = resolve(from);
- for (;;) {
- if (existsSync(join(d, '.git'))) return d;
- const up = dirname(d);
- if (up === d) return null;
- d = up;
- }
- }
- /** A file path written on a page: from the project root, then from the repository root above it. The found path,
- * or null. A leading `/` means the project root. */
- export function findFile(p: Project, path: string): string | null {
- const rel = path.trim().replace(/^\.\//, '');
- if (!rel) return null;
- if (isAbsolute(rel) && !rel.startsWith('/')) return existsSync(rel) ? rel : null;
- const bases = [p.root, repoRoot(p.root)].filter((x): x is string => !!x);
- for (const b of bases) {
- const f = join(b, rel.replace(/^\/+/, ''));
- if (existsSync(f) && statSync(f).isFile()) return f;
- }
- return null;
- }
- /** The phase name of a status value (`/runs/contours/runs/done` → `done`), lower case; '' when empty. */
- export function phase(v: unknown): string {
- const s = text(v).replace(/^\/+|\/+$/g, '');
- return s.split('/').pop()!.replace(/\.md$/, '').toLowerCase();
- }
- /** Records of a type key and the address of its collection; an empty list when the project has no such collection. */
- export function recordsOf(p: Project, key: string): Rec[] {
- return p.coll(key) ? p.records(key) : [];
- }