some sloc optimizations

This commit is contained in:
Luxferre
2026-08-18 09:28:24 +03:00
parent b8686c82ab
commit 4a936e850d
2 changed files with 76 additions and 139 deletions
+43 -67
View File
@@ -703,12 +703,7 @@ type streamDelta struct {
func cleanMessagesForLLM(msgs []Message) []Message {
out := make([]Message, len(msgs))
for i, m := range msgs {
out[i] = Message{
Role: m.Role,
Content: m.Content,
ToolCalls: m.ToolCalls,
ToolCallID: m.ToolCallID,
}
out[i] = Message{Role: m.Role, Content: m.Content, ToolCalls: m.ToolCalls, ToolCallID: m.ToolCallID}
}
return out
}
@@ -719,9 +714,7 @@ func filterText(s string) string {
for _, r := range s {
if r == ' ' || r == '\t' || r == '\n' {
b.WriteRune(r)
} else if unicode.Is(unicode.Z, r) || unicode.IsControl(r) || unicode.Is(unicode.C, r) {
continue
} else if unicode.IsPrint(r) {
} else if !unicode.Is(unicode.Z, r) && !unicode.IsControl(r) && !unicode.Is(unicode.C, r) && unicode.IsPrint(r) {
b.WriteRune(r)
}
}
@@ -1430,6 +1423,42 @@ func readLine(prompt string) (string, bool) {
}
}
func runDirectShell(cmd string, timeout int) {
cmd = filterText(strings.TrimSpace(cmd))
if cmd == "" { return }
astr, _ := json.Marshal(map[string]string{"command": cmd})
fmt.Println(c(fmt.Sprintf("[tool call: shell_exec(%s)]", string(astr)), 33))
sigCtx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
res := shell(sigCtx, cmd, timeout)
cancel()
fmt.Println(c("[tool result: shell_exec]", 32) + "\n" + c(res, 2))
}
func doCompact(cfg *Cfg, msgs []Message) []Message {
if len(msgs) <= 1 {
fmt.Println(c("Nothing to compact yet.", 33))
return msgs
}
fmt.Println(c("[compacting conversation...]", 33))
sigCtx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
nm, sm, err := compact(sigCtx, cfg, msgs)
interrupted := sigCtx.Err() != nil
cancel()
if err != nil {
if interrupted || errors.Is(err, context.Canceled) {
fmt.Println(c("\n[interrupted]", 33))
} else {
fmt.Println(c("[compact failed: "+err.Error()+"]", 31))
}
return msgs
}
msgs = nm
autosave(msgs)
fmt.Println(c(fmt.Sprintf("[compacted to %d messages]", len(msgs)), 32))
fmt.Println(c("--- summary ---", 33) + "\n" + c(sm, 2))
return msgs
}
func main() {
sp := prompt("system.txt")
cfg := getCfg("model.cfg")
@@ -1446,15 +1475,7 @@ func main() {
}
u := strings.TrimSpace(string(data))
if strings.HasPrefix(u, "!") {
cmd := filterText(strings.TrimSpace(strings.TrimPrefix(u, "!")))
if cmd != "" {
astr, _ := json.Marshal(map[string]string{"command": cmd})
fmt.Println(c(fmt.Sprintf("[tool call: shell_exec(%s)]", string(astr)), 33))
sigCtx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
res := shell(sigCtx, cmd, cfg.ShellTimeout)
cancel()
fmt.Println(c("[tool result: shell_exec]", 32) + "\n" + c(res, 2))
}
runDirectShell(strings.TrimPrefix(u, "!"), cfg.ShellTimeout)
return
}
msgs = append(msgs, Message{Role: "user", Content: strp(u)})
@@ -1527,27 +1548,7 @@ func main() {
fmt.Println(c("[session loaded: "+parts[1]+"]", 32) + " " + c(summary(msgs), 2))
continue
case u == "/compact":
if len(msgs) <= 1 {
fmt.Println(c("Nothing to compact yet.", 33))
continue
}
fmt.Println(c("[compacting conversation...]", 33))
sigCtx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
nm, sm, err := compact(sigCtx, &cfg, msgs)
interrupted := sigCtx.Err() != nil
cancel()
if err != nil {
if interrupted || errors.Is(err, context.Canceled) {
fmt.Println(c("\n[interrupted]", 33))
} else {
fmt.Println(c("[compact failed: "+err.Error()+"]", 31))
}
continue
}
msgs = nm
autosave(msgs)
fmt.Println(c(fmt.Sprintf("[compacted to %d messages]", len(msgs)), 32))
fmt.Println(c("--- summary ---", 33) + "\n" + c(sm, 2))
msgs = doCompact(&cfg, msgs)
continue
case strings.HasPrefix(u, "/cfg"):
parts := strings.SplitN(u, " ", 3)
@@ -1575,15 +1576,7 @@ func main() {
}
continue
case strings.HasPrefix(u, "!"):
cmd := filterText(strings.TrimSpace(strings.TrimPrefix(u, "!")))
if cmd != "" {
astr, _ := json.Marshal(map[string]string{"command": cmd})
fmt.Println(c(fmt.Sprintf("[tool call: shell_exec(%s)]", string(astr)), 33))
sigCtx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
res := shell(sigCtx, cmd, cfg.ShellTimeout)
cancel()
fmt.Println(c("[tool result: shell_exec]", 32) + "\n" + c(res, 2))
}
runDirectShell(strings.TrimPrefix(u, "!"), cfg.ShellTimeout)
continue
case u == "/help":
fmt.Println(c("Bantam commands:", 1, 36))
@@ -1612,27 +1605,10 @@ func main() {
pct := float64(usg.PromptTokens) * 100.0 / float64(cfg.ContextWindow)
if pct >= 60.0 && len(msgs) > 1 {
fmt.Print(c(fmt.Sprintf("Context usage is at %.1f%% (%d / %d tokens). Compact conversation? [Y/n]: ", pct, usg.PromptTokens, cfg.ContextWindow), 33))
ans, ok := readPlain("")
if ok {
if ans, ok := readPlain(""); ok {
ans = strings.TrimSpace(strings.ToLower(ans))
if ans == "" || ans == "y" || ans == "yes" {
fmt.Println(c("[compacting conversation...]", 33))
sigCtx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
nm, sm, err := compact(sigCtx, &cfg, msgs)
interrupted := sigCtx.Err() != nil
cancel()
if err != nil {
if interrupted || errors.Is(err, context.Canceled) {
fmt.Println(c("\n[interrupted]", 33))
} else {
fmt.Println(c("[compact failed: "+err.Error()+"]", 31))
}
} else {
msgs = nm
autosave(msgs)
fmt.Println(c(fmt.Sprintf("[compacted to %d messages]", len(msgs)), 32))
fmt.Println(c("--- summary ---", 33) + "\n" + c(sm, 2))
}
msgs = doCompact(&cfg, msgs)
}
}
}