fix(tui): Manually verified parenthesis balance for main loop
Some checks failed
Deploy-Agent-V15-Stdin / JOB-V15-STDIN (push) Failing after 2s

This commit is contained in:
2026-04-19 12:39:57 -04:00
parent c1d5c14412
commit 29a8af32f7
2 changed files with 14 additions and 29 deletions

View File

@@ -7,21 +7,16 @@
#+FILETAGS: :tui:ux:client: #+FILETAGS: :tui:ux:client:
* Overview * Overview
The OpenCortex TUI Client is a standalone Common Lisp executable providing a rich, interactive terminal experience via the ~croatoan~ library. The OpenCortex TUI Client is a standalone Common Lisp application built on **Croatoan** (a high-level CLOS wrapper for ncurses). It provides a real-time, multi-window interface for interacting with the OpenCortex daemon.
* Phase D: Build (Implementation) * Implementation
** Package Definition
#+begin_src lisp :tangle ../src/tui-client.lisp #+begin_src lisp :tangle ../src/tui-client.lisp
(in-package :cl-user) (in-package :cl-user)
(defpackage :opencortex.tui (defpackage :opencortex.tui
(:use :cl :croatoan) (:use :cl :croatoan)
(:export :main)) (:export :main))
(in-package :opencortex.tui) (in-package :opencortex.tui)
#+end_src
** State & Global Queues
#+begin_src lisp :tangle ../src/tui-client.lisp
(defvar *daemon-host* "127.0.0.1") (defvar *daemon-host* "127.0.0.1")
(defvar *daemon-port* 9105) (defvar *daemon-port* 9105)
(defvar *socket* nil) (defvar *socket* nil)
@@ -32,10 +27,7 @@ The OpenCortex TUI Client is a standalone Common Lisp executable providing a ric
(defvar *is-running* t) (defvar *is-running* t)
(defvar *queue-lock* (bt:make-lock)) (defvar *queue-lock* (bt:make-lock))
(defvar *incoming-msgs* nil) (defvar *incoming-msgs* nil)
#+end_src
** Networking Thread
#+begin_src lisp :tangle ../src/tui-client.lisp
(defun enqueue-msg (msg) (defun enqueue-msg (msg)
(bt:with-lock-held (*queue-lock*) (bt:with-lock-held (*queue-lock*)
(push msg *incoming-msgs*))) (push msg *incoming-msgs*)))
@@ -54,7 +46,6 @@ The OpenCortex TUI Client is a standalone Common Lisp executable providing a ric
(cond ((eq msg :eof) (setf *is-running* nil)) (cond ((eq msg :eof) (setf *is-running* nil))
((eq msg :error) (setf *status-text* "Protocol Error")) ((eq msg :error) (setf *status-text* "Protocol Error"))
((and (listp msg) (eq (getf msg :type) :EVENT)) ((and (listp msg) (eq (getf msg :type) :EVENT))
;; Handle Handshake or other events
(let ((payload (getf msg :payload))) (let ((payload (getf msg :payload)))
(when (eq (getf payload :action) :handshake) (when (eq (getf payload :action) :handshake)
(setf *status-text* "Ready")))) (setf *status-text* "Ready"))))
@@ -67,10 +58,7 @@ The OpenCortex TUI Client is a standalone Common Lisp executable providing a ric
(t (enqueue-msg (format nil "~s" msg)))))) (t (enqueue-msg (format nil "~s" msg))))))
(error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil))) (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil)))
(sleep 0.05))) (sleep 0.05)))
#+end_src
** Main TUI Loop
#+begin_src lisp :tangle ../src/tui-client.lisp
(defun main () (defun main ()
(handler-case (handler-case
(setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*)) (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*))
@@ -86,13 +74,12 @@ The OpenCortex TUI Client is a standalone Common Lisp executable providing a ric
(status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0))) (status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0)))
(input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0)))) (input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0))))
;; Initial Render ;; Initial Prompt
(add-string input-win "> ") (add-string input-win "> ")
(refresh input-win) (refresh input-win)
(loop while *is-running* do (loop while *is-running* do
;; Handle incoming messages ;; 1. Handle incoming messages
(let ((new-msgs (dequeue-msgs))) (let ((new-msgs (dequeue-msgs)))
(when new-msgs (when new-msgs
(dolist (msg new-msgs) (dolist (msg new-msgs)
@@ -106,12 +93,12 @@ The OpenCortex TUI Client is a standalone Common Lisp executable providing a ric
(incf line-num))) (incf line-num)))
(refresh chat-win))) (refresh chat-win)))
;; Render Status Bar ;; 2. Render Status Bar
(clear status-win) (clear status-win)
(add-string status-win *status-text* :attributes '(:reverse)) (add-string status-win *status-text* :attributes '(:reverse))
(refresh status-win) (refresh status-win)
;; Handle Keyboard Input ;; 3. Handle Keyboard Input
(let ((ch (get-char scr))) (let ((ch (get-char scr)))
(when ch (when ch
(cond (cond
@@ -131,9 +118,9 @@ The OpenCortex TUI Client is a standalone Common Lisp executable providing a ric
(clear input-win) (clear input-win)
(add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string))) (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string)))
(refresh input-win)) (refresh input-win)))
(refresh scr)
(refresh scr)
(sleep 0.02)))) (sleep 0.02))))
(setf *is-running* nil) (setf *is-running* nil)
(when *socket* (usocket:socket-close *socket*)))) (when *socket* (usocket:socket-close *socket*))))

View File

@@ -33,7 +33,6 @@
(cond ((eq msg :eof) (setf *is-running* nil)) (cond ((eq msg :eof) (setf *is-running* nil))
((eq msg :error) (setf *status-text* "Protocol Error")) ((eq msg :error) (setf *status-text* "Protocol Error"))
((and (listp msg) (eq (getf msg :type) :EVENT)) ((and (listp msg) (eq (getf msg :type) :EVENT))
;; Handle Handshake or other events
(let ((payload (getf msg :payload))) (let ((payload (getf msg :payload)))
(when (eq (getf payload :action) :handshake) (when (eq (getf payload :action) :handshake)
(setf *status-text* "Ready")))) (setf *status-text* "Ready"))))
@@ -62,13 +61,12 @@
(status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0))) (status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0)))
(input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0)))) (input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0))))
;; Initial Render ;; Initial Prompt
(add-string input-win "> ") (add-string input-win "> ")
(refresh input-win) (refresh input-win)
(loop while *is-running* do (loop while *is-running* do
;; Handle incoming messages ;; 1. Handle incoming messages
(let ((new-msgs (dequeue-msgs))) (let ((new-msgs (dequeue-msgs)))
(when new-msgs (when new-msgs
(dolist (msg new-msgs) (dolist (msg new-msgs)
@@ -82,12 +80,12 @@
(incf line-num))) (incf line-num)))
(refresh chat-win))) (refresh chat-win)))
;; Render Status Bar ;; 2. Render Status Bar
(clear status-win) (clear status-win)
(add-string status-win *status-text* :attributes '(:reverse)) (add-string status-win *status-text* :attributes '(:reverse))
(refresh status-win) (refresh status-win)
;; Handle Keyboard Input ;; 3. Handle Keyboard Input
(let ((ch (get-char scr))) (let ((ch (get-char scr)))
(when ch (when ch
(cond (cond
@@ -107,9 +105,9 @@
(clear input-win) (clear input-win)
(add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string))) (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string)))
(refresh input-win)) (refresh input-win)))
(refresh scr)
(refresh scr)
(sleep 0.02)))) (sleep 0.02))))
(setf *is-running* nil) (setf *is-running* nil)
(when *socket* (usocket:socket-close *socket*)))) (when *socket* (usocket:socket-close *socket*))))