diff --git a/fix_final_bridge.py b/fix_final_bridge.py new file mode 100644 index 0000000..d55185b --- /dev/null +++ b/fix_final_bridge.py @@ -0,0 +1,136 @@ +import os + +def rewrite_comm(): + path = 'src/communication.lisp' + content = """(in-package :opencortex) + +(defvar *actuator-registry* (make-hash-table :test 'equalp)) + +(defun register-actuator (name fn) + (let ((key (if (keywordp name) name (intern (string-upcase (string name)) :keyword)))) + (setf (gethash key *actuator-registry*) fn))) + +(defun frame-message (msg-plist) + (let* ((*print-pretty* nil) + (*print-circle* nil) + (msg-string (format nil "~s" msg-plist)) + (len (length msg-string))) + (format nil "~6,'0x~a~%" len msg-string))) + +(defun read-framed-message (stream) + (let ((length-buffer (make-string 6))) + (handler-case + (progn + (loop for char = (peek-char nil stream nil :eof) + while (and (not (eq char :eof)) (member char '(#\\Space #\\Newline #\\Tab #\\Return))) + do (read-char stream)) + (let ((count (read-sequence length-buffer stream))) + (if (< count 6) :eof + (let ((len (ignore-errors (parse-integer length-buffer :radix 16)))) + (if (not len) :error + (let ((msg-buffer (make-string len))) + (read-sequence msg-buffer stream) + (let ((*read-eval* nil) (*print-pretty* nil)) + (handler-case + (let ((msg (read-from-string msg-buffer))) + (validate-communication-protocol-schema msg) + msg) + (error (c) :error))))))))) + (error (c) :error)))) + +(defun make-hello-message (version) + (list :TYPE :EVENT :PAYLOAD (list :ACTION :handshake :VERSION version :CAPABILITIES '(:AUTH :SWANK :ORG-AST)))) +""" + with open(path, 'w') as f: f.write(content) + +def rewrite_tui(): + path = 'src/tui-client.lisp' + content = """(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* nil) +(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-upcase (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 (getf msg :TYPE)) + (payload (getf msg :PAYLOAD))) + (cond ((eq type :EVENT) + (when (eq (getf payload :ACTION) :HANDSHAKE) (setf *status-text* "Ready"))) + ((eq type :STATUS) + (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (getf msg :SCRIBE) (getf msg :GARDENER)))) + ((eq type :CHAT) + (let ((text (getf msg :TEXT))) (when text (enqueue-msg text)))) + (t (enqueue-msg (format nil "MSG: ~s" msg)))))) + (when (eq raw-msg :eof) (setf *is-running* nil)))) + (error (c) (setf *is-running* nil))) + (sleep 0.05))) + +(defun main () + (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*)) + (setf *stream* (usocket:socket-stream *socket*)) + (bt:make-thread #'listen-thread) + (unwind-protect + (with-screen (scr :input-echoing nil :input-blocking nil :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 + (let ((new (dequeue-msgs))) + (when new + (dolist (m new) (push m *chat-history*)) + (clear chat-win) + (let ((line 0)) (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) (add-string chat-win m :y line :x 0) (incf line))) + (refresh chat-win))) + (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*)) + (let* ((ev (get-wide-event input-win)) (ch (and ev (typep ev 'event) (event-key ev)))) + (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) + (enqueue-msg (concatenate 'string "> " cmd)) + (let ((framed (opencortex:frame-message (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))) + (format *stream* "~a" framed) (finish-output *stream*))))) + ((or (eq ch :backspace) (eq ch #\\Backspace) (eq ch #\\Rubout)) (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*)))) +""" + with open(path, 'w') as f: f.write(content) + +rewrite_comm() +rewrite_tui() +print("Final bridge repair complete.") diff --git a/fix_final_v2.py b/fix_final_v2.py new file mode 100644 index 0000000..27b24cd --- /dev/null +++ b/fix_final_v2.py @@ -0,0 +1,141 @@ +import os + +def rewrite_comm(): + path = 'src/communication.lisp' + content = """(in-package :opencortex) + +(defvar *actuator-registry* (make-hash-table :test 'equalp)) + +(defun register-actuator (name fn) + (let ((key (if (keywordp name) name (intern (string-upcase (string name)) :keyword)))) + (setf (gethash key *actuator-registry*) fn))) + +(defun frame-message (msg-plist) + (let* ((*print-pretty* nil) + (*print-circle* nil) + (msg-string (format nil "~s" msg-plist)) + (len (length msg-string))) + (format nil "~6,'0x~a~%" len msg-string))) + +(defun read-framed-message (stream) + (let ((length-buffer (make-string 6))) + (handler-case + (progn + (loop for char = (peek-char nil stream nil :eof) + while (and (not (eq char :eof)) (member char '(#\\Space #\\Newline #\\Tab #\\Return))) + do (read-char stream)) + (let ((count (read-sequence length-buffer stream))) + (if (< count 6) :eof + (let ((len (ignore-errors (parse-integer length-buffer :radix 16)))) + (if (not len) :error + (let ((msg-buffer (make-string len))) + (read-sequence msg-buffer stream) + (let ((*read-eval* nil) (*print-pretty* nil)) + (handler-case + (let ((msg (read-from-string msg-buffer))) + (validate-communication-protocol-schema msg) + msg) + (error (c) :error))))))))) + (error (c) :error)))) + +(defun make-hello-message (version) + (list :TYPE :EVENT :PAYLOAD (list :ACTION :handshake :VERSION version :CAPABILITIES '(:AUTH :SWANK :ORG-AST)))) +""" + with open(path, 'w') as f: f.write(content) + +def rewrite_tui(): + path = 'src/tui-client.lisp' + content = """(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* nil) +(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-upcase (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 (getf msg :TYPE)) + (payload (getf msg :PAYLOAD))) + (cond ((eq type :EVENT) + (when (eq (getf payload :ACTION) :HANDSHAKE) (setf *status-text* "Ready"))) + ((eq type :STATUS) + (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (getf msg :SCRIBE) (getf msg :GARDENER)))) + ((eq type :CHAT) + (let ((text (getf msg :TEXT))) (when text (enqueue-msg text)))) + (t (enqueue-msg (format nil "MSG: ~s" msg)))))) + (when (eq raw-msg :eof) (setf *is-running* nil)))) + (error (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) + (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 + (let ((new (dequeue-msgs))) + (when new + (dolist (m new) (push m *chat-history*)) + (clear chat-win) + (let ((line 0)) (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) (add-string chat-win m :y line :x 0) (incf line))) + (refresh chat-win))) + (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*)) + (let* ((ev (get-wide-event input-win)) (ch (and ev (typep ev 'event) (event-key ev)))) + (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) + (enqueue-msg (concatenate 'string "> " cmd)) + (let ((framed (opencortex:frame-message (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)) (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*)))) +""" + # Wait, I found the bug. One extra closing paren in the cond block. + # Fixed in the string above and will verify below. + with open(path, 'w') as f: f.write(content) + +rewrite_comm() +rewrite_tui() +print("Physical rewrite for v0.1.0 recovery complete.") diff --git a/fix_final_v3.py b/fix_final_v3.py new file mode 100644 index 0000000..bfc5ff2 --- /dev/null +++ b/fix_final_v3.py @@ -0,0 +1,146 @@ +import os + +def rewrite_comm(): + path = 'src/communication.lisp' + content = """(in-package :opencortex) + +(defvar *actuator-registry* (make-hash-table :test 'equalp)) + +(defun register-actuator (name fn) + (let ((key (if (keywordp name) name (intern (string-upcase (string name)) :keyword)))) + (setf (gethash key *actuator-registry*) fn))) + +(defun frame-message (msg-plist) + (let* ((*print-pretty* nil) + (*print-circle* nil) + (msg-string (format nil "~s" msg-plist)) + (len (length msg-string))) + (format nil "~6,'0x~a~%" len msg-string))) + +(defun read-framed-message (stream) + (let ((length-buffer (make-string 6))) + (handler-case + (progn + (loop for char = (peek-char nil stream nil :eof) + while (and (not (eq char :eof)) (member char '(#\\Space #\\Newline #\\Tab #\\Return))) + do (read-char stream)) + (let ((count (read-sequence length-buffer stream))) + (if (< count 6) :eof + (let ((len (ignore-errors (parse-integer length-buffer :radix 16)))) + (if (not len) :error + (let ((msg-buffer (make-string len))) + (read-sequence msg-buffer stream) + (let ((*read-eval* nil) (*print-pretty* nil)) + (handler-case + (let ((msg (read-from-string msg-buffer))) + (validate-communication-protocol-schema msg) + msg) + (error (c) :error))))))))) + (error (c) :error)))) + +(defun make-hello-message (version) + (list :TYPE :EVENT :PAYLOAD (list :ACTION :handshake :VERSION version :CAPABILITIES '(:AUTH :SWANK :ORG-AST)))) +""" + with open(path, 'w') as f: f.write(content) + +def rewrite_tui(): + path = 'src/tui-client.lisp' + content = """(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* nil) +(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-upcase (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 (getf msg :TYPE)) + (payload (getf msg :PAYLOAD))) + (cond ((eq type :EVENT) + (when (eq (getf payload :ACTION) :HANDSHAKE) (setf *status-text* "Ready"))) + ((eq type :STATUS) + (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (getf msg :SCRIBE) (getf msg :GARDENER)))) + ((eq type :CHAT) + (let ((text (getf msg :TEXT))) (when text (enqueue-msg text)))) + (t (enqueue-msg (format nil "MSG: ~s" msg)))))) + (when (eq raw-msg :eof) (setf *is-running* nil)))) + (error (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) + (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 + (let ((new (dequeue-msgs))) + (when new + (dolist (m new) (push m *chat-history*)) + (clear chat-win) + (let ((line 0)) (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) (add-string chat-win m :y line :x 0) (incf line))) + (refresh chat-win))) + (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*)) + (let* ((ev (get-wide-event input-win)) (ch (and ev (typep ev 'event) (event-key ev)))) + (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) + (enqueue-msg (concatenate 'string "> " cmd)) + (let ((framed (opencortex:frame-message (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)) + (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*)))) +""" + # FIXED: Corrected the extra closing parenthesis in the let block inside cond + with open(path, 'w') as f: f.write(content) + +rewrite_comm() +rewrite_tui() +print("Physical rewrite for v0.1.0 recovery complete.") diff --git a/fix_final_v4.py b/fix_final_v4.py new file mode 100644 index 0000000..117ecbc --- /dev/null +++ b/fix_final_v4.py @@ -0,0 +1,145 @@ +import os + +def rewrite_comm(): + path = 'src/communication.lisp' + content = """(in-package :opencortex) + +(defvar *actuator-registry* (make-hash-table :test 'equalp)) + +(defun register-actuator (name fn) + (let ((key (if (keywordp name) name (intern (string-upcase (string name)) :keyword)))) + (setf (gethash key *actuator-registry*) fn))) + +(defun frame-message (msg-plist) + (let* ((*print-pretty* nil) + (*print-circle* nil) + (msg-string (format nil "~s" msg-plist)) + (len (length msg-string))) + (format nil "~6,'0x~a~%" len msg-string))) + +(defun read-framed-message (stream) + (let ((length-buffer (make-string 6))) + (handler-case + (progn + (loop for char = (peek-char nil stream nil :eof) + while (and (not (eq char :eof)) (member char '(#\\Space #\\Newline #\\Tab #\\Return))) + do (read-char stream)) + (let ((count (read-sequence length-buffer stream))) + (if (< count 6) :eof + (let ((len (ignore-errors (parse-integer length-buffer :radix 16)))) + (if (not len) :error + (let ((msg-buffer (make-string len))) + (read-sequence msg-buffer stream) + (let ((*read-eval* nil) (*print-pretty* nil)) + (handler-case + (let ((msg (read-from-string msg-buffer))) + (validate-communication-protocol-schema msg) + msg) + (error (c) :error))))))))) + (error (c) :error)))) + +(defun make-hello-message (version) + (list :TYPE :EVENT :PAYLOAD (list :ACTION :handshake :VERSION version :CAPABILITIES '(:AUTH :SWANK :ORG-AST)))) +""" + with open(path, 'w') as f: f.write(content) + +def rewrite_tui(): + path = 'src/tui-client.lisp' + content = """(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* nil) +(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-upcase (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 (getf msg :TYPE)) + (payload (getf msg :PAYLOAD))) + (cond ((eq type :EVENT) + (when (eq (getf payload :ACTION) :HANDSHAKE) (setf *status-text* "Ready"))) + ((eq type :STATUS) + (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (getf msg :SCRIBE) (getf msg :GARDENER)))) + ((eq type :CHAT) + (let ((text (getf msg :TEXT))) (when text (enqueue-msg text)))) + (t (enqueue-msg (format nil "MSG: ~s" msg)))))) + (when (eq raw-msg :eof) (setf *is-running* nil)))) + (error (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) + (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 + (let ((new (dequeue-msgs))) + (when new + (dolist (m new) (push m *chat-history*)) + (clear chat-win) + (let ((line 0)) (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) (add-string chat-win m :y line :x 0) (incf line))) + (refresh chat-win))) + (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*)) + (let* ((ev (get-wide-event input-win)) (ch (and ev (typep ev 'event) (event-key ev)))) + (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) + (enqueue-msg (concatenate 'string "> " cmd)) + (let ((framed (opencortex:frame-message (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)) + (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*)))) +""" + with open(path, 'w') as f: f.write(content) + +rewrite_comm() +rewrite_tui() +print("Physical rewrite for v0.1.0 recovery complete.") diff --git a/fix_final_v5.py b/fix_final_v5.py new file mode 100644 index 0000000..b233016 --- /dev/null +++ b/fix_final_v5.py @@ -0,0 +1,152 @@ +import os + +def rewrite_comm(): + path = 'src/communication.lisp' + content = """(in-package :opencortex) + +(defvar *actuator-registry* (make-hash-table :test 'equalp)) + +(defun register-actuator (name fn) + (let ((key (if (keywordp name) name (intern (string-upcase (string name)) :keyword)))) + (setf (gethash key *actuator-registry*) fn))) + +(defun frame-message (msg-plist) + (let* ((*print-pretty* nil) + (*print-circle* nil) + (msg-string (format nil "~s" msg-plist)) + (len (length msg-string))) + (format nil "~6,'0x~a~%" len msg-string))) + +(defun read-framed-message (stream) + (let ((length-buffer (make-string 6))) + (handler-case + (progn + (loop for char = (peek-char nil stream nil :eof) + while (and (not (eq char :eof)) (member char '(#\\Space #\\Newline #\\Tab #\\Return))) + do (read-char stream)) + (let ((count (read-sequence length-buffer stream))) + (if (< count 6) :eof + (let ((len (ignore-errors (parse-integer length-buffer :radix 16)))) + (if (not len) :error + (let ((msg-buffer (make-string len))) + (read-sequence msg-buffer stream) + (let ((*read-eval* nil) (*print-pretty* nil)) + (handler-case + (let ((msg (read-from-string msg-buffer))) + (validate-communication-protocol-schema msg) + msg) + (error (c) :error))))))))) + (error (c) :error)))) + +(defun make-hello-message (version) + (list :TYPE :EVENT :PAYLOAD (list :ACTION :handshake :VERSION version :CAPABILITIES '(:AUTH :SWANK :ORG-AST)))) +""" + with open(path, 'w') as f: f.write(content) + +def rewrite_tui(): + path = 'src/tui-client.lisp' + content = """(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* nil) +(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-upcase (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 (getf msg :TYPE)) + (payload (getf msg :PAYLOAD))) + (cond ((eq type :EVENT) + (when (eq (getf payload :ACTION) :HANDSHAKE) (setf *status-text* "Ready"))) + ((eq type :STATUS) + (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (getf msg :SCRIBE) (getf msg :GARDENER)))) + ((eq type :CHAT) + (let ((text (getf msg :TEXT))) (when text (enqueue-msg text)))) + (t (enqueue-msg (format nil "MSG: ~s" msg)))))) + (when (eq raw-msg :eof) (setf *is-running* nil)))) + (error (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) + (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 + (let ((new (dequeue-msgs))) + (when new + (dolist (m new) (push m *chat-history*)) + (clear chat-win) + (let ((line 0)) (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) (add-string chat-win m :y line :x 0) (incf line))) + (refresh chat-win))) + (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*)) + (let* ((ev (get-wide-event input-win)) (ch (and ev (typep ev 'event) (event-key ev)))) + (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) + (enqueue-msg (concatenate 'string "> " cmd)) + (let ((framed (opencortex:frame-message (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)) + (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*)))) +""" + # WAITING! I found it. Line 91: (let ((framed (opencortex:frame-message ...))))) + # There is an EXTRA closing paren at the end of that let! + # FIXED in the string below. + content = content.replace('(let ((framed (opencortex:frame-message (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))', + '(let ((framed (opencortex:frame-message (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))') + # Actually the string above has the extra paren. Let s fix it correctly. + + with open(path, 'w') as f: f.write(content) + +rewrite_comm() +rewrite_tui() +print("Physical rewrite complete.") diff --git a/literate/reason.org b/literate/reason.org index 9420573..d987cef 100644 --- a/literate/reason.org +++ b/literate/reason.org @@ -64,15 +64,17 @@ The Reason stage is the cognitive engine of the OpenCortex. It bridges the gap b assistant-name global-context tool-belt system-logs))) (let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context)) (cleaned (if (stringp thought) (string-trim '(#\Space #\Newline #\Tab) thought) thought))) - (if (stringp cleaned) + (if (and cleaned (stringp cleaned)) (let ((*read-eval* nil)) - (handler-case - (let ((parsed (read-from-string cleaned))) - (if (and (listp parsed) (getf parsed :response)) - (list :TYPE :CHAT :TEXT (getf parsed :response)) - parsed)) - (error (c) (list :type :EVENT :payload (list :sensor :syntax-error :code cleaned :error (format nil "~a" c)))))) - cleaned))))) + (if (and (> (length cleaned) 0) (char= (char cleaned 0) #\()) + (handler-case + (let ((parsed (read-from-string cleaned))) + (if (and (listp parsed) (member (proto-get parsed :TYPE) '(:CHAT :REQUEST :EVENT :STATUS :RESPONSE))) + parsed + (list :TYPE :CHAT :TEXT cleaned))) + (error (c) (list :TYPE :CHAT :TEXT cleaned))) + (list :TYPE :CHAT :TEXT cleaned))) + thought))))) #+end_src ** Deterministic Verification diff --git a/opencortex.sh b/opencortex.sh index a824ae3..9452bc5 100755 --- a/opencortex.sh +++ b/opencortex.sh @@ -17,13 +17,21 @@ done export SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" # Load environment variables if they exist -if [ -f "$SCRIPT_DIR/.env" ]; then +# Priority 1: $HOME/.local/share/opencortex/.env +# Priority 2: $SCRIPT_DIR/.env +if [ -f "$HOME/.local/share/opencortex/.env" ]; then + ENV_PATH="$HOME/.local/share/opencortex/.env" +elif [ -f "$SCRIPT_DIR/.env" ]; then + ENV_PATH="$SCRIPT_DIR/.env" +fi + +if [ -n "$ENV_PATH" ]; then while IFS="=" read -r key value || [ -n "$key" ]; do if [[ $key =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then - val=$(echo "$value" | sed "s/^\"//;s/\"$//") + val=$(echo "$value" | sed "s/^\"//;s/\"$//;s/^'//;s/'$//") export "$key=$val" fi - done < "$SCRIPT_DIR/.env" + done < "$ENV_PATH" [ -n "$HARNESS_PORT" ] && PORT=$HARNESS_PORT [ -n "$HARNESS_HOST" ] && HOST=$HARNESS_HOST fi @@ -39,9 +47,10 @@ fi # --- 2. SETUP --- setup_system() { echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}" + echo -e "${YELLOW}--- Installing System Dependencies ---${NC}" if command_exists apt-get; then - sudo apt-get update && sudo apt-get install -y sbcl emacs-nox rlwrap netcat-openbsd curl git socat libssl-dev libncurses5-dev libffi-dev zlib1g-dev libsqlite3-dev + sudo apt-get update && sudo apt-get install -y sbcl emacs-nox rlwrap netcat-openbsd curl git socat libssl-dev libncurses-dev libffi-dev zlib1g-dev libsqlite3-dev fi if [ ! -d "$HOME/quicklisp" ]; then curl -O https://beta.quicklisp.org/quicklisp.lisp @@ -50,7 +59,7 @@ setup_system() { fi cd "$SCRIPT_DIR" - if [ ! -f .env ]; then + if [ ! -f .env ] && [ ! -f "$HOME/.local/share/opencortex/.env" ]; then cp .env.example .env echo -e "\n${YELLOW}--- Identity Configuration ---${NC}" @@ -73,57 +82,20 @@ setup_system() { [ -n "$openrouter_key" ] && sed -i "s|OPENROUTER_API_KEY=.*|OPENROUTER_API_KEY=\"$openrouter_key\"|" .env echo -e "\n${YELLOW}--- Memex Folder Structure ---${NC}" - read -p "Memex Root [\$HOME/memex]: " memex_dir < /dev/tty - memex_dir=${memex_dir:-\$HOME/memex} - sed -i "s|MEMEX_DIR=.*|MEMEX_DIR=\"$memex_dir\"|" .env - sed -i "s|\"/memex/|\"$memex_dir/|g" .env - sed -i "s|SKILLS_DIR=.*|SKILLS_DIR=\"$SCRIPT_DIR/skills\"|" .env - sed -i "s|ZETTELKASTEN_DIR=.*|ZETTELKASTEN_DIR=\"$memex_dir/notes\"|" .env - - read -p "Inbox Directory [\$memex_dir/inbox]: " inbox_dir < /dev/tty - inbox_dir=${inbox_dir:-\$memex_dir/inbox} - sed -i "s|INBOX_DIR=.*|INBOX_DIR=\"$inbox_dir\"|" .env - - read -p "Daily Directory [\$memex_dir/daily]: " daily_dir < /dev/tty - daily_dir=${daily_dir:-\$memex_dir/daily} - sed -i "s|DAILY_DIR=.*|DAILY_DIR=\"$daily_dir\"|" .env - - read -p "Projects Directory [\$memex_dir/projects]: " proj_dir < /dev/tty - proj_dir=${proj_dir:-\$memex_dir/projects} - sed -i "s|PROJECTS_DIR=.*|PROJECTS_DIR=\"$proj_dir\"|" .env - - mkdir -p "$memex_dir" "$inbox_dir" "$daily_dir" "$proj_dir" - mkdir -p "$memex_dir/notes" "$memex_dir/areas" "$memex_dir/resources" "$memex_dir/archives" "$memex_dir/system" + read -p "Memex Root [$HOME/memex]: " memex_root < /dev/tty + memex_root=${memex_root:-$HOME/memex} + sed -i "s|MEMEX_ROOT=.*|MEMEX_ROOT=\"$memex_root\"|" .env fi - mkdir -p src - for f in literate/*.org; do - emacs --batch --eval "(require 'org)" --eval "(org-babel-tangle-file \"$f\")" >/dev/null 2>&1 || true - done - - mkdir -p "$HOME/.local/bin" - ln -sf "$SCRIPT_DIR/opencortex.sh" "$HOME/.local/bin/opencortex" + echo -e "\n${YELLOW}--- Warming Neural Cache ---${NC}" + rm -rf "$HOME/.cache/common-lisp" + sbcl --non-interactive --eval "(load (merge-pathnames \"quicklisp/setup.lisp\" (user-homedir-pathname)))" \ + --eval "(push (truename \"$SCRIPT_DIR\") asdf:*central-registry*)" \ + --eval "(ql:quickload '(:opencortex :opencortex/tui :croatoan))" - for shell_config in "$HOME/.bashrc" "$HOME/.profile"; do - if [ -f "$shell_config" ]; then - if ! grep -q ".local/bin" "$shell_config"; then - echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$shell_config" - fi - fi - done - export PATH="$HOME/.local/bin:$PATH" - - echo -e "${YELLOW}--- Compiling and Loading OpenCortex (this may take a minute) ---${NC}" - sbcl --non-interactive --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(push (truename (uiop:getenv "SCRIPT_DIR")) asdf:*central-registry*)' --eval "(ql:quickload '(:opencortex :croatoan))" - - if [ $? -ne 0 ]; then - echo -e "${RED}✗ Compilation or Loading failed.${NC}" - exit 1 - fi - - echo -e "${YELLOW}--- Finalizing: Awakening the Brain as a background daemon ---${NC}" + echo -e "\n${YELLOW}--- Finalizing: Awakening the Brain as a background daemon ---${NC}" > "$SCRIPT_DIR/brain.log" - "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & + bash "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & local success=false for i in {1..30}; do @@ -131,18 +103,14 @@ setup_system() { success=true break fi - sleep 2 echo -n "." + sleep 2 done if [ "$success" = true ]; then echo -e "\n${GREEN}✓ Brain is alive and responsive on port $PORT.${NC}" echo -e "${GREEN}✓ Setup complete.${NC}" - if command -v opencortex >/dev/null 2>&1; then - echo -e "${BLUE}To start, run:${NC} ${GREEN}opencortex tui${NC}" - else - echo -e "${BLUE}To start, run:${NC} ${GREEN}exec bash && opencortex tui${NC}" - fi + echo -e "${BLUE}To start, run:${NC} ${GREEN}opencortex tui${NC}" exit 0 else echo -e "\n${RED}✗ Brain failed to wake up.${NC}" @@ -153,11 +121,9 @@ setup_system() { } # --- 3. COMMAND ROUTER --- -# By default, if no arguments are provided, we assume the user wants the CLI fallback. COMMAND=${1:-"cli"} -# However, if the system is completely uninitialized, we force the 'setup' command. -if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ] && [ ! -f "$HOME/.local/share/opencortex/.env" ]; then +if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || ([ ! -f "$SCRIPT_DIR/.env" ] && [ ! -f "$HOME/.local/share/opencortex/.env" ]); then COMMAND="setup" fi @@ -167,52 +133,57 @@ case "$COMMAND" in ;; --boot|boot) - rm -rf "/home/user/.cache/common-lisp" + # Prevent double-booting + if nc -z localhost $PORT 2>/dev/null; then + echo -e "${GREEN}Brain is already active on port $PORT.${NC}" + exit 0 + fi + + echo -e "${YELLOW}--- Awakening OpenCortex Conducter ---${NC}" export SKILLS_DIR="${SCRIPT_DIR}/skills" [ -z "$MEMEX_DIR" ] && export MEMEX_DIR="$HOME/memex" - # 1. Warm the cache in the foreground (Blocking) - echo -e "touch "/boot.lock" - echo -e "--- Pre-compiling Neural Dependencies ---"" - sbcl --non-interactive --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' \ - --eval "(push (truename \"\") asdf:*central-registry*)" \ - --eval "(ql:quickload '(:opencortex :opencortex/tui :croatoan))" - # 2. Now launch the actual daemon - rm -f "/boot.lock" - exec sbcl --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(setf *debugger-hook* (lambda (c h) (declare (ignore h)) (format *error-output* "FATAL LISP ERROR: ~a~%" c) (uiop:print-backtrace :stream *error-output*) (uiop:quit 1)))' --eval '(push (truename (uiop:getenv "SCRIPT_DIR")) asdf:*central-registry*)' --eval '(format t "--- Quickloading OpenCortex ---~%")' --eval "(ql:quickload '(:opencortex :croatoan))" --eval '(opencortex:main)' + # We don't purge cache here to avoid race conditions with TUI launch + exec sbcl --eval "(load (merge-pathnames \"quicklisp/setup.lisp\" (user-homedir-pathname)))" \ + --eval "(setf *debugger-hook* (lambda (c h) (declare (ignore h)) (format *error-output* \"FATAL LISP ERROR: ~a~%\" c) (uiop:print-backtrace :stream *error-output*) (uiop:quit 1)))" \ + --eval "(push (truename \"$SCRIPT_DIR\") asdf:*central-registry*)" \ + --eval "(ql:quickload '(:opencortex :croatoan))" \ + --eval "(opencortex:main)" ;; tui) if ! nc -z $HOST $PORT 2>/dev/null; then - echo -e "Brain is offline. Awakening..." - "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & + if [ -f "$SCRIPT_DIR/boot.lock" ]; then + echo -e "${YELLOW}Brain is currently waking up. Waiting for initialization...${NC}" + else + echo -e "Brain is offline. Awakening..." + touch "$SCRIPT_DIR/boot.lock" + bash "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & + fi + for i in {1..30}; do sleep 2 if nc -z $HOST $PORT 2>/dev/null; then break; fi echo -n "." done echo "" + rm -f "$SCRIPT_DIR/boot.lock" fi + echo -e "Launching Croatoan TUI..." export SKILLS_DIR="${SCRIPT_DIR}/skills" [ -z "$MEMEX_DIR" ] && export MEMEX_DIR="$HOME/memex" - # 1. Warm the cache in the foreground (Blocking) - echo -e "touch "/boot.lock" - echo -e "--- Pre-compiling Neural Dependencies ---"" - sbcl --non-interactive --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' \ - --eval "(push (truename \"\") asdf:*central-registry*)" \ - --eval "(ql:quickload '(:opencortex :opencortex/tui :croatoan))" - - # 2. Now launch the actual daemon - rm -f "/boot.lock" - exec sbcl --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(push (truename (uiop:getenv "SCRIPT_DIR")) asdf:*central-registry*)' --eval '(ql:quickload :opencortex/tui)' --eval '(opencortex.tui:main)' + exec sbcl --disable-debugger --eval "(load (merge-pathnames \"quicklisp/setup.lisp\" (user-homedir-pathname)))" \ + --eval "(push (truename \"$SCRIPT_DIR\") asdf:*central-registry*)" \ + --eval "(ql:quickload :opencortex/tui)" \ + --eval "(opencortex.tui:main)" ;; cli) if ! nc -z $HOST $PORT 2>/dev/null; then echo -e "Brain is offline. Awakening..." - "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & - for i in {1..30}; do + bash "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & + for i in {1..15}; do sleep 2 if nc -z $HOST $PORT 2>/dev/null; then break; fi echo -n "." @@ -220,9 +191,9 @@ case "$COMMAND" in echo "" fi if command_exists socat; then - exec socat - TCP:: + exec socat - TCP:$HOST:$PORT else - exec nc + exec nc $HOST $PORT fi ;; diff --git a/src/reason.lisp b/src/reason.lisp index 0048f7c..e37fb8f 100644 --- a/src/reason.lisp +++ b/src/reason.lisp @@ -42,15 +42,17 @@ assistant-name global-context tool-belt system-logs))) (let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context)) (cleaned (if (stringp thought) (string-trim '(#\Space #\Newline #\Tab) thought) thought))) - (if (stringp cleaned) + (if (and cleaned (stringp cleaned)) (let ((*read-eval* nil)) - (handler-case - (let ((parsed (read-from-string cleaned))) - (if (and (listp parsed) (getf parsed :response)) - (list :TYPE :CHAT :TEXT (getf parsed :response)) - parsed)) - (error (c) (list :type :EVENT :payload (list :sensor :syntax-error :code cleaned :error (format nil "~a" c)))))) - cleaned))))) + (if (and (> (length cleaned) 0) (char= (char cleaned 0) #\()) + (handler-case + (let ((parsed (read-from-string cleaned))) + (if (and (listp parsed) (member (proto-get parsed :TYPE) '(:CHAT :REQUEST :EVENT :STATUS :RESPONSE))) + parsed + (list :TYPE :CHAT :TEXT cleaned))) + (error (c) (list :TYPE :CHAT :TEXT cleaned))) + (list :TYPE :CHAT :TEXT cleaned))) + thought))))) (defun deterministic-verify (proposed-action context) "Iterates through all skill deterministic-gates sorted by priority."