// settings.lookouts — saved lookouts (lane/lookout). A lookout is a saved set of // channels, a number of days back and an optional prompt; a run reads those // channels' recent transcripts and answers with video ideas. Pure: the types, // the defaults, the sanitizer settingsSchema.ts runs the block through, and the // per-field docs SETTINGS.md renders. // // OFF AND EMPTY BY DEFAULT, and no channel is named here or anywhere in code: a // lookout is configured on the machine that runs it (`pnpm ops settings`), so // adding one is a settings patch, never a code change. // // Slice 1 reads this for the selection only (controller/lookoutPlan.ts, the // `lookout-run` dry run); the model run, `afterSync` and the umtool page come in // later slices and read the same fields. import type { FieldDocs } from "./fieldDocs"; export type LookoutItem = { id: string; label: string; channels: string[]; days: number; prompt: string; maxVideos: number; minSeconds: number; timeZone: string; siteOrigin: string; }; export type LookoutsSettings = { enabled: boolean; afterSync: boolean; items: LookoutItem[]; }; export const DEFAULT_LOOKOUTS_SETTINGS: LookoutsSettings = { enabled: false, afterSync: false, items: [], }; export const LOOKOUT_DEFAULT_DAYS = 7; export const LOOKOUT_MAX_DAYS = 60; export const LOOKOUT_DEFAULT_MAX_VIDEOS = 40; export const LOOKOUT_MAX_MAX_VIDEOS = 500; export const LOOKOUT_DEFAULT_MIN_SECONDS = 90; export const LOOKOUT_MAX_MIN_SECONDS = 3600; export const LOOKOUT_MAX_PROMPT_CHARS = 4000; // A lookout id is a path segment (`transcripts/.lookouts//`), so it is a // lowercase slug, nothing else. export const LOOKOUT_ID_RE = /^[a-z0-9][a-z0-9-]{0,63}$/; // A channel slug as the channels tree names it (the citations' segment rule). const CHANNEL_SLUG_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/; export const LOOKOUTS_SETTINGS_FIELD_DOCS: FieldDocs = { enabled: "The lookouts' master switch. Off: no lookout runs, by hand or after a sync; a dry run " + "(`pnpm ops lookout-run --json '{\"id\":\"…\",\"dryRun\":true}'`) still lists what a lookout would read, " + "and writes nothing. Default false.", afterSync: "Run each lookout after a sync of one of its channels finishes — at most once a day per lookout, never while " + "render mode holds the machine. Not acted on before the after-sync slice lands. Default false.", items: "The saved lookouts, one entry each (`lookouts.items[]` below). The list is replaced whole by a settings " + "patch. Default [] — no lookout.", }; export const LOOKOUT_ITEM_FIELD_DOCS: FieldDocs = { id: "The lookout's id: a lowercase slug (`a-z`, `0-9`, `-`, at most 64), the name its runs and verdicts are kept " + "under (`transcripts/.lookouts//`). Omitted: made from `label`. An entry with neither is dropped, and so " + "is a second entry with an id already used.", label: "What the lookout is called on its page. Omitted: the id.", channels: "The channel slugs it reads (`transcripts/channels//`). A local media library channel, or a slug this " + "archive does not hold (a channel only on a remote site), is refused by name when the lookout runs.", days: "How many days back it reads, today included, by AIR date in `timeZone` (`release_timestamp`, else " + "`timestamp`, from the video's metadata — not yt-dlp's UTC `upload_date`). Clamped to [1, 60]; default 7.", prompt: "What the operator wants from the ideas, added to the grouping step only — the per-video reading is the same " + "for every lookout. At most 4000 characters; default \"\".", maxVideos: "The most recordings one run reads, after duplicate uploads and stream cut-downs are folded in: the longest " + "first, and every one left out listed as skipped for the cap. Clamped to [1, 500]; default 40.", minSeconds: "Videos shorter than this are not read (and are counted as skipped). Clamped to [0, 3600]; default 90.", timeZone: "The IANA time zone its days are counted in (\"America/New_York\"). \"\" = the machine's. An unknown zone is " + "dropped to \"\". Default \"\".", siteOrigin: "The public site its links are built on (\"https://example.pages.dev\"), when the channels are published; " + "\"\" = each video's source URL with its start second. Default \"\".", }; function clampInt(value: unknown, fallback: number, min: number, max: number): number { const n = typeof value === "number" ? value : typeof value === "string" && value.trim() ? Number(value) : NaN; if (!Number.isFinite(n)) return fallback; return Math.min(max, Math.max(min, Math.round(n))); } /** Whether `tz` is an IANA zone this runtime knows. "" is not a zone (it means the machine's). */ export function isKnownTimeZone(tz: string): boolean { if (!tz) return false; try { new Intl.DateTimeFormat("en-US", { timeZone: tz }); return true; } catch { return false; } } /** A lowercase slug made from free text: "My Lookout!" → "my-lookout". */ export function lookoutIdFrom(text: string): string { return text .normalize("NFKD") .replace(/[̀-ͯ]/g, "") .toLowerCase() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 64) .replace(/-+$/, ""); } function siteOriginOf(value: unknown): string { if (typeof value !== "string" || !value.trim()) return ""; try { const u = new URL(value.trim()); if (u.protocol !== "https:" && u.protocol !== "http:") return ""; return u.origin; } catch { return ""; } } /** One entry, coerced; null when it has no usable id. */ export function sanitizeLookoutItem(value: unknown): LookoutItem | null { if (!value || typeof value !== "object" || Array.isArray(value)) return null; const r = value as Record; const rawId = typeof r.id === "string" ? r.id.trim() : ""; const rawLabel = typeof r.label === "string" ? r.label.trim() : ""; const id = LOOKOUT_ID_RE.test(rawId) ? rawId : lookoutIdFrom(rawId || rawLabel); if (!LOOKOUT_ID_RE.test(id)) return null; const channels: string[] = []; if (Array.isArray(r.channels)) { for (const c of r.channels) { if (typeof c !== "string") continue; const slug = c.trim(); if (slug && slug !== ".." && CHANNEL_SLUG_RE.test(slug) && !channels.includes(slug)) channels.push(slug); } } const tz = typeof r.timeZone === "string" ? r.timeZone.trim() : ""; const prompt = typeof r.prompt === "string" ? r.prompt.trim().slice(0, LOOKOUT_MAX_PROMPT_CHARS) : ""; return { id, label: rawLabel || id, channels, days: clampInt(r.days, LOOKOUT_DEFAULT_DAYS, 1, LOOKOUT_MAX_DAYS), prompt, maxVideos: clampInt(r.maxVideos, LOOKOUT_DEFAULT_MAX_VIDEOS, 1, LOOKOUT_MAX_MAX_VIDEOS), minSeconds: clampInt(r.minSeconds, LOOKOUT_DEFAULT_MIN_SECONDS, 0, LOOKOUT_MAX_MIN_SECONDS), timeZone: isKnownTimeZone(tz) ? tz : "", siteOrigin: siteOriginOf(r.siteOrigin), }; } export function sanitizeLookouts(value: unknown): LookoutsSettings { const r = (value && typeof value === "object" && !Array.isArray(value) ? value : {}) as Record; const items: LookoutItem[] = []; if (Array.isArray(r.items)) { for (const raw of r.items) { const item = sanitizeLookoutItem(raw); if (item && !items.some((i) => i.id === item.id)) items.push(item); } } return { enabled: r.enabled === true, afterSync: r.afterSync === true, items }; } /** The saved lookout with this id, or null. */ export function lookoutById(settings: LookoutsSettings, id: string): LookoutItem | null { return settings.items.find((i) => i.id === id) ?? null; }