Config: extract EMMS and clean up config, add gptel session save with AI-generated filenames

This commit is contained in:
User
2026-04-17 02:35:16 +08:00
parent 263df285fc
commit a755ad5a04
9 changed files with 274 additions and 212 deletions

View File

@@ -5,10 +5,10 @@
;;; Code:
(use-package gptel
:hook (gptel-mode-hook . visual-line-mode)
:config
(setq gptel-default-mode 'org-mode)
(defun my/org-auto-gptel ()
(when (derived-mode-p 'org-mode)
(save-excursion
@@ -86,12 +86,77 @@
(setq gptel-model 'kimi-for-coding
gptel-backend (gptel-get-backend "Kimi-Code"))
(defun my-gptel--sanitize-filename (name)
"Clean NAME into a safe kebab-case filename string."
(let ((clean (downcase name)))
(setq clean (replace-regexp-in-string "[^a-z0-9_-]" "-" clean))
(setq clean (replace-regexp-in-string "^-+\\|[-]+$" "" clean))
(setq clean (replace-regexp-in-string "-[-]+" "-" clean))
(string-trim clean)))
(defun my-gptel-save-session ()
"Save current gptel session to ~/gptel/ with AI-generated filename.
The filename is prefixed with a YYYYMMDD-HHmmss timestamp and generated
from the last 1500 characters of the buffer."
(interactive)
(unless gptel-mode
(user-error "Not a gptel session buffer"))
(let* ((buf (current-buffer))
(content (with-current-buffer buf
(buffer-substring-no-properties (point-min) (point-max))))
(tail (substring content (max 0 (- (length content) 1500))))
(prompt-text (format "Based on the following conversation, generate a short kebab-case filename (3 to 5 lowercase English words, hyphen-separated, no file extension). Output ONLY the filename, nothing else.\n\n%s"
tail)))
(message "Generating filename with AI...")
(let ((fsm (gptel-request prompt-text
:system "You are a filename generator. Output ONLY a short kebab-case filename (3-5 lowercase English words separated by hyphens). No explanations, no quotes, no punctuation, no file extension."
:stream nil
:callback
(lambda (response info)
(condition-case err
(cond
((null response)
(message "Failed to generate filename: %s"
(or (plist-get info :status) "unknown error")))
((not (stringp response))
(message "Unexpected response type: %S" response))
(t
(let* ((generated (my-gptel--sanitize-filename (string-trim response)))
(generated (if (string-empty-p generated)
"gptel-session"
generated))
(timestamp (format-time-string "%Y%m%d-%H%M%S"))
(dir (expand-file-name "~/gptel/"))
(base (concat timestamp "-" generated))
(file (expand-file-name (concat base ".org") dir)))
;; handle name collision
(when (file-exists-p file)
(setq base (concat timestamp "-" generated "-" (format-time-string "%S")))
(setq file (expand-file-name (concat base ".org") dir)))
(unless (file-directory-p dir)
(make-directory dir t))
;; write-file triggers gptel--save-state to preserve metadata
(with-current-buffer buf
(write-file file))
(if (file-exists-p file)
(progn
(pop-to-buffer (find-file-noselect file))
(message "Saved gptel session to %s" file))
(message "ERROR: File was not created: %s" file)))))
(error (message "gptel save-session error: %S" err)))))))
(unless (plist-get (gptel-fsm-info fsm) :callback)
(message "WARNING: gptel-request did not register callback!")))))
(with-eval-after-load 'gptel
(define-key gptel-mode-map (kbd "C-c C-s") #'my-gptel-save-session)
(setf gptel--system-message
"You are a large language model living in Emacs and a helpful assistant. Respond concisely.
- NEVER use unescaped org-mode headings. Lines MUST NOT begin with `* `, `** `, `*** `, etc. If you must start a line with asterisks, use the standard org escape format `,*` (e.g. `,* Heading`, `,** Sub-heading`).")))
(use-package gptel-agent
:init
(with-eval-after-load 'gptel
(require 'gptel-agent))
:config
(add-to-list 'gptel-agent-dirs "~/.emacs.d/lisp/" t)
(gptel-agent-update))