Skip to content

Instantly share code, notes, and snippets.

@g-simmons
Created September 16, 2024 00:43
Show Gist options
  • Save g-simmons/f8baacc2ef1893da304c3f8d1b80057f to your computer and use it in GitHub Desktop.
Save g-simmons/f8baacc2ef1893da304c3f8d1b80057f to your computer and use it in GitHub Desktop.
(defun gabe-gptel-rewrite ()
"Cursor.sh-like editing experience with gptel.
Construct a system message by prompting the user for a directive.
Call gptel-request with the current selection as user message.
On response from gptel, call ediff-regions-internal to compare
the original and changed versions linewise.
TODO:
- Store old directives, allow user to select from previous directives"
(interactive)
(require 'gptel)
(let* ((gptel--rewrite-message (concat "Refactor user inputs according to these instructions.\n\n Always respond with the complete refactored input. \n\n Do not add code fences. \n\n Instructions: " (read-string "Instructions: ")))
(original-buffer (current-buffer))
(region-beginning (region-beginning))
(region-end (region-end))
(prompt (buffer-substring-no-properties region-beginning region-end))
(stream gptel-stream)
(gptel--system-message gptel--rewrite-message)
(result-buffer (generate-new-buffer "*gptel-rewrite*")))
(progn
(message "%s" prompt)
(with-current-buffer result-buffer
(gptel-request
prompt
:in-place nil
:callback (lambda (response info)
(message "%s" response)
(with-current-buffer result-buffer (insert response))
(let ((result-beg (point-min))
(result-end (point-max)))
(progn
(message "%s" result-beg)
(message "%s" result-end)
(ediff-regions-internal
original-buffer region-beginning region-end
result-buffer result-beg result-end
nil 'ediff-regions-linewise nil nil)
)
)
)
:buffer result-buffer
:stream nil
:system gptel--system-message)
))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment