@@ -52,6 +52,10 @@ func (s *Server) GetOpenAPI() string {
5252 return string (prettyJSON )
5353}
5454
55+ // That's about 40 frames per second. It's slightly less
56+ // because the action of taking a snapshot takes time too.
57+ const snapshotInterval = 25 * time .Millisecond
58+
5559// NewServer creates a new server instance
5660func NewServer (ctx context.Context , agentType mf.AgentType , process * termexec.Process , port int ) * Server {
5761 router := chi .NewMux ()
@@ -72,9 +76,6 @@ func NewServer(ctx context.Context, agentType mf.AgentType, process *termexec.Pr
7276 formatMessage := func (message string , userInput string ) string {
7377 return mf .FormatAgentMessage (agentType , message , userInput )
7478 }
75- // That's about 40 frames per second. It's slightly less
76- // because the action of taking a snapshot takes time too.
77- snapshotInterval := 25 * time .Millisecond
7879 conversation := st .NewConversation (ctx , st.ConversationConfig {
7980 AgentIO : process ,
8081 GetTime : func () time.Time {
@@ -84,17 +85,7 @@ func NewServer(ctx context.Context, agentType mf.AgentType, process *termexec.Pr
8485 ScreenStabilityLength : 2 * time .Second ,
8586 FormatMessage : formatMessage ,
8687 })
87- conversation .StartSnapshotLoop (ctx )
8888 emitter := NewEventEmitter (1024 )
89- go func () {
90- for {
91- emitter .UpdateStatusAndEmitChanges (conversation .Status ())
92- emitter .UpdateMessagesAndEmitChanges (conversation .Messages ())
93- emitter .UpdateScreenAndEmitChanges (conversation .Screen ())
94- time .Sleep (snapshotInterval )
95- }
96- }()
97-
9889 s := & Server {
9990 router : router ,
10091 api : api ,
@@ -112,6 +103,18 @@ func NewServer(ctx context.Context, agentType mf.AgentType, process *termexec.Pr
112103 return s
113104}
114105
106+ func (s * Server ) StartSnapshotLoop (ctx context.Context ) {
107+ s .conversation .StartSnapshotLoop (ctx )
108+ go func () {
109+ for {
110+ s .emitter .UpdateStatusAndEmitChanges (s .conversation .Status ())
111+ s .emitter .UpdateMessagesAndEmitChanges (s .conversation .Messages ())
112+ s .emitter .UpdateScreenAndEmitChanges (s .conversation .Screen ())
113+ time .Sleep (snapshotInterval )
114+ }
115+ }()
116+ }
117+
115118// registerRoutes sets up all API endpoints
116119func (s * Server ) registerRoutes () {
117120 // GET /status endpoint
0 commit comments