v0.0.5: context compaction, minimal config, retry/search/code symbols/diff/status/test commands
This commit is contained in:
+111
-25
@@ -28,6 +28,7 @@ type Options struct {
|
||||
Yolo bool
|
||||
ThemeMode ThemeMode
|
||||
ModelManager ModelManager
|
||||
WorkingDir string
|
||||
}
|
||||
|
||||
type ModelManager interface {
|
||||
@@ -69,13 +70,14 @@ type message struct {
|
||||
}
|
||||
|
||||
type model struct {
|
||||
ctx context.Context
|
||||
agent *agent.Agent
|
||||
modelName string
|
||||
yolo bool
|
||||
models ModelManager
|
||||
theme theme
|
||||
styles styles
|
||||
ctx context.Context
|
||||
agent *agent.Agent
|
||||
modelName string
|
||||
yolo bool
|
||||
models ModelManager
|
||||
workingDir string
|
||||
theme theme
|
||||
styles styles
|
||||
|
||||
viewport viewport.Model
|
||||
input textarea.Model
|
||||
@@ -84,11 +86,12 @@ type model struct {
|
||||
width int
|
||||
height int
|
||||
|
||||
messages []message
|
||||
status string
|
||||
running bool
|
||||
cancel context.CancelFunc
|
||||
events <-chan tea.Msg
|
||||
messages []message
|
||||
status string
|
||||
contextUsage string
|
||||
running bool
|
||||
cancel context.CancelFunc
|
||||
events <-chan tea.Msg
|
||||
|
||||
// Session picker state
|
||||
picking bool
|
||||
@@ -119,6 +122,7 @@ type slashCommand struct {
|
||||
|
||||
var slashCommands = []slashCommand{
|
||||
{Name: "/clear", Description: "reset context"},
|
||||
{Name: "/compact", Description: "compress context"},
|
||||
{Name: "/exit", Description: "quit"},
|
||||
{Name: "/quit", Description: "quit"},
|
||||
{Name: "/model", Description: "model/provider/thinking"},
|
||||
@@ -126,6 +130,9 @@ var slashCommands = []slashCommand{
|
||||
{Name: "/resume", Description: "resume session"},
|
||||
{Name: "/rename", Description: "rename session"},
|
||||
{Name: "/new", Description: "new session"},
|
||||
{Name: "/status", Description: "show changed files"},
|
||||
{Name: "/diff", Description: "show unstaged diff"},
|
||||
{Name: "/test", Description: "run project tests"},
|
||||
}
|
||||
|
||||
func newModel(ctx context.Context, assistant *agent.Agent, opts Options) model {
|
||||
@@ -154,19 +161,22 @@ func newModel(ctx context.Context, assistant *agent.Agent, opts Options) model {
|
||||
vp.Style = st.Viewport
|
||||
vp.SetContent("")
|
||||
|
||||
return model{
|
||||
ctx: ctx,
|
||||
agent: assistant,
|
||||
modelName: opts.ModelName,
|
||||
yolo: opts.Yolo,
|
||||
models: opts.ModelManager,
|
||||
theme: th,
|
||||
styles: st,
|
||||
viewport: vp,
|
||||
input: input,
|
||||
spinner: spin,
|
||||
status: "ready",
|
||||
m := model{
|
||||
ctx: ctx,
|
||||
agent: assistant,
|
||||
modelName: opts.ModelName,
|
||||
yolo: opts.Yolo,
|
||||
models: opts.ModelManager,
|
||||
workingDir: opts.WorkingDir,
|
||||
theme: th,
|
||||
styles: st,
|
||||
viewport: vp,
|
||||
input: input,
|
||||
spinner: spin,
|
||||
status: "ready",
|
||||
}
|
||||
m.refreshContextUsage()
|
||||
return m
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd {
|
||||
@@ -272,6 +282,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if m.models != nil {
|
||||
_ = m.models.Save()
|
||||
}
|
||||
m.refreshContextUsage()
|
||||
m.resize()
|
||||
m.refreshViewport(true)
|
||||
cmds = append(cmds, textarea.Blink)
|
||||
@@ -338,13 +349,14 @@ func (m *model) submit() (tea.Model, tea.Cmd) {
|
||||
return *m, tea.Quit
|
||||
case "/clear":
|
||||
m.agent.Clear()
|
||||
m.refreshContextUsage()
|
||||
m.messages = []message{{role: roleSystem, content: "Context cleared."}}
|
||||
m.input.SetValue("")
|
||||
m.afterInputChanged()
|
||||
m.status = "ready"
|
||||
m.refreshViewport(true)
|
||||
return *m, nil
|
||||
case "/model":
|
||||
case "/model", "/compact":
|
||||
return m.runLocalCommand(input)
|
||||
}
|
||||
|
||||
@@ -541,6 +553,9 @@ func (m model) modelMetaView() string {
|
||||
parts = append(parts, "thinking "+currentThinking)
|
||||
}
|
||||
}
|
||||
if m.contextUsage != "" {
|
||||
parts = append(parts, m.contextUsage)
|
||||
}
|
||||
parts = append([]string{modelName, mode}, parts...)
|
||||
parts = append(parts, "theme "+string(m.theme.Mode))
|
||||
line := fitLine(strings.Join(parts, " · "), max(1, m.width-2))
|
||||
@@ -700,6 +715,58 @@ func (m model) modelInfo() string {
|
||||
return fmt.Sprintf("model: %s\nmode: %s\ntheme: %s", m.modelName, mode, m.theme.Mode)
|
||||
}
|
||||
|
||||
func (m *model) refreshContextUsage() {
|
||||
m.contextUsage = contextUsageMeta(m.agent)
|
||||
}
|
||||
|
||||
func contextUsageMeta(assistant *agent.Agent) string {
|
||||
if assistant == nil {
|
||||
return ""
|
||||
}
|
||||
info := assistant.RuntimeInfo()
|
||||
if info.ContextTokens <= 0 && info.MaxContextTokens <= 0 {
|
||||
return ""
|
||||
}
|
||||
used := formatTokenCount(info.ContextTokens)
|
||||
if info.MaxContextTokens <= 0 {
|
||||
return "ctx ~" + used
|
||||
}
|
||||
return fmt.Sprintf("ctx ~%s/%s (%s)", used, formatTokenCount(info.MaxContextTokens), contextPercent(info.ContextTokens, info.MaxContextTokens))
|
||||
}
|
||||
|
||||
func contextPercent(used int, limit int) string {
|
||||
if limit <= 0 {
|
||||
return ""
|
||||
}
|
||||
if used <= 0 {
|
||||
return "0%"
|
||||
}
|
||||
percent := used * 100 / limit
|
||||
if percent == 0 {
|
||||
return "<1%"
|
||||
}
|
||||
return fmt.Sprintf("%d%%", percent)
|
||||
}
|
||||
|
||||
func formatTokenCount(tokens int) string {
|
||||
if tokens < 0 {
|
||||
tokens = 0
|
||||
}
|
||||
if tokens < 1000 {
|
||||
return fmt.Sprintf("%d", tokens)
|
||||
}
|
||||
if tokens < 1_000_000 {
|
||||
if tokens%1000 == 0 {
|
||||
return fmt.Sprintf("%dk", tokens/1000)
|
||||
}
|
||||
return fmt.Sprintf("%.1fk", float64(tokens)/1000)
|
||||
}
|
||||
if tokens%1_000_000 == 0 {
|
||||
return fmt.Sprintf("%dm", tokens/1_000_000)
|
||||
}
|
||||
return fmt.Sprintf("%.1fm", float64(tokens)/1_000_000)
|
||||
}
|
||||
|
||||
func fitLine(text string, width int) string {
|
||||
width = max(1, width)
|
||||
if lipgloss.Width(text) <= width {
|
||||
@@ -741,6 +808,7 @@ func (m *model) runLocalCommand(input string) (tea.Model, tea.Cmd) {
|
||||
m.status = status
|
||||
// Rebuild display from agent history (matters after /resume and /new)
|
||||
m.syncMessages()
|
||||
m.refreshContextUsage()
|
||||
// Append command output after synced history
|
||||
m.messages = append(m.messages, message{role: role, content: output})
|
||||
m.refreshViewport(true)
|
||||
@@ -753,6 +821,8 @@ func (m model) handleLocalCommand(input string) (string, error) {
|
||||
return "", fmt.Errorf("empty command")
|
||||
}
|
||||
switch fields[0] {
|
||||
case "/compact":
|
||||
return m.handleCompactCommand()
|
||||
case "/model":
|
||||
return m.handleModelCommand(fields[1:])
|
||||
case "/sessions":
|
||||
@@ -763,11 +833,27 @@ func (m model) handleLocalCommand(input string) (string, error) {
|
||||
return m.handleRenameCommand(fields[1:])
|
||||
case "/new":
|
||||
return m.handleNewCommand()
|
||||
case "/status":
|
||||
return m.handleStatusCommand(fields[1:])
|
||||
case "/diff":
|
||||
return m.handleDiffCommand(fields[1:])
|
||||
case "/test":
|
||||
return m.handleTestCommand(fields[1:])
|
||||
default:
|
||||
return "", fmt.Errorf("unknown command: %s", fields[0])
|
||||
}
|
||||
}
|
||||
|
||||
func (m model) handleCompactCommand() (string, error) {
|
||||
if m.agent == nil {
|
||||
return "", fmt.Errorf("agent is not configured")
|
||||
}
|
||||
if err := m.agent.Compact(m.ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "Context compacted.", nil
|
||||
}
|
||||
|
||||
func (m model) handleModelCommand(args []string) (string, error) {
|
||||
if m.models == nil {
|
||||
if len(args) > 0 {
|
||||
|
||||
+116
-1
@@ -2,9 +2,15 @@ package tui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"agentu/internal/agent"
|
||||
"agentu/internal/llm"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
@@ -82,6 +88,20 @@ func TestModelMetaRendersBelowInput(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelMetaRendersContextUsage(t *testing.T) {
|
||||
assistant := agent.New(agent.Options{Model: "test-model", MaxContextTokens: 1000})
|
||||
assistant.SetMessages([]llm.Message{{Role: llm.RoleUser, Content: strings.Repeat("x", 800)}})
|
||||
m := newModel(context.Background(), assistant, Options{ModelName: "test-model"})
|
||||
next, _ := m.Update(tea.WindowSizeMsg{Width: 120, Height: 30})
|
||||
rendered := next.(model).modelMetaView()
|
||||
|
||||
for _, want := range []string{"ctx ~", "/1k", "%"} {
|
||||
if !strings.Contains(rendered, want) {
|
||||
t.Fatalf("model meta missing %q:\n%s", want, rendered)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitialViewDoesNotRenderTopNote(t *testing.T) {
|
||||
m := newModel(context.Background(), nil, Options{ModelName: "test-model"})
|
||||
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||
@@ -264,13 +284,108 @@ func TestSlashCommandSuggestions(t *testing.T) {
|
||||
m.afterInputChanged()
|
||||
|
||||
rendered := m.View()
|
||||
for _, want := range []string{"/clear reset context", "/exit quit", "/model model/provider/thinking"} {
|
||||
for _, want := range []string{"/clear reset context", "/compact compress context", "/exit quit", "/model model/provider/thinking"} {
|
||||
if !strings.Contains(rendered, want) {
|
||||
t.Fatalf("rendered suggestions missing %q:\n%s", want, rendered)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type tuiCompactProvider struct{}
|
||||
|
||||
func (tuiCompactProvider) ChatStream(ctx context.Context, req llm.ChatRequest, emit func(llm.StreamEvent) error) error {
|
||||
return emit(llm.StreamEvent{Content: "summary from tui"})
|
||||
}
|
||||
|
||||
func TestCompactCommand(t *testing.T) {
|
||||
assistant := agent.New(agent.Options{Provider: tuiCompactProvider{}, Model: "test", SystemPrompt: "system"})
|
||||
assistant.SetMessages([]llm.Message{
|
||||
{Role: llm.RoleSystem, Content: "system"},
|
||||
{Role: llm.RoleUser, Content: "old question 1"},
|
||||
{Role: llm.RoleAssistant, Content: "old answer 1"},
|
||||
{Role: llm.RoleUser, Content: "old question 2"},
|
||||
{Role: llm.RoleAssistant, Content: "old answer 2"},
|
||||
{Role: llm.RoleUser, Content: "recent question 1"},
|
||||
{Role: llm.RoleAssistant, Content: "recent answer 1"},
|
||||
{Role: llm.RoleUser, Content: "recent question 2"},
|
||||
{Role: llm.RoleAssistant, Content: "recent answer 2"},
|
||||
})
|
||||
m := newModel(context.Background(), assistant, Options{ModelName: "test-model"})
|
||||
|
||||
out, err := m.handleLocalCommand("/compact")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out != "Context compacted." {
|
||||
t.Fatalf("output = %q", out)
|
||||
}
|
||||
if !strings.Contains(messageContents(assistant.Messages()), "[context summary]") {
|
||||
t.Fatalf("messages missing summary: %#v", assistant.Messages())
|
||||
}
|
||||
}
|
||||
|
||||
func messageContents(messages []llm.Message) string {
|
||||
var b strings.Builder
|
||||
for _, msg := range messages {
|
||||
b.WriteString(msg.Content)
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func TestWorkflowStatusDiffAndTestCommands(t *testing.T) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
t.Skip("git not installed")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
runInDir(t, dir, "git", "init")
|
||||
runInDir(t, dir, "git", "config", "user.email", "agentu@example.test")
|
||||
runInDir(t, dir, "git", "config", "user.name", "agentu")
|
||||
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module example.com/p1\n\ngo 1.26\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "main_test.go"), []byte("package p1\n\nimport \"testing\"\n\nfunc TestOK(t *testing.T) {}\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runInDir(t, dir, "git", "add", ".")
|
||||
runInDir(t, dir, "git", "commit", "-m", "init")
|
||||
if err := os.WriteFile(filepath.Join(dir, "main_test.go"), []byte("package p1\n\nimport \"testing\"\n\nfunc TestOK(t *testing.T) { t.Log(\"changed\") }\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
m := newModel(context.Background(), nil, Options{ModelName: "test-model", WorkingDir: dir})
|
||||
status, err := m.handleStatusCommand(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(status, "main_test.go") {
|
||||
t.Fatalf("status missing changed file:\n%s", status)
|
||||
}
|
||||
diff, err := m.handleDiffCommand(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(diff, "changed") {
|
||||
t.Fatalf("diff missing changed content:\n%s", diff)
|
||||
}
|
||||
out, err := m.handleTestCommand(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(out, "$ go test ./...") {
|
||||
t.Fatalf("test output missing default command:\n%s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func runInDir(t *testing.T, dir string, name string, args ...string) {
|
||||
t.Helper()
|
||||
cmd := exec.Command(name, args...)
|
||||
cmd.Dir = dir
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("%s %v failed: %v\n%s", name, args, err, output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelCommand(t *testing.T) {
|
||||
m := newModel(context.Background(), nil, Options{ModelName: "test-model", Yolo: true})
|
||||
m.input.SetValue("/model")
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"agentu/internal/tools"
|
||||
)
|
||||
|
||||
const workflowCommandTimeout = 2 * time.Minute
|
||||
|
||||
func (m model) handleStatusCommand(args []string) (string, error) {
|
||||
if len(args) != 0 {
|
||||
return "", fmt.Errorf("usage: /status")
|
||||
}
|
||||
out, err := m.runGitCommand("status", "--short")
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
if strings.TrimSpace(out) == "" {
|
||||
return "Working tree clean.", nil
|
||||
}
|
||||
return "Changed files:\n" + out, nil
|
||||
}
|
||||
|
||||
func (m model) handleDiffCommand(args []string) (string, error) {
|
||||
if len(args) > 0 && args[0] == "--stat" {
|
||||
out, err := m.runGitCommand(append([]string{"diff", "--stat", "--"}, args[1:]...)...)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
if strings.TrimSpace(out) == "" {
|
||||
return "No unstaged diff.", nil
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
out, err := m.runGitCommand(append([]string{"diff", "--"}, args...)...)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
if strings.TrimSpace(out) == "" {
|
||||
return "No unstaged diff.", nil
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m model) handleTestCommand(args []string) (string, error) {
|
||||
command := strings.TrimSpace(strings.Join(args, " "))
|
||||
if command == "" {
|
||||
var err error
|
||||
command, err = defaultTestCommand(m.workflowDir())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
out, err := m.runShellCommand(command)
|
||||
if err != nil {
|
||||
if strings.TrimSpace(out) != "" {
|
||||
return out, err
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if strings.TrimSpace(out) == "" {
|
||||
return fmt.Sprintf("$ %s\n(no output)", command), nil
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m model) runGitCommand(args ...string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(m.ctx, workflowCommandTimeout)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "git", append([]string{"-C", m.workflowDir()}, args...)...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
text := tools.FormatResult(string(output))
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||
return text, fmt.Errorf("git command timed out: %w", ctx.Err())
|
||||
}
|
||||
if err != nil {
|
||||
if strings.TrimSpace(text) != "" {
|
||||
return text, fmt.Errorf("git command failed: %w", err)
|
||||
}
|
||||
return "", fmt.Errorf("git command failed: %w", err)
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
func (m model) runShellCommand(command string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(m.ctx, workflowCommandTimeout)
|
||||
defer cancel()
|
||||
|
||||
shell := os.Getenv("SHELL")
|
||||
if shell == "" {
|
||||
shell = "/bin/sh"
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, shell, "-lc", command)
|
||||
cmd.Dir = m.workflowDir()
|
||||
|
||||
var combined bytes.Buffer
|
||||
cmd.Stdout = &combined
|
||||
cmd.Stderr = &combined
|
||||
err := cmd.Run()
|
||||
text := strings.TrimRight(tools.FormatResult(combined.String()), "\n")
|
||||
if text != "" {
|
||||
text = fmt.Sprintf("$ %s\n%s", command, text)
|
||||
}
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||
return text, fmt.Errorf("command timed out: %w", ctx.Err())
|
||||
}
|
||||
if err != nil {
|
||||
return text, fmt.Errorf("command failed: %w", err)
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
func (m model) workflowDir() string {
|
||||
if strings.TrimSpace(m.workingDir) == "" {
|
||||
return "."
|
||||
}
|
||||
return m.workingDir
|
||||
}
|
||||
|
||||
func defaultTestCommand(dir string) (string, error) {
|
||||
if fileExists(filepath.Join(dir, "go.mod")) {
|
||||
return "go test ./...", nil
|
||||
}
|
||||
if fileExists(filepath.Join(dir, "package.json")) {
|
||||
return "npm test", nil
|
||||
}
|
||||
return "", fmt.Errorf("no default test command found; use /test <command>")
|
||||
}
|
||||
|
||||
func fileExists(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
return err == nil && !info.IsDir()
|
||||
}
|
||||
Reference in New Issue
Block a user