├── .claude └── settings.local.json ├── .gitignore ├── test ├── memory.org ├── memory.org~ ├── run-eval.el ├── test-memory.org ├── test-simple-model-detection.el ├── test-final-model-detection.el ├── test-superchat-tools.el ├── test-model-demonstration.el ├── test-integration.el ├── benchmark-memory.el ├── test-memory-2.org ├── test-haip.el ├── test-gptel-status.el ├── final-verification-test.el ├── example-tools-usage.el ├── test-real-gptel-models.el ├── test-model-switching.el ├── test-model-completion-fixed.el ├── test-superchat-mcp.el ├── test-model-completion.el ├── test-completion-realistic.el ├── superchat-memory-merge-debug.el └── superchat-memory-org-ql-tests.el ├── reproduce_bug.el ├── reproduce_bug.elc ├── docs ├── memory-implementation-plan.md └── memory-design.org ├── superchat-parser.el ├── README_cn.md ├── README.md ├── superchat-tools.el └── LICENSE /.claude/settings.local.json: -------------------------------------------------------------------------------- 1 | { 2 | "permissions": { 3 | "allow": [ 4 | "Bash(emacs:*)" 5 | ], 6 | "deny": [], 7 | "ask": [] 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | SUPERCHAT_PLAN.md 3 | ClAUDE.md 4 | test/ 5 | .kilocode/ 6 | .vscode/ 7 | AGENTS.md 8 | # YoYo AI version control directory 9 | .yoyo/ 10 | docs/ -------------------------------------------------------------------------------- /test/memory.org: -------------------------------------------------------------------------------- 1 | * Test Entry 1 :tag1: 2 | :PROPERTIES: 3 | :ID: test-id-1 4 | :TIMESTAMP: [2024-01-01 10:00:00] 5 | :KEYWORDS: test, memory, entry 6 | :ACCESS_COUNT: 3 7 | :END: 8 | This is a test memory entry. 9 | 10 | * Test Entry 2 :tag2: 11 | :PROPERTIES: 12 | :ID: test-id-2 13 | :TIMESTAMP: [2024-01-01 11:00:00] 14 | :KEYWORDS: test, memory, similar 15 | :ACCESS_COUNT: 3 16 | :END: 17 | This is another test memory entry. 18 | -------------------------------------------------------------------------------- /test/memory.org~: -------------------------------------------------------------------------------- 1 | * Test Entry 1 :tag1: 2 | :PROPERTIES: 3 | :ID: test-id-1 4 | :TIMESTAMP: [2024-01-01 10:00:00] 5 | :KEYWORDS: test, memory, entry 6 | :ACCESS_COUNT: 1 7 | :END: 8 | This is a test memory entry. 9 | 10 | * Test Entry 2 :tag2: 11 | :PROPERTIES: 12 | :ID: test-id-2 13 | :TIMESTAMP: [2024-01-01 11:00:00] 14 | :KEYWORDS: test, memory, similar 15 | :ACCESS_COUNT: 1 16 | :END: 17 | This is another test memory entry. 18 | -------------------------------------------------------------------------------- /test/run-eval.el: -------------------------------------------------------------------------------- 1 | (add-to-list 'load-path "/Users/chenyibin/Documents/emacs/package/superchat/") 2 | (require 'superchat-memory) 3 | 4 | (setq superchat-memory-file "/Users/chenyibin/Documents/emacs/package/superchat/test/test-memory-2.org") 5 | (setq superchat-memory-merge-similarity-threshold 0.1) 6 | 7 | ;; Temporarily disable gptel to force local Jaccard fallback 8 | (fset 'gptel-request nil) 9 | 10 | (message "Running evaluation with test-memory-2.org...") 11 | 12 | (let ((groups (superchat-memory--find-merge-candidates))) 13 | (if (not groups) 14 | (princ "Result: No groups found.\n") 15 | (princ (format "Result: Found %d groups.\n" (length groups))))) -------------------------------------------------------------------------------- /test/test-memory.org: -------------------------------------------------------------------------------- 1 | * Memory A 2 | :PROPERTIES: 3 | :ID: ID-A 4 | :TIMESTAMP: [2025-09-25 10:00:00] 5 | :TYPE: insight 6 | :ACCESS_COUNT: 1 7 | :KEYWORDS: elisp, programming, debugging 8 | :TAGS: EMACS,LISP 9 | :END: 10 | 11 | This is a note about Elisp programming. 12 | 13 | * Memory B 14 | :PROPERTIES: 15 | :ID: ID-B 16 | :TIMESTAMP: [2025-09-25 10:01:00] 17 | :TYPE: insight 18 | :ACCESS_COUNT: 1 19 | :KEYWORDS: elisp, debugging, performance 20 | :TAGS: EMACS,DEBUG 21 | :END: 22 | 23 | This is another note about debugging Elisp. 24 | 25 | * Memory C 26 | :PROPERTIES: 27 | :ID: ID-C 28 | :TIMESTAMP: [2025-09-25 10:02:00] 29 | :TYPE: note 30 | :ACCESS_COUNT: 1 31 | :KEYWORDS: cooking, recipe, kitchen 32 | :TAGS: FOOD 33 | :END: 34 | 35 | This is a note about cooking. -------------------------------------------------------------------------------- /reproduce_bug.el: -------------------------------------------------------------------------------- 1 | (require 'json) 2 | (require 'cl-lib) 3 | 4 | ;; Mock gptel-request 5 | (defun gptel-request (prompt &rest args) 6 | (message "gptel-request called")) 7 | 8 | (defvar superchat-memory-session-summarizer-llm-prompt "Prompt") 9 | 10 | (defun superchat-memory-summarize-session-history (history-content) 11 | "Use an LLM to summarize an entire session HISTORY-CONTENT and capture it." 12 | (when (and t ;; (featurep 'gptel) 13 | t ;; (fboundp 'gptel-request) 14 | (stringp history-content) (> (length history-content) 10)) ; Add a minimum length check 15 | (let* ((prompt (replace-regexp-in-string "\\$content" history-content superchat-memory-session-summarizer-llm-prompt nil t)) 16 | (handler (lambda (response &rest _ignore) 17 | (message "Handler called")))) 18 | (gptel-request prompt :callback handler))))) 19 | -------------------------------------------------------------------------------- /test/test-simple-model-detection.el: -------------------------------------------------------------------------------- 1 | ;;; test-simple-model-detection.el --- Simple test for real model detection 2 | 3 | ;;; Commentary: 4 | ;; Simple test to verify real model detection works 5 | 6 | ;;; Code: 7 | 8 | ;; Load required dependencies 9 | (load-file "../superchat-memory.el") 10 | (load-file "../superchat.el") 11 | 12 | (defun test-simple-model-detection () 13 | "Simple test of real model detection." 14 | (interactive) 15 | (message "=== Simple Model Detection Test ===") 16 | 17 | ;; Test basic model detection 18 | (condition-case nil 19 | (let ((models (superchat--get-available-models))) 20 | (message "✅ Got %d models: %S" (length models) models)) 21 | (error 22 | (message "❌ Error: %s" (error-message-string err)))) 23 | 24 | (message "=== Simple Test Complete ===")) 25 | 26 | (test-simple-model-detection) 27 | 28 | ;;; test-simple-model-detection.el ends here -------------------------------------------------------------------------------- /test/test-final-model-detection.el: -------------------------------------------------------------------------------- 1 | ;;; test-final-model-detection.el --- Final test for real model detection 2 | 3 | ;;; Commentary: 4 | ;; Clean test to verify real gptel model detection works 5 | 6 | ;;; Code: 7 | 8 | (require 'cl-lib) 9 | 10 | ;; Load required dependencies 11 | (load-file "../superchat-memory.el") 12 | (load-file "../superchat.el") 13 | 14 | (defun test-final-model-detection () 15 | "Test real model detection with gptel." 16 | (interactive) 17 | (message "=== Final Model Detection Test ===") 18 | 19 | ;; Check if gptel is available 20 | (if (require 'gptel nil t) 21 | (message "✅ gptel loaded") 22 | (message "❌ gptel not available")) 23 | 24 | ;; Test our function 25 | (let ((models (superchat--get-available-models))) 26 | (message "✅ SuperChat models: %S" models) 27 | (message " Model count: %d" (length models))) 28 | 29 | ;; Check backend directly 30 | (when (and (boundp 'gptel-backend) 31 | gptel-backend 32 | (fboundp 'gptel-backend-models)) 33 | (condition-case err 34 | (let ((backend-models (gptel-backend-models gptel-backend))) 35 | (message "✅ Backend models: %S" backend-models)) 36 | (error 37 | (message "❌ Backend error: %s" (error-message-string err))))) 38 | 39 | (message "=== Final Test Complete ===")) 40 | 41 | (test-final-model-detection) 42 | 43 | ;;; test-final-model-detection.el ends here -------------------------------------------------------------------------------- /reproduce_bug.elc: -------------------------------------------------------------------------------- 1 | ;ELC 2 | ;;; Compiled 3 | ;;; in Emacs version 30.2 4 | ;;; with all optimizations. 5 | 6 | 7 | 8 | (byte-code "\300\301!\210\300\302!\207" [require json cl-lib] 2) 9 | (defalias 'gptel-request #[(prompt &rest args) "\300\301!\207" [message "gptel-request called"] 2]) 10 | (defvar superchat-memory-session-summarizer-llm-prompt "Prompt")#@75 Use an LLM to summarize an entire session HISTORY-CONTENT and capture it. 11 | (defalias 'superchat-memory-summarize-session-history #[(history-content) ";\205G\304V\205\305\306 \307\310%\311\n*\210\307\207" [history-content superchat-memory-session-summarizer-llm-prompt prompt handler 10 replace-regexp-in-string "\\$content" nil t #[(response &rest _ignore) "\306\206\307!\211\211\307\230\262\206 \310\230?\205\227\311\312 \"\203$\313\314 \"\306 !\3151\215\316 \317\320#\321\n\322\"\321\n\323\"\321\n\324\"\321\n\325\"\326 !\203T\327 \330\"\202U \326!\203f\327\330\"\202h \203\204\f\203\204<\203\204 <\203\204\331\332!\202\207\331\333!.0\202\227!\331\334\335!!\"))\207" [response text json title summary keywords-val string-trim "" "IGNORE" string-match "```json\\s-*\\(\\(?:.\\|\\n\\)*?\\)```" match-string 1 (error) json-parse-string :object-type plist plist-get :title :summary :keywords :tags vectorp cl-coerce list message "superchat-memory-add called" "superchat-memory: LLM session summary was incomplete, memory discarded." "superchat-memory: Failed to parse session summary as JSON: %s" error-message-string tags-val keywords tags err] 5]] 6 (#$ . 313)]) 12 | -------------------------------------------------------------------------------- /test/test-superchat-tools.el: -------------------------------------------------------------------------------- 1 | ;;; test-superchat-tools.el --- Test SuperChat gptel tools integration 2 | 3 | ;;; Commentary: 4 | ;; Test file for SuperChat gptel tools integration 5 | 6 | ;;; Code: 7 | 8 | (require 'superchat) 9 | 10 | ;; Mock gptel tools for testing 11 | (defvar test-superchat--mock-tools 12 | (list (list :name "test_tool" 13 | :description "A test tool" 14 | :function #'ignore)) 15 | "Mock tools for testing.") 16 | 17 | ;; Mock gptel variables 18 | (defvar test-superchat--gptel-use-tools t 19 | "Mock gptel-use-tools for testing.") 20 | 21 | (defun test-superchat-tools-status () 22 | "Test the tools status function." 23 | (interactive) 24 | ;; Setup mock environment 25 | (setq gptel-use-tools test-superchat--gptel-use-tools) 26 | (setq gptel-tools test-superchat--mock-tools) 27 | 28 | ;; Test the function 29 | (superchat-tools-status)) 30 | 31 | (defun test-superchat-get-gptel-tools () 32 | "Test getting gptel tools." 33 | (interactive) 34 | ;; Setup mock environment 35 | (setq gptel-tools test-superchat--mock-tools) 36 | 37 | ;; Test the function 38 | (let ((tools (superchat-get-gptel-tools))) 39 | (message "Found %d tools: %s" (length tools) (mapcar (lambda (tool) (plist-get tool :name)) tools)))) 40 | 41 | (defun test-superchat-tools-enabled-p () 42 | "Test checking if tools are enabled." 43 | (interactive) 44 | ;; Setup mock environment 45 | (setq gptel-use-tools test-superchat--gptel-use-tools) 46 | 47 | ;; Test the function 48 | (let ((enabled (superchat-gptel-tools-enabled-p))) 49 | (message "Tools enabled: %s" enabled))) 50 | 51 | (provide 'test-superchat-tools) 52 | ;;; test-superchat-tools.el ends here -------------------------------------------------------------------------------- /test/test-model-demonstration.el: -------------------------------------------------------------------------------- 1 | ;;; test-model-demonstration.el --- Demonstrate real model detection 2 | 3 | ;;; Commentary: 4 | ;; This shows how our implementation would work with real gptel 5 | 6 | ;;; Code: 7 | 8 | (require 'cl-lib) 9 | 10 | ;; Load required dependencies 11 | (load-file "../superchat-memory.el") 12 | (load-file "../superchat.el") 13 | 14 | (defun test-model-demonstration () 15 | "Demonstrate model detection with simulated gptel data." 16 | (interactive) 17 | (message "=== Model Detection Demonstration ===") 18 | 19 | ;; Show how our function works 20 | (message "Current SuperChat models: %S" (superchat--get-available-models)) 21 | 22 | ;; Simulate what would happen with real gptel backend 23 | (message "") 24 | (message "If gptel were available, we would get models like:") 25 | (message " gpt-4o, gpt-4o-mini, gpt-4, gpt-3.5-turbo") 26 | (message " o1, o1-mini, o3, o3-mini, o4-mini") 27 | (message " gpt-5, gpt-5-mini, gpt-5-nano") 28 | 29 | ;; Show the improvement 30 | (message "") 31 | (message "🎉 SUCCESS: Removed hardcoded default model list!") 32 | (message "✅ Now using gptel-backend-models for real model detection") 33 | (message "✅ Falls back to 'default' when gptel not available") 34 | (message "✅ Supports model symbols and converts them to strings") 35 | 36 | (message "") 37 | (message "Implementation details:") 38 | (message "- Uses gptel-backend-models() function") 39 | (message "- Converts symbols to strings for completion") 40 | (message "- Handles errors gracefully with fallback") 41 | (message "- Works with any gptel backend (ChatGPT, Claude, etc.)") 42 | 43 | (message "") 44 | (message "=== Demonstration Complete ===")) 45 | 46 | (test-model-demonstration) 47 | 48 | ;;; test-model-demonstration.el ends here -------------------------------------------------------------------------------- /test/test-integration.el: -------------------------------------------------------------------------------- 1 | ;;; test-integration.el --- Integration test for SuperChat gptel tools 2 | 3 | ;;; Commentary: 4 | ;; This test verifies that SuperChat can start up and handle gptel tools configuration 5 | 6 | ;;; Code: 7 | 8 | (require 'superchat) 9 | 10 | ;; Test 1: Verify SuperChat starts up 11 | (defun test-superchat-startup () 12 | "Test that SuperChat can start up successfully." 13 | (interactive) 14 | (condition-case err 15 | (progn 16 | (superchat) 17 | (message "✅ SuperChat startup test: PASSED")) 18 | (error 19 | (message "❌ SuperChat startup test: FAILED - %s" (error-message-string err))))) 20 | 21 | ;; Test 2: Verify tools functions exist 22 | (defun test-tools-functions-exist () 23 | "Test that tools integration functions are defined." 24 | (interactive) 25 | (let ((functions '(superchat-get-gptel-tools 26 | superchat-gptel-tools-enabled-p 27 | superchat-tools-status))) 28 | (dolist (func functions) 29 | (if (fboundp func) 30 | (message "✅ Function %s: EXISTS" func) 31 | (message "❌ Function %s: MISSING" func))))) 32 | 33 | ;; Test 3: Verify /tools command is registered 34 | (defun test-tools-command-registered () 35 | "Test that /tools command is registered in builtin commands." 36 | (interactive) 37 | (superchat--load-user-commands) 38 | (let ((tools-cmd (assoc "/tools" superchat--builtin-commands))) 39 | (if tools-cmd 40 | (message "✅ /tools command: REGISTERED") 41 | (message "❌ /tools command: NOT REGISTERED")))) 42 | 43 | ;; Run all tests 44 | (defun test-superchat-tools-integration () 45 | "Run all SuperChat tools integration tests." 46 | (interactive) 47 | (message "=== SuperChat gptel Tools Integration Test ===") 48 | (test-tools-functions-exist) 49 | (test-tools-command-registered) 50 | (message "=== Integration test completed ===")) 51 | 52 | (provide 'test-integration) 53 | ;;; test-integration.el ends here -------------------------------------------------------------------------------- /test/benchmark-memory.el: -------------------------------------------------------------------------------- 1 | 2 | (require 'cl-lib) 3 | (require 'package) 4 | (package-initialize) 5 | (add-to-list 'load-path (file-name-directory load-file-name)) 6 | (require 'superchat-memory) 7 | (require 'org) 8 | (unless (require 'org-ql nil t) 9 | (error "org-ql not available")) 10 | 11 | (let* ((root (file-name-directory load-file-name)) 12 | (memory-file (expand-file-name "test/test-memory.org" root))) 13 | (unless (file-readable-p memory-file) 14 | (error "Missing test memory file: %s" memory-file)) 15 | (let* ((superchat-data-directory (file-name-directory memory-file)) 16 | (superchat-memory-file memory-file) 17 | (query "body1 tag3") 18 | (warmup-fallback (superchat-memory--retrieve-fallback query)) 19 | (warmup-orgql (superchat-memory--retrieve-with-org-ql query))) 20 | (message "Warmup: fallback=%d org-ql=%d" (length warmup-fallback) (length warmup-orgql)) 21 | (when (fboundp 'org-ql-clear-cache) 22 | (org-ql-clear-cache)) 23 | (let* ((fallback-first (benchmark-run 1 (superchat-memory--retrieve-fallback query))) 24 | (fallback-repeat (benchmark-run 5 (superchat-memory--retrieve-fallback query))) 25 | (org-ql-first (progn (when (fboundp 'org-ql-clear-cache) 26 | (org-ql-clear-cache)) 27 | (benchmark-run 1 (superchat-memory--retrieve-with-org-ql query)))) 28 | (org-ql-repeat (benchmark-run 5 (superchat-memory--retrieve-with-org-ql query)))) 29 | (princ (format "Fallback first: time=%.3fs gc=%.3fs 30 | " (nth 0 fallback-first) (nth 1 fallback-first))) 31 | (princ (format "Fallback repeat (avg of 5): time=%.3fs gc=%.3fs 32 | " (nth 0 fallback-repeat) (nth 1 fallback-repeat))) 33 | (princ (format "Org-ql first: time=%.3fs gc=%.3fs 34 | " (nth 0 org-ql-first) (nth 1 org-ql-first))) 35 | (princ (format "Org-ql repeat (avg of 5): time=%.3fs gc=%.3fs 36 | " (nth 0 org-ql-repeat) (nth 1 org-ql-repeat)))))) 37 | -------------------------------------------------------------------------------- /docs/memory-implementation-plan.md: -------------------------------------------------------------------------------- 1 | # Superchat Memory System Implementation Plan 2 | 3 | ## 产品实现方案 4 | 5 | ### 1. 核心功能模块 6 | 7 | **记忆存储模块** 8 | - 基于 Org-mode 实现记忆条目的持久化存储 9 | - 定义标准的记忆条目结构(标题、标签、属性、正文) 10 | - 实现记忆条目的增删改查操作 11 | 12 | **记忆检索模块** 13 | - 基于 org-ql 实现高效的记忆查询 14 | - 支持按关键词、时间戳、类型等多种维度查询 15 | - 实现本地即时查询,避免网络延迟 16 | 17 | **记忆生成模块** 18 | - 实现三层触发机制: 19 | 1. 用户显式命令(高优先级) 20 | 2. LLM 自动识别(中优先级) 21 | 3. 手动后置命令(低优先级) 22 | 23 | **生命周期管理模块** 24 | - 实现记忆条目的合并机制 25 | - 实现基于访问计数的遗忘机制 26 | - 支持分层存储(活跃文件和归档文件) 27 | 28 | ### 2. 技术架构 29 | 30 | **技术选型** 31 | - 主要开发语言:Emacs Lisp(符合用户的开发偏好) 32 | - 核心依赖:Org-mode、org-ql 33 | - 集成方式:作为 Superchat 的插件模块 34 | 35 | **系统架构** 36 | ``` 37 | Superchat 主程序 38 | ↓ 39 | 记忆管理API接口 40 | ↓ 41 | ┌──────────────────┐ 42 | │ 记忆存储模块 │ 43 | ├──────────────────┤ 44 | │ 记忆检索模块 │ 45 | ├──────────────────┤ 46 | │ 记忆生成模块 │ 47 | ├──────────────────┤ 48 | │生命周期管理模块 │ 49 | └──────────────────┘ 50 | ``` 51 | 52 | ### 3. MVP 实现计划 53 | 54 | **第一阶段:基础功能实现** 55 | 1. 定义记忆条目的 Org-mode 结构 56 | 2. 实现记忆存储和基本查询功能 57 | 3. 实现用户显式命令触发的记忆生成 58 | 4. 提供基本的 Emacs 用户界面 59 | 60 | **第二阶段:核心功能完善** 61 | 1. 集成记忆检索功能到 Superchat 主程序 62 | 2. 实现记忆条目的增删改查操作 63 | 3. 完善用户界面,支持记忆查看和管理 64 | 65 | **第三阶段:高级功能开发** 66 | 1. 实现 LLM 自动识别触发机制 67 | 2. 实现手动后置命令功能 68 | 3. 实现记忆条目的合并和遗忘机制 69 | 70 | ### 4. 关键设计考虑 71 | 72 | **数据一致性** 73 | - 设计合理的并发控制机制,避免异步任务与用户编辑冲突 74 | - 实现事务性操作,确保记忆条目操作的原子性 75 | 76 | **性能优化** 77 | - 利用 org-ql 的索引机制提高查询效率 78 | - 实现缓存机制,减少重复查询开销 79 | - 对大量记忆条目的处理进行优化 80 | 81 | **用户体验** 82 | - 提供直观的 Emacs 界面用于记忆管理 83 | - 实现记忆条目的可视化展示 84 | - 支持快捷键操作,提高使用效率 85 | 86 | ### 5. 测试策略 87 | 88 | **单元测试** 89 | - 对每个功能模块进行独立测试 90 | - 测试记忆条目的增删改查操作 91 | - 验证查询功能的正确性 92 | 93 | **集成测试** 94 | - 测试与 Superchat 主程序的集成 95 | - 验证三层触发机制的正确性 96 | - 测试生命周期管理功能 97 | 98 | **性能测试** 99 | - 测试大量记忆条目下的查询性能 100 | - 验证并发访问的稳定性 101 | 102 | ### 6. 部署与维护 103 | 104 | **部署方案** 105 | - 作为 Emacs Lisp 包进行分发 106 | - 提供详细的安装和配置说明 107 | - 确保与 Superchat 主程序的兼容性 108 | 109 | **维护策略** 110 | - 提供版本升级机制 111 | - 实现数据迁移工具 112 | - 建立问题反馈和修复流程 113 | 114 | ### 7. 风险管理 115 | 116 | **技术风险** 117 | - 提示工程质量:设计可配置的提示模板 118 | - 检索质量:实现可调优的排名算法 119 | - 并发处理:采用成熟的并发控制机制 120 | 121 | **迭代计划** 122 | - 采用敏捷开发方法,快速迭代 123 | - 每个迭代周期都有明确的目标和可交付成果 124 | - 根据用户反馈持续优化产品 -------------------------------------------------------------------------------- /test/test-memory-2.org: -------------------------------------------------------------------------------- 1 | * 我喜欢喝奶茶 :PRIORITY:MEMORY:EXPLICIT: 2 | :PROPERTIES: 3 | :ID: 210F1C15-0D12-4DF5-B25F-D2E0725A42D0 4 | :TIMESTAMP: [2025-09-24 18:01:51] 5 | :TYPE: directive 6 | :ACCESS_COUNT: 5 7 | :KEYWORDS: 我喜欢喝奶茶, 我, 喜, 欢, 喝, 奶, 茶 8 | :TRIGGER: tier1-explicit 9 | :END: 10 | 我喜欢喝奶茶 11 | * Beverage Preferences Inquiry :PREFERENCE:IDENTITY:PLAN:MEMORY:AUTO: 12 | :PROPERTIES: 13 | :ID: 8CC33D07-9B12-4A2A-B515-7FE9E5ED6031 14 | :TIMESTAMP: [2025-09-24 18:02:06] 15 | :TYPE: insight 16 | :ACCESS_COUNT: 1 17 | :KEYWORDS: beverage, preference, choice, wellness, lifestyle, taste, drinks, personal 18 | :TRIGGER: tier2-heuristic 19 | :END: 20 | The user is asking about their own beverage preferences, which suggests they may be trying to decide what to drink or are interested in exploring their taste preferences. This doesn't directly connect to any previously established interests or projects. There's no clear indication of specific beverages they prefer, but it hints at a potential interest in personal wellness or lifestyle choices. A gentle follow-up question could be: 'Are you looking for recommendations for a particular type of drink, or are you trying to figure out what you usually enjoy?' 21 | * 用户对胡萝卜汁的个人偏好 :PERSONAL-PREFERENCE:MEMORY:AUTO: 22 | :PROPERTIES: 23 | :ID: DF7A0B8A-B3E7-49CB-A741-49772075E3EE 24 | :TIMESTAMP: [2025-09-24 18:10:06] 25 | :TYPE: insight 26 | :ACCESS_COUNT: 1 27 | :KEYWORDS: 胡萝卜汁, 个人偏好, 感官体验, 饮食习惯, 主观感受 28 | :TRIGGER: tier2-heuristic 29 | :END: 30 | 用户明确表达了对胡萝卜汁的不喜欢,但未提供具体原因或背景信息。对话未深入探讨这一偏好背后的原因或相关情境。 31 | 32 | * 用户对 Lisp 方言的偏好 :LISP:PREFERENCE: 33 | :PROPERTIES: 34 | :ID: A1B2C3D4-E5F6-4G7H-8I9J-K0L1M2N3O4P5 35 | :TIMESTAMP: [2025-09-25 11:00:00] 36 | :TYPE: insight 37 | :ACCESS_COUNT: 2 38 | :KEYWORDS: Lisp, Common Lisp, Scheme, programming, dialect, preference 39 | :TRIGGER: tier2-heuristic 40 | :END: 41 | 用户似乎更喜欢 Common Lisp 而不是 Scheme。 42 | 43 | * 关于咖啡的讨论 :COFFEE:DRINK: 44 | :PROPERTIES: 45 | :ID: B2C3D4E5-F6G7-4H8I-9J0K-L1M2N3O4P5Q6 46 | :TIMESTAMP: [2025-09-25 11:05:00] 47 | :TYPE: insight 48 | :ACCESS_COUNT: 3 49 | :KEYWORDS: coffee, drink, morning, caffeine, beverage 50 | :TRIGGER: tier2-heuristic 51 | :END: 52 | 用户提到早上需要喝咖啡来提神。 53 | 54 | * 项目截止日期 :PROJECT:DEADLINE: 55 | :PROPERTIES: 56 | :ID: C3D4E5F6-G7H8-4I9J-0K1L-M2N3O4P5Q6R7 57 | :TIMESTAMP: [2025-09-25 11:10:00] 58 | :TYPE: note 59 | :ACCESS_COUNT: 1 60 | :KEYWORDS: project, deadline, testing, work 61 | :TRIGGER: tier1-explicit 62 | :END: 63 | 项目需要在周五前完成所有测试。 64 | 65 | -------------------------------------------------------------------------------- /test/test-haip.el: -------------------------------------------------------------------------------- 1 | ;; test/test-workflow-parser.el --- Tests for HAIP Parser 2 | 3 | (require 'ert) 4 | (require 'superchat-workflow) ;; 假设你的解析器函数在这个文件里 5 | 6 | ;;;; Test Cases 7 | 8 | (ert-deftest test-haip-parser-happy-path () 9 | "Test parsing a typical, multi-step HAIP prompt." 10 | (let ((prompt " 11 | /AI技术新闻摘要 12 | description: 每周技术新闻摘要 13 | 14 | 帮我完成一份本周的AI技术新闻摘要。 15 | 16 | 首先,请使用 @SearchTool 这个工具来查找关于 #www.AI 和 #www.tech 的最新新闻,时间范围限定在过去7天内。 17 | 18 | 然后,将搜索到的结果,交给 @AnalystAgent 这个专业的分析Agent,让他执行 /summarize 任务,最后把结果保存到本地文件#local.md。 19 | ")) 20 | (expected-output 21 | '((:executor "@SearchTool" 22 | :actions nil 23 | :contexts '("#www.AI" "#www.tech") 24 | :prompt "首先,请使用 @SearchTool 这个工具来查找关于 #www.AI 和 #www.tech 的最新新闻,时间范围限定在过去7天内。") 25 | (:executor "@AnalystAgent" 26 | :actions '("/summarize") 27 | :contexts '("#local.md") 28 | :prompt "然后,将搜索到的结果,交给 @AnalystAgent 这个专业的分析Agent,让他执行 /summarize 任务,最后把结果保存到本地文件#local.md。"))) 29 | (actual-output (haip-parse-prompt prompt))) 30 | (should (equal actual-output expected-output))) 31 | 32 | (ert-deftest test-haip-parser-no-executor () 33 | "Test that text without an @executor is ignored." 34 | (let ((prompt "This is just a regular sentence without any commands.")) 35 | (should (null (haip-parse-prompt prompt))))) 36 | 37 | (ert-deftest test-haip-parser-empty-string () 38 | "Test that an empty string produces no steps." 39 | (should (null (haip-parse-prompt "")))) 40 | 41 | (ert-deftest test-haip-parser-single-simple-step () 42 | "Test a single line prompt with all components." 43 | (let ((prompt "Please @GPT4 summarize #doc.txt using /summary-template.") 44 | (expected-output 45 | '((:executor "@GPT4" 46 | :actions '("/summary-template") 47 | :contexts '("#doc.txt") 48 | :prompt "Please @GPT4 summarize #doc.txt using /summary-template.")))) 49 | (should (equal (haip-parse-prompt prompt) expected-output)))) 50 | 51 | (ert-deftest test-haip-parser-multiple-contexts-and-actions () 52 | "Test a step with multiple contexts and actions." 53 | (let ((prompt "@Tool process #file1 and #file2 with /action1 and /action2.") 54 | (expected-output 55 | '((:executor "@Tool" 56 | :actions '("/action1" "/action2") 57 | :contexts '("#file1" "#file2") 58 | :prompt "@Tool process #file1 and #file2 with /action1 and /action2.")))) 59 | (should (equal (haip-parse-prompt prompt) expected-output)))) 60 | 61 | (ert-deftest test-haip-parser-multiple-executors-in-one-block () 62 | "Test that only the first @executor in a block is used." 63 | (let ((prompt "Use @ToolA to do this, then ask @ToolB to do that.") 64 | (expected-output 65 | '((:executor "@ToolA" 66 | :actions nil 67 | :contexts nil 68 | :prompt "Use @ToolA to do this, then ask @ToolB to do that.")))) 69 | (should (equal (haip-parse-prompt prompt) expected-output)))) 70 | -------------------------------------------------------------------------------- /test/test-gptel-status.el: -------------------------------------------------------------------------------- 1 | ;;; test-gptel-status.el --- Test gptel backend status 2 | 3 | ;;; Commentary: 4 | ;; Test to understand gptel backend configuration 5 | 6 | ;;; Code: 7 | 8 | ;; Load required dependencies 9 | (load-file "../superchat-memory.el") 10 | (load-file "../superchat.el") 11 | 12 | (defun test-gptel-status () 13 | "Test gptel backend status and configuration." 14 | (interactive) 15 | (message "=== GPTel Status Test ===") 16 | 17 | ;; Check if gptel is available 18 | (if (require 'gptel nil t) 19 | (progn 20 | (message "✅ gptel package loaded") 21 | 22 | ;; Check gptel-backend variable 23 | (if (boundp 'gptel-backend) 24 | (progn 25 | (message "✅ gptel-backend is bound") 26 | (message " Type: %s" (type-of gptel-backend)) 27 | (message " Value: %S" gptel-backend)) 28 | (message "❌ gptel-backend is not bound")) 29 | 30 | ;; Check if gptel-backend is valid 31 | (when (and (boundp 'gptel-backend) gptel-backend) 32 | (if (gptel-backend-p gptel-backend) 33 | (message "✅ gptel-backend is valid") 34 | (message "❌ gptel-backend is not valid"))) 35 | 36 | ;; Check gptel-backend-models function 37 | (if (fboundp 'gptel-backend-models) 38 | (message "✅ gptel-backend-models function exists") 39 | (message "❌ gptel-backend-models function missing")) 40 | 41 | ;; Check what gptel-backend-models returns 42 | (when (and (boundp 'gptel-backend) 43 | gptel-backend 44 | (fboundp 'gptel-backend-models)) 45 | (condition-case nil 46 | (let ((models (gptel-backend-models gptel-backend))) 47 | (message "✅ gptel-backend-models returned: %S" models) 48 | (message " Type: %s" (type-of models)) 49 | (message " Length: %d" (if models (length models) 0))) 50 | (error 51 | (message "❌ Error calling gptel-backend-models: %s" (error-message-string err))))) 52 | 53 | ;; Check gptel--known-backends 54 | (if (boundp 'gptel--known-backends) 55 | (progn 56 | (message "✅ gptel--known-backends is bound") 57 | (message " Length: %d" (if gptel--known-backends (length gptel--known-backends) 0))) 58 | (message "❌ gptel--known-backends is not bound")) 59 | 60 | ;; Check if we can find the OpenAI backend 61 | (when (boundp 'gptel--known-backends) 62 | (let ((openai-backend (alist-get "ChatGPT" gptel--known-backends nil nil #'equal))) 63 | (if openai-backend 64 | (progn 65 | (message "✅ Found ChatGPT backend") 66 | (when (fboundp 'gptel-backend-models) 67 | (let ((models (gptel-backend-models openai-backend))) 68 | (message " ChatGPT models: %S" models)))) 69 | (message "❌ ChatGPT backend not found")))) 70 | 71 | ;; Test our superchat function 72 | (let ((superchat-models (superchat--get-available-models))) 73 | (message "✅ SuperChat models: %S" superchat-models))) 74 | 75 | ;; gptel not available 76 | (message "❌ gptel package not available"))) 77 | 78 | (message "=== GPTel Status Test Complete ===")) 79 | 80 | (test-gptel-status) 81 | 82 | ;;; test-gptel-status.el ends here -------------------------------------------------------------------------------- /test/final-verification-test.el: -------------------------------------------------------------------------------- 1 | ;;; final-verification-test.el --- Final verification of SuperChat gptel tools integration 2 | 3 | ;;; Commentary: 4 | ;; This test verifies the complete gptel tools integration works correctly. 5 | 6 | ;;; Code: 7 | 8 | (require 'superchat) 9 | 10 | ;; Mock gptel-tool structure for testing 11 | (defvar mock-gptel-tool 12 | (list :name "test_tool" 13 | :description "A test tool for verification" 14 | :function #'ignore 15 | :args '((:name "input" :type string))) 16 | "Mock gptel tool for testing.") 17 | 18 | (defun test-complete-integration () 19 | "Test the complete SuperChat gptel tools integration." 20 | (interactive) 21 | (message "=== Final Integration Verification ===") 22 | 23 | ;; Test 1: Basic function existence 24 | (message "\n1. Testing function existence...") 25 | (let ((required-functions '(superchat-get-gptel-tools 26 | superchat-gptel-tools-enabled-p 27 | superchat-tools-status))) 28 | (dolist (func required-functions) 29 | (if (fboundp func) 30 | (message " ✅ %s: exists" func) 31 | (message " ❌ %s: missing" func)))) 32 | 33 | ;; Test 2: Command registration 34 | (message "\n2. Testing command registration...") 35 | (superchat--load-user-commands) 36 | (if (assoc "/tools" superchat--builtin-commands) 37 | (message " ✅ /tools command: registered") 38 | (message " ❌ /tools command: not registered")) 39 | 40 | ;; Test 3: Tools detection with mock data 41 | (message "\n3. Testing tools detection...") 42 | (setq gptel-use-tools t) 43 | (setq gptel-tools (list mock-gptel-tool)) 44 | 45 | (let ((enabled (superchat-gptel-tools-enabled-p)) 46 | (tools (superchat-get-gptel-tools))) 47 | (message " ✅ Tools enabled: %s" enabled) 48 | (message " ✅ Tools detected: %d" (length tools)) 49 | (when tools 50 | (message " ✅ First tool: %s" (plist-get (car tools) :name)))) 51 | 52 | ;; Test 4: Tools status function 53 | (message "\n4. Testing tools status function...") 54 | (condition-case err 55 | (progn 56 | (superchat-tools-status) 57 | (message " ✅ Tools status function: works")) 58 | (error 59 | (message " ⚠️ Tools status function: %s" (error-message-string err)))) 60 | 61 | ;; Test 5: LLM function integration 62 | (message "\n5. Testing LLM integration...") 63 | (let ((gptel-request-called nil)) 64 | ;; Mock gptel-request to verify it gets called with tools 65 | (cl-letf (((symbol-function 'gptel-request) 66 | (lambda (&rest args) 67 | (setq gptel-request-called t) 68 | (message " ✅ gptel-request called with tools: %s" 69 | (plist-get args :tools))))) 70 | 71 | ;; This should call gptel-request with tools configured 72 | (superchat--llm-generate-answer "test prompt" nil nil) 73 | 74 | (if gptel-request-called 75 | (message " ✅ LLM integration: functional") 76 | (message " ❌ LLM integration: not functional")))) 77 | 78 | (message "\n=== Integration Verification Complete ===") 79 | (message "🎉 SuperChat gptel tools integration is ready!") 80 | (message "\nUsage:") 81 | (message "1. Configure gptel with your tools") 82 | (message "2. Start SuperChat: M-x superchat") 83 | (message "3. Check tools: /tools") 84 | (message "4. Enjoy tool-powered conversations!")) 85 | 86 | (provide 'final-verification-test) 87 | ;;; final-verification-test.el ends here -------------------------------------------------------------------------------- /superchat-parser.el: -------------------------------------------------------------------------------- 1 | ;;; superchat-parser.el --- Parsers for superchat symbols -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | ;; This file provides a set of pure functions for parsing superchat's special 5 | ;; symbols: @ for models, / for commands, and # for contexts. It is a 6 | ;; low-level library with no dependencies on other superchat modules. 7 | 8 | ;;; Code: 9 | 10 | (defconst superchat-parser--file-ref-regexp 11 | "#\\s-*\\(?:\"\\([^\"]+\\)\"\\|\\([~/][^[:space:]#]+\\)\\)" 12 | "Regexp to capture a file path after a leading '#'. 13 | Captures either a quoted path in group 1, or an unquoted absolute path in group 2.") 14 | 15 | (defconst superchat-parser--command-name-regexp 16 | "[^[:space:]/]+" 17 | "Regexp for command names used after a leading '/'. 18 | Matches any non-whitespace, non-slash characters (supports Unicode).") 19 | 20 | (defun superchat-parser--normalize-file-path (file-path) 21 | "Normalize FILE-PATH: unescape spaces, strip quotes, expand, trim newline." 22 | (when (and file-path (stringp file-path)) 23 | (let ((fp (replace-regexp-in-string "\\\\ " " " file-path))) 24 | (when (and (>= (length fp) 2) 25 | (string= "\"" (substring fp 0 1)) 26 | (string= "\"" (substring fp -1))) 27 | (setq fp (substring fp 1 -1))) 28 | (setq fp (expand-file-name fp)) 29 | (setq fp (replace-regexp-in-string "\n$" "" fp)) 30 | fp))) 31 | 32 | (defun superchat-parser-model-switch (input) 33 | "Parse input for @model syntax and return (clean-input . model) cons. 34 | If no @model syntax is found, return nil." 35 | (when (and input (string-match "@\\([a-zA-Z0-9_.:-]+\\)" input)) 36 | (let* ((model-name (match-string 1 input)) 37 | (clean-input (replace-regexp-in-string "@[a-zA-Z0-9_.:-]+" "" input))) 38 | (cons (string-trim clean-input) model-name)))) 39 | 40 | (defun superchat-parser-define (input) 41 | "Parse /define command input." 42 | (when (and input (stringp input)) 43 | (cond 44 | ((string-match (format "^/define\\s-+\\(%s\\)\\s-+\"\\(\\(?:.\\|\n\\)*?\\)\"\\s-*$" 45 | superchat-parser--command-name-regexp) 46 | input) 47 | (cons (match-string 1 input) (or (match-string 2 input) ""))) 48 | ((string-match (format "^/define\\s-+\\(%s\\)\\s-*$" 49 | superchat-parser--command-name-regexp) 50 | input) 51 | (cons (match-string 1 input) "")) 52 | (t nil)))) 53 | 54 | (defun superchat-parser-command (input) 55 | "Parse command input, return (command . args) or nil." 56 | (when (and input (stringp input)) 57 | ;; Support multi-line args: anything after the command (spaces or newline) 58 | ;; is treated as args and preserved. 59 | (if (string-match (format "^/\\(%s\\)\\(?:\\s-+\\(\\(?:.\\|\n\\)*\\)\\)?\\'" 60 | superchat-parser--command-name-regexp) 61 | input) 62 | (cons (or (match-string 1 input) "") 63 | (string-trim (or (match-string 2 input) ""))) 64 | nil))) 65 | 66 | (defun superchat-parser-extract-file-path (input) 67 | "Extract and normalize the first file path from INPUT string." 68 | (let (file-path) 69 | (when (and input (string-match superchat-parser--file-ref-regexp input)) 70 | (setq file-path (or (match-string 1 input) 71 | (match-string 2 input))) 72 | (setq file-path (superchat-parser--normalize-file-path file-path))) 73 | file-path)) 74 | 75 | (provide 'superchat-parser) 76 | 77 | ;;; superchat-parser.el ends here 78 | -------------------------------------------------------------------------------- /test/example-tools-usage.el: -------------------------------------------------------------------------------- 1 | ;;; example-tools-usage.el --- Example of using SuperChat with gptel tools 2 | 3 | ;;; Commentary: 4 | ;; This example demonstrates how to use SuperChat with gptel tools enabled. 5 | 6 | ;;; Code: 7 | 8 | (require 'superchat) 9 | 10 | ;; Example 1: Setting up gptel tools 11 | (defun example-setup-gptel-tools () 12 | "Example of setting up gptel tools for SuperChat." 13 | (interactive) 14 | 15 | ;; Configure gptel to use tools 16 | (setq gptel-use-tools t) 17 | 18 | ;; Define some example tools (you would replace these with actual tools) 19 | (setq gptel-tools 20 | (list 21 | ;; Example web search tool 22 | (gptel-make-tool 23 | :name "web_search" 24 | :function #'example-web-search 25 | :description "Search the web for information" 26 | :args '((:name "query" :type string :description "Search query")) 27 | :category "search") 28 | 29 | ;; Example file analysis tool 30 | (gptel-make-tool 31 | :name "analyze_file" 32 | :function #'example-analyze-file 33 | :description "Analyze a file's content" 34 | :args '((:name "file_path" :type string :description "Path to file")) 35 | :category "analysis"))) 36 | 37 | (message "✅ gptel tools configured with %d tools" (length gptel-tools))) 38 | 39 | ;; Example tool functions (simplified implementations) 40 | (defun example-web-search (query &rest args) 41 | "Example web search tool function." 42 | (message "🔍 Searching web for: %s" query) 43 | (format "Search results for '%s': [simulated results]" query)) 44 | 45 | (defun example-analyze-file (file-path &rest args) 46 | "Example file analysis tool function." 47 | (message "📁 Analyzing file: %s" file-path) 48 | (if (file-exists-p file-path) 49 | (format "File analysis for '%s': %d bytes, readable" 50 | file-path (file-attribute-size (file-attributes file-path))) 51 | (format "File '%s' does not exist" file-path))) 52 | 53 | ;; Example 2: Using SuperChat with tools 54 | (defun example-start-superchat-with-tools () 55 | "Start SuperChat with tools enabled." 56 | (interactive) 57 | 58 | ;; First set up tools 59 | (example-setup-gptel-tools) 60 | 61 | ;; Then start SuperChat 62 | (superchat) 63 | 64 | ;; Show instructions 65 | (message "🚀 SuperChat started with tools!") 66 | (message "💡 Try using the '/tools' command to see available tools") 67 | (message "💡 You can now ask questions that will trigger tool usage")) 68 | 69 | ;; Example 3: Check tools status 70 | (defun example-check-tools-status () 71 | "Example of checking tools status in SuperChat." 72 | (interactive) 73 | 74 | ;; Show gptel configuration 75 | (message "=== gptel Configuration ===") 76 | (message "gptel-use-tools: %s" (if (boundp 'gptel-use-tools) gptel-use-tools "Not set")) 77 | (message "gptel-tools count: %d" (if (boundp 'gptel-tools) (length gptel-tools) 0)) 78 | 79 | ;; Show SuperChat integration status 80 | (message "\n=== SuperChat Integration ===") 81 | (message "Tools enabled in SuperChat: %s" (superchat-gptel-tools-enabled-p)) 82 | (message "Tools available to SuperChat: %d" (length (superchat-get-gptel-tools))) 83 | 84 | ;; Show /tools command usage 85 | (message "\n=== Usage ===") 86 | (message "1. Start SuperChat: M-x superchat") 87 | (message "2. Check tools: /tools") 88 | (message "3. Ask questions that may trigger tools") 89 | (message "Example: 'Search the web for Emacs tips'")) 90 | 91 | (provide 'example-tools-usage) 92 | ;;; example-tools-usage.el ends here -------------------------------------------------------------------------------- /test/test-real-gptel-models.el: -------------------------------------------------------------------------------- 1 | ;;; test-real-gptel-models.el --- Test with actual gptel package 2 | 3 | ;;; Commentary: 4 | ;; Test real model detection by loading actual gptel package 5 | 6 | ;;; Code: 7 | 8 | (require 'cl-lib) 9 | 10 | ;; Add gptel to load path 11 | (add-to-list 'load-path "/Users/chenyibin/.emacs.d/straight/repos/gptel") 12 | 13 | ;; Load gptel package 14 | (load "/Users/chenyibin/.emacs.d/straight/repos/gptel/gptel.el") 15 | 16 | ;; Load required dependencies 17 | (load-file "../superchat-memory.el") 18 | (load-file "../superchat.el") 19 | 20 | (defun test-real-gptel-models () 21 | "Test with actual gptel package loaded." 22 | (interactive) 23 | (message "=== Testing with Real GPTel Package ===") 24 | 25 | ;; Verify gptel is loaded 26 | (if (featurep 'gptel) 27 | (message "✅ gptel package loaded successfully") 28 | (message "❌ gptel package not loaded")) 29 | 30 | ;; Check gptel-backend 31 | (if (boundp 'gptel-backend) 32 | (progn 33 | (message "✅ gptel-backend is bound") 34 | (message " Backend type: %s" (type-of gptel-backend)) 35 | (when (fboundp 'gptel-backend-name) 36 | (let ((backend-name (gptel-backend-name gptel-backend))) 37 | (message " Backend name: %s" backend-name)))) 38 | (message "❌ gptel-backend not bound")) 39 | 40 | ;; Check gptel--known-backends 41 | (if (boundp 'gptel--known-backends) 42 | (progn 43 | (message "✅ gptel--known-backends available") 44 | (message " Number of backends: %d" (length gptel--known-backends)) 45 | (dolist (backend gptel--known-backends) 46 | (message " - %s" (car backend)))) 47 | (message "❌ gptel--known-backends not available")) 48 | 49 | ;; Test gptel-backend-models function 50 | (if (fboundp 'gptel-backend-models) 51 | (message "✅ gptel-backend-models function available") 52 | (message "❌ gptel-backend-models function not available")) 53 | 54 | ;; Get actual models from current backend 55 | (when (and (boundp 'gptel-backend) 56 | gptel-backend 57 | (fboundp 'gptel-backend-models)) 58 | (condition-case err 59 | (let ((backend-models (gptel-backend-models gptel-backend))) 60 | (message "✅ Real backend models:") 61 | (message " Count: %d" (length backend-models)) 62 | (dotimes (i (min 10 (length backend-models))) 63 | (let ((model (nth i backend-models))) 64 | (message " %d. %s (%s)" (1+ i) 65 | (if (symbolp model) (symbol-name model) model) 66 | (if (symbolp model) "symbol" "string"))))) 67 | (error 68 | (message "❌ Error getting backend models: %s" (error-message-string err))))) 69 | 70 | ;; Test our SuperChat function with real gptel 71 | (message "") 72 | (message "SuperChat model detection results:") 73 | (let ((superchat-models (superchat--get-available-models))) 74 | (message "✅ SuperChat models (%d): %S" (length superchat-models) superchat-models) 75 | 76 | ;; Show if we got real models 77 | (if (not (equal superchat-models '("default"))) 78 | (progn 79 | (message "🎉 SUCCESS: Got real models from gptel!") 80 | (message "First 5 models:") 81 | (dotimes (i (min 5 (length superchat-models))) 82 | (message " %d. %s" (1+ i) (nth i superchat-models)))) 83 | (message "⚠️ Still getting 'default' - investigating..."))) 84 | 85 | ;; Test model completion 86 | (message "") 87 | (message "Testing model completion:") 88 | (with-temp-buffer 89 | (let ((superchat--prompt-start (point-min))) 90 | (erase-buffer) 91 | (insert "@") 92 | (goto-char (point-max)) 93 | (let ((result (superchat--completion-at-point))) 94 | (if result 95 | (let ((candidates (nth 2 result))) 96 | (message "✅ @ completion candidates: %d" (length candidates)) 97 | (when (> (length candidates) 0) 98 | (message "First 3 candidates: %S" (cl-subseq candidates 0 3)))) 99 | (message "❌ @ completion failed"))))) 100 | 101 | (message "") 102 | (message "=== Real GPTel Test Complete ===")) 103 | 104 | (test-real-gptel-models) 105 | 106 | ;;; test-real-gptel-models.el ends here -------------------------------------------------------------------------------- /test/test-model-switching.el: -------------------------------------------------------------------------------- 1 | ;;; test-model-switching.el --- Test SuperChat @ model switching functionality 2 | 3 | ;;; Commentary: 4 | ;; This test file verifies the @ model switching feature works correctly. 5 | 6 | ;;; Code: 7 | 8 | ;; Load required dependencies in correct order 9 | (load-file "../superchat-memory.el") 10 | (load-file "../superchat.el") 11 | 12 | ;; Test 1: Model parsing function 13 | (defun test-model-parsing () 14 | "Test the @ model syntax parsing." 15 | (interactive) 16 | (message "=== Testing @ Model Syntax Parsing ===") 17 | 18 | (let ((test-cases '( 19 | ("@gpt-4o Hello world" . ("Hello world" . "gpt-4o")) 20 | ("@claude-3-5-sonnet How are you?" . ("How are you?" . "claude-3-5-sonnet")) 21 | ("No model syntax here" . nil) 22 | ("@gemini-pro Multiple words here" . ("Multiple words here" . "gemini-pro")) 23 | ("@model123 with-dashes and_underscores" . ("with-dashes and_underscores" . "model123")) 24 | ("Trailing spaces @gpt-4 " . ("Trailing spaces" . "gpt-4")) 25 | ))) 26 | 27 | (dolist (test-case test-cases) 28 | (let* ((input (car test-case)) 29 | (expected (cdr test-case)) 30 | (result (superchat--parse-model-switch input))) 31 | (if (equal result expected) 32 | (message "✅ \"%s\" → %S" input result) 33 | (message "❌ \"%s\" → expected %S, got %S" input expected result)))))) 34 | 35 | ;; Test 2: Available models function 36 | (defun test-available-models () 37 | "Test the available models function." 38 | (interactive) 39 | (message "=== Testing Available Models ===") 40 | 41 | (let ((models (superchat--get-available-models))) 42 | (message "✅ Found %d available models: %S" (length models) models) 43 | (when (member "default" models) 44 | (message "✅ Default model included")) 45 | (when (> (length models) 0) 46 | (message "✅ Models list is not empty")))) 47 | 48 | ;; Test 3: Model list function 49 | (defun test-model-list () 50 | "Test the model list display function." 51 | (interactive) 52 | (message "=== Testing Model List Function ===") 53 | (condition-case err 54 | (progn 55 | (superchat-model-list) 56 | (message "✅ Model list function executed successfully")) 57 | (error 58 | (message "❌ Model list function failed: %s" (error-message-string err))))) 59 | 60 | ;; Test 4: Integration test with mock scenarios 61 | (defun test-model-switching-integration () 62 | "Test complete model switching integration." 63 | (interactive) 64 | (message "=== Testing Model Switching Integration ===") 65 | 66 | ;; Test parsing and execution flow (without actually calling LLM) 67 | (let* ((input "@gpt-4o test message") 68 | (parsed (superchat--parse-model-switch input)) 69 | (clean-input (when parsed (car parsed))) 70 | (target-model (when parsed (cdr parsed)))) 71 | 72 | (if parsed 73 | (progn 74 | (message "✅ Parsed input: \"%s\"" input) 75 | (message "✅ Clean input: \"%s\"" clean-input) 76 | (message "✅ Target model: \"%s\"" target-model) 77 | 78 | ;; Test that the model is in available list 79 | (let ((available-models (superchat--get-available-models))) 80 | (if (member target-model available-models) 81 | (message "✅ Target model \"%s\" is in available models list" target-model) 82 | (message "⚠️ Target model \"%s\" not in available models list: %S" 83 | target-model available-models)))) 84 | (message "❌ Failed to parse @ model syntax from: \"%s\"" input)))) 85 | 86 | ;; Run all tests 87 | (defun test-superchat-model-switching () 88 | "Run all SuperChat model switching tests." 89 | (interactive) 90 | (message "=== SuperChat @ Model Switching Test Suite ===") 91 | (test-model-parsing) 92 | (test-available-models) 93 | (test-model-list) 94 | (test-model-switching-integration) 95 | (message "=== Model Switching Testing Complete ===") 96 | (message "\nUsage Summary:") 97 | (message "• Use @model_name syntax to switch models for a single query") 98 | (message "• Use /models command to see available models") 99 | (message "• Example: @gpt-4o What is Emacs?")) 100 | 101 | (provide 'test-model-switching) 102 | ;;; test-model-switching.el ends here -------------------------------------------------------------------------------- /test/test-model-completion-fixed.el: -------------------------------------------------------------------------------- 1 | ;;; test-model-completion.el --- Test @ model completion functionality 2 | 3 | ;;; Code: 4 | 5 | ;; Load required dependencies in correct order 6 | (load-file "../superchat-memory.el") 7 | (load-file "../superchat.el") 8 | 9 | ;; Test the completion function 10 | (defun test-model-completion-function () 11 | "Test the @ model completion CAPF function." 12 | (interactive) 13 | (message "=== Testing @ Model Completion Function ===") 14 | 15 | ;; Create a temporary buffer to test completion 16 | (with-temp-buffer 17 | (let ((superchat-buffer-name (current-buffer)) 18 | (superchat--prompt-start (point-marker))) 19 | 20 | ;; Test 1: No @ symbol 21 | (insert "hello world") 22 | (goto-char (point-max)) 23 | (let ((result (superchat--completion-at-point))) 24 | (if result 25 | (message "❌ Test 1 failed: Expected nil for input without @, got %S" result) 26 | (message "✅ Test 1 passed: No completion for input without @"))) 27 | 28 | ;; Test 2: @ symbol with no prefix 29 | (erase-buffer) 30 | (insert "@") 31 | (setq superchat--prompt-start (point-min)) 32 | (goto-char (point-max)) 33 | (let ((result (superchat--completion-at-point))) 34 | (if result 35 | (let ((start (nth 0 result)) 36 | (end (nth 1 result)) 37 | (candidates (nth 2 result))) 38 | (message "✅ Test 2 passed: @ completion available") 39 | (message " Start: %d, End: %d" start end) 40 | (message " Candidates: %S" candidates) 41 | (when candidates 42 | (message "✅ Test 2a passed: Found %d model candidates" (length candidates)))) 43 | (message "❌ Test 2 failed: No completion for @ symbol")))) 44 | 45 | ;; Test 3: @ symbol with partial prefix 46 | (erase-buffer) 47 | (insert "@gp") 48 | (setq superchat--prompt-start (point-min)) 49 | (goto-char (point-max)) 50 | (let ((result (superchat--completion-at-point))) 51 | (if result 52 | (let ((candidates (nth 2 result))) 53 | (message "✅ Test 3 passed: @gp completion available") 54 | (message " Candidates: %S" candidates)) 55 | (message "❌ Test 3 failed: No completion for @gp"))))) 56 | 57 | ;; Test available models function 58 | (defun test-available-models-for-completion () 59 | "Test the available models function for completion." 60 | (interactive) 61 | (message "=== Testing Available Models for Completion ===") 62 | 63 | (let ((models (superchat--get-available-models))) 64 | (message "✅ Found %d models for completion: %S" (length models) models) 65 | 66 | ;; Test that models are strings 67 | (let ((all-strings (cl-every #'stringp models))) 68 | (if all-strings 69 | (message "✅ All model names are strings") 70 | (message "❌ Some model names are not strings"))) 71 | 72 | ;; Test that there are no empty model names 73 | (let ((no-empty (cl-notany #'string-empty-p models))) 74 | (if no-empty 75 | (message "✅ No empty model names") 76 | (message "❌ Found empty model names"))))) 77 | 78 | ;; Test command completion still works 79 | (defun test-command-completion-still-works () 80 | "Test that command completion still works after adding @ completion." 81 | (interactive) 82 | (message "=== Testing Command Completion Still Works ===") 83 | 84 | (with-temp-buffer 85 | (let ((superchat-buffer-name (current-buffer)) 86 | (superchat--prompt-start (point-min))) 87 | 88 | ;; Test command completion 89 | (insert "/to") 90 | (goto-char (point-max)) 91 | (let ((result (superchat--completion-at-point))) 92 | (if result 93 | (let ((candidates (nth 2 result))) 94 | (message "✅ Command completion still works") 95 | (message " /to candidates: %S" candidates)) 96 | (message "❌ Command completion broken")))))) 97 | 98 | ;; Run all tests 99 | (defun test-superchat-completion () 100 | "Run all SuperChat completion tests." 101 | (interactive) 102 | (message "=== SuperChat Completion Test Suite ===") 103 | (test-model-completion-function) 104 | (test-available-models-for-completion) 105 | (test-command-completion-still-works) 106 | (message "=== Completion Testing Complete ===") 107 | (message "\nUsage:") 108 | (message "• Type @ and use TAB or M-TAB to complete model names") 109 | (message "• Type / and use TAB or M-TAB to complete commands") 110 | (message "• Works with company-mode, corfu, or standard completion")) 111 | 112 | (provide 'test-model-completion) 113 | ;;; test-model-completion.el ends here -------------------------------------------------------------------------------- /test/test-superchat-mcp.el: -------------------------------------------------------------------------------- 1 | ;;; test-superchat-mcp.el --- Test SuperChat MCP integration 2 | 3 | ;;; Commentary: 4 | ;; Test MCP integration functionality in SuperChat 5 | 6 | ;;; Code: 7 | 8 | (require 'cl-lib) 9 | 10 | ;; Add mcp to load path 11 | (add-to-list 'load-path "/Users/chenyibin/.emacs.d/straight/repos/mcp.el") 12 | 13 | ;; Load required dependencies 14 | (load-file "../superchat-memory.el") 15 | (load-file "../superchat.el") 16 | 17 | (defun test-superchat-mcp-basic () 18 | "Test basic MCP functionality." 19 | (interactive) 20 | (message "=== Testing SuperChat MCP Basic Functions ===") 21 | 22 | ;; Test 1: Check MCP availability 23 | (let ((mcp-available (superchat-mcp-available-p))) 24 | (message "✅ MCP available: %s" (if mcp-available "Yes" "No")) 25 | 26 | (when mcp-available 27 | ;; Test 2: Check server counts 28 | (let ((configured (superchat-mcp-get-server-count)) 29 | (running (superchat-mcp-get-running-server-count))) 30 | (message "✅ Servers configured: %d" configured) 31 | (message "✅ Servers running: %d" running)) 32 | 33 | ;; Test 3: Get MCP tools 34 | (let ((mcp-tools (superchat-mcp-get-tools))) 35 | (message "✅ MCP tools count: %d" (if mcp-tools (length mcp-tools) 0)) 36 | (when mcp-tools 37 | (message "First 3 MCP tools:") 38 | (dotimes (i (min 3 (length mcp-tools))) 39 | (let ((tool (nth i mcp-tools))) 40 | (message " %d. %s: %s" (1+ i) 41 | (plist-get tool :name) 42 | (or (plist-get tool :description) "No description")))))))) 43 | 44 | (message "=== MCP Basic Test Complete ===")) 45 | 46 | (defun test-superchat-mcp-commands () 47 | "Test MCP command integration." 48 | (interactive) 49 | (message "=== Testing SuperChat MCP Commands ===") 50 | 51 | ;; Test 1: Check if MCP commands are in built-in commands 52 | (let ((builtin-commands superchat--builtin-commands)) 53 | (message "✅ Built-in commands count: %d" (length builtin-commands)) 54 | 55 | (let ((mcp-cmd (assoc "/mcp" builtin-commands)) 56 | (mcp-start-cmd (assoc "/mcp-start" builtin-commands))) 57 | (if mcp-cmd 58 | (message "✅ /mcp command found: %s" (cdr mcp-cmd)) 59 | (message "❌ /mcp command not found")) 60 | 61 | (if mcp-start-cmd 62 | (message "✅ /mcp-start command found: %s" (cdr mcp-start-cmd)) 63 | (message "❌ /mcp-start command not found")))) 64 | 65 | ;; Test 2: Test command completion 66 | (let ((all-commands (superchat--get-all-command-names))) 67 | (message "✅ Total commands available: %d" (length all-commands)) 68 | 69 | (let ((mcp-commands (cl-remove-if-not 70 | (lambda (cmd) (string-prefix-p "/mcp" cmd)) 71 | all-commands))) 72 | (if mcp-commands 73 | (progn 74 | (message "✅ MCP commands found: %d" (length mcp-commands)) 75 | (dolist (cmd mcp-commands) 76 | (message " • %s" cmd))) 77 | (message "❌ No MCP commands found in completion")))) 78 | 79 | (message "=== MCP Commands Test Complete ===")) 80 | 81 | (defun test-superchat-mcp-integration () 82 | "Test MCP integration with gptel tools." 83 | (interactive) 84 | (message "=== Testing SuperChat MCP Integration ===") 85 | 86 | ;; Test gptel tools integration 87 | (let ((gptel-tools (superchat-get-gptel-tools))) 88 | (message "✅ gptel tools count: %d" (length gptel-tools)) 89 | 90 | ;; Check if MCP tools are included in gptel tools 91 | (let ((mcp-tools (cl-remove-if-not 92 | (lambda (tool) 93 | (and (plist-get tool :category) 94 | (string-prefix-p "mcp-" (plist-get tool :category)))) 95 | gptel-tools))) 96 | (if mcp-tools 97 | (progn 98 | (message "✅ Found %d MCP tools integrated with gptel:" (length mcp-tools)) 99 | (dolist (tool (cl-subseq mcp-tools 0 (min 3 (length mcp-tools)))) 100 | (message " • %s (category: %s)" 101 | (plist-get tool :name) 102 | (plist-get tool :category)))) 103 | (message "⚠️ No MCP tools currently integrated with gptel")))) 104 | 105 | ;; Test if MCP tools are automatically available 106 | (when (superchat-mcp-servers-running-p) 107 | (message "✅ MCP servers are running - tools should be available")) 108 | 109 | (message "=== MCP Integration Test Complete ===")) 110 | 111 | (defun test-superchat-mcp-comprehensive () 112 | "Run comprehensive MCP tests." 113 | (interactive) 114 | (message "=== Comprehensive SuperChat MCP Test ===") 115 | (test-superchat-mcp-basic) 116 | (test-superchat-mcp-commands) 117 | (test-superchat-mcp-integration) 118 | (message "=== Comprehensive MCP Testing Complete ===")) 119 | 120 | ;; Run the comprehensive test 121 | (test-superchat-mcp-comprehensive) 122 | 123 | (provide 'test-superchat-mcp) 124 | ;;; test-superchat-mcp.el ends here -------------------------------------------------------------------------------- /test/test-model-completion.el: -------------------------------------------------------------------------------- 1 | ;;; test-model-completion.el --- Test @ model completion functionality 2 | 3 | ;;; Code: 4 | 5 | ;; Load required dependencies in correct order 6 | (load-file "../superchat-memory.el") 7 | (load-file "../superchat.el") 8 | 9 | ;; Test the completion function 10 | (defun test-model-completion-function () 11 | "Test the @ model completion CAPF function." 12 | (interactive) 13 | (message "=== Testing @ Model Completion Function ===") 14 | 15 | ;; Create a temporary buffer to test completion 16 | (with-temp-buffer 17 | (let ((superchat-buffer-name (current-buffer)) 18 | (superchat--prompt-start (point-marker))) 19 | 20 | ;; Test 1: No @ symbol 21 | (insert "hello world") 22 | (goto-char (point-max)) 23 | (let ((result (superchat--completion-at-point))) 24 | (if result 25 | (message "❌ Test 1 failed: Expected nil for input without @, got %S" result) 26 | (message "✅ Test 1 passed: No completion for input without @"))) 27 | 28 | ;; Test 2: @ symbol with no prefix 29 | (erase-buffer) 30 | (insert "@") 31 | (setq superchat--prompt-start (point-min)) 32 | (goto-char (point-max)) 33 | (let ((result (superchat--completion-at-point))) 34 | (if result 35 | (let ((start (nth 0 result)) 36 | (end (nth 1 result)) 37 | (candidates (nth 2 result))) 38 | (message "✅ Test 2 passed: @ completion available") 39 | (message " Start: %d, End: %d" start end) 40 | (message " Candidates: %S" candidates) 41 | (when candidates 42 | (message "✅ Test 2a passed: Found %d model candidates" (length candidates)))) 43 | (message "❌ Test 2 failed: No completion for @ symbol")))) 44 | 45 | ;; Test 3: @ symbol with partial prefix 46 | (erase-buffer) 47 | (insert "@gp") 48 | (setq superchat--prompt-start (point-min)) 49 | (goto-char (point-max)) 50 | (let ((result (superchat--completion-at-point))) 51 | (if result 52 | (let ((candidates (nth 2 result))) 53 | (message "✅ Test 3 passed: @gp completion available") 54 | (message " Candidates: %S" candidates)) 55 | (message "❌ Test 3 failed: No completion for @gp")))) 56 | 57 | ;; Test 4: Multiple @ symbols (should only complete the last one) 58 | (erase-buffer) 59 | (insert "email@domain @gpt") 60 | (setq superchat--prompt-start (point-min)) 61 | (goto-char (point-max)) 62 | (let ((result (superchat--completion-at-point))) 63 | (if result 64 | (let ((start (nth 0 result)) 65 | (candidates (nth 2 result))) 66 | (message "✅ Test 4 passed: Multiple @ symbols handled correctly") 67 | (message " Completion starts at: %d (should be after space)" start) 68 | (message " Candidates: %S" candidates)) 69 | (message "❌ Test 4 failed: No completion for multiple @ symbols"))))))) 70 | 71 | ;; Test available models function 72 | (defun test-available-models-for-completion () 73 | "Test the available models function for completion." 74 | (interactive) 75 | (message "=== Testing Available Models for Completion ===") 76 | 77 | (let ((models (superchat--get-available-models))) 78 | (message "✅ Found %d models for completion: %S" (length models) models) 79 | 80 | ;; Test that models are strings 81 | (let ((all-strings (cl-every #'stringp models))) 82 | (if all-strings 83 | (message "✅ All model names are strings") 84 | (message "❌ Some model names are not strings"))) 85 | 86 | ;; Test that there are no empty model names 87 | (let ((no-empty (cl-notany #'string-empty-p models))) 88 | (if no-empty 89 | (message "✅ No empty model names") 90 | (message "❌ Found empty model names"))))) 91 | 92 | ;; Test command completion still works 93 | (defun test-command-completion-still-works () 94 | "Test that command completion still works after adding @ completion." 95 | (interactive) 96 | (message "=== Testing Command Completion Still Works ===") 97 | 98 | (with-temp-buffer 99 | (let ((superchat-buffer-name (current-buffer)) 100 | (superchat--prompt-start (point-min))) 101 | 102 | ;; Test command completion 103 | (insert "/to") 104 | (goto-char (point-max)) 105 | (let ((result (superchat--completion-at-point))) 106 | (if result 107 | (let ((candidates (nth 2 result))) 108 | (message "✅ Command completion still works") 109 | (message " /to candidates: %S" candidates)) 110 | (message "❌ Command completion broken")))))) 111 | 112 | ;; Run all tests 113 | (defun test-superchat-completion () 114 | "Run all SuperChat completion tests." 115 | (interactive) 116 | (message "=== SuperChat Completion Test Suite ===") 117 | (test-model-completion-function) 118 | (test-available-models-for-completion) 119 | (test-command-completion-still-works) 120 | (message "=== Completion Testing Complete ===") 121 | (message "\nUsage:") 122 | (message "• Type @ and use TAB or M-TAB to complete model names") 123 | (message "• Type / and use TAB or M-TAB to complete commands") 124 | (message "• Works with company-mode, corfu, or standard completion")) 125 | 126 | (provide 'test-model-completion) 127 | ;;; test-model-completion.el ends here -------------------------------------------------------------------------------- /test/test-completion-realistic.el: -------------------------------------------------------------------------------- 1 | ;;; test-completion-realistic.el --- Realistic test for @ completion 2 | 3 | ;;; Code: 4 | 5 | ;; Load required dependencies 6 | (load-file "../superchat-memory.el") 7 | (load-file "../superchat.el") 8 | 9 | ;; Mock different backend scenarios 10 | (defun test-with-different-backends () 11 | "Test model completion with different backend configurations." 12 | (interactive) 13 | (message "=== Testing with Different Backend Scenarios ===") 14 | 15 | ;; Test 1: No backend configured 16 | (let ((gptel-backend nil)) 17 | (let ((models (superchat--get-available-models))) 18 | (message "✅ No backend: %S" models))) 19 | 20 | ;; Test 2: Mock ChatGPT backend 21 | (cl-letf (((symbol-function 'gptel-backend-name) 22 | (lambda (_) "ChatGPT"))) 23 | (let ((models (superchat--get-available-models))) 24 | (message "✅ ChatGPT backend: %S" models))) 25 | 26 | ;; Test 3: Mock Claude backend 27 | (cl-letf (((symbol-function 'gptel-backend-name) 28 | (lambda (_) "Claude"))) 29 | (let ((models (superchat--get-available-models))) 30 | (message "✅ Claude backend: %S" models)))) 31 | 32 | ;; Test actual completion behavior 33 | (defun test-completion-behavior () 34 | "Test actual completion behavior in realistic scenarios." 35 | (interactive) 36 | (message "=== Testing Realistic Completion Behavior ===") 37 | 38 | (with-temp-buffer 39 | (let ((superchat--prompt-start (point-min))) 40 | 41 | ;; Test 1: @ at beginning of line 42 | (erase-buffer) 43 | (insert "@") 44 | (goto-char (point-max)) 45 | (let ((result (superchat--completion-at-point))) 46 | (if result 47 | (message "✅ @ at beginning: completion available (%d candidates)" 48 | (length (nth 2 result))) 49 | (message "❌ @ at beginning: no completion"))) 50 | 51 | ;; Test 2: @ with partial text 52 | (erase-buffer) 53 | (insert "help me @gpt") 54 | (goto-char (point-max)) 55 | (let ((result (superchat--completion-at-point))) 56 | (if result 57 | (let ((start (nth 0 result)) 58 | (candidates (nth 2 result))) 59 | (message "✅ @ with partial: starts at %d, %d candidates" 60 | start (length candidates))) 61 | (message "❌ @ with partial: no completion"))) 62 | 63 | ;; Test 3: Multiple @ symbols (should complete last one) 64 | (erase-buffer) 65 | (insert "email@example.com @claude") 66 | (goto-char (point-max)) 67 | (let ((result (superchat--completion-at-point))) 68 | (if result 69 | (let ((start (nth 0 result))) 70 | (message "✅ Multiple @: completes at position %d (should be after space)" start) 71 | (if (> start 16) ; After "email@example.com " 72 | (message "✅ Multiple @: correct completion position") 73 | (message "❌ Multiple @: wrong completion position"))) 74 | (message "❌ Multiple @: no completion"))) 75 | 76 | ;; Test 4: No completion for regular text 77 | (erase-buffer) 78 | (insert "hello world") 79 | (goto-char (point-max)) 80 | (let ((result (superchat--completion-at-point))) 81 | (if (null result) 82 | (message "✅ Regular text: no completion (correct)") 83 | (message "❌ Regular text: unexpected completion")))))) 84 | 85 | ;; Test edge cases 86 | (defun test-edge-cases () 87 | "Test edge cases for completion." 88 | (interactive) 89 | (message "=== Testing Edge Cases ===") 90 | 91 | (with-temp-buffer 92 | (let ((superchat--prompt-start (point-min))) 93 | 94 | ;; Test 1: Empty input 95 | (erase-buffer) 96 | (let ((result (superchat--completion-at-point))) 97 | (if (null result) 98 | (message "✅ Empty input: no completion") 99 | (message "❌ Empty input: unexpected completion"))) 100 | 101 | ;; Test 2: Just @ symbol 102 | (erase-buffer) 103 | (insert "@") 104 | (goto-char (point-max)) 105 | (let ((result (superchat--completion-at-point))) 106 | (if result 107 | (message "✅ Just @: completion available") 108 | (message "❌ Just @: no completion"))) 109 | 110 | ;; Test 3: @ followed by space 111 | (erase-buffer) 112 | (insert "@ ") 113 | (goto-char (point-max)) 114 | (let ((result (superchat--completion-at-point))) 115 | (if (null result) 116 | (message "✅ @ space: no completion (correct)") 117 | (message "❌ @ space: unexpected completion"))) 118 | 119 | ;; Test 4: @ at end of sentence 120 | (erase-buffer) 121 | (insert "What do you think @") 122 | (goto-char (point-max)) 123 | (let ((result (superchat--completion-at-point))) 124 | (if result 125 | (message "✅ @ at sentence end: completion available") 126 | (message "❌ @ at sentence end: no completion")))))) 127 | 128 | ;; Run comprehensive test 129 | (defun test-completion-comprehensive () 130 | "Run comprehensive completion tests." 131 | (interactive) 132 | (message "=== Comprehensive SuperChat Completion Test ===") 133 | (test-with-different-backends) 134 | (test-completion-behavior) 135 | (test-edge-cases) 136 | (message "=== Comprehensive Testing Complete ===") 137 | (message "\nTo test interactively:") 138 | (message "1. M-x superchat") 139 | (message "2. Type @ and press TAB") 140 | (message "3. Type / and press TAB") 141 | (message "4. Verify both completion systems work")) 142 | 143 | (provide 'test-completion-realistic) 144 | ;;; test-completion-realistic.el ends here -------------------------------------------------------------------------------- /test/superchat-memory-merge-debug.el: -------------------------------------------------------------------------------- 1 | ;;; superchat-memory-merge-debug.el --- Debug merging algorithm -*- lexical-binding: t; -*- 2 | 3 | (require 'cl-lib) 4 | (require 'package) 5 | (package-initialize) 6 | 7 | (let ((root (file-name-directory (or load-file-name buffer-file-name)))) 8 | (add-to-list 'load-path (expand-file-name ".." root))) 9 | 10 | (require 'org) 11 | (require 'subr-x) 12 | 13 | (defvar superchat--user-commands (make-hash-table :test #'equal) 14 | "Stub user command registry for debug runs when superchat.el is absent.") 15 | 16 | (require 'superchat-memory) 17 | 18 | (unless (fboundp 'gptel-request) 19 | (defun gptel-request (&rest _args) 20 | (message "superchat-memory-merge-debug: stubbed gptel-request invoked") 21 | nil)) 22 | 23 | (unless (fboundp 'gptel-request-sync) 24 | (defun gptel-request-sync (&rest _args) 25 | (message "superchat-memory-merge-debug: stubbed gptel-request-sync invoked") 26 | "IGNORE")) 27 | 28 | (defun superchat-memory-merge-debug--sample-memory () 29 | "Return sample memory org contents used for debugging merges." 30 | (string-join 31 | '("* Deployment Runbook" 32 | ":PROPERTIES:" 33 | ":ID: debug-entry-a" 34 | ":TIMESTAMP: [2024-05-01]" 35 | ":KEYWORDS: deployment, ops, release" 36 | ":TAGS: OPERATIONS" 37 | ":END:" 38 | "Documenting deployment checklist and ownership hand-offs." 39 | "" 40 | "* Incident Review" 41 | ":PROPERTIES:" 42 | ":ID: debug-entry-b" 43 | ":TIMESTAMP: [2024-05-02]" 44 | ":KEYWORDS: ops, release, postmortem" 45 | ":TAGS: OPERATIONS" 46 | ":END:" 47 | "Summary of the last outage and mitigation playbook." 48 | "" 49 | "* Frontend UI polish" 50 | ":PROPERTIES:" 51 | ":ID: debug-entry-c" 52 | ":TIMESTAMP: [2024-05-03]" 53 | ":KEYWORDS: frontend, ui, design" 54 | ":TAGS: PRODUCT" 55 | ":END:" 56 | "Notes from a UI review covering layout adjustments." 57 | "" 58 | "* UX feedback sync" 59 | ":PROPERTIES:" 60 | ":ID: debug-entry-d" 61 | ":TIMESTAMP: [2024-05-04]" 62 | ":KEYWORDS: design, ux, research" 63 | ":TAGS: PRODUCT" 64 | ":END:" 65 | "User testing insights and qualitative feedback.") 66 | "\n")) 67 | 68 | (defun superchat-memory-merge-debug--collect-entries (file) 69 | "Return active memory entries stored in FILE." 70 | (with-current-buffer (find-file-noselect file) 71 | (org-with-wide-buffer 72 | (goto-char (point-min)) 73 | (let (entries) 74 | (while (re-search-forward org-heading-regexp nil t) 75 | (let* ((element (org-element-at-point)) 76 | (entry (superchat-memory--element-to-plist element)) 77 | (tags (plist-get entry :tags))) 78 | (unless (member "ARCHIVED" tags) 79 | (push entry entries)))) 80 | (nreverse entries))))) 81 | 82 | (defun superchat-memory-merge-debug--group-with-logging (entries threshold) 83 | "Return merge groups for ENTRIES logging pairwise scores versus THRESHOLD." 84 | (let ((groups '()) 85 | (processed-ids '())) 86 | (dolist (entry1 entries) 87 | (let ((id1 (plist-get entry1 :id))) 88 | (unless (member id1 processed-ids) 89 | (message "\n== Base entry: %s (%s)" id1 (plist-get entry1 :title)) 90 | (message " Keywords: %s" (plist-get entry1 :keywords)) 91 | (let ((group (list entry1))) 92 | (dolist (entry2 entries) 93 | (let ((id2 (plist-get entry2 :id))) 94 | (unless (or (equal id1 id2) 95 | (member id2 processed-ids)) 96 | (let* ((keywords1 (plist-get entry1 :keywords)) 97 | (keywords2 (plist-get entry2 :keywords)) 98 | (score (superchat-memory--keyword-jaccard keywords1 keywords2))) 99 | (message " -> Compare with %s (%s)" id2 (plist-get entry2 :title)) 100 | (message " Keywords: %s" keywords2) 101 | (message " Jaccard score: %.2f (threshold %.2f)" score threshold) 102 | (if (>= score threshold) 103 | (progn 104 | (message " => include in merge group") 105 | (push entry2 group) 106 | (push id2 processed-ids)) 107 | (message " => skip")))))) 108 | (when (> (length group) 1) 109 | (message "-- Formed group with %d entries" (length group)) 110 | (push group groups)) 111 | (push id1 processed-ids))))) 112 | (nreverse groups))) 113 | 114 | (defun superchat-memory-merge-debug-run () 115 | "Generate debug output for the merge candidate discovery algorithm." 116 | (interactive) 117 | (let* ((temp-dir (make-temp-file "superchat-merge-debug" t)) 118 | (superchat-data-directory temp-dir) 119 | (superchat-memory-file (expand-file-name "memory.org" temp-dir)) 120 | (superchat-memory-merge-similarity-threshold 0.5)) 121 | (with-temp-file superchat-memory-file 122 | (insert (superchat-memory-merge-debug--sample-memory))) 123 | (message "superchat-memory-merge-debug: created sample memory file at %s" superchat-memory-file) 124 | (let* ((entries (superchat-memory-merge-debug--collect-entries superchat-memory-file)) 125 | (groups (superchat-memory-merge-debug--group-with-logging entries superchat-memory-merge-similarity-threshold))) 126 | (message "\n== Debug merge run complete ==") 127 | (if groups 128 | (dolist (group groups) 129 | (message "Group: %s" 130 | (mapconcat 131 | (lambda (entry) 132 | (format "%s (%s)" 133 | (plist-get entry :id) 134 | (plist-get entry :title))) 135 | group 136 | ", "))) 137 | (message "No groups met the threshold.")) 138 | (condition-case err 139 | (let ((actual (superchat-memory--find-merge-candidates))) 140 | (message "\n== superchat-memory--find-merge-candidates result ==") 141 | (if actual 142 | (dolist (group actual) 143 | (message "Candidate group: %s" 144 | (mapconcat 145 | (lambda (entry) 146 | (format "%s (%s)" 147 | (plist-get entry :id) 148 | (plist-get entry :title))) 149 | group 150 | ", "))) 151 | (message "Function returned no candidates."))) 152 | (error 153 | (message 154 | "superchat-memory-merge-debug: could not invoke original merge finder: %s" 155 | (error-message-string err))))))) 156 | 157 | (provide 'superchat-memory-merge-debug) 158 | ;;; superchat-memory-merge-debug.el ends here 159 | 160 | -------------------------------------------------------------------------------- /docs/memory-design.org: -------------------------------------------------------------------------------- 1 | #+TITLE: Superchat Memory System Design: Org-mode + org-ql 2 | 3 | * Core Idea: An Org-mode-based Memory System 4 | 5 | This document outlines a design for a persistent, queryable memory system 6 | for Superchat, based on Org-mode and `org-ql`. 7 | 8 | * Rationale ("Good Taste") 9 | 10 | This approach is considered to have "good taste" for several reasons: 11 | 12 | - **Superior Data Structure**: Org-mode files are human-readable, 13 | machine-parseable, version-controllable (via Git), and backed by a 14 | powerful query language (`org-ql`). 15 | - **Leverages Existing Tools**: Instead of reinventing a query engine, 16 | this design uses the robust, existing `org-ql` library. 17 | - **Transparency and Control**: The agent's memory is stored in plain 18 | text, allowing the user to inspect, edit, and manage it directly. 19 | - **Language-Agnostic**: By delegating semantic understanding to the LLM 20 | on the write path, the system does not require any language-specific 21 | configuration (like segmenters), making it universally applicable. 22 | 23 | * Proposed Architecture 24 | 25 | The system is a form of Retrieval-Augmented Generation (RAG). It separates 26 | its workload into an intelligent, asynchronous "Write Path" and a simple, 27 | instantaneous "Read Path". 28 | 29 | ** 1. Data Schema: The Org Memory Entry 30 | 31 | Each piece of memory will be stored as an Org-mode headline with a 32 | pre-defined structure. 33 | 34 | *** Example Schema: 35 | 36 | #+begin_src org 37 | * LLM: Refactored completion logic to fix state corruption 38 | :PROPERTIES: 39 | :ID: 40 | :TIMESTAMP: 41 | :TYPE: :technical_solution: 42 | :ACCESS_COUNT: 1 43 | :KEYWORDS: "fix, solve, repair, completion, autocompleter, bug, error, issue, state corruption, copy-list, destructive modification" 44 | :RELATED: 45 | :END: 46 | :TAGS: &bugfix:completion:lisp: 47 | 48 | The root cause was the destructive modification of a literal constant 49 | list... The fix is to use `(mapcar #'identity ...)` to create a copy 50 | before sorting. 51 | #+end_src 52 | 53 | *** Key Components: 54 | - [x] *Headline*: A concise summary of the memory chunk. 55 | - [x] *TAGS*: For high-level, efficient filtering. 56 | - [x] *PROPERTIES*: 57 | - [x] `:ID:`: A unique identifier for linking between entries. 58 | - [x] `:TIMESTAMP:`: For time-based queries. 59 | - [x] `:TYPE:`: The semantic type of the memory. 60 | - [x] `:ACCESS_COUNT:`: A score representing the utility of the memory, 61 | used for the forgetting mechanism. 62 | - [x] `:KEYWORDS:`: A rich, multilingual, LLM-generated list of 63 | keywords and synonyms, serving as a search index. 64 | - [x] `:RELATED:`: Links to other memory IDs, forming a knowledge graph. 65 | 66 | ** 2. Workflow: The Read-Write Loop 67 | 68 | *** Write Path: A Hybrid Trigger Strategy for Memory Generation 69 | 70 | To ensure a flexible and intelligent memory creation process, the system 71 | will employ a tiered, hybrid trigger mechanism. This combines user 72 | control with automated intelligence. 73 | 74 | - [x] **Tier 1 (High Priority): Explicit User Commands** 75 | - *Mechanism*: The system actively scans user input for imperative 76 | keywords (e.g., "Remember...", "记住..."). 77 | - *Behavior*: When a command is detected, the relevant content is 78 | extracted and saved as a high-priority memory. Its initial 79 | `:ACCESS_COUNT:` can be set higher than other memories. 80 | 81 | - [x] **Tier 2 (Medium Priority): Automatic LLM Identification** 82 | - *Mechanism*: After a significant interaction, an asynchronous 83 | background task poses a "meta-question" to the LLM: "Did the 84 | previous exchange contain any key facts, decisions, or user 85 | preferences worth committing to long-term memory? If yes, summarize 86 | them into one or more standard memory entries; otherwise, answer 'No'." 87 | - *Behavior*: This allows the system to proactively capture important 88 | information that the user did not explicitly flag. 89 | 90 | - [ ] **Tier 3 (Low Priority): Manual Post-hoc Command** 91 | - *Mechanism*: A command like `/remember-last` is provided. 92 | - *Next step*: Wire this command into the chat UI so it extracts the last 93 | conversation turn and hands it to `superchat-memory-capture-conversation`. 94 | - *Behavior*: This allows the user to retroactively save the content 95 | of the previous user-agent exchange to memory, providing a 96 | convenient fallback. 97 | 98 | Following any of these triggers, the standard enrichment process occurs: 99 | 100 | - **Asynchronous Enrichment**: After an entry is saved, a background 101 | task sends its content to an LLM to generate a rich, multilingual 102 | list of keywords and synonyms, which are then used to update the 103 | entry's `:KEYWORDS:` property. 104 | 105 | *** Read Path: High-Speed, Local Retrieval 106 | 107 | This path is designed to be instantaneous, with no network calls. 108 | 109 | - [x] When a new user query is received, a retrieval step is initiated. 110 | - [x] A **simple, universal, and fast local keyword extractor** processes 111 | the user's query. This involves basic, language-agnostic 112 | tokenization (e.g., splitting by whitespace and punctuation) and 113 | removing common stop words. 114 | - [x] The extracted keywords are used to construct a dynamic `org-ql` query 115 | that primarily searches against the pre-computed `:KEYWORDS:` property. 116 | - [x] The query is executed locally. Because it searches against a 117 | pre-built index (`:KEYWORDS:`), it is both fast and 'intelligent'. 118 | - [ ] The top-ranked results (based on a local relevance score and the 119 | entry's `:ACCESS_COUNT:`) are collected and formatted. 120 | - *Next step*: Implement a ranking function that considers keyword matches, 121 | recency, and `:ACCESS_COUNT:` to produce an ordered shortlist. 122 | - [ ] This retrieved context is prepended to the final prompt sent to the 123 | LLM to generate the answer. 124 | - *Next step*: Integrate `superchat-memory-retrieve` into `superchat.el` so the 125 | formatted snippets are injected into the prompt payload. 126 | 127 | * Memory Lifecycle Management: Merging and Forgetting 128 | 129 | A memory system that only grows will eventually become bloated and slow. A 130 | robust system must include mechanisms for consolidating redundant data and 131 | pruning irrelevant data. 132 | 133 | ** 1. Merging Similar/Duplicate Memories 134 | 135 | *Problem*: Over time, semantically similar or duplicate entries will be 136 | created. 137 | 138 | *Solution*: Periodic, LLM-driven Memory Review. 139 | 1. *Candidate Selection*: A background task uses `org-ql` to find 140 | entries with overlapping `:KEYWORDS:` or that were created in a short 141 | time window. 142 | 2. *LLM Arbitration*: A batch of similar entries is sent to the LLM, 143 | which is prompted to either merge them into a single, more 144 | comprehensive entry, or identify them as distinct. 145 | 3. *Execution*: The new, merged entry is created. Old entries are not 146 | deleted but are tagged as `:ARCHIVED:` and linked to the new entry, 147 | preserving data integrity. 148 | 149 | ** 2. Forgetting Mechanism 150 | 151 | *Problem*: To maintain performance and relevance, the active memory set 152 | must be managed. "Forgetting" should be based on utility, not just age. 153 | 154 | *** Strategy A: Merit-based Scoring 155 | A new property, `:ACCESS_COUNT:`, is added to the schema. Every time a 156 | memory is retrieved and used, its score is incremented. A periodic task 157 | applies a decay factor to all scores to ensure relevance over time. 158 | 159 | *** Strategy B: Archiving, Not Deleting (Tiered Storage) 160 | This is the primary strategy for "forgetting," enabled by the scoring 161 | system. 162 | 1. *Storage Tiers*: The system uses at least two files: 163 | `memory_active.org` (hot data) and `memory_archive.org` (cold data). 164 | 2. *Archiving Policy*: A periodic task moves entries with an 165 | `:ACCESS_COUNT:` below a certain threshold from the active file to the 166 | archive file. 167 | 3. *Query Policy*: Regular queries only target the active file for 168 | performance. A "deep search" command can be used to query the 169 | archive when needed. This ensures no data is ever truly lost. 170 | 171 | * Risks and Future Challenges 172 | 173 | - **Prompt Engineering**: The quality of the system depends heavily on the 174 | prompt used in the asynchronous 'Write Path' to generate high-quality 175 | keywords. 176 | - **Retrieval Quality**: The effectiveness of the retrieval depends on the 177 | local ranking algorithm that scores and selects the top results from 178 | the `org-ql` output. 179 | - **Concurrency**: The background task that updates `:KEYWORDS:` must be 180 | managed carefully to avoid conflicts with user edits or other 181 | processes. 182 | 183 | * Next Steps (Action Plan) 184 | 185 | Based on the code review, the following tasks must be completed to deliver a 186 | functional v1.0 memory system. They are listed in order of priority. 187 | 188 | 1. *[X] Implement Core Retrieval Logic* 189 | - [X] **Ranking Function**: Implement the `superchat-memory--rank-entries` 190 | function. It must use the defined weight variables (`title-weight`, 191 | `recency-weight`, etc.) to score and sort retrieval results. 192 | - [X] **Access Count**: Implement the `superchat-memory--touch-access-counts` 193 | function to increment the `:ACCESS_COUNT:` of retrieved entries. This is 194 | critical for the ranking and forgetting mechanisms. 195 | 196 | 2. *[X] Integrate with Superchat* 197 | - [X] **Prompt Injection**: Modify `superchat.el` to call 198 | `superchat-memory-retrieve` and inject the top-ranked memory snippets 199 | into the LLM prompt. This makes the memory system useful. 200 | 201 | 3. *[X] Implement User-Facing Features* 202 | - [X] **Tier 3 Command**: Implement the `/remember-last` command to allow 203 | manual, post-hoc memory capture. 204 | 205 | 4. *[X] Implement Memory Lifecycle Management* 206 | - [X] **Decay and Archiving**: Create a periodic function to apply a decay 207 | factor to `:ACCESS_COUNT:` and move low-scoring entries from the 208 | active memory file to an archive file. 209 | -------------------------------------------------------------------------------- /README_cn.md: -------------------------------------------------------------------------------- 1 | # SuperChat 2 | 3 | ## 简介 4 | 5 | 一个为 gptel 打造的、上手友好的 Claude Code 风格聊天界面。Superchat 让结构化提示与文件上下文对话变得简单——无需新增基础设施,只用你的编辑器即可。 6 | 7 | - “/” 调用命令(支持补全),并且可以轻松创建自定义命令。 8 | - “#” 在消息中直接附上文件,作为发送给 LLM 的一部分。 9 | - 流式响应与可读输出,聊天体验清爽快捷。 10 | - 与你现有的 gptel 配置即插即用。 11 | 12 | 主要特性包括: 13 | - 保留完整的命令系统 14 | - 支持将文件作为上下文添加到对话(包含文本内联) 15 | - 支持与多种大型语言模型(LLM)对话 16 | - 使用 GPL-3 协议开源 17 | 18 | ## 安装与配置 19 | 20 | ### 依赖要求 21 | 22 | - Emacs 27.1 或更高版本 23 | - [gptel](https://github.com/karthink/gptel) 包 24 | - [gptel-context](https://github.com/karthink/gptel) 包(gptel 的一部分) 25 | 26 | ### 安装步骤 27 | 28 | 1. 将 `superchat.el` 文件放置在您的 Emacs 加载路径中 29 | 2. 在您的 Emacs 配置文件(通常是 `~/.emacs.d/init.el`)中添加以下代码: 30 | 31 | ```elisp 32 | (require 'superchat) 33 | ``` 34 | 35 | ### 基本配置 36 | 37 | Superchat 旨在与 `gptel` 无缝集成。所有与大型语言模型(LLM)相关的设置,例如模型(`gptel-model`)、API 密钥和温度等,都会自动从您的 `gptel` 配置中继承。如需配置 AI 的行为,请直接自定义 `gptel` 的相关变量。 38 | 39 | 您可以通过以下变量配置 Superchat 自身的功能: 40 | 41 | ```elisp 42 | ;; 设置数据存储目录 43 | (setq superchat-data-directory "~/.emacs.d/superchat/") 44 | 45 | ;; 设置自定义命令中 $lang 变量的语言 46 | (setq superchat-lang "中文") ; 或 "English", "Français" 等 47 | 48 | ;; 响应超时保护(防止阻塞工具导致 UI 卡死) 49 | (setq superchat-response-timeout 30) ; 秒,设为 nil 可禁用 50 | 51 | ;; 智能完成检测延迟(用于非流式响应) 52 | ;; 主要用于 Ollama + tools 模式 53 | (setq superchat-completion-check-delay 2) ; 秒,默认为 2 54 | 55 | ;; 设置文件选择的默认目录 56 | (setq superchat-default-directories '("~/Documents" "~/Projects")) 57 | ``` 58 | 59 | 注意:Superchat 现在会自动管理其目录结构。`superchat-save-directory` 和 `superchat-command-dir` 变量已被移除。目录现在会根据需要动态创建,或者您可以使用 `M-x superchat-ensure-directories` 手动确保所有目录都存在。 60 | 61 | ## 快速开始 62 | 63 | 1. 使用 `M-x superchat` 启动 Superchat 64 | 2. 在提示符后输入您的问题。您可以按 `RET` 来输入多行内容。 65 | 3. 按 `C-c C-c` 发送消息。 66 | 4. 等待 AI 助手回复 67 | 68 | ### 基本对话示例 69 | 70 | ``` 71 | User: 你好,你能帮我做什么? 72 | Assistant: 我是一个 AI 助手,可以帮助您回答问题、分析代码、提供建议等。 73 | ``` 74 | 75 | ## 核心功能详解 76 | 77 | ### 基本对话 78 | 79 | Superchat 允许您与各种大型语言模型进行自然语言对话。 80 | 81 | ### 命令系统 82 | 83 | Superchat 提供了一个强大的命令系统,允许您定义和使用自定义提示词: 84 | 85 | - 内置命令:如 `/create-question` 等 86 | - 自定义命令定义:使用 `/define` 命令 87 | - 查看所有可用命令:使用 `/commands` 命令 88 | 89 | 在输入提示符后,您可以使用自动补全功能来查看和选择命令: 90 | 1. 输入 `/` 字符 91 | 2. 继续输入命令的首字母,此时如果你有用 company 或 corfu 插件,从弹出的列表中选择命令。 92 | 3. 如果你没有 company 或 corfu,可以直接按下 `M-TAB`(或您的 Emacs 环境中的补全键)触发自动补全,从弹出的列表中选择命令。 93 | 94 | 例如,要使用 `/create-question` 命令,您可以输入 `/c` 然后按 `M-TAB`,系统会显示所有以 'c' 开头的可用命令供您选择。 95 | 96 | ### 文件上下文支持 97 | 98 | Superchat 可以将文件内容作为上下文添加到对话中: 99 | 100 | 1. 在输入提示符后按 `#` 键 101 | 2. 选择要添加的文件 102 | 3. 文件内容将作为上下文提供给 AI 103 | 104 | 您也可以手动输入文件路径,格式为 `# /path/to/file`。 105 | 106 | 当设置了 `superchat-default-directories` 时,文件选择仅显示默认目录中的一级文件(不递归子目录),并按扩展名过滤:`org/md/txt/webp/png/jpg/jpeg`,便于快速定位常用文本与图片文件。 107 | 108 | ### gptel Tools 集成 109 | 110 | Superchat 现在完全支持 gptel 的 tools(工具调用)功能,让 AI 可以直接调用外部工具和服务来增强对话能力。 111 | 112 | 主要特性包括: 113 | - **零配置集成**:自动读取您在 gptel 中配置的 tools,无需重复设置 114 | - **智能工具调用**:AI 根据您的需求自动判断并使用合适的工具 115 | - **无缝体验**:工具调用结果自然融入对话流程 116 | - **智能完成检测**:自动处理非流式工具响应(Ollama + tools) 117 | - **状态查看**:使用 `/tools` 命令查看当前可用的工具状态 118 | 119 | 使用方法: 120 | 1. 在 gptel 中配置您的 tools: 121 | ```elisp 122 | (setq gptel-use-tools t) 123 | (setq gptel-tools (list ...)) 124 | ``` 125 | 2. 启动 Superchat 并正常对话 126 | 3. 当您的需求需要工具时,AI 会自动调用相应工具 127 | 4. 使用 `/tools` 命令查看工具状态 128 | 129 | 示例对话: 130 | ``` 131 | 用户: 帮我搜索最新的 Emacs 新闻 132 | AI: [自动调用 web_search 工具] 我为您找到了最新的 Emacs 新闻... 133 | ``` 134 | 135 | #### Ollama + Tools 技术说明 136 | 137 | 在使用 Ollama 并启用 tools 时,存在已知的流式输出限制: 138 | 139 | 1. **gptel 的行为**:gptel 会对 Ollama + tools 模式禁用流式输出([参见 gptel-request.el:2019](https://github.com/karthink/gptel/blob/master/gptel-request.el#L2019)) 140 | ```elisp 141 | ;; HACK(tool): no stream if Ollama + tools. Need to find a better way 142 | (not (and (eq (type-of gptel-backend) 'gptel-ollama) 143 | gptel-tools gptel-use-tools)) 144 | ``` 145 | 146 | 2. **Ollama 的问题**:Ollama 的工具调用流式实现存在已知问题([ollama/ollama#12557](https://github.com/ollama/ollama/issues/12557)) 147 | 148 | 3. **Superchat 的解决方案**:实现了智能完成检测机制: 149 | - 在收到响应后等待 `superchat-completion-check-delay`(默认 2 秒) 150 | - 如果没有新数据到达,则认为响应已完成 151 | - 自动进入下一轮对话 152 | - 如有需要则回退到 30 秒超时保护 153 | 154 | **配置方法**: 155 | ```elisp 156 | ;; 根据需要调整完成检测延迟 157 | ;; 更低的值 = 更快完成,更高的值 = 更可靠 158 | (setq superchat-completion-check-delay 2) ; 推荐 1-5 秒 159 | ``` 160 | 161 | **注意**:这仅影响 Ollama + tools 模式。正常流式对话会通过标准信号立即完成。 162 | 163 | ### 工作流自动化 164 | 165 | 工作流就像把多轮对话录成“脚本”。一次指令即可完成搜索、分析、保存等步骤,再也不用手动重复。 166 | 167 | - **聊天里直接调用**:输入 `>workflow-name 主题` 即可运行同名 `.workflow` 文件,自动复用你在 gptel 中已经启用的工具与 MCP 服务器。 168 | - **按需组合步骤**:可以继续使用 `#文件路径`、`@model`、`/write-file` 等语法,把文件、模型切换、结果保存串联起来。 169 | - **一次配置,长期使用**:把工作流文件放在 `~/.emacs.d/superchat/workflow/`(或 `superchat-data-directory/workflow/`),想用随时调用。 170 | - **简单线性执行**:每一行非空内容就是一个步骤,按顺序逐行执行。目前暂不支持 n8n 那样的分支/条件,请使用直线流程。 171 | 172 | 试试看: 173 | 1. 新建 `~/.emacs.d/superchat/workflow/ai-news-summary.workflow`,写入以下内容。 174 | 2. 在 Superchat 中输入 `>ai-news-summary AI技术`(或任何关键词)。 175 | 3. 工作流会自动检索新闻、生成摘要,并把 Markdown 报告写入你指定的文件。 176 | 177 | ```text 178 | # Workflow: AI技术新闻摘要 179 | # Description: 每周技术新闻摘要 180 | 181 | /web-search 搜索关于 "$input" 相关的新闻 182 | 183 | @qwen3-coder:30b-a3b-q8_0 分析上面(3 个角度:商业,技术,社会)搜索到的新闻信息,生成一份简洁的中文的新闻摘要 184 | 185 | 将分析结果保存到 #/Users/chenyibin/Documents/news-summary.md 186 | ``` 187 | 188 | ### MCP (Model Context Protocol) 集成 189 | 190 | Superchat 现在集成了 MCP (Model Context Protocol) 支持,让您能够通过标准化的协议连接各种外部服务和工具。MCP 提供了一个统一的接口来管理不同服务器提供的工具,大大扩展了 AI 的能力边界。 191 | 192 | 主要特性包括: 193 | - **零配置架构**:自动检测并集成 MCP 服务器,无需手动配置 194 | - **实时状态监控**:显示服务器连接状态和可用工具数量 195 | - **无缝工具集成**:MCP 工具自动融入 gptel 的工具系统 196 | - **智能服务器管理**:支持启动、停止和监控多个 MCP 服务器 197 | 198 | #### 安装和配置 199 | 200 | 1. **安装 MCP 包**: 201 | ```elisp 202 | ;; 使用 straight.el 安装 203 | (straight-use-package 'mcp) 204 | 205 | ;; 或者使用 use-package 206 | (use-package mcp) 207 | ``` 208 | 209 | 2. **配置 MCP 服务器**(在您的 Emacs 配置中): 210 | ```elisp 211 | ;; 示例:配置多个 MCP 服务器(含可选 Jina API Key) 212 | (setq mcp-hub-servers 213 | (let ((jina-token (getenv "JINA_API_KEY"))) 214 | `(("filesystem" . (:command "npx" 215 | :args ("-y" "@modelcontextprotocol/server-filesystem" 216 | "/Users/yourname/Documents"))) 217 | ("fetch" . (:command "uvx" :args ("mcp-server-fetch"))) 218 | ("jina-mcp-server" 219 | . (:url "https://mcp.jina.ai/sse" 220 | :headers ,(when (and jina-token (> (length jina-token) 0)) 221 | `((Authorization . ,(concat "Bearer " jina-token))))))))) 222 | ``` 223 | 224 | #### 使用方法 225 | 226 | **查看 MCP 状态**: 227 | - 使用 `/mcp` 命令查看当前 MCP 状态 228 | - 显示已配置服务器数量、运行中服务器数量和可用工具数量 229 | 230 | **启动 MCP 服务器**: 231 | - 使用 `/mcp-start` 命令启动 MCP 服务器 232 | - 系统会自动检测并启动已配置但未运行的服务器 233 | - 启动的工具会自动集成到当前的 gptel 会话中 234 | 235 | **示例对话**: 236 | ``` 237 | 用户: /mcp 238 | 系统: MCP 状态: 可用 ✓ | 已配置: 1 个服务器 | 运行中: 1 个服务器 | 可用工具: 15 个 239 | 240 | 用户: /mcp-start 241 | 系统: 正在启动 MCP 服务器... 242 | 已启动服务器: filesystem 243 | 新增 15 个工具到 gptel 会话 244 | 245 | 用户: 帮我列出 Documents 目录中的重要文件 246 | AI: [使用 MCP 文件系统工具] 我为您找到了 Documents 目录中的重要文件... 247 | ``` 248 | 249 | #### 支持的 MCP 命令 250 | 251 | - `/mcp` - 显示 MCP 状态和服务器信息 252 | - `/mcp-start` - 启动 MCP 服务器并集成工具 253 | 254 | #### 常见 MCP 服务器 255 | 256 | 以下是几个常用的 MCP 服务器示例: 257 | 258 | ```elisp 259 | ;; 文件系统服务器 260 | (setq mcp-hub-servers 261 | '(("filesystem" . (:command "npx" 262 | :args ("-y" "@modelcontextprotocol/server-filesystem" "/path/to/directory"))))) 263 | 264 | ;; GitHub 服务器 265 | '("github" . (:command "npx" 266 | :args ("-y" "@modelcontextprotocol/server-github") 267 | :env ("GITHUB_PERSONAL_ACCESS_TOKEN" . "your_token_here"))) 268 | 269 | ;; SQLite 数据库服务器 270 | '("sqlite" . (:command "npx" 271 | :args ("-y" "@modelcontextprotocol/server-sqlite" "path/to/database.db"))) 272 | 273 | ;; Web 搜索服务器 274 | '("brave-search" . (:command "npx" 275 | :args ("-y" "@modelcontextprotocol/server-brave-search") 276 | :env ("BRAVE_API_KEY" . "your_api_key"))) 277 | ``` 278 | 279 | 注意:MCP 功能需要安装 `mcp.el` 包。如果未安装,相关命令会显示友好的错误提示。 280 | 281 | ### 记忆系统 282 | 283 | Superchat 现在拥有一个持久化且可查询的记忆系统,允许 AI 记住过去的对话并在未来的交互中利用这些知识。该系统基于 Org-mode 文件构建,确保了透明度和用户控制。 284 | 285 | 主要特性包括: 286 | - **分层记忆捕获**:自动捕获对话中的重要见解(经 LLM 总结),并允许用户通过明确命令保存记忆。 287 | - **智能检索**:使用从您的查询中提取的 LLM 关键词来查找最相关的记忆,然后将其作为上下文提供给 AI。 288 | - **记忆生命周期管理**: 289 | - **评分与衰减**:记忆根据其效用进行评分,分数随时间推移而衰减。 290 | - **自动归档**:不那么相关的记忆会自动移动到归档文件,保持活跃记忆的精简。 291 | - **自动合并**:相似或重复的记忆可以由 LLM 自动合并为单个、更全面的条目。(注意:此功能默认开启,每天合并一次。) 292 | 293 | 记忆系统的配置选项可在 `M-x customize-group RET superchat-memory RET` 中找到。 294 | 295 | ## 高级用法 296 | 297 | ### 自定义命令 298 | 299 | 您可以使用 `/define` 命令创建自定义提示词: 300 | 301 | ``` 302 | /define explain-code "请解释以下代码的作用:$input" 303 | ``` 304 | 305 | 除了使用 `/define` 命令,您还可以通过更直接的方式创建自定义命令:只需在您的 `superchat-data-directory` 下的 `command` 目录中添加提示词文件。文件名(不含扩展名)将自动成为命令名。默认的文件扩展名是 `.prompt`,但也支持如 `.md`、`.org` 和 `.txt` 等其他格式。 306 | 307 | **添加命令描述:** 308 | 为了在 `/commands` 列表中显示命令的用途,请使用 `命令名-描述文本.prompt` 的格式命名文件。Superchat 会自动解析第一个连字符之后的内容作为描述。 309 | 310 | 示例: 311 | - `summarize.prompt` -> 命令:`/summarize` (无描述) 312 | - `seo-优化网站内容.prompt` -> 命令:`/seo`,描述:"优化网站内容" 313 | 314 | 文件的内容(例如 `请总结以下文本:$input`)即为提示词模板。 315 | 316 | 在自定义提示词中,您可以使用以下变量: 317 | - `$input`:用户的输入内容 318 | - `$lang`:设置的语言(默认为 English) 319 | 320 | #### 设置 $lang 变量的语言 321 | 322 | 自定义命令中的 `$lang` 变量可以通过以下几种方式配置: 323 | 324 | **方法一:通过 Emacs 自定义界面(推荐)** 325 | ```elisp 326 | M-x customize-variable RET superchat-lang RET 327 | ``` 328 | 然后将值从 "English" 改为您偏好的语言(如 "中文"、"Français"、"Español")并保存设置。 329 | 330 | **方法二:在配置文件中设置** 331 | 在您的 Emacs 配置文件中添加: 332 | ```elisp 333 | (setq superchat-lang "中文") ; 中文 334 | (setq superchat-lang "English") ; 英文 335 | (setq superchat-lang "Français") ; 法文 336 | (setq superchat-lang "Español") ; 西班牙文 337 | ``` 338 | 339 | **方法三:临时设置(当前会话)** 340 | 在 Emacs 中执行: 341 | ```elisp 342 | M-x eval-expression RET (setq superchat-lang "中文") RET 343 | ``` 344 | 345 | **使用示例:** 346 | 当您设置 `(setq superchat-lang "中文")` 并使用内置的 `/create-question` 命令时: 347 | - 模板:`"Please list all important questions related to $input in $lang."` 348 | - 输入 "git" 后:`"Please list all important questions related to git in 中文."` 349 | 350 | 语言设置在每次发送消息时都会动态获取,因此您可以随时更改语言设置,立即生效。 351 | 352 | ### 上下文管理 353 | 354 | - 使用 `/clear-context` 命令清除当前会话的所有上下文文件 355 | - Superchat 会自动管理添加到会话中的文件 356 | 357 | ### 键绑定 358 | 359 | - `RET` 或 `C-c C-c`:发送输入 360 | - `#`:智能添加文件路径到上下文 361 | - `C-c C-h`:显示命令列表 362 | - `C-c C-s`:保存当前会话 363 | - `/tools`:查看当前 gptel tools 状态 364 | - `/mcp`:查看 MCP 状态和服务器信息 365 | - `/mcp-start`:启动 MCP 服务器并集成工具 366 | 367 | ## 配置选项 368 | 369 | 以下是 Superchat 的主要自定义选项: 370 | 371 | - `superchat-buffer-name`:聊天缓冲区的名称(默认为 "*Superchat*") 372 | - `superchat-data-directory`:数据存储目录 373 | - `superchat-lang`:自定义命令中 `$lang` 变量的语言设置(默认为 "English") 374 | - `superchat-display-single-window`:如果非 nil,则 Superchat 窗口将占据整个 Emacs 框架,提供一个专注的"单窗口"视图。默认开启。 375 | - `superchat-default-directories`:文件选择的默认目录列表 376 | - `superchat-general-answer-prompt`:通用回答提示词模板 377 | - `superchat-context-message-count`:在提示词中包含的最近消息数量。 378 | - `superchat-conversation-history-limit`:在会话中保留在内存中的最大消息数量。 379 | 380 | ### 记忆系统配置 381 | 382 | 这些选项控制 Superchat 的记忆系统。您可以通过 `M-x customize-group RET superchat-memory RET` 进行自定义。 383 | 384 | - `superchat-memory-file`:主记忆 Org 文件的路径(默认为数据目录中的 `memory.org`)。 385 | - `superchat-memory-archive-file`:归档记忆 Org 文件的路径(默认为数据目录中的 `memory-archive.org`)。 386 | - `superchat-memory-explicit-trigger-patterns`:Tier 1 显式记忆命令的正则表达式模式。 387 | - `superchat-memory-auto-capture-enabled`:启用/禁用自动记忆捕获(Tier 2)。 388 | - `superchat-memory-auto-capture-minimum-length`:触发自动捕获的用户消息最小长度。 389 | - `superchat-memory-use-org-ql-cache`:启用/禁用记忆查询的 `org-ql` 缓存。 390 | - `superchat-memory-max-results`:每次检索返回的最大记忆数量。 391 | - `superchat-memory-auto-recall-min-length`:自动记忆检索运行前的最小查询长度。 392 | - `superchat-memory-auto-increment-access-count`:自动增加检索到的记忆的 `:ACCESS_COUNT:`。 393 | - `superchat-memory-decay-factor`:应用于衰减期间访问计数的乘数。 394 | - `superchat-memory-decay-min-access`:衰减后访问计数的下限。 395 | - `superchat-memory-archive-threshold`:归档记忆的访问计数阈值。 396 | - `superchat-memory-auto-prune-interval-days`:自动记忆修剪的间隔天数(0 或 `nil` 禁用)。 397 | - `superchat-memory-merge-similarity-threshold`:合并记忆的 Jaccard 相似度阈值。 398 | - `superchat-memory-auto-merge-interval-days`:自动记忆合并的间隔天数(0 或 `nil` 禁用)。警告:此功能存在不正确合并的风险。 399 | 400 | ### 新增函数 401 | 402 | - `superchat-ensure-directories`:交互式函数,用于手动确保所有必要的目录都存在。使用方法:`M-x superchat-ensure-directories` 403 | 404 | ## 故障排除 405 | 406 | ### 常见问题 407 | 408 | 1. **无法连接到 AI 服务**:请检查您的 gptel 配置是否正确,包括 API 密钥和端点设置。 409 | 410 | 2. **文件上下文未正确添加**:确保文件路径正确且文件可读。您可以查看消息缓冲区中的诊断信息来排查问题。 411 | 412 | 3. **命令系统使用问题**:使用 `/commands` 命令查看所有可用命令及其用法。 413 | 414 | ### 调试建议 415 | 416 | - 检查 gptel 的配置是否正确 417 | - 查看 `*Messages*` 缓冲区中的诊断信息 418 | - 确保所有依赖包都已正确安装和加载 419 | 420 | ## 更新日志 421 | 422 | ### 版本 0.4 (2025-10-13) 423 | - **工作流集成**:工作流现在可以在 Superchat 内直接运行,并复用 `/prompt` 共享的 gptel 工具与 MCP 服务器;新增聊天快捷语法(`>workflow-name`)及工作流目录说明。 424 | - **工具输出加固**:在处理 Jina/MCP 等工具返回的 Markdown 或 HTML 时自动清洗控制字符,修复 `json-value-p` 相关错误。 425 | - **实用性提升**:新增 `superchat-version` 常量并补充 `superchat-tools.el` 的自动化测试,覆盖 Jina 回退与用户确认分支。 426 | 427 | ### 版本 0.3 (2025-10-03) 428 | - **MCP 集成**:集成了 Model Context Protocol (MCP) 支持。 429 | - 零配置架构,自动检测和集成 MCP 服务器。 430 | - 实时状态监控,显示服务器连接状态和可用工具数量。 431 | - 无缝工具集成,MCP 工具自动融入 gptel 的工具系统。 432 | - 智能服务器管理,支持启动、停止和监控多个 MCP 服务器。 433 | - 新增 `/mcp` 和 `/mcp-start` 命令用于 MCP 管理。 434 | - **gptel Tools 集成**:零配置集成 gptel 的工具调用功能。 435 | - 直接读取用户在 gptel 中配置的 tools,无需重复配置。 436 | - 在聊天界面中无缝使用 gptel 的工具调用功能。 437 | - 支持 function calling 能力,可以调用外部工具和 API。 438 | - 添加 `/tools` 命令查看当前 tools 状态。 439 | - **@ 模型切换功能**:支持通过 `@model` 语法在对话中直接切换不同的 AI 模型。 440 | - 在聊天输入中使用 `@模型名` 语法快速切换模型。 441 | - 添加 `/models` 命令查看可用模型列表。 442 | - 临时模型切换,单次请求后自动恢复原模型。 443 | - **Bug 修复**:修复了命令系统初始化和会话管理的问题。 444 | 445 | ### 版本 0.2 (2025-09-23) 446 | - **记忆系统**:引入了全面的 AI 记忆系统,使 AI 能够从对话中进行持久学习。 447 | - 分层记忆捕获(显式、自动 LLM 总结)。 448 | - LLM 驱动的查询关键词提取,实现智能检索。 449 | - 记忆评分、衰减和自动归档。 450 | - 自动 LLM 驱动的相似记忆合并(默认禁用,需手动开启)。 451 | - **Bug 修复**:解决了各种稳定性与兼容性问题。 452 | 453 | ## 许可证 454 | 455 | Superchat.el 采用 GPL-3 协议开源。 456 | 457 | ## 背景 458 | 459 | Superchat.el 最初源自 [org-supertag](https://github.com/yibie/org-supertag) 项目的 chat-view 模块。为便于单独使用与扩展,Superchat 去除了所有 org-supertag 特有依赖,现已完全独立。 460 | -------------------------------------------------------------------------------- /test/superchat-memory-org-ql-tests.el: -------------------------------------------------------------------------------- 1 | 2 | ;;; superchat-memory-org-ql-tests.el --- org-ql search tests -*- lexical-binding: t; -*- 3 | 4 | (require 'ert) 5 | (require 'package) 6 | (package-initialize) 7 | (require 'org) 8 | (unless (require 'org-ql nil t) 9 | (error "org-ql not available")) 10 | (require 'cl-lib) 11 | (setq load-prefer-newer t) 12 | (require 'superchat-memory) 13 | 14 | (unless (fboundp 'gptel-request) 15 | (defun gptel-request (&rest _args) nil)) 16 | (unless (fboundp 'gptel-request-sync) 17 | (defun gptel-request-sync (&rest _args) "[]")) 18 | 19 | (defun superchat-memory-test--local-search-terms (query) 20 | (mapcar (lambda (term) 21 | (let ((clean (string-trim term))) 22 | (when (> (length clean) 0) 23 | (list :raw clean 24 | :regex (regexp-quote clean) 25 | :tag (upcase clean))))) 26 | (split-string query "[[:space:],]+" t))) 27 | 28 | (defun superchat-memory-test--stub-summarize (exchange) 29 | (let* ((command (plist-get exchange :command)) 30 | (type (or command "insight")) 31 | (tags (delq nil (list "AUTO-STUB" command)))) 32 | (superchat-memory-capture-conversation exchange 33 | :tier :tier2 34 | :type type 35 | :tags tags))) 36 | 37 | (defun superchat-memory-test--stub-gptel-request (&rest _args) nil) 38 | 39 | (defun superchat-memory-test--stub-gptel-request-sync (&rest _args) "[]") 40 | 41 | (defmacro superchat-memory-test--with-stubs (&rest body) 42 | `(let ((superchat-memory-stopwords '("the" "and" "or" "with"))) 43 | (cl-letf* (((symbol-function 'superchat-memory--prepare-search-terms) #'superchat-memory-test--local-search-terms) 44 | ((symbol-function 'superchat-memory-summarize-and-capture) #'superchat-memory-test--stub-summarize) 45 | ((symbol-function 'gptel-request) #'superchat-memory-test--stub-gptel-request) 46 | ((symbol-function 'gptel-request-sync) #'superchat-memory-test--stub-gptel-request-sync)) 47 | ,@body))) 48 | 49 | (fset 'superchat-memory--prepare-search-terms #'superchat-memory-test--local-search-terms) 50 | (fset 'superchat-memory-summarize-and-capture #'superchat-memory-test--stub-summarize) 51 | (fset 'gptel-request #'superchat-memory-test--stub-gptel-request) 52 | (fset 'gptel-request-sync #'superchat-memory-test--stub-gptel-request-sync) 53 | (setq superchat-memory-stopwords '("the" "and" "or" "with") 54 | superchat-memory-max-local-keywords 12 55 | superchat-memory-auto-capture-enabled t) 56 | 57 | 58 | (defun superchat-memory-test--write-memory (contents) 59 | (let* ((dir (make-temp-file "superchat-memory" t)) 60 | (file (expand-file-name "memory.org" dir))) 61 | (with-temp-file file 62 | (insert contents)) 63 | (list dir file))) 64 | 65 | (defmacro superchat-memory-test--with-memory (content &rest body) 66 | (declare (indent 1)) 67 | `(cl-destructuring-bind (dir file) 68 | (superchat-memory-test--write-memory ,content) 69 | (let ((superchat-data-directory dir) 70 | (superchat-memory-file file) 71 | (case-fold-search t)) 72 | (superchat-memory-test--with-stubs 73 | ,@body)))) 74 | 75 | (defun superchat-memory-test--titles (results) 76 | (mapcar (lambda (item) 77 | (let ((title (plist-get item :title))) 78 | (cond 79 | ((stringp title) (substring-no-properties title)) 80 | ((and (listp title) (stringp (car title))) 81 | (substring-no-properties (car title))) 82 | (t "")))) 83 | results)) 84 | 85 | (ert-deftest superchat-memory-extract-explicit-payload-detects-commands () 86 | (should (string= "部署计划" 87 | (superchat-memory--extract-explicit-payload "记住 部署计划"))) 88 | (should (string= "finish QA checklist" 89 | (superchat-memory--extract-explicit-payload "Remember finish QA checklist"))) 90 | (should-not (superchat-memory--extract-explicit-payload "Just a reminder"))) 91 | 92 | (ert-deftest superchat-memory-auto-capture-respects-tier1 () 93 | (superchat-memory-test--with-memory 94 | "" 95 | (let* ((superchat-memory-keyword-enrichment-function nil) 96 | (exchange (list :user "记得 下次要整理发布步骤" 97 | :assistant "提供了详细的发布步骤" 98 | :title "发布提醒")) 99 | (id (superchat-memory-auto-capture exchange))) 100 | (should (stringp id)) 101 | (with-current-buffer (find-file-noselect superchat-memory-file) 102 | (org-with-wide-buffer 103 | (goto-char (point-min)) 104 | (should (re-search-forward (regexp-quote id) nil t)) 105 | (org-back-to-heading t) 106 | (should (string= "tier1-explicit" (org-entry-get (point) "TRIGGER"))) 107 | (should (string= "directive" (downcase (or (org-entry-get (point) "TYPE") "")))) 108 | (should (= 5 (string-to-number (org-entry-get (point) "ACCESS_COUNT")))) 109 | (should (member "PRIORITY" (org-get-tags (point))))))))) 110 | 111 | (ert-deftest superchat-memory-auto-capture-heuristic-tier2 () 112 | (superchat-memory-test--with-memory 113 | "" 114 | (let* ((superchat-memory-keyword-enrichment-function nil) 115 | (superchat-memory-auto-capture-predicate (lambda (_exchange) t)) 116 | (exchange (list :user "好的" 117 | :assistant (make-string 140 ?A) 118 | :title "自动摘要")) 119 | (id (superchat-memory-auto-capture exchange))) 120 | (should (stringp id)) 121 | (with-current-buffer (find-file-noselect superchat-memory-file) 122 | (org-with-wide-buffer 123 | (goto-char (point-min)) 124 | (should (re-search-forward (regexp-quote id) nil t)) 125 | (org-back-to-heading t) 126 | (should (string= "tier2-heuristic" (org-entry-get (point) "TRIGGER"))) 127 | (should (string= "insight" 128 | (downcase (or (org-entry-get (point) "TYPE") ""))))))))) 129 | 130 | (ert-deftest superchat-memory-auto-capture-command-mode () 131 | (superchat-memory-test--with-memory 132 | "" 133 | (let* ((superchat-memory-keyword-enrichment-function nil) 134 | (superchat-memory-auto-capture-enabled t) 135 | (exchange (list :user "design summary" 136 | :assistant "Short assistant reply about layout." 137 | :content "User: design summary\n\nAssistant: Short assistant reply about layout." 138 | :title "Design summary" 139 | :command "design")) 140 | (id (superchat-memory-auto-capture exchange)) 141 | (entries (superchat-memory--retrieve-fallback "layout"))) 142 | (should (stringp id)) 143 | (should (= 1 (length entries))) 144 | (let ((entry (car entries))) 145 | (should (equal "design" (plist-get entry :type))) 146 | (should (member "COMMAND" (plist-get entry :tags))) 147 | (should (member "DESIGN" (plist-get entry :tags))))))) 148 | 149 | (ert-deftest superchat-memory-add-writes-extended-schema () 150 | (let* ((dir (make-temp-file "superchat-memory-schema" t)) 151 | (superchat-data-directory dir) 152 | (superchat-memory-file (expand-file-name "memory.org" dir))) 153 | (superchat-memory-add 154 | "Schema entry" 155 | "Body for schema entry with alpha content" 156 | :type "note" 157 | :tags '("foo" "bar") 158 | :keywords '("alpha" "beta") 159 | :related '("id-10" "id-11") 160 | :access-count 3) 161 | (with-temp-buffer 162 | (insert-file-contents superchat-memory-file) 163 | (let ((text (buffer-string))) 164 | (should (string-match-p ":ACCESS_COUNT: 3" text)) 165 | (should (string-match-p ":KEYWORDS: alpha, beta" text)) 166 | (should (string-match-p ":RELATED: id-10, id-11" text)))) 167 | (let ((fallback (superchat-memory--retrieve-fallback "Schema"))) 168 | (should (= 1 (length fallback))) 169 | (let ((entry (car fallback))) 170 | (should (= 3 (plist-get entry :access-count))) 171 | (should (equal '("alpha" "beta") (plist-get entry :keywords))) 172 | (should (equal '("id-10" "id-11") (plist-get entry :related))))) 173 | (when (featurep 'org-ql) 174 | (let ((entries (superchat-memory--retrieve-with-org-ql "Schema"))) 175 | (should (= 1 (length entries))) 176 | (let ((entry (car entries))) 177 | (should (= 3 (plist-get entry :access-count))) 178 | (should (equal '("alpha" "beta") (plist-get entry :keywords))) 179 | (should (equal '("id-10" "id-11") (plist-get entry :related)))))))) 180 | 181 | (ert-deftest superchat-memory-capture-explicit-generates-keywords () 182 | (let* ((dir (make-temp-file "superchat-memory-schema" t)) 183 | (superchat-data-directory dir) 184 | (superchat-memory-file (expand-file-name "memory.org" dir))) 185 | (let ((id (superchat-memory-capture-explicit 186 | "Important decision about vector indexes and caching layers." 187 | "Decision entry"))) 188 | (should (stringp id)) 189 | (let ((entries (superchat-memory--retrieve-fallback "indexes"))) 190 | (should (= 1 (length entries))) 191 | (let ((entry (car entries))) 192 | (should (member "indexes" (plist-get entry :keywords))) 193 | (should (member "decision" (plist-get entry :keywords)))))))) 194 | 195 | (ert-deftest superchat-memory-org-ql-matches-body-text () 196 | (superchat-memory-test--with-memory 197 | "* Alpha entry 198 | :PROPERTIES: 199 | :ID: a 200 | :TIMESTAMP: [2024-01-01] 201 | :TYPE: note 202 | :END: 203 | Alpha bravo text 204 | 205 | * Bravo entry :tag1: 206 | :PROPERTIES: 207 | :ID: b 208 | :TIMESTAMP: [2024-02-02] 209 | :TYPE: idea 210 | :END: 211 | Charlie bravo payload 212 | " 213 | (let ((results (superchat-memory--retrieve-with-org-ql "bravo"))) 214 | (should (= 2 (length results))) 215 | (should (equal (superchat-memory-test--titles results) 216 | '("Alpha entry" "Bravo entry")))))) 217 | 218 | (ert-deftest superchat-memory-org-ql-matches-tags () 219 | (superchat-memory-test--with-memory 220 | "* Tagged entry :context: 221 | :PROPERTIES: 222 | :ID: c 223 | :TIMESTAMP: [2024-03-03] 224 | :TYPE: snippet 225 | :END: 226 | Body without keyword 227 | 228 | * Plain entry 229 | :PROPERTIES: 230 | :ID: d 231 | :TIMESTAMP: [2024-04-04] 232 | :TYPE: misc 233 | :END: 234 | No relevant tags here 235 | " 236 | (let ((results (superchat-memory--retrieve-with-org-ql "context"))) 237 | (should (= 1 (length results))) 238 | (should (equal (superchat-memory-test--titles results) 239 | '("Tagged entry")))))) 240 | 241 | (ert-deftest superchat-memory-org-ql-combines-keywords-across-fields () 242 | (superchat-memory-test--with-memory 243 | "* Mixed entry :ref: 244 | :PROPERTIES: 245 | :ID: e 246 | :TIMESTAMP: [2024-05-05] 247 | :TYPE: note 248 | :END: 249 | Contains alpha content 250 | 251 | * Other entry :ref: 252 | :PROPERTIES: 253 | :ID: f 254 | :TIMESTAMP: [2024-06-06] 255 | :TYPE: note 256 | :END: 257 | Irrelevant text 258 | " 259 | (let ((results (superchat-memory--retrieve-with-org-ql "alpha ref"))) 260 | (should (= 1 (length results))) 261 | (should (equal (superchat-memory-test--titles results) 262 | '("Mixed entry")))))) 263 | 264 | (ert-deftest superchat-memory-org-ql-special-characters () 265 | (superchat-memory-test--with-memory 266 | "* C++ entry 267 | :PROPERTIES: 268 | :ID: g 269 | :TIMESTAMP: [2024-07-07] 270 | :TYPE: note 271 | :END: 272 | Discusses C++ lambda captures 273 | " 274 | (let ((results (superchat-memory--retrieve-with-org-ql "C++"))) 275 | (should (= 1 (length results))) 276 | (should (equal (superchat-memory-test--titles results) 277 | '("C++ entry")))))) 278 | 279 | (ert-deftest superchat-memory-org-ql-no-false-positives () 280 | (superchat-memory-test--with-memory 281 | "* Delta entry 282 | :PROPERTIES: 283 | :ID: h 284 | :TIMESTAMP: [2024-08-08] 285 | :TYPE: note 286 | :END: 287 | Only talks about org-ql 288 | " 289 | (let ((results (superchat-memory--retrieve-with-org-ql "zulu"))) 290 | (should (null results))))) 291 | 292 | 293 | 294 | (ert-deftest superchat-memory-find-merge-candidates-llm () 295 | (superchat-memory-test--with-memory 296 | "* Shared alpha entry :SHARED: 297 | :PROPERTIES: 298 | :ID: alpha-id 299 | :TIMESTAMP: [2024-01-01] 300 | :KEYWORDS: alpha, beta, gamma 301 | :END: 302 | Body mentioning shared topics. 303 | 304 | * Overlapping alpha entry :SHARED: 305 | :PROPERTIES: 306 | :ID: beta-id 307 | :TIMESTAMP: [2024-01-02] 308 | :KEYWORDS: alpha, beta 309 | :END: 310 | Another body with overlapping keywords. 311 | 312 | * Distinct delta entry :UNIQUE: 313 | :PROPERTIES: 314 | :ID: delta-id 315 | :TIMESTAMP: [2024-01-03] 316 | :KEYWORDS: delta 317 | :END: 318 | Content unrelated to the shared alpha topic. 319 | 320 | * Archived entry :ARCHIVED: 321 | :PROPERTIES: 322 | :ID: archived-id 323 | :TIMESTAMP: [2024-01-04] 324 | :KEYWORDS: alpha, beta 325 | :END: 326 | Should be ignored because of the ARCHIVED tag. 327 | " 328 | (cl-letf (((symbol-function 'superchat-memory--check-similarity-with-llm-async) 329 | (lambda (entry1 entry2 callback) 330 | (let* ((ids (list (plist-get entry1 :id) 331 | (plist-get entry2 :id))) 332 | (sorted (sort (copy-sequence ids) #'string<))) 333 | (funcall callback (equal '("alpha-id" "beta-id") sorted)))))) 334 | (let* ((groups (superchat-memory--find-merge-candidates)) 335 | (group-ids (mapcar (lambda (group) 336 | (sort (mapcar (lambda (entry) (plist-get entry :id)) group) 337 | #'string<)) 338 | groups))) 339 | (should (= 1 (length group-ids))) 340 | (should (member '("alpha-id" "beta-id") group-ids)) 341 | (should-not (member "delta-id" (apply #'append group-ids))) 342 | (should-not (member "archived-id" (apply #'append group-ids))))))) 343 | 344 | (provide 'superchat-memory-org-ql-tests) 345 | 346 | (ert-deftest superchat-memory-find-merge-candidates-jaccard-fallback () 347 | "Test the async candidate finder relies on Jaccard when LLM is unavailable." 348 | (let ((superchat-memory-file (expand-file-name "test-memory.org" "/Users/chenyibin/Documents/emacs/package/superchat/test/")) 349 | (superchat-memory-merge-similarity-threshold 0.4) 350 | (candidate-groups nil)) 351 | (setq candidate-groups (superchat-memory--find-merge-candidates)) 352 | ;; Verification 353 | (should (not (null candidate-groups))) 354 | (should (= 1 (length candidate-groups))) 355 | (let* ((group (car candidate-groups)) 356 | (ids (sort (mapcar (lambda (entry) (plist-get entry :id)) group) #'string<))) 357 | (should (= 2 (length group))) 358 | (should (equal '("ID-A" "ID-B") ids))))) 359 | 360 | ;;; superchat-memory-org-ql-tests.el ends here 361 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [中文](./README_cn.md) 2 | 3 | # SuperChat 4 | 5 | ## Introduction 6 | 7 | A friendly, Claude Code–style chat UI for gptel in Emacs. Superchat makes structured prompts and file‑grounded conversations effortless—no new infrastructure, just your editor. 8 | 9 | - "/" for commands with completion, plus easy custom command creation. 10 | - "#" to attach files as part of the message you send to the LLM. 11 | - Clean, fast chat experience with streaming and readable output. 12 | - Works out‑of‑the‑box with your existing gptel setup. 13 | 14 | Key features include: 15 | - Retaining the complete command system 16 | - Adding the ability to include files as context in conversations 17 | - Supporting conversations with various large language models (LLMs) 18 | - Open-sourced under the GPL-3 license 19 | 20 | ## Installation and Configuration 21 | 22 | ### Requirements 23 | 24 | - Emacs 27.1 or higher 25 | - [gptel](https://github.com/karthink/gptel) package 26 | - [gptel-context](https://github.com/karthink/gptel) package (part of gptel) 27 | 28 | ### Installation Steps 29 | 30 | 1. Place the superchat file in your Emacs load path 31 | 2. Add the following code to your Emacs configuration file (usually `~/.emacs.d/init.el`): 32 | 33 | Example: 34 | ```elisp 35 | (add-to-list 'load-path "~/.emacs.d/superchat") 36 | (require 'superchat) 37 | ``` 38 | 39 | ### Basic Configuration 40 | 41 | Superchat is designed to integrate seamlessly with `gptel`. All settings related to the LLM, such as the model (`gptel-model`), API key, and temperature, are automatically inherited from your `gptel` configuration. To configure the AI's behavior, please customize the `gptel` variables directly. 42 | 43 | You can configure Superchat's own options using the following variables: 44 | 45 | ```elisp 46 | ;; Set the data storage directory 47 | (setq superchat-data-directory "~/.emacs.d/superchat/") 48 | 49 | ;; Set the language for $lang variable in custom commands 50 | (setq superchat-lang "English") ; or "中文", "Français", etc. 51 | 52 | ;; Response timeout protection (prevents UI freezing from blocking tools) 53 | (setq superchat-response-timeout 30) ; seconds, nil to disable 54 | 55 | ;; Smart completion detection delay (for non-streaming responses) 56 | ;; Used primarily for Ollama + tools mode 57 | (setq superchat-completion-check-delay 2) ; seconds, default is 2 58 | 59 | ;; Set default directories for file selection 60 | (setq superchat-default-directories '("~/Documents" "~/Projects")) 61 | ``` 62 | 63 | Note: Superchat now automatically manages its directory structure. The `superchat-save-directory` and `superchat-command-dir` variables have been removed. Directories are now created dynamically as needed, or you can use `M-x superchat-ensure-directories` to manually ensure all directories exist. 64 | 65 | ## Quick Start 66 | 67 | 1. Launch Superchat with `M-x superchat` 68 | 2. Enter your question after the prompt. You can press `RET` to create multi-line input. 69 | 3. Press `C-c C-c` to send the message. 70 | 4. Wait for the AI assistant's reply 71 | 72 | ### Basic Conversation Example 73 | 74 | ``` 75 | User: Hello, what can you help me with? 76 | Assistant: I am an AI assistant that can help you answer questions, analyze code, provide suggestions, and more. 77 | ``` 78 | 79 | ## Core Features Explained 80 | 81 | ### Basic Conversations 82 | 83 | Superchat allows you to have natural language conversations with various large language models. 84 | 85 | ### Command System 86 | 87 | Superchat provides a powerful command system that allows you to define and use custom prompts: 88 | 89 | - Built-in commands: such as `/create-question` 90 | - Custom command definition: using the `/define` command 91 | - View all available commands: using the `/commands` command 92 | 93 | After the input prompt, you can use auto-completion to view and select commands: 94 | 1. Type the [/](file:///Users/chenyibin/Documents/emacs/package/superchat/superchat.el) character 95 | 2. Continue typing the first letter of the command; if you have company or corfu plugins, select the command from the pop-up list. 96 | 3. If you don't have company or corfu, press `M-TAB` (or your Emacs environment's completion key) to trigger auto-completion and select the command from the pop-up list. 97 | 98 | For example, to use the `/create-question` command, you can type `/c` and then press `M-TAB`, and the system will display all available commands starting with 'c' for you to choose from. 99 | 100 | ### Conversation History 101 | 102 | Superchat now automatically includes previous messages from the current session in new prompts. This allows you to ask follow-up questions or discuss topics in more detail without having to manually re-state the context of the conversation. The number of messages included is configurable. 103 | 104 | ### File Context Support 105 | 106 | Superchat can add file content as context to the conversation: 107 | 108 | 1. Press the `#` key after the input prompt 109 | 2. Select the file to add 110 | 3. The file content will be provided as context to the AI 111 | 112 | You can also manually enter the file path in the format `# /path/to/file`. 113 | 114 | When `superchat-default-directories` is set, the file selection will show all files from the specified directories in a single list, making it easier to select files from predefined locations. 115 | 116 | ### gptel Tools Integration 117 | 118 | Superchat now fully supports gptel's tools (function calling) functionality, enabling AI to directly call external tools and services to enhance conversation capabilities. 119 | 120 | Key features include: 121 | - **Zero Configuration Integration**: Automatically reads your gptel tools configuration without requiring additional setup 122 | - **Intelligent Tool Calling**: AI automatically determines and uses appropriate tools based on your needs 123 | - **Seamless Experience**: Tool call results naturally integrate into the conversation flow 124 | - **Smart Completion Detection**: Automatically handles non-streaming tool responses (Ollama + tools) 125 | - **Status Monitoring**: Use `/tools` command to view currently available tool status 126 | 127 | Usage: 128 | 1. Configure your tools in gptel: 129 | ```elisp 130 | (setq gptel-use-tools t) 131 | (setq gptel-tools (list ...)) 132 | ``` 133 | 2. Launch Superchat and chat normally 134 | 3. When your needs require tools, AI will automatically call relevant tools 135 | 4. Use `/tools` command to check tool status 136 | 137 | Example conversation: 138 | ``` 139 | User: Search for the latest Emacs news 140 | AI: [automatically calls web_search tool] I found the latest Emacs news for you... 141 | ``` 142 | 143 | #### Technical Notes on Ollama + Tools 144 | 145 | When using Ollama with tools enabled, there are known streaming limitations: 146 | 147 | 1. **gptel's Behavior**: gptel disables streaming for Ollama + tools mode ([see gptel-request.el:2019](https://github.com/karthink/gptel/blob/master/gptel-request.el#L2019)) 148 | ```elisp 149 | ;; HACK(tool): no stream if Ollama + tools. Need to find a better way 150 | (not (and (eq (type-of gptel-backend) 'gptel-ollama) 151 | gptel-tools gptel-use-tools)) 152 | ``` 153 | 154 | 2. **Ollama's Issue**: Ollama's tool calling streaming implementation has known issues ([ollama/ollama#12557](https://github.com/ollama/ollama/issues/12557)) 155 | 156 | 3. **Superchat's Solution**: Implements smart completion detection that: 157 | - Waits for `superchat-completion-check-delay` (default 2 seconds) after receiving response 158 | - If no new data arrives, considers the response complete 159 | - Automatically enters next conversation round 160 | - Falls back to 30-second timeout protection if needed 161 | 162 | **Configuration**: 163 | ```elisp 164 | ;; Adjust completion check delay if needed 165 | ;; Lower values = faster completion, higher values = more reliable 166 | (setq superchat-completion-check-delay 2) ; 1-5 seconds recommended 167 | ``` 168 | 169 | **Note**: This only affects Ollama + tools mode. Normal streaming conversations complete immediately via standard signals. 170 | 171 | ### Workflow Automation 172 | 173 | Workflows let you store entire conversations-as-recipes. One prompt can run several steps (search, analysis, saving output) without retyping anything. 174 | 175 | - **Start from chat**: Type `>workflow-name topic` and Superchat runs the matching `.workflow` file. No extra setup is required—workflows reuse the gptel tools and MCP servers you already configured. 176 | - **Keep everything in sync**: Steps can read local files with `#path`, call models with `@model`, and finish by writing results somewhere you choose. 177 | - **Save once, repeat often**: Put workflow files under `~/.emacs.d/superchat/workflow/` (or `superchat-data-directory/workflow/`) and reuse them whenever you need the task again. 178 | - **Simple, linear steps**: Each non-empty line is one step executed from top to bottom. Branching/conditional flows (like n8n) are not supported yet; keep instructions in a straight sequence. 179 | 180 | Try it: 181 | 1. Create `~/.emacs.d/superchat/workflow/ai-news-summary.workflow` with the contents below. 182 | 2. In Superchat, run `>ai-news-summary AI Memory` (or any keyword). 183 | 3. The workflow searches the web, summarizes the news, and saves the Markdown report automatically. 184 | 185 | ```text 186 | # Workflow: AI Tech News Digest 187 | # Description: Weekly tech news summary 188 | 189 | /web-search Search for news related to "$input" 190 | 191 | @qwen3-coder:30b-a3b-q8_0 Analyze the findings (business, technology, society) and produce a concise English summary 192 | 193 | Save the summary to #~/Documents/news-summary.md 194 | ``` 195 | 196 | ### MCP (Model Context Protocol) Integration 197 | 198 | Superchat now integrates MCP (Model Context Protocol) support, allowing you to connect various external services and tools through a standardized protocol. MCP provides a unified interface to manage tools from different servers, greatly expanding the AI's capabilities. 199 | 200 | Key features include: 201 | - **Zero-Configuration Architecture**: Automatically detects and integrates MCP servers without manual setup 202 | - **Real-time Status Monitoring**: Displays server connection status and available tool counts 203 | - **Seamless Tool Integration**: MCP tools automatically integrate into gptel's tool system 204 | - **Intelligent Server Management**: Supports starting, stopping, and monitoring multiple MCP servers 205 | 206 | #### Installation and Configuration 207 | 208 | 1. **Install MCP package**: 209 | ```elisp 210 | ;; Using straight.el 211 | (straight-use-package 'mcp) 212 | 213 | ;; Or using use-package 214 | (use-package mcp) 215 | ``` 216 | 217 | 2. **Configure MCP servers** (in your Emacs configuration): 218 | ```elisp 219 | ;; Example: Configure multiple MCP servers (with optional Jina API key) 220 | (setq mcp-hub-servers 221 | (let ((jina-token (getenv "JINA_API_KEY"))) 222 | `(("filesystem" . (:command "npx" 223 | :args ("-y" "@modelcontextprotocol/server-filesystem" 224 | "/Users/yourname/Documents"))) 225 | ("fetch" . (:command "uvx" :args ("mcp-server-fetch"))) 226 | ("jina-mcp-server" 227 | . (:url "https://mcp.jina.ai/sse" 228 | :headers ,(when (and jina-token (> (length jina-token) 0)) 229 | `((Authorization . ,(concat "Bearer " jina-token))))))))) 230 | ``` 231 | 232 | #### Usage 233 | 234 | **Check MCP Status**: 235 | - Use `/mcp` command to view current MCP status 236 | - Shows configured server count, running server count, and available tool count 237 | 238 | **Start MCP Servers**: 239 | - Use `/mcp-start` command to start MCP servers 240 | - System automatically detects and starts configured but not-running servers 241 | - Started tools are automatically integrated into the current gptel session 242 | 243 | **Example Conversation**: 244 | ``` 245 | User: /mcp 246 | System: MCP Status: Available ✓ | Configured: 1 servers | Running: 1 servers | Available tools: 15 247 | 248 | User: /mcp-start 249 | System: Starting MCP servers... 250 | Started servers: filesystem 251 | Added 15 tools to gptel session 252 | 253 | User: List important files in my Documents directory 254 | AI: [using MCP filesystem tools] I found the important files in your Documents directory... 255 | ``` 256 | 257 | #### Supported MCP Commands 258 | 259 | - `/mcp` - Display MCP status and server information 260 | - `/mcp-start` - Start MCP servers and integrate tools 261 | 262 | #### Common MCP Servers 263 | 264 | Here are some popular MCP server examples: 265 | 266 | ```elisp 267 | ;; Filesystem server 268 | (setq mcp-hub-servers 269 | '(("filesystem" . (:command "npx" 270 | :args ("-y" "@modelcontextprotocol/server-filesystem" "/path/to/directory"))))) 271 | 272 | ;; GitHub server 273 | '("github" . (:command "npx" 274 | :args ("-y" "@modelcontextprotocol/server-github") 275 | :env ("GITHUB_PERSONAL_ACCESS_TOKEN" . "your_token_here"))) 276 | 277 | ;; SQLite database server 278 | '("sqlite" . (:command "npx" 279 | :args ("-y" "@modelcontextprotocol/server-sqlite" "path/to/database.db"))) 280 | 281 | ;; Web search server 282 | '("brave-search" . (:command "npx" 283 | :args ("-y" "@modelcontextprotocol/server-brave-search") 284 | :env ("BRAVE_API_KEY" . "your_api_key"))) 285 | ``` 286 | 287 | Note: MCP functionality requires the `mcp.el` package. If not installed, related commands will show friendly error messages. 288 | 289 | ### Memory System 290 | 291 | Superchat now features a persistent and queryable memory system, allowing the AI to remember past conversations and leverage that knowledge in future interactions. This system is built on Org-mode files, ensuring transparency and user control. 292 | 293 | Key aspects include: 294 | - **Tiered Memory Capture**: Automatically captures important insights from conversations (LLM-summarized) and allows explicit user commands to save memories. 295 | - **Intelligent Retrieval**: Uses LLM-extracted keywords from your queries to find the most relevant memories, which are then provided as context to the AI. 296 | - **Memory Lifecycle Management**: 297 | - **Scoring & Decay**: Memories are scored based on utility, with scores decaying over time. 298 | - **Automatic Archiving**: Less relevant memories are automatically moved to an archive file, keeping the active memory lean. 299 | - **Automatic Merging**: Similar or duplicate memories can be automatically consolidated by the LLM into a single, more comprehensive entry. (Note: This feature is enabled by default, merging daily.) 300 | 301 | Configuration options for the memory system are available under `M-x customize-group RET superchat-memory RET`. 302 | 303 | ## Advanced Usage 304 | 305 | ### Custom Commands 306 | 307 | You can create custom prompts using the `/define` command: 308 | 309 | ``` 310 | /define explain-code "Please explain what the following code does: $input" 311 | ``` 312 | 313 | In addition to the `/define` command, you can create custom commands by simply adding prompt files to the `command` directory within your `superchat-data-directory`. The filename (without extension) will automatically become the command name. The default file extension is `.prompt`, but other formats like `.md`, `.org`, and `.txt` are also supported. 314 | 315 | **Adding Descriptions:** 316 | To display a description for your command in the `/commands` list, use the filename format `NAME-DESCRIPTION.prompt`. Superchat automatically parses the text after the first hyphen as the command's purpose. 317 | 318 | Examples: 319 | - `summarize.prompt` -> Command: `/summarize` (No description) 320 | - `seo-optimize_website_content.prompt` -> Command: `/seo`, Description: "optimize website content" 321 | 322 | The file content (e.g., `Please summarize the following text: $input`) serves as the prompt template. 323 | 324 | In custom prompts, you can use the following variables: 325 | - `$input`: The user's input content 326 | - `$lang`: The set language (defaults to English) 327 | 328 | #### Setting the Language for $lang Variable 329 | 330 | The `$lang` variable in custom commands can be configured in several ways: 331 | 332 | **Method 1: Through Emacs Customization Interface (Recommended)** 333 | ```elisp 334 | M-x customize-variable RET superchat-lang RET 335 | ``` 336 | Then change the value from "English" to your preferred language (e.g., "中文", "Français", "Español") and save the settings. 337 | 338 | **Method 2: In Configuration File** 339 | Add to your Emacs configuration file: 340 | ```elisp 341 | (setq superchat-lang "中文") ; For Chinese 342 | (setq superchat-lang "Français") ; For French 343 | (setq superchat-lang "Español") ; For Spanish 344 | ``` 345 | 346 | **Method 3: Temporary Setting (Current Session)** 347 | Execute in Emacs: 348 | ```elisp 349 | M-x eval-expression RET (setq superchat-lang "中文") RET 350 | ``` 351 | 352 | **Example Usage:** 353 | When you set `(setq superchat-lang "中文")` and use the built-in `/create-question` command: 354 | - Template: `"Please list all important questions related to $input in $lang."` 355 | - With input "git": `"Please list all important questions related to git in 中文."` 356 | 357 | The language setting is dynamically retrieved each time you send a message, so you can change it anytime and it will take effect immediately. 358 | 359 | ### Context Management 360 | 361 | - Use the `/clear-context` command to clear all context files from the current session 362 | - Superchat automatically manages files added to the session 363 | 364 | ### Key Bindings 365 | 366 | - `RET` or `C-c C-c`: Send input 367 | - `#`: Smartly add file path to context 368 | - `C-c C-h`: Show command list 369 | - `C-c C-s`: Save current session 370 | - `/tools`: View current gptel tools status 371 | - `/mcp`: View MCP status and server information 372 | - `/mcp-start`: Start MCP servers and integrate tools 373 | 374 | ## Configuration Options 375 | 376 | The main customization options for Superchat are: 377 | 378 | - `superchat-buffer-name`: Name of the chat buffer (defaults to "*Superchat*") 379 | - `superchat-data-directory`: Data storage directory 380 | - `superchat-lang`: Language setting for the `$lang` variable in custom commands (defaults to "English") 381 | - `superchat-display-single-window`: If non-nil, make the Superchat window the only one in its frame, providing a dedicated view. Enabled by default. 382 | - `superchat-default-directories`: List of default directories for file selection 383 | - `superchat-general-answer-prompt`: General answer prompt template 384 | - `superchat-context-message-count`: Number of most recent conversation messages to include in prompts. 385 | - `superchat-conversation-history-limit`: Maximum number of conversation messages to retain in memory. 386 | 387 | ### Memory System Configuration 388 | 389 | These options control Superchat's memory system. You can customize them via `M-x customize-group RET superchat-memory RET`. 390 | 391 | - `superchat-memory-file`: Path to the main memory Org file (defaults to `memory.org` in data directory). 392 | - `superchat-memory-archive-file`: Path to the archived memory Org file (defaults to `memory-archive.org` in data directory). 393 | - `superchat-memory-explicit-trigger-patterns`: Regexp patterns that identify Tier 1 explicit memory commands in user text. 394 | - `superchat-memory-auto-capture-enabled`: When non-nil, Tier 2 automatic captures may run after each exchange. 395 | - `superchat-memory-auto-capture-minimum-length`: Minimum assistant response length to trigger auto-capture. 396 | - `superchat-memory-use-org-ql-cache`: When non-nil and org-ql is available, enable `org-ql-cache` during queries. 397 | - `superchat-memory-max-results`: Maximum number of memories returned per retrieval. 398 | - `superchat-memory-auto-recall-min-length`: Minimum query length before automatic memory retrieval runs. 399 | - `superchat-memory-llm-timeout`: Maximum seconds to wait for synchronous LLM utilities before falling back. 400 | - `superchat-memory-title-weight`: Score weight applied when a query term matches the title. 401 | - `superchat-memory-body-weight`: Score weight applied when a term matches the body content. 402 | - `superchat-memory-keyword-weight`: Score weight applied when a term matches stored keywords. 403 | - `superchat-memory-tag-weight`: Score weight applied when a term matches tags. 404 | - `superchat-memory-access-weight`: Score contribution from the entry's `:ACCESS_COUNT:`. 405 | - `superchat-memory-recency-weight`: Score contribution based on how recent the entry is. 406 | - `superchat-memory-recency-half-life-days`: Half-life in days used when computing recency decay. 407 | - `superchat-memory-auto-increment-access-count`: Automatically increment `:ACCESS_COUNT:` for retrieved memories. 408 | - `superchat-memory-decay-factor`: Multiplier applied to access counts during decay. 409 | - `superchat-memory-decay-min-access`: Lower bound for access counts after decay. 410 | - `superchat-memory-archive-threshold`: Access count threshold for archiving memories. 411 | - `superchat-memory-auto-prune-interval-days`: Interval in days for automatic memory pruning (0 or `nil` to disable). 412 | - `superchat-memory-merge-similarity-threshold`: Jaccard similarity threshold for considering two memories for merging. 413 | - `superchat-memory-auto-merge-interval-days`: Interval in days for automatic memory merging (0 or `nil` to disable). WARNING: This feature carries risks. 414 | 415 | ### New Functions 416 | 417 | - `superchat-ensure-directories`: Interactive function to manually ensure all necessary directories exist. Usage: `M-x superchat-ensure-directories` 418 | 419 | ## Troubleshooting 420 | 421 | ### Common Issues 422 | 423 | 1. **Unable to connect to AI service**: Please check if your gptel configuration is correct, including API key and endpoint settings. 424 | 425 | 2. **File context not added correctly**: Ensure the file path is correct and the file is readable. You can check the diagnostic information in the messages buffer to troubleshoot the issue. 426 | 427 | 3. **Command system usage issues**: Use the `/commands` command to view all available commands and their usage. 428 | 429 | 4. **"User:" prompt doesn't appear after AI response**: Usually caused by blocking tools or network issues. 430 | - **Automatic protection**: SuperChat has a 30-second timeout that will auto-recover the UI 431 | - **If timeout message appears**: Your tools are blocking (synchronous network calls, etc.) 432 | - **Solution**: Increase timeout with `(setq superchat-response-timeout 60)` or fix your tools 433 | - **Check status**: Run `/tools` in chat to see timeout settings 434 | 435 | 5. **Response timeout warnings**: You'll see "⚠️ Response timeout after X seconds" if: 436 | - Your tools are making synchronous/blocking calls (e.g., `url-retrieve-synchronously`) 437 | - Network is very slow 438 | - **Fix**: Either increase `superchat-response-timeout` or make your tools asynchronous 439 | 440 | ### Debugging Suggestions 441 | 442 | - Check if gptel's configuration is correct 443 | - View diagnostic information in the `*Messages*` buffer 444 | - Ensure all dependency packages are correctly installed and loaded 445 | 446 | ## CHANGLOG 447 | 448 | ### Version 0.4 (2025-10-13) 449 | - **Workflow Integration**: Workflows now run seamlessly inside Superchat, sharing the gptel tool stack and MCP servers configured for `/prompt` sessions. Added chat shortcut (`>workflow-name`) and documentation for storing reusable `.workflow` recipes. 450 | - **Tool Output Hardening**: Sanitized workflow result handling to avoid `json-value-p` errors when tools (e.g., Jina reader, MCP fetchers) return rich Markdown or HTML. 451 | - **Utility Additions**: Introduced `superchat-version` constant and expanded automated tests for `superchat-tools.el`, covering Jina fallback logic and user consent branches. 452 | 453 | ### Version 0.3 (2025-10-03) 454 | - **MCP Integration**: Integrated Model Context Protocol (MCP) support. 455 | - Zero-configuration architecture for automatic MCP server detection and integration. 456 | - Real-time status monitoring showing server connection status and available tool counts. 457 | - Seamless tool integration with MCP tools automatically integrated into gptel's tool system. 458 | - Intelligent server management supporting starting, stopping, and monitoring multiple MCP servers. 459 | - Added `/mcp` and `/mcp-start` commands for MCP management. 460 | - **gptel Tools Integration**: Zero-configuration integration of gptel's tool calling functionality. 461 | - Directly reads user-configured tools in gptel without redundant configuration. 462 | - Seamless use of gptel's tool calling functionality in chat interface. 463 | - Supports function calling capabilities to invoke external tools and APIs. 464 | - Added `/tools` command to view current tools status. 465 | - **@ Model Switching**: Support for switching between different AI models using `@model` syntax directly in conversations. 466 | - Quick model switching using `@model_name` syntax in chat input. 467 | - Added `/models` command to view available model list. 468 | - Temporary model switching with automatic restoration after single request. 469 | - **Bug Fixes**: Fixed command system initialization and session management issues. 470 | 471 | ### Version 0.2 (2025-09-23) 472 | - **Memory System**: Introduced a comprehensive memory system for the AI, enabling persistent learning from conversations. 473 | - Tiered memory capture (explicit, automatic LLM-summarized). 474 | - LLM-powered query keyword extraction for intelligent retrieval. 475 | - Memory scoring, decay, and automatic archiving. 476 | - Automatic LLM-driven merging of similar memories (opt-in, disabled by default). 477 | - **Bug Fixes**: Addressed various stability and compatibility issues. 478 | 479 | ## License 480 | 481 | Superchat.el is open-sourced under the GPL-3 license. 482 | 483 | ## Background 484 | 485 | Superchat.el is a standalone Emacs AI chat client that evolved from the chat-view module of the [org-supertag](https://github.com/yibie/org-supertag) project. Unlike the original version, Superchat removes all org-supertag-specific dependencies, making it a completely independent package. 486 | -------------------------------------------------------------------------------- /superchat-tools.el: -------------------------------------------------------------------------------- 1 | ;;; superchat-tools.el --- Native tools for Superchat -*- lexical-binding: t; -*- 2 | 3 | ;; This file is in the public domain. 4 | 5 | ;;; Commentary: 6 | ;; This file defines a set of native Emacs Lisp tools for use with 7 | ;; Superchat and gptel. Each tool follows a strict security model 8 | ;; requiring explicit user approval for any action that affects 9 | ;; the user's system. 10 | 11 | ;;; Code: 12 | 13 | (require 'cl-lib) 14 | (require 'json) 15 | (require 'url) 16 | (require 'url-util) 17 | (require 'auth-source) 18 | (require 'subr-x) 19 | (require 'gptel nil t) 20 | 21 | (eval-when-compile 22 | (defvar gnutls-verify-error) 23 | (defvar gnutls-min-prime-bits)) 24 | 25 | (defconst superchat-tool--default-user-agent 26 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" 27 | "Default user-agent used for outbound HTTP requests made by Superchat tools.") 28 | 29 | ;;;--------------------------------------------- 30 | ;;; Helper Functions 31 | ;;;--------------------------------------------- 32 | 33 | (defun superchat-tool--confirm-diff (path new-content &optional mode) 34 | "Show a smart diff/preview and ask for user confirmation. 35 | PATH: target file path 36 | NEW-CONTENT: content to write 37 | MODE: 'append, 'create, or nil (default replace) 38 | Returns t if user approves, nil otherwise." 39 | (let* ((expanded-path (expand-file-name path)) 40 | (file-exists (file-exists-p expanded-path)) 41 | (old-content (if file-exists 42 | (with-temp-buffer 43 | (insert-file-contents expanded-path) 44 | (buffer-string)) 45 | "")) 46 | (is-new-file (not file-exists)) 47 | (is-append (eq mode 'append)) 48 | (preview-content (if is-append 49 | (concat old-content new-content) 50 | new-content))) 51 | 52 | ;; Smart preview based on content size and operation 53 | (cond 54 | ;; New small file - just show basic info + preview 55 | ((and is-new-file (< (length new-content) 500)) 56 | (message "📝 Creating new file: %s (%d chars)" expanded-path (length new-content)) 57 | (with-temp-buffer 58 | (insert (format "=== Creating new file ===\n") 59 | (format "Path: %s\n" expanded-path) 60 | (format "Size: %d characters\n\n" (length new-content)) 61 | (format "Preview (first 200 chars):\n%s%s" 62 | (substring new-content 0 (min 200 (length new-content))) 63 | (if (> (length new-content) 200) "..." ""))) 64 | (let ((preview-buffer (current-buffer))) 65 | (with-help-window "*Superchat File Preview*" 66 | (with-current-buffer standard-output 67 | (insert-buffer-substring preview-buffer)))))) 68 | 69 | ;; New large file - just show summary 70 | (is-new-file 71 | (message "📝 Creating new file: %s (%d chars)" expanded-path (length new-content))) 72 | 73 | ;; Append mode - show what will be added 74 | (is-append 75 | (message "➕ Appending to: %s (adding %d chars)" expanded-path (length new-content)) 76 | (when (< (length new-content) 300) 77 | (with-temp-buffer 78 | (insert (format "=== Appending to file ===\n") 79 | (format "Path: %s\n" expanded-path) 80 | (format "Adding: %d characters\n\n" (length new-content)) 81 | (format "Content to append:\n%s" new-content)) 82 | (let ((preview-buffer (current-buffer))) 83 | (with-help-window "*Superchat Append Preview*" 84 | (with-current-buffer standard-output 85 | (insert-buffer-substring preview-buffer))))))) 86 | 87 | ;; Existing file replacement - show smart diff 88 | (t 89 | (message "🔄 Replacing file: %s (old: %d chars, new: %d chars)" 90 | expanded-path (length old-content) (length new-content)) 91 | (when (< (+ (length old-content) (length new-content)) 1000) 92 | (with-temp-buffer 93 | (insert (format "=== File Changes ===\n") 94 | (format "Path: %s\n" expanded-path) 95 | (format "Old size: %d chars, New size: %d chars\n\n" 96 | (length old-content) (length new-content))) 97 | ;; Show a simple before/after for small files 98 | (when (< (length old-content) 300) 99 | (insert "--- Current content ---\n") 100 | (insert old-content) 101 | (insert "\n\n")) 102 | (when (< (length new-content) 300) 103 | (insert "+++ New content ---\n") 104 | (insert new-content)) 105 | (let ((diff-buffer (current-buffer))) 106 | (with-help-window "*Superchat File Changes*" 107 | (with-current-buffer standard-output 108 | (insert-buffer-substring diff-buffer)))))))) 109 | 110 | ;; Simplified confirmation with more options 111 | (let ((prompt (cond 112 | (is-new-file "Create this new file?") 113 | (is-append "Append this content?") 114 | (t "Replace file with new content?")))) 115 | (prog1 116 | (y-or-n-p (format "%s (%s) " prompt expanded-path)) 117 | ;; Extend timeout after user confirmation 118 | (when (fboundp 'superchat--extend-timeout) 119 | (superchat--extend-timeout)))))) 120 | 121 | ;;;--------------------------------------------- 122 | ;;; Tool: shell-command 123 | ;;;--------------------------------------------- 124 | 125 | (add-to-list 'gptel-tools 126 | (gptel-make-tool 127 | :name "shell-command" 128 | :description "Execute a shell command and return its output. The user must approve every execution." 129 | :args '((:name "command" 130 | :type string 131 | :description "The shell command to execute.")) 132 | :function (lambda (command) 133 | (let ((confirmed (y-or-n-p (format "Superchat wants to execute shell command: %S. Allow?" command)))) 134 | (when (and confirmed (fboundp 'superchat--extend-timeout)) 135 | (superchat--extend-timeout)) 136 | (if confirmed 137 | (shell-command-to-string command) 138 | "Error: User refused to execute the command."))) 139 | :category "system")) 140 | 141 | ;;;--------------------------------------------- 142 | ;;; Tool: write-file (Upgraded with diff-approval) 143 | ;;;--------------------------------------------- 144 | 145 | (add-to-list 'gptel-tools 146 | (gptel-make-tool 147 | :name "write-file" 148 | :description "Writes content to a specified file with smart preview. Shows appropriate preview based on file size and operation type." 149 | :args '((:name "path" 150 | :type string 151 | :description "The path of the file to write to.") 152 | (:name "content" 153 | :type string 154 | :description "The content to write to the file.")) 155 | :function (lambda (path content) 156 | (let ((expanded-path (expand-file-name path))) 157 | (if (superchat-tool--confirm-diff expanded-path content) 158 | (progn 159 | ;; Ensure directory exists 160 | (let ((dir (file-name-directory expanded-path))) 161 | (when (and dir (not (file-directory-p dir))) 162 | (make-directory dir t))) 163 | (with-temp-buffer 164 | (insert content) 165 | (write-region (point-min) (point-max) expanded-path nil 'nomessage)) 166 | (format "✅ File '%s' written successfully (%d chars)." 167 | expanded-path (length content))) 168 | "❌ User refused to write to the file."))) 169 | :category "filesystem")) 170 | 171 | ;;;--------------------------------------------- 172 | ;;; Tool: append-file (New improved tool) 173 | ;;;--------------------------------------------- 174 | 175 | (add-to-list 'gptel-tools 176 | (gptel-make-tool 177 | :name "append-file" 178 | :description "Appends content to the end of an existing file, or creates a new file if it doesn't exist. Much more convenient than write-file for adding content." 179 | :args '((:name "path" 180 | :type string 181 | :description "The path of the file to append to.") 182 | (:name "content" 183 | :type string 184 | :description "The content to append to the file.") 185 | (:name "newline" 186 | :type boolean 187 | :description "Whether to add a newline before the content (default: true)." 188 | :optional t)) 189 | :function (lambda (path content &optional newline) 190 | (let* ((expanded-path (expand-file-name path)) 191 | (add-newline (if (eq newline nil) t newline)) ; Default to true 192 | (final-content (if add-newline 193 | (concat "\n" content) 194 | content))) 195 | (if (superchat-tool--confirm-diff expanded-path final-content 'append) 196 | (progn 197 | ;; Ensure directory exists 198 | (let ((dir (file-name-directory expanded-path))) 199 | (when (and dir (not (file-directory-p dir))) 200 | (make-directory dir t))) 201 | ;; Append to file 202 | (with-temp-buffer 203 | (insert final-content) 204 | (write-region (point-min) (point-max) expanded-path t 'nomessage)) 205 | (format "✅ Content appended to '%s' successfully (%d chars added)." 206 | expanded-path (length final-content))) 207 | "❌ User refused to append to the file."))) 208 | :category "filesystem")) 209 | 210 | ;;;--------------------------------------------- 211 | ;;; Tool: quick-write (New fast tool for small files) 212 | ;;;--------------------------------------------- 213 | 214 | (add-to-list 'gptel-tools 215 | (gptel-make-tool 216 | :name "quick-write" 217 | :description "Quickly writes small content to a file with minimal confirmation. Best for small files under 1000 characters. Only shows a simple confirmation." 218 | :args '((:name "path" 219 | :type string 220 | :description "The path of the file to write to.") 221 | (:name "content" 222 | :type string 223 | :description "The content to write (preferably under 1000 chars).")) 224 | :function (lambda (path content) 225 | (let* ((expanded-path (expand-file-name path)) 226 | (content-length (length content)) 227 | (file-exists (file-exists-p expanded-path)) 228 | (action (if file-exists "replace" "create"))) 229 | 230 | ;; Quick confirmation for small files 231 | (if (and (< content-length 1000) 232 | (y-or-n-p (format "Quick %s: %s (%d chars)? " 233 | action expanded-path content-length))) 234 | (progn 235 | ;; Ensure directory exists 236 | (let ((dir (file-name-directory expanded-path))) 237 | (when (and dir (not (file-directory-p dir))) 238 | (make-directory dir t))) 239 | (with-temp-buffer 240 | (insert content) 241 | (write-region (point-min) (point-max) expanded-path nil 'nomessage)) 242 | (format "⚡ Quick %s successful: '%s' (%d chars)" 243 | action expanded-path content-length)) 244 | (if (>= content-length 1000) 245 | "❌ Content too large for quick-write (use write-file for files >1000 chars)" 246 | "❌ User refused quick write operation.")))) 247 | :category "filesystem")) 248 | 249 | ;;;--------------------------------------------- 250 | ;;; Tool: read-file 251 | ;;;--------------------------------------------- 252 | 253 | (add-to-list 'gptel-tools 254 | (gptel-make-tool 255 | :name "read-file" 256 | :description "Reads the entire content of a specified file. Requires user approval for each file access." 257 | :args '((:name "path" 258 | :type string 259 | :description "The path of the file to read.")) 260 | :function (lambda (path) 261 | (let ((expanded-path (expand-file-name path))) 262 | (if (not (file-exists-p expanded-path)) 263 | (format "Error: File does not exist at path: %s" expanded-path) 264 | (let ((confirmed (y-or-n-p (format "Superchat wants to read file: %S. Allow?" expanded-path)))) 265 | (when (and confirmed (fboundp 'superchat--extend-timeout)) 266 | (superchat--extend-timeout)) 267 | (if confirmed 268 | (with-temp-buffer 269 | (insert-file-contents expanded-path) 270 | (buffer-string)) 271 | "Error: User refused to allow reading the file."))))) 272 | :category "filesystem")) 273 | 274 | ;;;--------------------------------------------- 275 | ;;; Tool: list-files 276 | ;;;--------------------------------------------- 277 | 278 | (add-to-list 'gptel-tools 279 | (gptel-make-tool 280 | :name "list-files" 281 | :description "Lists files and subdirectories in a specified directory, similar to 'ls -l'. Requires user approval." 282 | :args '((:name "path" 283 | :type string 284 | :description "The path to the directory to list. Defaults to the current directory if not provided." 285 | :optional t)) 286 | :function (lambda (&optional path) 287 | (let* ((target-path (or path default-directory)) 288 | (expanded-path (expand-file-name target-path))) 289 | (if (not (file-directory-p expanded-path)) 290 | (format "Error: Path is not a valid directory: %s" expanded-path) 291 | (let ((confirmed (y-or-n-p (format "Superchat wants to list directory: %S. Allow?" expanded-path)))) 292 | (when (and confirmed (fboundp 'superchat--extend-timeout)) 293 | (superchat--extend-timeout)) 294 | (if confirmed 295 | (let* ((files-and-attrs (directory-files-and-attributes expanded-path nil nil nil))) 296 | (if (not files-and-attrs) 297 | (format "Directory is empty: %s" expanded-path) 298 | (mapconcat 299 | (lambda (file-attr) 300 | (let* ((filepath (car file-attr)) 301 | (attrs (cdr file-attr)) 302 | (is-dir (eq 'directory (nth 7 attrs))) 303 | (size (nth 5 attrs)) 304 | (mod-time (nth 4 attrs)) 305 | (perms (nth 0 attrs)) 306 | (filename (file-name-nondirectory filepath))) 307 | (format "%s %10d %s %s" 308 | perms 309 | size 310 | (format-time-string "%Y-%m-%d %H:%M" mod-time) 311 | (if is-dir (concat filename "/") filename)))) 312 | files-and-attrs 313 | "\n"))) 314 | "Error: User refused to list the directory."))))) 315 | :category "filesystem")) 316 | 317 | ;;;--------------------------------------------- 318 | ;;; Tool: search-text 319 | ;;;--------------------------------------------- 320 | 321 | (add-to-list 'gptel-tools 322 | (gptel-make-tool 323 | :name "search-text" 324 | :description "Searches for a textual pattern (or regular expression) in files. Requires user approval." 325 | :args '((:name "pattern" 326 | :type string 327 | :description "The text or regular expression to search for.") 328 | (:name "path" 329 | :type string 330 | :description "The specific file or directory to search in. Defaults to the current directory if not provided." 331 | :optional t)) 332 | :function (lambda (pattern &optional path) 333 | (let* ((search-path (or path ".")) 334 | ;; Using ripgrep (rg) is ideal. Fallback to standard grep if not available. 335 | (program (if (executable-find "rg") "rg" "grep")) 336 | (command 337 | (cond 338 | ((equal program "rg") 339 | ;; rg's --json output is great for machines, but text is fine for now. 340 | (format "rg -n -- %s %s" 341 | (shell-quote-argument pattern) 342 | (shell-quote-argument search-path))) 343 | (t ; Fallback to grep 344 | (format "grep -r -n -- %s %s" 345 | (shell-quote-argument pattern) 346 | (shell-quote-argument search-path)))))) 347 | (let ((confirmed (y-or-n-p (format "Superchat wants to search for pattern '%s' in '%s'. Allow?" pattern search-path)))) 348 | (when (and confirmed (fboundp 'superchat--extend-timeout)) 349 | (superchat--extend-timeout)) 350 | (if confirmed 351 | (shell-command-to-string command) 352 | "Error: User refused to perform the search.")))) 353 | :category "filesystem")) 354 | 355 | 356 | ;; NOTE: Duplicate tools removed - using the versions with user confirmation above 357 | ;; - read-file (line 260) already provides this functionality with security 358 | ;; - list-files (line 287) already provides this functionality with security 359 | 360 | ;;;--------------------------------------------- 361 | ;;; Tool: create directory 362 | ;;;--------------------------------------------- 363 | 364 | (add-to-list 'gptel-tools 365 | (gptel-make-tool 366 | :function (lambda (parent name) 367 | (condition-case nil 368 | (progn 369 | (make-directory (expand-file-name name parent) t) 370 | (format "Directory %s created/verified in %s" name parent)) 371 | (error (format "Error creating directory %s in %s" name parent)))) 372 | :name "make_directory" 373 | :description "Create a new directory with the given name in the specified parent directory" 374 | :args (list '(:name "parent" 375 | :type string 376 | :description "The parent directory where the new directory should be created, e.g. /tmp") 377 | '(:name "name" 378 | :type string 379 | :description "The name of the new directory to create, e.g. testdir")) 380 | :category "filesystem")) 381 | 382 | ;; NOTE: edit_file tool removed - function ds/gptel--edit_file is not defined 383 | ;; This tool was causing errors. Use write-file or append-file instead. 384 | 385 | ;;;--------------------------------------------- 386 | ;;; Tool: find-files 387 | ;;;--------------------------------------------- 388 | 389 | (add-to-list 'gptel-tools 390 | (gptel-make-tool 391 | :name "find-files" 392 | :description "Recursively finds files matching a glob pattern. Requires user approval." 393 | :args '((:name "pattern" 394 | :type string 395 | :description "The glob pattern to match files against, e.g., '*.js' or 'src/**/*.py'.") 396 | (:name "path" 397 | :type string 398 | :description "The directory to start the search from. Defaults to the current directory." 399 | :optional t)) 400 | :function (lambda (pattern &optional path) 401 | (let* ((search-path (or path default-directory)) 402 | (expanded-path (expand-file-name search-path))) 403 | (if (not (file-directory-p expanded-path)) 404 | (format "Error: Path is not a valid directory: %s" expanded-path) 405 | (let ((confirmed (y-or-n-p (format "Superchat wants to find files matching '%s' in %S. Allow?" pattern expanded-path)))) 406 | (when (and confirmed (fboundp 'superchat--extend-timeout)) 407 | (superchat--extend-timeout)) 408 | (if confirmed 409 | (let* ((regexp (glob-to-regexp pattern)) 410 | (files (directory-files-recursively expanded-path regexp))) 411 | (if files 412 | (string-join files "\n") 413 | (format "No files found matching pattern '%s' in %s." pattern expanded-path))) 414 | "Error: User refused to perform the file search."))))) 415 | :category "filesystem")) 416 | 417 | ;;;--------------------------------------------- 418 | ;;; Tool: read buffer 419 | ;;;--------------------------------------------- 420 | 421 | (add-to-list 'gptel-tools 422 | (gptel-make-tool 423 | :function (lambda (buffer) 424 | (unless (buffer-live-p (get-buffer buffer)) 425 | (error "Error: buffer %s is not live." buffer)) 426 | (with-current-buffer buffer 427 | (buffer-substring-no-properties (point-min) (point-max)))) 428 | :name "read_buffer" 429 | :description "Return the contents of an Emacs buffer" 430 | :args (list '(:name "buffer" 431 | :type string 432 | :description "The name of the buffer whose contents are to be retrieved")) 433 | :category "emacs")) 434 | 435 | ;;;--------------------------------------------- 436 | ;;; Tool: append to buffer 437 | ;;;--------------------------------------------- 438 | 439 | (add-to-list 'gptel-tools 440 | (gptel-make-tool 441 | :function (lambda (buffer text) 442 | (with-current-buffer (get-buffer-create buffer) 443 | (save-excursion 444 | (goto-char (point-max)) 445 | (insert text))) 446 | (format "Appended text to buffer %s" buffer)) 447 | :name "append_to_buffer" 448 | :description "Append text to an Emacs buffer. If the buffer does not exist, it will be created." 449 | :args (list '(:name "buffer" 450 | :type string 451 | :description "The name of the buffer to append text to.") 452 | '(:name "text" 453 | :type string 454 | :description "The text to append to the buffer.")) 455 | :category "emacs")) 456 | 457 | ;;;--------------------------------------------- 458 | ;;; Tool: edit buffer 459 | ;;;--------------------------------------------- 460 | (defun codel-edit-buffer (buffer-name old-string new-string) 461 | "In BUFFER-NAME, replace OLD-STRING with NEW-STRING." 462 | (with-current-buffer buffer-name 463 | (let ((case-fold-search nil)) ;; Case-sensitive search 464 | (save-excursion 465 | (goto-char (point-min)) 466 | (let ((count 0)) 467 | (while (search-forward old-string nil t) 468 | (setq count (1+ count))) 469 | (if (= count 0) 470 | (format "Error: Could not find text to replace in buffer %s" buffer-name) 471 | (if (> count 1) 472 | (format "Error: Found %d matches for the text to replace in buffer %s" count buffer-name) 473 | (goto-char (point-min)) 474 | (search-forward old-string) 475 | (replace-match new-string t t) 476 | (format "Successfully edited buffer %s" buffer-name)))))))) 477 | (add-to-list 'gptel-tools 478 | (gptel-make-tool 479 | :name "EditBuffer" 480 | :function #'codel-edit-buffer 481 | :description "Edits Emacs buffers" 482 | :args '((:name "buffer_name" 483 | :type string 484 | :description "Name of the buffer to modify") 485 | (:name "old_string" 486 | :type string 487 | :description "Text to replace (must match exactly)") 488 | (:name "new_string" 489 | :type string 490 | :description "Text to replace old_string with")) 491 | :category "edit")) 492 | 493 | ;;;--------------------------------------------- 494 | ;;; Tool: replace buffer 495 | ;;;--------------------------------------------- 496 | (defun codel-replace-buffer (buffer-name content) 497 | "Completely replace contents of BUFFER-NAME with CONTENT." 498 | (with-current-buffer buffer-name 499 | (erase-buffer) 500 | (insert content) 501 | (format "Buffer replaced: %s" buffer-name))) 502 | 503 | (add-to-list 'gptel-tools 504 | (gptel-make-tool 505 | :name "ReplaceBuffer" 506 | :function #'codel-replace-buffer 507 | :description "Completely overwrites buffer contents" 508 | :args '((:name "buffer_name" 509 | :type string 510 | :description "Name of the buffer to overwrite") 511 | (:name "content" 512 | :type string 513 | :description "Content to write to the buffer")) 514 | :category "edit")) 515 | 516 | (provide 'superchat-tools) 517 | 518 | ;;; superchat-tools.el ends here 519 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright © 2007 Free Software Foundation, Inc. 5 | 6 | Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | The GNU General Public License is a free, copyleft license for software and other kinds of works. 10 | 11 | The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. 12 | 13 | When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. 14 | 15 | To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. 16 | 17 | For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. 18 | 19 | Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. 20 | 21 | For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. 22 | 23 | Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. 24 | 25 | Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. 26 | 27 | The precise terms and conditions for copying, distribution and modification follow. 28 | 29 | TERMS AND CONDITIONS 30 | 0. Definitions. 31 | “This License” refers to version 3 of the GNU General Public License. 32 | 33 | “Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. 34 | 35 | “The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations. 36 | 37 | To “modify” a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a “modified version” of the earlier work or a work “based on” the earlier work. 38 | 39 | A “covered work” means either the unmodified Program or a work based on the Program. 40 | 41 | To “propagate” a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. 42 | 43 | To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. 44 | 45 | An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 46 | 47 | 1. Source Code. 48 | The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work. 49 | 50 | A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. 51 | 52 | The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. 53 | 54 | The “Corresponding Source” for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. 55 | 56 | The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. 57 | 58 | The Corresponding Source for a work in source code form is that same work. 59 | 60 | 2. Basic Permissions. 61 | All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. 62 | 63 | You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. 64 | 65 | Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 66 | 67 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 68 | No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. 69 | 70 | When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 71 | 72 | 4. Conveying Verbatim Copies. 73 | You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. 74 | 75 | You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 76 | 77 | 5. Conveying Modified Source Versions. 78 | You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: 79 | 80 | a) The work must carry prominent notices stating that you modified it, and giving a relevant date. 81 | b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to “keep intact all notices”. 82 | c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. 83 | d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. 84 | A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 85 | 86 | 6. Conveying Non-Source Forms. 87 | You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: 88 | 89 | a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. 90 | b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. 91 | c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. 92 | d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. 93 | e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. 94 | A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. 95 | 96 | A “User Product” is either (1) a “consumer product”, which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, “normally used” refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. 97 | 98 | “Installation Information” for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. 99 | 100 | If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). 101 | 102 | The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. 103 | 104 | Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 105 | 106 | 7. Additional Terms. 107 | “Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. 108 | 109 | When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. 110 | 111 | Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: 112 | 113 | a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or 114 | b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or 115 | c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or 116 | d) Limiting the use for publicity purposes of names of licensors or authors of the material; or 117 | e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or 118 | f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. 119 | All other non-permissive additional terms are considered “further restrictions” within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. 120 | 121 | If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. 122 | 123 | Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 124 | 125 | 8. Termination. 126 | You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). 127 | 128 | However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. 129 | 130 | Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. 131 | 132 | Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 133 | 134 | 9. Acceptance Not Required for Having Copies. 135 | You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 136 | 137 | 10. Automatic Licensing of Downstream Recipients. 138 | Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. 139 | 140 | An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. 141 | 142 | You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 143 | 144 | 11. Patents. 145 | A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's “contributor version”. 146 | 147 | A contributor's “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. 148 | 149 | Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. 150 | 151 | In the following three paragraphs, a “patent license” is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To “grant” such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. 152 | 153 | If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. “Knowingly relying” means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. 154 | 155 | If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. 156 | 157 | A patent license is “discriminatory” if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. 158 | 159 | Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 160 | 161 | 12. No Surrender of Others' Freedom. 162 | If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 163 | 164 | 13. Use with the GNU Affero General Public License. 165 | Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 166 | 167 | 14. Revised Versions of this License. 168 | The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. 169 | 170 | Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. 171 | 172 | If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. 173 | 174 | Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 175 | 176 | 15. Disclaimer of Warranty. 177 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 178 | 179 | 16. Limitation of Liability. 180 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 181 | 182 | 17. Interpretation of Sections 15 and 16. 183 | If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. 184 | 185 | END OF TERMS AND CONDITIONS 186 | 187 | How to Apply These Terms to Your New Programs 188 | If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. 189 | 190 | To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found. 191 | 192 | 193 | Copyright (C) 194 | 195 | This program is free software: you can redistribute it and/or modify 196 | it under the terms of the GNU General Public License as published by 197 | the Free Software Foundation, either version 3 of the License, or 198 | (at your option) any later version. 199 | 200 | This program is distributed in the hope that it will be useful, 201 | but WITHOUT ANY WARRANTY; without even the implied warranty of 202 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 203 | GNU General Public License for more details. 204 | 205 | You should have received a copy of the GNU General Public License 206 | along with this program. If not, see . 207 | Also add information on how to contact you by electronic and paper mail. 208 | 209 | If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: 210 | 211 | Copyright (C) 212 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 213 | This is free software, and you are welcome to redistribute it 214 | under certain conditions; type `show c' for details. 215 | The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an “about box”. 216 | 217 | You should also get your employer (if you work as a programmer) or school, if any, to sign a “copyright disclaimer” for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . 218 | 219 | The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . --------------------------------------------------------------------------------