Skip to content

Commit b434b09

Browse files
johnstcn35C4n0r
andauthored
refactor(lib): extract Conversation interface (#172)
Co-authored-by: 35C4n0r <70096901+35C4n0r@users.noreply.github.com>
1 parent 2ab7a0b commit b434b09

File tree

7 files changed

+585
-563
lines changed

7 files changed

+585
-563
lines changed

cmd/attach/attach.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
tea "github.com/charmbracelet/bubbletea"
1616
"github.com/coder/agentapi/lib/httpapi"
17+
"github.com/coder/agentapi/lib/util"
18+
"github.com/coder/quartz"
1719
"github.com/spf13/cobra"
1820
sse "github.com/tmaxmax/go-sse"
1921
"golang.org/x/term"
@@ -213,7 +215,7 @@ func runAttach(remoteUrl string) error {
213215
p.Send(finishMsg{})
214216
select {
215217
case <-pErrCh:
216-
case <-time.After(1 * time.Second):
218+
case <-util.After(quartz.NewReal(), 1*time.Second):
217219
}
218220

219221
return err

lib/httpapi/server.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type Server struct {
4141
srv *http.Server
4242
mu sync.RWMutex
4343
logger *slog.Logger
44-
conversation *st.Conversation
44+
conversation *st.PTYConversation
4545
agentio *termexec.Process
4646
agentType mf.AgentType
4747
emitter *EventEmitter
@@ -244,7 +244,7 @@ func NewServer(ctx context.Context, config ServerConfig) (*Server, error) {
244244
return mf.FormatToolCall(config.AgentType, message)
245245
}
246246

247-
conversation := st.NewConversation(ctx, st.ConversationConfig{
247+
conversation := st.NewPTY(ctx, st.PTYConversationConfig{
248248
AgentType: config.AgentType,
249249
AgentIO: config.Process,
250250
Clock: config.Clock,
@@ -337,7 +337,7 @@ func sseMiddleware(ctx huma.Context, next func(huma.Context)) {
337337
}
338338

339339
func (s *Server) StartSnapshotLoop(ctx context.Context) {
340-
s.conversation.StartSnapshotLoop(ctx)
340+
s.conversation.Start(ctx)
341341
go func() {
342342
ticker := s.clock.NewTicker(snapshotInterval)
343343
defer ticker.Stop()
@@ -346,7 +346,7 @@ func (s *Server) StartSnapshotLoop(ctx context.Context) {
346346

347347
// Send initial prompt when agent becomes stable for the first time
348348
if !s.conversation.InitialPromptSent && convertStatus(currentStatus) == AgentStatusStable {
349-
if err := s.conversation.SendMessage(FormatMessage(s.agentType, s.conversation.InitialPrompt)...); err != nil {
349+
if err := s.conversation.Send(FormatMessage(s.agentType, s.conversation.InitialPrompt)...); err != nil {
350350
s.logger.Error("Failed to send initial prompt", "error", err)
351351
} else {
352352
s.conversation.InitialPromptSent = true
@@ -357,7 +357,7 @@ func (s *Server) StartSnapshotLoop(ctx context.Context) {
357357
}
358358
s.emitter.UpdateStatusAndEmitChanges(currentStatus, s.agentType)
359359
s.emitter.UpdateMessagesAndEmitChanges(s.conversation.Messages())
360-
s.emitter.UpdateScreenAndEmitChanges(s.conversation.Screen())
360+
s.emitter.UpdateScreenAndEmitChanges(s.conversation.Text())
361361

362362
select {
363363
case <-ctx.Done():
@@ -461,7 +461,7 @@ func (s *Server) createMessage(ctx context.Context, input *MessageRequest) (*Mes
461461

462462
switch input.Body.Type {
463463
case MessageTypeUser:
464-
if err := s.conversation.SendMessage(FormatMessage(s.agentType, input.Body.Content)...); err != nil {
464+
if err := s.conversation.Send(FormatMessage(s.agentType, input.Body.Content)...); err != nil {
465465
return nil, xerrors.Errorf("failed to send message: %w", err)
466466
}
467467
case MessageTypeRaw:

0 commit comments

Comments
 (0)