50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// HandleCommand intercepts /commands before they reach the LLM.
|
|
// / prefix routes to ctl via o: /compact → o session/agent 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
|
|
}
|
|
// Use session/agent context syntax
|
|
ctx := ag.sessionID + "/" + ag.id
|
|
args := append([]string{ctx, "ctl"}, strings.Fields(rest)...)
|
|
cmd := exec.Command("o", args...)
|
|
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})
|
|
}
|