Files
passepartout/org/channel-tui-state.org
Amr Gharbeia b5a07a5dcb bump passepartout: v0.8.0 TUI upgrade — all 6 items
Minibuffer (dialog stack), conversation view (ScrollBox+Markdown),
command palette (Ctrl+P), sidebar (6 panels, Ctrl+B), status bar
(degraded-mode signaling), keybinding layer (defkeymap).
2026-05-13 17:57:54 -04:00

14 KiB

Passepartout TUI — Model

Model

The TUI state is a single plist accessed via st / (setf st). All state mutation flows through event handlers in the controller.

Contract

  1. (init-state): returns a fresh state plist with :msgs list, :input buffer, :dirty flag, :busy flag, and :connection status.
  2. (add-msg role content &key gate-trace): appends a message object to the :messages vector (v0.3.3), tagged with timestamp, role, and optional gate-trace from the daemon (v0.4.0).
  3. (queue-event ev): thread-safely enqueues an event for the reader loop. (drain-queue) returns and clears the queue.

Package + State

(defpackage :passepartout.channel-tui
  (:use :cl :passepartout :usocket :bordeaux-threads)
  (:export :tui-main :st :add-msg :now :input-string
           :queue-event :drain-queue :init-state
           :view-status :view-chat :view-input :redraw
           :on-key :on-daemon-msg :send-daemon
           :connect-daemon :disconnect-daemon
           :*tui-theme* :theme-color))
(in-package :passepartout.channel-tui)

(defvar *state* nil)
(defvar *event-queue* nil)
(defvar *event-lock* (bt:make-lock "tui-event-lock"))

(defvar *tui-theme*
  ;; Roles
  '(:user :green :agent :white :system :yellow
    ;; Content
    :input :cyan :timestamp :yellow :help :cyan :error :red :warning :yellow
    ;; Status
    :connected :green :disconnected :red :busy :magenta :idle :white
    ;; Gate trace
    :gate-passed :green :gate-blocked :red :gate-approval :yellow
    :hitl :magenta
    ;; Tools (future use)
    :tool-running :magenta :tool-success :green :tool-failure :red :tool-output :white
    ;; Display
    :scroll-indicator :cyan :border :white :background :black
    ;; Differentiator (v0.4.0)
    :rule-count :cyan :focus-map :yellow
    ;; UI
    :dim :white :highlight :cyan :accent :green
    ;; Degraded
    :degraded :bright-yellow)
  "Color theme plist. 28 semantic keys → hex color strings.
See *tui-theme-presets* for named presets (dark, light, solarized, gruvbox).")

(defvar *tui-theme-presets*
  '(:dark  (:user :green :agent :white :system :yellow
            :input :cyan :timestamp :yellow :help :cyan :error :red :warning :yellow
            :connected :green :disconnected :red :busy :magenta :idle :white
            :gate-passed :green :gate-blocked :red :gate-approval :yellow
            :tool-running :magenta :tool-success :green :tool-failure :red :tool-output :white
            :scroll-indicator :cyan :border :white :background :black
            :rule-count :cyan :focus-map :yellow
            :dim :white :highlight :cyan :accent :green
            :degraded :bright-yellow)
    :light (:user :blue :agent :black :system :red
            :input :black :timestamp :yellow :help :blue :error :red :warning :yellow
            :connected :green :disconnected :red :busy :magenta :idle :black
            :gate-passed :green :gate-blocked :red :gate-approval :yellow
            :tool-running :magenta :tool-success :green :tool-failure :red :tool-output :black
            :scroll-indicator :blue :border :black :background :white
            :rule-count :blue :focus-map :red
            :dim :white :highlight :blue :accent :green
            :degraded :bright-yellow)
    :gruvbox (:user "#458588" :agent "#ebdbb2" :system "#fabd2f"
              :input "#ebdbb2" :timestamp "#928374" :help "#83a598" :error "#fb4934" :warning "#fabd2f"
              :connected "#b8bb26" :disconnected "#fb4934" :busy "#d3869b" :idle "#a89984"
              :gate-passed "#b8bb26" :gate-blocked "#fb4934" :gate-approval "#fabd2f"
              :tool-running "#d3869b" :tool-success "#b8bb26" :tool-failure "#fb4934" :tool-output "#ebdbb2"
              :scroll-indicator "#83a598" :border "#a89984" :background "#282828"
              :rule-count "#83a598" :focus-map "#fabd2f"
              :dim "#928374" :highlight "#83a598" :accent "#b8bb26"
              :degraded "#fabd2f")
    :solarized (:user "#268bd2" :agent "#839496" :system "#b58900"
                :input "#839496" :timestamp "#93a1a1" :help "#2aa198" :error "#dc322f" :warning "#b58900"
                :connected "#859900" :disconnected "#dc322f" :busy "#d33682" :idle "#657b83"
                :gate-passed "#859900" :gate-blocked "#dc322f" :gate-approval "#b58900"
                :tool-running "#d33682" :tool-success "#859900" :tool-failure "#dc322f" :tool-output "#839496"
                :scroll-indicator "#2aa198" :border "#657b83" :background "#002b36"
                :rule-count "#2aa198" :focus-map "#b58900"
                :dim "#586e75" :highlight "#2aa198" :accent "#859900"
              :degraded "#b58900"))
  "Named theme presets. /theme <name> loads one into *tui-theme*.")

(defvar *tui-theme-current-name* :dark
  "Name of the currently active theme preset.")

(defun theme-save ()
  "Persist current theme to disk."
  (let ((path (merge-pathnames ".cache/passepartout/theme.lisp"
                               (user-homedir-pathname))))
    (uiop:ensure-all-directories-exist (list path))
    (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
      (format out ";; Passepartout TUI theme — auto-generated~%")
      (format out "(setf passepartout.channel-tui::*tui-theme* '~s)~%" *tui-theme*)
      (format out "(setf passepartout.channel-tui::*tui-theme-current-name* ~s)~%" *tui-theme-current-name*))
    t))

(defun theme-load ()
  "Load persisted theme from disk. Called at startup."
  (let ((path (merge-pathnames ".cache/passepartout/theme.lisp"
                               (user-homedir-pathname))))
    (when (uiop:file-exists-p path)
      (ignore-errors (load path)))))

(defun theme-switch (name)
  "Switch to a named theme preset. Returns the preset name or nil if not found."
  (let* ((key (intern (string-upcase (string name)) :keyword))
         (preset (getf *tui-theme-presets* key)))
    (when preset
      (setf *tui-theme* (copy-list preset)
            *tui-theme-current-name* key)
      (theme-save)
      (setf (st :dirty) (list t t t))
      key)))

(defun theme-color (role)
  "Returns a hex color string for a semantic role, suitable for cl-tty."
  (let ((val (or (getf *tui-theme* role) :white)))
    (cond
      ((stringp val) val)
      (t (case val
           (:green "#00FF00") (:red "#FF0000") (:cyan "#00FFFF")
           (:yellow "#FFFF00") (:magenta "#FF00FF") (:blue "#0000FF")
           (:white "#FFFFFF") (:black "#000000")
           (:bright-yellow "#FFD700")
           (t "#FFFFFF"))))))

(defun st (key) (getf *state* key))
(defun (setf st) (val key) (setf (getf *state* key) val))

(defun init-state ()
  (setf *state*
        (list :running t :mode :chat :connected nil :stream nil
              :input-buffer nil :input-history nil :input-hpos 0
              :messages (make-array 16 :adjustable t :fill-pointer 0)
              :scroll-offset 0 :busy nil :cursor-pos 0
              :pending-ctrl-x nil
              :scroll-at-bottom t :scroll-notify nil
              :streaming-text nil :url-buffer nil            ; v0.7.1
              :collapsed-gates nil                          ; v0.7.2
              :search-mode nil :search-query ""           ; v0.7.2
              :search-matches nil :search-match-idx 0
               :sidebar-visible nil                        ; v0.8.0
               :sidebar-width 30                           ; v0.8.0
               :expand-tool-calls nil                      ; v0.8.0
               :mcp-count 0                                ; v0.8.0
               :dialog-stack nil                           ; v0.8.0
               :minibuffer-active nil                      ; v0.8.0
               :command-palette-active nil                 ; v0.8.0
               :command-palette-dialog nil                 ; v0.8.0
               :dirty (list nil nil nil))))

Sidebar panel definitions

(defvar *sidebar-panels*
  '((:id :gate-trace :title "Gate Trace" :width 28)
    (:id :focus :title "Focus" :width 28)
    (:id :rules :title "Rules" :width 28)
    (:id :context :title "Context" :width 28)
    (:id :cost :title "Cost" :width 28)
    (:id :files :title "Files" :width 28))
  "Sidebar panel definitions for cl-tty slot registrations.")

Helpers

(defun now ()
  (multiple-value-bind (s m h) (get-decoded-time)
    (declare (ignore s))
    (format nil "~2,'0d:~2,'0d" h m)))

(defun input-string ()
  (coerce (reverse (st :input-buffer)) 'string))

(defun input-insert-char (ch)
  "Insert character at cursor position into the input buffer."
  (let* ((buf (st :input-buffer))
         (pos (or (st :cursor-pos) 0))
         (s (coerce (reverse buf) 'string))
         (new (concatenate 'string (subseq s 0 pos) (string ch) (subseq s pos))))
    (setf (st :input-buffer) (reverse (coerce new 'list)))
    (setf (st :cursor-pos) (1+ pos))))

(defun input-delete-char ()
  "Delete character before cursor position (standard backspace)."
  (let* ((buf (st :input-buffer))
         (pos (or (st :cursor-pos) 0)))
    (when (and buf (> pos 0))
      (let* ((s (coerce (reverse buf) 'string))
             (new (concatenate 'string (subseq s 0 (1- pos)) (subseq s pos))))
        (setf (st :input-buffer) (reverse (coerce new 'list)))
        (setf (st :cursor-pos) (1- pos))))))

(defun add-msg (role content &key gate-trace panel)
  (vector-push-extend (list :role role :content content :time (now) :gate-trace gate-trace :panel panel) (st :messages))
  ;; v0.7.0: notify when scrolled up and new msg arrives
  (unless (st :scroll-at-bottom)
    (setf (st :scroll-notify) t))
  (setf (st :dirty) (list t t nil)))

Slash Commands

(defvar *slash-commands*
  '((:title "/eval <expr> — Evaluate Lisp"        :value "/eval"   :category :session)
    (:title "/undo — Undo last operation"           :value "/undo"   :category :session)
    (:title "/redo — Redo last operation"           :value "/redo"   :category :session)
    (:title "/reconnect — Re-establish daemon"     :value "/reconnect" :category :session)
    (:title "/quit — Save history and exit"        :value "/quit"   :category :session)
    (:title "/q — Quick quit"                       :value "/q"      :category :session)
    (:title "/why — Show last gate trace"           :value "/why"    :category :memory)
    (:title "/identity — Edit IDENTITY.org"         :value "/identity" :category :memory)
    (:title "/tags — List tag severities"           :value "/tags"   :category :memory)
    (:title "/audit <id> — Inspect memory"          :value "/audit"  :category :memory)
    (:title "/audit verify — Memory integrity"      :value "/audit verify" :category :memory)
    (:title "/rewind <n> — Rewind to snapshot"     :value "/rewind" :category :memory)
    (:title "/sessions — Show memory snapshots"    :value "/sessions" :category :memory)
    (:title "/resume <n> — Resume from snapshot"   :value "/resume" :category :memory)
    (:title "/focus <project> — Set context"        :value "/focus"  :category :system)
    (:title "/scope <scope> — Change scope"         :value "/scope"  :category :system)
    (:title "/unfocus — Pop context"               :value "/unfocus" :category :system)
    (:title "/theme [name] — Show/switch theme"    :value "/theme"  :category :system)
    (:title "/context — Show context summary"      :value "/context" :category :system)
    (:title "/context why <id> — Debug memory"     :value "/context why" :category :system)
    (:title "/context dropped — Estimate pruned"   :value "/context dropped" :category :system)
    (:title "/search <query> — Search messages"    :value "/search" :category :navigation)
    (:title "/help — Show commands"                 :value "/help"   :category :help)
    (:title "/help <topic> — Search manual"         :value "/help <topic>" :category :help))
  "Slash commands for minibuffer select-dialog.")

Daemon Commands

(defvar *daemon-commands*
  '((:title "Status — Daemon health info"          :value (:action :status)         :category :session)
    (:title "Stats — Daemon statistics"              :value (:action :stats)          :category :session)
    (:title "Ping — Daemon reachability"             :value (:action :ping)           :category :session)
    (:title "Memory Snapshot — Capture state"        :value (:action :memory-snapshot) :category :memory)
    (:title "Memory Rebuild — Rebuild indices"       :value (:action :memory-rebuild)  :category :memory)
    (:title "Memory Compact — Optimize storage"      :value (:action :memory-compact)  :category :memory)
    (:title "Reload Config — Reload configuration"   :value (:action :reload-config)   :category :system)
    (:title "Reload Identity — Reload identity file" :value (:action :reload-identity) :category :system)
    (:title "List Skills — Available skills"         :value (:action :list-skills)     :category :system)
    (:title "Help — Show daemon help"                :value (:action :help)           :category :help))
  "Daemon commands for the command palette (Ctrl+P).")

Event Queue

(defun queue-event (ev)
  (bt:with-lock-held (*event-lock*) (push ev *event-queue*)))

(defun drain-queue ()
  (bt:with-lock-held (*event-lock*)
    (let ((evs (nreverse *event-queue*)))
      (setf *event-queue* nil) evs)))