ollie/agent/commands.go

53 lines
1.3 KiB
Go

package agent
import (
"context"
"os"
"os/exec"
"strings"
)
// HandleCommand intercepts /commands before they reach the LLM.
// / prefix routes to ctl via o: /compact → o ctl compact.
// Returns true if the input was consumed as a command.
func (ag *Agent) HandleCommand(_ context.Context, input string) bool {
if !strings.HasPrefix(input, "/") {
return false
}
rest := strings.TrimSpace(input[1:])
if rest == "" {
return false
}
args := append([]string{"ctl"}, strings.Fields(rest)...)
cmd := exec.Command("o", args...)
cmd.Env = append(os.Environ(),
"session="+ag.sessionID,
"agent="+ag.id,
)
out, err := cmd.CombinedOutput()
if len(out) > 0 {
ag.emit(infoEvent(strings.TrimRight(string(out), "\n")))
}
if err != nil {
ag.emit(infoEvent("error: " + err.Error()))
}
return true
}
// Inject sets a pending inject prompt. If one is already pending, queues it.
func (ag *Agent) Inject(prompt string) {
if !ag.pendingInject.CompareAndSwap(nil, &prompt) {
ag.fifo.Push(prompt)
return
}
ag.emit(Event{Role: "info", Content: "\n"})
ag.emit(Event{Role: "user", Content: prompt})
}
// InjectRewrite replaces any pending inject with the given prompt.
func (ag *Agent) InjectRewrite(prompt string) {
ag.pendingInject.Store(&prompt)
ag.emit(Event{Role: "info", Content: "\n"})
ag.emit(Event{Role: "user", Content: prompt})
}