2026-04-26 08:05:10 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/sipeed/picoclaw/pkg/agent"
|
|
|
|
|
runtimeevents "github.com/sipeed/picoclaw/pkg/events"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-26 09:02:48 +00:00
|
|
|
type gatewayEventPayload struct {
|
2026-04-26 08:05:10 +00:00
|
|
|
DurationMS int64 `json:"duration_ms,omitempty"`
|
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 09:02:48 +00:00
|
|
|
func publishGatewayEvent(
|
2026-04-26 08:05:10 +00:00
|
|
|
al *agent.AgentLoop,
|
|
|
|
|
kind runtimeevents.Kind,
|
|
|
|
|
startedAt time.Time,
|
|
|
|
|
err error,
|
|
|
|
|
) {
|
|
|
|
|
if al == nil || al.RuntimeEventBus() == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
severity := runtimeevents.SeverityInfo
|
2026-04-26 09:02:48 +00:00
|
|
|
payload := gatewayEventPayload{}
|
2026-04-26 08:05:10 +00:00
|
|
|
if !startedAt.IsZero() {
|
|
|
|
|
payload.DurationMS = time.Since(startedAt).Milliseconds()
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
severity = runtimeevents.SeverityError
|
|
|
|
|
payload.Error = err.Error()
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 05:09:03 +00:00
|
|
|
al.RuntimeEventBus().PublishNonBlocking(runtimeevents.Event{
|
2026-04-26 08:05:10 +00:00
|
|
|
Kind: kind,
|
|
|
|
|
Source: runtimeevents.Source{Component: "gateway"},
|
|
|
|
|
Severity: severity,
|
|
|
|
|
Payload: payload,
|
2026-04-27 05:09:03 +00:00
|
|
|
Attrs: gatewayEventAttrs(payload),
|
2026-04-26 08:05:10 +00:00
|
|
|
})
|
|
|
|
|
}
|
2026-04-27 05:09:03 +00:00
|
|
|
|
|
|
|
|
func gatewayEventAttrs(payload gatewayEventPayload) map[string]any {
|
|
|
|
|
attrs := map[string]any{}
|
|
|
|
|
if payload.DurationMS > 0 {
|
|
|
|
|
attrs["duration_ms"] = payload.DurationMS
|
|
|
|
|
}
|
|
|
|
|
if payload.Error != "" {
|
|
|
|
|
attrs["error"] = payload.Error
|
|
|
|
|
}
|
|
|
|
|
return attrs
|
|
|
|
|
}
|