tui.go

  1package tui
  2
  3import (
  4	"context"
  5	"fmt"
  6	"math/rand"
  7	"regexp"
  8	"slices"
  9	"strings"
 10	"time"
 11
 12	"charm.land/bubbles/v2/key"
 13	tea "charm.land/bubbletea/v2"
 14	"charm.land/lipgloss/v2"
 15	"github.com/charmbracelet/crush/internal/agent/tools/mcp"
 16	"github.com/charmbracelet/crush/internal/app"
 17	"github.com/charmbracelet/crush/internal/config"
 18	"github.com/charmbracelet/crush/internal/event"
 19	"github.com/charmbracelet/crush/internal/permission"
 20	"github.com/charmbracelet/crush/internal/pubsub"
 21	"github.com/charmbracelet/crush/internal/stringext"
 22	cmpChat "github.com/charmbracelet/crush/internal/tui/components/chat"
 23	"github.com/charmbracelet/crush/internal/tui/components/chat/splash"
 24	"github.com/charmbracelet/crush/internal/tui/components/completions"
 25	"github.com/charmbracelet/crush/internal/tui/components/core"
 26	"github.com/charmbracelet/crush/internal/tui/components/core/layout"
 27	"github.com/charmbracelet/crush/internal/tui/components/core/status"
 28	"github.com/charmbracelet/crush/internal/tui/components/dialogs"
 29	"github.com/charmbracelet/crush/internal/tui/components/dialogs/commands"
 30	"github.com/charmbracelet/crush/internal/tui/components/dialogs/filepicker"
 31	"github.com/charmbracelet/crush/internal/tui/components/dialogs/models"
 32	"github.com/charmbracelet/crush/internal/tui/components/dialogs/permissions"
 33	"github.com/charmbracelet/crush/internal/tui/components/dialogs/quit"
 34	"github.com/charmbracelet/crush/internal/tui/components/dialogs/sessions"
 35	"github.com/charmbracelet/crush/internal/tui/page"
 36	"github.com/charmbracelet/crush/internal/tui/page/chat"
 37	"github.com/charmbracelet/crush/internal/tui/styles"
 38	"github.com/charmbracelet/crush/internal/tui/util"
 39	"golang.org/x/mod/semver"
 40	"golang.org/x/text/cases"
 41	"golang.org/x/text/language"
 42)
 43
 44var lastMouseEvent time.Time
 45
 46func MouseEventFilter(m tea.Model, msg tea.Msg) tea.Msg {
 47	switch msg.(type) {
 48	case tea.MouseWheelMsg, tea.MouseMotionMsg:
 49		now := time.Now()
 50		// trackpad is sending too many requests
 51		if now.Sub(lastMouseEvent) < 15*time.Millisecond {
 52			return nil
 53		}
 54		lastMouseEvent = now
 55	}
 56	return msg
 57}
 58
 59// appModel represents the main application model that manages pages, dialogs, and UI state.
 60type appModel struct {
 61	wWidth, wHeight int // Window dimensions
 62	width, height   int
 63	keyMap          KeyMap
 64
 65	currentPage  page.PageID
 66	previousPage page.PageID
 67	pages        map[page.PageID]util.Model
 68	loadedPages  map[page.PageID]bool
 69
 70	// Status
 71	status          status.StatusCmp
 72	showingFullHelp bool
 73
 74	app *app.App
 75
 76	dialog       dialogs.DialogCmp
 77	completions  completions.Completions
 78	isConfigured bool
 79
 80	// Chat Page Specific
 81	selectedSessionID string // The ID of the currently selected session
 82
 83	// sendProgressBar instructs the TUI to send progress bar updates to the
 84	// terminal.
 85	sendProgressBar bool
 86
 87	// QueryVersion instructs the TUI to query for the terminal version when it
 88	// starts.
 89	QueryVersion bool
 90}
 91
 92// Init initializes the application model and returns initial commands.
 93func (a appModel) Init() tea.Cmd {
 94	item, ok := a.pages[a.currentPage]
 95	if !ok {
 96		return nil
 97	}
 98
 99	var cmds []tea.Cmd
100	cmd := item.Init()
101	cmds = append(cmds, cmd)
102	a.loadedPages[a.currentPage] = true
103
104	cmd = a.status.Init()
105	cmds = append(cmds, cmd)
106	if a.QueryVersion {
107		cmds = append(cmds, tea.RequestTerminalVersion)
108	}
109
110	return tea.Batch(cmds...)
111}
112
113// Update handles incoming messages and updates the application state.
114func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
115	var cmds []tea.Cmd
116	var cmd tea.Cmd
117	a.isConfigured = config.HasInitialDataConfig()
118
119	switch msg := msg.(type) {
120	case tea.EnvMsg:
121		// Is this Windows Terminal?
122		if !a.sendProgressBar {
123			a.sendProgressBar = slices.Contains(msg, "WT_SESSION")
124		}
125	case tea.TerminalVersionMsg:
126		if a.sendProgressBar {
127			return a, nil
128		}
129		termVersion := strings.ToLower(msg.Name)
130		switch {
131		case stringext.ContainsAny(termVersion, "ghostty", "rio"):
132			a.sendProgressBar = true
133		case strings.Contains(termVersion, "iterm2"):
134			// iTerm2 supports progress bars from version v3.6.6
135			matches := regexp.MustCompile(`^iterm2 (\d+\.\d+\.\d+)$`).FindStringSubmatch(termVersion)
136			if len(matches) == 2 && semver.Compare("v"+matches[1], "v3.6.6") >= 0 {
137				a.sendProgressBar = true
138			}
139		}
140		return a, nil
141	case tea.KeyboardEnhancementsMsg:
142		// A non-zero value means we have key disambiguation support.
143		if msg.Flags > 0 {
144			a.keyMap.Models.SetHelp("ctrl+m", "models")
145		}
146		for id, page := range a.pages {
147			m, pageCmd := page.Update(msg)
148			a.pages[id] = m
149
150			if pageCmd != nil {
151				cmds = append(cmds, pageCmd)
152			}
153		}
154		return a, tea.Batch(cmds...)
155	case tea.WindowSizeMsg:
156		a.wWidth, a.wHeight = msg.Width, msg.Height
157		a.completions.Update(msg)
158		return a, a.handleWindowResize(msg.Width, msg.Height)
159
160	case pubsub.Event[mcp.Event]:
161		switch msg.Payload.Type {
162		case mcp.EventStateChanged:
163			return a, a.handleStateChanged(context.Background())
164		case mcp.EventPromptsListChanged:
165			return a, handleMCPPromptsEvent(context.Background(), msg.Payload.Name)
166		case mcp.EventToolsListChanged:
167			return a, handleMCPToolsEvent(context.Background(), msg.Payload.Name)
168		}
169
170	// Completions messages
171	case completions.OpenCompletionsMsg, completions.FilterCompletionsMsg,
172		completions.CloseCompletionsMsg, completions.RepositionCompletionsMsg:
173		u, completionCmd := a.completions.Update(msg)
174		if model, ok := u.(completions.Completions); ok {
175			a.completions = model
176		}
177
178		return a, completionCmd
179
180	// Dialog messages
181	case dialogs.OpenDialogMsg, dialogs.CloseDialogMsg:
182		u, completionCmd := a.completions.Update(completions.CloseCompletionsMsg{})
183		a.completions = u.(completions.Completions)
184		u, dialogCmd := a.dialog.Update(msg)
185		a.dialog = u.(dialogs.DialogCmp)
186		return a, tea.Batch(completionCmd, dialogCmd)
187	case commands.ShowArgumentsDialogMsg:
188		var args []commands.Argument
189		for _, arg := range msg.ArgNames {
190			args = append(args, commands.Argument{
191				Name:     arg,
192				Title:    cases.Title(language.English).String(arg),
193				Required: true,
194			})
195		}
196		return a, util.CmdHandler(
197			dialogs.OpenDialogMsg{
198				Model: commands.NewCommandArgumentsDialog(
199					msg.CommandID,
200					msg.CommandID,
201					msg.CommandID,
202					msg.Description,
203					args,
204					msg.OnSubmit,
205				),
206			},
207		)
208	case commands.ShowMCPPromptArgumentsDialogMsg:
209		args := make([]commands.Argument, 0, len(msg.Prompt.Arguments))
210		for _, arg := range msg.Prompt.Arguments {
211			args = append(args, commands.Argument(*arg))
212		}
213		dialog := commands.NewCommandArgumentsDialog(
214			msg.Prompt.Name,
215			msg.Prompt.Title,
216			msg.Prompt.Name,
217			msg.Prompt.Description,
218			args,
219			msg.OnSubmit,
220		)
221		return a, util.CmdHandler(
222			dialogs.OpenDialogMsg{
223				Model: dialog,
224			},
225		)
226	// Page change messages
227	case page.PageChangeMsg:
228		return a, a.moveToPage(msg.ID)
229
230	// Status Messages
231	case util.InfoMsg, util.ClearStatusMsg:
232		s, statusCmd := a.status.Update(msg)
233		a.status = s.(status.StatusCmp)
234		cmds = append(cmds, statusCmd)
235		return a, tea.Batch(cmds...)
236
237	// Session
238	case cmpChat.SessionSelectedMsg:
239		a.selectedSessionID = msg.ID
240	case cmpChat.SessionClearedMsg:
241		a.selectedSessionID = ""
242	// Commands
243	case commands.SwitchSessionsMsg:
244		return a, func() tea.Msg {
245			allSessions, _ := a.app.Sessions.List(context.Background())
246			return dialogs.OpenDialogMsg{
247				Model: sessions.NewSessionDialogCmp(allSessions, a.selectedSessionID),
248			}
249		}
250
251	case commands.SwitchModelMsg:
252		return a, util.CmdHandler(
253			dialogs.OpenDialogMsg{
254				Model: models.NewModelDialogCmp(),
255			},
256		)
257	// Compact
258	case commands.CompactMsg:
259		return a, func() tea.Msg {
260			err := a.app.AgentCoordinator.Summarize(context.Background(), msg.SessionID)
261			if err != nil {
262				return util.ReportError(err)()
263			}
264			return nil
265		}
266	case commands.QuitMsg:
267		return a, util.CmdHandler(dialogs.OpenDialogMsg{
268			Model: quit.NewQuitDialog(),
269		})
270	case commands.ToggleYoloModeMsg:
271		a.app.Permissions.SetSkipRequests(!a.app.Permissions.SkipRequests())
272	case commands.ToggleHelpMsg:
273		a.status.ToggleFullHelp()
274		a.showingFullHelp = !a.showingFullHelp
275		return a, a.handleWindowResize(a.wWidth, a.wHeight)
276	// Model Switch
277	case models.ModelSelectedMsg:
278		if a.app.AgentCoordinator.IsBusy() {
279			return a, util.ReportWarn("Agent is busy, please wait...")
280		}
281
282		cfg := config.Get()
283		if err := cfg.UpdatePreferredModel(msg.ModelType, msg.Model); err != nil {
284			return a, util.ReportError(err)
285		}
286
287		go a.app.UpdateAgentModel(context.TODO())
288
289		modelTypeName := "large"
290		if msg.ModelType == config.SelectedModelTypeSmall {
291			modelTypeName = "small"
292		}
293		return a, util.ReportInfo(fmt.Sprintf("%s model changed to %s", modelTypeName, msg.Model.Model))
294
295	// File Picker
296	case commands.OpenFilePickerMsg:
297		event.FilePickerOpened()
298
299		if a.dialog.ActiveDialogID() == filepicker.FilePickerID {
300			// If the commands dialog is already open, close it
301			return a, util.CmdHandler(dialogs.CloseDialogMsg{})
302		}
303		return a, util.CmdHandler(dialogs.OpenDialogMsg{
304			Model: filepicker.NewFilePickerCmp(a.app.Config().WorkingDir()),
305		})
306	// Permissions
307	case pubsub.Event[permission.PermissionNotification]:
308		item, ok := a.pages[a.currentPage]
309		if !ok {
310			return a, nil
311		}
312
313		// Forward to view.
314		updated, itemCmd := item.Update(msg)
315		a.pages[a.currentPage] = updated
316
317		return a, itemCmd
318	case pubsub.Event[permission.PermissionRequest]:
319		return a, util.CmdHandler(dialogs.OpenDialogMsg{
320			Model: permissions.NewPermissionDialogCmp(msg.Payload, &permissions.Options{
321				DiffMode: config.Get().Options.TUI.DiffMode,
322			}),
323		})
324	case permissions.PermissionResponseMsg:
325		switch msg.Action {
326		case permissions.PermissionAllow:
327			// TODO: get the context from somewhere
328			a.app.Permissions.Grant(context.Background(), msg.Permission)
329		case permissions.PermissionAllowForSession:
330			// TODO: get the context from somewhere
331			a.app.Permissions.GrantPersistent(context.Background(), msg.Permission)
332		case permissions.PermissionDeny:
333			// TODO: get the context from somewhere
334			a.app.Permissions.Deny(context.Background(), msg.Permission)
335		}
336		return a, nil
337	case splash.OnboardingCompleteMsg:
338		item, ok := a.pages[a.currentPage]
339		if !ok {
340			return a, nil
341		}
342
343		a.isConfigured = config.HasInitialDataConfig()
344		updated, pageCmd := item.Update(msg)
345		a.pages[a.currentPage] = updated
346
347		cmds = append(cmds, pageCmd)
348		return a, tea.Batch(cmds...)
349
350	case tea.KeyPressMsg:
351		return a, a.handleKeyPressMsg(msg)
352
353	case tea.MouseWheelMsg:
354		if a.dialog.HasDialogs() {
355			u, dialogCmd := a.dialog.Update(msg)
356			a.dialog = u.(dialogs.DialogCmp)
357			cmds = append(cmds, dialogCmd)
358		} else {
359			item, ok := a.pages[a.currentPage]
360			if !ok {
361				return a, nil
362			}
363
364			updated, pageCmd := item.Update(msg)
365			a.pages[a.currentPage] = updated
366
367			cmds = append(cmds, pageCmd)
368		}
369		return a, tea.Batch(cmds...)
370	case tea.PasteMsg:
371		if a.dialog.HasDialogs() {
372			u, dialogCmd := a.dialog.Update(msg)
373			if model, ok := u.(dialogs.DialogCmp); ok {
374				a.dialog = model
375			}
376
377			cmds = append(cmds, dialogCmd)
378		} else {
379			item, ok := a.pages[a.currentPage]
380			if !ok {
381				return a, nil
382			}
383
384			updated, pageCmd := item.Update(msg)
385			a.pages[a.currentPage] = updated
386
387			cmds = append(cmds, pageCmd)
388		}
389		return a, tea.Batch(cmds...)
390	// Update Available
391	case pubsub.UpdateAvailableMsg:
392		// Show update notification in status bar
393		statusMsg := fmt.Sprintf("Crush update available: v%s → v%s.", msg.CurrentVersion, msg.LatestVersion)
394		if msg.IsDevelopment {
395			statusMsg = fmt.Sprintf("This is a development version of Crush. The latest version is v%s.", msg.LatestVersion)
396		}
397		s, statusCmd := a.status.Update(util.InfoMsg{
398			Type: util.InfoTypeUpdate,
399			Msg:  statusMsg,
400			TTL:  10 * time.Second,
401		})
402		a.status = s.(status.StatusCmp)
403		return a, statusCmd
404	}
405	s, _ := a.status.Update(msg)
406	a.status = s.(status.StatusCmp)
407
408	item, ok := a.pages[a.currentPage]
409	if !ok {
410		return a, nil
411	}
412
413	updated, cmd := item.Update(msg)
414	a.pages[a.currentPage] = updated
415
416	if a.dialog.HasDialogs() {
417		u, dialogCmd := a.dialog.Update(msg)
418		if model, ok := u.(dialogs.DialogCmp); ok {
419			a.dialog = model
420		}
421
422		cmds = append(cmds, dialogCmd)
423	}
424	cmds = append(cmds, cmd)
425	return a, tea.Batch(cmds...)
426}
427
428// handleWindowResize processes window resize events and updates all components.
429func (a *appModel) handleWindowResize(width, height int) tea.Cmd {
430	var cmds []tea.Cmd
431
432	// TODO: clean up these magic numbers.
433	if a.showingFullHelp {
434		height -= 5
435	} else {
436		height -= 2
437	}
438
439	a.width, a.height = width, height
440	// Update status bar
441	s, cmd := a.status.Update(tea.WindowSizeMsg{Width: width, Height: height})
442	if model, ok := s.(status.StatusCmp); ok {
443		a.status = model
444	}
445	cmds = append(cmds, cmd)
446
447	// Update the current view.
448	for p, page := range a.pages {
449		updated, pageCmd := page.Update(tea.WindowSizeMsg{Width: width, Height: height})
450		a.pages[p] = updated
451
452		cmds = append(cmds, pageCmd)
453	}
454
455	// Update the dialogs
456	dialog, cmd := a.dialog.Update(tea.WindowSizeMsg{Width: width, Height: height})
457	if model, ok := dialog.(dialogs.DialogCmp); ok {
458		a.dialog = model
459	}
460
461	cmds = append(cmds, cmd)
462
463	return tea.Batch(cmds...)
464}
465
466// handleKeyPressMsg processes keyboard input and routes to appropriate handlers.
467func (a *appModel) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd {
468	// Check this first as the user should be able to quit no matter what.
469	if key.Matches(msg, a.keyMap.Quit) {
470		if a.dialog.ActiveDialogID() == quit.QuitDialogID {
471			return tea.Quit
472		}
473		return util.CmdHandler(dialogs.OpenDialogMsg{
474			Model: quit.NewQuitDialog(),
475		})
476	}
477
478	if a.completions.Open() {
479		// completions
480		keyMap := a.completions.KeyMap()
481		switch {
482		case key.Matches(msg, keyMap.Up), key.Matches(msg, keyMap.Down),
483			key.Matches(msg, keyMap.Select), key.Matches(msg, keyMap.Cancel),
484			key.Matches(msg, keyMap.UpInsert), key.Matches(msg, keyMap.DownInsert):
485			u, cmd := a.completions.Update(msg)
486			a.completions = u.(completions.Completions)
487			return cmd
488		}
489	}
490	if a.dialog.HasDialogs() {
491		u, dialogCmd := a.dialog.Update(msg)
492		a.dialog = u.(dialogs.DialogCmp)
493		return dialogCmd
494	}
495	switch {
496	// help
497	case key.Matches(msg, a.keyMap.Help):
498		a.status.ToggleFullHelp()
499		a.showingFullHelp = !a.showingFullHelp
500		return a.handleWindowResize(a.wWidth, a.wHeight)
501	// dialogs
502	case key.Matches(msg, a.keyMap.Commands):
503		// if the app is not configured show no commands
504		if !a.isConfigured {
505			return nil
506		}
507		if a.dialog.ActiveDialogID() == commands.CommandsDialogID {
508			return util.CmdHandler(dialogs.CloseDialogMsg{})
509		}
510		if a.dialog.HasDialogs() {
511			return nil
512		}
513		return util.CmdHandler(dialogs.OpenDialogMsg{
514			Model: commands.NewCommandDialog(a.selectedSessionID),
515		})
516	case key.Matches(msg, a.keyMap.Models):
517		// if the app is not configured show no models
518		if !a.isConfigured {
519			return nil
520		}
521		if a.dialog.ActiveDialogID() == models.ModelsDialogID {
522			return util.CmdHandler(dialogs.CloseDialogMsg{})
523		}
524		if a.dialog.HasDialogs() {
525			return nil
526		}
527		return util.CmdHandler(dialogs.OpenDialogMsg{
528			Model: models.NewModelDialogCmp(),
529		})
530	case key.Matches(msg, a.keyMap.Sessions):
531		// if the app is not configured show no sessions
532		if !a.isConfigured {
533			return nil
534		}
535		if a.dialog.ActiveDialogID() == sessions.SessionsDialogID {
536			return util.CmdHandler(dialogs.CloseDialogMsg{})
537		}
538		if a.dialog.HasDialogs() && a.dialog.ActiveDialogID() != commands.CommandsDialogID {
539			return nil
540		}
541		var cmds []tea.Cmd
542		cmds = append(cmds,
543			func() tea.Msg {
544				allSessions, _ := a.app.Sessions.List(context.Background())
545				return dialogs.OpenDialogMsg{
546					Model: sessions.NewSessionDialogCmp(allSessions, a.selectedSessionID),
547				}
548			},
549		)
550		return tea.Sequence(cmds...)
551	case key.Matches(msg, a.keyMap.Suspend):
552		if a.app.AgentCoordinator != nil && a.app.AgentCoordinator.IsBusy() {
553			return util.ReportWarn("Agent is busy, please wait...")
554		}
555		return tea.Suspend
556	default:
557		item, ok := a.pages[a.currentPage]
558		if !ok {
559			return nil
560		}
561
562		updated, cmd := item.Update(msg)
563		a.pages[a.currentPage] = updated
564		return cmd
565	}
566}
567
568// moveToPage handles navigation between different pages in the application.
569func (a *appModel) moveToPage(pageID page.PageID) tea.Cmd {
570	if a.app.AgentCoordinator.IsBusy() {
571		// TODO: maybe remove this :  For now we don't move to any page if the agent is busy
572		return util.ReportWarn("Agent is busy, please wait...")
573	}
574
575	var cmds []tea.Cmd
576	if _, ok := a.loadedPages[pageID]; !ok {
577		cmd := a.pages[pageID].Init()
578		cmds = append(cmds, cmd)
579		a.loadedPages[pageID] = true
580	}
581	a.previousPage = a.currentPage
582	a.currentPage = pageID
583	if sizable, ok := a.pages[a.currentPage].(layout.Sizeable); ok {
584		cmd := sizable.SetSize(a.width, a.height)
585		cmds = append(cmds, cmd)
586	}
587
588	return tea.Batch(cmds...)
589}
590
591// View renders the complete application interface including pages, dialogs, and overlays.
592func (a *appModel) View() tea.View {
593	var view tea.View
594	t := styles.CurrentTheme()
595	view.AltScreen = true
596	view.MouseMode = tea.MouseModeCellMotion
597	view.BackgroundColor = t.BgBase
598	if a.wWidth < 25 || a.wHeight < 15 {
599		view.Content = t.S().Base.Width(a.wWidth).Height(a.wHeight).
600			Align(lipgloss.Center, lipgloss.Center).
601			Render(t.S().Base.
602				Padding(1, 4).
603				Foreground(t.White).
604				BorderStyle(lipgloss.RoundedBorder()).
605				BorderForeground(t.Primary).
606				Render("Window too small!"),
607			)
608		return view
609	}
610
611	page := a.pages[a.currentPage]
612	if withHelp, ok := page.(core.KeyMapHelp); ok {
613		a.status.SetKeyMap(withHelp.Help())
614	}
615	pageView := page.View()
616	components := []string{
617		pageView,
618	}
619	components = append(components, a.status.View())
620
621	appView := lipgloss.JoinVertical(lipgloss.Top, components...)
622	layers := []*lipgloss.Layer{
623		lipgloss.NewLayer(appView),
624	}
625	if a.dialog.HasDialogs() {
626		layers = append(
627			layers,
628			a.dialog.GetLayers()...,
629		)
630	}
631
632	var cursor *tea.Cursor
633	if v, ok := page.(util.Cursor); ok {
634		cursor = v.Cursor()
635		// Hide the cursor if it's positioned outside the textarea
636		statusHeight := a.height - strings.Count(pageView, "\n") + 1
637		if cursor != nil && cursor.Y+statusHeight+chat.EditorHeight-2 <= a.height { // 2 for the top and bottom app padding
638			cursor = nil
639		}
640	}
641	activeView := a.dialog.ActiveModel()
642	if activeView != nil {
643		cursor = nil // Reset cursor if a dialog is active unless it implements util.Cursor
644		if v, ok := activeView.(util.Cursor); ok {
645			cursor = v.Cursor()
646		}
647	}
648
649	if a.completions.Open() && cursor != nil {
650		cmp := a.completions.View()
651		x, y := a.completions.Position()
652		layers = append(
653			layers,
654			lipgloss.NewLayer(cmp).X(x).Y(y),
655		)
656	}
657
658	comp := lipgloss.NewCompositor(layers...)
659	view.Content = comp.Render()
660	view.Cursor = cursor
661
662	if a.sendProgressBar && a.app != nil && a.app.AgentCoordinator != nil && a.app.AgentCoordinator.IsBusy() {
663		// HACK: use a random percentage to prevent ghostty from hiding it
664		// after a timeout.
665		view.ProgressBar = tea.NewProgressBar(tea.ProgressBarIndeterminate, rand.Intn(100))
666	}
667	return view
668}
669
670func (a *appModel) handleStateChanged(ctx context.Context) tea.Cmd {
671	return func() tea.Msg {
672		a.app.UpdateAgentModel(ctx)
673		return nil
674	}
675}
676
677func handleMCPPromptsEvent(ctx context.Context, name string) tea.Cmd {
678	return func() tea.Msg {
679		mcp.RefreshPrompts(ctx, name)
680		return nil
681	}
682}
683
684func handleMCPToolsEvent(ctx context.Context, name string) tea.Cmd {
685	return func() tea.Msg {
686		mcp.RefreshTools(ctx, name)
687		return nil
688	}
689}
690
691// New creates and initializes a new TUI application model.
692func New(app *app.App) *appModel {
693	chatPage := chat.New(app)
694	keyMap := DefaultKeyMap()
695	keyMap.pageBindings = chatPage.Bindings()
696
697	model := &appModel{
698		currentPage: chat.ChatPageID,
699		app:         app,
700		status:      status.NewStatusCmp(),
701		loadedPages: make(map[page.PageID]bool),
702		keyMap:      keyMap,
703
704		pages: map[page.PageID]util.Model{
705			chat.ChatPageID: chatPage,
706		},
707
708		dialog:      dialogs.NewDialogCmp(),
709		completions: completions.New(),
710	}
711
712	return model
713}