dabf5bfecc
- Replace fixed 6-message compact retention with ratio-based logic: retain ~20% of compactable estimated tokens, with a 6-message floor and user-turn boundary alignment - Add compactRetainedTokenBudget, compactRecentStart, compactTurnBoundary helpers in internal/agent/agent.go - Add TestAgentCompactKeepsTwentyPercentRecentContext - Move startup/session/repl/bootstrap logic from root into internal/app so main.go is a thin binary entry point (15 lines) - Update /compact docs in README.md
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
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()
|
|
}
|
|
}
|