Skip to main content

Module app

Module app 

Source
Expand description

The engine-agnostic render/reducer model: ReplApp.

Why: DOC-50 §3.2/§5 Slice 4 generalizes tagent’s ReplApp (crates/trusty-agents/src/repl/tui/types.rs/app.rs) into the shape both AgentEngine (tagent) and CodeEngine (tcode) can render against without either product’s semantics leaking into the shared crate. The original interleaves ~15 tagent-specific fields (OpenRouter cost tracking, TM/claude-mpm session counts, model/provider pickers, agent scope) with the genuinely generic chat/input/history/scroll state DOC-50 §3.2 calls out for extraction — this module keeps only the latter. Product-specific data (statusline content, picker items, the slash-command table) flows in through the Slice-1/1.5 seam (crate::event::ReplEvent::StatuslineUpdate, crate::model) instead of being hardcoded here, per the generalization mandate.

What: ReplApp is the M the shared crate::run::event_loop drives (it implements crate::run::TuiModel). reduce::apply is the apply: FnMut(&mut M, ReplEvent) reducer half of that contract; this module owns the state and its primitive mutators (insert_char, backspace, push_user, scroll, …). Most are direct ports; a few (Up/Down/Ctrl-E key handling) are ports of tagent’s REAL production behavior rather than its dead-code helpers — see the disclosure list below, which spells out every point (not just those) where this module’s behavior differs from tagent’s, since Slice 10’s cutover plans against exactly this list. A product wiring run() (Slice 5+, tagent’s eventual AgentEngine/tcode’s CodeEngine) supplies its own product-specific fields (splash art, banner title, command table) via the public setters below rather than this crate special-casing either product.

§Disclosed behavioral differences from tagent (DOC-50 §5 Slice 4)

Ported faithfully (same production behavior, verified against tagent’s actual keys.rs/app.rs, not just its doc comments — one round of review on this slice caught a doc/reality drift in tagent itself, see history below):

  • Up-arrow recalls Self::last_prompt (the single most recent submission) and, when Self::busy, ALSO sets Self::pending_cancel — mirrors crates/trusty-agents/src/repl/tui/keys.rs’s real KeyCode::Up arm, not the multi-level history_prev/history_next #[allow(dead_code)] helpers tagent’s own doc comment (misleadingly) attributes to production Up-handling.
  • Down-arrow calls Self::history_next — tagent’s real KeyCode::Down arm does exactly this, even though nothing (including this crate’s Up handler) ever sets history_idx, making it a functional no-op today in both tagent and here. Ported for fidelity, not because it currently does anything.
  • Ctrl-E, with an empty input buffer and a non-None Self::last_bash_block, pastes only that block’s first non-blank line (not the whole block) — matches crates/trusty-agents/src/repl/tui/keys.rs’s KeyCode::Char('e') arm exactly (the REPL input is single-line; pasting a multi-line block would silently truncate at the first \n on submit).
  • The input composer’s right-aligned [thinking...] label renders whenever Self::busy is true, REGARDLESS of whether the input buffer is empty — mirrors crates/trusty-agents/src/repl/tui/chat.rs::draw_input. busy is set true at submit time (not on the first streamed chunk), so the pre-first-token latency window after Enter also shows the indicator.

Deliberately NOT ported (Phase 2 / later-slice / no-shared-crate- formula scope — not oversights):

  • The three-row animated activity panel (spinner cycling, elapsed timer, rust-rainbow shimmer, latest thinking-step echo — repl/tui/layout.rs::draw_activity, repl/tui/status.rs’s SPINNER_FRAMES/rainbow_spans/hsl_to_rgb). busy: bool is this crate’s entire generalization of tagent’s two-field thinking: bool + busy_since: Option<Instant> — there is no elapsed- time derivation available here (no stored start Instant).
  • Inline token counters in the input row and ALL cost/usage tracking (tokens_in/tokens_out, daily_cost_start, usage-file persistence, the OpenRouter pricing formula) — DOC-50 Q9: no cost formula belongs in the shared crate; cost is engine-supplied, pre-formatted, via crate::model::StatuslineSegment::Cost.
  • Model/provider pickers (PickerState/PickerKind) and the inline LLM-offered/slash-completion choice list (choices/choices_context/ update_slash_completions) — Phase 2 (DOC-50 §5 Slice 7 and later); crate::model::PickerItem/crate::model::PickerRequest exist (Slice 1.5) but no widget consumes them yet.
  • AgentScope’s User/Project cyan/yellow semantic — replaced by a plain Self::accent_color field with no “scope” concept; the engine decides what it means and when to change it.
  • The shared trusty_common::banner splash art, tagent’s hardcoded banner title/identity text, and its fixed /help//connect//clear/ /status command list — all now engine-supplied fields (Self::banner_art, Self::banner_title, Self::commands, Self::recent_activity) rather than constants (this crate must not depend on trusty_common, see DOC-50 §2.2’s dependency direction).
  • The literal "[trusty-agents] " status prefix and its exact idle-hint copy ("Ask ctrl anything, or /connect <path> for project work") — generalized to Self::status_prefix (defaulted from label) and a generic crate::widgets::input_composer hint string respectively.

Deliberately DIVERGES from tagent (Slice 5, DOC-50 §5 — an intentional improvement per spec, not an oversight or a parity gap):

  • Ctrl-C cancels the in-flight request (see reduce::apply_key’s 'c' arm, staging Self::pending_cancel for crate::run::run’s dispatch step to relay as a real TuiEngine::cancel_session RPC). Tagent’s actual Ctrl-c (crates/trusty-agents/src/repl/tui/keys.rs) only clears the input buffer — tagent’s cancel trigger is Up-arrow while thinking. DOC-50 §5 Slice 5 specifies Ctrl-C as the cancel key (the conventional terminal interrupt), which this crate implements as written; Up-arrow-while-busy ALSO still signals cancel here (ported faithfully from tagent, see above), so both triggers work.

§Spec References

Structs§

ChatLine
One rendered chat entry in the scrollback.
ReplApp
All mutable state the shared render/event loop needs to draw one frame.

Enums§

ChatRole
Source/role of a chat line — drives the leader glyph and color chosen by crate::widgets::scrollback::build_chat_lines.

Functions§

apply
Apply one ReplEvent to app, mutating it in place.