ai-agent/internal/tui/adapter.go
admin 8dc496b626
Some checks failed
CI / test (push) Has been cancelled
Release / release (push) Failing after 4m36s
first commit
2026-03-08 15:40:34 +07:00

51 lines
1.3 KiB
Go

package tui
import (
"time"
tea "charm.land/bubbletea/v2"
)
// Adapter bridges the agent.Output interface to BubbleTea messages.
type Adapter struct {
program *tea.Program
}
// NewAdapter creates an Adapter that sends messages to the given program.
func NewAdapter(p *tea.Program) *Adapter {
return &Adapter{program: p}
}
func (a *Adapter) StreamText(text string) {
sendMsg(a.program, StreamTextMsg{Text: text})
}
func (a *Adapter) StreamDone(evalCount, promptTokens int) {
sendMsg(a.program, StreamDoneMsg{EvalCount: evalCount, PromptTokens: promptTokens})
}
func (a *Adapter) ToolCallStart(name string, args map[string]any) {
sendMsg(a.program, ToolCallStartMsg{Name: name, Args: args, StartTime: time.Now()})
}
func (a *Adapter) ToolCallResult(name string, result string, isError bool, duration time.Duration) {
sendMsg(a.program, ToolCallResultMsg{Name: name, Result: result, IsError: isError, Duration: duration})
}
func (a *Adapter) SystemMessage(msg string) {
sendMsg(a.program, SystemMessageMsg{Msg: msg})
}
func (a *Adapter) Error(msg string) {
// Log error for debugging
if len(msg) > 100 {
msg = msg[:97] + "..."
}
sendMsg(a.program, ErrorMsg{Msg: msg})
}
// Done sends the final completion message.
func (a *Adapter) Done() {
sendMsg(a.program, AgentDoneMsg{})
}