v0.44.0 efd47b3

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.stream.Reset()
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.stream.Reset()
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
327func (i *Interactive) takeQueuedMessagesLocked() []core.QueuedMessage {
328	var pending []core.QueuedMessage
329	if i.agent != nil {
330		pending = i.agent.DrainQueuedMessages()
331	}
332	pending = append(pending, i.queued...)
333	i.queued = nil
334	return pending
335}
336
337func (i *Interactive) discardQueuedMessagesLocked(preserveHostEvents bool) {
338	pending := i.takeQueuedMessagesLocked()
339	if !preserveHostEvents {
340		return
341	}
342	for _, message := range pending {
343		if !message.HostEvent {
344			continue
345		}
346		if i.agent != nil {
347			i.agent.QueuePrompt(message)
348		} else {
349			i.queued = append(i.queued, message)
350		}
351	}
352}
353
354func (i *Interactive) ChangelogVersion() string {
355	if i.changelogDialog != nil {
356		return i.changelogDialog.version
357	}
358	return ""
359}
360func (i *Interactive) CancelTurn() {
361	i.mu.Lock()
362	cancel := i.cancelTurn
363	i.mu.Unlock()
364	if cancel != nil {
365		i.cancelCoordinator()
366
367		i.mu.Lock()
368		handoff, persistHandoff := i.resetCompactContinuationLocked()
369		i.mu.Unlock()
370		cancel()
371		i.confirmDialog.CancelAll("turn cancelled")
372		if persistHandoff {
373			i.persistCompactHandoff(handoff)
374		}
375	}
376}
377func (i *Interactive) Insert(text string) {
378	i.ed.Insert(text)
379	i.invalidate()
380}
381func (i *Interactive) Display(extName, text string) {
382	i.appendExtensionNote(extName, text, "info")
383	i.invalidate()
384}
385func (i *Interactive) ReportError(err error) {
386	if err == nil {
387		return
388	}
389	i.mu.Lock()
390	i.statusOK = ""
391	i.statusErr = err.Error()
392	i.mu.Unlock()
393	i.invalidate()
394}
395func (i *Interactive) SetStatus(extName, key, level, text string) {
396	if strings.TrimSpace(extName) == "" || strings.TrimSpace(key) == "" {
397		return
398	}
399	i.mu.Lock()
400	if i.extStatuses == nil {
401		i.extStatuses = map[string]map[string]extensionStatus{}
402	}
403	if strings.TrimSpace(text) == "" {
404		if items := i.extStatuses[extName]; items != nil {
405			delete(items, key)
406			if len(items) == 0 {
407				delete(i.extStatuses, extName)
408			}
409		}
410	} else {
411		if i.extStatuses[extName] == nil {
412			i.extStatuses[extName] = map[string]extensionStatus{}
413		}
414		i.extStatuses[extName][key] = extensionStatus{Level: level, Text: text}
415	}
416	i.mu.Unlock()
417	i.invalidate()
418}
419func (i *Interactive) SetWidget(extName, id, position, title string, lines []string) {
420	if strings.TrimSpace(extName) == "" || strings.TrimSpace(id) == "" {
421		return
422	}
423	position = extproto.NormalizeWidgetPosition(position)
424	i.mu.Lock()
425	if i.extWidgets == nil {
426		i.extWidgets = map[string]map[string]extensionWidget{}
427	}
428	if i.extWidgets[extName] == nil {
429		i.extWidgets[extName] = map[string]extensionWidget{}
430	}
431	i.extWidgets[extName][id] = extensionWidget{
432		Position: position,
433		Title:    title,
434		Lines:    append([]string(nil), lines...),
435	}
436	i.mu.Unlock()
437	i.invalidate()
438}
439func (i *Interactive) ClearWidget(extName, id string) {
440	i.mu.Lock()
441	if items := i.extWidgets[extName]; items != nil {
442		delete(items, id)
443		if len(items) == 0 {
444			delete(i.extWidgets, extName)
445		}
446	}
447	i.mu.Unlock()
448	i.invalidate()
449}
450func (i *Interactive) ClearExtensionChrome(extName string) {
451	if strings.TrimSpace(extName) == "" {
452		return
453	}
454	marker := "[" + extName + "] "
455	i.mu.Lock()
456	changed := false
457	if _, ok := i.extStatuses[extName]; ok {
458		delete(i.extStatuses, extName)
459		changed = true
460	}
461	if _, ok := i.extWidgets[extName]; ok {
462		delete(i.extWidgets, extName)
463		changed = true
464	}
465	if len(i.extNotes) > 0 {
466		kept := i.extNotes[:0:0]
467		for _, line := range i.extNotes {
468			if strings.Contains(line, marker) {
469				changed = true
470				continue
471			}
472			kept = append(kept, line)
473		}
474		i.extNotes = kept
475	}
476	if i.extPanel != nil && i.extPanel.Active() && i.extPanel.ext == extName {
477		i.extPanel.Close()
478		changed = true
479	}
480	i.mu.Unlock()
481	if changed {
482		i.invalidate()
483	}
484}
485func (i *Interactive) OpenPanel(extName string, spec extproto.PanelSpec) {
486	i.mu.Lock()
487	defer i.mu.Unlock()
488	i.extPanel.Open(extName, spec.ID, spec.Title, spec.Lines, spec.Footer)
489	i.confirmDialog.Blur()
490	if i.cfg.Extensions != nil {
491		cols, rows := i.cfg.Terminal.Size()
492		_ = cols
493		_ = rows
494	}
495	i.invalidate()
496}
497func (i *Interactive) UpdatePanel(extName, panelID, title string, lines []string, footer string) {
498	i.mu.Lock()
499	defer i.mu.Unlock()
500	if i.extPanel.Active() && i.extPanel.ext == extName && i.extPanel.id == panelID {
501		i.extPanel.Update(title, lines, footer)
502		i.invalidate()
503	}
504}
505func (i *Interactive) ClosePanel(extName, panelID string) {
506	i.mu.Lock()
507	defer i.mu.Unlock()
508	if i.extPanel.Active() && i.extPanel.ext == extName && i.extPanel.id == panelID {
509		i.extPanel.Close()
510		i.invalidate()
511	}
512}

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