diff --git a/fix_tui_physically.py b/fix_tui_physically.py new file mode 100644 index 0000000..b4b932e --- /dev/null +++ b/fix_tui_physically.py @@ -0,0 +1,154 @@ +import os + +content = r""":PROPERTIES: +:ID: tui-client-spec +:CREATED: [2026-04-17 Fri 11:00] +:END: +#+TITLE: OpenCortex TUI Client (Standalone) +#+STARTUP: content +#+FILETAGS: :tui:ux:client: + +* Overview +The OpenCortex TUI Client is a standalone Common Lisp application built on **Croatoan**. It provides a real-time, multi-window interface for interacting with the OpenCortex daemon. + +* Implementation +#+begin_src lisp :tangle ../src/tui-client.lisp +(in-package :cl-user) +(defpackage :opencortex.tui + (:use :cl :croatoan) + (:export :main)) +(in-package :opencortex.tui) + +(defvar *daemon-host* "127.0.0.1") +(defvar *daemon-port* 9105) +(defvar *socket* nil) +(defvar *stream* nil) +(defvar *chat-history* (list)) +(defvar *status-text* "Connecting...") +(defvar *input-buffer* (make-array 0 :element-type 'char :fill-pointer 0 :adjustable t)) +(defvar *is-running* t) +(defvar *queue-lock* (bt:make-lock)) +(defvar *incoming-msgs* nil) + +(defun enqueue-msg (msg) + (bt:with-lock-held (*queue-lock*) + (push msg *incoming-msgs*))) + +(defun dequeue-msgs () + (bt:with-lock-held (*queue-lock*) + (let ((msgs (nreverse *incoming-msgs*))) + (setf *incoming-msgs* nil) + msgs))) + +(defun clean-keywords (msg) + (if (listp msg) + (let ((clean nil)) + (loop for (k v) on msg by #'cddr + do (push (intern (string k) :keyword) clean) + (push v clean)) + (nreverse clean)) + msg)) + +(defun listen-thread () + (loop while *is-running* do + (handler-case + (when (and *stream* (open-stream-p *stream*)) + (let ((raw-msg (opencortex:read-framed-message *stream*))) + (unless (member raw-msg '(:eof :error)) + (let* ((msg (clean-keywords raw-msg)) + (type (or (getf msg :TYPE) (getf msg :type))) + (payload (or (getf msg :PAYLOAD) (getf msg :payload)))) + (cond ((and (listp msg) (eq type :EVENT)) + (let ((action (or (getf payload :ACTION) (getf payload :action))) + (text (or (getf payload :TEXT) (getf payload :text) (getf payload :MESSAGE) (getf payload :message)))) + (cond ((eq action :handshake) (setf *status-text* "Ready")) + (text (enqueue-msg (format nil "SYSTEM: ~a" text)))))) + ((and (listp msg) (eq type :STATUS)) + (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" + (or (getf msg :SCRIBE) (getf msg :scribe)) + (or (getf msg :GARDENER) (getf msg :gardener))))) + ((and (listp msg) (eq type :CHAT)) + (enqueue-msg (or (getf msg :TEXT) (getf msg :text)))) + (t (harness-log "TUI: Ignored unknown type ~a" type))))) + (when (eq raw-msg :eof) (setf *is-running* nil)) + (when (eq raw-msg :error) (setf *status-text* "Protocol Error")))) + (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil))) + (sleep 0.05))) + +(defun main () + (handler-case + (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*)) + (error (e) (format t "Error connecting: ~a~%" e) (return-from main))) + (setf *stream* (usocket:socket-stream *socket*)) + (bt:make-thread #'listen-thread :name "tui-listener") + + (unwind-protect + (with-screen (scr :input-echoing nil :input-blocking nil :enable-colors t :cursor-visible t) + (let* ((h (height scr)) + (w (width scr)) + (chat-win (make-instance 'window :height (- h 2) :width w :position (list 0 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))) + (last-status nil)) + + (setf (function-keys-enabled-p input-win) t) + (setf (input-blocking input-win) nil) + + (loop while *is-running* do + ;; 1. Handle incoming messages + (let ((new-msgs (dequeue-msgs))) + (when new-msgs + (dolist (msg new-msgs) + (push msg *chat-history*) + (setf *chat-history* (subseq *chat-history* 0 (min (length *chat-history*) 500)))) + + (clear chat-win) + (let ((line-num 0)) + (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) + (add-string chat-win m :y line-num :x 0) + (incf line-num))) + (refresh chat-win))) + + ;; 2. Render Status Bar ONLY if changed + (unless (equal *status-text* last-status) + (clear status-win) + (add-string status-win *status-text* :attributes '(:reverse)) + (refresh status-win) + (setf last-status *status-text*)) + + ;; 3. Handle Keyboard Input + (let* ((event (get-wide-event input-win)) + (ch (and event (typep event 'event) (event-key event)))) + (when ch + (cond + ((or (eq ch #\Newline) (eq ch #\Return)) + (let ((cmd (coerce *input-buffer* 'string))) + (setf (fill-pointer *input-buffer*) 0) + (when (> (length cmd) 0) + ;; Local Echo + (enqueue-msg (concatenate 'string "> " cmd)) + ;; Send to Brain + (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)))) + ((or (eq ch :backspace) (eq ch #\Backspace) (eq ch #\Rubout) (eq ch #\Del)) + (when (> (length *input-buffer*) 0) + (decf (fill-pointer *input-buffer*)))) + ((characterp ch) + (vector-push-extend ch *input-buffer*)))) + + (clear input-win) + (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string))) + (move input-win 0 (+ 2 (length *input-buffer*))) + (refresh input-win)) + + (sleep 0.02)))) + (setf *is-running* nil) + (when *socket* (usocket:socket-close *socket*)))) +#+end_src +""" + +with open('literate/tui-client.org', 'w') as f: + f.write(content) +print("Physical Org file rewritten.") diff --git a/fix_tui_precise.py b/fix_tui_precise.py new file mode 100644 index 0000000..149a812 --- /dev/null +++ b/fix_tui_precise.py @@ -0,0 +1,43 @@ +import sys + +filepath = 'literate/tui-client.org' +with open(filepath, 'r') as f: + lines = f.read() + +# I will replace the block from (defun listen-thread to (sleep 0.05))) +# with a guaranteed balanced version. + +import re +pattern = r'\(defun listen-thread \(.*?\)\s+\(sleep 0.05\)\)\)' +replacement = """(defun listen-thread () + (loop while *is-running* do + (handler-case + (when (and *stream* (open-stream-p *stream*)) + (let ((raw-msg (opencortex:read-framed-message *stream*))) + (unless (member raw-msg '(:eof :error)) + (let* ((msg (clean-keywords raw-msg)) + (type (or (getf msg :TYPE) (getf msg :type))) + (payload (or (getf msg :PAYLOAD) (getf msg :payload)))) + (cond ((eq type :EVENT) + (let ((action (or (getf payload :ACTION) (getf payload :action))) + (text (or (getf payload :TEXT) (getf payload :text) (getf payload :MESSAGE) (getf payload :message)))) + (cond ((eq action :handshake) (setf *status-text* "Ready")) + (text (enqueue-msg (format nil "SYSTEM: ~a" text)))))) + ((eq type :STATUS) + (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" + (or (getf msg :SCRIBE) (getf msg :scribe)) + (or (getf msg :GARDENER) (getf msg :gardener))))) + ((eq type :CHAT) + (enqueue-msg (or (getf msg :TEXT) (getf msg :text)))) + (t (harness-log "TUI: Ignored unknown type ~a" type)))))) + (when (eq raw-msg :eof) (setf *is-running* nil)) + (when (eq raw-msg :error) (setf *status-text* "Protocol Error")))) + (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil))) + (sleep 0.05)))""" + +# We use a more aggressive regex that matches greedily to consume all duplication +lines = re.sub(r'\(defun listen-thread \(.*?\)\s+\(sleep 0.05\)\)\).*?\(sleep 0.05\)\)\)', replacement, lines, flags=re.DOTALL) + +with open(filepath, 'w') as f: + f.write(lines) +print("Precise repair applied.") diff --git a/literate/tui-client.org b/literate/tui-client.org index 4601bc7..7763f29 100644 --- a/literate/tui-client.org +++ b/literate/tui-client.org @@ -56,34 +56,18 @@ The OpenCortex TUI Client is a standalone Common Lisp application built on **Cro (let* ((msg (clean-keywords raw-msg)) (type (or (getf msg :TYPE) (getf msg :type))) (payload (or (getf msg :PAYLOAD) (getf msg :payload)))) - (cond ((eq type :EVENT) + (cond ((and (listp msg) (eq type :EVENT)) (let ((action (or (getf payload :ACTION) (getf payload :action))) - (sensor (or (getf payload :SENSOR) (getf payload :sensor))) (text (or (getf payload :TEXT) (getf payload :text) (getf payload :MESSAGE) (getf payload :message)))) (cond ((eq action :handshake) (setf *status-text* "Ready")) (text (enqueue-msg (format nil "SYSTEM: ~a" text)))))) - ((eq type :STATUS) + ((and (listp msg) (eq type :STATUS)) (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (or (getf msg :SCRIBE) (getf msg :scribe)) (or (getf msg :GARDENER) (getf msg :gardener))))) - ((eq type :CHAT) + ((and (listp msg) (eq type :CHAT)) (enqueue-msg (or (getf msg :TEXT) (getf msg :text)))) - (t (harness-log "TUI: Ignored unknown type ~a" type)))))) - (when (eq raw-msg :eof) (setf *is-running* nil)) - (when (eq raw-msg :error) (setf *status-text* "Protocol Error")))) - (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil))) - (sleep 0.05))) - (cond ((eq type :EVENT) - (let ((payload (or (getf msg :PAYLOAD) (getf msg :payload)))) - (when (eq (or (getf payload :ACTION) (getf payload :action)) :handshake) - (setf *status-text* "Ready")))) - ((eq type :STATUS) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" - (or (getf msg :SCRIBE) (getf msg :scribe)) - (or (getf msg :GARDENER) (getf msg :gardener))))) - ((eq type :CHAT) - (enqueue-msg (or (getf msg :TEXT) (getf msg :text)))) - (t (enqueue-msg (format nil "~s" msg)))))) + (t (harness-log "TUI: Ignored unknown type ~a" type))))) (when (eq raw-msg :eof) (setf *is-running* nil)) (when (eq raw-msg :error) (setf *status-text* "Protocol Error")))) (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil))) diff --git a/src/tui-client.lisp b/src/tui-client.lisp index b1908c0..ccab0d9 100644 --- a/src/tui-client.lisp +++ b/src/tui-client.lisp @@ -43,34 +43,18 @@ (let* ((msg (clean-keywords raw-msg)) (type (or (getf msg :TYPE) (getf msg :type))) (payload (or (getf msg :PAYLOAD) (getf msg :payload)))) - (cond ((eq type :EVENT) + (cond ((and (listp msg) (eq type :EVENT)) (let ((action (or (getf payload :ACTION) (getf payload :action))) - (sensor (or (getf payload :SENSOR) (getf payload :sensor))) (text (or (getf payload :TEXT) (getf payload :text) (getf payload :MESSAGE) (getf payload :message)))) (cond ((eq action :handshake) (setf *status-text* "Ready")) (text (enqueue-msg (format nil "SYSTEM: ~a" text)))))) - ((eq type :STATUS) + ((and (listp msg) (eq type :STATUS)) (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (or (getf msg :SCRIBE) (getf msg :scribe)) (or (getf msg :GARDENER) (getf msg :gardener))))) - ((eq type :CHAT) + ((and (listp msg) (eq type :CHAT)) (enqueue-msg (or (getf msg :TEXT) (getf msg :text)))) - (t (harness-log "TUI: Ignored unknown type ~a" type)))))) - (when (eq raw-msg :eof) (setf *is-running* nil)) - (when (eq raw-msg :error) (setf *status-text* "Protocol Error")))) - (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil))) - (sleep 0.05))) - (cond ((eq type :EVENT) - (let ((payload (or (getf msg :PAYLOAD) (getf msg :payload)))) - (when (eq (or (getf payload :ACTION) (getf payload :action)) :handshake) - (setf *status-text* "Ready")))) - ((eq type :STATUS) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" - (or (getf msg :SCRIBE) (getf msg :scribe)) - (or (getf msg :GARDENER) (getf msg :gardener))))) - ((eq type :CHAT) - (enqueue-msg (or (getf msg :TEXT) (getf msg :text)))) - (t (enqueue-msg (format nil "~s" msg)))))) + (t (harness-log "TUI: Ignored unknown type ~a" type))))) (when (eq raw-msg :eof) (setf *is-running* nil)) (when (eq raw-msg :error) (setf *status-text* "Protocol Error")))) (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil)))