Календарь пассивных окон по дням недели (вторник весь день по Тампе): агенты в режиме passive; API; тесты; docs
All checks were successful
ci / test (push) Successful in 26s
ci / deploy (push) Successful in 1m33s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-19 21:13:38 +03:00
parent c59ac87486
commit 2a92458106
8 changed files with 166 additions and 4 deletions

View File

@@ -8,6 +8,39 @@ export const NOT_PAUSED_SQL = (alias = "cm"): string => `(not ${alias}.agent_pau
export type PauseMode = "passive" | "off";
/** Пассивное окно календаря: день недели (ISO, 1 - понедельник) и интервал в минутах от полуночи в часовом поясе окна. */
export interface PassiveWindow {
id?: string;
weekday: number;
fromMinute: number;
toMinute: number;
timezone: string;
note: string | null;
}
/** День недели (ISO) и минуты от полуночи момента `now` в часовом поясе. */
export function localWeekdayMinute(now: Date, timezone: string): { weekday: number; minute: number } {
const parts = new Intl.DateTimeFormat("en-US", { timeZone: timezone, weekday: "short", hour: "2-digit", minute: "2-digit", hourCycle: "h23" }).formatToParts(now);
const get = (t: string) => parts.find((x) => x.type === t)?.value ?? "";
const weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].indexOf(get("weekday")) + 1;
return { weekday, minute: Number(get("hour")) * 60 + Number(get("minute")) };
}
export const inWindow = (w: PassiveWindow, now: Date): boolean => {
const l = localWeekdayMinute(now, w.timezone);
return l.weekday === w.weekday && l.minute >= w.fromMinute && l.minute < w.toMinute;
};
export async function loadWindows(pool: Pool): Promise<PassiveWindow[]> {
const { rows } = await pool.query("select id, weekday, from_minute, to_minute, timezone, note from agent_passive_window where is_active order by weekday, from_minute");
return rows.map((r) => ({ id: String(r.id), weekday: Number(r.weekday), fromMinute: Number(r.from_minute), toMinute: Number(r.to_minute), timezone: String(r.timezone), note: (r.note as string | null) ?? null }));
}
/** Пассивное окно календаря, действующее сейчас (null - нет). */
export async function activeWindow(pool: Pool, now: Date = new Date()): Promise<PassiveWindow | null> {
return (await loadWindows(pool)).find((w) => inWindow(w, now)) ?? null;
}
export interface PauseState {
paused: boolean;
/** passive - молчит, пока ему не пишут (на сообщения отвечает как обычно); off - ничего не делает. */
@@ -19,7 +52,7 @@ export interface PauseState {
by: string | null;
}
export async function pauseState(pool: Pool, managerId: string): Promise<PauseState> {
export async function pauseState(pool: Pool, managerId: string, now: Date = new Date()): Promise<PauseState> {
const { rows } = await pool.query(
`select (not ${NOT_PAUSED_SQL("cm")}) as paused, cm.pause_mode, cm.agent_paused, cm.paused_until, cm.pause_reason, cm.paused_by from construction_manager cm where cm.person_id = $1`,
[managerId],
@@ -27,6 +60,11 @@ export async function pauseState(pool: Pool, managerId: string): Promise<PauseSt
const r = rows[0];
if (!r) return { paused: false, mode: "passive", indefinite: false, until: null, reason: null, by: null };
const paused = r.paused === true;
if (!paused) {
// календарь пассивных окон (например, вторник): агент в режиме passive у всех менеджеров
const w = await activeWindow(pool, now);
if (w) return { paused: true, mode: "passive", indefinite: false, until: null, reason: `календарь: ${w.note ?? "пассивное окно"}`, by: "calendar" };
}
return { paused, mode: r.pause_mode === "off" ? "off" : "passive", indefinite: paused && r.agent_paused === true, until: paused && !r.agent_paused ? (r.paused_until as Date) : null, reason: paused ? (r.pause_reason as string | null) : null, by: paused ? (r.paused_by as string | null) : null };
}