import { pool } from '../pool.js'; import { recordAudit } from './audit.js'; export async function create({ action_id, tier, params, agent_id, requested_by }) { const { rows: [r] } = await pool.query( `INSERT INTO agent_actions(action_id, tier, params, agent_id, requested_by) VALUES($1,$2,$3,$4,$5) RETURNING *`, [action_id, tier, params || {}, agent_id || null, requested_by || null] ); await recordAudit(requested_by, 'create', 'agent_action', r.id, null, r); return r; } export async function listPending({ limit = 100 } = {}) { const { rows } = await pool.query( `SELECT * FROM agent_actions WHERE status='pending' ORDER BY created_at LIMIT $1`, [limit]); return rows; } export async function getById(id) { const { rows: [r] } = await pool.query(`SELECT * FROM agent_actions WHERE id=$1`, [id]); return r; } export async function resolve(id, status, result, resolved_by) { const { rows: [r] } = await pool.query( `UPDATE agent_actions SET status=$1, result=$2, resolved_by=$3, resolved_at=now() WHERE id=$4 AND status='pending' RETURNING *`, [status, result || null, resolved_by || null, id]); if (r) await recordAudit(resolved_by, 'update', 'agent_action', id, null, r); return r; } export async function recent({ limit = 50 } = {}) { const { rows } = await pool.query( `SELECT * FROM agent_actions WHERE status<>'pending' ORDER BY resolved_at DESC NULLS LAST LIMIT $1`, [limit]); return rows; }