Skip to content

Instantly share code, notes, and snippets.

@jwiegley
Last active May 25, 2025 06:31
Show Gist options
  • Save jwiegley/09c4e2379caf5dd601f8fb5ee1fd0ba6 to your computer and use it in GitHub Desktop.
Save jwiegley/09c4e2379caf5dd601f8fb5ee1fd0ba6 to your computer and use it in GitHub Desktop.
(defun eshell/gptel (&rest args)
(eshell-eval-using-options
"gptel" args
'((?h "help" nil nil "show this usage screen")
(?n "dry-run" nil dry-run "Only show what would happen")
(?m "model" nil model "LLM model to use")
(?s "system" nil system "System prompt to use (optional, can be a symbol)")
(?S "stream" nil stream "Whether to stream the output")
:usage "[OPTION...] [PROMPT]
Send prompt (or context, or both) to an LLM.")
(eshell-with-copied-handles
(let ((buf (generate-new-buffer " *eshell/gptel*"))
(eshell-buf (current-buffer)))
(with-current-buffer buf
(org-mode)
(gptel-mode)
(if model (setq-local gptel-model model))
(if stream (setq-local gptel-stream stream)))
(eshell-function-target-create
`(lambda (input)
(with-current-buffer ,buf
(insert input)))
`(lambda (success)
(when success
(cl-flet
((callback
(lambda (response info)
(cond
((stringp response)
(with-current-buffer ,eshell-buf
(let ((eshell-current-handles
,eshell-current-handles))
(eshell-print response)
(unless gptel-stream
(eshell-print "\n")
(eshell-reset))))
(unless gptel-stream
(kill-buffer ,buf)))
((consp response)
;; Need Karthik here for tools and reasoning and such
)
((null response)
(with-current-buffer ,eshell-buf
(let ((eshell-current-handles
,eshell-current-handles))
(eshell-error "LLM failed; call Karthik\n")))
(kill-buffer ,buf))
(t
(with-current-buffer ,eshell-buf
(eshell-print "\n")
(eshell-reset))
(kill-buffer ,buf))))))
(with-current-buffer ,buf
(gptel-request (car args)
:dry-run ,dry-run
:system (or (alist-get (intern-soft ,system)
gptel-directives)
,system
(alist-get 'default gptel-directives))
:stream gptel-stream
:context (buffer-string)
:callback #'callback)
(throw 'eshell-defer t))))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment