Режим passive при отключении агента: молчит по своей инициативе, на сообщения менеджера отвечает как обычно (миграция 018); off - вообще ничего; тесты; docs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
import type { Pool } from "khb_common";
|
||||
|
||||
/** Условие SQL: агент не в режиме off (passive и включённый - шаг 2 и ответы идут). */
|
||||
export const NOT_OFF_SQL = (alias = "cm"): string => `(${alias}.pause_mode = 'passive' or (not ${alias}.agent_paused and (${alias}.paused_until is null or ${alias}.paused_until <= now())))`;
|
||||
|
||||
/** Условие SQL: агент менеджера включён (не отключён бессрочно и срок отключения истёк). */
|
||||
export const NOT_PAUSED_SQL = (alias = "cm"): string => `(not ${alias}.agent_paused and (${alias}.paused_until is null or ${alias}.paused_until <= now()))`;
|
||||
|
||||
export type PauseMode = "passive" | "off";
|
||||
|
||||
export interface PauseState {
|
||||
paused: boolean;
|
||||
/** passive - молчит, пока ему не пишут (на сообщения отвечает как обычно); off - ничего не делает. */
|
||||
mode: PauseMode;
|
||||
/** true - отключён до команды владельца (без срока). */
|
||||
indefinite: boolean;
|
||||
until: Date | null;
|
||||
@@ -14,13 +21,13 @@ export interface PauseState {
|
||||
|
||||
export async function pauseState(pool: Pool, managerId: string): Promise<PauseState> {
|
||||
const { rows } = await pool.query(
|
||||
`select (not ${NOT_PAUSED_SQL("cm")}) as paused, cm.agent_paused, cm.paused_until, cm.pause_reason, cm.paused_by from construction_manager cm where cm.person_id = $1`,
|
||||
`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],
|
||||
);
|
||||
const r = rows[0];
|
||||
if (!r) return { paused: false, indefinite: false, until: null, reason: null, by: null };
|
||||
if (!r) return { paused: false, mode: "passive", indefinite: false, until: null, reason: null, by: null };
|
||||
const paused = r.paused === true;
|
||||
return { paused, 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 };
|
||||
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 };
|
||||
}
|
||||
|
||||
export interface PauseOptions {
|
||||
@@ -28,14 +35,16 @@ export interface PauseOptions {
|
||||
until?: Date | null;
|
||||
reason?: string | null;
|
||||
by?: string | null;
|
||||
/** passive (по умолчанию) - молчит, пока ему не пишут; off - вообще ничего. */
|
||||
mode?: PauseMode;
|
||||
}
|
||||
|
||||
const SET_PAUSE = `update construction_manager
|
||||
set agent_paused = $2, paused_until = $3, pause_reason = $4, paused_by = $5, paused_at = now()`;
|
||||
set agent_paused = $2, paused_until = $3, pause_reason = $4, paused_by = $5, paused_at = now(), pause_mode = $6`;
|
||||
|
||||
/** Отключает агента менеджера: на время (until) или до команды (until не задан). Возвращает false, если менеджера нет. */
|
||||
export async function setPause(pool: Pool, managerId: string, o: PauseOptions = {}): Promise<boolean> {
|
||||
const r = await pool.query(`${SET_PAUSE} where person_id = $1`, [managerId, !o.until, o.until ?? null, o.reason ?? null, o.by ?? null]);
|
||||
const r = await pool.query(`${SET_PAUSE} where person_id = $1`, [managerId, !o.until, o.until ?? null, o.reason ?? null, o.by ?? null, o.mode ?? "passive"]);
|
||||
return (r.rowCount ?? 0) > 0;
|
||||
}
|
||||
|
||||
@@ -51,8 +60,8 @@ export async function resumeManager(pool: Pool, managerId: string): Promise<bool
|
||||
/** Отключает агентов всех активных менеджеров. Возвращает число менеджеров. */
|
||||
export async function setPauseAll(pool: Pool, o: PauseOptions = {}): Promise<number> {
|
||||
const r = await pool.query(
|
||||
"update construction_manager set agent_paused = $1, paused_until = $2, pause_reason = $3, paused_by = $4, paused_at = now() where is_active",
|
||||
[!o.until, o.until ?? null, o.reason ?? null, o.by ?? null],
|
||||
"update construction_manager set agent_paused = $1, paused_until = $2, pause_reason = $3, paused_by = $4, paused_at = now(), pause_mode = $5 where is_active",
|
||||
[!o.until, o.until ?? null, o.reason ?? null, o.by ?? null, o.mode ?? "passive"],
|
||||
);
|
||||
return r.rowCount ?? 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user