mirror of
https://github.com/patriceckhart/zot.git
synced 2026-06-26 21:36:31 +02:00
Clears every deferred extension todo in one push:
1) Interception expands to three events: tool_call (already shipped),
turn_start (gate the turn before the model call, e.g. rate-limit /
business-hour), and assistant_message (suppress or rewrite the
user-visible text while keeping the model's original output in
the transcript).
2) Tool-call args can now be rewritten mid-flight. An interceptor
returning modified_args replaces the JSON the tool actually
receives, without the model seeing the rewrite. Chains: each
subscriber sees the previous one's output, letting guards
successively redact / patch / augment. Invalid JSON is dropped
safely.
3) /reload-ext hot-reloads every extension without restarting zot.
The manager gracefully shuts down all running subprocesses,
re-reads extension.json from disk, respawns (including --ext
paths remembered from startup), and the host rebuilds the agent's
tool registry in-place so freshly-registered tools are callable
immediately.
Wire-format changes (extproto):
- EventInterceptResponseFromExt gains modified_args and replace_text
fields (both optional, ignored when block=true).
- EventInterceptFromHost gains Step (for turn_start) and Text (for
assistant_message) alongside the existing tool_call payload.
Core agent changes:
- BeforeToolExecute signature now returns (allowed, reason,
modifiedArgs json.RawMessage). Non-nil+valid JSON args replace
tc.Arguments before Tool.Execute runs.
- New BeforeTurn hook, invoked in runLoop before oneTurn. Blocking
cancels the turn with an EvTurnEnd{StopError} carrying the reason.
- New BeforeAssistantMessage hook, invoked after finalMsg is
assembled but before the EvAssistantMessage emit. Supports
suppress (block=true) and text rewrite (replace_text). Transcript
always gets the original; UI gets the rewritten text.
- New SetTools(reg) so /reload-ext can swap the registry on the
live agent under the agent mutex.
Manager changes:
- InterceptToolCall now returns InterceptResult (Block, Reason,
ModifiedArgs, ReplaceText), with a chain that folds rewrites.
- New InterceptTurnStart and InterceptAssistantMessage.
- New Reload(ctx, grace) tears down and respawns everything,
returning ReloadStats{Stopped, Loaded, Ready, Errors}.
- New SetOnReload(fn) callback the host uses to rebuild the agent
tool registry after a reload.
- LoadExplicit remembers --ext paths so Reload respawns them.
- subscribe accepts "tool_call", "turn_start", "assistant_message"
under "intercept".
SDK (pkg/zotext):
- New handler types: ToolCallHandler, TurnStartHandler,
AssistantMessageHandler, and their decision structs
(ToolCallDecision with ModifiedArgs, AssistantMessageDecision
with ReplaceText).
- New registration methods: InterceptToolCallX (rich variant of
the existing InterceptToolCall), InterceptTurnStart,
InterceptAssistantMessage.
- dispatchIntercept routes per-event with panic recovery and
always emits exactly one event_intercept_response.
TUI:
- /reload-ext slash command registered in slashCatalog and
runSlash. Added to slashCancelsTurn so it waits for idle like
/compact does.
- runReloadExt shows a "reloading extensions..." status, runs the
Manager.Reload on a goroutine, and reports the resulting stats.
Tests:
- internal/core/intercept_test.go: verifies args are actually
rewritten on the way to Tool.Execute, malformed JSON is ignored,
and block surfaces the reason as an error ToolResult.
- internal/agent/extensions/intercept_test.go: end-to-end with a
bash extension subprocess that blocks rm -rf, rewrites other bash
args to "echo GUARDED:", passes through read calls, allows
turn_start, and redacts SECRET in assistant messages. Second test
verifies Reload respawns the subprocess, re-registers its command,
and fires the onReload callback.
Docs:
- docs/extensions.md: rewrote the intercept section to cover all
three events, added a table of event_intercept_response fields,
documented the /reload-ext hot-reload command, expanded the SDK
section with examples of every handler, moved the old "future"
items into a shipped Phase 4.
- README.md: extensions summary mentions intercept beyond tool_call,
/reload-ext added to the slash-commands table and to the
turn-cancel list in "Queued messages".
356 lines
10 KiB
Go
356 lines
10 KiB
Go
package modes
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/patriceckhart/zot/internal/tui"
|
|
)
|
|
|
|
// slashCommand is one entry in the autocomplete popup. Header rows
|
|
// (group dividers like "── extensions ───") are real entries
|
|
// flagged with header=true; they render but aren't navigable.
|
|
type slashCommand struct {
|
|
Name string // with leading "/"
|
|
Desc string
|
|
Header bool // true = visual divider, not selectable
|
|
}
|
|
|
|
// slashCancelsTurn reports whether the named slash command, when run
|
|
// while a turn is in flight, requires the active turn to be cancelled
|
|
// first. The destructive commands (those that mutate the transcript
|
|
// or rebuild the agent) need a quiet state; the rest run alongside
|
|
// the streaming response without trouble.
|
|
func slashCancelsTurn(head string) bool {
|
|
switch head {
|
|
case "/clear", "/compact", "/logout", "/login", "/model", "/reload-ext":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// slashCatalog lists every slash command the interactive mode handles.
|
|
// Keep in sync with runSlash().
|
|
var slashCatalog = []slashCommand{
|
|
{Name: "/help", Desc: "show key bindings and commands"},
|
|
{Name: "/login", Desc: "log in via api key or subscription"},
|
|
{Name: "/logout", Desc: "clear a provider's credentials"},
|
|
{Name: "/model", Desc: "pick a model (or /model <id>)"},
|
|
{Name: "/sessions", Desc: "resume a previous session for this directory"},
|
|
{Name: "/jump", Desc: "scroll the chat to a previous turn (or /jump <text>)"},
|
|
{Name: "/btw", Desc: "side-chat that doesn't add to the main thread (saves tokens)"},
|
|
{Name: "/skills", Desc: "list discovered skills (SKILL.md files)"},
|
|
{Name: "/compact", Desc: "summarize and replace the transcript to free up context"},
|
|
{Name: "/lock", Desc: "confine tools to the current directory"},
|
|
{Name: "/unlock", Desc: "allow tools to touch paths outside this directory"},
|
|
{Name: "/reload-ext", Desc: "hot-reload all extensions (re-read manifests and respawn)"},
|
|
{Name: "/clear", Desc: "clear the chat transcript"},
|
|
{Name: "/exit", Desc: "exit zot"},
|
|
}
|
|
|
|
// slashSuggester renders the popup that appears when the editor starts
|
|
// with "/". It does not own any input state — the editor drives.
|
|
type slashSuggester struct {
|
|
cursor int
|
|
|
|
// extra are commands contributed by extensions, refreshed each
|
|
// frame from the extension manager. Empty when no extensions
|
|
// have registered any. Sorted by name in SetExtra so map
|
|
// iteration order doesn't reshuffle the popup between frames.
|
|
extra []slashCommand
|
|
|
|
// lastMatches is the list shown in the most recent Render call.
|
|
// Up/Down read it so they know which indexes to skip across
|
|
// header rows.
|
|
lastMatches []slashCommand
|
|
}
|
|
|
|
// SetExtra updates the extension-contributed command list. Called
|
|
// once per render with the live snapshot from the extension manager.
|
|
// The list is sorted by name so the popup ordering stays stable
|
|
// across redraws (Manager.Commands() iterates a map, which Go
|
|
// randomises).
|
|
func (s *slashSuggester) SetExtra(cmds []slashCommand) {
|
|
sorted := append([]slashCommand(nil), cmds...)
|
|
sort.Slice(sorted, func(i, j int) bool { return sorted[i].Name < sorted[j].Name })
|
|
s.extra = sorted
|
|
}
|
|
|
|
// allCatalog returns slashCatalog plus the current extra commands
|
|
// (extension-registered) with a header divider between the two
|
|
// groups. Extra entries are only kept if they don't collide with
|
|
// a built-in name; the built-in always wins.
|
|
func (s *slashSuggester) allCatalog() []slashCommand {
|
|
if len(s.extra) == 0 {
|
|
return slashCatalog
|
|
}
|
|
out := make([]slashCommand, 0, len(slashCatalog)+len(s.extra)+1)
|
|
out = append(out, slashCatalog...)
|
|
var kept []slashCommand
|
|
for _, c := range s.extra {
|
|
dup := false
|
|
for _, b := range slashCatalog {
|
|
if b.Name == c.Name {
|
|
dup = true
|
|
break
|
|
}
|
|
}
|
|
if !dup {
|
|
kept = append(kept, c)
|
|
}
|
|
}
|
|
if len(kept) > 0 {
|
|
out = append(out, slashCommand{Header: true, Name: "extensions"})
|
|
out = append(out, kept...)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// looksLikeSlashCommand reports whether text is an attempt at a slash
|
|
// command (valid or not). Returns true for things like "/foo" or
|
|
// "/bar baz" but false for paths ("/Users/pat/...") and regexes
|
|
// ("/foo.bar/") so those can be sent to the model as-is.
|
|
//
|
|
// The head after "/" must be a single simple word: only letters,
|
|
// digits, hyphens, and underscores. That excludes paths (contain "/"),
|
|
// regexes (contain "."), and URLs.
|
|
func looksLikeSlashCommand(text string) bool {
|
|
text = strings.TrimSpace(text)
|
|
if len(text) < 2 || text[0] != '/' {
|
|
return false
|
|
}
|
|
head := text[1:]
|
|
if i := strings.IndexAny(head, " \t\n"); i >= 0 {
|
|
head = head[:i]
|
|
}
|
|
if head == "" {
|
|
return false
|
|
}
|
|
for _, r := range head {
|
|
if !(r >= 'a' && r <= 'z') && !(r >= 'A' && r <= 'Z') &&
|
|
!(r >= '0' && r <= '9') && r != '-' && r != '_' {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// isKnownSlashCommand reports whether text's head matches a registered
|
|
// slash command name in slashCatalog. Built-in only; extension
|
|
// commands are looked up separately by the dispatcher (which
|
|
// consults the extension manager).
|
|
func isKnownSlashCommand(text string) bool {
|
|
text = strings.TrimSpace(text)
|
|
if text == "" || text[0] != '/' {
|
|
return false
|
|
}
|
|
head := text
|
|
if i := strings.IndexAny(text, " \t\n"); i >= 0 {
|
|
head = text[:i]
|
|
}
|
|
for _, c := range slashCatalog {
|
|
if c.Name == head {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func newSlashSuggester() *slashSuggester { return &slashSuggester{} }
|
|
|
|
// matches returns the commands whose name has input as a prefix.
|
|
// If input is just "/", everything is shown.
|
|
func (s *slashSuggester) matches(input string) []slashCommand {
|
|
input = strings.TrimRight(input, " ")
|
|
if input == "" || !strings.HasPrefix(input, "/") {
|
|
return nil
|
|
}
|
|
// If there is a space, the user has moved past the command name.
|
|
if idx := strings.IndexByte(input, ' '); idx >= 0 {
|
|
return nil
|
|
}
|
|
var out []slashCommand
|
|
for _, c := range s.allCatalog() {
|
|
if c.Header {
|
|
// Headers ride along whenever there's at least one
|
|
// matching command from their group; we drop trailing
|
|
// orphan headers below.
|
|
out = append(out, c)
|
|
continue
|
|
}
|
|
if strings.HasPrefix(c.Name, input) {
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
return pruneOrphanHeaders(out)
|
|
}
|
|
|
|
// pruneOrphanHeaders removes header rows that have no commands
|
|
// after them (i.e. the next non-header is missing or another
|
|
// header). Keeps the popup clean when the input filters out a whole
|
|
// group.
|
|
func pruneOrphanHeaders(in []slashCommand) []slashCommand {
|
|
out := make([]slashCommand, 0, len(in))
|
|
for i, c := range in {
|
|
if c.Header {
|
|
nextReal := false
|
|
for j := i + 1; j < len(in); j++ {
|
|
if !in[j].Header {
|
|
nextReal = true
|
|
break
|
|
}
|
|
}
|
|
if !nextReal {
|
|
continue
|
|
}
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// clampCursor keeps the cursor inside the current match list and
|
|
// nudges it past any header row so navigation never lands on one.
|
|
func (s *slashSuggester) clampCursor(n int) {
|
|
if n <= 0 {
|
|
s.cursor = 0
|
|
return
|
|
}
|
|
if s.cursor < 0 {
|
|
s.cursor = 0
|
|
}
|
|
if s.cursor >= n {
|
|
s.cursor = n - 1
|
|
}
|
|
}
|
|
|
|
// Up / Down navigate the suggestion list, skipping header rows in
|
|
// either direction so the cursor only ever lands on selectable
|
|
// commands.
|
|
func (s *slashSuggester) Up() {
|
|
s.skipHeader(-1)
|
|
}
|
|
func (s *slashSuggester) Down() {
|
|
s.skipHeader(+1)
|
|
}
|
|
|
|
// skipHeader moves the cursor by step, then keeps moving in the same
|
|
// direction across header rows until it lands on a real command (or
|
|
// hits the edge, in which case it bounces back to the nearest real
|
|
// row).
|
|
func (s *slashSuggester) skipHeader(step int) {
|
|
list := s.lastMatches
|
|
n := len(list)
|
|
if n == 0 {
|
|
return
|
|
}
|
|
s.cursor += step
|
|
for s.cursor >= 0 && s.cursor < n && list[s.cursor].Header {
|
|
s.cursor += step
|
|
}
|
|
if s.cursor < 0 {
|
|
// Bounce: find the first non-header from the top.
|
|
for i, c := range list {
|
|
if !c.Header {
|
|
s.cursor = i
|
|
return
|
|
}
|
|
}
|
|
s.cursor = 0
|
|
}
|
|
if s.cursor >= n {
|
|
// Bounce: find the last non-header.
|
|
for i := n - 1; i >= 0; i-- {
|
|
if !list[i].Header {
|
|
s.cursor = i
|
|
return
|
|
}
|
|
}
|
|
s.cursor = n - 1
|
|
}
|
|
}
|
|
|
|
// Active reports whether the popup is visible for the given input.
|
|
func (s *slashSuggester) Active(input string) bool {
|
|
return len(s.matches(input)) > 0
|
|
}
|
|
|
|
// Selection returns the currently highlighted command for input, or "".
|
|
// Headers are never returned even if the cursor index would point at
|
|
// one; the cursor is moved forward to the next real command.
|
|
func (s *slashSuggester) Selection(input string) string {
|
|
m := s.matches(input)
|
|
if len(m) == 0 {
|
|
return ""
|
|
}
|
|
s.clampCursor(len(m))
|
|
if m[s.cursor].Header {
|
|
for i := s.cursor + 1; i < len(m); i++ {
|
|
if !m[i].Header {
|
|
s.cursor = i
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if m[s.cursor].Header {
|
|
return ""
|
|
}
|
|
return m[s.cursor].Name
|
|
}
|
|
|
|
// Render returns the popup lines or nil.
|
|
func (s *slashSuggester) Render(input string, th tui.Theme, width int) []string {
|
|
m := s.matches(input)
|
|
if len(m) == 0 {
|
|
return nil
|
|
}
|
|
s.lastMatches = m
|
|
s.clampCursor(len(m))
|
|
// Snap cursor off any header (e.g. after a filter change put it on one).
|
|
if s.cursor >= 0 && s.cursor < len(m) && m[s.cursor].Header {
|
|
for i := s.cursor + 1; i < len(m); i++ {
|
|
if !m[i].Header {
|
|
s.cursor = i
|
|
break
|
|
}
|
|
}
|
|
}
|
|
var lines []string
|
|
for i, c := range m {
|
|
if c.Header {
|
|
// Breathing room around group dividers — a blank row
|
|
// before AND after makes the boundary read at a glance.
|
|
lines = append(lines, "")
|
|
rule := strings.Repeat("─", width)
|
|
label := "── " + c.Name + " "
|
|
if len(label) < width {
|
|
rule = label + strings.Repeat("─", width-len(label))
|
|
}
|
|
lines = append(lines, th.FG256(th.Muted, rule))
|
|
lines = append(lines, "")
|
|
continue
|
|
}
|
|
name := c.Name
|
|
if len(name) < 10 {
|
|
name = name + strings.Repeat(" ", 10-len(name))
|
|
}
|
|
plain := " " + name + " " + c.Desc
|
|
if i == s.cursor {
|
|
lines = append(lines, th.PadHighlight(plain, width))
|
|
} else {
|
|
lines = append(lines, th.FG256(th.Muted, plain))
|
|
}
|
|
}
|
|
// Blank row before the hint visually detaches it from the
|
|
// command list and groups it with its trailing blank.
|
|
lines = append(lines, "")
|
|
lines = append(lines, th.FG256(th.Muted, " ↑/↓ navigate · tab complete · enter run"))
|
|
// Blank row after the hint separates the popup from the status
|
|
// bar / editor below it.
|
|
lines = append(lines, "")
|
|
return lines
|
|
}
|
|
|
|
// Reset puts the cursor back to the first match. Call this whenever the
|
|
// input changes in a way that reshapes the match list.
|
|
func (s *slashSuggester) Reset() { s.cursor = 0 }
|