Files
dotemacs/config-mac.el
T
Willem van den Ende 580b213793 fix(live-dictate): talk to whisper.cpp's HTTP API instead of Lemonade realtime
Live dictation sent the OpenAI realtime protocol to ws://host/v1/realtime,
which only Lemonade-style servers implement.  Against the local
whisper-server on port 13305 every connection failed with
"websocket-received-error-http-response 404".

Retarget the module at whisper.cpp's native POST /inference endpoint:

- capture 16 kHz mono PCM16 with FFmpeg (now with binary process coding,
  which the old version lacked and which would have corrupted the audio)
- cut the stream into utterances with an energy-based VAD: speech starts
  above live-dictate-silence-threshold, flushes after a configurable
  silence window, with pre-roll and a max-utterance cap
- wrap each utterance in a WAV header and post it with curl, one request
  at a time so transcripts are inserted in spoken order
- insert text at the dictation marker with correct spacing, keep the
  mode-line indicator and follow-cursor behaviour
- warn when the microphone delivers pure digital silence (OS permission)

New defcustoms: live-dictate-language, -prompt, -silence-threshold,
-silence-duration, -min-utterance, -max-utterance, -preroll.

Comments in config-mac.el and packages.el updated; websocket is no
longer needed by dictation.
2026-11-05 12:14:30 +00:00

279 lines
13 KiB
EmacsLisp

;;; config-mac.el --- Mac-specific configuration -*- lexical-binding: t; -*-
;; Seeded from mac repo 157ed9a, then edited locally. This file is loaded
;; only on darwin (see the dispatch at the end of config.el); it is the
;; home for anything with a macOS path, macOS binary, or macOS-hosted
;; backend. It is no longer a pristine vendor copy -- treat it as ours.
;; Enable tool use in gptel
(setq gptel-use-tools t)
;; Always redirect reasoning tokens to a separate buffer
(setq gptel-include-reasoning "*llm-thinking*")
;;; gptel backend configuration
;; Local llama.cpp server
(setq gptel-backend (gptel-make-openai "llama.cpp"
:host "localhost:8000"
:protocol "http"
:stream t
:models '(Qwen3.6-27B-Q4_K_M.gguf
Qwen3.6-35B-A3B-MXFP4_MOE.gguf
unsloth/Qwen3-Coder-Next-GGUF:UD-IQ3_XXS
unsloth/Qwen3.5-9B-GGUF:UD-Q4_K_XL
Qwen3-Coder-Next-UD-Q4_K_XL
;; GLM models (reasoning handled globally via gptel-include-reasoning)
(GLM-4.7-Flash-Q4_K_M
:description "GLM-4.7-Flash 30B MoE Q4 (17GB)"
:request-params (:max_tokens 8192))
(GLM-4.7-Flash-UD-Q6_K_XL
:description "GLM-4.7-Flash 30B MoE Q6 XL (26GB)"
:request-params (:max_tokens 8192))))
gptel-model 'Qwen3.6-35B-A3B-MXFP4_MOE.gguf)
;; Prevent ChatGPT from being used as default
(setq-default gptel-model 'Qwen3-Coder-30B-A3B-Instruct-Q6_K)
(setq gptel-backend (gptel-make-openai "mlx-lm"
:host "localhost:8000"
:protocol "http"
:stream t
:models '(mlx-community/Qwen3.6-27B-4bit))
gptel-model 'mlx-community/Qwen3.6-27B-4bit)
(setq gptel-backend (gptel-make-openai "mlx-studio"
:host "localhost:8080"
:protocol "http"
:stream t
:models '(inferencerlabs/Qwen3.5-35B-A3B-MLX-5.5bit))
gptel-model 'inferencerlabs/Qwen3.5-35B-A3B-MLX-5.5bit)
;; Additional backend: Ollama for smaller models
(gptel-make-ollama "Ollama"
:host "localhost:11434"
:stream t
:models '(qwen2.5-coder:7b
qwen3:0.6B
(qwen2.5-coder:32b
:description "qwen2.5 coder 32b: large coding model"
:capabilities (tools media)
:mime-types ("image/jpeg" "image/png" "image/svg"))))
;; Presets (these reference the backends defined above)
(gptel-make-preset 'explain
:system "Explain what this code does to a novice programmer."
:model 'Qwen3-Coder-30B-A3B-Instruct-Q6_K
:backend "llama.cpp")
(gptel-make-preset 'elixir
:system "You are an expert programmer, specialising in Elixir and TDD, you live in Emacs"
:model 'Qwen3-Coder-30B-A3B-Instruct-Q6_K
:backend "llama.cpp")
(gptel-make-preset 'elisp
:system "You are an expert programmer, specialising in Emacs Lisp and testing with ERT, you live in Emacs"
:model 'qwen3-coder-80b
:backend "llama.cpp")
;; GLM-4.7-Flash presets
;; GLM requires explicit stop tokens to prevent infinite generation
;; Fast mode: thinking disabled for quick responses
(gptel-make-preset 'glm-fast
:description "GLM-4.7-Flash without thinking (fast)"
:system "You are a coding agent living in Emacs. Be concise. Code only, minimal explanation. No preamble."
:model 'GLM-4.7-Flash-Q4_K_M
:backend "llama.cpp"
:request-params '(:chat_template_kwargs (:enable_thinking :json-false)
:max_tokens 1024
:stop ["<|user|>" "<|observation|>" "<|endoftext|>"]))
;; Thinking mode: reasoning streamed to separate buffer
;; Uses GLM-think backend (DeepSeek parser) for reasoning_content extraction
(gptel-make-preset 'glm-think
:description "GLM-4.7-Flash with thinking (reasoning in side buffer)"
:system "Think briefly. Give a short, direct final answer. No verbose explanations."
:model 'GLM-4.7-Flash-UD-Q6_K_XL
:backend "GLM-think"
:request-params '(:max_tokens 2048
:stop ["<|user|>" "<|observation|>" "<|endoftext|>"]))
(gptel-make-tool
:name "projectile-search"
:function (lambda (pattern)
(require 'projectile)
(projectile-find-file (projectile-guess-project-root))
(let* ((root (projectile-project-root))
(default-directory root)
(grep-command (concat "grep -r --exclude-dir={.git,node_modules,vendor} -I '"
pattern
" --include={'*.el','*.py','*.js','*.java','*.c','*.cpp','*.h','*.hs','*.rb','*.go','*.jsx','*.ts','*.tsx','*.scala','*.php','*.sh','*.yaml','*.yml','*.json'}"
))
(output (shell-command-to-string grep-command)))
(if (string-empty-p output)
(format "No results found for pattern: %s in project %s" pattern (or (projectile-project-name) "current"))
(if (> (length output) 3000)
(concat (substring output 0 3000) "\n... (truncated)")
output))))
:description "Search the current project using projectile. Sets working directory to project root and searches for a pattern in common source files."
:args (list '(:name "pattern"
:type "string"
:description "Search pattern to find in project files"))
:category "project")
;; GLM search tool - searches org-roam notes via ripgrep
(gptel-make-tool
:name "search_notes"
:function (lambda (query)
(let* ((roam-dir (expand-file-name "~/MyDocuments/qwan/essays/willem"))
(cmd (format "rg --type org -i -C 1 --max-count 2 --max-filesize 100K %s %s 2>/dev/null | head -100"
(shell-quote-argument query)
(shell-quote-argument roam-dir)))
(output (shell-command-to-string cmd)))
(if (string-empty-p output)
(format "No results found for: %s" query)
(if (> (length output) 3000)
(concat (substring output 0 3000) "\n... (truncated)")
output))))
:description "Search org-roam notes using ripgrep. Returns matching lines with context from the user's personal knowledge base."
:args (list '(:name "query"
:type "string"
:description "Search term or pattern to find in notes"))
:category "org-roam")
;; GLM search preset - uses the search_notes tool
(gptel-make-preset 'glm-search
:description "GLM-4.7-Flash with org-roam search"
:system "You are a helpful assistant with access to the user's org-roam knowledge base. Use the search_notes tool to find relevant information before answering. Be concise."
:model 'GLM-4.7-Flash-UD-Q6_K_XL
:backend "llama.cpp"
:tools '("search_notes"))
(defun gptel-glm-search (query)
"Search org-roam notes using GLM-4.7-Flash with tool calling.
Returns answer with org-roam links to sources."
(interactive "sSearch query: ")
(let* ((script (expand-file-name "~/dev/spikes/llm/llama-metal-openwebui/glm_search.py"))
(buf (get-buffer-create "*glm-search*")))
(with-current-buffer buf
(erase-buffer)
(org-mode)
(yas-minor-mode -1) ; Disable yasnippet to prevent < triggers
(insert (format "* Query: %s\n\nSearching...\n" query)))
(display-buffer buf)
(redisplay)
;; Run synchronously to ensure all output captured
(let ((output (shell-command-to-string
(format "python3 %s %s 2>/dev/null"
(shell-quote-argument script)
(shell-quote-argument query)))))
(with-current-buffer buf
(erase-buffer)
(insert (format "* Query: %s\n\n" query))
;; Remove everything before ==== line
(let ((clean-output (if (string-match "====\n" output)
(substring output (match-end 0))
output)))
(insert clean-output))
(goto-char (point-min))))))
;; @glm-search directive - searches org-roam with tool calling
(defun my/gptel-glm-search-run (query)
"Run glm-search script and return results."
(message "Searching org-roam for: %s..." query)
(let* ((script (expand-file-name "~/dev/spikes/llm/llama-metal-openwebui/glm_search.py"))
(output (shell-command-to-string
(format "python3 %s %s 2>/dev/null"
(shell-quote-argument script)
(shell-quote-argument query)))))
(if (string-match "====\n" output)
(substring output (match-end 0))
output)))
(defun gptel-glm-search-send ()
"Search org-roam using @glm-search or @gptel-search query on current line."
(interactive)
(let ((line (buffer-substring-no-properties
(line-beginning-position) (line-end-position))))
;; Accept both @glm-search and @gptel-search patterns
(if (string-match "@\\(?:glm\\|gptel\\)-search\\s-+\\(.+\\)$" line)
(let* ((query (string-trim (match-string 1 line)))
(result (my/gptel-glm-search-run query))
(yas-was-active (bound-and-true-p yas-minor-mode)))
(when yas-was-active (yas-minor-mode -1))
(end-of-line)
(insert "\n\n" result)
(when yas-was-active (yas-minor-mode 1)))
(message "No @glm-search or @gptel-search query found on current line"))))
;; Elixir language server. Moved out of the shared config.el (where a
;; hard-coded macOS path leaked to Linux); that ~/.elixir-ls checkout no
;; longer exists on this machine either. Mirrors config-linux.el: prefer
;; `expert' if it is installed, else any elixir-ls on PATH, else leave eglot
;; alone rather than configuring a server that cannot start. Covers all
;; three Elixir modes, including heex-ts-mode.
(with-eval-after-load 'eglot
(let ((server (cond ((executable-find "expert")
'("expert" "--stdio"))
((executable-find "language_server.sh")
(list (or (executable-find "bash") "sh")
(executable-find "language_server.sh"))))))
(when server
(setf (alist-get '(elixir-mode elixir-ts-mode heex-ts-mode)
eglot-server-programs
nil nil #'equal)
server))))
;; Blog export destinations. Kept in this file rather than in the shared
;; config.el because the jekyll checkout sits under ~/MyDocuments on macOS and
;; under ~/Documents on Linux. Override in config-local.el if it moves.
(setq org-roam-export-blog-firehose-root "~/dev/elixir/firehose"
org-roam-export-jekyll-root "~/MyDocuments/qwan/sites/qwaneu.github.io")
;;; Speech-to-text (whisper.el + streaming dictation)
;;
;; Ported from config-linux.el so both machines have the same capabilities.
;; `whisper' is declared in shared packages.el, and lisp/live-dictate.el
;; picks avfoundation for FFmpeg input on darwin by itself.
;;
;; Backend on this machine: homebrew's whisper.cpp server, started by hand:
;;
;; whisper-server -m ~/.cache/whisper/ggml-large-v3-turbo.bin \
;; --host 127.0.0.1 --port 13305
;;
;; Mode `remote' POSTs multipart (file, response_format=json, language) to
;; <baseurl>/inference and parses the {"text": ...} reply -- whisper.cpp's
;; native API. `live-dictate' (SPC A d) uses the same endpoint: it cuts the
;; microphone stream into utterances with a client-side VAD and posts each
;; one as a WAV, so no WebSocket or Lemonade server is needed.
(add-load-path! "lisp")
(autoload #'live-dictate "live-dictate" nil t)
(use-package! whisper
:commands (whisper-run whisper-file whisper-select-language)
:init
(map! :leader
(:prefix ("A" . "+ai")
:desc "Whisper record/transcribe" "w" #'whisper-run
:desc "Whisper transcribe file" "W" #'whisper-file
:desc "Whisper select language" "l" #'whisper-select-language
:desc "Live dictate (streaming)" "d" #'live-dictate))
:config
(setopt whisper-install-whispercpp nil ; use homebrew's whisper.cpp, not a private copy
whisper-server-mode 'remote ; talk to a running whisper.cpp server
whisper-server-baseurl "http://localhost:13305"
whisper-language "en"
whisper-translate nil ; whisper.cpp translates only via server --translate flag
whisper-return-cursor 'end))
;; live-dictate's avfoundation input needs an explicit `-i' of the form
;; "<video-index>:<audio-index>"; ":0" means no video, default microphone.
;; First use will trigger the macOS microphone permission prompt.
(with-eval-after-load 'live-dictate
(setopt live-dictate-ffmpeg-input-device ":0"))
;;; config-mac.el ends here