Anyone knows if there's a ready made package that lets you run Emacs commands using natural language through AI model? I made a little command like this:
(defun llm-execute-command-with-q-cli ()
"Use Q CLI LLM to convert natural language to Emacs command."
(interactive)
(let* ((user-request (read-string "What do you want to do? "))
(full-prompt (format "You are an Emacs expert. Convert this request into a single Emacs command. Reply ONLY with the elisp function call, nothing else. Examples: (save-buffers-kill-terminal) for 'quit emacs', (save-buffer) for 'save file'.\n\nUser request: %s\n\nEmacs command:" user-request))
(raw-response (shell-command-to-string
(format "echo %s | q chat --no-interactive --trust-tools= 2>/dev/null"
(shell-quote-argument full-prompt))))
(clean-response (replace-regexp-in-string "\033\\[[0-9;]*m" "" raw-response))
(lines (split-string clean-response "\n" t))
(response (string-trim
(replace-regexp-in-string "^.*> " "" (car (last lines))))))
(when (y-or-n-p (format "Execute: %s? " response))
(let ((result (eval (read response))))
(message "Result: %s" result)
result))))
And it works, but it's pretty slow as it delegates to a full agent cli (Q in this case) and also doesn't run async.I believe the same could be achieve with ECA after https://github.com/editor-code-assistant/eca-emacs/issues/62, and it's not hard to support it, it would be async and have all ECA features in the background (model choose, behaviors, MCP etc)
Right I could then use that under the hood. Though it be nice to not even have to wrap this in the command haha. Maybe another way would be to have an Emacs eval tool ? So you could just ask the same chat to run things in your emacs?
not sure I got because you would have access to a elisp function which would return a string, then you would be able to do whatever you want with that
Motivation is, I often forget the non intuitive names of emacs commands. Like what's the command to reload file from disk. I was thinking, M-x is a dumb fuzzy search. What if I had a M-x-like where I type natural language and it shows me the command that does it and then I don't need to remember the names.
ah got it, yeah, ECA support custom tools, so you could create a tool that do elisp things I guess
But ya, if there was just a way to send prompt and get response back I could update my above function to use that instead of using a shell command.
I would say for my use case, I wouldn't want any tools use, and I wouldn't want any "turn", it should be single question/answer.
But also, it shouldn't go into the same context, each invocation should be a clean context.
makes sense
Probably being able to choose the model.would be nice, haiku is probably enough. And maybe a separate system prompt input from user prompt could be convenient, but I can also do string concat beforehand.