diff --git a/fs/handlers.go b/fs/handlers.go index df614c5..0f254af 100644 --- a/fs/handlers.go +++ b/fs/handlers.go @@ -17,6 +17,13 @@ import ( "ollie/toolsrv" ) +// wireAgentStateEvents sets up the agent to publish state change events to the event bus. +func wireAgentStateEvents(sessID string, ag *agent.Agent) { + ag.SetOnStateChange(func(agentID, state string) { + session.PublishEvent("session."+sessID+".agent."+agentID+".state", state) + }) +} + func readBackends(_ HandlerCtx) ([]byte, error) { return []byte(strings.Join(backend.Backends(), "\n") + "\n"), nil } @@ -397,6 +404,7 @@ func requestAgentNew(ctx HandlerCtx, data []byte) ([]byte, error) { al := NewAgentLog("") ctx.Session.SetAgentLog(ag.ID(), al) ag.SetOutput(NewEventHandler(al)) + wireAgentStateEvents(ctx.Session.ID(), ag) return []byte(ag.ID() + "\n"), nil } diff --git a/fs/newroot.go b/fs/newroot.go index 14d8933..ae0565e 100644 --- a/fs/newroot.go +++ b/fs/newroot.go @@ -86,6 +86,7 @@ func NewRoot(cfg Config) *Tree { // Wire up the event handler so agent events flow to the AgentLog if ag := r.Session.FindAgent(ra.ID); ag != nil { ag.SetOutput(NewEventHandler(al)) + wireAgentStateEvents(r.Session.ID(), ag) } } }