Календарь пассивных окон по дням недели (вторник весь день по Тампе): агенты в режиме passive; API; тесты; docs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import type { Pool } from "khb_common";
|
||||
import { loadMemory, saveMemory } from "./agents/session.js";
|
||||
import { NOT_PAUSED_SQL, pauseState, resumeAll, resumeManager, setPause, setPauseAll } from "./pause.js";
|
||||
import { activeWindow, loadWindows, NOT_PAUSED_SQL, pauseState, resumeAll, resumeManager, setPause, setPauseAll } from "./pause.js";
|
||||
|
||||
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
const clampLimit = (v: unknown, def: number, max: number): number => {
|
||||
@@ -94,6 +94,41 @@ export function registerAgentRoutes(app: FastifyInstance, pool: Pool): void {
|
||||
|
||||
app.post("/agents/resume-all", async () => ({ ok: true, managers: await resumeAll(pool) }));
|
||||
|
||||
/** Календарь пассивных окон по дням недели: в это время все агенты в режиме passive. */
|
||||
app.get("/agents/passive-windows", async () => ({ windows: await loadWindows(pool), activeNow: await activeWindow(pool) }));
|
||||
|
||||
const WEEKDAYS: Record<string, number> = { mon: 1, tue: 2, wed: 3, thu: 4, fri: 5, sat: 6, sun: 7 };
|
||||
const hhmm = (v: unknown, def: number): number | null => {
|
||||
if (v === undefined || v === null || v === "") return def;
|
||||
const m = String(v).match(/^(\d{1,2}):(\d{2})$/);
|
||||
if (!m) return null;
|
||||
const x = Number(m[1]) * 60 + Number(m[2]);
|
||||
return x >= 0 && x <= 1440 ? x : null;
|
||||
};
|
||||
/** Добавить пассивное окно: weekday (1-7 или mon..sun), from/to ("HH:MM", по умолчанию весь день), timezone, note. */
|
||||
app.post<{ Body: { weekday?: string | number; from?: string; to?: string; timezone?: string; note?: string } }>("/agents/passive-windows", async (req, reply) => {
|
||||
const b = req.body ?? {};
|
||||
const wd = typeof b.weekday === "number" ? b.weekday : (WEEKDAYS[String(b.weekday ?? "").slice(0, 3).toLowerCase()] ?? Number(b.weekday));
|
||||
const from = hhmm(b.from, 0);
|
||||
const to = hhmm(b.to, 1440);
|
||||
if (!(wd >= 1 && wd <= 7)) return reply.code(400).send({ error: "weekday: 1-7 (1 - понедельник) или mon..sun" });
|
||||
if (from === null || to === null || from >= to || from > 1439) return reply.code(400).send({ error: "from/to: время HH:MM, from меньше to (to может быть 24:00)" });
|
||||
const tz = b.timezone ?? "America/New_York";
|
||||
try {
|
||||
new Intl.DateTimeFormat("en-US", { timeZone: tz });
|
||||
} catch {
|
||||
return reply.code(400).send({ error: "timezone: неизвестный часовой пояс" });
|
||||
}
|
||||
const { rows } = await pool.query("insert into agent_passive_window (weekday, from_minute, to_minute, timezone, note) values ($1,$2,$3,$4,$5) returning id", [wd, from, to, tz, b.note ?? null]);
|
||||
return { ok: true, id: rows[0].id };
|
||||
});
|
||||
|
||||
app.delete<{ Params: { wid: string } }>("/agents/passive-windows/:wid", async (req, reply) => {
|
||||
if (!UUID.test(req.params.wid)) return reply.code(404).send({ error: "окно не найдено" });
|
||||
const r = await pool.query("delete from agent_passive_window where id = $1", [req.params.wid]);
|
||||
return r.rowCount ? { ok: true } : reply.code(404).send({ error: "окно не найдено" });
|
||||
});
|
||||
|
||||
app.get<{ Params: { id: string } }>("/agents/:id/memory", async (req, reply) => {
|
||||
const m = await knownManager(req.params.id);
|
||||
if (!m) return reply.code(404).send({ error: "менеджер не найден" });
|
||||
|
||||
Reference in New Issue
Block a user