v0.32.0 362913c

interactive_host.go RawHistoryFolder

1package modes
2
3import (
4	"context"
5	"encoding/json"
6	"fmt"
7	"os"
8	"strings"
9	"time"
10
11	"github.com/bnema/zut/packages/agent/extproto"
12	"github.com/bnema/zut/packages/agent/internal/orchestration"
13	"github.com/bnema/zut/packages/agent/subagents"
14	"github.com/bnema/zut/packages/core"
15	"github.com/bnema/zut/packages/provider"
16	"github.com/bnema/zut/packages/tui"
17)
18
19func (i *Interactive) invokeExtensionCommand(ctx context.Context, name, args string) {
20	resp, err := i.cfg.Extensions.Invoke(ctx, name, args, 30*time.Second)
21	if err != nil {
22		i.mu.Lock()
23		i.statusErr = "extension /" + name + ": " + err.Error()
24		i.statusOK = ""
25		i.mu.Unlock()
26		i.invalidate()
27		return
28	}
29	if resp.Error != "" {
30		i.mu.Lock()
31		i.statusErr = "extension /" + name + ": " + resp.Error
32		i.statusOK = ""
33		i.mu.Unlock()
34		i.invalidate()
35		return
36	}
37	switch resp.Action {
38	case "open_panel":
39		if resp.OpenPanel != nil {
40			extName := name
41			if i.cfg.Extensions != nil {
42				if owner := i.cfg.Extensions.CommandOwner(name); owner != "" {
43					extName = owner
44				}
45			}
46			i.OpenPanel(extName, *resp.OpenPanel)
47		}
48	case "prompt":
49		if strings.TrimSpace(resp.Prompt) == "" {
50			return
51		}
52		i.startTurn(i.runCtx, resp.Prompt)
53	case "insert":
54		i.ed.Insert(resp.Insert)
55		i.invalidate()
56	case "display":
57		i.appendExtensionNote(name, resp.Display, "info")
58	case "noop", "":
59		// nothing
60	default:
61		i.mu.Lock()
62		i.statusErr = "extension /" + name + ": unknown action " + resp.Action
63		i.mu.Unlock()
64		i.invalidate()
65	}
66}
67func (i *Interactive) appendExtensionNote(extName, msg, level string) {
68	if msg == "" {
69		return
70	}
71	i.mu.Lock()
72	defer i.mu.Unlock()
73	color := i.cfg.Theme.Muted
74	switch level {
75	case "warn":
76		color = i.cfg.Theme.Warning
77	case "error":
78		color = i.cfg.Theme.Error
79	case "success":
80		color = i.cfg.Theme.Tool
81	}
82	prefix := i.cfg.Theme.FGColor(i.cfg.Theme.Accent, "["+extName+"] ")
83	for _, line := range strings.Split(msg, "\n") {
84		i.statusOK = "" // clear any stale ok
85		i.statusErr = ""
86		i.extNotes = append(i.extNotes, prefix+i.cfg.Theme.FGColor(color, line))
87	}
88	// Extension slash commands complete asynchronously. If their result is
89	// displayed while confirmation is waiting, keep Esc assigned to the note
90	// until it is dismissed rather than letting it answer the tool call.
91	i.confirmDialog.Blur()
92}
93func (i *Interactive) ApplySessionAgent(ag *core.Agent, providerName, model string) {
94	i.ApplySessionAgentWithCompactHandoff(ag, providerName, model, nil)
95}
96func (i *Interactive) ApplySessionAgentWithCompactHandoff(ag *core.Agent, providerName, model string, compactHandoff json.RawMessage) {
97	if ag == nil {
98		return
99	}
100	i.agentMu.Lock()
101	defer i.agentMu.Unlock()
102	i.mu.Lock()
103	i.prepareReplacementAgentLocked(ag)
104	i.compactContinuation = decodeCompactHandoff(compactHandoff)
105	i.agent = ag
106	i.cfg.Provider = providerName
107	i.cfg.Model = model
108	i.cfg.detachMismatchedModelProfile()
109	i.view.Messages = filterHiddenTranscriptMessages(ag.Messages())
110	i.cumUsage = ag.Cost()
111	last := ag.LastTurnUsage()
112	i.lastCtxInput = last.InputTokens + last.CacheReadTokens + last.CacheWriteTokens
113	if len(i.view.Messages) > initialResumeTailLimit {
114		i.view.TailLimit = initialResumeTailLimit
115	} else {
116		i.view.TailLimit = 0
117	}
118	i.view.InvalidateRenderCache()
119	i.toolCalls = map[string]*tui.ToolCallView{}
120	i.toolOrder = nil
121	i.toolGate = map[string]int{}
122	i.helpBlock = nil
123	i.sessionInfoBlocks = nil
124	i.extNotes = nil
125	i.parkedTurn = 0
126	i.parkedTotal = 0
127	i.mu.Unlock()
128	i.invalidate()
129}
130
131// ApplySessionAgentWithCWD commits a resumed agent together with its stored
132// workspace. Unlike a fresh /cd transition it preserves the session's compact
133// handoff and title while rebinding picker and terminal cwd state.
134func (i *Interactive) ApplySessionAgentWithCWD(ag *core.Agent, providerName, model, cwd string, compactHandoff json.RawMessage) {
135	i.ApplySessionAgentWithCompactHandoff(ag, providerName, model, compactHandoff)
136	if cwd == "" {
137		return
138	}
139	home, _ := os.UserHomeDir()
140	profiles, _ := subagents.Discover(cwd, home)
141	i.mu.Lock()
142	i.cfg.CWD = cwd
143	i.cfg.SubagentsSystemAddendum = subagents.SystemPromptAddendum(profiles)
144	i.managedAutoSubagentsAddenda = autoSubagentsAddenda(i.cfg, i.autoSubagentsEnabledLocked())
145	i.cfg.StartupContextPaths = nil
146	i.view.StartupContextPaths = nil
147	if i.cfg.Terminal != nil {
148		if seq := tui.ReportCWD(cwd); seq != "" {
149			_, _ = i.cfg.Terminal.Write([]byte(seq))
150		}
151	}
152	i.mu.Unlock()
153	i.fileSuggest.Reset()
154	i.fileSuggest.SetCWD(cwd)
155	i.invalidate()
156}
157
158func (i *Interactive) SetSubagentSessionScope(_ string) {
159	i.invalidate()
160}
161func (i *Interactive) ApplyChangedCWD(ag *core.Agent, provider, model, cwd string) {
162	i.applyChangedCWD(ag, provider, model, cwd, nil)
163}
164func (i *Interactive) ApplyChangedCWDWithStartupContext(ag *core.Agent, provider, model, cwd string, startupContextPaths []string) {
165	i.applyChangedCWD(ag, provider, model, cwd, startupContextPaths)
166}
167func (i *Interactive) applyChangedCWD(ag *core.Agent, provider, model, cwd string, startupContextPaths []string) {
168	home, _ := os.UserHomeDir()
169	profiles, _ := subagents.Discover(cwd, home)
170	subagentsAddendum := subagents.SystemPromptAddendum(profiles)
171
172	i.agentMu.Lock()
173	defer i.agentMu.Unlock()
174	i.mu.Lock()
175	i.prepareReplacementAgentLocked(ag)
176	i.resetCompactContinuationLocked()
177	i.agent = ag
178	i.cfg.CWD = cwd
179	i.cfg.SubagentsSystemAddendum = subagentsAddendum
180	i.managedAutoSubagentsAddenda = autoSubagentsAddenda(i.cfg, i.autoSubagentsEnabledLocked())
181	i.cfg.StartupContextPaths = append([]string(nil), startupContextPaths...)
182	i.view.StartupContextPaths = nil
183	i.view.InvalidateRenderCache()
184	if i.cfg.ShowInstructionsAtStartup != nil && *i.cfg.ShowInstructionsAtStartup {
185		i.view.StartupContextPaths = append(i.view.StartupContextPaths, startupContextPaths...)
186	}
187	// Re-report the working directory to the terminal so "new tab here"
188	// tracks the /cd change (OSC 7, see issue #38).
189	if i.cfg.Terminal != nil {
190		if seq := tui.ReportCWD(cwd); seq != "" {
191			_, _ = i.cfg.Terminal.Write([]byte(seq))
192		}
193	}
194	i.cfg.Provider = provider
195	i.cfg.Model = model
196	i.cfg.detachMismatchedModelProfile()
197	titleCancel := i.titleCancel
198	i.titleCancel = nil
199	i.titleVersion++
200	i.sessionTitle = ""
201	i.titleRealPromptSeen = false
202	i.titleGenerationStarted = false
203	i.writeTerminalTitleLocked("")
204	i.toolCalls = map[string]*tui.ToolCallView{}
205	i.toolOrder = nil
206	i.toolGate = map[string]int{}
207	i.helpBlock = nil
208	i.sessionInfoBlocks = nil
209	i.parkedTurn = 0
210	i.statusErr = ""
211	i.mu.Unlock()
212	if titleCancel != nil {
213		titleCancel()
214	}
215	i.fileSuggest.Reset()
216	i.fileSuggest.SetCWD(cwd)
217	i.invalidate()
218}
219func (i *Interactive) SubmitSlash(text string) {
220	i.mu.Lock()
221	i.doubleEscape.Reset()
222	i.mu.Unlock()
223	text = strings.TrimSpace(text)
224	if !strings.HasPrefix(text, "/") {
225		return
226	}
227	if slashCommandCancelsTurn(text) {
228		i.cancelAndWaitForIdle()
229	}
230	i.runSlash(i.runCtx, text)
231	i.invalidate()
232}
233func (i *Interactive) SubmitOrQueue(text string, images []provider.ImageBlock) {
234	i.submitOrQueue(text, images, true)
235}
236
237// SubmitFollowUp submits a scheduler-owned prompt without interpreting shell
238// escapes or steering an active agent loop. A busy interactive waits for its
239// current turn to end, then starts this prompt as a distinct follow-up turn.
240func (i *Interactive) SubmitFollowUp(ctx context.Context, text string) error {
241	text = strings.TrimSpace(text)
242	if text == "" {
243		return fmt.Errorf("scheduled prompt is empty")
244	}
245	i.mu.Lock()
246	if i.agent == nil {
247		i.mu.Unlock()
248		return fmt.Errorf("no agent running; log in first")
249	}
250	if i.busy {
251		accepted := make(chan error, 1)
252		i.scheduled = append(i.scheduled, scheduledFollowUp{text: text, accepted: accepted})
253		i.mu.Unlock()
254		i.invalidate()
255		select {
256		case err := <-accepted:
257			return err
258		case <-ctx.Done():
259			return ctx.Err()
260		}
261	}
262	i.mu.Unlock()
263	i.startTurn(i.runCtx, text)
264	return nil
265}
266func (i *Interactive) submitOrQueue(text string, images []provider.ImageBlock, userInput bool) {
267	i.submitOrQueueMessage(core.QueuedMessage{Text: text, Images: images}, userInput)
268}
269func (i *Interactive) submitOrQueueMessage(message core.QueuedMessage, userInput bool) {
270	text, images := message.Text, message.Images
271	if cmd, ok := shellEscapeCommand(text); ok {
272		i.startShellEscape(i.runCtx, cmd)
273		return
274	}
275	i.mu.Lock()
276	if i.agent == nil {
277		i.statusErr = "not logged in. type /login first."
278		i.mu.Unlock()
279		i.invalidate()
280		return
281	}
282	i.mu.Unlock()
283	if userInput && i.cfg.EnsureMission != nil {
284		if err := i.cfg.EnsureMission(text); err != nil {
285			i.ReportError(fmt.Errorf("persist mission: %w", err))
286			return
287		}
288	}
289	// User input is an orchestration event, not merely a queue append. When
290	// the manager is dormant behind a sealed worker wave, the coordinator makes
291	// this the next manager turn and carries any completed worker summary with it.
292	if userInput && i.coordinatorAcceptsUserInput() {
293		actions := i.applyCoordinator(orchestration.Event{Kind: orchestration.EventUserInput, Text: text, Images: images})
294		i.executeCoordinatorActions(actions)
295		// The coordinator has accepted the input for its sealed worker wave.
296		// It may intentionally defer an action until pending workers finish;
297		// falling through would start a normal turn and discard that wave.
298		return
299	}
300	i.maybeStartSessionTitle(i.runCtx, text)
301	i.mu.Lock()
302	if i.busy {
303		// Keep the interactive mutex held through Agent.QueueMessage so turn teardown
304		// cannot inspect the agent queue between the busy check and enqueue.
305		// Compaction uses the host queue because it has no active agent loop
306		// to drain Agent.QueueMessage entries.
307		var handoff json.RawMessage
308		var persistHandoff bool
309		if i.agent != nil && !i.compacting {
310			handoff, persistHandoff = i.resetCompactContinuationLocked()
311			i.agent.QueuePrompt(message)
312		} else {
313			handoff, persistHandoff = i.resetCompactContinuationLocked()
314			i.queued = append(i.queued, message)
315		}
316		i.mu.Unlock()
317		if persistHandoff {
318			i.persistCompactHandoff(handoff)
319		}
320		i.invalidate()
321		return
322	}
323	i.mu.Unlock()
324	i.startQueuedTurn(i.runCtx, message)
325}
326
327// discardQueuedMessagesLocked drops stale input after an error or cancellation.
328// Host evidence survives errors in the agent queue for the next explicit turn;
329// it must not automatically retry the failed provider or compaction request.
330func (i *Interactive) discardQueuedMessagesLocked(preserveHostEvents bool) {
331	var pending []core.QueuedMessage
332	if i.agent != nil {
333		pending = i.agent.DrainQueuedMessages()
334	}
335	pending = append(pending, i.queued...)
336	i.queued = nil
337	if !preserveHostEvents {
338		return
339	}
340	for _, message := range pending {
341		if !message.HostEvent {
342			continue
343		}
344		if i.agent != nil {
345			i.agent.QueuePrompt(message)
346		} else {
347			i.queued = append(i.queued, message)
348		}
349	}
350}
351
352func (i *Interactive) ChangelogVersion() string {
353	if i.changelogDialog != nil {
354		return i.changelogDialog.version
355	}
356	return ""
357}
358func (i *Interactive) CancelTurn() {
359	i.mu.Lock()
360	cancel := i.cancelTurn
361	i.mu.Unlock()
362	if cancel != nil {
363		i.cancelCoordinator()
364
365		i.mu.Lock()
366		handoff, persistHandoff := i.resetCompactContinuationLocked()
367		i.mu.Unlock()
368		cancel()
369		i.confirmDialog.CancelAll("turn cancelled")
370		if persistHandoff {
371			i.persistCompactHandoff(handoff)
372		}
373	}
374}
375func (i *Interactive) Insert(text string) {
376	i.ed.Insert(text)
377	i.invalidate()
378}
379func (i *Interactive) Display(extName, text string) {
380	i.appendExtensionNote(extName, text, "info")
381	i.invalidate()
382}
383func (i *Interactive) ReportError(err error) {
384	if err == nil {
385		return
386	}
387	i.mu.Lock()
388	i.statusOK = ""
389	i.statusErr = err.Error()
390	i.mu.Unlock()
391	i.invalidate()
392}
393func (i *Interactive) SetStatus(extName, key, level, text string) {
394	if strings.TrimSpace(extName) == "" || strings.TrimSpace(key) == "" {
395		return
396	}
397	i.mu.Lock()
398	if i.extStatuses == nil {
399		i.extStatuses = map[string]map[string]extensionStatus{}
400	}
401	if strings.TrimSpace(text) == "" {
402		if items := i.extStatuses[extName]; items != nil {
403			delete(items, key)
404			if len(items) == 0 {
405				delete(i.extStatuses, extName)
406			}
407		}
408	} else {
409		if i.extStatuses[extName] == nil {
410			i.extStatuses[extName] = map[string]extensionStatus{}
411		}
412		i.extStatuses[extName][key] = extensionStatus{Level: level, Text: text}
413	}
414	i.mu.Unlock()
415	i.invalidate()
416}
417func (i *Interactive) SetWidget(extName, id, position, title string, lines []string) {
418	if strings.TrimSpace(extName) == "" || strings.TrimSpace(id) == "" {
419		return
420	}
421	position = extproto.NormalizeWidgetPosition(position)
422	i.mu.Lock()
423	if i.extWidgets == nil {
424		i.extWidgets = map[string]map[string]extensionWidget{}
425	}
426	if i.extWidgets[extName] == nil {
427		i.extWidgets[extName] = map[string]extensionWidget{}
428	}
429	i.extWidgets[extName][id] = extensionWidget{
430		Position: position,
431		Title:    title,
432		Lines:    append([]string(nil), lines...),
433	}
434	i.mu.Unlock()
435	i.invalidate()
436}
437func (i *Interactive) ClearWidget(extName, id string) {
438	i.mu.Lock()
439	if items := i.extWidgets[extName]; items != nil {
440		delete(items, id)
441		if len(items) == 0 {
442			delete(i.extWidgets, extName)
443		}
444	}
445	i.mu.Unlock()
446	i.invalidate()
447}
448func (i *Interactive) ClearExtensionChrome(extName string) {
449	if strings.TrimSpace(extName) == "" {
450		return
451	}
452	marker := "[" + extName + "] "
453	i.mu.Lock()
454	changed := false
455	if _, ok := i.extStatuses[extName]; ok {
456		delete(i.extStatuses, extName)
457		changed = true
458	}
459	if _, ok := i.extWidgets[extName]; ok {
460		delete(i.extWidgets, extName)
461		changed = true
462	}
463	if len(i.extNotes) > 0 {
464		kept := i.extNotes[:0:0]
465		for _, line := range i.extNotes {
466			if strings.Contains(line, marker) {
467				changed = true
468				continue
469			}
470			kept = append(kept, line)
471		}
472		i.extNotes = kept
473	}
474	if i.extPanel != nil && i.extPanel.Active() && i.extPanel.ext == extName {
475		i.extPanel.Close()
476		changed = true
477	}
478	i.mu.Unlock()
479	if changed {
480		i.invalidate()
481	}
482}
483func (i *Interactive) OpenPanel(extName string, spec extproto.PanelSpec) {
484	i.mu.Lock()
485	defer i.mu.Unlock()
486	i.extPanel.Open(extName, spec.ID, spec.Title, spec.Lines, spec.Footer)
487	i.confirmDialog.Blur()
488	if i.cfg.Extensions != nil {
489		cols, rows := i.cfg.Terminal.Size()
490		_ = cols
491		_ = rows
492	}
493	i.invalidate()
494}
495func (i *Interactive) UpdatePanel(extName, panelID, title string, lines []string, footer string) {
496	i.mu.Lock()
497	defer i.mu.Unlock()
498	if i.extPanel.Active() && i.extPanel.ext == extName && i.extPanel.id == panelID {
499		i.extPanel.Update(title, lines, footer)
500		i.invalidate()
501	}
502}
503func (i *Interactive) ClosePanel(extName, panelID string) {
504	i.mu.Lock()
505	defer i.mu.Unlock()
506	if i.extPanel.Active() && i.extPanel.ext == extName && i.extPanel.id == panelID {
507		i.extPanel.Close()
508		i.invalidate()
509	}
510}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 477

↑↓ moveenter openesc close