-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathgptel.el
More file actions
2946 lines (2671 loc) · 131 KB
/
gptel.el
File metadata and controls
2946 lines (2671 loc) · 131 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; gptel.el --- Interact with ChatGPT or other LLMs -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2025 Karthik Chikmagalur
;; Author: Karthik Chikmagalur <karthik.chikmagalur@gmail.com>
;; Version: 0.9.9.4
;; Package-Requires: ((emacs "27.1") (transient "0.7.4") (compat "30.1.0.0"))
;; Keywords: convenience, tools
;; URL: https://github.com/karthink/gptel
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;; This file is NOT part of GNU Emacs.
;;; Commentary:
;; gptel is a simple Large Language Model chat client, with support for multiple
;; models and backends.
;;
;; It works in the spirit of Emacs, available at any time and in any buffer.
;;
;; gptel supports:
;;
;; - The services ChatGPT, Azure, Gemini, Anthropic AI, Together.ai, Perplexity,
;; AI/ML API, Anyscale, OpenRouter, Groq, PrivateGPT, DeepSeek, Cerebras, Github Models,
;; GitHub Copilot chat, AWS Bedrock, Novita AI, xAI, Sambanova, Mistral Le
;; Chat and Kagi (FastGPT & Summarizer).
;; - Local models via Ollama, Llama.cpp, Llamafiles or GPT4All
;;
;; Additionally, any LLM service (local or remote) that provides an
;; OpenAI-compatible API is supported.
;;
;; Features:
;;
;; - Interact with LLMs from anywhere in Emacs (any buffer, shell, minibuffer,
;; wherever).
;; - LLM responses are in Markdown or Org markup.
;; - Supports conversations and multiple independent sessions.
;; - Supports tool-use to equip LLMs with agentic capabilities.
;; - Supports Model Context Protocol (MCP) integration using the mcp.el package.
;; - Supports multi-modal models (send images, documents).
;; - Supports "reasoning" content in LLM responses.
;; - Save chats as regular Markdown/Org/Text files and resume them later.
;; - You can go back and edit your previous prompts or LLM responses when
;; continuing a conversation. These will be fed back to the model.
;; - Redirect prompts and responses easily
;; - Rewrite, refactor or fill in regions in buffers.
;; - Write your own commands for custom tasks with a simple API.
;;
;; Requirements for ChatGPT, Azure, Gemini or Kagi:
;;
;; - You need an appropriate API key. Set the variable `gptel-api-key' to the
;; key or to a function of no arguments that returns the key. (It tries to
;; use `auth-source' by default)
;;
;; ChatGPT is configured out of the box. For the other sources:
;;
;; - For Azure: define a gptel-backend with `gptel-make-azure'.
;; - For Gemini: define a gptel-backend with `gptel-make-gemini'.
;; - For Anthropic (Claude): define a gptel-backend with `gptel-make-anthropic'.
;; - For AI/ML API, Together.ai, Anyscale, Groq, OpenRouter, DeepSeek, Cerebras
;; or Github Models: define a gptel-backend with `gptel-make-openai'.
;; - For PrivateGPT: define a backend with `gptel-make-privategpt'.
;; - For Perplexity: define a backend with `gptel-make-perplexity'.
;; - For Deepseek: define a backend with `gptel-make-deepseek'.
;; - For Kagi: define a gptel-backend with `gptel-make-kagi'.
;;
;; For local models using Ollama, Llama.cpp or GPT4All:
;;
;; - The model has to be running on an accessible address (or localhost)
;; - Define a gptel-backend with `gptel-make-ollama' or `gptel-make-gpt4all'.
;; - Llama.cpp or Llamafiles: Define a gptel-backend with `gptel-make-openai'.
;;
;; Consult the package README for examples and more help with configuring
;; backends.
;;
;; Usage:
;;
;; gptel can be used in any buffer or in a dedicated chat buffer. The
;; interaction model is simple: Type in a query and the response will be
;; inserted below. You can continue the conversation by typing below the
;; response.
;;
;; To use this in any buffer:
;;
;; - Call `gptel-send' to send the buffer's text up to the cursor. Select a
;; region to send only the region.
;;
;; - You can select previous prompts and responses to continue the conversation.
;;
;; - Call `gptel-send' with a prefix argument to access a menu where you can set
;; your backend, model and other parameters, or to redirect the
;; prompt/response.
;;
;; To use this in a dedicated buffer:
;;
;; - M-x gptel: Start a chat session.
;;
;; - In the chat session: Press `C-c RET' (`gptel-send') to send your prompt.
;; Use a prefix argument (`C-u C-c RET') to access a menu. In this menu you
;; can set chat parameters like the system directives, active backend or
;; model, or choose to redirect the input or output elsewhere (such as to the
;; kill ring or the echo area).
;;
;; - You can save this buffer to a file. When opening this file, turn on
;; `gptel-mode' before editing it to restore the conversation state and
;; continue chatting.
;;
;; - To include media files with your request, you can add them to the context
;; (described next), or include them as links in Org or Markdown mode chat
;; buffers. Sending media is disabled by default, you can turn it on globally
;; via `gptel-track-media', or locally in a chat buffer via the header line.
;;
;; Include more context with requests:
;;
;; If you want to provide the LLM with more context, you can add arbitrary
;; regions, buffers, files or directories to the query with `gptel-add'. To add
;; text or media files, call `gptel-add' in Dired or use the dedicated
;; `gptel-add-file'.
;;
;; You can also add context from gptel's menu instead (`gptel-send' with a
;; prefix arg), as well as examine or modify context.
;;
;; When context is available, gptel will include it with each LLM query.
;;
;; LLM Tool use:
;;
;; gptel supports "tool calling" behavior, where LLMs can specify arguments with
;; which to call provided "tools" (elisp functions). The results of running the
;; tools are fed back to the LLM, giving it capabilities and knowledge beyond
;; what is available out of the box. For example, tools can perform web
;; searches or API lookups, modify files and directories, and so on.
;;
;; Tools can be specified via `gptel-make-tool', or obtained from other
;; repositories, or from Model Context Protocol (MCP) servers using the mcp.el
;; package. See the README for details.
;;
;; Tools can be included with LLM queries using gptel's menu, or from
;; `gptel-tools'.
;;
;; Rewrite interface
;;
;; In any buffer: with a region selected, you can rewrite prose, refactor code
;; or fill in the region. This is accessible via `gptel-rewrite', and also from
;; the `gptel-send' menu.
;;
;; Presets
;;
;; Define a bundle of configuration (model, backend, system message, tools etc)
;; as a "preset" that can be applied together, making it easy to switch between
;; tasks in gptel. Presets can be saved and applied from gptel's transient
;; menu. You can also include a cookie of the form "@preset-name" in the prompt
;; to send a request with a preset applied. This feature works everywhere, but
;; preset cookies are also fontified in chat buffers.
;;
;; gptel in Org mode:
;;
;; gptel offers a few extra conveniences in Org mode:
;;
;; - You can limit the conversation context to an Org heading with
;; `gptel-org-set-topic'.
;;
;; - You can have branching conversations in Org mode, where each hierarchical
;; outline path through the document is a separate conversation branch.
;; See the variable `gptel-org-branching-context'.
;;
;; - You can declare the gptel model, backend, temperature, system message and
;; other parameters as Org properties with the command
;; `gptel-org-set-properties'. gptel queries under the corresponding heading
;; will always use these settings, allowing you to create mostly reproducible
;; LLM chat notebooks.
;;
;; Finally, gptel offers a general purpose API for writing LLM ineractions that
;; suit your workflow. See `gptel-request', and `gptel-fsm' for more advanced
;; usage.
;;; Code:
(defconst gptel-version "0.9.9.4")
(declare-function markdown-mode "markdown-mode")
(declare-function gptel-menu "gptel-transient")
(declare-function gptel-system-prompt "gptel-transient")
(declare-function gptel-tools "gptel-transient")
(declare-function gptel--vterm-pre-insert "gptel-integrations")
(declare-function pulse-momentary-highlight-region "pulse")
(declare-function ediff-make-cloned-buffer "ediff-util")
(declare-function ediff-regions-internal "ediff")
(declare-function hl-line-highlight "hl-line")
(declare-function org-escape-code-in-string "org-src")
(declare-function gptel-org-set-topic "gptel-org")
(declare-function gptel-org--save-state "gptel-org")
(declare-function gptel-org--restore-state "gptel-org")
(declare-function gptel-org--annotate-links "gptel-org")
(define-obsolete-function-alias
'gptel-set-topic 'gptel-org-set-topic "0.7.5")
(declare-function markdown-link-at-pos "markdown-mode")
(eval-when-compile
(require 'subr-x))
(require 'cl-lib)
(require 'compat nil t)
(require 'url)
(require 'map)
(require 'text-property-search)
(require 'cl-generic)
(eval-and-compile (require 'gptel-request))
;;; User options
(defcustom gptel-pre-response-hook nil
"Hook run before inserting the LLM response into the current buffer.
This hook is called in the buffer where the LLM response will be
inserted.
Note: this hook only runs if the request succeeds."
:type 'hook
:group 'gptel)
(define-obsolete-variable-alias
'gptel-post-response-hook 'gptel-post-response-functions
"0.6.0"
"Post-response functions are now called with two arguments: the
start and end buffer positions of the response.")
(defcustom gptel-post-response-functions nil
"Abnormal hook run after inserting the LLM response into the current buffer.
This hook is called in the buffer to which the LLM response is
sent, and after the full response has been inserted. Each
function is called with two arguments: the response beginning and
end positions.
Note: this hook runs even if the request fails. In this case the
response beginning and end positions are both the cursor position
at the time of the request."
:type 'hook
:group 'gptel)
(add-hook 'gptel-post-response-functions 'pulse-momentary-highlight-region 70)
(defcustom gptel-post-stream-hook nil
"Hook run after each insertion of the LLM's streaming response.
This hook is called in the buffer from which the prompt was sent
to the LLM, and after a text insertion."
:type 'hook
:group 'gptel)
(defcustom gptel-pre-tool-call-functions nil
"Abnormal hook called before each tool call.
Each hook function is called a plist with the following keys:
:name - the name of the tool being called, a string
:args - a plist of the tool call arguments, as specified in the tool
definition. For a hypothetical edit_file tool that takes three
arguments, a FILENAME, an ORIGINAL and REPLACEMENT strings, this
plist is structured as
(:filename \"/path/to/file.md\"
:original \"...\"
:replacement \"...\")
:buffer - The name of the buffer from which the request was sent.
:backend - The name of the gptel backend used for the request.
:model - The name of the gptel model used for the request.
The function can work by side effects and return nil, or return a plist
with one or more of the following keys.
:stop - If non-nil, stop the request entirely.
:stop-reason - If :stop is non-nil, the reason for stopping. Intended
for the user, not the LLM.
:block - If non-nil, continue the request but block this tool call and
mark it as having erred. Can be a string to send as the
result instead, typically an explanation for why the tool was
not run. Intended for the LLM, not the user.
:confirm - Whether the tool call should seek confirmation from the user.
t and nil are both meaningful, signifying that the tool call
should and should not seek user confirmation, respectively.
When present, this key overrides all other confirmation
options (such as `gptel-confirm-tool-calls' and the tool's
CONFIRM slot).
:args - The updated argument plist for the tool call.
:result - The result of this tool call, used instead of the tool call
output. Not marked as an error."
:type 'hook
:group 'gptel)
(defcustom gptel-post-tool-call-functions nil
"Abnormal hook called after each tool call.
Each hook function is called a plist with the following keys:
:name - the name of the tool being called, a string
:args - a plist of the tool call arguments, as specified in the tool
definition. For a hypothetical edit_file tool that takes three
arguments, a FILENAME, an ORIGINAL and REPLACEMENT strings, this
plist is structured as
(:filename \"/path/to/file.md\"
:original \"...\"
:replacement \"...\")
:result - The tool call result, serialized to a string.
:buffer - The name of the buffer from which the request was sent.
:backend - The name of the gptel backend used for the request.
:model - The name of the gptel model used for the request.
The function can work by side effects and return nil, or return a plist
with one or more of the following keys.
:stop - If non-nil, stop the request entirely.
:stop-reason - If :stop is non-nil, the reason for stopping. Intended
for the user, not the LLM.
:block - If non-nil, continue the request but block this tool call and
mark it as having erred. Can be a string to send as the
result instead, typically an explanation for why the tool was
not run. Intended for the LLM, not the user.
:result - The updated result of this tool call, used instead of the
tool call output. Not marked as an error."
:type 'hook
:group 'gptel)
(defcustom gptel-save-state-hook nil
"Hook run before gptel saves model parameters to a file.
You can use this hook to store additional conversation state or
model parameters to the chat buffer, or to modify the buffer in
some other way."
:type 'hook
:group 'gptel)
(defcustom gptel-default-mode (if (fboundp 'markdown-mode)
'markdown-mode
'text-mode)
"The default major mode for dedicated chat buffers.
If `markdown-mode' is available, it is used. Otherwise gptel
defaults to `text-mode'."
:type 'function
:group 'gptel)
(defcustom gptel-use-header-line t
"Whether `gptel-mode' should use header-line for status information.
When set to nil, use the mode line for (minimal) status
information and the echo area for messages."
:type 'boolean
:group 'gptel)
;; Set minimally to avoid display-buffer action alist conflicts (#533)
(defcustom gptel-display-buffer-action `(nil (body-function . ,#'select-window))
"The action used to display gptel chat buffers.
The gptel buffer is displayed in a window using
(display-buffer BUFFER gptel-display-buffer-action)
The value of this option has the form (FUNCTION . ALIST),
where FUNCTION is a function or a list of functions. Each such
function should accept two arguments: a buffer to display and an
alist of the same form as ALIST. See info node `(elisp)Choosing
Window' for details."
:type display-buffer--action-custom-type
:group 'gptel)
(defcustom gptel-crowdsourced-prompts-file
(let ((cache-dir (or (eval-when-compile
(require 'xdg)
(xdg-cache-home))
user-emacs-directory)))
(expand-file-name "gptel-crowdsourced-prompts.csv" cache-dir))
"File used to store crowdsourced system prompts.
These are prompts cached from an online source (see
`gptel--crowdsourced-prompts-url'), and can be set from the
transient menu interface provided by `gptel-menu'."
:type 'file
:group 'gptel)
(defvar gptel-refresh-buffer-hook '(jit-lock-refontify)
"Hook run in gptel buffers after changing gptel's configuration.
This hook runs in gptel chat buffers after making a change to gptel's
configuration that might require a UI update.")
(defvar-local gptel--bounds nil)
(put 'gptel--bounds 'safe-local-variable #'listp)
(defvar gptel--preset nil
"Name of last applied gptel preset.
For internal use only.")
(put 'gptel--preset 'safe-local-variable #'symbolp)
(defvar-local gptel--tool-names nil
"Store to persist tool names to file across Emacs sessions.
Note: Changing this variable does not affect gptel\\='s behavior
in any way.")
(put 'gptel--tool-names 'safe-local-variable #'listp)
(defvar-local gptel--backend-name nil
"Store to persist backend name across Emacs sessions.
Note: Changing this variable does not affect gptel\\='s behavior
in any way.")
(put 'gptel--backend-name 'safe-local-variable #'stringp)
(defvar-local gptel--old-header-line nil)
(defvar gptel--markdown-block-map
(define-keymap
"<tab>" 'gptel-markdown-cycle-block
"TAB" 'gptel-markdown-cycle-block)
"Keymap for folding and unfolding Markdown code blocks.")
;;; Utility functions
(defun gptel--modify-value (original new-spec)
"Combine ORIGINAL with NEW-SPEC and return the new result.
This function is non-destructive, ORIGINAL is not modified.
NEW-SPEC is either a declarative action spec (plist) of the form
(:key val ...), or a simple value. Recognized spec keys are :append,
:prepend, :eval, :function and :merge. If NEW-SPEC does not have this
form it is returned as is.
- :append and :prepend will append/prepend val (a list or string) to ORIGINAL.
Actions on strings are idempotent, they will only be appended/prepended once.
- :eval will evaluate val and return the result, and
- :function will call val with ORIGINAL as its argument, and return the result.
- :merge will treat ORIGINAL and NEW-SPEC as plists and return a merged plist,
with NEW-SPEC taking precedence."
(if (not (and (consp new-spec) (keywordp (car new-spec))))
new-spec
(let ((current original) (tail new-spec))
(while tail
(let ((key (pop tail)) (form (pop tail)))
(setq current
(pcase key
(:append (if (stringp form)
(if (string-suffix-p form current t)
current (concat current form))
(append current form)))
(:prepend (if (stringp form)
(if (string-prefix-p form current t)
current (concat form current))
(append form current)))
(:eval (eval form t))
(:function (funcall form current))
(:merge (gptel--merge-plists (copy-sequence current) form))
(_ new-spec)))))
current)))
(defun gptel-auto-scroll ()
"Scroll window if LLM response continues below viewport.
Note: This will move the cursor."
(when-let* ((win (get-buffer-window (current-buffer) 'visible))
((not (pos-visible-in-window-p (point) win)))
(scroll-error-top-bottom t))
(condition-case nil
(with-selected-window win
(scroll-up-command))
(error nil))))
(defun gptel-beginning-of-response (&optional beg _end arg)
"Move point to BEG, or to the beginning of the LLM response ARG times."
(interactive (list nil nil
(prefix-numeric-value current-prefix-arg)))
(gptel-end-of-response beg nil (- (or arg 1))))
(defun gptel-end-of-response (&optional beg end arg)
"Move point to end of LLM response.
With BEG, start search from BEG when ARG is negative.
With END, start search from END when ARG is positive.
Otherwise move ARG times, defaulting to 1."
(interactive (list nil nil
(prefix-numeric-value current-prefix-arg)))
(unless arg (setq arg 1))
(let* ((search (if (> arg 0)
#'text-property-search-forward
#'text-property-search-backward))
(goto-prefix-end
(lambda () (when-let* ((prefix (gptel-prompt-prefix-string))
((not (string-empty-p prefix)))
((looking-at (concat "\n\\{1,2\\}"
(regexp-quote prefix) "?"))))
(goto-char (match-end 0)))))
(goto-prefix-beg
(lambda () (when-let* ((prefix (gptel-response-prefix-string))
((not (string-empty-p prefix)))
((looking-back (concat (regexp-quote prefix) "?")
(point-min))))
(goto-char (match-beginning 0))))))
(cond
((and end (> arg 0)) (goto-char end) (cl-decf arg) (funcall goto-prefix-end))
((and beg (< arg 0)) (goto-char beg) (cl-incf arg) (funcall goto-prefix-beg)))
(dotimes (_ (abs arg))
(funcall search 'gptel 'response t)
(if (> arg 0)
(funcall goto-prefix-end)
(funcall goto-prefix-beg)))))
(defun gptel-markdown-cycle-block ()
"Cycle code blocks in Markdown."
(interactive)
(save-excursion
(forward-line 0)
(let (start end (parity 0))
(cond ;Find start and end of block, with possible nested blocks
((looking-at-p "^``` *\n") ;end of block, find corresponding start
(setq parity -1 end (line-end-position))
(while (and (not (= parity 0)) (not (bobp)) (forward-line -1))
(cond ((looking-at-p "^``` *\n") (cl-decf parity))
((looking-at-p "^``` ?[a-z]") (cl-incf parity))))
(when (= parity 0) (setq start (point))))
((looking-at-p "^``` ?[a-z]") ;beginning of block, find corresponding end
(setq parity 1 start (point))
(while (and (not (= parity 0)) (not (eobp)) (forward-line 1))
(cond ((looking-at-p "^``` *\n") (cl-decf parity))
((looking-at-p "^``` ?[a-z]") (cl-incf parity))))
(when (= parity 0) (setq end (line-end-position)))))
(when (and start end)
(goto-char start)
(end-of-line)
(pcase-let* ((`(,value . ,hide-ov)
(get-char-property-and-overlay (point) 'invisible)))
(if (and hide-ov (eq value t))
(delete-overlay hide-ov)
(unless hide-ov (setq hide-ov (make-overlay (point) end)))
(overlay-put hide-ov 'evaporate t)
(overlay-put hide-ov 'invisible t)
(overlay-put hide-ov 'before-string
(propertize "..." 'face 'shadow))))))))
(defsubst gptel--annotate-link (ov link-status)
"Annotate link overlay OV according to LINK-STATUS.
LINK-STATUS is a list of link properties relevant to gptel queries, of
the form (valid . REST). See `gptel-markdown--validate-link' for
details. Indicate the (in)validity of the link for inclusion with gptel
queries via OV."
(cl-destructuring-bind
(valid _ path resource-type user-check readablep mime-valid _mime)
link-status
(if valid
(progn
(overlay-put
ov 'before-string
(concat (propertize "SEND" 'face '(:inherit success :height 0.8))
(if (display-graphic-p)
(propertize " " 'display '(space :width 0.5)) " ")))
(overlay-put ov 'help-echo
(format "Sending %s %s with gptel requests" resource-type path)))
(overlay-put ov 'before-string
(concat (propertize "!" 'face '(:inherit error))
(propertize " " 'display '(space :width 0.3))))
(overlay-put
ov 'help-echo
(concat
"Sending only link text with gptel requests, "
"this link will not be followed to its source.\n\nReason: "
(cond
((not resource-type) "Not a supported link type\
(Only \"file\" and \"attachment\" are supported)")
((not user-check)
(concat
"\nNot a standalone link -- separate link from text around it. \n (OR)
Link failed to validate, see `gptel-markdown-validate-link' or `gptel-org-validate-link'."))
((not readablep) (format "File %s is not readable" path))
((not mime-valid)
(pcase resource-type
('file (format "%s does not support binary file %s" gptel-model path))
('url (format "%s does not support fetching non-image URLs" gptel-model))))))))))
(defun gptel--annotate-link-clear (&optional beg end)
"Delete all gptel org link annotations between BEG and END."
(mapc #'delete-overlay
(cl-delete-if-not
(lambda (o) (overlay-get o 'gptel-track-media))
(overlays-in (or beg (point-min)) (or end (point-max))))))
;;;; Response text recognition
(defun gptel--get-buffer-bounds ()
"Return the gptel response boundaries in the buffer as an alist."
(save-excursion
(save-restriction
(widen)
(goto-char (point-max))
(let ((bounds) (prev-pt (point)))
(while (and (/= prev-pt (point-min))
(goto-char (previous-single-property-change
(point) 'gptel nil (point-min))))
(when-let* ((prop (get-char-property (point) 'gptel)))
(let* ((prop-name (if (symbolp prop) prop (car prop)))
(val (when (consp prop) (cdr prop)))
(bound (if val
(list (point) prev-pt val)
(list (point) prev-pt))))
(push bound (alist-get prop-name bounds))))
(setq prev-pt (point)))
bounds))))
(define-obsolete-function-alias
'gptel--get-bounds 'gptel--get-response-bounds "0.9.8")
(defun gptel--get-response-bounds ()
"Return the gptel response boundaries around point."
(let (prop)
(save-excursion
(when (text-property-search-forward
'gptel 'response t)
(when (setq prop (text-property-search-backward
'gptel 'response t))
(cons (prop-match-beginning prop)
(prop-match-end prop)))))))
(defun gptel--in-response-p (&optional pt)
"Check if position PT is inside a gptel response."
(eq (get-char-property (or pt (point)) 'gptel) 'response))
(defun gptel--at-response-history-p (&optional pt)
"Check if gptel response at position PT has variants."
(get-char-property (or pt (point)) 'gptel-history))
;;; Saving and restoring state
(defun gptel--restore-props (bounds-alist)
"Restore text properties from BOUNDS-ALIST.
BOUNDS-ALIST is (PROP . BOUNDS). BOUNDS is a list of BOUND. Each BOUND
is either (BEG END VAL) or (BEG END).
For (BEG END VAL) forms, even if VAL is nil, the gptel property will be
set to (PROP . VAL). For (BEG END) forms, except when PROP is response,
the gptel property is set to just PROP.
The legacy structure, a list of (BEG . END) is also supported and will be
applied before being re-persisted in the new structure."
;; Run silently to avoid `gptel--inherit-stickiness' and other hooks that
;; might modify the gptel text property.
(with-silent-modifications
(if (symbolp (caar bounds-alist))
(mapc
(lambda (bounds)
(let* ((prop (pop bounds)))
(mapc
(lambda (bound)
(let ((prop-has-val (> (length bound) 2)))
(add-text-properties
(pop bound) (pop bound)
(if (eq prop 'response)
'(gptel response front-sticky (gptel))
(list 'gptel
(if prop-has-val
(cons prop (pop bound))
prop))))))
bounds)))
bounds-alist)
(mapc (lambda (bound)
(add-text-properties
(car bound) (cdr bound) '(gptel response front-sticky (gptel))))
bounds-alist))))
(defun gptel--restore-state ()
"Restore gptel state when turning on `gptel-mode'."
(when (buffer-file-name)
(if (derived-mode-p 'org-mode)
(progn
(require 'gptel-org)
(gptel-org--restore-state))
(when gptel--bounds
(gptel--restore-props gptel--bounds)
(message "gptel chat restored."))
(when gptel--preset
(if (gptel-get-preset gptel--preset)
(gptel--apply-preset
gptel--preset (lambda (sym val) (set (make-local-variable sym) val)))
(display-warning
'(gptel presets)
(format "Could not activate gptel preset `%s' in buffer \"%s\""
gptel--preset (buffer-name)))))
(when gptel--backend-name
(if-let* ((backend (alist-get
gptel--backend-name gptel--known-backends
nil nil #'equal)))
(setq-local gptel-backend backend)
(message
(substitute-command-keys
(concat
"Could not activate gptel backend \"%s\"! "
"Switch backends with \\[universal-argument] \\[gptel-send]"
" before using gptel."))
gptel--backend-name)))
(when gptel--tool-names
(if-let* ((tools (cl-loop
for tname in gptel--tool-names
for tool = (with-demoted-errors "gptel: %S"
(gptel-get-tool tname))
if tool collect tool else do
(display-warning
'(gptel org tools)
(format "Tool %s not found, ignoring" tname)))))
(setq-local gptel-tools tools))))))
(defun gptel--save-state ()
"Write the gptel state to the buffer.
This saves chat metadata when writing the buffer to disk. To
restore a chat session, turn on `gptel-mode' after opening the
file.
If a gptel preset has been applied in this buffer, a reference to it is
saved.
Additional metadata is stored only if no preset was applied or if it
differs from the preset specification. This is limited to the active
gptel model and backend names, the system message, active tools, the
response temperature, max tokens and number of conversation turns to
send in queries. (See `gptel--num-messages-to-send' for the last one.)"
(run-hooks 'gptel-save-state-hook)
(if (derived-mode-p 'org-mode)
(progn
(require 'gptel-org)
(gptel-org--save-state))
(let ((print-escape-newlines t)
(preset-spec (and gptel--preset
(gptel-get-preset gptel--preset))))
(save-excursion
(save-restriction
(if preset-spec
(add-file-local-variable 'gptel--preset gptel--preset)
(delete-file-local-variable 'gptel--preset))
;; Model and backend
(if (gptel--preset-mismatch-value preset-spec :model gptel-model)
(add-file-local-variable 'gptel-model gptel-model))
(if (gptel--preset-mismatch-value preset-spec :backend gptel-backend)
(add-file-local-variable 'gptel--backend-name
(gptel-backend-name gptel-backend)))
;; System message
(let ((parsed (car-safe (gptel--parse-directive gptel--system-message))))
(if (gptel--preset-mismatch-value preset-spec :system parsed)
(add-file-local-variable 'gptel--system-message parsed)
(delete-file-local-variable 'gptel--system-message)))
;; Tools
(let ((tool-names (mapcar #'gptel-tool-name gptel-tools)))
(if (gptel--preset-mismatch-value preset-spec :tools tool-names)
(add-file-local-variable 'gptel--tool-names tool-names)
(delete-file-local-variable 'gptel--tool-names)))
;; Temperature, max tokens and cutoff
(if (and (gptel--preset-mismatch-value preset-spec :temperature gptel-temperature)
(not (equal (default-value 'gptel-temperature) gptel-temperature)))
(add-file-local-variable 'gptel-temperature gptel-temperature)
(delete-file-local-variable 'gptel-temperature))
(if (and (gptel--preset-mismatch-value preset-spec :max-tokens gptel-max-tokens)
gptel-max-tokens)
(add-file-local-variable 'gptel-max-tokens gptel-max-tokens)
(delete-file-local-variable 'gptel-max-tokens))
(if (and (gptel--preset-mismatch-value
preset-spec :num-messages-to-send gptel--num-messages-to-send)
(natnump gptel--num-messages-to-send))
(add-file-local-variable 'gptel--num-messages-to-send
gptel--num-messages-to-send)
(delete-file-local-variable 'gptel--num-messages-to-send))
(add-file-local-variable 'gptel--bounds (gptel--get-buffer-bounds)))))))
;;; Minor modes and UI
;; NOTE: It's not clear that this is the best strategy:
(cl-pushnew '(gptel . t) (default-value 'text-property-default-nonsticky)
:test #'equal)
(defun gptel--inherit-stickiness (beg end _pre)
"Mark any change to an LLM response region as a response.
Intended to be added to `after-change-functions' in gptel chat buffers,
which see for BEG, END and PRE."
(and (/= beg end) (< end (point-max))
(and-let* ((val (get-text-property end 'gptel)))
(add-text-properties
beg end `(gptel ,val front-sticky (gptel))))))
(defun gptel-markdown--annotate-links (beg end)
"Annotate Markdown links whose sources will be sent with `gptel-send'.
Search between BEG and END."
(when gptel-track-media
(save-excursion
(goto-char beg) (forward-line -1)
(let ((link-ovs (cl-loop for o in (overlays-in (point) end)
if (overlay-get o 'gptel-track-media)
collect o into os finally return os)))
(while (re-search-forward gptel-markdown--link-regex end t)
(unless (gptel--in-response-p (1- (point)))
(let* ((link (markdown-link-at-pos (point)))
(from (car link)) (to (cadr link))
(link-status (gptel-markdown--validate-link link))
(ov (cl-loop for o in (overlays-in from to)
if (overlay-get o 'gptel-track-media)
return o)))
(if ov ; Ensure overlay over each link
(progn (move-overlay ov from to)
(setq link-ovs (delq ov link-ovs)))
(setq ov (make-overlay from to nil t))
(overlay-put ov 'gptel-track-media t)
(overlay-put ov 'evaporate t)
(overlay-put ov 'priority -80))
;; Check if link will be sent, and annotate accordingly
(gptel--annotate-link ov link-status))))
(and link-ovs (mapc #'delete-overlay link-ovs))))
`(jit-lock-bounds ,beg . ,end)))
(defvar gptel--header-line-info
'(:eval
(let* ((model (gptel--model-name gptel-model))
(system
(propertize
(buttonize
(format "[Prompt: %s]"
(or (car-safe (rassoc gptel--system-message gptel-directives))
(gptel--describe-directive gptel--system-message 15)))
(lambda (&rest _) (gptel-system-prompt)))
'mouse-face 'highlight
'help-echo "System message for session"))
(context
(and gptel-context
(cl-loop
for entry in gptel-context
if (bufferp (or (car-safe entry) entry)) count it into bufs
else count (stringp (or (car-safe entry) entry)) into files
finally return
(propertize
(buttonize
(concat "[Context: "
(and (> bufs 0) (format "%d buf" bufs))
(and (> bufs 1) "s")
(and (> bufs 0) (> files 0) ", ")
(and (> files 0) (format "%d file" files))
(and (> files 1) "s")
"]")
(lambda (&rest _)
(require 'gptel-context)
(gptel-context--buffer-setup)))
'mouse-face 'highlight
'help-echo "Active gptel context"))))
(toggle-track-media
(lambda (&rest _)
(setq-local gptel-track-media (not gptel-track-media))
(if gptel-track-media
(progn
(run-hooks 'gptel-refresh-buffer-hook)
(message "Sending media from included links."))
(without-restriction (gptel--annotate-link-clear))
(message "Ignoring links. Only link text will be sent."))
(run-at-time 0 nil #'force-mode-line-update)))
(track-media
(and (gptel--model-capable-p 'media)
(if gptel-track-media
(propertize
(buttonize "[Sending media]" toggle-track-media)
'mouse-face 'highlight
'help-echo
"Sending media from links/urls when supported.\nClick to toggle")
(propertize
(buttonize "[Ignoring media]" toggle-track-media)
'mouse-face 'highlight
'help-echo
"Ignoring media from links/urls.\nClick to toggle"))))
(toggle-tools (lambda (&rest _) (interactive)
(run-at-time 0 nil
(lambda () (call-interactively #'gptel-tools)))))
(tools (when (and gptel-use-tools gptel-tools)
(propertize
(buttonize (pcase (length gptel-tools)
(0 "[No tools]") (1 "[1 tool]")
(len (format "[%d tools]" len)))
toggle-tools)
'mouse-face 'highlight
'help-echo "Select tools"))))
(let ((rhs (concat
tools (and track-media " ") track-media
(and context " ") context " " system " "
(propertize
(buttonize (concat "[" model "]")
(lambda (&rest _) (gptel-menu)))
'mouse-face 'highlight
'help-echo "Model in use"))))
(concat
(propertize
" " 'display
(if (fboundp 'string-pixel-width)
`(space :align-to (- right (,(string-pixel-width rhs))))
`(space :align-to (- right ,(+ 5 (string-width rhs))))))
rhs))))
"Information segment for the header-line in `gptel-mode'.")
(defun gptel-use-header-line ()
"Set up the header-line for a gptel buffer.
It is composed of three segments: the backend name, the
status (Ready/Waiting etc) and the info segment, showing the current
context, tools, system prompt, model and more."
(setq
header-line-format
(list '(:eval (concat (propertize " " 'display '(space :align-to 0))
(format "%s" (gptel-backend-name gptel-backend))))
(propertize " Ready" 'face 'success)
gptel--header-line-info)))
;;;###autoload
(define-minor-mode gptel-mode
"Minor mode for interacting with LLMs."
:lighter " GPT"
:keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c RET") #'gptel-send)
map)
(if gptel-mode
(progn
(unless (derived-mode-p 'org-mode 'markdown-mode 'text-mode)
(gptel-mode -1)
(user-error (format "`gptel-mode' is not supported in `%s'." major-mode)))
(add-hook 'before-save-hook #'gptel--save-state nil t)
(add-hook 'after-change-functions 'gptel--inherit-stickiness nil t)
(gptel--prettify-preset)
(cond
((derived-mode-p 'org-mode)
(require 'gptel-org)
(jit-lock-register 'gptel-org--annotate-links)
;; Work around bug in `org-fontify-extend-region'.
(add-hook 'gptel-post-response-functions #'font-lock-flush nil t))
((derived-mode-p 'markdown-mode)
(font-lock-add-keywords ;keymap is a font-lock-managed property in markdown-mode
nil '(("^```[ \t]*\\([[:alpha:]][^\n]*\\)?$" ;match code fences
0 (list 'face nil 'keymap gptel--markdown-block-map))))
(jit-lock-register 'gptel-markdown--annotate-links)))
(gptel--restore-state)
(if gptel-use-header-line
(progn (setq gptel--old-header-line header-line-format)
(gptel-use-header-line))
(gptel--update-status " Ready" 'success)))
(remove-hook 'before-save-hook #'gptel--save-state t)
(remove-hook 'after-change-functions 'gptel--inherit-stickiness t)
(cond
((derived-mode-p 'org-mode)
(jit-lock-unregister #'gptel-org--annotate-links)
(without-restriction (gptel--annotate-link-clear)))
((derived-mode-p 'markdown-mode)
(jit-lock-unregister #'gptel-markdown--annotate-links)
(without-restriction (gptel--annotate-link-clear))))
(gptel--prettify-preset)
(if gptel-use-header-line
(setq header-line-format gptel--old-header-line
gptel--old-header-line nil)
(setq mode-line-process nil))))
;; ;TODO(request-lib): Declaration no longer needed
(defvar gptel--fsm-last) ;Defined further below
(defun gptel--update-status (msg &optional face)
"Update status MSG with FACE."
(when gptel-mode
(let* ((inspect (lambda (&rest _) (gptel--inspect-fsm)))
(button (propertize (buttonize msg inspect)
'mouse-face 'highlight)))
(when face (setq button (propertize button 'face face)))
(if gptel-use-header-line
(and (consp header-line-format) (setf (nth 1 header-line-format) button))
(if (equal msg " Ready")
(setq mode-line-process