Repository navigation
Expand file tree
/
Copy patheca-chat-compose.el
More file actions
198 lines (181 loc) · 8.72 KB
/
Copy patheca-chat-compose.el
File metadata and controls
198 lines (181 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
;;; eca-chat-compose.el --- ECA chat prompt compose buffer -*- lexical-binding: t; -*-
;; Copyright (C) 2025 Eric Dallo
;;
;; SPDX-License-Identifier: Apache-2.0
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Compose a chat prompt in a dedicated markdown buffer instead of
;; the inline chat prompt field, convenient for long or multi-line
;; prompts. `eca-chat-compose' opens a buffer targeting the current
;; chat; `C-c C-c' sends the buffer content as a prompt to that chat
;; and `C-c C-k' discards it. @context and #filepath mentions
;; complete against the ECA server just like in the chat prompt, and
;; yanking a clipboard image inserts an @file mention pointing at a
;; saved screenshot, mirroring the chat buffer behavior.
;;
;;; Code:
(require 'eca-util)
(require 'eca-chat)
;; Variables
(defvar-local eca-chat-compose--target-buffer nil
"The chat buffer the composed prompt will be sent to.")
(defvar-local eca-chat-compose--original-prompt nil
"The text of the target chat prompt this buffer took over, or nil.
It goes back to the chat prompt when the buffer is killed unsent.")
;; Internal
(defun eca-chat-compose--restore-prompt ()
"Put the taken-over prompt text back into the target chat prompt.
Run from `kill-buffer-hook', so cancelling or killing the compose
buffer unsent leaves the chat prompt as it was."
(when-let* ((prompt eca-chat-compose--original-prompt)
(target eca-chat-compose--target-buffer)
((buffer-live-p target)))
(eca-chat--with-current-buffer target
(eca-chat--set-prompt prompt))))
(defun eca-chat-compose--yank-image-handler (type data)
"Save clipboard image DATA of mime TYPE and insert an @file mention.
Writes the image to a temporary eca-screenshot file, like the eca
chat buffer does, and inserts \"@/path/to/file \" at point so the
server picks it up as a file context when the prompt is sent. RET
on the mention toggles the image thumbnail."
(when-let* ((output-path (eca-chat-media--save-clipboard-image type data)))
(unless (memq (char-before) '(nil ?\s ?\t ?\n))
(insert " "))
(let ((start (point)))
(insert (eca-chat--propertize-image-mention
(concat eca-chat-context-prefix output-path)
output-path))
(eca-chat--add-image-link-overlay start (point)))
(insert " ")
(eca-info "Image added, size: %s"
(file-size-human-readable (file-attribute-size (file-attributes output-path))))))
;; Public
(defun eca-chat-compose-tab ()
"Complete the @/# mention at point, else do markdown cycling.
With point after an @context or #filepath prefix this triggers
`completion-at-point'; anywhere else it behaves like TAB in
`markdown-mode'."
(interactive)
(if (eca-chat--completion-type-at-point)
(completion-at-point)
(call-interactively #'markdown-cycle)))
(defun eca-chat-compose-yank ()
"Yank into the compose buffer, routing images through `yank-media'.
A clipboard image is saved to a temporary file and inserted as an
@file mention; anything else falls back to a plain `yank'."
(interactive)
(if (and (fboundp 'yank-media)
(boundp 'yank-media--registered-handlers)
yank-media--registered-handlers
(eca-chat--clipboard-image-p))
(call-interactively #'yank-media)
(call-interactively #'yank)))
(eca-chat-define-derived-mode eca-chat-compose-mode "eca-chat-compose"
"Major mode for composing a prompt destined for an ECA chat.
The target chat is captured when the buffer is created by
`eca-chat-compose'. Yanking a clipboard image inserts an @file
mention pointing at a temporary screenshot file, mirroring the
eca chat buffer behavior.
\\{eca-chat-compose-mode-map}"
(setq header-line-format
(substitute-command-keys
"Compose prompt: \\[eca-chat-compose-send] to send, \
\\[eca-chat-compose-cancel] to cancel"))
;; ECA server completion for @contexts and #filepaths, mirroring the
;; chat prompt setup including its completion-style overrides.
(setq-local completion-at-point-functions (list #'eca-chat-completion-at-point))
(setq-local completion-category-defaults
(cons '(eca-capf (styles basic substring))
completion-category-defaults))
(setq-local completion-ignore-case t)
(eca-chat--setup-item-stickiness)
(add-hook 'after-change-functions
#'eca-chat--auto-show-image-thumbnails-after-change nil t)
(add-hook 'kill-buffer-hook #'eca-chat-compose--restore-prompt nil t)
;; Paste image from clipboard support, mirroring eca-chat-mode: drop
;; the handlers inherited from markdown-mode (which insert markdown
;; image links) and register the @file mention handler.
(when (fboundp 'yank-media-handler)
(setq-local yank-media--registered-handlers nil)
(yank-media-handler "image/png" #'eca-chat-compose--yank-image-handler)
(yank-media-handler "image/jpeg" #'eca-chat-compose--yank-image-handler)
(yank-media-handler "image/jpg" #'eca-chat-compose--yank-image-handler)
(yank-media-handler "image/gif" #'eca-chat-compose--yank-image-handler)
(yank-media-handler "image/webp" #'eca-chat-compose--yank-image-handler)))
(define-key eca-chat-compose-mode-map (kbd "C-c C-c") #'eca-chat-compose-send)
(define-key eca-chat-compose-mode-map (kbd "C-c C-k") #'eca-chat-compose-cancel)
(define-key eca-chat-compose-mode-map [remap yank] #'eca-chat-compose-yank)
(define-key eca-chat-compose-mode-map (kbd "TAB") #'eca-chat-compose-tab)
(define-key eca-chat-compose-mode-map (kbd "<tab>") #'eca-chat-compose-tab)
;;;###autoload
(defun eca-chat-compose ()
"Compose a prompt for the current chat in a dedicated buffer.
When called from a chat buffer the prompt targets that chat,
otherwise it targets the session's last used chat. The text typed
in the chat prompt moves into the buffer, with point after it.
\\<eca-chat-compose-mode-map>\\[eca-chat-compose-send] sends the \
buffer content as a prompt to that chat;
\\[eca-chat-compose-cancel] discards it and puts the moved text back."
(interactive)
(let ((session (eca-session)))
(eca-assert-session-running session)
(let ((target (if (derived-mode-p 'eca-chat-mode)
(current-buffer)
(eca-chat--get-last-buffer session))))
(unless (buffer-live-p target)
(user-error "No chat buffer found to compose for"))
(let ((buffer (generate-new-buffer
(format "*eca-compose:%s*" (buffer-name target)))))
(with-current-buffer buffer
(eca-chat-compose-mode)
(setq eca-chat-compose--target-buffer target)
;; Make `eca-session' resolve to the target's session from
;; this buffer, so completion and the send path work
;; regardless of where the compose window ends up.
(setq-local eca--session-id-cache (eca--session-id session))
(when-let* ((dir (car (eca--session-workspace-folders session))))
(setq-local default-directory dir))
;; Carry the target's chat id so @/# completion queries
;; (chat/queryContext, chat/queryFiles) run against that chat.
(setq-local eca-chat--id (buffer-local-value 'eca-chat--id target))
(when-let* ((prompt (with-current-buffer target
(eca-chat--prompt-content)))
((not (string-empty-p prompt))))
(insert prompt)
(setq eca-chat-compose--original-prompt prompt)
(eca-chat--with-current-buffer target
(eca-chat--set-prompt ""))))
(pop-to-buffer buffer)))))
(defun eca-chat-compose-send ()
"Send the composed prompt to the captured target chat.
Kills the compose buffer afterwards."
(interactive)
(let ((text (string-trim (buffer-string)))
(target eca-chat-compose--target-buffer)
(session (eca-session)))
(eca-assert-session-running session)
(when (string-empty-p text)
(user-error "Nothing to send"))
(unless (buffer-live-p target)
(user-error "The target chat buffer no longer exists"))
(setf (eca--session-last-chat-buffer session) target)
(eca-chat--with-current-buffer target
;; Sent with its text properties from the prompt field, as on RET:
;; `eca-chat--send-prompt' expands the @context and #filepath items
;; of the text and takes their contexts from the prompt field.
(eca-chat--set-prompt text)
(eca-chat--send-prompt session text))
(setq eca-chat-compose--original-prompt nil)
(quit-window t)
(eca-info "Prompt sent to %s" (buffer-name target))))
(defun eca-chat-compose-cancel ()
"Discard the composed prompt and kill the compose buffer.
The chat prompt gets back the text the buffer took over."
(interactive)
(quit-window t)
(eca-info "Compose cancelled"))
(provide 'eca-chat-compose)
;;; eca-chat-compose.el ends here