package users import ( "database/sql" "errors" "time" "supermarket/internal/db" ) // An agent's limits: calls a minute, things sent out a day, and whether // it's paused. Agents without a row have the defaults. // Default limits. const ( DefaultPerMinute = 60 DefaultOutPerDay = 50 ) // Limits are an agent's. type Limits struct { PerMinute int OutPerDay int Paused time.Time // zero when running PausedBy int64 } // LimitsOf are an agent's limits. func LimitsOf(d *db.DB, agent int64) (Limits, error) { l := Limits{PerMinute: DefaultPerMinute, OutPerDay: DefaultOutPerDay} var paused string err := d.QueryRow(`SELECT per_minute, out_per_day, paused_at, paused_by FROM agent_limits WHERE agent_id = ?`, agent). Scan(&l.PerMinute, &l.OutPerDay, &paused, &l.PausedBy) if errors.Is(err, sql.ErrNoRows) { return l, nil } l.Paused = parseTS(paused) return l, err } // SetLimits changes an agent's calls a minute and sends a day (each at // least 1). func SetLimits(d *db.DB, agent int64, perMinute, outPerDay int) error { if perMinute < 1 || outPerDay < 1 { return errors.New("limits are at least 1") } _, err := d.Exec(`INSERT INTO agent_limits (agent_id, per_minute, out_per_day) VALUES (?, ?, ?) ON CONFLICT (agent_id) DO UPDATE SET per_minute = excluded.per_minute, out_per_day = excluded.out_per_day`, agent, perMinute, outPerDay) return err } // Pause stops all of an agent's calls (by: who), or lets them go on. func Pause(d *db.DB, agent int64, paused bool, by int64, now time.Time) error { at := "" if paused { at = ts(now) } _, err := d.Exec(`INSERT INTO agent_limits (agent_id, per_minute, out_per_day, paused_at, paused_by) VALUES (?, ?, ?, ?, ?) ON CONFLICT (agent_id) DO UPDATE SET paused_at = excluded.paused_at, paused_by = excluded.paused_by`, agent, DefaultPerMinute, DefaultOutPerDay, at, by) return err } // Call is one tool call an agent made. type Call struct { ID int64 Agent int64 At time.Time Tool string Outcome string // ok, error, refused Detail string Took time.Duration } // Outcomes. const ( CallOK = "ok" CallError = "error" CallRefused = "refused" ) // RecordCall notes a tool call. func RecordCall(d *db.DB, c Call) error { if r := []rune(c.Detail); len(r) > 300 { c.Detail = string(r[:299]) + "…" } _, err := d.Exec(`INSERT INTO agent_calls (agent_id, at, tool, outcome, detail, ms) VALUES (?, ?, ?, ?, ?, ?)`, c.Agent, ts(c.At), c.Tool, c.Outcome, c.Detail, c.Took.Milliseconds()) return err } // CallsSince counts an agent's calls since a time, not counting those // refused over the rate. func CallsSince(d *db.DB, agent int64, since time.Time) (int, error) { var n int err := d.QueryRow(`SELECT count(*) FROM agent_calls WHERE agent_id = ? AND at >= ? AND outcome <> ?`, agent, ts(since), CallRefused).Scan(&n) return n, err } // RecentCalls are an agent's last calls, newest first. func RecentCalls(d *db.DB, agent int64, limit int) ([]Call, error) { rows, err := d.Query(`SELECT id, agent_id, at, tool, outcome, detail, ms FROM agent_calls WHERE agent_id = ? ORDER BY id DESC LIMIT ?`, agent, limit) if err != nil { return nil, err } defer rows.Close() var out []Call for rows.Next() { var c Call var at string var ms int64 if err := rows.Scan(&c.ID, &c.Agent, &at, &c.Tool, &c.Outcome, &c.Detail, &ms); err != nil { return nil, err } c.At, c.Took = parseTS(at), time.Duration(ms)*time.Millisecond out = append(out, c) } return out, rows.Err() } // PruneCalls forgets calls older than 30 days. func PruneCalls(d *db.DB, now time.Time) error { _, err := d.Exec(`DELETE FROM agent_calls WHERE at < ?`, ts(now.Add(-30*24*time.Hour))) return err }