Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 34 additions & 38 deletions TeXmacs/progs/dynamic/chat-adapter.scm
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,20 @@
;; ----
;; #<unspecified>
;; 无显式返回值。
;;
;; 逻辑
;; ----
;; 1. 检查是否提供了 model-opt
;; - 若提供,调用 chat-tab-session-select-model 设置模型
;; 2. 检查聊天标签页缓冲区是否已存在
;; - 若存在,直接切换到该缓冲区
;; - 若不存在,创建新缓冲区并初始化:
;; a. 设置缓冲区内容为空文档
;; b. 设置缓冲区标题为 "Chat"
;; c. 切换到该缓冲区
;; d. 标记缓冲区为已保存状态
;;
;; 注意
;; ----
;; 此函数是 LLM 聊天功能的入口,负责聊天标签页的创建和切换。
;; 聊天标签页使用固定的 tmfs://chat-tab URL。

(tm-define (open-llm-chat-tab . model-opt)
(:synopsis "Open or switch to the LLM Chat tab")
(when (nnull? model-opt)
(chat-tab-session-select-model (car model-opt))
) ;when
(if (buffer-exists? chat-tab-url)
(switch-to-buffer chat-tab-url)
(begin
(switch-to-buffer chat-tab-url)
;; 如果指定了模型,在已有的 Chat Tab 中新建会话
(when (nnull? model-opt)
(qt-chat-tab-new-session (car model-opt))
) ;when
) ;begin
(begin
(buffer-set chat-tab-url '(document ""))
(buffer-set-title chat-tab-url "Chat")
Expand All @@ -73,38 +63,44 @@
;;
;; 语法
;; ----
;; (chat-tab-send message-buffer input-buffer body)
;; (chat-tab-send session-id)
;;
;; 参数
;; ----
;; message-buffer : url
;; 消息缓冲区名称(URL),用于存储对话历史。
;;
;; input-buffer : url
;; 输入缓冲区名称(URL),用户在此输入消息。
;;
;; body : tree
;; 输入消息树,即用户要发送的内容。
;; session-id : string
;; 会话 UUID。
;;
;; 返回值
;; ----
;; boolean
;; 委托 chat-tab-session-send 的返回值:
;; - #t : 消息发送成功
;; - #f : 消息为空,未发送

(tm-define (chat-tab-send session-id)
(:synopsis "Adapter send entry for a chat tab")
(:argument session-id "Session UUID")
(chat-tab-session-send session-id)
) ;tm-define

;; chat-tab-cancel
;; 聊天标签的适配器取消入口。
;;
;; 逻辑
;; 语法
;; ----
;; 1. 直接调用 chat-tab-session-send,将参数原样传递
;; (chat-tab-cancel session-id)
;;
;; 注意
;; 参数
;; ----
;; 此函数是 chat-tab-session.scm 的薄包装层,用于解耦适配层与会话引擎。
;; 所有实际发送逻辑由 chat-tab-session-send 处理。
(tm-define (chat-tab-send message-buffer input-buffer body)
(:synopsis "Adapter send entry for a chat tab")
(:argument message-buffer "Message buffer name")
(:argument input-buffer "Input buffer name")
(:argument body "Input message tree")
(chat-tab-session-send message-buffer input-buffer body)
;; session-id : string
;; 会话 UUID。

(tm-define (chat-tab-cancel session-id)
(:synopsis "Adapter cancel entry for a chat tab")
(:argument session-id "Session UUID")
(let* ((st (chat-tab-get-state session-id))
(model (chat-tab-state-model st))
(plugin-ses (string-append model ":chat-tab:" session-id)))
(plugin-cancel chat-tab-session-name plugin-ses #f)
) ;let*
) ;tm-define
Loading
Loading