-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcopilot-chat-shell-maker.el
165 lines (138 loc) · 6.36 KB
/
copilot-chat-shell-maker.el
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
;;; copilot-chat --- copilot-chat-shell-maker.el --- copilot chat interface, shell-maker frontend -*- indent-tabs-mode: nil; lisp-indent-offset: 2; lexical-binding: t -*-
;; Copyright (C) 2024 copilot-chat maintainers
;; The MIT License (MIT)
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Commentary:
;;; Code:
(require 'shell-maker)
(require 'copilot-chat-copilot)
;; Variables
(defvar copilot-chat--shell-cb-fn nil)
(defvar copilot-chat--shell-config
(make-shell-maker-config
:name "Copilot-Chat"
:execute-command 'copilot-chat--shell-cb))
(defvar copilot-chat--shell-maker-answer-point 0
"Start of the current answer.")
;; Constants
(defconst copilot-chat--shell-maker-temp-buffer "*copilot-chat-shell-maker-temp*")
;; Functions
(defun copilot-chat--shell-maker-prompt-send()
"Function to send the prompt content."
(with-current-buffer (copilot-chat--shell-maker-get-buffer)
(shell-maker-submit)
(display-buffer (current-buffer))))
(defun copilot-chat--shell-maker-get-buffer ()
(unless (buffer-live-p copilot-chat--buffer)
(setq copilot-chat--buffer (copilot-chat--shell)))
(let ((tempb (get-buffer-create copilot-chat--shell-maker-temp-buffer))
(inhibit-read-only t))
(with-current-buffer tempb
(markdown-view-mode))
copilot-chat--buffer))
(defun copilot-chat--shell-maker-font-lock-faces ()
"Replace faces by font-lock-faces."
(with-current-buffer copilot-chat--shell-maker-temp-buffer
(let ((inhibit-read-only t))
(font-lock-ensure)
(goto-char (point-min))
(while (not (eobp))
(let ((next-change (or (next-property-change (point) nil (point-max)) (point-max)))
(face (get-text-property (point) 'face)))
(when face
(font-lock-append-text-property (point) next-change 'font-lock-face face))
(goto-char next-change))))))
(defun copilot-chat--shell-maker-copy-faces()
"Apply faces to the copilot chat buffer."
(with-current-buffer copilot-chat--shell-maker-temp-buffer
(save-restriction
(widen)
(font-lock-ensure)
(copilot-chat--shell-maker-font-lock-faces)
(let ((content (buffer-substring (point-min) (point-max))))
(with-current-buffer copilot-chat--buffer
(goto-char (1+ copilot-chat--shell-maker-answer-point))
(insert content)
(delete-region (point) (+ (point) (1- (length content))))
(goto-char (point-max)))))))
(defun copilot-chat--shell-cb-prompt (shell content)
"Callback for Copilot Chat shell-maker.
Argument SHELL is the shell-maker instance.
Argument CONTENT is copilot chat answer."
(with-current-buffer copilot-chat--buffer
(goto-char (point-max))
(when copilot-chat--first-word-answer
(setq copilot-chat--first-word-answer nil)
(let ((str (concat (format-time-string "# [%T] ") (format "Copilot(%s):\n" copilot-chat-model)))
(inhibit-read-only t))
(with-current-buffer copilot-chat--shell-maker-temp-buffer
(insert str))
(funcall (map-elt shell :write-output) str)))
(if (string= content copilot-chat--magic)
(progn
(funcall (map-elt shell :finish-output) t); the end
(copilot-chat--shell-maker-copy-faces)
(setq copilot-chat--first-word-answer t))
(progn
(with-current-buffer copilot-chat--shell-maker-temp-buffer
(goto-char (point-max))
(let ((inhibit-read-only t))
(insert content)))
(funcall (map-elt shell :write-output) content)))))
(defun copilot-chat--shell-cb-prompt-wrapper (shell content)
"Wrapper around copilot-chat--shell-cb-prompt.
Argument SHELL is the shell-maker instance.
Argument CONTENT is copilot chat answer."
(if copilot-chat-follow
(copilot-chat--shell-cb-prompt shell content)
(save-excursion
(copilot-chat--shell-cb-prompt shell content))))
(defun copilot-chat--shell-cb (command shell)
"Callback for Copilot Chat shell-maker.
Argument COMMAND is the command to send to Copilot.
Argument CALLBACK is the callback function to call.
Argument ERROR-CALLBACK is the error callback function to call."
(setq
copilot-chat--shell-cb-fn
(apply-partially #'copilot-chat--shell-cb-prompt-wrapper shell)
copilot-chat--shell-maker-answer-point (point))
(let ((inhibit-read-only t))
(with-current-buffer copilot-chat--shell-maker-temp-buffer
(erase-buffer)))
(copilot-chat--ask command copilot-chat--shell-cb-fn))
(defun copilot-chat--shell ()
"Start a Copilot Chat shell."
(shell-maker-start
copilot-chat--shell-config
t nil t
(copilot-chat--get-buffer-name)))
(defun copilot-chat--shell-maker-insert-prompt(prompt)
"Insert PROMPT in the chat buffer."
(with-current-buffer (copilot-chat--shell-maker-get-buffer)
(insert prompt)))
(defun copilot-chat--shell-maker-clean()
"Clean the copilot chat shell-maker frontend."
(when (buffer-live-p copilot-chat--buffer)
(with-current-buffer copilot-chat--buffer
(shell-maker--write-input-ring-history copilot-chat--shell-config)))
(advice-remove 'copilot-chat-prompt-send #'copilot-chat--shell-maker-prompt-send))
(defun copilot-chat-shell-maker-init()
"Initialize the copilot chat shell-maker frontend."
(setq copilot-chat-prompt copilot-chat-markdown-prompt)
(advice-add 'copilot-chat-prompt-send :override #'copilot-chat--shell-maker-prompt-send))
(provide 'copilot-chat-shell-maker)
;;; copilot-chat-shell-maker.el ends here