Отключение агента менеджера: на время и до команды владельца (миграция 017, API pause/resume, события, шаг 2 и приём сообщений); тесты; docs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
63
packages/khbuild_assistant_prep/src/pause.ts
Normal file
63
packages/khbuild_assistant_prep/src/pause.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import type { Pool } from "khb_common";
|
||||
|
||||
/** Условие 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 interface PauseState {
|
||||
paused: boolean;
|
||||
/** true - отключён до команды владельца (без срока). */
|
||||
indefinite: boolean;
|
||||
until: Date | null;
|
||||
reason: string | null;
|
||||
by: string | null;
|
||||
}
|
||||
|
||||
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`,
|
||||
[managerId],
|
||||
);
|
||||
const r = rows[0];
|
||||
if (!r) return { paused: false, 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 };
|
||||
}
|
||||
|
||||
export interface PauseOptions {
|
||||
/** Срок отключения; null/не задан - до команды владельца. */
|
||||
until?: Date | null;
|
||||
reason?: string | null;
|
||||
by?: string | null;
|
||||
}
|
||||
|
||||
const SET_PAUSE = `update construction_manager
|
||||
set agent_paused = $2, paused_until = $3, pause_reason = $4, paused_by = $5, paused_at = now()`;
|
||||
|
||||
/** Отключает агента менеджера: на время (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]);
|
||||
return (r.rowCount ?? 0) > 0;
|
||||
}
|
||||
|
||||
/** Включает агента менеджера обратно. */
|
||||
export async function resumeManager(pool: Pool, managerId: string): Promise<boolean> {
|
||||
const r = await pool.query(
|
||||
"update construction_manager set agent_paused = false, paused_until = null, pause_reason = null, paused_by = null, paused_at = null where person_id = $1",
|
||||
[managerId],
|
||||
);
|
||||
return (r.rowCount ?? 0) > 0;
|
||||
}
|
||||
|
||||
/** Отключает агентов всех активных менеджеров. Возвращает число менеджеров. */
|
||||
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],
|
||||
);
|
||||
return r.rowCount ?? 0;
|
||||
}
|
||||
|
||||
export async function resumeAll(pool: Pool): Promise<number> {
|
||||
const r = await pool.query("update construction_manager set agent_paused = false, paused_until = null, pause_reason = null, paused_by = null, paused_at = null where agent_paused or paused_until is not null");
|
||||
return r.rowCount ?? 0;
|
||||
}
|
||||
Reference in New Issue
Block a user