Compare commits
4 Commits
7b8bad5e62
...
v0.1.5
| Author | SHA1 | Date | |
|---|---|---|---|
| c8d8329dbb | |||
| dabf5bfecc | |||
| 9abd5c3b5f | |||
| 3d403bd685 |
@@ -23,6 +23,12 @@ export AGENTU_API_KEY='your-api-key'
|
|||||||
go run .
|
go run .
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Use dark mode when your terminal is dark:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go run . --theme dark
|
||||||
|
```
|
||||||
|
|
||||||
Use `--yolo` only when you want agentu to write files or run shell commands:
|
Use `--yolo` only when you want agentu to write files or run shell commands:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -94,22 +100,25 @@ go run . [flags]
|
|||||||
## TUI Controls
|
## TUI Controls
|
||||||
|
|
||||||
- `enter` sends the message.
|
- `enter` sends the message.
|
||||||
- `ctrl+j` or `alt+enter` inserts a newline.
|
- `ctrl+j` inserts a newline.
|
||||||
|
- `up` recalls the previous input; `ctrl+p` / `ctrl+n` navigate input history.
|
||||||
- The input starts at one line and grows up to six lines.
|
- The input starts at one line and grows up to six lines.
|
||||||
- `page up` / `page down` scrolls the transcript.
|
- `page up` / `page down` scrolls the transcript.
|
||||||
- `esc` cancels a running response.
|
- `esc` cancels a running response.
|
||||||
- Type `/` to show available commands.
|
- Type `/` to show available commands.
|
||||||
|
- In `--yolo` mode, type `! <command>` to run a shell command from the configured working directory.
|
||||||
|
|
||||||
Slash commands:
|
Slash commands:
|
||||||
|
|
||||||
- `/clear` resets conversation history.
|
- `/clear` resets conversation history.
|
||||||
- `/compact` summarizes older conversation history and keeps recent context.
|
- `/compact` summarizes older conversation history and keeps the newest ~20% of context, with a small recent-message floor.
|
||||||
- `/status` shows changed files with `git status --short`.
|
- `/status` shows changed files with `git status --short`.
|
||||||
- `/diff [--stat] [path...]` shows the unstaged git diff.
|
- `/diff [--stat] [path...]` shows the unstaged git diff.
|
||||||
- `/test [command...]` runs a project test command; defaults to `go test ./...` for Go modules or `npm test` for Node projects.
|
- `/test [command...]` runs a project test command; defaults to `go test ./...` for Go modules or `npm test` for Node projects.
|
||||||
- `/model` shows current provider, model, thinking, and switch usage.
|
- `/model` shows current provider, model, thinking, and switch usage.
|
||||||
- `/model provider <name>` switches provider for the current session.
|
- `/model provider <name>` switches provider for the current session.
|
||||||
- `/model model <name>` or `/model <name>` switches model for the current session.
|
- `/model model <name>` or `/model <name>` switches model for the current session.
|
||||||
|
In the TUI, type `/model ` or `/model model ` to choose from configured model IDs in the candidate palette.
|
||||||
- `/model thinking <none|middle|high|xhigh|max>` changes thinking for the current session.
|
- `/model thinking <none|middle|high|xhigh|max>` changes thinking for the current session.
|
||||||
- `/sessions` lists saved sessions.
|
- `/sessions` lists saved sessions.
|
||||||
- `/resume [id]` resumes by ID or opens an interactive picker.
|
- `/resume [id]` resumes by ID or opens an interactive picker.
|
||||||
@@ -125,6 +134,9 @@ Read-only tools are available by default:
|
|||||||
- `file_list`
|
- `file_list`
|
||||||
- `file_search` with optional `context_lines` and `max_results`
|
- `file_search` with optional `context_lines` and `max_results`
|
||||||
- `code_symbols` for Go packages, imports, types, functions, and methods
|
- `code_symbols` for Go packages, imports, types, functions, and methods
|
||||||
|
- `git_status` shows changed files
|
||||||
|
- `git_diff` shows unstaged or staged diff output
|
||||||
|
- `git_log` shows recent commit history
|
||||||
|
|
||||||
Web tools are also available in read-only mode:
|
Web tools are also available in read-only mode:
|
||||||
|
|
||||||
@@ -133,8 +145,9 @@ Web tools are also available in read-only mode:
|
|||||||
|
|
||||||
`--yolo` additionally enables:
|
`--yolo` additionally enables:
|
||||||
|
|
||||||
- `file_write`
|
- `file_write` for creating or replacing whole files
|
||||||
- `shell_run`
|
- `file_edit` for surgical line-range or pattern edits to existing files
|
||||||
|
- `shell_run` for non-interactive shell commands
|
||||||
|
|
||||||
Use `--yolo` for prompts that ask agentu to run or inspect local commands, for
|
Use `--yolo` for prompts that ask agentu to run or inspect local commands, for
|
||||||
example:
|
example:
|
||||||
@@ -143,6 +156,8 @@ example:
|
|||||||
请帮我运行 `go test ./...` 并总结结果
|
请帮我运行 `go test ./...` 并总结结果
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
Run the verification suite:
|
Run the verification suite:
|
||||||
|
|||||||
+154
-23
@@ -7,18 +7,23 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"agentu/pkg/llm"
|
|
||||||
"agentu/internal/tools"
|
"agentu/internal/tools"
|
||||||
|
"agentu/pkg/llm"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultMaxToolRounds = 50
|
defaultMaxToolRounds = 50
|
||||||
doomLoopThreshold = 3
|
doomLoopThreshold = 3
|
||||||
compactRecentMessages = 6
|
compactMinRecentMessages = 6
|
||||||
summaryMessageLimit = 8000
|
compactRecentRetentionPercent = 20
|
||||||
contextSummaryPrefix = "[context summary]"
|
summaryMessageLimit = 8000
|
||||||
|
contextSummaryPrefix = "[context summary]"
|
||||||
|
toolLogArgumentLimit = 400
|
||||||
|
toolLogOutputPreviewBytes = 4096
|
||||||
|
toolLogOutputMarker = "[output]"
|
||||||
|
|
||||||
summarizerSystemPrompt = "You are a conversation summarizer. Summarize the following conversation history into a concise but detailed summary. Preserve:\n" +
|
summarizerSystemPrompt = "You are a conversation summarizer. Summarize the following conversation history into a concise but detailed summary. Preserve:\n" +
|
||||||
"- Key facts, decisions, and conclusions\n" +
|
"- Key facts, decisions, and conclusions\n" +
|
||||||
@@ -29,6 +34,11 @@ const (
|
|||||||
"Output only the summary, no preamble."
|
"Output only the summary, no preamble."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type toolExecutionResult struct {
|
||||||
|
call llm.ToolCall
|
||||||
|
output string
|
||||||
|
}
|
||||||
|
|
||||||
type Agent struct {
|
type Agent struct {
|
||||||
provider llm.Provider
|
provider llm.Provider
|
||||||
providerName string
|
providerName string
|
||||||
@@ -172,6 +182,61 @@ func (a *Agent) SetLastUsage(usage *llm.Usage) {
|
|||||||
copy := *usage
|
copy := *usage
|
||||||
a.lastUsage = ©
|
a.lastUsage = ©
|
||||||
}
|
}
|
||||||
|
// compactRetainedTokenBudget returns the number of estimated tokens to retain
|
||||||
|
// based on compactRecentRetentionPercent, using integer ceiling division.
|
||||||
|
func compactRetainedTokenBudget(totalTokens int) int {
|
||||||
|
if totalTokens <= 0 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return (totalTokens*compactRecentRetentionPercent + 99) / 100
|
||||||
|
}
|
||||||
|
|
||||||
|
// compactTurnBoundary walks backward from start until it finds a user message
|
||||||
|
// or reaches prefixEnd, ensuring the retained suffix starts at a user turn.
|
||||||
|
func compactTurnBoundary(messages []llm.Message, prefixEnd int, start int) int {
|
||||||
|
for start > prefixEnd && messages[start].Role != llm.RoleUser {
|
||||||
|
start--
|
||||||
|
}
|
||||||
|
return start
|
||||||
|
}
|
||||||
|
|
||||||
|
// compactRecentStart returns the index where the retained recent suffix begins.
|
||||||
|
// It balances the 20% token-budget ratio with the compactMinRecentMessages floor
|
||||||
|
// and aligns to a user-turn boundary.
|
||||||
|
func compactRecentStart(messages []llm.Message, prefixEnd int) int {
|
||||||
|
compactableCount := len(messages) - prefixEnd
|
||||||
|
if compactableCount <= compactMinRecentMessages {
|
||||||
|
return len(messages)
|
||||||
|
}
|
||||||
|
|
||||||
|
compactable := messages[prefixEnd:]
|
||||||
|
totalTokens := llm.EstimateTokens(compactable, nil)
|
||||||
|
retainTokens := compactRetainedTokenBudget(totalTokens)
|
||||||
|
|
||||||
|
// Walk backward from the end to find the earliest suffix fitting the budget.
|
||||||
|
ratioStart := len(messages) - 1
|
||||||
|
for i := len(messages) - 1; i >= prefixEnd; i-- {
|
||||||
|
if llm.EstimateTokens(messages[i:], nil) <= retainTokens {
|
||||||
|
ratioStart = i
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enforce the minimum recent-message floor.
|
||||||
|
minStart := len(messages) - compactMinRecentMessages
|
||||||
|
if minStart < prefixEnd {
|
||||||
|
minStart = prefixEnd
|
||||||
|
}
|
||||||
|
// Choose the earlier start so the floor is never weakened.
|
||||||
|
start := minStart
|
||||||
|
if ratioStart < start {
|
||||||
|
start = ratioStart
|
||||||
|
}
|
||||||
|
|
||||||
|
return compactTurnBoundary(messages, prefixEnd, start)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Compact summarizes older conversation messages while preserving the system
|
// Compact summarizes older conversation messages while preserving the system
|
||||||
// prompt and recent messages intact.
|
// prompt and recent messages intact.
|
||||||
@@ -187,12 +252,8 @@ func (a *Agent) Compact(ctx context.Context) error {
|
|||||||
if a.messages[0].Role == llm.RoleSystem {
|
if a.messages[0].Role == llm.RoleSystem {
|
||||||
prefixEnd = 1
|
prefixEnd = 1
|
||||||
}
|
}
|
||||||
if len(a.messages)-prefixEnd <= compactRecentMessages {
|
recentStart := compactRecentStart(a.messages, prefixEnd)
|
||||||
return nil
|
if recentStart <= prefixEnd || recentStart >= len(a.messages) {
|
||||||
}
|
|
||||||
|
|
||||||
recentStart := len(a.messages) - compactRecentMessages
|
|
||||||
if recentStart <= prefixEnd {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
middle := append([]llm.Message(nil), a.messages[prefixEnd:recentStart]...)
|
middle := append([]llm.Message(nil), a.messages[prefixEnd:recentStart]...)
|
||||||
@@ -364,14 +425,15 @@ func (a *Agent) RunTurn(ctx context.Context, input string, out io.Writer, logs i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, call := range calls {
|
results := a.executeTools(ctx, calls, logs)
|
||||||
result := a.executeTool(ctx, call, logs)
|
for _, result := range results {
|
||||||
a.messages = append(a.messages, llm.Message{
|
a.messages = append(a.messages, llm.Message{
|
||||||
Role: llm.RoleTool,
|
Role: llm.RoleTool,
|
||||||
ToolCallID: call.ID,
|
ToolCallID: result.call.ID,
|
||||||
Content: result,
|
Content: result.output,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("tool round limit reached after %d rounds; the task may be too complex for a single turn", a.maxToolRounds)
|
return fmt.Errorf("tool round limit reached after %d rounds; the task may be too complex for a single turn", a.maxToolRounds)
|
||||||
@@ -433,11 +495,8 @@ func (a *Agent) toolDefinitions() []llm.Tool {
|
|||||||
return a.toolRegistry.Definitions()
|
return a.toolRegistry.Definitions()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) executeTool(ctx context.Context, call llm.ToolCall, logs io.Writer) string {
|
func (a *Agent) executeTool(ctx context.Context, call llm.ToolCall) string {
|
||||||
name := call.Function.Name
|
name := call.Function.Name
|
||||||
if logs != nil {
|
|
||||||
fmt.Fprintf(logs, "\n[tool] %s %s\n", name, compact(call.Function.Arguments, 400))
|
|
||||||
}
|
|
||||||
|
|
||||||
if a.toolRegistry == nil {
|
if a.toolRegistry == nil {
|
||||||
return tools.FormatError(fmt.Errorf("tool registry is disabled"))
|
return tools.FormatError(fmt.Errorf("tool registry is disabled"))
|
||||||
@@ -461,12 +520,84 @@ func (a *Agent) executeTool(ctx context.Context, call llm.ToolCall, logs io.Writ
|
|||||||
}
|
}
|
||||||
return tools.FormatError(err)
|
return tools.FormatError(err)
|
||||||
}
|
}
|
||||||
if logs != nil {
|
|
||||||
fmt.Fprintf(logs, "[tool] %s done\n", name)
|
|
||||||
}
|
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Agent) hasMutatingCall(calls []llm.ToolCall) bool {
|
||||||
|
if a.toolRegistry == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, call := range calls {
|
||||||
|
if tool, ok := a.toolRegistry.Get(call.Function.Name); ok {
|
||||||
|
if tools.IsMutating(tool) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Agent) executeTools(ctx context.Context, calls []llm.ToolCall, logs io.Writer) []toolExecutionResult {
|
||||||
|
results := make([]toolExecutionResult, len(calls))
|
||||||
|
|
||||||
|
// Log all tool starts sequentially so TUI gets ordered placeholders.
|
||||||
|
if logs != nil {
|
||||||
|
for _, call := range calls {
|
||||||
|
logToolStart(logs, call)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.hasMutatingCall(calls) {
|
||||||
|
// Sequential: avoid racing mutating tools.
|
||||||
|
for i, call := range calls {
|
||||||
|
output := a.executeTool(ctx, call)
|
||||||
|
results[i] = toolExecutionResult{call: call, output: output}
|
||||||
|
if logs != nil {
|
||||||
|
logToolOutput(logs, call, output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
// Concurrent: independent read-only tools.
|
||||||
|
var mu sync.Mutex
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i, call := range calls {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(idx int, c llm.ToolCall) {
|
||||||
|
defer wg.Done()
|
||||||
|
output := a.executeTool(ctx, c)
|
||||||
|
results[idx] = toolExecutionResult{call: c, output: output}
|
||||||
|
if logs != nil {
|
||||||
|
mu.Lock()
|
||||||
|
logToolOutput(logs, c, output)
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
|
}(i, call)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
func logToolStart(logs io.Writer, call llm.ToolCall) {
|
||||||
|
fmt.Fprintf(logs, "\n[tool] %s %s\n", call.Function.Name, compact(call.Function.Arguments, toolLogArgumentLimit))
|
||||||
|
}
|
||||||
|
|
||||||
|
func logToolOutput(logs io.Writer, call llm.ToolCall, output string) {
|
||||||
|
fmt.Fprintf(logs, "\n[tool] %s %s\n%s\n%s\n", call.Function.Name, compact(call.Function.Arguments, toolLogArgumentLimit), toolLogOutputMarker, toolOutputPreview(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
func toolOutputPreview(output string) string {
|
||||||
|
output = strings.TrimSpace(output)
|
||||||
|
if output == "" {
|
||||||
|
return "(no output)"
|
||||||
|
}
|
||||||
|
if len(output) <= toolLogOutputPreviewBytes {
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
return output[:toolLogOutputPreviewBytes] + fmt.Sprintf("\n[truncated: showing first %d bytes; omitted %d bytes]", toolLogOutputPreviewBytes, len(output)-toolLogOutputPreviewBytes)
|
||||||
|
}
|
||||||
|
|
||||||
type toolCallBuilder struct {
|
type toolCallBuilder struct {
|
||||||
index int
|
index int
|
||||||
id string
|
id string
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"agentu/pkg/llm"
|
|
||||||
"agentu/internal/tools"
|
"agentu/internal/tools"
|
||||||
|
"agentu/pkg/llm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fakeProvider struct {
|
type fakeProvider struct {
|
||||||
@@ -145,6 +145,9 @@ func TestAgentCompact(t *testing.T) {
|
|||||||
if messages[len(messages)-1].Content != "recent-answer-2" {
|
if messages[len(messages)-1].Content != "recent-answer-2" {
|
||||||
t.Fatalf("last recent message = %#v", messages[len(messages)-1])
|
t.Fatalf("last recent message = %#v", messages[len(messages)-1])
|
||||||
}
|
}
|
||||||
|
if messages[2].Content != "recent-user-0" {
|
||||||
|
t.Fatalf("first retained recent message = %#v", messages[2])
|
||||||
|
}
|
||||||
if usage := a.LastUsage(); usage == nil || usage.TotalTokens != 12 {
|
if usage := a.LastUsage(); usage == nil || usage.TotalTokens != 12 {
|
||||||
t.Fatalf("usage = %#v", usage)
|
t.Fatalf("usage = %#v", usage)
|
||||||
}
|
}
|
||||||
@@ -217,6 +220,48 @@ func longConversation() []llm.Message {
|
|||||||
return messages
|
return messages
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func manyTurnConversation(turns int) []llm.Message {
|
||||||
|
messages := []llm.Message{{Role: llm.RoleSystem, Content: "system"}}
|
||||||
|
for i := 0; i < turns; i++ {
|
||||||
|
messages = append(messages,
|
||||||
|
llm.Message{Role: llm.RoleUser, Content: fmt.Sprintf("user-%02d", i)},
|
||||||
|
llm.Message{Role: llm.RoleAssistant, Content: fmt.Sprintf("assistant-%02d", i)},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return messages
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAgentCompactKeepsTwentyPercentRecentContext(t *testing.T) {
|
||||||
|
provider := &compactProvider{}
|
||||||
|
a := New(Options{Provider: provider, Model: "test", SystemPrompt: "system"})
|
||||||
|
a.SetMessages(manyTurnConversation(25))
|
||||||
|
|
||||||
|
if err := a.Compact(context.Background()); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
messages := a.Messages()
|
||||||
|
// 1 system + 1 summary + 10 retained recent messages (5 user/assistant turns)
|
||||||
|
if len(messages) != 12 {
|
||||||
|
t.Fatalf("messages = %d, want 12; contents: %v", len(messages), joinMessageContents(messages))
|
||||||
|
}
|
||||||
|
if provider.summaryCalls != 1 {
|
||||||
|
t.Fatalf("summaryCalls = %d, want 1", provider.summaryCalls)
|
||||||
|
}
|
||||||
|
if messages[2].Content != "user-20" {
|
||||||
|
t.Fatalf("first retained message = %#v, want user-20", messages[2])
|
||||||
|
}
|
||||||
|
if messages[len(messages)-1].Content != "assistant-24" {
|
||||||
|
t.Fatalf("last retained message = %#v, want assistant-24", messages[len(messages)-1])
|
||||||
|
}
|
||||||
|
if !strings.Contains(provider.lastSummary, "user-19") {
|
||||||
|
t.Fatalf("summary should contain user-19 (part of summarized 80%%): %q", provider.lastSummary)
|
||||||
|
}
|
||||||
|
if strings.Contains(provider.lastSummary, "user-20") {
|
||||||
|
t.Fatalf("summary should not contain user-20 (part of retained 20%%): %q", provider.lastSummary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func joinMessageContents(messages []llm.Message) string {
|
func joinMessageContents(messages []llm.Message) string {
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
for _, msg := range messages {
|
for _, msg := range messages {
|
||||||
@@ -340,3 +385,174 @@ func TestAgentHardLimitStopsVaryingCalls(t *testing.T) {
|
|||||||
t.Fatalf("calls = %d, want %d", provider.calls, limit)
|
t.Fatalf("calls = %d, want %d", provider.calls, limit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// slowTool blocks on a channel before returning.
|
||||||
|
type slowTool struct {
|
||||||
|
name string
|
||||||
|
started chan<- string
|
||||||
|
release <-chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *slowTool) Definition() llm.Tool {
|
||||||
|
return llm.Tool{
|
||||||
|
Type: "function",
|
||||||
|
Function: llm.ToolFunction{
|
||||||
|
Name: t.name,
|
||||||
|
Description: "slow test tool",
|
||||||
|
Parameters: json.RawMessage(`{"type":"object","properties":{"text":{"type":"string"}}}`),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *slowTool) Execute(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||||
|
var args struct {
|
||||||
|
Text string `json:"text"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(raw, &args); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
t.started <- t.name
|
||||||
|
<-t.release
|
||||||
|
return args.Text, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// mutatingSlowTool is like slowTool but implements MutatingTool.
|
||||||
|
type mutatingSlowTool struct {
|
||||||
|
slowTool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*mutatingSlowTool) Mutates() bool { return true }
|
||||||
|
|
||||||
|
func TestAgentExecutesIndependentToolCallsInParallel(t *testing.T) {
|
||||||
|
started := make(chan string, 2)
|
||||||
|
release := make(chan struct{})
|
||||||
|
|
||||||
|
tool1 := &slowTool{name: "slow_one", started: started, release: release}
|
||||||
|
tool2 := &slowTool{name: "slow_two", started: started, release: release}
|
||||||
|
|
||||||
|
provider := ¶llelProvider{
|
||||||
|
responses: [][]llm.ToolCallDelta{
|
||||||
|
{
|
||||||
|
{Index: 0, ID: "call_1", Type: "function", Name: "slow_one", Arguments: `{"text":"one"}`},
|
||||||
|
{Index: 1, ID: "call_2", Type: "function", Name: "slow_two", Arguments: `{"text":"two"}`},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
finalContent: "done",
|
||||||
|
}
|
||||||
|
|
||||||
|
a := New(Options{
|
||||||
|
Provider: provider,
|
||||||
|
Model: "test",
|
||||||
|
SystemPrompt: "system",
|
||||||
|
ToolRegistry: tools.NewRegistry(tool1, tool2),
|
||||||
|
})
|
||||||
|
|
||||||
|
var out strings.Builder
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
done <- a.RunTurn(context.Background(), "run both", &out, nil)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Both tools must start before we release them.
|
||||||
|
names := make(map[string]bool)
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
select {
|
||||||
|
case name := <-started:
|
||||||
|
names[name] = true
|
||||||
|
case <-make(chan struct{}):
|
||||||
|
// Use a timer in real code, but for test simplicity we rely on deadlock detection.
|
||||||
|
t.Fatal("timed out waiting for tool to start")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !names["slow_one"] || !names["slow_two"] {
|
||||||
|
t.Fatalf("expected both tools to start, got %v", names)
|
||||||
|
}
|
||||||
|
close(release)
|
||||||
|
|
||||||
|
if err := <-done; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if out.String() != "done" {
|
||||||
|
t.Fatalf("out = %q", out.String())
|
||||||
|
}
|
||||||
|
// Verify tool results were in order.
|
||||||
|
msgs := a.Messages()
|
||||||
|
var toolMsgs []string
|
||||||
|
for _, m := range msgs {
|
||||||
|
if m.Role == llm.RoleTool {
|
||||||
|
toolMsgs = append(toolMsgs, m.Content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(toolMsgs) != 2 || toolMsgs[0] != "one" || toolMsgs[1] != "two" {
|
||||||
|
t.Fatalf("tool messages = %v, want [one two]", toolMsgs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAgentRunsMutatingToolCallsSequentially(t *testing.T) {
|
||||||
|
started := make(chan string, 2)
|
||||||
|
release := make(chan struct{})
|
||||||
|
|
||||||
|
tool1 := &mutatingSlowTool{slowTool{name: "mut_one", started: started, release: release}}
|
||||||
|
tool2 := &mutatingSlowTool{slowTool{name: "mut_two", started: started, release: release}}
|
||||||
|
|
||||||
|
provider := ¶llelProvider{
|
||||||
|
responses: [][]llm.ToolCallDelta{
|
||||||
|
{
|
||||||
|
{Index: 0, ID: "call_1", Type: "function", Name: "mut_one", Arguments: `{"text":"one"}`},
|
||||||
|
{Index: 1, ID: "call_2", Type: "function", Name: "mut_two", Arguments: `{"text":"two"}`},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
finalContent: "done",
|
||||||
|
}
|
||||||
|
|
||||||
|
a := New(Options{
|
||||||
|
Provider: provider,
|
||||||
|
Model: "test",
|
||||||
|
SystemPrompt: "system",
|
||||||
|
ToolRegistry: tools.NewRegistry(tool1, tool2),
|
||||||
|
})
|
||||||
|
|
||||||
|
var out strings.Builder
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
done <- a.RunTurn(context.Background(), "run both", &out, nil)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Only the first tool should start.
|
||||||
|
select {
|
||||||
|
case name := <-started:
|
||||||
|
if name != "mut_one" {
|
||||||
|
t.Fatalf("expected mut_one to start first, got %q", name)
|
||||||
|
}
|
||||||
|
case <-make(chan struct{}):
|
||||||
|
t.Fatal("timed out waiting for first tool to start")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Second tool should NOT have started yet.
|
||||||
|
select {
|
||||||
|
case name := <-started:
|
||||||
|
t.Fatalf("second tool started too early: %q", name)
|
||||||
|
default:
|
||||||
|
// Expected: second tool hasn't started.
|
||||||
|
}
|
||||||
|
|
||||||
|
close(release)
|
||||||
|
if err := <-done; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parallelProvider emits a batch of tool calls on the first request, then returns finalContent.
|
||||||
|
type parallelProvider struct {
|
||||||
|
responses [][]llm.ToolCallDelta
|
||||||
|
finalContent string
|
||||||
|
calls int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parallelProvider) ChatStream(ctx context.Context, req llm.ChatRequest, emit func(llm.StreamEvent) error) error {
|
||||||
|
p.calls++
|
||||||
|
if p.calls <= len(p.responses) {
|
||||||
|
return emit(llm.StreamEvent{ToolCalls: p.responses[p.calls-1]})
|
||||||
|
}
|
||||||
|
return emit(llm.StreamEvent{Content: p.finalContent})
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,164 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"agentu/internal/agent"
|
||||||
|
"agentu/internal/session"
|
||||||
|
"agentu/internal/tools"
|
||||||
|
"agentu/internal/tui"
|
||||||
|
"agentu/pkg/config"
|
||||||
|
"agentu/pkg/llm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type cliOptions struct {
|
||||||
|
configPath string
|
||||||
|
yolo bool
|
||||||
|
plain bool
|
||||||
|
themeName string
|
||||||
|
resumeID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func Run() error {
|
||||||
|
opts := parseCLIOptions()
|
||||||
|
|
||||||
|
themeMode, err := tui.ParseThemeMode(opts.themeName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := loadConfig(opts.configPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
providerName, providerConfig, provider := defaultProvider(cfg, http.DefaultClient)
|
||||||
|
assistant := newAssistant(cfg, providerName, providerConfig, provider, opts.yolo, http.DefaultClient)
|
||||||
|
modelManager, err := newSessionManager(cfg, assistant, http.DefaultClient)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := initializeSession(modelManager, opts.resumeID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||||
|
defer stop()
|
||||||
|
|
||||||
|
return runInterface(ctx, assistant, modelManager, providerConfig, cfg.Agent.WorkingDir, opts, themeMode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseCLIOptions() cliOptions {
|
||||||
|
configPath := flag.String("config", config.DefaultPath, "path to YAML config")
|
||||||
|
yolo := flag.Bool("yolo", false, "also enable automatic file writes and shell execution")
|
||||||
|
plain := flag.Bool("plain", false, "use the simple line-based REPL instead of the TUI")
|
||||||
|
themeName := flag.String("theme", "light", "TUI theme: light or dark")
|
||||||
|
resumeID := flag.String("resume", "", "resume a specific session by ID")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
return cliOptions{
|
||||||
|
configPath: *configPath,
|
||||||
|
yolo: *yolo,
|
||||||
|
plain: *plain,
|
||||||
|
themeName: *themeName,
|
||||||
|
resumeID: *resumeID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadConfig(configPath string) (*config.Config, error) {
|
||||||
|
cfg, err := config.Load(configPath)
|
||||||
|
if err == nil {
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if bootstrapErr := bootstrapConfig(configPath); bootstrapErr != nil {
|
||||||
|
return nil, bootstrapErr
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("created default config at %s; edit it to set your API key and provider, then restart agentu", configPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultProvider(cfg *config.Config, httpClient *http.Client) (string, config.ProviderConfig, llm.Provider) {
|
||||||
|
providerName := cfg.DefaultProviderName()
|
||||||
|
providerConfig := cfg.Providers[providerName]
|
||||||
|
provider := llm.NewOpenAICompatibleClient(providerConfig.BaseURL, providerConfig.APIKey, httpClient)
|
||||||
|
return providerName, providerConfig, provider
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAssistant(cfg *config.Config, providerName string, providerConfig config.ProviderConfig, provider llm.Provider, yolo bool, httpClient *http.Client) *agent.Agent {
|
||||||
|
return agent.New(agent.Options{
|
||||||
|
Provider: provider,
|
||||||
|
ProviderName: providerName,
|
||||||
|
Model: providerConfig.Model,
|
||||||
|
Thinking: providerConfig.Thinking,
|
||||||
|
ThinkingParam: providerConfig.ThinkingParam,
|
||||||
|
ThinkingEnabled: providerConfig.ThinkingConfigured,
|
||||||
|
SystemPrompt: systemPrompt(cfg, yolo),
|
||||||
|
ToolRegistry: newToolRegistry(cfg.Agent.WorkingDir, yolo, httpClient),
|
||||||
|
ToolTimeout: cfg.Agent.ToolTimeout,
|
||||||
|
MaxContextTokens: maxContextTokens(cfg, providerConfig),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func systemPrompt(cfg *config.Config, yolo bool) string {
|
||||||
|
instructions := tools.AgentInstructions(cfg.Agent.WorkingDir, yolo)
|
||||||
|
return strings.TrimSpace(cfg.Agent.SystemPrompt + "\n\n" + instructions)
|
||||||
|
}
|
||||||
|
|
||||||
|
func maxContextTokens(cfg *config.Config, providerConfig config.ProviderConfig) int {
|
||||||
|
if cfg.Agent.MaxContextTokens != 0 {
|
||||||
|
return cfg.Agent.MaxContextTokens
|
||||||
|
}
|
||||||
|
return providerConfig.ContextTokens
|
||||||
|
}
|
||||||
|
|
||||||
|
func newToolRegistry(workingDir string, yolo bool, httpClient *http.Client) *tools.Registry {
|
||||||
|
var registry *tools.Registry
|
||||||
|
if yolo {
|
||||||
|
registry = tools.Builtins(workingDir)
|
||||||
|
} else {
|
||||||
|
registry = tools.ReadOnlyBuiltins(workingDir)
|
||||||
|
}
|
||||||
|
registry.Register(tools.NewBuiltinSearchTool(httpClient))
|
||||||
|
registry.Register(tools.NewBuiltinFetchTool(httpClient))
|
||||||
|
return registry
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSessionManager(cfg *config.Config, assistant *agent.Agent, httpClient *http.Client) (*session.Manager, error) {
|
||||||
|
home, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("resolve home dir: %w", err)
|
||||||
|
}
|
||||||
|
sessionsDir := filepath.Join(home, ".agentu", "sessions")
|
||||||
|
store, err := session.NewStore(sessionsDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("init session store: %w", err)
|
||||||
|
}
|
||||||
|
return session.NewManager(cfg, assistant, httpClient, store)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runInterface(ctx context.Context, assistant *agent.Agent, modelManager *session.Manager, providerConfig config.ProviderConfig, workingDir string, opts cliOptions, themeMode tui.ThemeMode) error {
|
||||||
|
if opts.plain {
|
||||||
|
return repl(ctx, assistant, modelManager)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := tui.Run(ctx, assistant, tui.Options{
|
||||||
|
ModelName: providerConfig.Model,
|
||||||
|
Yolo: opts.yolo,
|
||||||
|
ThemeMode: themeMode,
|
||||||
|
ModelManager: modelManager,
|
||||||
|
WorkingDir: workingDir,
|
||||||
|
})
|
||||||
|
printExitSession(modelManager)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"agentu/pkg/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultConfigContent = `# agentu configuration
|
||||||
|
# Replace the placeholder values below with your actual provider settings.
|
||||||
|
# See agentu.example.yaml for the full reference.
|
||||||
|
providers:
|
||||||
|
default:
|
||||||
|
base_url: https://api.openai.com
|
||||||
|
api_key: YOUR_API_KEY_HERE
|
||||||
|
models:
|
||||||
|
- id: gpt-4o
|
||||||
|
context: 128000
|
||||||
|
- id: gpt-4o-mini
|
||||||
|
context: 128000
|
||||||
|
`
|
||||||
|
|
||||||
|
func bootstrapConfig(configPath string) error {
|
||||||
|
resolved, err := config.ResolvePath(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("resolve config path: %w", err)
|
||||||
|
}
|
||||||
|
dir := filepath.Dir(resolved)
|
||||||
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||||
|
return fmt.Errorf("create config directory: %w", err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(resolved, []byte(defaultConfigContent), 0o644); err != nil {
|
||||||
|
return fmt.Errorf("write default config: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"agentu/internal/agent"
|
||||||
|
"agentu/internal/session"
|
||||||
|
)
|
||||||
|
|
||||||
|
func repl(ctx context.Context, assistant *agent.Agent, modelManager *session.Manager) error {
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
|
||||||
|
|
||||||
|
fmt.Println("agentu")
|
||||||
|
fmt.Println("Type /exit to quit, /clear to reset context, /compact to compress context.")
|
||||||
|
for {
|
||||||
|
fmt.Print("> ")
|
||||||
|
if !scanner.Scan() {
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
printExitSession(modelManager)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
input := strings.TrimSpace(scanner.Text())
|
||||||
|
switch input {
|
||||||
|
case "":
|
||||||
|
continue
|
||||||
|
case "/exit", "/quit":
|
||||||
|
printExitSession(modelManager)
|
||||||
|
return nil
|
||||||
|
case "/clear":
|
||||||
|
assistant.Clear()
|
||||||
|
fmt.Println("context cleared")
|
||||||
|
continue
|
||||||
|
case "/compact":
|
||||||
|
if err := assistant.Compact(ctx); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "compact error:", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("context compacted")
|
||||||
|
if modelManager != nil {
|
||||||
|
_ = modelManager.Save()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := assistant.RunTurn(ctx, input, os.Stdout, os.Stderr); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "\nerror:", err)
|
||||||
|
}
|
||||||
|
_ = modelManager.Save()
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"agentu/internal/session"
|
||||||
|
)
|
||||||
|
|
||||||
|
func initializeSession(modelManager *session.Manager, resumeID string) error {
|
||||||
|
if resumeID != "" {
|
||||||
|
if err := modelManager.Resume(resumeID); err != nil {
|
||||||
|
return fmt.Errorf("resume session: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if _, err := modelManager.NewSession(); err != nil {
|
||||||
|
return fmt.Errorf("start new session: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func printExitSession(m *session.Manager) {
|
||||||
|
if m == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = m.Save()
|
||||||
|
id := m.CurrentSessionID()
|
||||||
|
name := m.CurrentSessionName()
|
||||||
|
if id != "" {
|
||||||
|
if name != "" && name != id {
|
||||||
|
fmt.Fprintf(os.Stderr, "session: %s (%s)\n", id, name)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stderr, "session: %s\n", id)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stderr, "resume: agentu --resume %s\n", id)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -288,9 +288,13 @@ func (m *Manager) providerNames() []string {
|
|||||||
return names
|
return names
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Manager) AvailableModels() []string {
|
||||||
|
return append([]string(nil), m.provider.Models...)
|
||||||
|
}
|
||||||
|
|
||||||
func contains(values []string, value string) bool {
|
func contains(values []string, value string) bool {
|
||||||
for _, item := range values {
|
for _, v := range values {
|
||||||
if item == value {
|
if v == value {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,6 +109,18 @@ func TestManagerRejectsUnknownModelAndThinking(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestManagerExposesModelCommandCandidates(t *testing.T) {
|
||||||
|
manager, _, _ := testManager(t)
|
||||||
|
models := manager.AvailableModels()
|
||||||
|
if len(models) != 2 || models[0] != "model-a" || models[1] != "model-b" {
|
||||||
|
t.Fatalf("AvailableModels() = %v", models)
|
||||||
|
}
|
||||||
|
models[0] = "mutated"
|
||||||
|
if got := manager.AvailableModels()[0]; got != "model-a" {
|
||||||
|
t.Fatalf("mutation leaked: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestManagerSaveAndResume(t *testing.T) {
|
func TestManagerSaveAndResume(t *testing.T) {
|
||||||
manager, assistant, _ := testManager(t)
|
manager, assistant, _ := testManager(t)
|
||||||
|
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ type FileWriteTool struct {
|
|||||||
func NewFileWriteTool(workingDir string) *FileWriteTool {
|
func NewFileWriteTool(workingDir string) *FileWriteTool {
|
||||||
return &FileWriteTool{workingDir: workingDir}
|
return &FileWriteTool{workingDir: workingDir}
|
||||||
}
|
}
|
||||||
|
func (*FileWriteTool) Mutates() bool { return true }
|
||||||
|
|
||||||
func (t *FileWriteTool) Definition() llm.Tool {
|
func (t *FileWriteTool) Definition() llm.Tool {
|
||||||
return llm.Tool{
|
return llm.Tool{
|
||||||
|
|||||||
@@ -0,0 +1,178 @@
|
|||||||
|
package tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"agentu/pkg/llm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FileEditTool struct {
|
||||||
|
workingDir string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFileEditTool(workingDir string) *FileEditTool {
|
||||||
|
return &FileEditTool{workingDir: workingDir}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*FileEditTool) Mutates() bool { return true }
|
||||||
|
|
||||||
|
func (t *FileEditTool) Definition() llm.Tool {
|
||||||
|
return llm.Tool{
|
||||||
|
Type: "function",
|
||||||
|
Function: llm.ToolFunction{
|
||||||
|
Name: "file_edit",
|
||||||
|
Description: "Edit an existing UTF-8 text file by replacing an inclusive 1-based line range or an exact old text pattern.",
|
||||||
|
Parameters: JSONSchema(`{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"path": {"type": "string", "description": "Existing file path relative to the configured agent working directory."},
|
||||||
|
"start_line": {"type": "integer", "description": "1-based first line for a line-range edit."},
|
||||||
|
"end_line": {"type": "integer", "description": "Optional 1-based final line; defaults to start_line when omitted."},
|
||||||
|
"old": {"type": "string", "description": "Exact text to replace for a pattern edit; not regex."},
|
||||||
|
"new": {"type": "string", "description": "Replacement text; may be empty string for deletion."},
|
||||||
|
"replace_all": {"type": "boolean", "description": "When true, replace every exact old occurrence; otherwise require exactly one occurrence."}
|
||||||
|
},
|
||||||
|
"required": ["path", "new"],
|
||||||
|
"additionalProperties": false
|
||||||
|
}`),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type fileEditArgs struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
StartLine int `json:"start_line"`
|
||||||
|
EndLine int `json:"end_line"`
|
||||||
|
Old string `json:"old"`
|
||||||
|
New *string `json:"new"`
|
||||||
|
ReplaceAll bool `json:"replace_all"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *FileEditTool) Execute(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||||
|
var args fileEditArgs
|
||||||
|
if err := json.Unmarshal(raw, &args); err != nil {
|
||||||
|
return "", fmt.Errorf("parse arguments: %w", err)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(args.Path) == "" {
|
||||||
|
return "", errors.New("path is required")
|
||||||
|
}
|
||||||
|
if args.New == nil {
|
||||||
|
return "", errors.New("new is required")
|
||||||
|
}
|
||||||
|
path, err := resolvePath(t.workingDir, args.Path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("cannot access %s: %w", args.Path, err)
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
return "", fmt.Errorf("%s is a directory, not a file", args.Path)
|
||||||
|
}
|
||||||
|
perm := info.Mode().Perm()
|
||||||
|
|
||||||
|
hasLine := args.StartLine > 0 || args.EndLine > 0
|
||||||
|
hasOld := strings.TrimSpace(args.Old) != ""
|
||||||
|
|
||||||
|
if hasLine && hasOld {
|
||||||
|
return "", errors.New("provide either start_line/end_line or old, not both")
|
||||||
|
}
|
||||||
|
if !hasLine && !hasOld {
|
||||||
|
return "", errors.New("provide either start_line/end_line or old")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
content := string(data)
|
||||||
|
|
||||||
|
var result string
|
||||||
|
if hasOld {
|
||||||
|
result, err = editByPattern(content, args.Path, args.Old, *args.New, args.ReplaceAll)
|
||||||
|
} else {
|
||||||
|
result, err = editByLineRange(content, args.Path, args.StartLine, args.EndLine, *args.New)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(path, []byte(result), perm); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case hasOld:
|
||||||
|
count := strings.Count(content, args.Old)
|
||||||
|
if args.ReplaceAll {
|
||||||
|
return fmt.Sprintf("edited %s: replaced %d occurrences", args.Path, count), nil
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("edited %s: replaced 1 occurrence", args.Path), nil
|
||||||
|
default:
|
||||||
|
endLine := args.EndLine
|
||||||
|
if endLine == 0 {
|
||||||
|
endLine = args.StartLine
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("edited %s: replaced lines %d-%d", args.Path, args.StartLine, endLine), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func editByLineRange(content, displayPath string, startLine, endLine int, replacement string) (string, error) {
|
||||||
|
if startLine < 1 {
|
||||||
|
return "", errors.New("start_line must be >= 1")
|
||||||
|
}
|
||||||
|
if endLine == 0 {
|
||||||
|
endLine = startLine
|
||||||
|
}
|
||||||
|
if endLine < startLine {
|
||||||
|
return "", fmt.Errorf("end_line (%d) must be >= start_line (%d)", endLine, startLine)
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(content, "\n")
|
||||||
|
if startLine > len(lines) {
|
||||||
|
return "", fmt.Errorf("line range %d-%d is outside file with %d lines", startLine, endLine, len(lines))
|
||||||
|
}
|
||||||
|
if endLine > len(lines) {
|
||||||
|
return "", fmt.Errorf("line range %d-%d is outside file with %d lines", startLine, endLine, len(lines))
|
||||||
|
}
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString(strings.Join(lines[:startLine-1], "\n"))
|
||||||
|
if startLine > 1 {
|
||||||
|
b.WriteString("\n")
|
||||||
|
}
|
||||||
|
b.WriteString(replacement)
|
||||||
|
if endLine < len(lines) {
|
||||||
|
if !strings.HasSuffix(replacement, "\n") && replacement != "" {
|
||||||
|
b.WriteString("\n")
|
||||||
|
}
|
||||||
|
b.WriteString(strings.Join(lines[endLine:], "\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func editByPattern(content, displayPath, old, replacement string, replaceAll bool) (string, error) {
|
||||||
|
count := strings.Count(content, old)
|
||||||
|
if count == 0 {
|
||||||
|
return "", errors.New("old text not found")
|
||||||
|
}
|
||||||
|
if !replaceAll && count != 1 {
|
||||||
|
return "", fmt.Errorf("old text matched %d occurrences; set replace_all=true or provide a more specific old value", count)
|
||||||
|
}
|
||||||
|
return strings.ReplaceAll(content, old, replacement), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
package tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFileEditReplacesLineRange(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := filepath.Join(dir, "notes.txt")
|
||||||
|
if err := os.WriteFile(path, []byte("one\ntwo\nthree\n"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
edit := NewFileEditTool(dir)
|
||||||
|
out, err := edit.Execute(context.Background(), json.RawMessage(`{"path":"notes.txt","start_line":2,"end_line":2,"new":"TWO\n"}`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "replaced lines 2-2") {
|
||||||
|
t.Fatalf("output = %q", out)
|
||||||
|
}
|
||||||
|
got, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(got) != "one\nTWO\nthree\n" {
|
||||||
|
t.Fatalf("file content = %q", string(got))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileEditReplacesExactPattern(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := filepath.Join(dir, "notes.txt")
|
||||||
|
if err := os.WriteFile(path, []byte("alpha beta alpha"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
edit := NewFileEditTool(dir)
|
||||||
|
out, err := edit.Execute(context.Background(), json.RawMessage(`{"path":"notes.txt","old":"beta","new":"BETA"}`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "replaced 1 occurrence") {
|
||||||
|
t.Fatalf("output = %q", out)
|
||||||
|
}
|
||||||
|
got, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(got) != "alpha BETA alpha" {
|
||||||
|
t.Fatalf("file content = %q", string(got))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileEditRejectsAmbiguousPattern(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := filepath.Join(dir, "notes.txt")
|
||||||
|
if err := os.WriteFile(path, []byte("x x"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
edit := NewFileEditTool(dir)
|
||||||
|
_, err := edit.Execute(context.Background(), json.RawMessage(`{"path":"notes.txt","old":"x","new":"y"}`))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for ambiguous pattern")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "matched 2 occurrences") {
|
||||||
|
t.Fatalf("error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileEditReplaceAll(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := filepath.Join(dir, "notes.txt")
|
||||||
|
if err := os.WriteFile(path, []byte("x x"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
edit := NewFileEditTool(dir)
|
||||||
|
out, err := edit.Execute(context.Background(), json.RawMessage(`{"path":"notes.txt","old":"x","new":"y","replace_all":true}`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "replaced 2 occurrences") {
|
||||||
|
t.Fatalf("output = %q", out)
|
||||||
|
}
|
||||||
|
got, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(got) != "y y" {
|
||||||
|
t.Fatalf("file content = %q", string(got))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileEditRejectsEscapingPath(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
edit := NewFileEditTool(dir)
|
||||||
|
_, err := edit.Execute(context.Background(), json.RawMessage(`{"path":"../outside.txt","old":"x","new":"y"}`))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for escaping path")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "escapes") {
|
||||||
|
t.Fatalf("error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileEditRejectsMissingNew(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := filepath.Join(dir, "notes.txt")
|
||||||
|
if err := os.WriteFile(path, []byte("hello"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
edit := NewFileEditTool(dir)
|
||||||
|
_, err := edit.Execute(context.Background(), json.RawMessage(`{"path":"notes.txt","old":"hello"}`))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for missing new")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "new is required") {
|
||||||
|
t.Fatalf("error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileEditRejectsNonExistentFile(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
edit := NewFileEditTool(dir)
|
||||||
|
_, err := edit.Execute(context.Background(), json.RawMessage(`{"path":"missing.txt","old":"x","new":"y"}`))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for non-existent file")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,246 @@
|
|||||||
|
package tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"agentu/pkg/llm"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultGitLogMaxCount = 20
|
||||||
|
maxGitLogMaxCount = 100
|
||||||
|
)
|
||||||
|
|
||||||
|
func runGit(ctx context.Context, workingDir string, args ...string) (string, error) {
|
||||||
|
gitArgs := append([]string{"-C", workingDir}, args...)
|
||||||
|
cmd := exec.CommandContext(ctx, "git", gitArgs...)
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
text := FormatResult(string(output))
|
||||||
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), "context deadline exceeded") || strings.Contains(err.Error(), "signal: killed") {
|
||||||
|
if text != "" {
|
||||||
|
return text, fmt.Errorf("git command timed out: %w", err)
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("git command timed out: %w", err)
|
||||||
|
}
|
||||||
|
if text != "" {
|
||||||
|
return text, fmt.Errorf("git command failed: %w", err)
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("git command failed: %w", err)
|
||||||
|
}
|
||||||
|
return text, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func gitPathspec(baseDir, path string) (string, error) {
|
||||||
|
if strings.TrimSpace(path) == "" {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
resolved, err := resolvePath(baseDir, path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
rel, err := filepath.Rel(baseDir, resolved)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return filepath.ToSlash(filepath.Clean(rel)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeGitLogMaxCount(value int) int {
|
||||||
|
if value <= 0 {
|
||||||
|
return defaultGitLogMaxCount
|
||||||
|
}
|
||||||
|
if value > maxGitLogMaxCount {
|
||||||
|
return maxGitLogMaxCount
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
// git_status
|
||||||
|
|
||||||
|
type GitStatusTool struct {
|
||||||
|
workingDir string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGitStatusTool(workingDir string) *GitStatusTool {
|
||||||
|
return &GitStatusTool{workingDir: workingDir}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GitStatusTool) Definition() llm.Tool {
|
||||||
|
return llm.Tool{
|
||||||
|
Type: "function",
|
||||||
|
Function: llm.ToolFunction{
|
||||||
|
Name: "git_status",
|
||||||
|
Description: "Show changed files using git status --short.",
|
||||||
|
Parameters: JSONSchema(`{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"path": {"type": "string", "description": "Optional path to limit status to. Relative paths are resolved from the configured agent working directory."}
|
||||||
|
},
|
||||||
|
"additionalProperties": false
|
||||||
|
}`),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type gitStatusArgs struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GitStatusTool) Execute(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||||
|
var args gitStatusArgs
|
||||||
|
if len(raw) > 0 {
|
||||||
|
if err := json.Unmarshal(raw, &args); err != nil {
|
||||||
|
return "", fmt.Errorf("parse arguments: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gitArgs := []string{"status", "--short"}
|
||||||
|
if args.Path != "" {
|
||||||
|
pathspec, err := gitPathspec(t.workingDir, args.Path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
gitArgs = append(gitArgs, "--", pathspec)
|
||||||
|
}
|
||||||
|
text, err := runGit(ctx, t.workingDir, gitArgs...)
|
||||||
|
if err != nil {
|
||||||
|
return text, err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(text) == "" {
|
||||||
|
return "Working tree clean.", nil
|
||||||
|
}
|
||||||
|
return "Changed files:\n" + text, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// git_diff
|
||||||
|
|
||||||
|
type GitDiffTool struct {
|
||||||
|
workingDir string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGitDiffTool(workingDir string) *GitDiffTool {
|
||||||
|
return &GitDiffTool{workingDir: workingDir}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GitDiffTool) Definition() llm.Tool {
|
||||||
|
return llm.Tool{
|
||||||
|
Type: "function",
|
||||||
|
Function: llm.ToolFunction{
|
||||||
|
Name: "git_diff",
|
||||||
|
Description: "Show unstaged or staged git diff output, optionally as --stat.",
|
||||||
|
Parameters: JSONSchema(`{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"path": {"type": "string", "description": "Optional path to limit diff to. Relative paths are resolved from the configured agent working directory."},
|
||||||
|
"staged": {"type": "boolean", "description": "Show staged diff instead of unstaged."},
|
||||||
|
"stat": {"type": "boolean", "description": "Show diffstat summary instead of full diff."}
|
||||||
|
},
|
||||||
|
"additionalProperties": false
|
||||||
|
}`),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type gitDiffArgs struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
Staged bool `json:"staged"`
|
||||||
|
Stat bool `json:"stat"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GitDiffTool) Execute(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||||
|
var args gitDiffArgs
|
||||||
|
if len(raw) > 0 {
|
||||||
|
if err := json.Unmarshal(raw, &args); err != nil {
|
||||||
|
return "", fmt.Errorf("parse arguments: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gitArgs := []string{"diff"}
|
||||||
|
if args.Staged {
|
||||||
|
gitArgs = append(gitArgs, "--cached")
|
||||||
|
}
|
||||||
|
if args.Stat {
|
||||||
|
gitArgs = append(gitArgs, "--stat")
|
||||||
|
}
|
||||||
|
if args.Path != "" {
|
||||||
|
pathspec, err := gitPathspec(t.workingDir, args.Path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
gitArgs = append(gitArgs, "--", pathspec)
|
||||||
|
}
|
||||||
|
text, err := runGit(ctx, t.workingDir, gitArgs...)
|
||||||
|
if err != nil {
|
||||||
|
return text, err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(text) == "" {
|
||||||
|
if args.Staged {
|
||||||
|
return "No staged diff.", nil
|
||||||
|
}
|
||||||
|
return "No unstaged diff.", nil
|
||||||
|
}
|
||||||
|
return text, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// git_log
|
||||||
|
|
||||||
|
type GitLogTool struct {
|
||||||
|
workingDir string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGitLogTool(workingDir string) *GitLogTool {
|
||||||
|
return &GitLogTool{workingDir: workingDir}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GitLogTool) Definition() llm.Tool {
|
||||||
|
return llm.Tool{
|
||||||
|
Type: "function",
|
||||||
|
Function: llm.ToolFunction{
|
||||||
|
Name: "git_log",
|
||||||
|
Description: "Show recent git commits with git log --oneline --decorate.",
|
||||||
|
Parameters: JSONSchema(`{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"path": {"type": "string", "description": "Optional path to limit log to. Relative paths are resolved from the configured agent working directory."},
|
||||||
|
"max_count": {"type": "integer", "description": "Maximum number of commits to show. Defaults to 20, capped at 100."}
|
||||||
|
},
|
||||||
|
"additionalProperties": false
|
||||||
|
}`),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type gitLogArgs struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
MaxCount int `json:"max_count"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GitLogTool) Execute(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||||
|
var args gitLogArgs
|
||||||
|
if len(raw) > 0 {
|
||||||
|
if err := json.Unmarshal(raw, &args); err != nil {
|
||||||
|
return "", fmt.Errorf("parse arguments: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
maxCount := normalizeGitLogMaxCount(args.MaxCount)
|
||||||
|
gitArgs := []string{"log", "--oneline", "--decorate", fmt.Sprintf("-n%d", maxCount)}
|
||||||
|
if args.Path != "" {
|
||||||
|
pathspec, err := gitPathspec(t.workingDir, args.Path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
gitArgs = append(gitArgs, "--", pathspec)
|
||||||
|
}
|
||||||
|
text, err := runGit(ctx, t.workingDir, gitArgs...)
|
||||||
|
if err != nil {
|
||||||
|
return text, err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(text) == "" {
|
||||||
|
return "No git history.", nil
|
||||||
|
}
|
||||||
|
return text, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
package tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func requireGit(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
if _, err := exec.LookPath("git"); err != nil {
|
||||||
|
t.Skip("git not installed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runGitForTest(t *testing.T, dir string, args ...string) {
|
||||||
|
t.Helper()
|
||||||
|
gitArgs := append([]string{"-C", dir}, args...)
|
||||||
|
cmd := exec.Command("git", gitArgs...)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("git %v failed: %v\n%s", args, err, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initTestRepo(t *testing.T) string {
|
||||||
|
t.Helper()
|
||||||
|
dir := t.TempDir()
|
||||||
|
runGitForTest(t, dir, "init")
|
||||||
|
runGitForTest(t, dir, "-c", "user.email=test@example.com", "-c", "user.name=Test", "commit", "--allow-empty", "-m", "initial")
|
||||||
|
return dir
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGitStatusToolShowsChangedFiles(t *testing.T) {
|
||||||
|
requireGit(t)
|
||||||
|
dir := initTestRepo(t)
|
||||||
|
|
||||||
|
path := filepath.Join(dir, "tracked.txt")
|
||||||
|
if err := os.WriteFile(path, []byte("hello"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
runGitForTest(t, dir, "add", "tracked.txt")
|
||||||
|
runGitForTest(t, dir, "-c", "user.email=test@example.com", "-c", "user.name=Test", "commit", "-m", "add tracked")
|
||||||
|
|
||||||
|
if err := os.WriteFile(path, []byte("world"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tool := NewGitStatusTool(dir)
|
||||||
|
out, err := tool.Execute(context.Background(), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "Changed files:") {
|
||||||
|
t.Fatalf("output missing 'Changed files:': %q", out)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "M tracked.txt") {
|
||||||
|
t.Fatalf("output missing 'M tracked.txt': %q", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGitDiffToolShowsUnstagedDiff(t *testing.T) {
|
||||||
|
requireGit(t)
|
||||||
|
dir := initTestRepo(t)
|
||||||
|
|
||||||
|
path := filepath.Join(dir, "tracked.txt")
|
||||||
|
if err := os.WriteFile(path, []byte("hello"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
runGitForTest(t, dir, "add", "tracked.txt")
|
||||||
|
runGitForTest(t, dir, "-c", "user.email=test@example.com", "-c", "user.name=Test", "commit", "-m", "add tracked")
|
||||||
|
|
||||||
|
if err := os.WriteFile(path, []byte("world"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tool := NewGitDiffTool(dir)
|
||||||
|
out, err := tool.Execute(context.Background(), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "diff --git") {
|
||||||
|
t.Fatalf("output missing 'diff --git': %q", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGitDiffToolShowsStat(t *testing.T) {
|
||||||
|
requireGit(t)
|
||||||
|
dir := initTestRepo(t)
|
||||||
|
|
||||||
|
path := filepath.Join(dir, "tracked.txt")
|
||||||
|
if err := os.WriteFile(path, []byte("hello"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
runGitForTest(t, dir, "add", "tracked.txt")
|
||||||
|
runGitForTest(t, dir, "-c", "user.email=test@example.com", "-c", "user.name=Test", "commit", "-m", "add tracked")
|
||||||
|
|
||||||
|
if err := os.WriteFile(path, []byte("world"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tool := NewGitDiffTool(dir)
|
||||||
|
out, err := tool.Execute(context.Background(), json.RawMessage(`{"stat":true}`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "tracked.txt") {
|
||||||
|
t.Fatalf("output missing 'tracked.txt': %q", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGitLogToolShowsCommits(t *testing.T) {
|
||||||
|
requireGit(t)
|
||||||
|
dir := initTestRepo(t)
|
||||||
|
|
||||||
|
tool := NewGitLogTool(dir)
|
||||||
|
out, err := tool.Execute(context.Background(), json.RawMessage(`{"max_count":1}`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "initial") {
|
||||||
|
t.Fatalf("output missing 'initial': %q", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGitToolsRejectEscapingPath(t *testing.T) {
|
||||||
|
requireGit(t)
|
||||||
|
dir := initTestRepo(t)
|
||||||
|
|
||||||
|
tool := NewGitStatusTool(dir)
|
||||||
|
_, err := tool.Execute(context.Background(), json.RawMessage(`{"path":"../outside"}`))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for escaping path")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "escapes") {
|
||||||
|
t.Fatalf("error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ type ShellRunTool struct {
|
|||||||
func NewShellRunTool(workingDir string) *ShellRunTool {
|
func NewShellRunTool(workingDir string) *ShellRunTool {
|
||||||
return &ShellRunTool{workingDir: workingDir}
|
return &ShellRunTool{workingDir: workingDir}
|
||||||
}
|
}
|
||||||
|
func (*ShellRunTool) Mutates() bool { return true }
|
||||||
|
|
||||||
func (t *ShellRunTool) Definition() llm.Tool {
|
func (t *ShellRunTool) Definition() llm.Tool {
|
||||||
return llm.Tool{
|
return llm.Tool{
|
||||||
|
|||||||
+27
-3
@@ -19,6 +19,18 @@ type Tool interface {
|
|||||||
Execute(ctx context.Context, args json.RawMessage) (string, error)
|
Execute(ctx context.Context, args json.RawMessage) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MutatingTool is optionally implemented by tools that modify external state.
|
||||||
|
// It is used by the agent to determine whether tool calls can run in parallel.
|
||||||
|
type MutatingTool interface {
|
||||||
|
Mutates() bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsMutating reports whether tool implements MutatingTool and returns true.
|
||||||
|
func IsMutating(tool Tool) bool {
|
||||||
|
mutating, ok := tool.(MutatingTool)
|
||||||
|
return ok && mutating.Mutates()
|
||||||
|
}
|
||||||
|
|
||||||
type Registry struct {
|
type Registry struct {
|
||||||
tools map[string]Tool
|
tools map[string]Tool
|
||||||
primary []string
|
primary []string
|
||||||
@@ -66,30 +78,39 @@ func ReadOnlyBuiltins(workingDir string) *Registry {
|
|||||||
fileList := NewFileListTool(workingDir)
|
fileList := NewFileListTool(workingDir)
|
||||||
fileSearch := NewFileSearchTool(workingDir)
|
fileSearch := NewFileSearchTool(workingDir)
|
||||||
codeSymbols := NewCodeSymbolsTool(workingDir)
|
codeSymbols := NewCodeSymbolsTool(workingDir)
|
||||||
|
gitStatus := NewGitStatusTool(workingDir)
|
||||||
|
gitDiff := NewGitDiffTool(workingDir)
|
||||||
|
gitLog := NewGitLogTool(workingDir)
|
||||||
|
|
||||||
registry := NewRegistry(fileRead, fileList, fileSearch, codeSymbols)
|
registry := NewRegistry(fileRead, fileList, fileSearch, codeSymbols, gitStatus, gitDiff, gitLog)
|
||||||
registry.RegisterAlias("file.read", fileRead)
|
registry.RegisterAlias("file.read", fileRead)
|
||||||
registry.RegisterAlias("file.list", fileList)
|
registry.RegisterAlias("file.list", fileList)
|
||||||
registry.RegisterAlias("file.search", fileSearch)
|
registry.RegisterAlias("file.search", fileSearch)
|
||||||
registry.RegisterAlias("code.symbols", codeSymbols)
|
registry.RegisterAlias("code.symbols", codeSymbols)
|
||||||
|
registry.RegisterAlias("git.status", gitStatus)
|
||||||
|
registry.RegisterAlias("git.diff", gitDiff)
|
||||||
|
registry.RegisterAlias("git.log", gitLog)
|
||||||
return registry
|
return registry
|
||||||
}
|
}
|
||||||
|
|
||||||
func Builtins(workingDir string) *Registry {
|
func Builtins(workingDir string) *Registry {
|
||||||
registry := ReadOnlyBuiltins(workingDir)
|
registry := ReadOnlyBuiltins(workingDir)
|
||||||
fileWrite := NewFileWriteTool(workingDir)
|
fileWrite := NewFileWriteTool(workingDir)
|
||||||
|
fileEdit := NewFileEditTool(workingDir)
|
||||||
shellRun := NewShellRunTool(workingDir)
|
shellRun := NewShellRunTool(workingDir)
|
||||||
registry.Register(fileWrite)
|
registry.Register(fileWrite)
|
||||||
|
registry.Register(fileEdit)
|
||||||
registry.Register(shellRun)
|
registry.Register(shellRun)
|
||||||
registry.RegisterAlias("file.write", fileWrite)
|
registry.RegisterAlias("file.write", fileWrite)
|
||||||
|
registry.RegisterAlias("file.edit", fileEdit)
|
||||||
registry.RegisterAlias("shell.run", shellRun)
|
registry.RegisterAlias("shell.run", shellRun)
|
||||||
return registry
|
return registry
|
||||||
}
|
}
|
||||||
|
|
||||||
func AgentInstructions(workingDir string, yolo bool) string {
|
func AgentInstructions(workingDir string, yolo bool) string {
|
||||||
searchInstructions := webSearchInstructions()
|
searchInstructions := webSearchInstructions()
|
||||||
readOnlyTools := "file_list, file_read, file_search, code_symbols, web_search, web_fetch"
|
readOnlyTools := "file_list, file_read, file_search, code_symbols, git_status, git_diff, git_log, web_search, web_fetch"
|
||||||
yoloTools := "file_list, file_read, file_search, code_symbols, file_write, shell_run, web_search, web_fetch"
|
yoloTools := "file_list, file_read, file_search, code_symbols, file_write, file_edit, shell_run, git_status, git_diff, git_log, web_search, web_fetch"
|
||||||
if yolo {
|
if yolo {
|
||||||
return fmt.Sprintf(`Local project context:
|
return fmt.Sprintf(`Local project context:
|
||||||
- The project working directory is %q.
|
- The project working directory is %q.
|
||||||
@@ -99,7 +120,9 @@ func AgentInstructions(workingDir string, yolo bool) string {
|
|||||||
- When the user asks to inspect, list, search, or read project files, use file_list, file_search, and file_read instead of saying you cannot access local files.
|
- When the user asks to inspect, list, search, or read project files, use file_list, file_search, and file_read instead of saying you cannot access local files.
|
||||||
- When the user asks about code structure, symbols, functions, methods, types, or definitions in Go files, use code_symbols before reading whole files.
|
- When the user asks about code structure, symbols, functions, methods, types, or definitions in Go files, use code_symbols before reading whole files.
|
||||||
- When the user asks to run, execute, test, check, debug, or inspect a shell command, use shell_run. This includes local commands written in backticks, such as go, make, git, pwd, ls, ps, and similar terminal commands.
|
- When the user asks to run, execute, test, check, debug, or inspect a shell command, use shell_run. This includes local commands written in backticks, such as go, make, git, pwd, ls, ps, and similar terminal commands.
|
||||||
|
- When editing existing files, prefer file_edit for small or localized changes; use file_write only for creating files, replacing whole files intentionally, or appending.
|
||||||
- Do not use file tools as a substitute for a command execution request.
|
- Do not use file tools as a substitute for a command execution request.
|
||||||
|
- When the user asks about changed files, diffs, or git history, use git_status, git_diff, or git_log instead of shell_run.
|
||||||
- Do not start interactive or long-running sessions. Prefer bounded, non-interactive local commands with clear completion conditions.
|
- Do not start interactive or long-running sessions. Prefer bounded, non-interactive local commands with clear completion conditions.
|
||||||
- %s
|
- %s
|
||||||
- Tool paths should normally be relative to the project working directory.
|
- Tool paths should normally be relative to the project working directory.
|
||||||
@@ -113,6 +136,7 @@ func AgentInstructions(workingDir string, yolo bool) string {
|
|||||||
- When the user asks to inspect, list, search, or read project files, use file_list, file_search, and file_read instead of saying you cannot access local files.
|
- When the user asks to inspect, list, search, or read project files, use file_list, file_search, and file_read instead of saying you cannot access local files.
|
||||||
- When the user asks about code structure, symbols, functions, methods, types, or definitions in Go files, use code_symbols before reading whole files.
|
- When the user asks about code structure, symbols, functions, methods, types, or definitions in Go files, use code_symbols before reading whole files.
|
||||||
- Shell command execution is disabled in this mode. When the user asks to run, execute, test, check, debug, or inspect a shell command, do not use file tools as a substitute; explain that they need to restart agentu with --yolo to enable shell_run.
|
- Shell command execution is disabled in this mode. When the user asks to run, execute, test, check, debug, or inspect a shell command, do not use file tools as a substitute; explain that they need to restart agentu with --yolo to enable shell_run.
|
||||||
|
- When the user asks about changed files, diffs, or git history, use git_status, git_diff, or git_log instead of shell_run.
|
||||||
- %s
|
- %s
|
||||||
- Tool paths should normally be relative to the project working directory.
|
- Tool paths should normally be relative to the project working directory.
|
||||||
- Available tools: %s.`, workingDir, searchInstructions, readOnlyTools)
|
- Available tools: %s.`, workingDir, searchInstructions, readOnlyTools)
|
||||||
|
|||||||
@@ -104,12 +104,12 @@ func TestReadOnlyBuiltinsExposeOpenAICompatibleNamesAndAliases(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, want := range []string{"file_list", "file_read", "file_search", "code_symbols"} {
|
for _, want := range []string{"file_list", "file_read", "file_search", "code_symbols", "git_status", "git_diff", "git_log"} {
|
||||||
if !slices.Contains(names, want) {
|
if !slices.Contains(names, want) {
|
||||||
t.Fatalf("definitions missing %s: %#v", want, names)
|
t.Fatalf("definitions missing %s: %#v", want, names)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, alias := range []string{"file.list", "file.read", "file.search", "code.symbols"} {
|
for _, alias := range []string{"file.list", "file.read", "file.search", "code.symbols", "git.status", "git.diff", "git.log"} {
|
||||||
if _, ok := registry.Get(alias); !ok {
|
if _, ok := registry.Get(alias); !ok {
|
||||||
t.Fatalf("alias missing: %s", alias)
|
t.Fatalf("alias missing: %s", alias)
|
||||||
}
|
}
|
||||||
@@ -117,6 +117,9 @@ func TestReadOnlyBuiltinsExposeOpenAICompatibleNamesAndAliases(t *testing.T) {
|
|||||||
if _, ok := registry.Get("file_write"); ok {
|
if _, ok := registry.Get("file_write"); ok {
|
||||||
t.Fatal("read-only registry should not expose file_write")
|
t.Fatal("read-only registry should not expose file_write")
|
||||||
}
|
}
|
||||||
|
if _, ok := registry.Get("file_edit"); ok {
|
||||||
|
t.Fatal("read-only registry should not expose file_edit")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCodeSymbolsToolListsGoSymbols(t *testing.T) {
|
func TestCodeSymbolsToolListsGoSymbols(t *testing.T) {
|
||||||
@@ -170,7 +173,7 @@ func TestRegistryCanExposeWebSearch(t *testing.T) {
|
|||||||
|
|
||||||
func TestBuiltinsExposeYoloTools(t *testing.T) {
|
func TestBuiltinsExposeYoloTools(t *testing.T) {
|
||||||
registry := Builtins(t.TempDir())
|
registry := Builtins(t.TempDir())
|
||||||
for _, name := range []string{"file_write", "shell_run", "file.write", "shell.run"} {
|
for _, name := range []string{"file_write", "file_edit", "shell_run", "file.write", "file.edit", "shell.run"} {
|
||||||
if _, ok := registry.Get(name); !ok {
|
if _, ok := registry.Get(name); !ok {
|
||||||
t.Fatalf("tool missing: %s", name)
|
t.Fatalf("tool missing: %s", name)
|
||||||
}
|
}
|
||||||
@@ -179,14 +182,14 @@ func TestBuiltinsExposeYoloTools(t *testing.T) {
|
|||||||
|
|
||||||
func TestAgentInstructionsExplainCommandModes(t *testing.T) {
|
func TestAgentInstructionsExplainCommandModes(t *testing.T) {
|
||||||
readOnly := AgentInstructions("/tmp/project", false)
|
readOnly := AgentInstructions("/tmp/project", false)
|
||||||
for _, want := range []string{"Shell command execution is disabled", "--yolo", "Do not call tools for greetings", "Do not explore the project preemptively", "Do not call file_list just to discover context"} {
|
for _, want := range []string{"Shell command execution is disabled", "--yolo", "Do not call tools for greetings", "Do not explore the project preemptively", "Do not call file_list just to discover context", "git_status, git_diff, git_log"} {
|
||||||
if !strings.Contains(readOnly, want) {
|
if !strings.Contains(readOnly, want) {
|
||||||
t.Fatalf("read-only instructions missing %q:\n%s", want, readOnly)
|
t.Fatalf("read-only instructions missing %q:\n%s", want, readOnly)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
yolo := AgentInstructions("/tmp/project", true)
|
yolo := AgentInstructions("/tmp/project", true)
|
||||||
for _, want := range []string{"use shell_run", "commands written in backticks", "Do not start interactive", "non-interactive local commands", "Do not call tools for greetings"} {
|
for _, want := range []string{"use shell_run", "commands written in backticks", "Do not start interactive", "non-interactive local commands", "Do not call tools for greetings", "prefer file_edit"} {
|
||||||
if !strings.Contains(yolo, want) {
|
if !strings.Contains(yolo, want) {
|
||||||
t.Fatalf("yolo instructions missing %q:\n%s", want, yolo)
|
t.Fatalf("yolo instructions missing %q:\n%s", want, yolo)
|
||||||
}
|
}
|
||||||
@@ -195,7 +198,7 @@ func TestAgentInstructionsExplainCommandModes(t *testing.T) {
|
|||||||
|
|
||||||
func TestAgentInstructionsExplainWebSearch(t *testing.T) {
|
func TestAgentInstructionsExplainWebSearch(t *testing.T) {
|
||||||
withSearch := AgentInstructions("/tmp/project", false)
|
withSearch := AgentInstructions("/tmp/project", false)
|
||||||
for _, want := range []string{"use web_search", "Use web_fetch", "latest information", "source URLs", "Available tools: file_list, file_read, file_search, code_symbols, web_search, web_fetch"} {
|
for _, want := range []string{"use web_search", "Use web_fetch", "latest information", "source URLs", "Available tools: file_list, file_read, file_search, code_symbols, git_status, git_diff, git_log, web_search, web_fetch"} {
|
||||||
if !strings.Contains(withSearch, want) {
|
if !strings.Contains(withSearch, want) {
|
||||||
t.Fatalf("web search instructions missing %q:\n%s", want, withSearch)
|
t.Fatalf("web search instructions missing %q:\n%s", want, withSearch)
|
||||||
}
|
}
|
||||||
@@ -257,3 +260,16 @@ func TestResolvePathRejectsDotDotThatEscapes(t *testing.T) {
|
|||||||
t.Fatal("expected error for path escaping via ..")
|
t.Fatal("expected error for path escaping via ..")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsMutating(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
if IsMutating(NewFileReadTool(dir)) {
|
||||||
|
t.Fatal("FileReadTool should not be mutating")
|
||||||
|
}
|
||||||
|
if !IsMutating(NewFileWriteTool(dir)) {
|
||||||
|
t.Fatal("FileWriteTool should be mutating")
|
||||||
|
}
|
||||||
|
if !IsMutating(NewShellRunTool(dir)) {
|
||||||
|
t.Fatal("ShellRunTool should be mutating")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+524
-107
@@ -50,6 +50,7 @@ type ModelManager interface {
|
|||||||
Rename(name string) (string, error)
|
Rename(name string) (string, error)
|
||||||
Sessions() (string, error)
|
Sessions() (string, error)
|
||||||
NewSession() (string, error)
|
NewSession() (string, error)
|
||||||
|
AvailableModels() []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(ctx context.Context, assistant *agent.Agent, opts Options) error {
|
func Run(ctx context.Context, assistant *agent.Agent, opts Options) error {
|
||||||
@@ -90,12 +91,13 @@ type model struct {
|
|||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
|
|
||||||
messages []message
|
messages []message
|
||||||
status string
|
status string
|
||||||
contextUsage string
|
contextUsage string
|
||||||
inputHistory []string
|
inputHistory []string
|
||||||
historyIndex int
|
historyIndex int
|
||||||
draftInput string
|
draftInput string
|
||||||
|
commandCursor int
|
||||||
|
|
||||||
running bool
|
running bool
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
@@ -133,6 +135,13 @@ type commandMatch struct {
|
|||||||
score int
|
score int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type commandSuggestion struct {
|
||||||
|
label string
|
||||||
|
description string
|
||||||
|
value string
|
||||||
|
executable bool
|
||||||
|
}
|
||||||
|
|
||||||
var slashCommands = []slashCommand{
|
var slashCommands = []slashCommand{
|
||||||
{Name: "/clear", Description: "reset context"},
|
{Name: "/clear", Description: "reset context"},
|
||||||
{Name: "/compact", Description: "compress context"},
|
{Name: "/compact", Description: "compress context"},
|
||||||
@@ -145,7 +154,7 @@ var slashCommands = []slashCommand{
|
|||||||
{Name: "/new", Description: "new session"},
|
{Name: "/new", Description: "new session"},
|
||||||
{Name: "/status", Description: "show changed files"},
|
{Name: "/status", Description: "show changed files"},
|
||||||
{Name: "/diff", Description: "show unstaged diff"},
|
{Name: "/diff", Description: "show unstaged diff"},
|
||||||
{Name: "/test", Description: "run project tests"},
|
{Name: "/theme", Description: "switch theme light|dark"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func newModel(ctx context.Context, assistant *agent.Agent, opts Options) model {
|
func newModel(ctx context.Context, assistant *agent.Agent, opts Options) model {
|
||||||
@@ -157,7 +166,7 @@ func newModel(ctx context.Context, assistant *agent.Agent, opts Options) model {
|
|||||||
st := th.styles()
|
st := th.styles()
|
||||||
|
|
||||||
input := textarea.New()
|
input := textarea.New()
|
||||||
input.Placeholder = "Message agentu..."
|
input.Placeholder = ""
|
||||||
input.Prompt = ""
|
input.Prompt = ""
|
||||||
input.ShowLineNumbers = false
|
input.ShowLineNumbers = false
|
||||||
input.MaxHeight = maxInputLines
|
input.MaxHeight = maxInputLines
|
||||||
@@ -197,8 +206,6 @@ func (m model) Init() tea.Cmd {
|
|||||||
m.syncMessages()
|
m.syncMessages()
|
||||||
return textarea.Blink
|
return textarea.Blink
|
||||||
}
|
}
|
||||||
|
|
||||||
// syncMessages rebuilds the TUI display messages from the agent's conversation history.
|
|
||||||
func (m *model) syncMessages() {
|
func (m *model) syncMessages() {
|
||||||
if m.agent == nil {
|
if m.agent == nil {
|
||||||
return
|
return
|
||||||
@@ -246,6 +253,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
if m.picking {
|
if m.picking {
|
||||||
return m.updatePicker(msg)
|
return m.updatePicker(msg)
|
||||||
}
|
}
|
||||||
|
if handled, next, cmd := m.updateCommandSuggestions(msg); handled {
|
||||||
|
return next, cmd
|
||||||
|
}
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c":
|
case "ctrl+c":
|
||||||
if m.cancel != nil {
|
if m.cancel != nil {
|
||||||
@@ -263,6 +273,29 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
return m.submit()
|
return m.submit()
|
||||||
|
case "up":
|
||||||
|
if m.running {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
if m.input.Line() == 0 {
|
||||||
|
m.previousInputHistory()
|
||||||
|
m.afterInputChanged()
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
case "down":
|
||||||
|
if m.running {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
value := m.input.Value()
|
||||||
|
lineCount := 1
|
||||||
|
if value != "" {
|
||||||
|
lineCount = len(strings.Split(value, "\n"))
|
||||||
|
}
|
||||||
|
if m.input.Line() >= lineCount-1 {
|
||||||
|
m.nextInputHistory()
|
||||||
|
m.afterInputChanged()
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
case "ctrl+p":
|
case "ctrl+p":
|
||||||
if m.running {
|
if m.running {
|
||||||
return m, nil
|
return m, nil
|
||||||
@@ -277,7 +310,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
m.nextInputHistory()
|
m.nextInputHistory()
|
||||||
m.afterInputChanged()
|
m.afterInputChanged()
|
||||||
return m, nil
|
return m, nil
|
||||||
case "alt+enter", "ctrl+j":
|
case "ctrl+j":
|
||||||
return m.insertInputNewline()
|
return m.insertInputNewline()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,8 +323,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
case toolLogMsg:
|
case toolLogMsg:
|
||||||
text := cleanToolLog(string(msg))
|
text := cleanToolLog(string(msg))
|
||||||
if text != "" {
|
if text != "" {
|
||||||
m.messages = append(m.messages, message{role: roleTool, content: text})
|
if !m.mergeToolLog(text) {
|
||||||
m.status = text
|
m.messages = append(m.messages, message{role: roleTool, content: text})
|
||||||
|
}
|
||||||
|
m.status = toolStatusText(text)
|
||||||
m.refreshViewport(true)
|
m.refreshViewport(true)
|
||||||
}
|
}
|
||||||
return m, m.waitForEvent()
|
return m, m.waitForEvent()
|
||||||
@@ -336,7 +371,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
if !m.running {
|
if !m.running {
|
||||||
nextInput, cmd := m.input.Update(msg)
|
nextInput, cmd := m.input.Update(msg)
|
||||||
m.input = nextInput
|
m.input = nextInput
|
||||||
m.afterInputChanged()
|
|
||||||
cmds = append(cmds, cmd)
|
cmds = append(cmds, cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,7 +388,7 @@ func (m model) View() string {
|
|||||||
|
|
||||||
if m.picking {
|
if m.picking {
|
||||||
parts := []string{m.viewport.View()}
|
parts := []string{m.viewport.View()}
|
||||||
parts = append(parts, m.pickerView(), m.modelMetaView(), m.footerView())
|
parts = append(parts, m.pickerView(), m.footerView())
|
||||||
body := lipgloss.JoinVertical(lipgloss.Left, parts...)
|
body := lipgloss.JoinVertical(lipgloss.Left, parts...)
|
||||||
return m.styles.App.Width(m.width).Height(m.height).Render(body)
|
return m.styles.App.Width(m.width).Height(m.height).Render(body)
|
||||||
}
|
}
|
||||||
@@ -363,7 +397,7 @@ func (m model) View() string {
|
|||||||
if activity := m.activityView(); activity != "" {
|
if activity := m.activityView(); activity != "" {
|
||||||
parts = append(parts, activity)
|
parts = append(parts, activity)
|
||||||
}
|
}
|
||||||
parts = append(parts, m.inputView(), m.modelMetaView(), m.footerView())
|
parts = append(parts, m.inputView(), m.footerView())
|
||||||
body := lipgloss.JoinVertical(lipgloss.Left, parts...)
|
body := lipgloss.JoinVertical(lipgloss.Left, parts...)
|
||||||
return m.styles.App.Width(m.width).Height(m.height).Render(body)
|
return m.styles.App.Width(m.width).Height(m.height).Render(body)
|
||||||
}
|
}
|
||||||
@@ -387,7 +421,11 @@ func (m *model) submit() (tea.Model, tea.Cmd) {
|
|||||||
m.status = "ready"
|
m.status = "ready"
|
||||||
m.refreshViewport(true)
|
m.refreshViewport(true)
|
||||||
return *m, nil
|
return *m, nil
|
||||||
case "/model", "/compact":
|
case "/model", "/compact", "/theme":
|
||||||
|
return m.runLocalCommand(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(input, "!") {
|
||||||
return m.runLocalCommand(input)
|
return m.runLocalCommand(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -436,6 +474,16 @@ func (m *model) insertInputNewline() (tea.Model, tea.Cmd) {
|
|||||||
m.historyIndex = -1
|
m.historyIndex = -1
|
||||||
m.draftInput = ""
|
m.draftInput = ""
|
||||||
m.afterInputChanged()
|
m.afterInputChanged()
|
||||||
|
// The textarea's repositionView was called inside Update with the old
|
||||||
|
// viewport height, so the YOffset may be stale after syncInputHeight
|
||||||
|
// changed the height. Send up then down to force repositionView to
|
||||||
|
// recalculate with the new height.
|
||||||
|
up := tea.KeyMsg{Type: tea.KeyUp}
|
||||||
|
vp, _ := m.input.Update(up)
|
||||||
|
m.input = vp
|
||||||
|
down := tea.KeyMsg{Type: tea.KeyDown}
|
||||||
|
vp, _ = m.input.Update(down)
|
||||||
|
m.input = vp
|
||||||
return *m, cmd
|
return *m, cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -465,7 +513,7 @@ func (m *model) appendAssistantChunk(chunk string) {
|
|||||||
|
|
||||||
func (m *model) resize() {
|
func (m *model) resize() {
|
||||||
width := max(40, m.width)
|
width := max(40, m.width)
|
||||||
inputWidth := max(20, width-m.styles.InputBox.GetHorizontalFrameSize())
|
inputWidth := max(20, width-m.styles.InputBox.GetHorizontalFrameSize()-m.inputModeWidth()-1)
|
||||||
m.input.SetWidth(inputWidth)
|
m.input.SetWidth(inputWidth)
|
||||||
m.syncInputHeight()
|
m.syncInputHeight()
|
||||||
|
|
||||||
@@ -572,8 +620,9 @@ func roleLabel(r role) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type toolLogParts struct {
|
type toolLogParts struct {
|
||||||
name string
|
name string
|
||||||
args string
|
args string
|
||||||
|
output string
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseToolLog(content string) toolLogParts {
|
func parseToolLog(content string) toolLogParts {
|
||||||
@@ -581,14 +630,21 @@ func parseToolLog(content string) toolLogParts {
|
|||||||
if content == "" {
|
if content == "" {
|
||||||
return toolLogParts{}
|
return toolLogParts{}
|
||||||
}
|
}
|
||||||
idx := strings.IndexFunc(content, unicode.IsSpace)
|
beforeOutput, output, hasOutput := strings.Cut(content, "\n[output]\n")
|
||||||
|
parts := toolLogParts{}
|
||||||
|
if hasOutput {
|
||||||
|
parts.output = strings.TrimSpace(output)
|
||||||
|
}
|
||||||
|
firstLine := strings.SplitN(beforeOutput, "\n", 2)[0]
|
||||||
|
firstLine = strings.TrimSpace(firstLine)
|
||||||
|
idx := strings.IndexFunc(firstLine, unicode.IsSpace)
|
||||||
if idx < 0 {
|
if idx < 0 {
|
||||||
return toolLogParts{name: content}
|
parts.name = firstLine
|
||||||
}
|
return parts
|
||||||
return toolLogParts{
|
|
||||||
name: strings.TrimSpace(content[:idx]),
|
|
||||||
args: strings.TrimSpace(content[idx+1:]),
|
|
||||||
}
|
}
|
||||||
|
parts.name = strings.TrimSpace(firstLine[:idx])
|
||||||
|
parts.args = strings.TrimSpace(firstLine[idx+1:])
|
||||||
|
return parts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) toolMessageView(content string, width int) string {
|
func (m model) toolMessageView(content string, width int) string {
|
||||||
@@ -599,82 +655,84 @@ func (m model) toolMessageView(content string, width int) string {
|
|||||||
args := fitLine(parts.args, max(1, width-4))
|
args := fitLine(parts.args, max(1, width-4))
|
||||||
lines = append(lines, m.styles.ToolValue.Render(args))
|
lines = append(lines, m.styles.ToolValue.Render(args))
|
||||||
}
|
}
|
||||||
|
if parts.output != "" {
|
||||||
|
lines = append(lines, m.styles.ToolKey.Render("output"))
|
||||||
|
previewLines := toolOutputPreviewLines(parts.output, max(1, width-4))
|
||||||
|
for _, line := range previewLines {
|
||||||
|
lines = append(lines, m.styles.ToolValue.Render(line))
|
||||||
|
}
|
||||||
|
}
|
||||||
bodyContent := strings.Join(lines, "\n")
|
bodyContent := strings.Join(lines, "\n")
|
||||||
bodyWidth := messageBodyWidth(bodyContent, width, m.styles.ToolMessage)
|
bodyWidth := messageBodyWidth(bodyContent, width, m.styles.ToolMessage)
|
||||||
return m.styles.ToolMessage.Width(bodyWidth).Render(bodyContent)
|
return m.styles.ToolMessage.Width(bodyWidth).Render(bodyContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const maxToolOutputPreviewLines = 8
|
||||||
|
|
||||||
|
func toolStatusText(content string) string {
|
||||||
|
content = strings.TrimSpace(content)
|
||||||
|
if content == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
lines := strings.SplitN(content, "\n", 2)
|
||||||
|
return lines[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) mergeToolLog(text string) bool {
|
||||||
|
incoming := parseToolLog(text)
|
||||||
|
if incoming.output == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := len(m.messages) - 1; i >= 0; i-- {
|
||||||
|
if m.messages[i].role != roleTool {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
existing := parseToolLog(m.messages[i].content)
|
||||||
|
if existing.name == incoming.name && existing.args == incoming.args && existing.output == "" {
|
||||||
|
m.messages[i].content = text
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func toolOutputPreviewLines(output string, width int) []string {
|
||||||
|
lines := strings.Split(output, "\n")
|
||||||
|
if len(lines) > maxToolOutputPreviewLines {
|
||||||
|
lines = lines[:maxToolOutputPreviewLines]
|
||||||
|
lines = append(lines, fmt.Sprintf("[truncated: showing first %d lines]", maxToolOutputPreviewLines))
|
||||||
|
}
|
||||||
|
result := make([]string, 0, len(lines))
|
||||||
|
for _, line := range lines {
|
||||||
|
result = append(result, fitLine(line, width))
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
type metaPill struct {
|
type metaPill struct {
|
||||||
text string
|
text string
|
||||||
accent bool
|
accent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) renderMetaPills(parts []metaPill) string {
|
func (m model) inputModeView() string {
|
||||||
if len(parts) == 0 {
|
if strings.HasPrefix(strings.TrimSpace(m.input.Value()), "!") {
|
||||||
return ""
|
return m.styles.InputModeCommand.Render(iconCommand)
|
||||||
}
|
}
|
||||||
|
if m.yolo {
|
||||||
plain := make([]string, len(parts))
|
return m.styles.InputModeYolo.Render("Y")
|
||||||
for i, part := range parts {
|
|
||||||
plain[i] = part.text
|
|
||||||
}
|
}
|
||||||
|
return m.styles.InputModeReadonly.Render("R")
|
||||||
width := max(1, m.width-2)
|
|
||||||
visible := len(plain)
|
|
||||||
for visible > 1 && lipgloss.Width(strings.Join(plain[:visible], " ")) > width {
|
|
||||||
visible--
|
|
||||||
}
|
|
||||||
if lipgloss.Width(strings.Join(plain[:visible], " ")) > width {
|
|
||||||
return m.styles.PillAccent.Render(fitLine(plain[0], width))
|
|
||||||
}
|
|
||||||
|
|
||||||
rendered := make([]string, 0, visible)
|
|
||||||
for i := 0; i < visible; i++ {
|
|
||||||
style := m.styles.Pill
|
|
||||||
if parts[i].accent {
|
|
||||||
style = m.styles.PillAccent
|
|
||||||
}
|
|
||||||
rendered = append(rendered, style.Render(parts[i].text))
|
|
||||||
}
|
|
||||||
return strings.Join(rendered, " ")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) modelMetaView() string {
|
func (m model) inputModeWidth() int {
|
||||||
mode := "read-only tools"
|
return lipgloss.Width(m.inputModeView())
|
||||||
if m.yolo {
|
|
||||||
mode = "yolo tools"
|
|
||||||
}
|
|
||||||
|
|
||||||
modelName := m.modelName
|
|
||||||
if m.models != nil {
|
|
||||||
if currentModel := m.models.CurrentModel(); currentModel != "" {
|
|
||||||
modelName = currentModel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
parts := []metaPill{{text: iconModel + " " + modelName, accent: true}}
|
|
||||||
if m.models != nil {
|
|
||||||
if provider := m.models.CurrentProvider(); provider != "" {
|
|
||||||
parts = append(parts, metaPill{text: "provider " + provider})
|
|
||||||
}
|
|
||||||
if currentThinking := m.models.CurrentThinking(); currentThinking != "" && currentThinking != "unset" {
|
|
||||||
parts = append(parts, metaPill{text: "thinking " + currentThinking})
|
|
||||||
}
|
|
||||||
if sessionName := m.models.CurrentSessionName(); sessionName != "" {
|
|
||||||
parts = append(parts, metaPill{text: iconSession + " " + sessionName})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if m.contextUsage != "" {
|
|
||||||
parts = append(parts, metaPill{text: iconContext + " " + m.contextUsage})
|
|
||||||
}
|
|
||||||
parts = append(parts, metaPill{text: mode}, metaPill{text: "theme " + string(m.theme.Mode)})
|
|
||||||
|
|
||||||
return m.styles.ModelMeta.Width(max(1, m.width)).Render(m.renderMetaPills(parts))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) inputView() string {
|
func (m model) inputView() string {
|
||||||
palette := m.commandPaletteView()
|
palette := m.commandPaletteView()
|
||||||
box := m.styles.InputBox.Width(max(20, m.width-2)).Render(m.input.View())
|
gap := lipgloss.NewStyle().Background(m.styles.InputBox.GetBackground()).Render(" ")
|
||||||
|
content := lipgloss.JoinHorizontal(lipgloss.Top, m.inputModeView(), gap, m.input.View())
|
||||||
|
box := m.styles.InputBox.Width(max(20, m.width)).Render(content)
|
||||||
if palette != "" {
|
if palette != "" {
|
||||||
return lipgloss.JoinVertical(lipgloss.Left, palette, box)
|
return lipgloss.JoinVertical(lipgloss.Left, palette, box)
|
||||||
}
|
}
|
||||||
@@ -692,13 +750,85 @@ func (m model) activityView() string {
|
|||||||
line := m.styles.StatusInfo.Render(iconWorking) + " Working " + m.spinner.View() + " " + status + " · esc cancels"
|
line := m.styles.StatusInfo.Render(iconWorking) + " Working " + m.spinner.View() + " " + status + " · esc cancels"
|
||||||
return m.styles.Activity.Render(fitLine(line, max(1, m.width-2)))
|
return m.styles.Activity.Render(fitLine(line, max(1, m.width-2)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) footerView() string {
|
func (m model) footerView() string {
|
||||||
hint := "enter send · ctrl+j newline · alt+enter newline · ctrl+p/ctrl+n history · / commands"
|
hint := ""
|
||||||
if m.running {
|
if m.running {
|
||||||
hint = "esc cancel · ctrl+c quit"
|
hint = "esc cancel · ctrl+c quit"
|
||||||
}
|
}
|
||||||
return m.styles.Footer.Width(max(1, m.width)).Render(fitLine(hint, max(1, m.width-2)))
|
if m.showCommandPalette() {
|
||||||
|
if sug, ok := m.selectedCommandSuggestion(); ok && sug.executable {
|
||||||
|
hint = "↑/↓ select · enter apply · tab fill"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inner := max(1, m.width-2) // Footer padding(0,1)
|
||||||
|
pills := m.modelMetaPillsClamped(inner / 3)
|
||||||
|
pillsW := lipgloss.Width(pills)
|
||||||
|
|
||||||
|
var line string
|
||||||
|
if pills != "" {
|
||||||
|
remain := max(1, inner-pillsW-1)
|
||||||
|
line = pills + " " + fitLine(hint, remain)
|
||||||
|
} else {
|
||||||
|
line = fitLine(hint, inner)
|
||||||
|
}
|
||||||
|
return m.styles.Footer.Width(max(1, m.width)).Render(line)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) modelMetaPillsClamped(maxW int) string {
|
||||||
|
modelName := m.modelName
|
||||||
|
if m.models != nil {
|
||||||
|
if currentModel := m.models.CurrentModel(); currentModel != "" {
|
||||||
|
modelName = currentModel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := []metaPill{{text: iconModel + " " + modelName, accent: true}}
|
||||||
|
if m.models != nil {
|
||||||
|
if provider := m.models.CurrentProvider(); provider != "" {
|
||||||
|
parts = append(parts, metaPill{text: provider})
|
||||||
|
}
|
||||||
|
if currentThinking := m.models.CurrentThinking(); currentThinking != "" && currentThinking != "unset" {
|
||||||
|
parts = append(parts, metaPill{text: currentThinking})
|
||||||
|
}
|
||||||
|
if sessionName := m.models.CurrentSessionName(); sessionName != "" {
|
||||||
|
parts = append(parts, metaPill{text: iconSession + " " + sessionName})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m.contextUsage != "" {
|
||||||
|
parts = append(parts, metaPill{text: iconContext + " " + m.contextUsage})
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.renderMetaPillsClamped(parts, maxW)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) renderMetaPillsClamped(parts []metaPill, width int) string {
|
||||||
|
if len(parts) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
plain := make([]string, len(parts))
|
||||||
|
for i, part := range parts {
|
||||||
|
plain[i] = part.text
|
||||||
|
}
|
||||||
|
|
||||||
|
visible := len(plain)
|
||||||
|
for visible > 1 && lipgloss.Width(strings.Join(plain[:visible], " ")) > width {
|
||||||
|
visible--
|
||||||
|
}
|
||||||
|
if visible > 0 && lipgloss.Width(strings.Join(plain[:visible], " ")) > width {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
rendered := make([]string, 0, visible)
|
||||||
|
for i := 0; i < visible; i++ {
|
||||||
|
style := m.styles.Pill
|
||||||
|
if parts[i].accent {
|
||||||
|
style = m.styles.PillAccent
|
||||||
|
}
|
||||||
|
rendered = append(rendered, style.Render(parts[i].text))
|
||||||
|
}
|
||||||
|
return strings.Join(rendered, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
type eventWriter struct {
|
type eventWriter struct {
|
||||||
@@ -742,6 +872,7 @@ func isViewportKey(key string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *model) afterInputChanged() {
|
func (m *model) afterInputChanged() {
|
||||||
|
m.commandCursor = 0
|
||||||
m.syncInputHeight()
|
m.syncInputHeight()
|
||||||
if m.width > 0 && m.height > 0 {
|
if m.width > 0 && m.height > 0 {
|
||||||
m.resize()
|
m.resize()
|
||||||
@@ -823,18 +954,37 @@ func (m model) inputBlockHeight() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m model) commandPaletteHeight() int {
|
func (m model) commandPaletteHeight() int {
|
||||||
matches := commandMatches(commandPaletteQuery(m.input.Value()))
|
sugs := m.commandSuggestions()
|
||||||
count := len(matches)
|
total := len(sugs)
|
||||||
if count > maxCommandPaletteItems {
|
if !m.showCommandPalette() {
|
||||||
count = maxCommandPaletteItems
|
return 0
|
||||||
}
|
}
|
||||||
|
count := min(total, maxCommandPaletteItems)
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
count = 1
|
count = 1
|
||||||
}
|
}
|
||||||
return 1 + count + m.styles.Palette.GetVerticalFrameSize()
|
scrollUp := 0
|
||||||
|
scrollDown := 0
|
||||||
|
if total > maxCommandPaletteItems {
|
||||||
|
visible := min(total, maxCommandPaletteItems)
|
||||||
|
scroll := max(0, m.commandCursor-visible+1)
|
||||||
|
if m.commandCursor < scroll {
|
||||||
|
scroll = m.commandCursor
|
||||||
|
}
|
||||||
|
if scroll > 0 {
|
||||||
|
scrollUp = 1
|
||||||
|
}
|
||||||
|
if scroll+visible < total {
|
||||||
|
scrollDown = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1 + scrollUp + count + scrollDown + m.styles.Palette.GetVerticalFrameSize()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) showCommandPalette() bool {
|
func (m model) showCommandPalette() bool {
|
||||||
|
if len(m.commandSuggestions()) > 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
value := strings.TrimSpace(m.input.Value())
|
value := strings.TrimSpace(m.input.Value())
|
||||||
return strings.HasPrefix(value, "/") && !strings.Contains(value, " ")
|
return strings.HasPrefix(value, "/") && !strings.Contains(value, " ")
|
||||||
}
|
}
|
||||||
@@ -843,18 +993,42 @@ func (m model) commandPaletteView() string {
|
|||||||
if !m.showCommandPalette() {
|
if !m.showCommandPalette() {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
matches := commandMatches(commandPaletteQuery(m.input.Value()))
|
sugs := m.commandSuggestions()
|
||||||
lines := []string{m.styles.Header.Render(iconCommand + " Commands")}
|
if len(sugs) > 0 {
|
||||||
if len(matches) == 0 {
|
header := iconCommand + " Commands"
|
||||||
lines = append(lines, m.styles.Muted.Render("No matching commands"))
|
if sugs[0].executable {
|
||||||
} else {
|
header = iconCommand + " Model candidates"
|
||||||
for i, match := range matches {
|
|
||||||
if i >= maxCommandPaletteItems {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
lines = append(lines, fmt.Sprintf("%s %s", m.styles.PaletteMatch.Render(match.command.Name), m.styles.Muted.Render(match.command.Description)))
|
|
||||||
}
|
}
|
||||||
|
lines := []string{m.styles.Header.Render(header)}
|
||||||
|
m.clampCommandCursor()
|
||||||
|
total := len(sugs)
|
||||||
|
visible := min(total, maxCommandPaletteItems)
|
||||||
|
scroll := max(0, m.commandCursor-visible+1)
|
||||||
|
if m.commandCursor < scroll {
|
||||||
|
scroll = m.commandCursor
|
||||||
|
}
|
||||||
|
if scroll > 0 {
|
||||||
|
lines = append(lines, m.styles.Muted.Render(" ↑ more"))
|
||||||
|
}
|
||||||
|
for i := scroll; i < scroll+visible && i < total; i++ {
|
||||||
|
s := sugs[i]
|
||||||
|
prefix := " "
|
||||||
|
if i == m.commandCursor {
|
||||||
|
prefix = "▸ "
|
||||||
|
}
|
||||||
|
lines = append(lines, fmt.Sprintf("%s%s %s", prefix, m.styles.PaletteMatch.Render(s.label), m.styles.Muted.Render(s.description)))
|
||||||
|
}
|
||||||
|
if scroll+visible < total {
|
||||||
|
lines = append(lines, m.styles.Muted.Render(" ↓ more"))
|
||||||
|
}
|
||||||
|
return m.styles.Palette.Width(max(20, m.width-2)).Render(strings.Join(lines, "\n"))
|
||||||
}
|
}
|
||||||
|
value := strings.TrimSpace(m.input.Value())
|
||||||
|
if strings.Contains(value, " ") {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
lines := []string{m.styles.Header.Render(iconCommand + " Commands")}
|
||||||
|
lines = append(lines, m.styles.Muted.Render("No matching commands"))
|
||||||
return m.styles.Palette.Width(max(20, m.width-2)).Render(strings.Join(lines, "\n"))
|
return m.styles.Palette.Width(max(20, m.width-2)).Render(strings.Join(lines, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -932,6 +1106,162 @@ func subsequenceScore(candidate string, query string, base int) (int, bool) {
|
|||||||
return base + gaps, true
|
return base + gaps, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m model) commandSuggestions() []commandSuggestion {
|
||||||
|
input := m.input.Value()
|
||||||
|
if sugs := m.modelCommandSuggestions(input); len(sugs) > 0 {
|
||||||
|
return sugs
|
||||||
|
}
|
||||||
|
value := strings.TrimSpace(input)
|
||||||
|
if strings.HasPrefix(value, "/") && !strings.Contains(value, " ") {
|
||||||
|
return slashCommandSuggestions(commandPaletteQuery(input))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func slashCommandSuggestions(query string) []commandSuggestion {
|
||||||
|
matches := commandMatches(query)
|
||||||
|
sugs := make([]commandSuggestion, 0, len(matches))
|
||||||
|
for _, m := range matches {
|
||||||
|
sugs = append(sugs, commandSuggestion{
|
||||||
|
label: m.command.Name,
|
||||||
|
description: m.command.Description,
|
||||||
|
value: m.command.Name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return sugs
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitModelCommandTail(input string) (query string, ok bool) {
|
||||||
|
fields := strings.Fields(input)
|
||||||
|
if len(fields) == 0 || fields[0] != "/model" {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
hasTrailingSpace := strings.TrimRight(input, " \t") != input
|
||||||
|
switch len(fields) {
|
||||||
|
case 1:
|
||||||
|
if !hasTrailingSpace {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
return "", true
|
||||||
|
case 2:
|
||||||
|
if fields[1] == "provider" || fields[1] == "thinking" {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
if fields[1] == "model" {
|
||||||
|
if !hasTrailingSpace {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
return "", true
|
||||||
|
}
|
||||||
|
if hasTrailingSpace {
|
||||||
|
return fields[1], true
|
||||||
|
}
|
||||||
|
return fields[1], true
|
||||||
|
case 3:
|
||||||
|
if fields[1] != "model" {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
if hasTrailingSpace {
|
||||||
|
return fields[2], true
|
||||||
|
}
|
||||||
|
return fields[2], true
|
||||||
|
default:
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) modelCommandSuggestions(input string) []commandSuggestion {
|
||||||
|
if m.models == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
query, ok := splitModelCommandTail(input)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
available := m.models.AvailableModels()
|
||||||
|
if len(available) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
current := m.models.CurrentModel()
|
||||||
|
candidates := make([]commandSuggestion, 0, len(available))
|
||||||
|
for _, id := range available {
|
||||||
|
desc := "switch model"
|
||||||
|
if id == current {
|
||||||
|
desc = "current model"
|
||||||
|
}
|
||||||
|
candidates = append(candidates, commandSuggestion{
|
||||||
|
label: id,
|
||||||
|
description: desc,
|
||||||
|
value: "/model model " + id,
|
||||||
|
executable: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return scoredCommandSuggestions(candidates, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
func scoredCommandSuggestions(candidates []commandSuggestion, query string) []commandSuggestion {
|
||||||
|
if query == "" {
|
||||||
|
return candidates
|
||||||
|
}
|
||||||
|
type scored struct {
|
||||||
|
sug commandSuggestion
|
||||||
|
score int
|
||||||
|
idx int
|
||||||
|
}
|
||||||
|
var kept []scored
|
||||||
|
for i, c := range candidates {
|
||||||
|
if s, ok := scoreCommandSuggestion(c.label, query); ok {
|
||||||
|
kept = append(kept, scored{sug: c, score: s, idx: i})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Slice(kept, func(i, j int) bool {
|
||||||
|
if kept[i].score != kept[j].score {
|
||||||
|
return kept[i].score < kept[j].score
|
||||||
|
}
|
||||||
|
return kept[i].idx < kept[j].idx
|
||||||
|
})
|
||||||
|
out := make([]commandSuggestion, 0, len(kept))
|
||||||
|
for _, s := range kept {
|
||||||
|
out = append(out, s.sug)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func scoreCommandSuggestion(label string, query string) (int, bool) {
|
||||||
|
query = strings.ToLower(strings.TrimSpace(query))
|
||||||
|
name := strings.ToLower(label)
|
||||||
|
if query == "" || query == name {
|
||||||
|
return 0, true
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(name, query) {
|
||||||
|
return 100 + len(name) - len(query), true
|
||||||
|
}
|
||||||
|
return subsequenceScore(name, query, 200)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) selectedCommandSuggestion() (commandSuggestion, bool) {
|
||||||
|
sugs := m.commandSuggestions()
|
||||||
|
m.clampCommandCursor()
|
||||||
|
if m.commandCursor >= 0 && m.commandCursor < len(sugs) {
|
||||||
|
return sugs[m.commandCursor], true
|
||||||
|
}
|
||||||
|
return commandSuggestion{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) clampCommandCursor() {
|
||||||
|
sugs := m.commandSuggestions()
|
||||||
|
if len(sugs) == 0 {
|
||||||
|
m.commandCursor = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if m.commandCursor < 0 {
|
||||||
|
m.commandCursor = 0
|
||||||
|
}
|
||||||
|
if m.commandCursor >= len(sugs) {
|
||||||
|
m.commandCursor = len(sugs) - 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (m model) slashSuggestionCount() int {
|
func (m model) slashSuggestionCount() int {
|
||||||
return m.commandPaletteHeight()
|
return m.commandPaletteHeight()
|
||||||
}
|
}
|
||||||
@@ -948,11 +1278,7 @@ func (m model) modelInfo() string {
|
|||||||
if m.models != nil {
|
if m.models != nil {
|
||||||
return m.models.Info()
|
return m.models.Info()
|
||||||
}
|
}
|
||||||
mode := "read-only tools"
|
return m.modelName
|
||||||
if m.yolo {
|
|
||||||
mode = "yolo tools"
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("model: %s\nmode: %s\ntheme: %s", m.modelName, mode, m.theme.Mode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *model) refreshContextUsage() {
|
func (m *model) refreshContextUsage() {
|
||||||
@@ -1031,6 +1357,9 @@ func fitLine(text string, width int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *model) runLocalCommand(input string) (tea.Model, tea.Cmd) {
|
func (m *model) runLocalCommand(input string) (tea.Model, tea.Cmd) {
|
||||||
|
if strings.HasPrefix(input, "/theme") {
|
||||||
|
return m.handleThemeCommand(input)
|
||||||
|
}
|
||||||
output, err := m.handleLocalCommand(input)
|
output, err := m.handleLocalCommand(input)
|
||||||
if err == errOpenPicker {
|
if err == errOpenPicker {
|
||||||
m.openSessionPicker()
|
m.openSessionPicker()
|
||||||
@@ -1055,7 +1384,60 @@ func (m *model) runLocalCommand(input string) (tea.Model, tea.Cmd) {
|
|||||||
return *m, nil
|
return *m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *model) handleThemeCommand(input string) (tea.Model, tea.Cmd) {
|
||||||
|
fields := strings.Fields(input)
|
||||||
|
if len(fields) < 2 {
|
||||||
|
m.input.SetValue("")
|
||||||
|
m.afterInputChanged()
|
||||||
|
m.messages = append(m.messages, message{
|
||||||
|
role: roleSystem, content: "theme: " + string(m.theme.Mode) + "\nusage: /theme light|dark",
|
||||||
|
})
|
||||||
|
m.refreshViewport(true)
|
||||||
|
return *m, nil
|
||||||
|
}
|
||||||
|
mode, err := ParseThemeMode(fields[1])
|
||||||
|
if err != nil {
|
||||||
|
m.input.SetValue("")
|
||||||
|
m.afterInputChanged()
|
||||||
|
m.messages = append(m.messages, message{role: roleError, content: err.Error()})
|
||||||
|
m.refreshViewport(true)
|
||||||
|
return *m, nil
|
||||||
|
}
|
||||||
|
m.theme = themeForMode(mode)
|
||||||
|
m.styles = m.theme.styles()
|
||||||
|
applyTextareaTheme(&m.input, m.theme)
|
||||||
|
m.input.SetValue("")
|
||||||
|
m.afterInputChanged()
|
||||||
|
m.messages = append(m.messages, message{role: roleSystem, content: "theme: " + string(mode)})
|
||||||
|
m.refreshViewport(true)
|
||||||
|
return *m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) handleBangCommand(input string) (string, error) {
|
||||||
|
command := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(input), "!"))
|
||||||
|
if command == "" {
|
||||||
|
return "", fmt.Errorf("usage: ! <command>")
|
||||||
|
}
|
||||||
|
if !m.yolo {
|
||||||
|
return "", fmt.Errorf("shell command input requires --yolo")
|
||||||
|
}
|
||||||
|
out, err := m.runShellCommand(command)
|
||||||
|
if err != nil {
|
||||||
|
if strings.TrimSpace(out) != "" {
|
||||||
|
return "", fmt.Errorf("%s\n%w", strings.TrimRight(out, "\n"), err)
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(out) == "" {
|
||||||
|
return fmt.Sprintf("$ %s\n(no output)", command), nil
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m model) handleLocalCommand(input string) (string, error) {
|
func (m model) handleLocalCommand(input string) (string, error) {
|
||||||
|
if strings.HasPrefix(strings.TrimSpace(input), "!") {
|
||||||
|
return m.handleBangCommand(input)
|
||||||
|
}
|
||||||
fields := strings.Fields(input)
|
fields := strings.Fields(input)
|
||||||
if len(fields) == 0 {
|
if len(fields) == 0 {
|
||||||
return "", fmt.Errorf("empty command")
|
return "", fmt.Errorf("empty command")
|
||||||
@@ -1251,6 +1633,41 @@ func (m *model) updatePicker(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
return *m, nil
|
return *m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *model) updateCommandSuggestions(msg tea.KeyMsg) (bool, tea.Model, tea.Cmd) {
|
||||||
|
if m.running || len(m.commandSuggestions()) == 0 {
|
||||||
|
return false, *m, nil
|
||||||
|
}
|
||||||
|
m.clampCommandCursor()
|
||||||
|
switch msg.String() {
|
||||||
|
case "up":
|
||||||
|
if m.commandCursor > 0 {
|
||||||
|
m.commandCursor--
|
||||||
|
}
|
||||||
|
return true, *m, nil
|
||||||
|
case "down":
|
||||||
|
if m.commandCursor < len(m.commandSuggestions())-1 {
|
||||||
|
m.commandCursor++
|
||||||
|
}
|
||||||
|
return true, *m, nil
|
||||||
|
case "tab":
|
||||||
|
if sug, ok := m.selectedCommandSuggestion(); ok {
|
||||||
|
m.input.SetValue(sug.value)
|
||||||
|
m.afterInputChanged()
|
||||||
|
}
|
||||||
|
return true, *m, nil
|
||||||
|
case "enter":
|
||||||
|
if sug, ok := m.selectedCommandSuggestion(); ok && sug.executable {
|
||||||
|
m.rememberInput(sug.value)
|
||||||
|
m.input.SetValue(sug.value)
|
||||||
|
m.afterInputChanged()
|
||||||
|
next, cmd := m.runLocalCommand(sug.value)
|
||||||
|
return true, next, cmd
|
||||||
|
}
|
||||||
|
return false, *m, nil
|
||||||
|
}
|
||||||
|
return false, *m, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m model) pickerView() string {
|
func (m model) pickerView() string {
|
||||||
if !m.picking || len(m.pickerItems) == 0 {
|
if !m.picking || len(m.pickerItems) == 0 {
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
+361
-23
@@ -21,7 +21,7 @@ func TestModelRendersChatShell(t *testing.T) {
|
|||||||
m = next.(model)
|
m = next.(model)
|
||||||
rendered := m.View()
|
rendered := m.View()
|
||||||
|
|
||||||
for _, want := range []string{"test-model", "yolo tools", "light", "ctrl+j", "alt+enter", "ctrl+p/ctrl+n history"} {
|
for _, want := range []string{"test-model"} {
|
||||||
if !strings.Contains(rendered, want) {
|
if !strings.Contains(rendered, want) {
|
||||||
t.Fatalf("rendered view missing %q:\n%s", want, rendered)
|
t.Fatalf("rendered view missing %q:\n%s", want, rendered)
|
||||||
}
|
}
|
||||||
@@ -37,18 +37,16 @@ func TestModelRendersChatShell(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInputTextDoesNotPaintBackground(t *testing.T) {
|
func TestInputTextBackgroundMatchesInputBox(t *testing.T) {
|
||||||
m := newModel(context.Background(), nil, Options{ModelName: "test-model"})
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model"})
|
||||||
for name, color := range map[string]any{
|
want := lightTheme().InputBg
|
||||||
"input box": m.styles.InputBox.GetBackground(),
|
for name, color := range map[string]lipgloss.TerminalColor{
|
||||||
"focused base": m.input.FocusedStyle.Base.GetBackground(),
|
"focused base": m.input.FocusedStyle.Base.GetBackground(),
|
||||||
"focused cursor line": m.input.FocusedStyle.CursorLine.GetBackground(),
|
"focused cursor line": m.input.FocusedStyle.CursorLine.GetBackground(),
|
||||||
"blurred base": m.input.BlurredStyle.Base.GetBackground(),
|
"blurred base": m.input.BlurredStyle.Base.GetBackground(),
|
||||||
"blurred cursor line": m.input.BlurredStyle.CursorLine.GetBackground(),
|
"blurred cursor line": m.input.BlurredStyle.CursorLine.GetBackground(),
|
||||||
} {
|
} {
|
||||||
if _, ok := color.(lipgloss.NoColor); !ok {
|
requireStyleColor(t, name, color, want)
|
||||||
t.Fatalf("%s should not paint input text background", name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,12 +56,12 @@ func TestInputBoxIsRoomier(t *testing.T) {
|
|||||||
m = next.(model)
|
m = next.(model)
|
||||||
|
|
||||||
if got := m.styles.InputBox.GetVerticalFrameSize(); got != 2 {
|
if got := m.styles.InputBox.GetVerticalFrameSize(); got != 2 {
|
||||||
t.Fatalf("input box vertical frame should stay compact, got %d", got)
|
t.Fatalf("input box vertical frame should be 2 with vertical padding, got %d", got)
|
||||||
}
|
}
|
||||||
if got := m.styles.InputBox.GetHorizontalFrameSize(); got < 6 {
|
if got := m.styles.InputBox.GetHorizontalFrameSize(); got != 3 {
|
||||||
t.Fatalf("input box horizontal frame should include roomier padding, got %d", got)
|
t.Fatalf("input box horizontal frame should be 3 (asymmetric padding), got %d", got)
|
||||||
}
|
}
|
||||||
if got, want := m.input.Width(), 100-m.styles.InputBox.GetHorizontalFrameSize(); got != want {
|
if got, want := m.input.Width(), 100-m.styles.InputBox.GetHorizontalFrameSize()-m.inputModeWidth()-1; got != want {
|
||||||
t.Fatalf("input content width = %d, want %d", got, want)
|
t.Fatalf("input content width = %d, want %d", got, want)
|
||||||
}
|
}
|
||||||
rendered := m.inputView()
|
rendered := m.inputView()
|
||||||
@@ -78,13 +76,8 @@ func TestModelMetaRendersBelowInput(t *testing.T) {
|
|||||||
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
rendered := next.(model).View()
|
rendered := next.(model).View()
|
||||||
|
|
||||||
inputIndex := strings.Index(rendered, "Message agentu")
|
if !strings.Contains(rendered, "test-model") {
|
||||||
modelIndex := strings.Index(rendered, "test-model")
|
t.Fatalf("rendered view missing %q:\n%s", "test-model", rendered)
|
||||||
if inputIndex < 0 || modelIndex < 0 {
|
|
||||||
t.Fatalf("rendered view missing input or model meta:\n%s", rendered)
|
|
||||||
}
|
|
||||||
if modelIndex < inputIndex {
|
|
||||||
t.Fatalf("model meta should render below input:\n%s", rendered)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +86,7 @@ func TestModelMetaRendersContextUsage(t *testing.T) {
|
|||||||
assistant.SetMessages([]llm.Message{{Role: llm.RoleUser, Content: strings.Repeat("x", 800)}})
|
assistant.SetMessages([]llm.Message{{Role: llm.RoleUser, Content: strings.Repeat("x", 800)}})
|
||||||
m := newModel(context.Background(), assistant, Options{ModelName: "test-model"})
|
m := newModel(context.Background(), assistant, Options{ModelName: "test-model"})
|
||||||
next, _ := m.Update(tea.WindowSizeMsg{Width: 120, Height: 30})
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 120, Height: 30})
|
||||||
rendered := next.(model).modelMetaView()
|
rendered := next.(model).footerView()
|
||||||
|
|
||||||
for _, want := range []string{"ctx ~", "/1k", "%"} {
|
for _, want := range []string{"ctx ~", "/1k", "%"} {
|
||||||
if !strings.Contains(rendered, want) {
|
if !strings.Contains(rendered, want) {
|
||||||
@@ -336,6 +329,23 @@ func TestInputHistoryNavigation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpArrowRecallsPreviousInput(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model"})
|
||||||
|
m.rememberInput("first")
|
||||||
|
m.rememberInput("second")
|
||||||
|
|
||||||
|
next, _ := m.Update(tea.KeyMsg{Type: tea.KeyUp})
|
||||||
|
m = next.(model)
|
||||||
|
if got := m.input.Value(); got != "second" {
|
||||||
|
t.Fatalf("after first up input = %q, want %q", got, "second")
|
||||||
|
}
|
||||||
|
next, _ = m.Update(tea.KeyMsg{Type: tea.KeyUp})
|
||||||
|
m = next.(model)
|
||||||
|
if got := m.input.Value(); got != "first" {
|
||||||
|
t.Fatalf("after second up input = %q, want %q", got, "first")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestToolMessageRendersStructuredCard(t *testing.T) {
|
func TestToolMessageRendersStructuredCard(t *testing.T) {
|
||||||
m := newModel(context.Background(), nil, Options{ModelName: "test-model"})
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model"})
|
||||||
m.messages = append(m.messages, message{role: roleTool, content: `file_read {"path":"README.md"}`})
|
m.messages = append(m.messages, message{role: roleTool, content: `file_read {"path":"README.md"}`})
|
||||||
@@ -348,6 +358,43 @@ func TestToolMessageRendersStructuredCard(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToolMessageRendersOutputPreview(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model"})
|
||||||
|
m.messages = append(m.messages, message{role: roleTool, content: "file_read {\"path\":\"README.md\"}\n[output]\nfirst line\nsecond line"})
|
||||||
|
|
||||||
|
plain := stripANSI(m.renderMessages())
|
||||||
|
for _, want := range []string{iconTool + " file_read", "README.md", "output", "first line", "second line"} {
|
||||||
|
if !strings.Contains(plain, want) {
|
||||||
|
t.Fatalf("tool card missing %q:\n%s", want, plain)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToolLogOutputUpdatesExistingToolCard(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model"})
|
||||||
|
m.messages = append(m.messages, message{role: roleTool, content: `file_read {"path":"README.md"}`})
|
||||||
|
|
||||||
|
updated := m.mergeToolLog("file_read {\"path\":\"README.md\"}\n[output]\nhello")
|
||||||
|
if !updated {
|
||||||
|
t.Fatal("expected mergeToolLog to return true")
|
||||||
|
}
|
||||||
|
if len(m.messages) != 1 {
|
||||||
|
t.Fatalf("messages count = %d, want 1", len(m.messages))
|
||||||
|
}
|
||||||
|
plain := stripANSI(m.renderMessages())
|
||||||
|
if !strings.Contains(plain, "hello") {
|
||||||
|
t.Fatalf("rendered output missing 'hello':\n%s", plain)
|
||||||
|
}
|
||||||
|
|
||||||
|
status := toolStatusText("file_read {\"path\":\"README.md\"}\n[output]\nhello world")
|
||||||
|
if strings.Contains(status, "hello") {
|
||||||
|
t.Fatalf("status should not contain output: %q", status)
|
||||||
|
}
|
||||||
|
if !strings.Contains(status, "file_read") {
|
||||||
|
t.Fatalf("status missing tool name: %q", status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type tuiCompactProvider struct{}
|
type tuiCompactProvider struct{}
|
||||||
|
|
||||||
func (tuiCompactProvider) ChatStream(ctx context.Context, req llm.ChatRequest, emit func(llm.StreamEvent) error) error {
|
func (tuiCompactProvider) ChatStream(ctx context.Context, req llm.ChatRequest, emit func(llm.StreamEvent) error) error {
|
||||||
@@ -443,16 +490,60 @@ func runInDir(t *testing.T, dir string, name string, args ...string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBangShellCommandRunsFromInput(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model", Yolo: true, WorkingDir: dir})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
m.input.SetValue("! printf hello")
|
||||||
|
next, _ = m.submit()
|
||||||
|
m = next.(model)
|
||||||
|
if m.input.Value() != "" {
|
||||||
|
t.Fatalf("input not cleared after bang command: %q", m.input.Value())
|
||||||
|
}
|
||||||
|
rendered := stripANSI(m.renderMessages())
|
||||||
|
if !strings.Contains(rendered, "$ printf hello") {
|
||||||
|
t.Fatalf("rendered output missing shell echo:\n%s", rendered)
|
||||||
|
}
|
||||||
|
if !strings.Contains(rendered, "hello") {
|
||||||
|
t.Fatalf("rendered output missing 'hello':\n%s", rendered)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBangShellCommandRequiresYolo(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model", Yolo: false})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
m.input.SetValue("! printf hello")
|
||||||
|
next, _ = m.submit()
|
||||||
|
m = next.(model)
|
||||||
|
rendered := stripANSI(m.renderMessages())
|
||||||
|
if !strings.Contains(rendered, "shell command input requires --yolo") {
|
||||||
|
t.Fatalf("rendered output missing yolo requirement:\n%s", rendered)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBangShellCommandRejectsEmptyCommand(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model", Yolo: true})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
m.input.SetValue("!")
|
||||||
|
next, _ = m.submit()
|
||||||
|
m = next.(model)
|
||||||
|
rendered := stripANSI(m.renderMessages())
|
||||||
|
if !strings.Contains(rendered, "usage: ! <command>") {
|
||||||
|
t.Fatalf("rendered output missing usage hint:\n%s", rendered)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestModelCommand(t *testing.T) {
|
func TestModelCommand(t *testing.T) {
|
||||||
m := newModel(context.Background(), nil, Options{ModelName: "test-model", Yolo: true})
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model", Yolo: true})
|
||||||
m.input.SetValue("/model")
|
m.input.SetValue("/model")
|
||||||
|
|
||||||
next, _ := m.submit()
|
next, _ := m.submit()
|
||||||
rendered := next.(model).renderMessages()
|
rendered := next.(model).renderMessages()
|
||||||
for _, want := range []string{"model: test-model", "mode: yolo tools", "theme: light"} {
|
if !strings.Contains(rendered, "test-model") {
|
||||||
if !strings.Contains(rendered, want) {
|
t.Fatalf("model command output missing %q:\n%s", "test-model", rendered)
|
||||||
t.Fatalf("model command output missing %q:\n%s", want, rendered)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -471,11 +562,124 @@ func TestModelThinkingCommand(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestModelCommandPaletteShowsConfiguredModels(t *testing.T) {
|
||||||
|
manager := &fakeModelManager{model: "model-a", models: []string{"model-a", "model-b"}}
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "model-a", ModelManager: manager})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
m.input.SetValue("/model ")
|
||||||
|
m.afterInputChanged()
|
||||||
|
plain := stripANSI(m.View())
|
||||||
|
for _, want := range []string{iconCommand + " Model candidates", "model-a", "current model", "model-b", "switch model"} {
|
||||||
|
if !strings.Contains(plain, want) {
|
||||||
|
t.Fatalf("rendered palette missing %q:\n%s", want, plain)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestModelCommandPaletteSelectsConfiguredModel(t *testing.T) {
|
||||||
|
manager := &fakeModelManager{model: "model-a", models: []string{"model-a", "model-b"}}
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "model-a", ModelManager: manager})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
m.input.SetValue("/model ")
|
||||||
|
m.afterInputChanged()
|
||||||
|
next, _ = m.Update(tea.KeyMsg{Type: tea.KeyDown})
|
||||||
|
m = next.(model)
|
||||||
|
next, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
||||||
|
m = next.(model)
|
||||||
|
if manager.model != "model-b" {
|
||||||
|
t.Fatalf("model = %q", manager.model)
|
||||||
|
}
|
||||||
|
if got := m.input.Value(); got != "" {
|
||||||
|
t.Fatalf("input = %q", got)
|
||||||
|
}
|
||||||
|
rendered := stripANSI(m.renderMessages())
|
||||||
|
if !strings.Contains(rendered, "model: model-b") {
|
||||||
|
t.Fatalf("rendered output missing model switch:\n%s", rendered)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestModelCommandPaletteFiltersBareModelPartial(t *testing.T) {
|
||||||
|
manager := &fakeModelManager{model: "claude-sonnet", models: []string{"claude-sonnet", "deepseek-v4-flash"}}
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "claude-sonnet", ModelManager: manager})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
m.input.SetValue("/model deep")
|
||||||
|
m.afterInputChanged()
|
||||||
|
palette := stripANSI(m.commandPaletteView())
|
||||||
|
if !strings.Contains(palette, "deepseek-v4-flash") {
|
||||||
|
t.Fatalf("expected deepseek-v4-flash in palette:\n%s", palette)
|
||||||
|
}
|
||||||
|
if strings.Contains(palette, "claude-sonnet") {
|
||||||
|
t.Fatalf("unexpected claude-sonnet in palette:\n%s", palette)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestModelCommandPaletteFiltersExplicitModelSubcommand(t *testing.T) {
|
||||||
|
manager := &fakeModelManager{model: "claude-sonnet", models: []string{"claude-sonnet", "deepseek-v4-flash"}}
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "claude-sonnet", ModelManager: manager})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
m.input.SetValue("/model model deep")
|
||||||
|
m.afterInputChanged()
|
||||||
|
palette := stripANSI(m.commandPaletteView())
|
||||||
|
if !strings.Contains(palette, "deepseek-v4-flash") {
|
||||||
|
t.Fatalf("expected deepseek-v4-flash in palette:\n%s", palette)
|
||||||
|
}
|
||||||
|
if strings.Contains(palette, "claude-sonnet") {
|
||||||
|
t.Fatalf("unexpected claude-sonnet in palette:\n%s", palette)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestModelCommandPaletteTabFillsConfiguredModel(t *testing.T) {
|
||||||
|
manager := &fakeModelManager{model: "claude-sonnet", models: []string{"claude-sonnet", "deepseek-v4-flash"}}
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "claude-sonnet", ModelManager: manager})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
m.input.SetValue("/model deep")
|
||||||
|
m.afterInputChanged()
|
||||||
|
next, _ = m.Update(tea.KeyMsg{Type: tea.KeyTab})
|
||||||
|
m = next.(model)
|
||||||
|
if got := m.input.Value(); got != "/model model deepseek-v4-flash" {
|
||||||
|
t.Fatalf("input = %q", got)
|
||||||
|
}
|
||||||
|
if manager.model != "claude-sonnet" {
|
||||||
|
t.Fatalf("model should not change on tab: %q", manager.model)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestModelCommandPaletteIgnoresProviderAndThinkingSubcommands(t *testing.T) {
|
||||||
|
manager := &fakeModelManager{model: "model-a", models: []string{"model-a", "model-b"}}
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "model-a", ModelManager: manager})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
|
||||||
|
m.input.SetValue("/model provider ")
|
||||||
|
m.afterInputChanged()
|
||||||
|
if sugs := m.commandSuggestions(); len(sugs) != 0 {
|
||||||
|
t.Fatalf("provider suggestions should be empty, got %d", len(sugs))
|
||||||
|
}
|
||||||
|
if view := m.commandPaletteView(); view != "" {
|
||||||
|
t.Fatalf("provider palette should be empty:\n%s", view)
|
||||||
|
}
|
||||||
|
|
||||||
|
m.input.SetValue("/model thinking ")
|
||||||
|
m.afterInputChanged()
|
||||||
|
if sugs := m.commandSuggestions(); len(sugs) != 0 {
|
||||||
|
t.Fatalf("thinking suggestions should be empty, got %d", len(sugs))
|
||||||
|
}
|
||||||
|
if view := m.commandPaletteView(); view != "" {
|
||||||
|
t.Fatalf("thinking palette should be empty:\n%s", view)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type fakeModelManager struct {
|
type fakeModelManager struct {
|
||||||
provider string
|
provider string
|
||||||
model string
|
model string
|
||||||
thinking string
|
thinking string
|
||||||
sessionName string
|
sessionName string
|
||||||
|
models []string
|
||||||
saved bool
|
saved bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,6 +750,16 @@ func (f *fakeModelManager) NewSession() (string, error) {
|
|||||||
return "New session.", nil
|
return "New session.", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *fakeModelManager) AvailableModels() []string {
|
||||||
|
if len(f.models) > 0 {
|
||||||
|
return append([]string(nil), f.models...)
|
||||||
|
}
|
||||||
|
if f.model != "" {
|
||||||
|
return []string{f.model}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseThemeMode(t *testing.T) {
|
func TestParseThemeMode(t *testing.T) {
|
||||||
for input, want := range map[string]ThemeMode{
|
for input, want := range map[string]ThemeMode{
|
||||||
"": ThemeLight,
|
"": ThemeLight,
|
||||||
@@ -567,6 +781,130 @@ func TestParseThemeMode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func requireStyleColor(t *testing.T, name string, got lipgloss.TerminalColor, want string) {
|
||||||
|
t.Helper()
|
||||||
|
color, ok := got.(lipgloss.Color)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("%s = %T, want lipgloss.Color", name, got)
|
||||||
|
}
|
||||||
|
if string(color) != want {
|
||||||
|
t.Fatalf("%s = %q, want %q", name, string(color), want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func requireNoStyleColor(t *testing.T, name string, got lipgloss.TerminalColor) {
|
||||||
|
t.Helper()
|
||||||
|
if _, ok := got.(lipgloss.NoColor); !ok {
|
||||||
|
t.Fatalf("%s should not paint a color, got %T", name, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDarkThemePaintsAppBackground(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model", ThemeMode: ThemeDark})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
rendered := stripANSI(m.View())
|
||||||
|
_ = rendered
|
||||||
|
if m.theme.Mode != ThemeDark {
|
||||||
|
t.Fatalf("theme.Mode = %q, want %q", m.theme.Mode, ThemeDark)
|
||||||
|
}
|
||||||
|
want := darkTheme()
|
||||||
|
requireStyleColor(t, "App.Background", m.styles.App.GetBackground(), want.Background)
|
||||||
|
requireStyleColor(t, "App.Foreground", m.styles.App.GetForeground(), want.Text)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDarkThemeKeepsComponentBackgroundsLightweight(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model", ThemeMode: ThemeDark})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
requireNoStyleColor(t, "Viewport.Background", m.styles.Viewport.GetBackground())
|
||||||
|
requireStyleColor(t, "InputBox.Background", m.styles.InputBox.GetBackground(), darkTheme().InputBg)
|
||||||
|
requireNoStyleColor(t, "Activity.Background", m.styles.Activity.GetBackground())
|
||||||
|
m.running = true
|
||||||
|
m.status = "thinking..."
|
||||||
|
m.resize()
|
||||||
|
if backgroundPattern.MatchString(m.activityView()) {
|
||||||
|
t.Fatalf("activity view should not paint a background:\n%q", m.activityView())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDarkThemeStylesUseDarkPalette(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model", ThemeMode: ThemeDark})
|
||||||
|
want := darkTheme()
|
||||||
|
requireStyleColor(t, "Header.Foreground", m.styles.Header.GetForeground(), want.Title)
|
||||||
|
requireStyleColor(t, "Muted.Foreground", m.styles.Muted.GetForeground(), want.Muted)
|
||||||
|
requireStyleColor(t, "Pill.Background", m.styles.Pill.GetBackground(), want.SurfaceAlt)
|
||||||
|
requireStyleColor(t, "input.FocusedStyle.Base.Foreground", m.input.FocusedStyle.Base.GetForeground(), want.Text)
|
||||||
|
requireStyleColor(t, "input.FocusedStyle.Placeholder.Foreground", m.input.FocusedStyle.Placeholder.GetForeground(), want.Muted)
|
||||||
|
requireStyleColor(t, "InputModeYolo.Foreground", m.styles.InputModeYolo.GetForeground(), want.Error)
|
||||||
|
requireStyleColor(t, "InputModeCommand.Foreground", m.styles.InputModeCommand.GetForeground(), want.User)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInputModeSymbolReflectsYoloAndBangCommand(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model", Yolo: true})
|
||||||
|
next, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
|
||||||
|
m = next.(model)
|
||||||
|
|
||||||
|
rendered := stripANSI(m.inputView())
|
||||||
|
if !strings.Contains(rendered, "Y") {
|
||||||
|
t.Fatalf("YOLO input view should contain Y symbol:\n%s", rendered)
|
||||||
|
}
|
||||||
|
|
||||||
|
m.input.SetValue("! pwd")
|
||||||
|
m.afterInputChanged()
|
||||||
|
rendered = stripANSI(m.inputView())
|
||||||
|
if !strings.Contains(rendered, iconCommand) {
|
||||||
|
t.Fatalf("bang input view should contain command icon:\n%s", rendered)
|
||||||
|
}
|
||||||
|
if strings.Contains(rendered, "Y") {
|
||||||
|
t.Fatalf("bang input view should not contain Y symbol:\n%s", rendered)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInputModeYoloUsesWarningColor(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model", Yolo: true})
|
||||||
|
requireStyleColor(t, "InputModeYolo.Foreground", m.styles.InputModeYolo.GetForeground(), lightTheme().Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestThemeSlashCommandSwitchesAtRuntime(t *testing.T) {
|
||||||
|
m := newModel(context.Background(), nil, Options{ModelName: "test-model"})
|
||||||
|
if m.theme.Mode != ThemeLight {
|
||||||
|
t.Fatalf("initial theme = %q, want light", m.theme.Mode)
|
||||||
|
}
|
||||||
|
requireNoStyleColor(t, "initial App.Background", m.styles.App.GetBackground())
|
||||||
|
|
||||||
|
// Switch to dark
|
||||||
|
m.input.SetValue("/theme dark")
|
||||||
|
next, _ := m.submit()
|
||||||
|
m = next.(model)
|
||||||
|
if m.theme.Mode != ThemeDark {
|
||||||
|
t.Fatalf("after /theme dark: Mode = %q, want dark", m.theme.Mode)
|
||||||
|
}
|
||||||
|
requireStyleColor(t, "dark App.Background", m.styles.App.GetBackground(), darkTheme().Background)
|
||||||
|
last := m.messages[len(m.messages)-1]
|
||||||
|
if !strings.Contains(last.content, "theme: dark") {
|
||||||
|
t.Fatalf("expected confirmation message, got %q", last.content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch back to light
|
||||||
|
m.input.SetValue("/theme light")
|
||||||
|
next, _ = m.submit()
|
||||||
|
m = next.(model)
|
||||||
|
if m.theme.Mode != ThemeLight {
|
||||||
|
t.Fatalf("after /theme light: Mode = %q, want light", m.theme.Mode)
|
||||||
|
}
|
||||||
|
requireNoStyleColor(t, "restored App.Background", m.styles.App.GetBackground())
|
||||||
|
|
||||||
|
// No arg shows usage
|
||||||
|
m.input.SetValue("/theme")
|
||||||
|
next, _ = m.submit()
|
||||||
|
m = next.(model)
|
||||||
|
last = m.messages[len(m.messages)-1]
|
||||||
|
if !strings.Contains(last.content, "usage: /theme") {
|
||||||
|
t.Fatalf("expected usage hint, got %q", last.content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCleanToolLog(t *testing.T) {
|
func TestCleanToolLog(t *testing.T) {
|
||||||
got := cleanToolLog("\n[tool] shell_run {\"command\":\"pwd\"}\n")
|
got := cleanToolLog("\n[tool] shell_run {\"command\":\"pwd\"}\n")
|
||||||
want := `shell_run {"command":"pwd"}`
|
want := `shell_run {"command":"pwd"}`
|
||||||
|
|||||||
+68
-43
@@ -43,6 +43,7 @@ type theme struct {
|
|||||||
Background string
|
Background string
|
||||||
Surface string
|
Surface string
|
||||||
SurfaceAlt string
|
SurfaceAlt string
|
||||||
|
InputBg string
|
||||||
Border string
|
Border string
|
||||||
Text string
|
Text string
|
||||||
Muted string
|
Muted string
|
||||||
@@ -56,35 +57,37 @@ type theme struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type styles struct {
|
type styles struct {
|
||||||
App lipgloss.Style
|
App lipgloss.Style
|
||||||
Muted lipgloss.Style
|
Muted lipgloss.Style
|
||||||
Activity lipgloss.Style
|
Activity lipgloss.Style
|
||||||
Header lipgloss.Style
|
Header lipgloss.Style
|
||||||
ModelMeta lipgloss.Style
|
Footer lipgloss.Style
|
||||||
Footer lipgloss.Style
|
Viewport lipgloss.Style
|
||||||
Viewport lipgloss.Style
|
InputBox lipgloss.Style
|
||||||
InputBox lipgloss.Style
|
CommandHint lipgloss.Style
|
||||||
CommandHint lipgloss.Style
|
InputModeReadonly lipgloss.Style
|
||||||
StatusReady lipgloss.Style
|
InputModeYolo lipgloss.Style
|
||||||
StatusWarn lipgloss.Style
|
InputModeCommand lipgloss.Style
|
||||||
StatusError lipgloss.Style
|
StatusReady lipgloss.Style
|
||||||
StatusInfo lipgloss.Style
|
StatusWarn lipgloss.Style
|
||||||
Pill lipgloss.Style
|
StatusError lipgloss.Style
|
||||||
PillAccent lipgloss.Style
|
StatusInfo lipgloss.Style
|
||||||
Palette lipgloss.Style
|
Pill lipgloss.Style
|
||||||
PaletteMatch lipgloss.Style
|
PillAccent lipgloss.Style
|
||||||
ToolName lipgloss.Style
|
Palette lipgloss.Style
|
||||||
ToolKey lipgloss.Style
|
PaletteMatch lipgloss.Style
|
||||||
ToolValue lipgloss.Style
|
ToolName lipgloss.Style
|
||||||
UserLabel lipgloss.Style
|
ToolKey lipgloss.Style
|
||||||
ToolLabel lipgloss.Style
|
ToolValue lipgloss.Style
|
||||||
ErrorLabel lipgloss.Style
|
UserLabel lipgloss.Style
|
||||||
SystemLabel lipgloss.Style
|
ToolLabel lipgloss.Style
|
||||||
UserMessage lipgloss.Style
|
ErrorLabel lipgloss.Style
|
||||||
AssistantMsg lipgloss.Style
|
SystemLabel lipgloss.Style
|
||||||
ToolMessage lipgloss.Style
|
UserMessage lipgloss.Style
|
||||||
ErrorMessage lipgloss.Style
|
AssistantMsg lipgloss.Style
|
||||||
SystemMessage lipgloss.Style
|
ToolMessage lipgloss.Style
|
||||||
|
ErrorMessage lipgloss.Style
|
||||||
|
SystemMessage lipgloss.Style
|
||||||
}
|
}
|
||||||
|
|
||||||
func themeForMode(mode ThemeMode) theme {
|
func themeForMode(mode ThemeMode) theme {
|
||||||
@@ -98,8 +101,8 @@ func lightTheme() theme {
|
|||||||
return theme{
|
return theme{
|
||||||
Mode: ThemeLight,
|
Mode: ThemeLight,
|
||||||
Background: "#F8FAFC",
|
Background: "#F8FAFC",
|
||||||
Surface: "#FFFFFF",
|
|
||||||
SurfaceAlt: "#EEF2F7",
|
SurfaceAlt: "#EEF2F7",
|
||||||
|
InputBg: "#E4E4E4",
|
||||||
Border: "#CBD5E1",
|
Border: "#CBD5E1",
|
||||||
Text: "#111827",
|
Text: "#111827",
|
||||||
Muted: "#64748B",
|
Muted: "#64748B",
|
||||||
@@ -116,8 +119,8 @@ func darkTheme() theme {
|
|||||||
return theme{
|
return theme{
|
||||||
Mode: ThemeDark,
|
Mode: ThemeDark,
|
||||||
Background: "#111827",
|
Background: "#111827",
|
||||||
Surface: "#1F2937",
|
|
||||||
SurfaceAlt: "#0F172A",
|
SurfaceAlt: "#0F172A",
|
||||||
|
InputBg: "#383838",
|
||||||
Border: "#475569",
|
Border: "#475569",
|
||||||
Text: "#E5E7EB",
|
Text: "#E5E7EB",
|
||||||
Muted: "#94A3B8",
|
Muted: "#94A3B8",
|
||||||
@@ -130,10 +133,17 @@ func darkTheme() theme {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appStyle(t theme) lipgloss.Style {
|
||||||
|
style := lipgloss.NewStyle().Foreground(lipgloss.Color(t.Text))
|
||||||
|
if t.Mode == ThemeDark {
|
||||||
|
style = style.Background(lipgloss.Color(t.Background))
|
||||||
|
}
|
||||||
|
return style
|
||||||
|
}
|
||||||
|
|
||||||
func (t theme) styles() styles {
|
func (t theme) styles() styles {
|
||||||
return styles{
|
return styles{
|
||||||
App: lipgloss.NewStyle().
|
App: appStyle(t),
|
||||||
Foreground(lipgloss.Color(t.Text)),
|
|
||||||
|
|
||||||
Muted: lipgloss.NewStyle().
|
Muted: lipgloss.NewStyle().
|
||||||
Foreground(lipgloss.Color(t.Muted)),
|
Foreground(lipgloss.Color(t.Muted)),
|
||||||
@@ -146,10 +156,6 @@ func (t theme) styles() styles {
|
|||||||
Bold(true).
|
Bold(true).
|
||||||
Foreground(lipgloss.Color(t.Title)),
|
Foreground(lipgloss.Color(t.Title)),
|
||||||
|
|
||||||
ModelMeta: lipgloss.NewStyle().
|
|
||||||
Foreground(lipgloss.Color(t.Muted)).
|
|
||||||
Padding(0, 1),
|
|
||||||
|
|
||||||
Footer: lipgloss.NewStyle().
|
Footer: lipgloss.NewStyle().
|
||||||
Foreground(lipgloss.Color(t.Muted)).
|
Foreground(lipgloss.Color(t.Muted)).
|
||||||
Padding(0, 1),
|
Padding(0, 1),
|
||||||
@@ -159,14 +165,31 @@ func (t theme) styles() styles {
|
|||||||
|
|
||||||
InputBox: lipgloss.NewStyle().
|
InputBox: lipgloss.NewStyle().
|
||||||
Foreground(lipgloss.Color(t.Text)).
|
Foreground(lipgloss.Color(t.Text)).
|
||||||
Border(lipgloss.ThickBorder()).
|
Background(lipgloss.Color(t.InputBg)).
|
||||||
BorderForeground(lipgloss.Color(t.Border)).
|
Padding(1, 2, 1, 1),
|
||||||
Padding(0, 2),
|
|
||||||
|
|
||||||
CommandHint: lipgloss.NewStyle().
|
CommandHint: lipgloss.NewStyle().
|
||||||
Foreground(lipgloss.Color(t.Muted)).
|
Foreground(lipgloss.Color(t.Muted)).
|
||||||
Padding(0, 1),
|
Padding(0, 1),
|
||||||
|
|
||||||
|
InputModeReadonly: lipgloss.NewStyle().
|
||||||
|
Bold(true).
|
||||||
|
Foreground(lipgloss.Color(t.Muted)).
|
||||||
|
Background(lipgloss.Color(t.InputBg)).
|
||||||
|
Padding(0, 1),
|
||||||
|
|
||||||
|
InputModeYolo: lipgloss.NewStyle().
|
||||||
|
Bold(true).
|
||||||
|
Foreground(lipgloss.Color(t.Error)).
|
||||||
|
Background(lipgloss.Color(t.InputBg)).
|
||||||
|
Padding(0, 1),
|
||||||
|
|
||||||
|
InputModeCommand: lipgloss.NewStyle().
|
||||||
|
Bold(true).
|
||||||
|
Foreground(lipgloss.Color(t.User)).
|
||||||
|
Background(lipgloss.Color(t.InputBg)).
|
||||||
|
Padding(0, 1),
|
||||||
|
|
||||||
StatusReady: lipgloss.NewStyle().
|
StatusReady: lipgloss.NewStyle().
|
||||||
Foreground(lipgloss.Color(t.Assistant)),
|
Foreground(lipgloss.Color(t.Assistant)),
|
||||||
|
|
||||||
@@ -251,8 +274,10 @@ func bubbleMessageStyle(textColor, backgroundColor string) lipgloss.Style {
|
|||||||
func applyTextareaTheme(input *textarea.Model, t theme) {
|
func applyTextareaTheme(input *textarea.Model, t theme) {
|
||||||
focused := textarea.Style{
|
focused := textarea.Style{
|
||||||
Base: lipgloss.NewStyle().
|
Base: lipgloss.NewStyle().
|
||||||
Foreground(lipgloss.Color(t.Text)),
|
Foreground(lipgloss.Color(t.Text)).
|
||||||
CursorLine: lipgloss.NewStyle(),
|
Background(lipgloss.Color(t.InputBg)),
|
||||||
|
CursorLine: lipgloss.NewStyle().
|
||||||
|
Background(lipgloss.Color(t.InputBg)),
|
||||||
Placeholder: lipgloss.NewStyle().
|
Placeholder: lipgloss.NewStyle().
|
||||||
Foreground(lipgloss.Color(t.Muted)),
|
Foreground(lipgloss.Color(t.Muted)),
|
||||||
Prompt: lipgloss.NewStyle().
|
Prompt: lipgloss.NewStyle().
|
||||||
@@ -263,7 +288,7 @@ func applyTextareaTheme(input *textarea.Model, t theme) {
|
|||||||
Foreground(lipgloss.Color(t.Surface)),
|
Foreground(lipgloss.Color(t.Surface)),
|
||||||
}
|
}
|
||||||
blurred := focused
|
blurred := focused
|
||||||
blurred.CursorLine = lipgloss.NewStyle()
|
blurred.CursorLine = lipgloss.NewStyle().Background(lipgloss.Color(t.InputBg))
|
||||||
|
|
||||||
input.FocusedStyle = focused
|
input.FocusedStyle = focused
|
||||||
input.BlurredStyle = blurred
|
input.BlurredStyle = blurred
|
||||||
|
|||||||
@@ -1,228 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"agentu/internal/agent"
|
"agentu/internal/app"
|
||||||
"agentu/internal/session"
|
|
||||||
"agentu/internal/tools"
|
|
||||||
"agentu/internal/tui"
|
|
||||||
"agentu/pkg/config"
|
|
||||||
"agentu/pkg/llm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultConfigContent = `# agentu configuration
|
|
||||||
# Replace the placeholder values below with your actual provider settings.
|
|
||||||
# See agentu.example.yaml for the full reference.
|
|
||||||
providers:
|
|
||||||
default:
|
|
||||||
base_url: https://api.openai.com
|
|
||||||
api_key: YOUR_API_KEY_HERE
|
|
||||||
models:
|
|
||||||
- id: gpt-4o
|
|
||||||
context: 128000
|
|
||||||
- id: gpt-4o-mini
|
|
||||||
context: 128000
|
|
||||||
`
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := run(); err != nil {
|
if err := app.Run(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "agentu:", err)
|
fmt.Fprintln(os.Stderr, "agentu:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func run() error {
|
|
||||||
configPath := flag.String("config", config.DefaultPath, "path to YAML config")
|
|
||||||
yolo := flag.Bool("yolo", false, "also enable automatic file writes and shell execution")
|
|
||||||
plain := flag.Bool("plain", false, "use the simple line-based REPL instead of the TUI")
|
|
||||||
themeName := flag.String("theme", "light", "TUI theme: light or dark")
|
|
||||||
resumeID := flag.String("resume", "", "resume a specific session by ID")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
themeMode, err := tui.ParseThemeMode(*themeName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := config.Load(*configPath)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
if bootstrapErr := bootstrapConfig(*configPath); bootstrapErr != nil {
|
|
||||||
return bootstrapErr
|
|
||||||
}
|
|
||||||
return fmt.Errorf("created default config at %s; edit it to set your API key and provider, then restart agentu", *configPath)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
providerName := cfg.DefaultProviderName()
|
|
||||||
providerConfig := cfg.Providers[providerName]
|
|
||||||
provider := llm.NewOpenAICompatibleClient(providerConfig.BaseURL, providerConfig.APIKey, http.DefaultClient)
|
|
||||||
|
|
||||||
var registry *tools.Registry
|
|
||||||
if *yolo {
|
|
||||||
registry = tools.Builtins(cfg.Agent.WorkingDir)
|
|
||||||
} else {
|
|
||||||
registry = tools.ReadOnlyBuiltins(cfg.Agent.WorkingDir)
|
|
||||||
}
|
|
||||||
registry.Register(tools.NewBuiltinSearchTool(http.DefaultClient))
|
|
||||||
registry.Register(tools.NewBuiltinFetchTool(http.DefaultClient))
|
|
||||||
systemPrompt := strings.TrimSpace(cfg.Agent.SystemPrompt + "\n\n" + tools.AgentInstructions(cfg.Agent.WorkingDir, *yolo))
|
|
||||||
maxContextTokens := cfg.Agent.MaxContextTokens
|
|
||||||
if maxContextTokens == 0 {
|
|
||||||
maxContextTokens = providerConfig.ContextTokens
|
|
||||||
}
|
|
||||||
|
|
||||||
assistant := agent.New(agent.Options{
|
|
||||||
Provider: provider,
|
|
||||||
ProviderName: providerName,
|
|
||||||
Model: providerConfig.Model,
|
|
||||||
Thinking: providerConfig.Thinking,
|
|
||||||
ThinkingParam: providerConfig.ThinkingParam,
|
|
||||||
ThinkingEnabled: providerConfig.ThinkingConfigured,
|
|
||||||
SystemPrompt: systemPrompt,
|
|
||||||
ToolRegistry: registry,
|
|
||||||
ToolTimeout: cfg.Agent.ToolTimeout,
|
|
||||||
MaxContextTokens: maxContextTokens,
|
|
||||||
})
|
|
||||||
|
|
||||||
// Initialize session store
|
|
||||||
home, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("resolve home dir: %w", err)
|
|
||||||
}
|
|
||||||
sessionsDir := filepath.Join(home, ".agentu", "sessions")
|
|
||||||
store, err := session.NewStore(sessionsDir)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("init session store: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
modelManager, err := session.NewManager(cfg, assistant, http.DefaultClient, store)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := initializeSession(modelManager, *resumeID); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
||||||
defer stop()
|
|
||||||
|
|
||||||
if !*plain {
|
|
||||||
err := tui.Run(ctx, assistant, tui.Options{
|
|
||||||
ModelName: providerConfig.Model,
|
|
||||||
Yolo: *yolo,
|
|
||||||
ThemeMode: themeMode,
|
|
||||||
ModelManager: modelManager,
|
|
||||||
WorkingDir: cfg.Agent.WorkingDir,
|
|
||||||
})
|
|
||||||
printExitSession(modelManager)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return repl(ctx, assistant, modelManager)
|
|
||||||
}
|
|
||||||
|
|
||||||
func initializeSession(modelManager *session.Manager, resumeID string) error {
|
|
||||||
if resumeID != "" {
|
|
||||||
if err := modelManager.Resume(resumeID); err != nil {
|
|
||||||
return fmt.Errorf("resume session: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if _, err := modelManager.NewSession(); err != nil {
|
|
||||||
return fmt.Errorf("start new session: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func bootstrapConfig(configPath string) error {
|
|
||||||
resolved, err := config.ResolvePath(configPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("resolve config path: %w", err)
|
|
||||||
}
|
|
||||||
dir := filepath.Dir(resolved)
|
|
||||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
||||||
return fmt.Errorf("create config directory: %w", err)
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(resolved, []byte(defaultConfigContent), 0o644); err != nil {
|
|
||||||
return fmt.Errorf("write default config: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func repl(ctx context.Context, assistant *agent.Agent, modelManager *session.Manager) error {
|
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
|
||||||
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
|
|
||||||
|
|
||||||
fmt.Println("agentu")
|
|
||||||
fmt.Println("Type /exit to quit, /clear to reset context, /compact to compress context.")
|
|
||||||
for {
|
|
||||||
fmt.Print("> ")
|
|
||||||
if !scanner.Scan() {
|
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
printExitSession(modelManager)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
input := strings.TrimSpace(scanner.Text())
|
|
||||||
switch input {
|
|
||||||
case "":
|
|
||||||
continue
|
|
||||||
case "/exit", "/quit":
|
|
||||||
printExitSession(modelManager)
|
|
||||||
return nil
|
|
||||||
case "/clear":
|
|
||||||
assistant.Clear()
|
|
||||||
fmt.Println("context cleared")
|
|
||||||
continue
|
|
||||||
case "/compact":
|
|
||||||
if err := assistant.Compact(ctx); err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, "compact error:", err)
|
|
||||||
} else {
|
|
||||||
fmt.Println("context compacted")
|
|
||||||
if modelManager != nil {
|
|
||||||
_ = modelManager.Save()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := assistant.RunTurn(ctx, input, os.Stdout, os.Stderr); err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, "\nerror:", err)
|
|
||||||
}
|
|
||||||
_ = modelManager.Save()
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func printExitSession(m *session.Manager) {
|
|
||||||
if m == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_ = m.Save()
|
|
||||||
id := m.CurrentSessionID()
|
|
||||||
name := m.CurrentSessionName()
|
|
||||||
if id != "" {
|
|
||||||
if name != "" && name != id {
|
|
||||||
fmt.Fprintf(os.Stderr, "session: %s (%s)\n", id, name)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(os.Stderr, "session: %s\n", id)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, "resume: agentu --resume %s\n", id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user