Files
correx/apps/tui-go/internal/app/model.go
T
kami b563d9841d feat(tui-go): workflow-propose choice panel
Render a workflow.proposed frame as a single-select picker with a manual-answer
slot: ↑↓/jk to choose, enter to launch, e for custom, esc to peek away. Picking
a candidate sends StartSession (and focuses the launched session); the custom
slot continues the conversation via ChatInput. Reuses the modal helpers from the
clarification view.
2026-06-14 13:29:34 +04:00

366 lines
9.7 KiB
Go

package app
import (
"github.com/correx/tui-go/internal/protocol"
"github.com/correx/tui-go/internal/ws"
)
// DisplayState is the top-level screen the UI renders.
type DisplayState int
const (
StateIdle DisplayState = iota
StateInSession
StateApproval
StateClarification
StateWorkflowPropose
)
// InputMode toggles what a submitted line targets.
type InputMode int
const (
ModeRouter InputMode = iota // chat / steering input
ModeFilter // session-list filter
ModeIntent // freeform request for a workflow being started
)
// EditMode is the vim-style modality: Normal = bare-key commands, Insert = typing.
type EditMode int
const (
ModeNormal EditMode = iota
ModeInsert
)
// ChatMode selects how a turn is sent to the router.
const (
ChatModeChat = "CHAT"
ChatModeSteering = "STEERING"
)
// OverlayKind is the active modal (immediate-mode: drawn on top when set).
type OverlayKind int
const (
OverlayNone OverlayKind = iota
OverlayPalette
OverlayEventInspector
OverlayDiff
OverlayToolPalette
OverlayModels
OverlayArtifacts
OverlayConfig
OverlayStats
)
// RouterEntry is one line in a session's conversation transcript.
type RouterEntry struct {
Role string // user | router | tool | narration | narration_llm
Content string
Metrics *TurnMetrics
}
// TurnMetrics carries optional latency + token cost for a ROUTER chat turn.
type TurnMetrics struct {
LatencyMs int64
TotalTokens int
}
// EventEntry is a row in the event stream.
type EventEntry struct {
Time string
Type string
Detail string
}
// ToolStatus mirrors the Kotlin ToolDisplayStatus.
type ToolStatus int
const (
ToolStarted ToolStatus = iota
ToolCompleted
ToolFailed
ToolRejected
)
type ToolRecord struct {
Name string
Tier int
Status ToolStatus
}
// ManifestTool is a declared (not-yet-run) tool from a stage manifest.
type ManifestTool struct {
Name string
Tier int
}
// Approval is a pending approval gate for a session.
type Approval struct {
RequestID string
SessionID string
Tier string
Risk string
ToolName string
Preview string
// Rationale holds the plane-2 verified preconditions ("[PATH_OUTSIDE_WORKSPACE] …")
// that justify the gate — shown in the approval band instead of an opaque tier.
Rationale []string
}
// ClarQuestion is one open question a stage raised (mirrors the wire DTO).
type ClarQuestion struct {
ID string
Prompt string
Header string
Options []string
MultiSelect bool
}
// Clarification is a stage's pending set of open questions for the operator.
type Clarification struct {
RequestID string
SessionID string
StageID string
Questions []ClarQuestion
}
// ProposeCandidate is one workflow the router suggests (mirrors the wire DTO).
type ProposeCandidate struct {
WorkflowID string
Reason string
}
// Proposal is the router's triage suggestion of candidate workflows for a chat
// request, rendered as a choice panel with a manual-answer slot. Picking a candidate
// launches that workflow; the custom slot continues the conversation.
type Proposal struct {
ProposalID string
SessionID string
Prompt string
OriginalRequest string
Candidates []ProposeCandidate
}
// Session is the UI's view of one server session.
type Session struct {
ID string
Status string
WorkflowID string
Name string
LastEventAt int64
CurrentStage string
WorkspaceRoot string // bound cwd, from session.workspace_bound
LastOutput string
LastResponse string
Tools []ToolRecord
ToolsByStage map[string][]ManifestTool
Events []EventEntry
Pending *Approval
Clar *Clarification // open questions awaiting answers (clarification view)
Propose *Proposal // router workflow suggestion awaiting a pick (propose view)
Active bool // an inference/tool is in flight (drives the spinner)
}
// Workflow is a launchable workflow advertised by the server.
type Workflow struct {
ID string
Description string
}
// Model is the whole TUI state. View is a pure function of it (immediate-mode),
// which is what keeps overlays from desyncing.
type Model struct {
width, height int
client *ws.Client
theme Theme
quitting bool
// connection
connected bool
reconnecting bool
// sessions
sessions []Session
selectedID string
filter string
workflows []Workflow
wfIndex int // -1 = not in workflow picker
wfVisible bool
wfPendingID string // workflow chosen, awaiting an intent line before StartSession
wfPendingName string
bgUpdates int
// input
editMode EditMode
inputMode InputMode
inputBuffer string
inputCursor int
history map[string][]string
historyIndex int
savedBuffer string
// flow flags
sessionEntered bool
approvalDismissed bool
pendingWorkflowFocus bool
// router transcript
routerMessages map[string][]RouterEntry
routerConnected bool
chatMode string
// provider
currentModel string
providerType string // LOCAL | REMOTE
// managed-model swap (nil resource fields = unavailable)
availableModels []string
modelsIndex int
gpuUsedMB *int64
gpuTotalMB *int64
gpuUtil *int
ramMB *int64
sysRamUsedMB *int64
sysRamTotalMB *int64
// diff / overlay
overlay OverlayKind
overlayEventIdx int
diffScrollOffset int
eventStripShown bool
// artifact viewer (OverlayArtifacts) — populated by the artifact.list response
artifacts []protocol.ArtifactDto
artifactsFor string // sessionId the current listing belongs to
artifactsIndex int
artifactScroll int
artifactsLoading bool
// config editor (OverlayConfig) — populated by the config.snapshot response
configFields []protocol.ConfigFieldDto
configIndex int
configStaged map[string]string // key -> edited value, pending save
configEditing bool // true while typing a value into configEditBuf
configEditBuf string
configError string
configRestart []string // keys from the last save that need a restart
configLoading bool
// session stats (OverlayStats) — populated by the session.stats reply
stats *protocol.StatsDto
statsFor string // sessionId the current stats belong to
statsLoading bool
// command palette
paletteFilter string
paletteIndex int
// animation
frame int // tick counter; drives spinner + caret blink
// snapshot phase
snapshotPhase bool
pendingEvents []protocol.ServerMessage
// approval steering input buffer
steerBuffer string
steering bool
// clarification view (the interactive question form)
clarFocus int // focused question index
clarCursor int // option cursor in the focused question (== len(opts) → custom slot)
clarChosen []map[int]bool // per-question selected option indices
clarText []string // per-question free-text / custom answer
clarTyping bool // typing into the custom buffer for the focused question
clarDismissed bool // peeked away from the form (it can be reopened)
// workflow-propose view (the router's candidate-workflow picker)
proposeCursor int // cursor over candidates (== len(candidates) → custom slot)
proposeText string // free-text answer typed into the custom slot
proposeTyping bool // typing into the custom buffer
proposeDismissed bool // peeked away from the picker (it can be reopened)
}
// NewModel builds the initial idle state.
func NewModel(client *ws.Client) Model {
return Model{
client: client,
theme: NewTheme(SoftBlue),
wfIndex: -1,
inputMode: ModeRouter,
history: map[string][]string{},
historyIndex: -1,
routerMessages: map[string][]RouterEntry{},
configStaged: map[string]string{},
chatMode: ChatModeChat,
providerType: "LOCAL",
snapshotPhase: true,
eventStripShown: true,
}
}
// displayState derives the active screen. A session must be *entered*
// (sessionEntered) before its in-session or approval surfaces show — otherwise
// merely moving the list cursor onto a session with a pending gate would yank
// you into the approval, and `l` back-to-list couldn't escape it.
func (m Model) displayState() DisplayState {
if m.selectedID == "" || !m.sessionEntered {
return StateIdle
}
s := m.session(m.selectedID)
// A stage's open questions take precedence: the run is parked on them, and they
// must be answered (or peeked away) before anything else makes sense.
if s != nil && s.Clar != nil && !m.clarDismissed {
return StateClarification
}
// A router workflow proposal is the operator's call to make before chatting on.
if s != nil && s.Propose != nil && !m.proposeDismissed {
return StateWorkflowPropose
}
if m.approvalDismissed {
return StateInSession
}
if s != nil && s.Pending != nil {
return StateApproval
}
return StateInSession
}
func (m Model) session(id string) *Session {
for i := range m.sessions {
if m.sessions[i].ID == id {
return &m.sessions[i]
}
}
return nil
}
// ensureSession returns the session with id, creating an ACTIVE entry if absent.
// Session existence is derived from the event stream (any event for an unknown
// session vivifies it) rather than a dedicated control frame.
func (m *Model) ensureSession(id string) *Session {
if s := m.session(id); s != nil {
return s
}
m.sessions = append(m.sessions, Session{
ID: id, Status: "ACTIVE", LastEventAt: nowMillis(),
})
return &m.sessions[len(m.sessions)-1]
}
// filteredSessions applies the workflow-id filter.
func (m Model) filteredSessions() []Session {
if m.filter == "" {
return m.sessions
}
out := make([]Session, 0, len(m.sessions))
for _, s := range m.sessions {
if containsFold(s.WorkflowID, m.filter) {
out = append(out, s)
}
}
return out
}