// Стратегия и правила автономности (BRM-53). Версии, согласования Татьяны, режимы, согласия, задачи. import fs from "node:fs"; import path from "node:path"; import crypto from "node:crypto"; export const SECTIONS = ["positioning", "audience", "channels", "kpi", "budget", "tone", "plan90"]; export const STATUSES = ["draft", "revision", "approved"]; export const MODES = ["draft", "each", "auto"]; // от строгого к свободному export const KINDS = ["channel", "rubric", "action"]; export const MUST_CONSENT = ["reposition", "new_account", "personal_fact", "letter_as_owner", "spend", "price", "contract", "delete_content"]; export const DECIDER = "Татьяна"; const hash = (text) => crypto.createHash("sha256").update(String(text ?? "")).digest("hex"); export function createStrategy(file) { let st; try { st = JSON.parse(fs.readFileSync(file, "utf8")); } catch { st = {}; } st.versions ??= []; st.rules ??= []; st.consents ??= []; st.tasks ??= []; const save = () => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, JSON.stringify(st, null, 2)); }; const cur = () => st.versions.at(-1) ?? null; const empty = (v) => SECTIONS.filter((s) => !String(v.sections[s] ?? "").trim()); const view = (v) => v && { n: v.n, status: v.status, sections: v.sections, comments: v.comments, history: v.history, approvedAt: v.approvedAt ?? null }; function edit(sections, by, now) { if (!sections || typeof sections !== "object") return { err: "err.strategy.sections" }; const keys = Object.keys(sections); if (!keys.length || keys.some((k) => !SECTIONS.includes(k))) return { err: "err.strategy.sections" }; let v = cur(); if (!v || v.status === "approved") { v = { n: st.versions.length + 1, status: "draft", sections: Object.fromEntries(SECTIONS.map((s) => [s, v?.sections[s] ?? ""])), comments: [], history: [] }; st.versions.push(v); v.history.push({ to: "draft", by: by || "", at: now }); } for (const k of keys) v.sections[k] = String(sections[k] ?? ""); save(); return { ok: true, version: view(v) }; } function decide({ action, comment, by }, now) { const v = cur(); if (!v || v.status === "approved") return { err: "err.strategy.nothingToDecide" }; if (by !== DECIDER) return { err: "err.strategy.notDecider" }; if (action === "approve") { const miss = empty(v); if (miss.length) return { err: "err.strategy.incomplete", missing: miss }; v.status = "approved"; v.approvedAt = now; v.history.push({ to: "approved", by, at: now }); for (const line of v.sections.plan90.split("\n").map((l) => l.trim()).filter(Boolean)) st.tasks.push({ id: st.tasks.length + 1, version: v.n, title: line, status: "todo", createdAt: now }); save(); return { ok: true, version: view(v) }; } if (action === "return") { const text = String(comment ?? "").trim(); if (!text) return { err: "err.strategy.commentRequired" }; v.comments.push({ text, by, at: now }); v.status = "revision"; v.history.push({ to: "revision", by, at: now }); save(); return { ok: true, version: view(v) }; } return { err: "err.action" }; } // Замечание Татьяны к текущей версии: уходит агенту, статус версии не меняет. function comment({ text, by }, now) { const v = cur(); if (!v) return { err: "err.strategy.nothingToDecide" }; const body = String(text ?? "").trim(); if (!body) return { err: "err.strategy.commentRequired" }; v.comments.push({ text: body, by: by || "", at: now }); save(); return { ok: true, version: view(v) }; } function setRule({ kind, key, mode }) { if (!KINDS.includes(kind) || !String(key ?? "").trim()) return { err: "err.strategy.rule" }; if (!MODES.includes(mode)) return { err: "err.strategy.mode" }; if (kind === "action" && MUST_CONSENT.includes(key) && mode === "auto") return { err: "err.strategy.mustConsent" }; st.rules = st.rules.filter((r) => !(r.kind === kind && r.key === key)); st.rules.push({ kind, key: String(key), mode }); save(); return { ok: true }; } const modeOf = (kind, key) => st.rules.find((r) => r.kind === kind && r.key === String(key))?.mode; function grant({ text, by }, now) { if (by !== DECIDER) return { err: "err.strategy.notDecider" }; if (!String(text ?? "").trim()) return { err: "err.strategy.consentText" }; if (!hasConsent(text)) st.consents.push({ hash: hash(text), by, at: now }); save(); return { ok: true }; } const hasConsent = (text) => st.consents.some((c) => c.hash === hash(text)); const isApproved = () => cur()?.status === "approved"; // Проверка для публикатора: причина отказа или null. function gate({ channel, rubric, action, text }) { if (!isApproved()) return "strategy-not-approved"; // канал без режима = «только черновик»; не заданные рубрика и действие ничего не меняют const modes = [modeOf("channel", channel) ?? "draft", modeOf("rubric", rubric), modeOf("action", action)].filter(Boolean); const mode = MODES.find((m) => modes.includes(m)); // самый строгий if (mode === "draft") return "mode-draft"; if ((mode === "each" || MUST_CONSENT.includes(action)) && !hasConsent(text)) return "no-consent"; return null; } return { edit, decide, comment, setRule, grant, gate, isApproved, current: () => view(cur()), versions: () => st.versions.map(view), rules: () => st.rules.slice(), tasks: () => st.tasks.slice(), meta: () => ({ sections: SECTIONS, statuses: STATUSES, modes: MODES, mustConsent: MUST_CONSENT, decider: DECIDER }), }; }