fix(tui): Synchronize TUI client with protocol framing and fix input handling
Some checks failed
Deploy-Agent-V15-Stdin / JOB-V15-STDIN (push) Failing after 3s

This commit is contained in:
2026-04-17 20:04:49 -04:00
parent da38bea182
commit 3471870ab3
2 changed files with 40 additions and 44 deletions

View File

@@ -50,26 +50,22 @@ The OpenCortex TUI Client is a standalone Common Lisp executable providing a ric
(loop while *is-running* do (loop while *is-running* do
(handler-case (handler-case
(when (and *stream* (open-stream-p *stream*)) (when (and *stream* (open-stream-p *stream*))
(let ((line (read-line *stream* nil :eof))) (let ((msg (opencortex:read-framed-message *stream*)))
(if (eq line :eof) (cond ((eq msg :eof) (setf *is-running* nil))
(setf *is-running* nil) ((eq msg :error) (setf *status-text* "Protocol Error"))
(let* ((*read-eval* nil) ((and (listp msg) (eq (getf msg :type) :EVENT))
(sexp (ignore-errors (read-from-string line)))) ;; Handle Handshake or other events
(if (and sexp (listp sexp)) (let ((payload (getf msg :payload)))
(cond (when (eq (getf payload :action) :handshake)
((eq (getf sexp :type) :status) (setf *status-text* "Ready"))))
(setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" ((and (listp msg) (eq (getf msg :type) :status))
(getf sexp :scribe) (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]"
(getf sexp :gardener)))) (getf msg :scribe)
((eq (getf sexp :type) :chat) (getf msg :gardener))))
(enqueue-msg (getf sexp :text))) ((and (listp msg) (eq (getf msg :type) :chat))
((eq (getf sexp :type) :info) (enqueue-msg (getf msg :text)))
(enqueue-msg (format nil "*System*: ~a" (getf sexp :text)))) (t (enqueue-msg (format nil "~s" msg))))))
((eq (getf sexp :type) :error) (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil)))
(enqueue-msg (format nil "*Error*: ~a" (getf sexp :text))))
(t (enqueue-msg line)))
(enqueue-msg line))))))
(error () (setf *is-running* nil)))
(sleep 0.05))) (sleep 0.05)))
#+end_src #+end_src
@@ -117,8 +113,10 @@ The OpenCortex TUI Client is a standalone Common Lisp executable providing a ric
((eq ch #\Newline) ((eq ch #\Newline)
(let ((cmd (coerce *input-buffer* 'string))) (let ((cmd (coerce *input-buffer* 'string)))
(setf (fill-pointer *input-buffer*) 0) (setf (fill-pointer *input-buffer*) 0)
(format *stream* "~a~%" cmd) (when (> (length cmd) 0)
(finish-output *stream*) (let ((framed (opencortex:frame-message (format nil "~s" (list :type :EVENT :payload (list :sensor :chat-message :text cmd))))))
(format *stream* "~a~%" framed)
(finish-output *stream*)))
(when (string= cmd "/exit") (setf *is-running* nil)))) (when (string= cmd "/exit") (setf *is-running* nil))))
((eq ch :backspace) ((eq ch :backspace)
(when (> (length *input-buffer*) 0) (when (> (length *input-buffer*) 0)

View File

@@ -29,26 +29,22 @@
(loop while *is-running* do (loop while *is-running* do
(handler-case (handler-case
(when (and *stream* (open-stream-p *stream*)) (when (and *stream* (open-stream-p *stream*))
(let ((line (read-line *stream* nil :eof))) (let ((msg (opencortex:read-framed-message *stream*)))
(if (eq line :eof) (cond ((eq msg :eof) (setf *is-running* nil))
(setf *is-running* nil) ((eq msg :error) (setf *status-text* "Protocol Error"))
(let* ((*read-eval* nil) ((and (listp msg) (eq (getf msg :type) :EVENT))
(sexp (ignore-errors (read-from-string line)))) ;; Handle Handshake or other events
(if (and sexp (listp sexp)) (let ((payload (getf msg :payload)))
(cond (when (eq (getf payload :action) :handshake)
((eq (getf sexp :type) :status) (setf *status-text* "Ready"))))
(setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" ((and (listp msg) (eq (getf msg :type) :status))
(getf sexp :scribe) (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]"
(getf sexp :gardener)))) (getf msg :scribe)
((eq (getf sexp :type) :chat) (getf msg :gardener))))
(enqueue-msg (getf sexp :text))) ((and (listp msg) (eq (getf msg :type) :chat))
((eq (getf sexp :type) :info) (enqueue-msg (getf msg :text)))
(enqueue-msg (format nil "*System*: ~a" (getf sexp :text)))) (t (enqueue-msg (format nil "~s" msg))))))
((eq (getf sexp :type) :error) (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil)))
(enqueue-msg (format nil "*Error*: ~a" (getf sexp :text))))
(t (enqueue-msg line)))
(enqueue-msg line))))))
(error () (setf *is-running* nil)))
(sleep 0.05))) (sleep 0.05)))
(defun main () (defun main ()
@@ -93,8 +89,10 @@
((eq ch #\Newline) ((eq ch #\Newline)
(let ((cmd (coerce *input-buffer* 'string))) (let ((cmd (coerce *input-buffer* 'string)))
(setf (fill-pointer *input-buffer*) 0) (setf (fill-pointer *input-buffer*) 0)
(format *stream* "~a~%" cmd) (when (> (length cmd) 0)
(finish-output *stream*) (let ((framed (opencortex:frame-message (format nil "~s" (list :type :EVENT :payload (list :sensor :chat-message :text cmd))))))
(format *stream* "~a~%" framed)
(finish-output *stream*)))
(when (string= cmd "/exit") (setf *is-running* nil)))) (when (string= cmd "/exit") (setf *is-running* nil))))
((eq ch :backspace) ((eq ch :backspace)
(when (> (length *input-buffer*) 0) (when (> (length *input-buffer*) 0)