Skip to content

Commit db3af7b

Browse files
committed
feat: move initial prompt to conversation
1 parent 087acf8 commit db3af7b

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

lib/httpapi/server.go

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@ import (
2929

3030
// Server represents the HTTP server
3131
type Server struct {
32-
router chi.Router
33-
api huma.API
34-
port int
35-
srv *http.Server
36-
mu sync.RWMutex
37-
logger *slog.Logger
38-
conversation *st.Conversation
39-
agentio *termexec.Process
40-
agentType mf.AgentType
41-
emitter *EventEmitter
42-
chatBasePath string
43-
initialPrompt string
44-
initialPromptSent bool
32+
router chi.Router
33+
api huma.API
34+
port int
35+
srv *http.Server
36+
mu sync.RWMutex
37+
logger *slog.Logger
38+
conversation *st.Conversation
39+
agentio *termexec.Process
40+
agentType mf.AgentType
41+
emitter *EventEmitter
42+
chatBasePath string
4543
}
4644

4745
func (s *Server) NormalizeSchema(schema any) any {
@@ -233,20 +231,18 @@ func NewServer(ctx context.Context, config ServerConfig) (*Server, error) {
233231
SnapshotInterval: snapshotInterval,
234232
ScreenStabilityLength: 2 * time.Second,
235233
FormatMessage: formatMessage,
236-
})
234+
}, config.InitialPrompt)
237235
emitter := NewEventEmitter(1024)
238236
s := &Server{
239-
router: router,
240-
api: api,
241-
port: config.Port,
242-
conversation: conversation,
243-
logger: logger,
244-
agentio: config.Process,
245-
agentType: config.AgentType,
246-
emitter: emitter,
247-
chatBasePath: strings.TrimSuffix(config.ChatBasePath, "/"),
248-
initialPrompt: config.InitialPrompt,
249-
initialPromptSent: len(config.InitialPrompt) == 0,
237+
router: router,
238+
api: api,
239+
port: config.Port,
240+
conversation: conversation,
241+
logger: logger,
242+
agentio: config.Process,
243+
agentType: config.AgentType,
244+
emitter: emitter,
245+
chatBasePath: strings.TrimSuffix(config.ChatBasePath, "/"),
250246
}
251247

252248
// Register API routes
@@ -314,11 +310,11 @@ func (s *Server) StartSnapshotLoop(ctx context.Context) {
314310
currentStatus := s.conversation.Status()
315311

316312
// Send initial prompt when agent becomes stable for the first time
317-
if !s.initialPromptSent && convertStatus(currentStatus) == AgentStatusStable {
318-
if err := s.conversation.SendMessage(FormatMessage(s.agentType, s.initialPrompt)...); err != nil {
313+
if !s.conversation.InitialPromptSent && convertStatus(currentStatus) == AgentStatusStable {
314+
if err := s.conversation.SendMessage(FormatMessage(s.agentType, s.conversation.InitialPrompt)...); err != nil {
319315
s.logger.Error("Failed to send initial prompt", "error", err)
320316
} else {
321-
s.initialPromptSent = true
317+
s.conversation.InitialPromptSent = true
322318
currentStatus = st.ConversationStatusChanging
323319
s.logger.Info("Initial prompt sent successfully")
324320
}

lib/screentracker/conversation.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ type Conversation struct {
7474
messages []ConversationMessage
7575
screenBeforeLastUserMessage string
7676
lock sync.Mutex
77+
// InitialPrompt is the initial prompt passed to the agent
78+
InitialPrompt string
79+
// InitialPromptSent keeps track if the InitialPrompt has been successfully sent to the agents
80+
InitialPromptSent bool
7781
}
7882

7983
type ConversationStatus string
@@ -94,7 +98,7 @@ func getStableSnapshotsThreshold(cfg ConversationConfig) int {
9498
return threshold + 1
9599
}
96100

97-
func NewConversation(ctx context.Context, cfg ConversationConfig) *Conversation {
101+
func NewConversation(ctx context.Context, cfg ConversationConfig, initialPrompt string) *Conversation {
98102
threshold := getStableSnapshotsThreshold(cfg)
99103
c := &Conversation{
100104
cfg: cfg,
@@ -107,6 +111,8 @@ func NewConversation(ctx context.Context, cfg ConversationConfig) *Conversation
107111
Time: cfg.GetTime(),
108112
},
109113
},
114+
InitialPrompt: initialPrompt,
115+
InitialPromptSent: len(initialPrompt) == 0,
110116
}
111117
return c
112118
}

lib/screentracker/conversation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func statusTest(t *testing.T, params statusTestParams) {
4242
if params.cfg.GetTime == nil {
4343
params.cfg.GetTime = func() time.Time { return time.Now() }
4444
}
45-
c := st.NewConversation(ctx, params.cfg)
45+
c := st.NewConversation(ctx, params.cfg, "")
4646
assert.Equal(t, st.ConversationStatusInitializing, c.Status())
4747

4848
for i, step := range params.steps {
@@ -147,7 +147,7 @@ func TestMessages(t *testing.T) {
147147
for _, opt := range opts {
148148
opt(&cfg)
149149
}
150-
return st.NewConversation(context.Background(), cfg)
150+
return st.NewConversation(context.Background(), cfg, "")
151151
}
152152

153153
t.Run("messages are copied", func(t *testing.T) {

0 commit comments

Comments
 (0)