mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-14 18:17:29 +08:00
add trigger audit stats and expose background trigger visibility
This commit is contained in:
87
pkg/agent/trigger_audit.go
Normal file
87
pkg/agent/trigger_audit.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TriggerStats struct {
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
Counts map[string]int `json:"counts"`
|
||||
}
|
||||
|
||||
type triggerAudit struct {
|
||||
workspace string
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
type triggerEvent struct {
|
||||
Time string `json:"time"`
|
||||
Trigger string `json:"trigger"`
|
||||
Channel string `json:"channel"`
|
||||
Session string `json:"session"`
|
||||
Suppressed bool `json:"suppressed,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func newTriggerAudit(workspace string) *triggerAudit {
|
||||
return &triggerAudit{workspace: workspace}
|
||||
}
|
||||
|
||||
func (ta *triggerAudit) Record(trigger, channel, session string, suppressed bool, err error) {
|
||||
if ta == nil {
|
||||
return
|
||||
}
|
||||
trigger = normalizeTrigger(trigger)
|
||||
e := triggerEvent{
|
||||
Time: time.Now().UTC().Format(time.RFC3339),
|
||||
Trigger: trigger,
|
||||
Channel: channel,
|
||||
Session: session,
|
||||
Suppressed: suppressed,
|
||||
}
|
||||
if err != nil {
|
||||
e.Error = err.Error()
|
||||
}
|
||||
|
||||
ta.mu.Lock()
|
||||
defer ta.mu.Unlock()
|
||||
|
||||
memDir := filepath.Join(ta.workspace, "memory")
|
||||
_ = os.MkdirAll(memDir, 0755)
|
||||
|
||||
logPath := filepath.Join(memDir, "trigger-audit.jsonl")
|
||||
if data, mErr := json.Marshal(e); mErr == nil {
|
||||
f, oErr := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if oErr == nil {
|
||||
_, _ = f.Write(append(data, '\n'))
|
||||
_ = f.Close()
|
||||
}
|
||||
}
|
||||
|
||||
statsPath := filepath.Join(memDir, "trigger-stats.json")
|
||||
stats := TriggerStats{Counts: map[string]int{}}
|
||||
if raw, rErr := os.ReadFile(statsPath); rErr == nil {
|
||||
_ = json.Unmarshal(raw, &stats)
|
||||
if stats.Counts == nil {
|
||||
stats.Counts = map[string]int{}
|
||||
}
|
||||
}
|
||||
stats.Counts[trigger]++
|
||||
stats.UpdatedAt = e.Time
|
||||
if raw, mErr := json.MarshalIndent(stats, "", " "); mErr == nil {
|
||||
_ = os.WriteFile(statsPath, raw, 0644)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeTrigger(v string) string {
|
||||
s := strings.ToLower(strings.TrimSpace(v))
|
||||
if s == "" {
|
||||
return "user"
|
||||
}
|
||||
return s
|
||||
}
|
||||
Reference in New Issue
Block a user