Function: gptel--save-state
gptel--save-state is a natively compiled function defined in gptel.el.
Signature
(gptel--save-state)
Documentation
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(var)/gptel-mode(fun) 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.)
Source Code
;; Defined in /nix/store/q3r7g81pvghw7a523kjk2k2zih3pfk8z-emacs-packages-deps/share/emacs/site-lisp/elpa/gptel-20261002.545/gptel.el
(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 compat
;; TODO(v1.0): Remove this fix for duplicate system prompts
(delete-file-local-variable 'gptel--system-message)
;; System message
(let ((parsed (car-safe (gptel--parse-directive gptel-system-prompt))))
(if (gptel--preset-mismatch-value preset-spec :system parsed)
(add-file-local-variable 'gptel-system-prompt parsed)
(delete-file-local-variable 'gptel-system-prompt)))
;; 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)))))))