- Add normalize-plist-keywords to convert LLM output TYPE -> :TYPE - Fix reason-gate to validate candidate is proper plist - Add debugging logs to trace LLM responses - Fix syntax error (extra paren) in think function
134 lines
7.9 KiB
Common Lisp
134 lines
7.9 KiB
Common Lisp
(in-package :opencortex)
|
|
|
|
(defvar *probabilistic-backends* (make-hash-table :test 'equal))
|
|
(defvar *provider-cascade* nil)
|
|
(defvar *model-selector-fn* nil)
|
|
(defvar *consensus-enabled-p* nil)
|
|
|
|
(defun register-probabilistic-backend (name fn)
|
|
"Registers a neural provider (e.g., :gemini, :anthropic) with its calling function."
|
|
(setf (gethash name *probabilistic-backends*) fn))
|
|
|
|
(defun probabilistic-call (prompt &key (system-prompt "You are the Probabilistic engine.") (cascade nil) (context nil))
|
|
"Dispatches a neural request through the provider cascade. Returns a Lisp plist or a failure log."
|
|
(let ((backends (or cascade *provider-cascade*)))
|
|
(or (dolist (backend backends)
|
|
(let ((backend-fn (gethash backend *probabilistic-backends*)))
|
|
(when backend-fn
|
|
(harness-log "PROBABILISTIC: Attempting backend ~a..." backend)
|
|
(let* ((model (when *model-selector-fn* (funcall *model-selector-fn* backend context)))
|
|
(result (if model
|
|
(funcall backend-fn prompt system-prompt :model model)
|
|
(funcall backend-fn prompt system-prompt))))
|
|
(cond ((and (listp result) (eq (getf result :status) :success))
|
|
(return (getf result :content)))
|
|
((stringp result) (return result))
|
|
(t (harness-log "PROBABILISTIC: Backend ~a failed: ~a" backend (getf result :message))))))))
|
|
(list :type :LOG :payload (list :text "Neural Cascade Failure: All providers exhausted.")))))
|
|
|
|
(defun strip-markdown (text)
|
|
"Strips common markdown code block markers from text."
|
|
(if (and text (stringp text))
|
|
(let ((cleaned text))
|
|
(setf cleaned (cl-ppcre:regex-replace-all "^```[a-z]*\\n" cleaned ""))
|
|
(setf cleaned (cl-ppcre:regex-replace-all "\\n```$" cleaned ""))
|
|
(setf cleaned (cl-ppcre:regex-replace-all "```" cleaned ""))
|
|
(string-trim '(#\Space #\Newline #\Tab) cleaned))
|
|
text))
|
|
|
|
(defun normalize-plist-keywords (plist)
|
|
"Normalize all keys in a plist to keywords (e.g., TYPE -> :TYPE)."
|
|
(when (listp plist)
|
|
(loop for (k . rest) on plist by #'cddr
|
|
collect (if (and (symbolp k) (not (keywordp k)))
|
|
(intern (string k) :keyword)
|
|
k)
|
|
collect (car rest))))
|
|
|
|
(defun think (context)
|
|
"Generates a Lisp action proposal based on current context."
|
|
(let* ((active-skill (find-triggered-skill context))
|
|
(tool-belt (generate-tool-belt-prompt))
|
|
(global-context (context-assemble-global-awareness))
|
|
(system-logs (context-get-system-logs))
|
|
(assistant-name (or (uiop:getenv "MEMEX_ASSISTANT") "Agent")))
|
|
(let* ((prompt-generator (when active-skill (skill-probabilistic-prompt active-skill)))
|
|
(raw-prompt (if prompt-generator
|
|
(funcall prompt-generator context)
|
|
(let ((p (proto-get (proto-get context :payload) :text)))
|
|
(if (and p (stringp p)) p "Maintain metabolic stasis."))))
|
|
(system-prompt (format nil "IDENTITY: ~a. MANDATE: Respond with ONE Lisp plist. ~a ~a RECENT_LOGS: ~a
|
|
IMPORTANT: To reply to the user, you MUST use:
|
|
(:TYPE :REQUEST :PAYLOAD (:ACTION :MESSAGE :TEXT \"<Response Text>\"))
|
|
|
|
To call a tool, you MUST use:
|
|
(:TYPE :REQUEST :TARGET :TOOL :ACTION :CALL :TOOL \"<name>\" :ARGS (:arg1 \"val\"))
|
|
|
|
MANDATORY VALIDATION RULE: Before declaring any Lisp code edit complete, you MUST call the `:validate-lisp` tool with the proposed code. If the tool returns `:status :error`, read the `:reason` and `:failed` fields, fix the defect, and re-validate. You are strictly forbidden from relying on your own paren-balancing or syntax intuition.
|
|
|
|
PROVIDER RULE: Always use the default cascade provider unless a specific model or capability is required for the task."
|
|
assistant-name global-context tool-belt system-logs)))
|
|
(let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context))
|
|
(cleaned (strip-markdown thought))
|
|
(meta (proto-get context :meta))
|
|
(source (proto-get meta :source)))
|
|
(harness-log "THINK: raw cleaned = ~a" (subseq cleaned 0 (min 100 (length cleaned))))
|
|
(if (and cleaned (stringp cleaned))
|
|
(let ((*read-eval* nil))
|
|
(if (and (> (length cleaned) 0) (char= (char cleaned 0) #\())
|
|
(handler-case
|
|
(let ((parsed (read-from-string cleaned)))
|
|
(harness-log "THINK: parsed = ~a" parsed)
|
|
(let ((parsed-normalized (normalize-plist-keywords parsed))
|
|
(type (proto-get parsed :TYPE))
|
|
(target (or (proto-get parsed :TARGET) (proto-get parsed :target))))
|
|
(cond ((member type '(:REQUEST :EVENT :STATUS :RESPONSE))
|
|
(unless (proto-get parsed :target) (setf (getf parsed :target) (or source :CLI)))
|
|
parsed-normalized)
|
|
((or (eq target :TOOL) (eq target :tool) (getf parsed :TOOL) (getf parsed :tool)
|
|
(and (listp parsed) (listp (car parsed)) (keywordp (caar parsed))))
|
|
(list :TYPE :REQUEST :TARGET :TOOL :PAYLOAD (normalize-plist-keywords parsed)))
|
|
(t (list :TYPE :REQUEST :TARGET (or source :CLI) :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned))))))
|
|
(error (c) (harness-log "THINK ERROR: ~a" c) (list :TYPE :REQUEST :TARGET (or source :CLI) :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned))))
|
|
(list :TYPE :REQUEST :TARGET (or source :CLI) :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned))))
|
|
thought)))))
|
|
|
|
(defun deterministic-verify (proposed-action context)
|
|
"Iterates through all skill deterministic-gates sorted by priority."
|
|
(let ((current-action proposed-action)
|
|
(skills nil))
|
|
(maphash (lambda (name skill) (declare (ignore name)) (when (skill-deterministic-fn skill) (push skill skills))) *skills-registry*)
|
|
(setf skills (sort skills #'> :key #'skill-priority))
|
|
(dolist (skill skills)
|
|
(let ((trigger (skill-trigger-fn skill))
|
|
(gate (skill-deterministic-fn skill)))
|
|
(when (or (null trigger) (ignore-errors (funcall trigger context)))
|
|
(let ((next-action (funcall gate current-action context)))
|
|
(let ((original-type (proto-get current-action :type)))
|
|
(when (and (listp next-action)
|
|
(member (proto-get next-action :type) '(:LOG :EVENT :log :event))
|
|
(or (not (member original-type '(:LOG :EVENT :log :event)))
|
|
(not (eq next-action current-action))))
|
|
(harness-log "DETERMINISTIC: Intercepted by skill '~a'" (skill-name skill))
|
|
(return-from deterministic-verify next-action)))
|
|
(setf current-action next-action)))))
|
|
current-action))
|
|
|
|
(defun reason-gate (signal)
|
|
"Unified Stage: Combines Probabilistic proposals and Deterministic verification."
|
|
(let* ((type (proto-get signal :type))
|
|
(payload (proto-get signal :payload))
|
|
(sensor (proto-get payload :sensor)))
|
|
(unless (and (eq type :EVENT) (member sensor '(:user-input :chat-message)))
|
|
(return-from reason-gate signal))
|
|
(let ((candidate (think signal)))
|
|
(harness-log "REASON: candidate = ~a" (type-of candidate))
|
|
(if (and candidate (listp candidate)
|
|
(or (keywordp (car candidate)) (eq (car candidate) 'TYPE) (eq (car candidate) 'type)))
|
|
(setf (getf signal :approved-action) (deterministic-verify candidate signal))
|
|
(progn
|
|
(harness-log "REASON: Invalid candidate type ~a, dropping" (type-of candidate))
|
|
(setf (getf signal :approved-action) nil)))
|
|
(setf (getf signal :status) :reasoned)
|
|
signal)))
|