Files
agent-dashboard/internal/hook/remote.go
2026-06-02 14:35:30 +08:00

80 lines
1.8 KiB
Go

package hook
import (
"context"
"strings"
"time"
"github.com/longbin/agent-notify/internal/config"
"github.com/longbin/agent-notify/internal/hostmeta"
"github.com/longbin/agent-notify/internal/logx"
"github.com/longbin/agent-notify/internal/remote"
"github.com/longbin/agent-notify/internal/transcript"
)
var reportRemoteHook = reportRemote
func reportRemote(cfg config.Config, agent, event, cwd string, transcriptPath, conversationID string) {
if !cfg.Remote.Enabled || cfg.Remote.URL == "" {
return
}
hostname, err := hostmeta.Hostname()
if err != nil {
logx.Append("remote report hostname: %v", err)
hostname = ""
}
ips := hostmeta.IPs()
var lastUser, lastAgent string
if transcriptPath != "" {
lastUser, lastAgent, err = transcript.LastMessages(transcriptPath, 2000)
if err != nil {
logx.Append("remote report transcript: %v", err)
}
}
timeout := time.Duration(cfg.Remote.TimeoutMS) * time.Millisecond
if timeout <= 0 {
timeout = 2 * time.Second
}
report := remote.StatusReport{
Hostname: hostname,
IPs: ips,
Agent: agent,
CWD: cwd,
Status: eventToStatus(event),
Event: event,
ConversationID: conversationID,
LastUser: lastUser,
LastAgent: lastAgent,
UpdatedAt: time.Now().UTC(),
}
client := remote.NewClient(remote.ClientConfig{
URL: cfg.Remote.URL,
Token: cfg.Remote.Token,
Timeout: timeout,
})
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := client.Report(ctx, report); err != nil {
logx.Append("remote report failed: %v", err)
}
}
func eventToStatus(event string) string {
switch strings.ToLower(event) {
case "stop":
return "waiting"
case "response":
return "running"
case "tool":
return "tool"
case "idle":
return "idle"
default:
return "waiting"
}
}