148 lines
6.9 KiB
Org Mode
148 lines
6.9 KiB
Org Mode
:PROPERTIES:
|
|
:ID: 4829cb25-efcb-4e0f-9285-7a045213d8b9
|
|
:CREATED: [2026-03-30 Mon 21:16]
|
|
:EDITED: [2026-04-07 Tue 13:42]
|
|
:END:
|
|
#+TITLE: SKILL: Chat Agent (Universal Literate Note)
|
|
#+STARTUP: content
|
|
#+FILETAGS: :chat:conversational:ui:autonomy:
|
|
|
|
* Overview
|
|
The *Chat Agent* provides a dedicated conversational interface within Emacs (`*opencortex-chat*`). It enables fluid dialogue while maintaining strict persona alignment and contextual awareness.
|
|
|
|
* Phase A: Demand (PRD)
|
|
:PROPERTIES:
|
|
:STATUS: FROZEN
|
|
:END:
|
|
|
|
** 1. Purpose
|
|
Define the interfaces for direct human-to-agent conversational interaction.
|
|
|
|
** 2. User Needs
|
|
- *Direct Interaction:* Specialized handler for `:chat-message` events.
|
|
- *Persona Alignment:* Consistency with the Identity Agent's definitions.
|
|
- *Contextual Awareness:* Reference to chat history for dialogue continuity.
|
|
- *Structural Output:* Responses formatted as valid Org-mode subtrees.
|
|
|
|
** 3. Success Criteria
|
|
*** TODO Chat Event Triggering
|
|
*** TODO Persona-Driven Response Generation
|
|
*** TODO Emacs Buffer Insertion Verification
|
|
|
|
* Phase B: Blueprint (PROTOCOL)
|
|
:PROPERTIES:
|
|
:STATUS: SIGNED
|
|
:END:
|
|
|
|
** 1. Architectural Intent
|
|
Interfaces for conversational event handling and UI integration. Source of truth is the dynamic chat buffer and the Identity skill.
|
|
|
|
** 2. Semantic Interfaces
|
|
#+begin_src lisp
|
|
(defun trigger-skill-chat (context)
|
|
"Triggers on :sensor :chat-message.")
|
|
|
|
(defun verify-skill-chat (proposed-action context)
|
|
"Ensures response is targeted to the correct Emacs buffer.")
|
|
#+end_src
|
|
|
|
* Phase D: Build (Implementation)
|
|
|
|
** Event Perception
|
|
#+begin_src lisp
|
|
|
|
(defun chat-archive-message (text &key (role :user) channel chat-id)
|
|
"Archives a chat message into the persistent Memory and triggers a snapshot."
|
|
(let* ((msg-id (org-id-new))
|
|
(obj (make-org-object
|
|
:id msg-id
|
|
:type :CHAT-MESSAGE
|
|
:attributes `(:role ,role :channel ,channel :chat-id ,chat-id :timestamp ,(get-universal-time))
|
|
:content text
|
|
:version (get-universal-time))))
|
|
(setf (gethash msg-id *memory*) obj)
|
|
(harness-log "CHAT - Message archived: ~a (~a)" msg-id role)
|
|
(snapshot-memory)
|
|
msg-id))
|
|
|
|
(defun trigger-skill-chat (context)
|
|
(let* ((payload (getf context :payload))
|
|
(sensor (getf payload :sensor)))
|
|
(when (eq sensor :chat-message)
|
|
;; Archive inbound message
|
|
(chat-archive-message (getf payload :text) :role :user :channel (getf payload :channel) :chat-id (getf payload :chat-id))
|
|
t)))
|
|
#+end_src
|
|
|
|
** Deterministic Verification
|
|
#+begin_src lisp
|
|
(defun verify-skill-chat (proposed-action context)
|
|
(let* ((payload (getf proposed-action :payload))
|
|
(action (or (getf payload :action) (getf proposed-action :action)))
|
|
(target (getf proposed-action :target)))
|
|
(if (and (listp proposed-action)
|
|
(or (and (member (getf proposed-action :type) '(:request :REQUEST))
|
|
(or (and (member target '(:emacs :EMACS))
|
|
(member action '(:insert-at-end :INSERT-AT-END)))
|
|
(and (member target '(:telegram :TELEGRAM))
|
|
(or (getf payload :chat-id) (getf proposed-action :chat-id)))
|
|
(and (member target '(:signal :SIGNAL))
|
|
(or (getf payload :chat-id) (getf proposed-action :chat-id)))
|
|
(and (member target '(:matrix :MATRIX))
|
|
(or (getf payload :room-id) (getf proposed-action :room-id)))
|
|
(and (member target '(:shell :SHELL))
|
|
(or (getf payload :cmd) (getf proposed-action :cmd)))
|
|
(member target '(:tool :TOOL))))
|
|
(member (getf proposed-action :type) '(:response :RESPONSE :log :LOG))))
|
|
(progn
|
|
;; Archive outbound response
|
|
(when (and (member (getf proposed-action :type) '(:request :REQUEST))
|
|
(not (eq target :tool)))
|
|
(chat-archive-message (getf payload :text) :role :agent :channel target :chat-id (or (getf payload :chat-id) (getf payload :room-id))))
|
|
proposed-action)
|
|
(let ((err-text (format nil "\n\n*System Error:* Chat agent returned invalid action: ~s" proposed-action)))
|
|
`(:type :request :target :emacs :payload (:action :insert-at-end :buffer "*opencortex-chat*" :text ,err-text))))))
|
|
#+end_src
|
|
|
|
** Neural Response Generation
|
|
The Chat skill acts as the conversational UI. Because the ~opencortex~ kernel evaluates LLM output via ~read-from-string~ (expecting a valid s-expression) and the chat verifier strictly expects an Emacs ~:insert-at-end~ actuation, we must explicitly mandate that the LLM wraps its conversational output in a Common Lisp property list.
|
|
|
|
#+begin_src lisp
|
|
(defun probabilistic-skill-chat (context)
|
|
"Generates a conversational response, stripping system errors from context."
|
|
(let* ((payload (getf context :payload))
|
|
(raw-text (getf payload :text))
|
|
(channel (or (getf payload :channel) :emacs))
|
|
(chat-id (getf payload :chat-id))
|
|
;; Context Purge: Remove system errors and hallucinations from the history
|
|
(clean-text (cl-ppcre:regex-replace-all "(?i)Unknown request|System Error.*|Thinking\\.\\.\\." raw-text ""))
|
|
(trimmed-text (if (> (length clean-text) 1000)
|
|
(subseq clean-text (- (length clean-text) 1000))
|
|
clean-text))
|
|
(reply-instruction
|
|
(case channel
|
|
(:telegram (format nil "- To reply via Telegram: (:type :REQUEST :target :telegram :chat-id \"~a\" :text \"<Response>\")" chat-id))
|
|
(:signal (format nil "- To reply via Signal: (:type :REQUEST :target :signal :chat-id \"~a\" :text \"<Response>\")" chat-id))
|
|
(:matrix (format nil "- To reply via Matrix: (:type :REQUEST :target :matrix :room-id \"~a\" :text \"<Response>\")" chat-id))
|
|
(:cli (format nil "- To reply via CLI: (:type :REQUEST :target :cli :text \"<Response>\")"))
|
|
(t "- To reply via Emacs: (:type :REQUEST :target :emacs :action :insert-at-end :buffer \"*opencortex-chat*\" :text \"* <Response>\")"))))
|
|
(ask-probabilistic trimmed-text :system-prompt (concatenate 'string
|
|
"ACTUATOR IDENTITY: You are the pure Lisp actuator for the opencortex kernel.
|
|
MANDATE: Output EXACTLY ONE Common Lisp property list starting with (:type :REQUEST).
|
|
ZERO CONVERSATION: Do not explain. Do not use markdown.
|
|
STRICT RULE: Never output the strings 'Unknown request' or 'System Error'.
|
|
|
|
REQUIRED FORMATS:
|
|
" reply-instruction "
|
|
- To use a tool: (:type :REQUEST :target :tool :action :call :tool \"<name>\" :args (...))"))))
|
|
#+end_src
|
|
|
|
* Registration
|
|
#+begin_src lisp
|
|
(defskill :skill-chat
|
|
:priority 100
|
|
:trigger #'trigger-skill-chat
|
|
:probabilistic #'probabilistic-skill-chat
|
|
:deterministic #'verify-skill-chat)
|
|
#+end_src
|