improved tool debugging

This commit is contained in:
Luxferre
2026-03-22 12:01:29 +02:00
parent 77d0d17c89
commit cd2de78c2c
16 changed files with 986 additions and 592 deletions
+180 -45
View File
@@ -16,29 +16,30 @@ import (
var (
titleStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFDF5")).
Background(lipgloss.Color("#25A065")).
Padding(0, 1).
Bold(true)
Foreground(lipgloss.Color("#FFFDF5")).
Background(lipgloss.Color("#25A065")).
Padding(0, 1).
Bold(true)
statusStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFDF5")).
Background(lipgloss.Color("#3C3C3C")).
Padding(0, 1)
Foreground(lipgloss.Color("#FFFDF5")).
Background(lipgloss.Color("#3C3C3C")).
Padding(0, 1)
viewportStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
Padding(0, 1)
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
Padding(0, 1)
textareaStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("240")).
Padding(0, 1)
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("240")).
Padding(0, 1)
textareaFocusStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
Padding(0, 1)
userStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("6")).Bold(true)
agentStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("5")).Bold(true)
sysStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Italic(true)
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
Padding(0, 1)
userStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("6")).Bold(true)
agentStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("5")).Bold(true)
sysStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Italic(true)
bannerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFF00")).Bold(true)
banner = ` _ __ __ _ __
___ (_) ___/ / ___ / /__ (_) ____ / /__
@@ -48,17 +49,20 @@ var (
)
type model struct {
viewport viewport.Model
textarea textarea.Model
agent *sidekick.Sidekick
pool map[string]*sidekick.Sidekick
history []sidekick.Message
isThinking bool
err error
ready bool
width int
height int
renderer *glamour.TermRenderer
viewport viewport.Model
textarea textarea.Model
agent *sidekick.Sidekick
pool map[string]*sidekick.Sidekick
history []sidekick.Message
transientSteps []string
currentStream string
currentReasoning string
isThinking bool
err error
ready bool
width int
height int
renderer *glamour.TermRenderer
}
type agentResponseMsg struct {
@@ -66,6 +70,32 @@ type agentResponseMsg struct {
err error
}
type streamMsg string
type reasoningMsg string
type intermediateMsg string
var streamChan = make(chan string, 100)
var reasoningChan = make(chan string, 100)
var intermediateChan = make(chan string, 100)
func waitForStream() tea.Cmd {
return func() tea.Msg {
return streamMsg(<-streamChan)
}
}
func waitForReasoning() tea.Cmd {
return func() tea.Msg {
return reasoningMsg(<-reasoningChan)
}
}
func waitForIntermediate() tea.Cmd {
return func() tea.Msg {
return intermediateMsg(<-intermediateChan)
}
}
func initialModel() model {
ta := textarea.New()
ta.Placeholder = "Type a message..."
@@ -85,7 +115,7 @@ func initialModel() model {
} else {
// Attempt to load and apply prompts
tmpl, _ := sidekick.LoadPrompts("prompts.toml")
sidekick.InitMCPServers(cfg.MCPServers)
pool = make(map[string]*sidekick.Sidekick)
for id, aCfg := range cfg.Agents {
@@ -108,15 +138,41 @@ func initialModel() model {
agent = pool[entryAgent]
}
// Force configs on all pool agents, including fallback 'agent'
agentsToUpdate := []*sidekick.Sidekick{agent}
for _, a := range pool {
agentsToUpdate = append(agentsToUpdate, a)
}
for _, a := range agentsToUpdate {
if a != nil {
a.Config.Streaming = true
a.Config.LogIntermediate = true
a.StreamHandler = func(s string) {
streamChan <- s
}
a.ReasoningHandler = func(s string) {
reasoningChan <- s
}
a.IntermediateHandler = func(s string) {
intermediateChan <- s
}
}
}
return model{
textarea: ta,
agent: agent,
pool: pool,
history: []sidekick.Message{{Role: sidekick.MessageRoleSystem, Content: banner + "\n\nSidekick initialized. Type a message below."}},
history: []sidekick.Message{
{Role: sidekick.MessageRoleSystem, Content: banner + "\n\nSidekick initialized. Type a message below."},
},
}
}
func (m model) Init() tea.Cmd { return textarea.Blink }
func (m model) Init() tea.Cmd {
return tea.Batch(textarea.Blink, waitForStream(), waitForReasoning(), waitForIntermediate())
}
func (m model) renderMessage(msg sidekick.Message) string {
label := ""
@@ -132,13 +188,38 @@ func (m model) renderMessage(msg sidekick.Message) string {
}
content := msg.Content
if m.renderer != nil && msg.Role != sidekick.MessageRoleSystem {
rendered, err := m.renderer.Render(content)
if err == nil {
content = strings.TrimSpace(rendered)
reasoning := msg.Reasoning
// If this is the banner message, handle it specially to preserve ASCII art
if strings.Contains(content, "----") && msg.Role == sidekick.MessageRoleSystem {
if strings.HasPrefix(content, banner) {
bannerPart := bannerStyle.Render(banner)
otherPart := strings.TrimPrefix(content, banner)
if m.renderer != nil && strings.TrimSpace(otherPart) != "" {
if r, err := m.renderer.Render(otherPart); err == nil {
otherPart = "\n" + strings.TrimSpace(r)
}
}
return label + "\n" + bannerPart + otherPart
}
}
if m.renderer != nil {
if reasoning != "" {
if r, err := m.renderer.Render("> *Thinking:*\n>\n> " + reasoning); err == nil {
reasoning = strings.TrimSpace(r) + "\n"
}
}
if content != "" {
if r, err := m.renderer.Render(content); err == nil {
content = strings.TrimSpace(r)
}
}
}
if reasoning != "" {
return label + "\n" + reasoning + content
}
return label + "\n" + content
}
@@ -147,6 +228,32 @@ func (m *model) updateViewportContent() {
for _, msg := range m.history {
rendered = append(rendered, m.renderMessage(msg))
}
for _, step := range m.transientSteps {
wrapped := step
if m.renderer != nil {
if r, err := m.renderer.Render(step); err == nil {
wrapped = strings.TrimSpace(r)
}
}
rendered = append(rendered, sysStyle.Render("System")+"\n"+wrapped)
}
if m.currentReasoning != "" || m.currentStream != "" {
content := m.currentStream
reasoning := m.currentReasoning
if m.renderer != nil {
if reasoning != "" {
if r, err := m.renderer.Render("> *Thinking:*\n>\n> " + reasoning); err == nil {
reasoning = strings.TrimSpace(r) + "\n"
}
}
if content != "" {
if r, err := m.renderer.Render(content); err == nil {
content = strings.TrimSpace(r)
}
}
}
rendered = append(rendered, agentStyle.Render("Sidekick")+"\n"+reasoning+content)
}
m.viewport.SetContent(strings.Join(rendered, "\n\n"))
}
@@ -160,10 +267,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
headerHeight := 1
inputHeight := 5 // Textarea height(3) + border/padding
vpWidth := msg.Width - 4 // Account for viewportStyle padding and border
vpHeight := msg.Height - headerHeight - inputHeight - 2
@@ -186,7 +293,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.updateViewportContent()
m.viewport.GotoBottom()
}
m.textarea.SetWidth(msg.Width - 4)
case tea.KeyMsg:
@@ -207,12 +314,14 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if strings.TrimSpace(v) == "" || m.isThinking {
return m, nil
}
m.history = append(m.history, sidekick.Message{
Role: sidekick.MessageRoleUser,
Content: v,
})
m.currentStream = ""
m.currentReasoning = ""
m.transientSteps = nil
m.updateViewportContent()
m.textarea.Reset()
m.viewport.GotoBottom()
@@ -220,8 +329,35 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, m.runAgent()
}
case streamMsg:
if !m.isThinking {
return m, waitForStream()
}
m.currentStream += string(msg)
m.updateViewportContent()
m.viewport.GotoBottom()
return m, waitForStream()
case reasoningMsg:
if !m.isThinking {
return m, waitForReasoning()
}
m.currentReasoning += string(msg)
m.updateViewportContent()
m.viewport.GotoBottom()
return m, waitForReasoning()
case intermediateMsg:
if !m.isThinking {
return m, waitForIntermediate()
}
m.transientSteps = append(m.transientSteps, string(msg))
m.updateViewportContent()
m.viewport.GotoBottom()
return m, waitForIntermediate()
case agentResponseMsg:
m.isThinking = false
m.currentStream = ""
m.currentReasoning = ""
m.transientSteps = nil
if msg.err != nil {
m.history = append(m.history, sidekick.Message{
Role: sidekick.MessageRoleSystem,
@@ -259,14 +395,14 @@ func (m model) View() string {
if m.isThinking {
status = "THINKING"
}
header := lipgloss.JoinHorizontal(lipgloss.Top,
titleStyle.Render(" SIDEKICK TUI "),
statusStyle.Render(" "+status+" "),
)
vpView := viewportStyle.Width(m.width - 2).Render(m.viewport.View())
taStyle := textareaStyle
if m.textarea.Focused() {
taStyle = textareaFocusStyle
@@ -280,7 +416,6 @@ func (m model) View() string {
)
}
func main() {
if _, err := tea.NewProgram(initialModel(), tea.WithAltScreen()).Run(); err != nil {
log.Fatal(err)
+64 -64
View File
@@ -1,82 +1,82 @@
package main
import (
"strings"
"testing"
"strings"
"testing"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
)
// Since sidekick-tui/main.go uses global variables and is a tea.Model,
// Since sidekick-tui/main.go uses global variables and is a tea.Model,
// we test basic model behavior.
func TestInitialModel(t *testing.T) {
// For testing, sidekick.LoadConfig would fail or look in its default dir.
// This ensures that the model can be initialized even without a config.
m := initialModel()
if m.agent == nil {
t.Error("expected default agent to be initialized even without config")
}
if m.textarea.Value() != "" {
t.Errorf("expected empty textarea, got %s", m.textarea.Value())
}
if len(m.history) != 1 {
t.Errorf("expected 1 initial message in history, got %d", len(m.history))
}
if !strings.Contains(m.history[0].Content, "Sidekick initialized") {
t.Error("expected initial message content to contain 'Sidekick initialized'")
}
// For testing, sidekick.LoadConfig would fail or look in its default dir.
// This ensures that the model can be initialized even without a config.
m := initialModel()
if m.agent == nil {
t.Error("expected default agent to be initialized even without config")
}
if m.textarea.Value() != "" {
t.Errorf("expected empty textarea, got %s", m.textarea.Value())
}
if len(m.history) != 1 {
t.Errorf("expected 1 initial message in history, got %d", len(m.history))
}
if !strings.Contains(m.history[0].Content, "Sidekick initialized") {
t.Error("expected initial message content to contain 'Sidekick initialized'")
}
}
func TestModelUpdate(t *testing.T) {
m := initialModel()
m.ready = true
m.width = 80
m.height = 24
m := initialModel()
m.ready = true
m.width = 80
m.height = 24
// Test a key message (Enter)
m.textarea.SetValue("Hello")
newModel, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
tm := newModel.(model)
if tm.isThinking != true {
t.Error("expected model to be in thinking state after Enter")
}
if tm.textarea.Value() != "" {
t.Error("expected textarea to be reset after Enter")
}
if cmd == nil {
t.Error("expected a command after Enter")
}
// Test a key message (Enter)
m.textarea.SetValue("Hello")
newModel, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
// Test an agent response message
respModel, _ := tm.Update(agentResponseMsg{response: "Hi"})
rm := respModel.(model)
if rm.isThinking != false {
t.Error("expected model to stop thinking after response")
}
if len(rm.history) < 2 {
t.Error("expected history list to grow after response")
}
tm := newModel.(model)
if tm.isThinking != true {
t.Error("expected model to be in thinking state after Enter")
}
if tm.textarea.Value() != "" {
t.Error("expected textarea to be reset after Enter")
}
if cmd == nil {
t.Error("expected a command after Enter")
}
// Test an agent response message
respModel, _ := tm.Update(agentResponseMsg{response: "Hi"})
rm := respModel.(model)
if rm.isThinking != false {
t.Error("expected model to stop thinking after response")
}
if len(rm.history) < 2 {
t.Error("expected history list to grow after response")
}
}
func TestMessageWrapping(t *testing.T) {
m := initialModel()
m.ready = true
m.width = 10
m.height = 20
m.viewport = viewport.New(10, 5) // Very narrow
longMsg := "This is a very long message that should be wrapped."
respModel, _ := m.Update(agentResponseMsg{response: longMsg})
rm := respModel.(model)
content := rm.viewport.View()
if !strings.Contains(content, "\n") {
t.Errorf("expected long message to be wrapped, but no newline found in viewport")
}
m := initialModel()
m.ready = true
m.width = 10
m.height = 20
m.viewport = viewport.New(10, 5) // Very narrow
longMsg := "This is a very long message that should be wrapped."
respModel, _ := m.Update(agentResponseMsg{response: longMsg})
rm := respModel.(model)
content := rm.viewport.View()
if !strings.Contains(content, "\n") {
t.Errorf("expected long message to be wrapped, but no newline found in viewport")
}
}