Skip to main content

mermaid_cli/domain/
state.rs

1//! The single state shape for the whole application.
2//!
3//! `State` is the value the reducer operates on. Everything the UI
4//! shows — from the chat log to the input buffer to the "Thinking…"
5//! animation — is derived from fields in this struct. Mutation happens
6//! only inside `update(state, msg)`; no other code is allowed to hold
7//! a `&mut State`.
8//!
9//! The sub-state enums (`TurnState`, `UiMode`, `McpServerStatus`) are
10//! intentionally explicit sum types. A previous generation of this
11//! codebase used bools like `is_generating: bool`, `is_cancelling:
12//! bool`, `is_tool_call_pending: bool` — the invariants between those
13//! bools were load-bearing and enforced by convention. Expressing the
14//! same state as a single enum makes it impossible to be in two modes
15//! at once, and the reducer can pattern-match instead of guarding with
16//! if-chains.
17
18use std::collections::{HashMap, VecDeque};
19use std::path::PathBuf;
20use std::time::SystemTime;
21
22use crate::app::instructions::LoadedInstructions;
23use crate::app::{Config, McpServerConfig};
24use crate::models::ChatMessage;
25use crate::models::tool_call::ToolCall as ModelToolCall;
26use crate::models::{ReasoningLevel, TokenUsage, TokenUsageSource};
27use crate::session::ConversationHistory;
28
29use super::cmd::ChatRequest;
30use super::compaction::CompactionTrigger;
31use super::ids::{IdAllocator, ToolCallId, TurnId};
32use super::msg::Msg;
33use super::runtime::{RuntimeState, ToolArtifact, ToolRunMetadata, ToolStatus};
34
35/// Root state. The reducer takes `State` by value, returns a new
36/// `State`, and emits any side-effects as a `Vec<Cmd>`. No `&mut` — a
37/// deliberate choice so tests can diff before/after without aliasing
38/// worries, and so replay ("compute the final State that this Msg log
39/// would produce") is a straight fold.
40#[derive(Debug, Clone)]
41pub struct State {
42    pub session: Session,
43    pub turn: TurnState,
44    pub ui: UiState,
45    pub mcp: McpState,
46    pub settings: Config,
47    pub instructions: Option<LoadedInstructions>,
48    /// Current working directory. Captured once at startup; tools
49    /// receive it via `ExecContext::workdir` and spawned subprocesses
50    /// inherit it. Centralized here so tests can inject a fake cwd.
51    pub cwd: PathBuf,
52    pub ids: IdAllocatorBundle,
53    /// When `Some`, the next render should pop up a modal confirmation
54    /// (e.g. "are you sure you want to /clear?"). Cleared by the
55    /// reducer when the user answers.
56    pub confirm: Option<Confirmation>,
57    /// Transient status line under the input box. One-shot — cleared by
58    /// `Msg::StatusConsumed` or by the next rendered frame depending on
59    /// `StatusKind`.
60    pub status: Option<StatusLine>,
61    /// Runtime-only observability state: process registry, provider
62    /// capability snapshot, and lifecycle timeline. Not sent to the
63    /// model.
64    pub runtime: RuntimeState,
65    /// Quit flag. When set, the main loop drains pending effects and
66    /// exits. The reducer never panics on its own; it sets this instead.
67    pub should_exit: bool,
68}
69
70impl State {
71    /// Build a fresh state tied to a specific model + project dir.
72    /// Nothing about this touches the filesystem or tokio — pure.
73    pub fn new(settings: Config, cwd: PathBuf, model_id: String) -> Self {
74        let project_path = cwd.display().to_string();
75        let conversation = ConversationHistory::new(project_path, model_id.clone());
76        let initial_title = conversation.title.clone();
77        // F5: seed `mcp.servers` from the user's configured MCP
78        // servers with `Starting` status. Previously the map started
79        // empty, and `McpServerReady` handlers used `get_mut` —
80        // configured servers never populated, so their tools never
81        // reached `build_chat_request`'s outgoing tool list.
82        let mcp = {
83            let mut m = McpState::default();
84            for (name, cfg) in &settings.mcp_servers {
85                m.servers.insert(
86                    name.clone(),
87                    McpServerEntry {
88                        config: cfg.clone(),
89                        status: McpServerStatus::Starting,
90                        tools: Vec::new(),
91                    },
92                );
93            }
94            m
95        };
96        // F11: honor the per-model reasoning preference (persisted via
97        // `/reasoning high` while using a specific model). Falls back to
98        // the global default when no entry exists.
99        let reasoning = settings
100            .reasoning_per_model
101            .get(&model_id)
102            .copied()
103            .unwrap_or(settings.default_model.reasoning);
104        let runtime = RuntimeState::new(&model_id);
105        Self {
106            session: Session {
107                conversation,
108                model_id,
109                reasoning,
110                cumulative_tokens: 0,
111                last_token_usage: None,
112                cumulative_token_usage: TokenUsageTotals::default(),
113                context_usage: None,
114            },
115            turn: TurnState::Idle,
116            ui: UiState {
117                last_title_dispatched: Some(initial_title),
118                ..UiState::default()
119            },
120            mcp,
121            settings,
122            instructions: None,
123            cwd,
124            ids: IdAllocatorBundle::default(),
125            confirm: None,
126            status: None,
127            runtime,
128            should_exit: false,
129        }
130    }
131
132    /// True iff the reducer is currently mid-turn. UI uses this for
133    /// the "⏎ cancels generation" hint and for keybind routing.
134    pub fn is_busy(&self) -> bool {
135        !matches!(self.turn, TurnState::Idle)
136    }
137
138    /// The active `TurnId`, if any turn is in flight. The reducer
139    /// filters incoming effect messages by comparing their embedded
140    /// `TurnId` to this value — if the user cancelled and started a
141    /// new turn, stale results from the old turn are dropped cleanly.
142    pub fn current_turn_id(&self) -> Option<TurnId> {
143        self.turn.id()
144    }
145}
146
147/// Prompt/completion/total token counts normalized for UI display.
148/// Providers report usage per API request; the session keeps both the
149/// last request and the cumulative API usage so the footer does not
150/// imply this is the current model context length.
151#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
152pub struct TokenUsageTotals {
153    pub prompt_tokens: usize,
154    pub completion_tokens: usize,
155    pub total_tokens: usize,
156    pub cached_input_tokens: usize,
157    pub cache_creation_input_tokens: usize,
158    pub reasoning_output_tokens: usize,
159}
160
161impl TokenUsageTotals {
162    pub fn from_usage(usage: &TokenUsage) -> Self {
163        Self {
164            prompt_tokens: usage.prompt_tokens,
165            completion_tokens: usage.completion_tokens,
166            total_tokens: usage.total_tokens,
167            cached_input_tokens: usage.cached_input_tokens,
168            cache_creation_input_tokens: usage.cache_creation_input_tokens,
169            reasoning_output_tokens: usage.reasoning_output_tokens,
170        }
171    }
172
173    pub fn add_assign(&mut self, other: Self) {
174        self.prompt_tokens = self.prompt_tokens.saturating_add(other.prompt_tokens);
175        self.completion_tokens = self
176            .completion_tokens
177            .saturating_add(other.completion_tokens);
178        self.total_tokens = self.total_tokens.saturating_add(other.total_tokens);
179        self.cached_input_tokens = self
180            .cached_input_tokens
181            .saturating_add(other.cached_input_tokens);
182        self.cache_creation_input_tokens = self
183            .cache_creation_input_tokens
184            .saturating_add(other.cache_creation_input_tokens);
185        self.reasoning_output_tokens = self
186            .reasoning_output_tokens
187            .saturating_add(other.reasoning_output_tokens);
188    }
189
190    pub fn input_total_tokens(&self) -> usize {
191        self.prompt_tokens
192            .saturating_add(self.cached_input_tokens)
193            .saturating_add(self.cache_creation_input_tokens)
194    }
195
196    pub fn output_total_tokens(&self) -> usize {
197        self.completion_tokens
198            .saturating_add(self.reasoning_output_tokens)
199    }
200}
201
202/// Approximate request-context breakdown used before provider usage
203/// arrives. These numbers are diagnostic estimates, not billing facts.
204#[derive(Debug, Clone, Default, PartialEq, Eq)]
205pub struct PromptTokenBreakdown {
206    pub system_tokens: usize,
207    pub instructions_tokens: usize,
208    pub message_tokens: usize,
209    pub tool_schema_tokens: usize,
210    pub image_count: usize,
211    pub message_count: usize,
212    pub tool_count: usize,
213}
214
215impl PromptTokenBreakdown {
216    pub fn total_tokens(&self) -> usize {
217        self.system_tokens
218            .saturating_add(self.instructions_tokens)
219            .saturating_add(self.message_tokens)
220            .saturating_add(self.tool_schema_tokens)
221    }
222}
223
224/// The model-visible context for the latest request. This is separate
225/// from cumulative session usage, which is an API/accounting total.
226#[derive(Debug, Clone, PartialEq, Eq)]
227pub struct ContextUsageSnapshot {
228    pub used_tokens: usize,
229    pub max_tokens: Option<usize>,
230    pub remaining_tokens: Option<usize>,
231    pub used_percent: Option<u8>,
232    pub source: TokenUsageSource,
233    pub prompt_tokens: usize,
234    pub cached_input_tokens: usize,
235    pub cache_creation_input_tokens: usize,
236    pub completion_tokens: usize,
237    pub reasoning_output_tokens: usize,
238    pub breakdown: Option<PromptTokenBreakdown>,
239}
240
241impl ContextUsageSnapshot {
242    pub fn from_usage(usage: &TokenUsage, max_tokens: Option<usize>) -> Self {
243        Self::new(
244            usage.total_tokens,
245            max_tokens,
246            usage.source,
247            usage.prompt_tokens,
248            usage.cached_input_tokens,
249            usage.cache_creation_input_tokens,
250            usage.completion_tokens,
251            usage.reasoning_output_tokens,
252            None,
253        )
254    }
255
256    pub fn from_estimate(breakdown: PromptTokenBreakdown, max_tokens: Option<usize>) -> Self {
257        let used = breakdown.total_tokens();
258        Self::new(
259            used,
260            max_tokens,
261            TokenUsageSource::Estimate,
262            used,
263            0,
264            0,
265            0,
266            0,
267            Some(breakdown),
268        )
269    }
270
271    #[allow(clippy::too_many_arguments)]
272    fn new(
273        used_tokens: usize,
274        max_tokens: Option<usize>,
275        source: TokenUsageSource,
276        prompt_tokens: usize,
277        cached_input_tokens: usize,
278        cache_creation_input_tokens: usize,
279        completion_tokens: usize,
280        reasoning_output_tokens: usize,
281        breakdown: Option<PromptTokenBreakdown>,
282    ) -> Self {
283        let remaining_tokens = max_tokens.map(|max| max.saturating_sub(used_tokens));
284        let used_percent = max_tokens
285            .filter(|max| *max > 0)
286            .map(|max| ((used_tokens.saturating_mul(100)) / max).min(100) as u8);
287        Self {
288            used_tokens,
289            max_tokens,
290            remaining_tokens,
291            used_percent,
292            source,
293            prompt_tokens,
294            cached_input_tokens,
295            cache_creation_input_tokens,
296            completion_tokens,
297            reasoning_output_tokens,
298            breakdown,
299        }
300    }
301
302    pub fn is_estimate(&self) -> bool {
303        self.source == TokenUsageSource::Estimate
304    }
305}
306
307pub fn estimate_context_usage_for_request(
308    request: &ChatRequest,
309    max_tokens: Option<usize>,
310) -> ContextUsageSnapshot {
311    let system_tokens = approx_tokens(&request.system_prompt);
312    let instructions_tokens = request
313        .instructions
314        .as_deref()
315        .map(approx_tokens)
316        .unwrap_or(0);
317    let message_tokens = request
318        .messages
319        .iter()
320        .map(|msg| {
321            let image_chars = msg
322                .images
323                .as_ref()
324                .map(|imgs| imgs.iter().map(|img| img.len()).sum::<usize>())
325                .unwrap_or(0);
326            approx_tokens(&msg.content).saturating_add(approx_tokens(&format!(
327                "{:?}{}{}",
328                msg.role,
329                msg.tool_name.as_deref().unwrap_or(""),
330                msg.tool_call_id.as_deref().unwrap_or("")
331            ))) + image_chars.div_ceil(4)
332        })
333        .sum();
334    let tool_schema: Vec<_> = request
335        .tools
336        .iter()
337        .map(|tool| tool.to_openai_json())
338        .collect();
339    let tool_schema_tokens = serde_json::to_string(&tool_schema)
340        .map(|s| approx_tokens(&s))
341        .unwrap_or(0);
342    let image_count = request
343        .messages
344        .iter()
345        .filter_map(|msg| msg.images.as_ref())
346        .map(Vec::len)
347        .sum();
348    ContextUsageSnapshot::from_estimate(
349        PromptTokenBreakdown {
350            system_tokens,
351            instructions_tokens,
352            message_tokens,
353            tool_schema_tokens,
354            image_count,
355            message_count: request.messages.len(),
356            tool_count: request.tools.len(),
357        },
358        max_tokens,
359    )
360}
361
362fn approx_tokens(text: &str) -> usize {
363    text.len().div_ceil(4)
364}
365
366/// Persistent conversational state that survives across turns.
367///
368/// "Session" here means the user-visible chat session, not the tokio
369/// runtime or the TCP connection to the provider. One chat = one
370/// `Session` = one on-disk `ConversationHistory` file.
371#[derive(Debug, Clone)]
372pub struct Session {
373    pub conversation: ConversationHistory,
374    pub model_id: String,
375    pub reasoning: ReasoningLevel,
376    /// Running total of tokens consumed across every API request in
377    /// this session. Kept for CLI JSON compatibility; the richer
378    /// prompt/completion breakdown lives in `cumulative_token_usage`.
379    pub cumulative_tokens: usize,
380    /// Token usage for the most recent completed provider request.
381    /// `None` means the provider did not report usage for that turn.
382    pub last_token_usage: Option<TokenUsageTotals>,
383    /// Prompt/completion/total API usage accumulated for this session.
384    pub cumulative_token_usage: TokenUsageTotals,
385    /// Latest model-visible context snapshot. This may be an estimate
386    /// while a request is in flight and is replaced by provider-reported
387    /// usage when available.
388    pub context_usage: Option<ContextUsageSnapshot>,
389}
390
391impl Session {
392    /// The committed message log. All messages visible in the chat
393    /// widget live here; partial in-flight content lives in
394    /// `TurnState::Generating`.
395    pub fn messages(&self) -> &[ChatMessage] {
396        &self.conversation.messages
397    }
398
399    /// Append a committed assistant/user/tool message. Mutation happens
400    /// through here so the reducer has one chokepoint to update the
401    /// conversation's `updated_at` and derived title. Pure — no I/O.
402    pub fn append(&mut self, msg: ChatMessage) {
403        self.conversation.add_messages(&[msg]);
404    }
405}
406
407/// The turn state machine. Each variant carries its own `TurnId` so
408/// the reducer can cheaply check "is this effect result for the
409/// current turn?" without threading the ID through every match arm.
410///
411/// The `ExecutingTools::outcomes: Vec<Option<ToolOutcome>>` field is
412/// the architectural payoff: every slot starts `None`, flips to
413/// `Some(outcome)` as each tool finishes, and the transition to the
414/// follow-up `Generating` state requires `outcomes` to be fully
415/// populated. Statically impossible to "lose" a tool result.
416#[derive(Debug, Clone)]
417pub enum TurnState {
418    Idle,
419    Generating {
420        id: TurnId,
421        started: SystemTime,
422        partial_text: String,
423        partial_reasoning: String,
424        /// Running token estimate — updated by `StreamText` events.
425        tokens: usize,
426        /// Sub-phase for richer status display (see `GenPhase`).
427        phase: GenPhase,
428        /// Anthropic-only: carries forward across the turn so we can
429        /// attach it to the committed assistant message. `None` until
430        /// the Anthropic adapter emits a signature event.
431        thinking_signature: Option<String>,
432        /// Tool calls the model has streamed so far this turn.
433        /// `StreamToolCall` messages push here; `StreamDone` drains
434        /// the vec, allocates `PendingToolCall` entries, and
435        /// transitions to `ExecutingTools`. When the vec is empty at
436        /// stream end, the turn returns to `Idle`.
437        pending_tool_calls: Vec<ModelToolCall>,
438    },
439    ExecutingTools {
440        id: TurnId,
441        calls: Vec<PendingToolCall>,
442        outcomes: Vec<Option<ToolOutcome>>,
443    },
444    /// A manual `/compact` request is summarizing history. Auto
445    /// compaction runs while `Generating` because it is preflight for
446    /// the same user turn; this variant is only for explicit user
447    /// compaction.
448    Compacting {
449        id: TurnId,
450        started: SystemTime,
451        trigger: CompactionTrigger,
452    },
453    /// `CancelTurn` was dispatched. The reducer has already emitted a
454    /// `Cmd::CancelScope` — now we wait for the final `Cancelled` /
455    /// `StreamDone` that the effect runner sends back when the scope's
456    /// `JoinSet` drains. Only then do we transition to `Idle`.
457    ///
458    /// Stuck in `Cancelling` too long = effect runner has a bug. UI
459    /// surfaces a "cleanup taking a while…" hint after 2s.
460    Cancelling {
461        id: TurnId,
462        since: SystemTime,
463    },
464}
465
466impl TurnState {
467    pub fn id(&self) -> Option<TurnId> {
468        match self {
469            TurnState::Idle => None,
470            TurnState::Generating { id, .. }
471            | TurnState::ExecutingTools { id, .. }
472            | TurnState::Compacting { id, .. }
473            | TurnState::Cancelling { id, .. } => Some(*id),
474        }
475    }
476
477    /// True when a `Msg` tagged with the given `TurnId` should be
478    /// accepted. Events from prior turns return false — the reducer's
479    /// first line on every effect-result arm.
480    pub fn accepts(&self, event_turn: TurnId) -> bool {
481        self.id() == Some(event_turn)
482    }
483}
484
485/// Sub-phase of `Generating`. Informational — the reducer updates it
486/// as the provider's stream progresses so the UI can show a meaningful
487/// status ("Thinking…" vs "Sending…" vs "Streaming").
488#[derive(Debug, Clone, Copy, PartialEq, Eq)]
489pub enum GenPhase {
490    /// Request dispatched, awaiting first byte.
491    Sending,
492    /// First chunk was reasoning content — currently inside a
493    /// thinking/reasoning block.
494    Thinking,
495    /// Streaming assistant content (post-thinking, or no thinking at
496    /// all).
497    Streaming,
498}
499
500/// One pending tool call that the model has asked us to execute. Wraps
501/// the wire-format tool call with an internal ID + the original
502/// provider-native structure so the reducer never loses provenance.
503#[derive(Debug, Clone)]
504pub struct PendingToolCall {
505    pub call_id: ToolCallId,
506    /// The raw tool call as it appeared in the model's response.
507    /// Preserved verbatim so the follow-up tool-result message can
508    /// reference the right function name + id on the wire.
509    pub source: ModelToolCall,
510}
511
512/// Outcome of a single tool execution.
513///
514/// `model_content` is the text that goes back to the model in the
515/// follow-up tool message. Everything else is Mermaid-owned
516/// structure for rendering, replay, process tracking, and timeline
517/// inspection.
518#[derive(Debug, Clone, PartialEq)]
519pub struct ToolOutcome {
520    pub status: ToolStatus,
521    pub summary: String,
522    pub model_content: String,
523    pub error: Option<String>,
524    pub metadata: Box<ToolRunMetadata>,
525    pub artifacts: Vec<ToolArtifact>,
526    pub duration_secs: Option<f64>,
527}
528
529impl ToolOutcome {
530    pub fn success(
531        model_content: impl Into<String>,
532        summary: impl Into<String>,
533        duration_secs: f64,
534    ) -> Self {
535        let duration = Some(duration_secs);
536        let metadata = ToolRunMetadata {
537            duration_secs: duration,
538            ..ToolRunMetadata::default()
539        };
540        Self {
541            status: ToolStatus::Success,
542            summary: summary.into(),
543            model_content: model_content.into(),
544            error: None,
545            metadata: Box::new(metadata),
546            artifacts: Vec::new(),
547            duration_secs: duration,
548        }
549    }
550
551    pub fn error(error: impl Into<String>, duration_secs: f64) -> Self {
552        let error = error.into();
553        let duration = Some(duration_secs);
554        Self {
555            status: ToolStatus::Error,
556            summary: error.clone(),
557            model_content: format!("Error: {}", error),
558            error: Some(error),
559            metadata: Box::new(ToolRunMetadata {
560                duration_secs: duration,
561                ..ToolRunMetadata::default()
562            }),
563            artifacts: Vec::new(),
564            duration_secs: duration,
565        }
566    }
567
568    pub fn cancelled() -> Self {
569        Self {
570            status: ToolStatus::Cancelled,
571            summary: "[cancelled]".to_string(),
572            model_content: "[Tool call skipped: the user cancelled before execution]".to_string(),
573            error: None,
574            metadata: Box::new(ToolRunMetadata::default()),
575            artifacts: Vec::new(),
576            duration_secs: None,
577        }
578    }
579
580    pub fn with_metadata(mut self, mut metadata: ToolRunMetadata) -> Self {
581        metadata.duration_secs = self.duration_secs;
582        self.metadata = Box::new(metadata);
583        self
584    }
585
586    pub fn with_artifacts(mut self, artifacts: Vec<ToolArtifact>) -> Self {
587        self.artifacts = artifacts.clone();
588        self.metadata.artifacts = artifacts;
589        self
590    }
591
592    pub fn with_images(self, images: Vec<String>) -> Self {
593        self.with_artifacts(
594            images
595                .into_iter()
596                .map(|data| ToolArtifact::Image { data })
597                .collect(),
598        )
599    }
600
601    pub fn was_cancelled(&self) -> bool {
602        self.status == ToolStatus::Cancelled
603    }
604
605    pub fn is_success(&self) -> bool {
606        self.status == ToolStatus::Success
607    }
608
609    pub fn output(&self) -> &str {
610        &self.model_content
611    }
612
613    pub fn error_message(&self) -> Option<&str> {
614        self.error.as_deref()
615    }
616
617    pub fn images(&self) -> Option<Vec<String>> {
618        let images: Vec<String> = self
619            .artifacts
620            .iter()
621            .filter_map(|artifact| match artifact {
622                ToolArtifact::Image { data } => Some(data.clone()),
623                _ => None,
624            })
625            .collect();
626        if images.is_empty() {
627            None
628        } else {
629            Some(images)
630        }
631    }
632
633    /// Convert to a textual representation suitable for embedding in
634    /// the follow-up `tool` role message. Cancellation produces a
635    /// placeholder so the model sees "this was skipped" rather than
636    /// the history becoming malformed.
637    pub fn as_tool_message_content(&self) -> String {
638        self.model_content.clone()
639    }
640}
641
642/// All UI-only state. Things in `UiState` never affect what gets sent
643/// to the model — only what the user sees.
644#[derive(Debug, Clone, Default)]
645pub struct UiState {
646    pub mode: UiMode,
647    pub input_buffer: String,
648    /// Byte position within `input_buffer`. The reducer normalizes to
649    /// a UTF-8 char boundary on every mutation via
650    /// `floor_char_boundary`, so widgets can slice safely.
651    pub input_cursor: usize,
652    /// Pending image pastes queued for the next user message.
653    pub attachments: Vec<Attachment>,
654    /// When true, keyboard focus is on the attachment bar (up arrow
655    /// from input moves focus up here; Esc returns focus to input).
656    pub attachment_focused: bool,
657    /// Highlighted attachment index when focused. Ignored when
658    /// `attachment_focused` is false.
659    pub attachment_selected: usize,
660    /// Scroll offset for the chat pane.
661    pub chat_scroll: usize,
662    /// When the slash-palette is open, this holds the filter prefix
663    /// (typed after the leading `/`) so the palette widget can
664    /// re-query the registry.
665    pub palette_filter: String,
666    /// When `Some(i)`, the palette has a highlighted row. `None` =
667    /// closed / not showing.
668    pub palette_cursor: Option<usize>,
669    /// Messages the user typed while a turn was in flight. The
670    /// reducer pops the oldest and auto-submits on a successful
671    /// `StreamDone`. FIFO order.
672    pub queued_messages: VecDeque<String>,
673    /// Last terminal title dispatched via `Cmd::SetTerminalTitle`.
674    /// Arms that change `session.conversation.title` consult this
675    /// and emit a fresh `SetTerminalTitle` only on diff.
676    pub last_title_dispatched: Option<String>,
677    /// Follow-up `Msg`s the reducer has queued for re-entry. The
678    /// outer `update()` drains this after each single-step call so
679    /// a handler can emit a synthetic event (e.g. Enter-on-slash
680    /// queuing `Msg::Slash(cmd)`) without self-invoking the
681    /// reducer. Bounded drain depth guards against runaway loops.
682    pub pending_msgs: VecDeque<Msg>,
683    /// Up-arrow history navigation cursor into
684    /// `session.conversation.input_history`. `None` = not
685    /// navigating (input_buffer is whatever the user typed).
686    /// `Some(i)` = currently displaying history entry at index `i`
687    /// from the END (0 = newest).
688    pub input_history_cursor: Option<usize>,
689    /// Whatever the user had typed before hitting Up. Preserved so
690    /// stepping past the newest history entry with Down restores
691    /// the partial input unchanged. Cleared on any non-nav key.
692    pub history_draft: String,
693    /// Running accumulator for mouse-wheel scroll events (F13). The
694    /// reducer adds the delta here on `Msg::MouseScroll`; the render
695    /// layer compares against its last-seen snapshot and applies the
696    /// diff to the chat pane's `ChatState`. This keeps the reducer
697    /// pure — it doesn't touch render-layer state, it just publishes
698    /// an intent. `i32` wraps at ~2 billion scrolls (never).
699    pub mouse_scroll_accum: i32,
700}
701
702/// Top-level UI mode. Like `TurnState` this is a sum type instead of a
703/// zoo of independent bools. `EditingInput` is the default.
704#[derive(Debug, Clone, PartialEq, Eq, Default)]
705pub enum UiMode {
706    #[default]
707    EditingInput,
708    /// Slash-command palette open (user typed `/`).
709    Palette,
710    /// `/load` — list of saved conversations visible. `candidates`
711    /// holds what the effect handler returned; `cursor` is the
712    /// highlighted row.
713    ConversationList {
714        candidates: Vec<ConversationSummary>,
715        cursor: usize,
716    },
717    /// `/model` — list of available models visible.
718    ModelList,
719}
720
721/// Summary row for the conversation picker. Produced by
722/// `Cmd::ListConversations` → `Msg::ConversationsListed`.
723#[derive(Debug, Clone, PartialEq, Eq)]
724pub struct ConversationSummary {
725    pub id: String,
726    pub title: String,
727    pub message_count: usize,
728    pub updated_at: String,
729}
730
731/// One pasted image, ready to send. Kept in the reducer state — not on
732/// disk — because the image hasn't been confirmed for a message yet.
733#[derive(Debug, Clone)]
734pub struct Attachment {
735    pub id: u64,
736    pub base64_data: String,
737    /// Temp file path (written by the effect runner when the paste
738    /// event comes in, so the TUI can show a preview).
739    pub temp_path: PathBuf,
740    pub size_bytes: usize,
741    pub format: String,
742}
743
744/// MCP server lifecycle state. Mutation is driven by `Msg::McpServer*`
745/// events emitted from `effect::mcp` when a server starts, advertises
746/// tools, or exits.
747#[derive(Debug, Clone, Default)]
748pub struct McpState {
749    pub servers: HashMap<String, McpServerEntry>,
750}
751
752#[derive(Debug, Clone)]
753pub struct McpServerEntry {
754    pub config: McpServerConfig,
755    pub status: McpServerStatus,
756    /// Tools advertised by the server. Populated on the
757    /// `McpServerReady` event; reducer exposes these to the model
758    /// when building the tool list for the next request.
759    pub tools: Vec<McpToolSpec>,
760}
761
762#[derive(Debug, Clone, PartialEq, Eq)]
763pub enum McpServerStatus {
764    /// `initialize` request dispatched, not yet acknowledged.
765    Starting,
766    Ready,
767    Errored {
768        reason: String,
769    },
770    Stopped,
771}
772
773/// Subset of the MCP `ToolDefinition` carried in reducer state. The
774/// reducer doesn't need the full schema; the effect layer uses the
775/// server name + tool name to route, and the reducer uses the
776/// description for palette display.
777#[derive(Debug, Clone)]
778pub struct McpToolSpec {
779    pub name: String,
780    pub description: String,
781    pub input_schema: serde_json::Value,
782}
783
784/// A pending user confirmation (modal). Examples: confirming `/clear`,
785/// confirming overwrite of an existing file on `/save <name>`.
786#[derive(Debug, Clone)]
787pub struct Confirmation {
788    pub prompt: String,
789    pub accept_msg_token: ConfirmationTarget,
790}
791
792/// What to do when the user confirms. The reducer translates
793/// `Msg::ConfirmAccepted` into a secondary dispatch based on this.
794#[derive(Debug, Clone)]
795pub enum ConfirmationTarget {
796    ClearConversation,
797}
798
799/// Transient status line shown under the input box. Self-clears after
800/// its kind's expected lifetime — `Persistent` entries stay until
801/// explicitly dismissed.
802#[derive(Debug, Clone)]
803pub struct StatusLine {
804    pub text: String,
805    pub kind: StatusKind,
806    pub shown_at: SystemTime,
807}
808
809#[derive(Debug, Clone, Copy, PartialEq, Eq)]
810pub enum StatusKind {
811    Info,
812    Warn,
813    Error,
814    /// Stays until the next turn or explicit dismissal.
815    Persistent,
816}
817
818/// All ID allocators for the session. Grouped so the reducer can
819/// request any of them through a single `&mut state.ids`.
820#[derive(Debug, Clone, Copy, Default)]
821pub struct IdAllocatorBundle {
822    pub turn: IdAllocator,
823    pub tool_call: IdAllocator,
824}
825
826impl IdAllocatorBundle {
827    pub fn fresh_turn(&mut self) -> TurnId {
828        TurnId(self.turn.next())
829    }
830
831    pub fn fresh_tool_call(&mut self) -> ToolCallId {
832        ToolCallId(self.tool_call.next())
833    }
834}
835
836#[cfg(test)]
837mod tests {
838    use super::*;
839
840    fn mock_state() -> State {
841        State::new(
842            Config::default(),
843            PathBuf::from("/tmp/project"),
844            "ollama/test".to_string(),
845        )
846    }
847
848    #[test]
849    fn fresh_state_is_idle() {
850        let s = mock_state();
851        assert!(matches!(s.turn, TurnState::Idle));
852        assert!(!s.is_busy());
853        assert!(s.current_turn_id().is_none());
854    }
855
856    #[test]
857    fn turn_state_accepts_matches_id() {
858        let s = TurnState::Generating {
859            id: TurnId(7),
860            started: SystemTime::now(),
861            partial_text: String::new(),
862            partial_reasoning: String::new(),
863            tokens: 0,
864            phase: GenPhase::Sending,
865            thinking_signature: None,
866            pending_tool_calls: Vec::new(),
867        };
868        assert!(s.accepts(TurnId(7)));
869        assert!(!s.accepts(TurnId(6)));
870        assert!(!s.accepts(TurnId(8)));
871    }
872
873    #[test]
874    fn idle_rejects_all_turn_ids() {
875        let s = TurnState::Idle;
876        assert!(!s.accepts(TurnId(1)));
877        assert!(!s.accepts(TurnId(999)));
878    }
879
880    #[test]
881    fn fresh_id_allocators_monotonic() {
882        let mut bundle = IdAllocatorBundle::default();
883        assert_eq!(bundle.fresh_turn(), TurnId(1));
884        assert_eq!(bundle.fresh_turn(), TurnId(2));
885        assert_eq!(bundle.fresh_tool_call(), ToolCallId(1));
886        // Cross-allocator independence — fresh turns don't consume
887        // tool call IDs.
888    }
889
890    #[test]
891    fn tool_outcome_cancelled_content_is_placeholder() {
892        let o = ToolOutcome::cancelled();
893        assert!(o.was_cancelled());
894        let content = o.as_tool_message_content();
895        assert!(content.contains("cancelled"));
896    }
897
898    #[test]
899    fn tool_outcome_finished_returns_output_verbatim() {
900        let o = ToolOutcome::success("hello world", "hello world", 0.1);
901        assert_eq!(o.as_tool_message_content(), "hello world");
902        assert!(!o.was_cancelled());
903    }
904
905    #[test]
906    fn session_append_records_message() {
907        let mut s = mock_state();
908        s.session.append(ChatMessage::user("hi"));
909        assert_eq!(s.session.messages().len(), 1);
910        assert_eq!(s.session.messages()[0].content, "hi");
911    }
912}