;;; minuet.el --- Code completion using LLM -*- lexical-binding: t; -*-

;; Author: Milan Glacier <dev@milanglacier.com>
;; Maintainer: Milan Glacier <dev@milanglacier.com>
;; Version: 0.4.4
;; URL: https://github.com/milanglacier/minuet-ai.el
;; Package-Requires: ((emacs "29") (plz "0.9") (dash "2.19.1"))

;;; This file is NOT part of GNU Emacs

;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.

;;; Commentary:
;; AI-powered code completion with dual modes:
;;
;; - Specialized prompts and various enhancements for chat-based LLMs
;;   on code completion tasks.
;; - Fill-in-the-middle (FIM) completion for compatible models
;;   (DeepSeek, Codestral, and some Ollama models).
;;
;; Minuet supports multiple AI providers (OpenAI, Claude, Gemini,
;; Codestral, Ollama, Llama.cpp, and OpenAI-compatible providers)
;;
;; You can use it with overlay-based ghost text via
;; `minuet-show-suggestion' or selecting the candidates via
;; `minuet-complete-with-minibuffer'.  You can toggle automatic
;; suggestion popup with `minuet-auto-suggestion-mode'.

;;; Code:

(require 'plz)
(require 'dash)
(require 'cl-lib)

(defgroup minuet nil
  "Minuet group."
  :group 'applications)

(declare-function evil-emacs-state-p "evil-states")
(declare-function evil-insert-state-p "evil-states")
(declare-function consult--read "consult")
(declare-function consult--insertion-preview "consult")

(defcustom minuet-auto-suggestion-debounce-delay 0.4
  "Debounce delay in seconds for auto-suggestions."
  :type 'number)

(defcustom minuet-auto-suggestion-block-functions '(minuet-evil-not-insert-state-p)
  "List of functions to determine whether auto-suggestions should be blocked.

Each function should return non-nil if auto-suggestions should be
blocked.  If any function in this list returns non-nil,
auto-suggestions will not be shown."
  :type '(repeat function))

(defcustom minuet-auto-suggestion-throttle-delay 1.0
  "Minimum time in seconds between auto-suggestions."
  :type 'number)

(defface minuet-suggestion-face
  '((t :inherit shadow))
  "Face used for displaying inline suggestions.")


(defvar-local minuet--current-overlay nil
  "Overlay used for displaying the current suggestion.")


(defvar-local minuet--last-point nil
  "Last known cursor position for suggestion overlay.")

(defvar-local minuet--auto-last-point nil
  "Last known cursor position for auto-suggestion.")


(defvar-local minuet--current-suggestions nil
  "List of current completion suggestions.")

(defvar-local minuet--current-suggestion-index 0
  "Index of currently displayed suggestion.")

(defvar-local minuet--current-requests nil
  "List of current active request processes for this buffer.")


(defvar-local minuet--last-auto-suggestion-time nil
  "Timestamp of last auto-suggestion.")

(defvar-local minuet--debounce-timer nil
  "Timer for debouncing auto-suggestions.")

(defvar minuet-buffer-name "*minuet*" "The basename for minuet buffers.")

(defcustom minuet-provider 'openai-fim-compatible
  "The provider to use for code completion.
Must be one of the supported providers: codestral, openai, claude, etc."
  :type '(choice (const :tag "Codestral" codestral)
                 (const :tag "OpenAI" openai)
                 (const :tag "Claude" claude)
                 (const :tag "OpenAI Compatible" openai-compatible)
                 (const :tag "OpenAI FIM Compatible" openai-fim-compatible)
                 (const :tag "Gemini" gemini)))

(defcustom minuet-context-window 16000
  "The maximum total characters of the context before and after cursor.
This limits how much surrounding code is sent to the LLM for context.
The default is 16000 characters which would roughly equate 4000
tokens."
  :type 'integer)

(defcustom minuet-context-ratio 0.75
  "Ratio of context before cursor vs after cursor.
When the total characters exceed the context window, this ratio
determines how much context to keep before vs after the cursor.  A
larger ratio means more context before the cursor will be used."
  :type 'float)

(defcustom minuet-request-timeout 3
  "Maximum timeout in seconds for sending completion requests."
  :type 'integer)

(defcustom minuet-add-single-line-entry t
  "Whether to create additional single-line completion items.
When non-nil and a completion item has multiple lines, create another
completion item containing only its first line."
  :type 'boolean)

(defcustom minuet-after-cursor-filter-length 15
  "Length of context after cursor used to filter completion text.

Defines the length of non-whitespace context after the cursor used to
filter completion text.  Set to 0 to disable filtering.

Example: With after_cursor_filter_length = 3 and context: \"def
fib(n):\\n|\\n\\nfib(5)\" (where | represents cursor position), if the
completion text contains \"fib\", then \"fib\" and subsequent text
will be removed.  This setting filters repeated text generated by the
LLM.  A large value (e.g., 15) is recommended to avoid false
positives."
  :type 'integer)

(defcustom minuet-n-completions 3
  "Number of completion items.
For FIM model, this is the number of requests to send.  For chat LLM ,
this is the number of completions encoded as part of the prompt.  Note
that when `minuet-add-single-line-entry` is true, the actual number of
returned items may exceed this value.  Additionally, the LLM cannot
guarantee the exact number of completion items specified, as this
parameter serves only as a prompt guideline.  The default is `3`."
  :type 'integer)

(defvar minuet-default-prompt
  "You are the backend of an AI-powered code completion engine. Your task is to
provide code suggestions based on the user's input. The user's code will be
enclosed in markers:

- `<contextAfterCursor>`: Code context after the cursor
- `<cursorPosition>`: Current cursor location
- `<contextBeforeCursor>`: Code context before the cursor

Note that the user's code will be prompted in reverse order: first the code
after the cursor, then the code before the cursor.
"
  "The default prompt for minuet completion.")

(defvar minuet-default-guidelines
  "Guidelines:
1. Offer completions after the `<cursorPosition>` marker.
2. Make sure you have maintained the user's existing whitespace and indentation.
   This is REALLY IMPORTANT!
3. Provide multiple completion options when possible.
4. Return completions separated by the marker <endCompletion>.
5. The returned message will be further parsed and processed. DO NOT include
   additional comments or markdown code block fences. Return the result directly.
6. Keep each completion option concise, limiting it to a single line or a few lines.
7. Create entirely new code completion that DO NOT REPEAT OR COPY any user's existing code around <cursorPosition>."
  "The default guidelines for minuet completion.")

(defvar minuet-default-n-completion-template
  "8. Provide at most %d completion items."
  "The default prompt for minuet for number of completions request.")

(defvar minuet-default-system-template
  "{{{:prompt}}}\n{{{:guidelines}}}\n{{{:n-completions-template}}}"
  "The default template for minuet system template.")

(defvar minuet-default-chat-input-template
  "{{{:language-and-tab}}}
<contextAfterCursor>
{{{:context-after-cursor}}}
<contextBeforeCursor>
{{{:context-before-cursor}}}<cursorPosition>"
  "The default template for minuet chat input.")

(defvar minuet-default-fewshots
  `((:role "user"
     :content "# language: python
<contextAfterCursor>

fib(5)
<contextBeforeCursor>
def fibonacci(n):
    <cursorPosition>")
    (:role "assistant"
     :content "    '''
    Recursive Fibonacci implementation
    '''
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)
<endCompletion>
    '''
    Iterative Fibonacci implementation
    '''
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a
<endCompletion>
")))

(defvar minuet-claude-options
  `(:model "claude-3-5-haiku-20241022"
    :max_tokens 512
    :api-key "ANTHROPIC_API_KEY"
    :system
    (:template minuet-default-system-template
     :prompt minuet-default-prompt
     :guidelines minuet-default-guidelines
     :n-completions-template minuet-default-n-completion-template)
    :fewshots minuet-default-fewshots
    :chat-input
    (:template minuet-default-chat-input-template
     :language-and-tab minuet--default-chat-input-language-and-tab-function
     :context-before-cursor minuet--default-chat-input-before-cursor-function
     :context-after-cursor minuet--default-chat-input-after-cursor-function)
    :optional nil)
  "Config options for Minuet Claude provider.")

(defvar minuet-openai-options
  `(:model "gpt-4o-mini"
    :api-key "OPENAI_API_KEY"
    :system
    (:template minuet-default-system-template
     :prompt minuet-default-prompt
     :guidelines minuet-default-guidelines
     :n-completions-template minuet-default-n-completion-template)
    :fewshots minuet-default-fewshots
    :chat-input
    (:template minuet-default-chat-input-template
     :language-and-tab minuet--default-chat-input-language-and-tab-function
     :context-before-cursor minuet--default-chat-input-before-cursor-function
     :context-after-cursor minuet--default-chat-input-after-cursor-function)
    :optional nil)
  "Config options for Minuet OpenAI provider.")

(defvar minuet-codestral-options
  '(:model "codestral-latest"
    :end-point "https://codestral.mistral.ai/v1/fim/completions"
    :api-key "CODESTRAL_API_KEY"
    :template (:prompt minuet--default-fim-prompt-function
               :suffix minuet--default-fim-suffix-function)
    :optional nil)
  "Config options for Minuet Codestral provider.")

(defvar minuet-openai-compatible-options
  `(:end-point "https://api.groq.com/openai/v1/chat/completions"
    :api-key "GROQ_API_KEY"
    :model "llama-3.3-70b-versatile"
    :system
    (:template minuet-default-system-template
     :prompt minuet-default-prompt
     :guidelines minuet-default-guidelines
     :n-completions-template minuet-default-n-completion-template)
    :fewshots minuet-default-fewshots
    :chat-input
    (:template minuet-default-chat-input-template
     :language-and-tab minuet--default-chat-input-language-and-tab-function
     :context-before-cursor minuet--default-chat-input-before-cursor-function
     :context-after-cursor minuet--default-chat-input-after-cursor-function)
    :optional nil)
  "Config options for Minuet OpenAI compatible provider.")

(defvar minuet-openai-fim-compatible-options
  '(:model "deepseek-chat"
    :end-point "https://api.deepseek.com/beta/completions"
    :api-key "DEEPSEEK_API_KEY"
    :name "Deepseek"
    :template (:prompt minuet--default-fim-prompt-function
               :suffix minuet--default-fim-suffix-function)
    :optional nil)
  "Config options for Minuet OpenAI FIM compatible provider.")

(defvar minuet-gemini-options
  `(:model "gemini-2.0-flash"
    :api-key "GEMINI_API_KEY"
    :system
    (:template minuet-default-system-template
     :prompt minuet-default-prompt
     :guidelines minuet-default-guidelines
     :n-completions-template minuet-default-n-completion-template)
    :fewshots minuet-default-fewshots
    :chat-input
    (:template minuet-default-chat-input-template
     :language-and-tab minuet--default-chat-input-language-and-tab-function
     :context-before-cursor minuet--default-chat-input-before-cursor-function
     :context-after-cursor minuet--default-chat-input-after-cursor-function)
    :optional nil)
  "Config options for Minuet Gemini provider.")


(defun minuet-evil-not-insert-state-p ()
  "Return non-nil if evil is loaded and not in insert or Emacs state."
  (and (bound-and-true-p evil-local-mode)
       (not (or (evil-insert-state-p)
                (evil-emacs-state-p)))))

(defun minuet-set-optional-options (options key val &optional field)
  "Set the value of KEY in the FIELD of OPTIONS to VAL.
If FIELD is not provided, it defaults to :optional.  If VAL is nil,
then remove KEY from OPTIONS.  This helper function simplifies setting
values in a two-level nested plist structure."
  (let ((field (or field :optional)))
    (if val
        (setf (plist-get options field)
              (plist-put (plist-get options field) key val))
      (setf (plist-get options field)
            (map-delete (plist-get options field) key)))))

(defun minuet--eval-value (value)
  "Eval a VALUE for minuet.
If value is a function (either lambda or a callable symbol), eval the
function (with no argument) and return the result.  Else if value is a
symbol, return its value.  Else return itself."
  (cond ((functionp value) (funcall value))
        ((and (symbolp value) (boundp value)) (symbol-value value))
        (t value)))

(defun minuet--cancel-requests ()
  "Cancel all current minuet requests for this buffer."
  (when minuet--current-requests
    (dolist (proc minuet--current-requests)
      (when (process-live-p proc)
        (minuet--log (format "%s process killed" (prin1-to-string proc)))
        (delete-process proc)))
    (setq minuet--current-requests nil)))

(defun minuet--cleanup-suggestion (&optional no-cancel)
  "Remove the current suggestion overlay.
Also cancel any pending requests unless NO-CANCEL is t."
  (unless no-cancel
    (minuet--cancel-requests))
  (when minuet--current-overlay
    (delete-overlay minuet--current-overlay)
    (setq minuet--current-overlay nil)
    (minuet-active-mode -1))
  (remove-hook 'post-command-hook #'minuet--on-cursor-moved t)
  (setq minuet--last-point nil))

(defun minuet--cursor-moved-p ()
  "Check if cursor moved from last suggestion position."
  (and minuet--last-point
       (not (eq minuet--last-point (point)))))

(defun minuet--on-cursor-moved ()
  "Minuet event on cursor moved."
  (when (minuet--cursor-moved-p)
    (minuet--cleanup-suggestion)))

(defun minuet--display-suggestion (suggestions &optional index)
  "Display suggestion from SUGGESTIONS at INDEX using an overlay at point."
  ;; we only cancel requests when cursor is moved. Because the
  ;; completion items may be accumulated during multiple concurrent
  ;; curl requests.
  (minuet--cleanup-suggestion t)
  (add-hook 'post-command-hook #'minuet--on-cursor-moved nil t)
  (when-let* ((suggestions suggestions)
              (cursor-not-moved (not (minuet--cursor-moved-p)))
              (index (or index 0))
              (total (length suggestions))
              (suggestion (nth index suggestions))
              ;; Ensure the overlay appears after the cursor If
              ;; point is not at end-of-line, offset the overlay
              ;; position by 1
              (ov-point (if (eolp) (point) (1+ (point))))
              (ov (make-overlay ov-point ov-point)))
    (setq minuet--current-suggestions suggestions
          minuet--current-suggestion-index index
          minuet--last-point (point))
    ;; HACK: Adapted from copilot.el We add a 'cursor text property to the
    ;; first character of the suggestion to simulate the visual effect of
    ;; placing the overlay after the cursor
    (put-text-property 0 1 'cursor t suggestion)
    (overlay-put ov 'after-string
                 (propertize
                  (format "%s%s"
                          suggestion
                          (if (= total minuet-n-completions 1) ""
                            (format " (%d/%d)" (1+ index) total)))
                  'face 'minuet-suggestion-face))
    (overlay-put ov 'minuet t)
    (setq minuet--current-overlay ov)
    (minuet-active-mode 1)))

;;;###autoload
(defun minuet-next-suggestion ()
  "Cycle to next suggestion."
  (interactive)
  (if (and minuet--current-suggestions
           minuet--current-overlay)
      (let ((next-index (mod (1+ minuet--current-suggestion-index)
                             (length minuet--current-suggestions))))
        (minuet--display-suggestion minuet--current-suggestions next-index))
    (minuet-show-suggestion)))

;;;###autoload
(defun minuet-previous-suggestion ()
  "Cycle to previous suggestion."
  (interactive)
  (if (and minuet--current-suggestions
           minuet--current-overlay)
      (let ((prev-index (mod (1- minuet--current-suggestion-index)
                             (length minuet--current-suggestions))))
        (minuet--display-suggestion minuet--current-suggestions prev-index))
    (minuet-show-suggestion)))

;;;###autoload
(defun minuet-show-suggestion ()
  "Show code suggestion using overlay at point."
  (interactive)
  (minuet--cleanup-suggestion)
  (setq minuet--last-point (point))
  (let ((current-buffer (current-buffer))
        (available-p-fn (intern (format "minuet--%s-available-p" minuet-provider)))
        (complete-fn (intern (format "minuet--%s-complete" minuet-provider)))
        (context (minuet--get-context)))
    (unless (funcall available-p-fn)
      (minuet--log (format "Minuet provider %s is not available" minuet-provider))
      (error "Minuet provider %s is not available" minuet-provider))
    (funcall complete-fn
             context
             (lambda (items)
               (setq items (-distinct items))
               (with-current-buffer current-buffer
                 (when (and items (not (minuet--cursor-moved-p)))
                   (minuet--display-suggestion items 0)))))))

(defun minuet--log (message &optional message-p)
  "Log minuet messages into `minuet-buffer-name'.
Also print the MESSAGE when MESSAGE-P is t."
  (with-current-buffer (get-buffer-create minuet-buffer-name)
    (goto-char (point-max))
    (insert (format "%s %s\n" message (format-time-string "%Y-%02m-%02d %02H:%02M:%02S")))
    (when message-p
      (message "%s" message))))

(defun minuet--add-tab-comment ()
  "Add comment string for tab use into the prompt."
  (if-let* ((language-p (derived-mode-p 'prog-mode 'text-mode 'conf-mode))
            (commentstring (format "%s %%s%s"
                                   (or (replace-regexp-in-string "^%" "%%" comment-start) "#")
                                   (or comment-end ""))))
      (if indent-tabs-mode
          (format commentstring "indentation: use \t for a tab")
        (format commentstring (format "indentation: use %d spaces for a tab" tab-width)))
    ""))

(defun minuet--add-language-comment ()
  "Add comment string for language use into the prompt."
  (if-let* ((language-p (derived-mode-p 'prog-mode 'text-mode 'conf-mode))
            (mode (symbol-name major-mode))
            (mode (replace-regexp-in-string "-ts-mode" "" mode))
            (mode (replace-regexp-in-string "-mode" "" mode))
            (commentstring (format "%s %%s%s"
                                   (or (replace-regexp-in-string "^%" "%%" comment-start) "#")
                                   (or comment-end ""))))
      (format commentstring (concat "language: " mode))
    ""))

(defun minuet--add-single-line-entry (data)
  "Add single line entry into the DATA."
  (cl-loop
   for item in data
   when (stringp item)
   append (list (car (split-string item "\n"))
                item)))

(defun minuet--remove-spaces (items)
  "Remove trailing and leading spaces in each item in ITEMS."
  ;; Emacs use \\` and \\' to match the beginning/end of the string,
  ;; ^ and $ are used to match bol or eol
  (setq items (mapcar (lambda (x)
                        (if (or (equal x "")
                                (string-match "\\`[\s\t\n]+\\'" x))
                            nil
                          (string-trim x)))
                      items)
        items (seq-filter #'identity items)))

(defun minuet--get-context ()
  "Get the context for minuet completion."
  (let* ((point (point))
         (n-chars-before point)
         (point-max (point-max))
         (n-chars-after (- point-max point))
         (before-start (point-min))
         (after-end point-max)
         (is-incomplete-before nil)
         (is-incomplete-after nil))
    ;; Calculate context window boundaries before extracting text
    (when (>= (+ n-chars-before n-chars-after) minuet-context-window)
      (cond ((< n-chars-before (* minuet-context-ratio minuet-context-window))
             ;; If context before cursor does not exceed context-window,
             ;; only limit after-cursor content
             (setq after-end (+ point (- minuet-context-window n-chars-before))
                   is-incomplete-after t))
            ((< n-chars-after (* (- 1 minuet-context-ratio) minuet-context-window))
             ;; If context after cursor does not exceed context-window,
             ;; limit before-cursor content
             (setq before-start (- point (- minuet-context-window n-chars-after))
                   is-incomplete-before t))
            (t
             ;; At middle of file, use ratio to determine both boundaries
             (setq is-incomplete-before t
                   is-incomplete-after t
                   after-end (+ point (floor (* minuet-context-window (- 1 minuet-context-ratio))))
                   before-start (+ (point-min)
                                   (max 0 (- n-chars-before
                                             (floor (* minuet-context-window minuet-context-ratio)))))))))
    `(:before-cursor ,(buffer-substring-no-properties before-start point)
      :after-cursor ,(buffer-substring-no-properties point after-end)
      :language-and-tab ,(format "%s\n%s" (minuet--add-language-comment) (minuet--add-tab-comment))
      :is-incomplete-before ,is-incomplete-before
      :is-incomplete-after ,is-incomplete-after)))

(defun minuet--make-chat-llm-shot (context options)
  "Build the final chat input for chat llm.
CONTEXT is read from current buffer content.
OPTIONS should be the provider options plist."
  (let* ((chat-input (copy-tree (plist-get options :chat-input)))
         (template (minuet--eval-value (plist-get chat-input :template)))
         (parts nil))
    ;; Remove template from options to avoid infinite recursion
    (setq chat-input (plist-put chat-input :template nil))
    ;; Use cl-loop for better control flow
    (cl-loop with last-pos = 0
             for match = (string-match "{{{\\(.+?\\)}}}" template last-pos)
             until (not match)
             for start-pos = (match-beginning 0)
             for end-pos = (match-end 0)
             for key = (match-string 1 template)
             do
             ;; Add text before placeholder
             (when (> start-pos last-pos)
               (push (substring template last-pos start-pos) parts))
             ;; Get and add replacement value
             (when-let* ((repl-fn (plist-get chat-input (intern key)))
                         (value (funcall repl-fn context)))
               (push value parts))
             (setq last-pos end-pos)
             finally
             ;; Add remaining text after last match
             (push (substring template last-pos) parts))
    ;; Join parts in reverse order
    (apply #'concat (nreverse parts))))

(defun minuet--make-context-filter-sequence (context len)
  "Create a filtering string based on CONTEXT with maximum length LEN."
  (if-let* ((is-string (stringp context))
            (is-positive (> len 0))
            (context (replace-regexp-in-string "\\`[\s\t\n]+" "" context))
            (should-filter (>= (length context) len))
            (context (substring context 0 len))
            (context (replace-regexp-in-string "[\s\t\n]+\\'" "" context)))
      context
    ""))

(defun minuet--filter-text (text sequence)
  "Remove the SEQUENCE and the rest part from TEXT."
  (cond
   ((or (null sequence) (null text)) text)
   ((equal sequence "") text)
   (t
    (let ((start (string-match-p (regexp-quote sequence) text)))
      (if start
          (substring text 0 start)
        text)))))

(defun minuet--filter-sequence-in-items (items sequence)
  "For each item in ITEMS, apply `minuet--filter-text' with SEQUENCE."
  (mapcar (lambda (x) (minuet--filter-text x sequence))
          items))

(defun minuet--filter-context-sequence-in-items (items context)
  "Apply the filter sequence in each item in ITEMS.
The filter sequence is obtained from CONTEXT."
  (minuet--filter-sequence-in-items
   items (minuet--make-context-filter-sequence
          (plist-get context :after-cursor)
          minuet-after-cursor-filter-length)))

(defun minuet--stream-decode (response get-text-fn)
  "Decode the RESPONSE using GET-TEXT-FN."
  (setq response (split-string response "[\r]?\n"))
  (let (result)
    (dolist (line response)
      (if-let* ((json (ignore-errors
                        (json-parse-string
                         (replace-regexp-in-string "^data: " "" line)
                         :object-type 'plist :array-type 'list)))
                (text (ignore-errors
                        (funcall get-text-fn json))))
          (when (and (stringp text)
                     (not (equal text "")))
            (push text result))))
    (setq result (apply #'concat (nreverse result)))
    (if (equal result "")
        (progn (minuet--log (format "Minuet returns no text for streaming: %s" response))
               nil)
      result)))

(defmacro minuet--make-process-stream-filter (response)
  "Store the data into RESPONSE which should hold a plain list."
  (declare (debug (gv-place)))
  `(lambda (proc text)
     (funcall #'internal-default-process-filter proc text)
     ;; (setq ,response (append ,response (list text)))
     (push text ,response)))

(defun minuet--stream-decode-raw (response get-text-fn)
  "Decode the raw stream used by minuet.

RESPONSE will be stored in the temp variable create by
`minuet--make-process-stream-filter' parsed by GET-TEXT-FN."
  (when-let* ((response (nreverse response))
              (response (apply #'concat response)))
    (minuet--stream-decode response get-text-fn)))

(defun minuet--handle-chat-completion-timeout (context err response get-text-fn name callback)
  "Handle the timeout error for chat completion.
This function will decode and send the partial complete response to
the callback, and log the error.  CONTEXT, ERR, RESPONSE, GET-TEXT-FN,
NAME, CALLBACK are used to deliver partial completion items and log
the errors."
  (if (equal (car (plz-error-curl-error err)) 28)
      (progn
        (minuet--log (format "%s Request timeout" name))
        (when-let* ((result (minuet--stream-decode-raw response get-text-fn))
                    (completion-items (minuet--parse-completion-itmes-default result))
                    (completion-items (minuet--filter-context-sequence-in-items
                                       completion-items
                                       context))
                    (completion-items (minuet--remove-spaces completion-items)))
          (funcall callback completion-items)))
    (minuet--log (format "An error occured when sending request to %s" name))
    (minuet--log err)))

(defmacro minuet--with-temp-response (&rest body)
  "Execute BODY with a temporary response collection.
This macro creates a local variable `--response--' that can be used to
collect process output within the BODY.  It's designed to work in
conjunction with `minuet--make-process-stream-filter'.  The
`--response--' variable is initialized as an empty list and can be
used to accumulate text output from a process.  After execution,
`--response--' will contain the collected responses in reverse order."
  (declare (debug t) (indent 0))
  `(let (--response--) ,@body))

;;;###autoload
(defun minuet-accept-suggestion ()
  "Accept the current overlay suggestion."
  (interactive)
  (when (and minuet--current-suggestions
             minuet--current-overlay)
    (let ((suggestion (nth minuet--current-suggestion-index
                           minuet--current-suggestions)))
      (minuet--cleanup-suggestion)
      (insert suggestion))))

;;;###autoload
(defun minuet-dismiss-suggestion ()
  "Dismiss the current overlay suggestion."
  (interactive)
  (minuet--cleanup-suggestion))

;;;###autoload
(defun minuet-accept-suggestion-line (&optional n)
  "Accept N lines of the current suggestion.
When called interactively with a numeric prefix argument, accept that
many lines.  Without a prefix argument, accept only the first line."
  (interactive "p")
  (when (and minuet--current-suggestions
             minuet--current-overlay)
    (let* ((suggestion (nth minuet--current-suggestion-index
                            minuet--current-suggestions))
           (lines (split-string suggestion "\n"))
           (n (or n 1))
           (selected-lines (seq-take lines n)))
      (minuet--cleanup-suggestion)
      (insert (string-join selected-lines "\n")))))

;;;###autoload
(defun minuet-complete-with-minibuffer ()
  "Complete using minibuffer interface."
  (interactive)
  (let ((current-buffer (current-buffer))
        (available-p-fn (intern (format "minuet--%s-available-p" minuet-provider)))
        (complete-fn (intern (format "minuet--%s-complete" minuet-provider)))
        (context (minuet--get-context))
        (completing-read (lambda (items) (completing-read "Complete: " items nil t)))
        (consult--read (lambda (items)
                         (consult--read
                          items
                          :prompt "Complete: "
                          :require-match t
                          :state (consult--insertion-preview (point) (point))))))
    (unless (funcall available-p-fn)
      (minuet--log (format "Minuet provider %s is not available" minuet-provider))
      (error "Minuet provider %s is not available" minuet-provider))
    (funcall complete-fn
             context
             (lambda (items)
               (with-current-buffer current-buffer
                 (setq items (if minuet-add-single-line-entry
                                 (minuet--add-single-line-entry items)
                               items)
                       items (-distinct items))
                 ;; close current minibuffer session, if any
                 (when (active-minibuffer-window)
                   (abort-recursive-edit))
                 (when-let* ((items)
                             (selected (funcall
                                        (if (require 'consult nil t) consult--read completing-read)
                                        items)))
                   (unless (string-empty-p selected)
                     (insert selected))))))))

(defun minuet--get-api-key (api-key)
  "Get the api-key from API-KEY.
API-KEY can be a string (as an environment variable) or a function.
Return nil if not exists or is an empty string."
  (let ((key (if (stringp api-key)
                 (getenv api-key)
               (when (functionp api-key)
                 (funcall api-key)))))
    (when (or (null key)
              (string-empty-p key))
      (minuet--log
       (if (stringp api-key)
           (format "%s is not a valid environment variable.
If using ollama you can just set it to 'TERM'." api-key)
         "The api-key function returns nil or returns an empty string")))
    (and (not (equal key "")) key)))


(defun minuet--codestral-available-p ()
  "Check if codestral if available."
  (minuet--get-api-key (plist-get minuet-codestral-options :api-key)))

(defun minuet--openai-available-p ()
  "Check if openai if available."
  (minuet--get-api-key (plist-get minuet-openai-options :api-key)))

(defun minuet--claude-available-p ()
  "Check if claude is available."
  (minuet--get-api-key (plist-get minuet-claude-options :api-key)))

(defun minuet--openai-compatible-available-p ()
  "Check if the specified openai-compatible service is available."
  (when-let* ((options minuet-openai-compatible-options)
              (env-var (plist-get options :api-key))
              (end-point (plist-get options :end-point))
              (model (plist-get options :model)))
    (minuet--get-api-key env-var)))

(defun minuet--openai-fim-compatible-available-p ()
  "Check if the specified openai-fim-compatible service is available."
  (when-let* ((options minuet-openai-fim-compatible-options)
              (env-var (plist-get options :api-key))
              (name (plist-get options :name))
              (end-point (plist-get options :end-point))
              (model (plist-get options :model)))
    (minuet--get-api-key env-var)))

(defun minuet--gemini-available-p ()
  "Check if gemini is available."
  (minuet--get-api-key (plist-get minuet-gemini-options :api-key)))

(defun minuet--parse-completion-itmes-default (items)
  "Parse ITEMS into a list of completion entries."
  (split-string items "<endCompletion>"))

(defun minuet--make-system-prompt (template &optional n-completions)
  "Create system prompt used in chat LLM from TEMPLATE and N-COMPLETIONS."
  (let* ((tmpl (plist-get template :template))
         (tmpl (minuet--eval-value tmpl))
         (n-completions (or n-completions minuet-n-completions 1))
         (n-completions-template (plist-get template :n-completions-template))
         (n-completions-template (minuet--eval-value n-completions-template))
         (n-completions-template (if (stringp n-completions-template)
                                     (format n-completions-template n-completions)
                                   "")))
    (setq tmpl (replace-regexp-in-string "{{{:n-completions-template}}}"
                                         n-completions-template
                                         tmpl)
          tmpl (replace-regexp-in-string
                "{{{\\([^{}]+\\)}}}"
                (lambda (str)
                  (minuet--eval-value (plist-get template (intern (match-string 1 str)))))
                tmpl)
          ;; replace placeholders that are not replaced
          tmpl (replace-regexp-in-string "{{{.*}}}" "" tmpl))))

(defun minuet--openai-fim-complete-base (options get-text-fn context callback)
  "The base function to complete code with openai fim API.
OPTIONS are the provider options.  GET-TEXT-FN are the function to get
the completion items from json.  CONTEXT is to be used to build the
prompt.  CALLBACK is the function to be called when completion items
arrive."
  (let ((total-try (or minuet-n-completions 1))
        (name (plist-get options :name))
        (body (json-serialize
               `(,@(plist-get options :optional)
                 :stream t
                 :model ,(plist-get options :model)
                 :prompt ,(funcall (--> options
                                        (plist-get it :template)
                                        (plist-get it :prompt))
                                   context)
                 ,@(when-let* ((suffix-fn (--> options
                                               (plist-get it :template)
                                               (plist-get it :suffix))))
                     (list :suffix (funcall suffix-fn context))))))
        completion-items)
    (dotimes (_ total-try)
      (minuet--with-temp-response
        (push
         (plz 'post (plist-get options :end-point)
           :headers `(("Content-Type" . "application/json")
                      ("Accept" . "application/json")
                      ("Authorization" . ,(concat "Bearer " (minuet--get-api-key (plist-get options :api-key)))))
           :timeout minuet-request-timeout
           :body body
           :as 'string
           :filter (minuet--make-process-stream-filter --response--)
           :then
           (lambda (json)
             (when-let* ((result (minuet--stream-decode json get-text-fn)))
               ;; insert the current result into the completion items list
               (push result completion-items))
             (setq completion-items (minuet--filter-context-sequence-in-items
                                     completion-items
                                     context))
             (setq completion-items (minuet--remove-spaces completion-items))
             (funcall callback completion-items))
           :else
           (lambda (err)
             (if (equal (car (plz-error-curl-error err)) 28)
                 (progn
                   (minuet--log (format "%s Request timeout" name))
                   (when-let* ((result (minuet--stream-decode-raw --response-- get-text-fn)))
                     (push result completion-items)))
               (minuet--log (format "An error occured when sending request to %s" name))
               (minuet--log err))
             (setq completion-items
                   (minuet--filter-context-sequence-in-items
                    completion-items
                    context))
             (setq completion-items (minuet--remove-spaces completion-items))
             (funcall callback completion-items)))
         minuet--current-requests)))))

(defun minuet--codestral-complete (context callback)
  "Complete code with codestral.
CONTEXT and CALLBACK will be passed to the base function."
  (minuet--openai-fim-complete-base
   (plist-put (copy-tree minuet-codestral-options) :name "Codestral")
   #'minuet--openai-get-text-fn
   context
   callback))

(defun minuet--openai-fim-compatible-complete (context callback)
  "Complete code with openai fim API.
CONTEXT and CALLBACK will be passed to the base function."
  (minuet--openai-fim-complete-base
   (copy-tree minuet-openai-fim-compatible-options)
   #'minuet--openai-fim-get-text-fn
   context
   callback))

(defun minuet--openai-fim-get-text-fn (json)
  "Function to get the completion from a JSON object for openai-fim compatible."
  (--> json
       (plist-get it :choices)
       car
       (plist-get it :text)))

(defun minuet--openai-get-text-fn (json)
  "Function to get the completion from a JSON object for openai compatible service."
  (--> json
       (plist-get it :choices)
       car
       (plist-get it :delta)
       (plist-get it :content)))

(defun minuet--openai-complete-base (options context callback)
  "The base function to complete code with openai API.
OPTIONS are the provider options.  the completion items from json.
CONTEXT is to be used to build the prompt.  CALLBACK is the function
to be called when completion items arrive."
  (minuet--with-temp-response
    (push
     (plz 'post (plist-get options :end-point)
       :headers
       `(("Content-Type" . "application/json")
         ("Accept" . "application/json")
         ("Authorization" . ,(concat "Bearer " (minuet--get-api-key (plist-get options :api-key)))))
       :timeout minuet-request-timeout
       :body
       (json-serialize
        `(,@(plist-get options :optional)
          :stream t
          :model ,(plist-get options :model)
          :messages ,(vconcat
                      `((:role "system"
                         :content ,(minuet--make-system-prompt (plist-get options :system)))
                        ,@(minuet--eval-value (plist-get options :fewshots))
                        (:role "user"
                         :content ,(minuet--make-chat-llm-shot context options))))))
       :as 'string
       :filter (minuet--make-process-stream-filter --response--)
       :then
       (lambda (json)
         (when-let* ((result (minuet--stream-decode json #'minuet--openai-get-text-fn))
                     (completion-items (minuet--parse-completion-itmes-default result))
                     (completion-items (minuet--filter-context-sequence-in-items
                                        completion-items
                                        context))
                     (completion-items (minuet--remove-spaces completion-items)))
           ;; insert the current result into the completion items list
           (funcall callback completion-items)))
       :else
       (lambda (err)
         (minuet--handle-chat-completion-timeout
          context err --response-- #'minuet--openai-get-text-fn "OpenAI" callback)))
     minuet--current-requests)))

(defun minuet--openai-complete (context callback)
  "Complete code with OpenAI.
CONTEXT and CALLBACK will be passed to the base function."
  (minuet--openai-complete-base
   (--> (copy-tree minuet-openai-options)
        (plist-put it :end-point "https://api.openai.com/v1/chat/completions"))
   context callback))

(defun minuet--openai-compatible-complete (context callback)
  "Complete code with OpenAI compatible service.
CONTEXT and CALLBACK will be passed to the base function."
  (minuet--openai-complete-base
   (copy-tree minuet-openai-compatible-options) context callback))

(defun minuet--claude-get-text-fn (json)
  "Function to get the completion from a JSON object for claude."
  (--> json
       (plist-get it :delta)
       (plist-get it :text)))

(defun minuet--claude-complete (context callback)
  "Complete code with Claude.
CONTEXT is to be used to build the prompt.  CALLBACK is the function
to be called when completion items arrive."
  (minuet--with-temp-response
    (push
     (plz 'post "https://api.anthropic.com/v1/messages"
       :headers `(("Content-Type" . "application/json")
                  ("Accept" . "application/json")
                  ("x-api-key" . ,(minuet--get-api-key (plist-get minuet-claude-options :api-key)))
                  ("anthropic-version" . "2023-06-01"))
       :timeout minuet-request-timeout
       :body
       (json-serialize
        (let ((options (copy-tree minuet-claude-options)))
          `(,@(plist-get options :optional)
            :stream t
            :model ,(plist-get options :model)
            :system ,(minuet--make-system-prompt (plist-get options :system))
            :max_tokens ,(plist-get options :max_tokens)
            :messages ,(vconcat
                        `(,@(minuet--eval-value (plist-get options :fewshots))
                          (:role "user"
                           :content ,(minuet--make-chat-llm-shot context minuet-claude-options)))))))
       :as 'string
       :filter (minuet--make-process-stream-filter --response--)
       :then
       (lambda (json)
         (when-let* ((result (minuet--stream-decode json #'minuet--claude-get-text-fn))
                     (completion-items (minuet--parse-completion-itmes-default result))
                     (completion-items (minuet--filter-context-sequence-in-items
                                        completion-items
                                        context))
                     (completion-items (minuet--remove-spaces completion-items)))
           ;; insert the current result into the completion items list
           (funcall callback completion-items)))
       :else
       (lambda (err)
         (minuet--handle-chat-completion-timeout
          context err --response-- #'minuet--claude-get-text-fn "Claude" callback)))
     minuet--current-requests)))

(defun minuet--gemini-get-text-fn (json)
  "Function to get the completion from a JSON object for gemini."
  (--> json
       (plist-get it :candidates)
       car
       (plist-get it :content)
       (plist-get it :parts)
       car
       (plist-get it :text)))

(defun minuet--gemini-complete (context callback)
  "Complete code with gemini.
CONTEXT is to be used to build the prompt.  CALLBACK is the function
to be called when completion items arrive."
  (minuet--with-temp-response
    (push
     (plz 'post (format "https://generativelanguage.googleapis.com/v1beta/models/%s:streamGenerateContent?alt=sse&key=%s"
                        (plist-get minuet-gemini-options :model)
                        (minuet--get-api-key (plist-get minuet-gemini-options :api-key)))
       :headers `(("Content-Type" . "application/json")
                  ("Accept" . "application/json"))
       :timeout minuet-request-timeout
       :body
       (json-serialize
        (let* ((options (copy-tree minuet-gemini-options))
               (fewshots (minuet--eval-value (plist-get options :fewshots)))
               (fewshots (mapcar
                          (lambda (shot)
                            `(:role
                              ,(if (equal (plist-get shot :role) "user") "user" "model")
                              :parts
                              [(:text ,(plist-get shot :content))]))
                          fewshots)))
          `(,@(plist-get options :optional)
            :system_instruction (:parts (:text ,(minuet--make-system-prompt (plist-get options :system))))
            :contents ,(vconcat
                        `(,@fewshots
                          (:role "user"
                           :parts [(:text ,(minuet--make-chat-llm-shot context minuet-gemini-options))]))))))
       :as 'string
       :filter (minuet--make-process-stream-filter --response--)
       :then
       (lambda (json)
         (when-let* ((result (minuet--stream-decode json #'minuet--gemini-get-text-fn))
                     (completion-items (minuet--parse-completion-itmes-default result))
                     (completion-items (minuet--filter-context-sequence-in-items
                                        completion-items
                                        context))
                     (completion-items (minuet--remove-spaces completion-items)))
           (funcall callback completion-items)))
       :else
       (lambda (err)
         (minuet--handle-chat-completion-timeout
          context err --response-- #'minuet--gemini-get-text-fn "Gemini" callback)))
     minuet--current-requests)))


(defun minuet--setup-auto-suggestion ()
  "Setup auto-suggestion with `post-command-hook'."
  (add-hook 'post-command-hook #'minuet--maybe-show-suggestion nil t))

(defun minuet--is-minuet-command ()
  "Return t if current command is a minuet command."
  (and this-command
       (symbolp this-command)
       (string-match-p "^minuet" (symbol-name this-command))))

(defun minuet--is-not-on-throttle ()
  "Return t if current time since last time is larger than the throttle delay."
  (or (null minuet--last-auto-suggestion-time)
      (> (float-time (time-since minuet--last-auto-suggestion-time))
         minuet-auto-suggestion-throttle-delay)))

(defun minuet--maybe-show-suggestion ()
  "Show suggestion with debouncing and throttling."
  (when (and (minuet--is-not-on-throttle)
             (not (minuet--is-minuet-command)))
    (when minuet--debounce-timer
      (cancel-timer minuet--debounce-timer))
    (setq minuet--debounce-timer
          (let ((buffer (current-buffer)))
            (run-with-idle-timer
             minuet-auto-suggestion-debounce-delay nil
             (lambda ()
               (when (and (eq buffer (current-buffer))
                          (or (null minuet--auto-last-point)
                              (not (eq minuet--auto-last-point (point))))
                          (not (run-hook-with-args-until-success 'minuet-auto-suggestion-block-functions)))
                 (setq minuet--last-auto-suggestion-time (current-time)
                       minuet--auto-last-point (point))
                 (minuet-show-suggestion))))))))

(defun minuet--default-fim-prompt-function (ctx)
  "Default function to generate prompt for FIM completions from CTX."
  (format "%s\n%s"
          (plist-get ctx :language-and-tab)
          (plist-get ctx :before-cursor)))

(defun minuet--default-fim-suffix-function (ctx)
  "Default function to generate suffix for FIM completions from CTX."
  (plist-get ctx :after-cursor))

(defun minuet--default-chat-input-language-and-tab-function (ctx)
  "Default function to get language and tab style from CTX."
  (plist-get ctx :language-and-tab))

(defun minuet--default-chat-input-before-cursor-function (ctx)
  "Default function to get before cursor from CTX.
If context is incomplete, remove first line to avoid partial code."
  (let ((text (plist-get ctx :before-cursor))
        (incomplete (plist-get ctx :is-incomplete-before)))
    (when incomplete
      (setq text (replace-regexp-in-string "\\`.*\n" "" text)))
    text))

(defun minuet--default-chat-input-after-cursor-function (ctx)
  "Default function to get after cursor from CTX.
If context is incomplete, remove last line to avoid partial code."
  (let ((text (plist-get ctx :after-cursor))
        (incomplete (plist-get ctx :is-incomplete-after)))
    (when incomplete
      (setq text (replace-regexp-in-string "\n.*\\'" "" text)))
    text))

(defun minuet--cleanup-auto-suggestion ()
  "Clean up auto-suggestion timers and hooks."
  (remove-hook 'post-command-hook #'minuet--maybe-show-suggestion t)
  (when minuet--debounce-timer
    (cancel-timer minuet--debounce-timer)
    (setq minuet--debounce-timer nil))
  (setq minuet--auto-last-point nil))

;;;###autoload
(define-minor-mode minuet-auto-suggestion-mode
  "Toggle automatic code suggestions.
When enabled, Minuet will automatically show suggestions while you type."
  :init-value nil
  :lighter " Minuet"
  (if minuet-auto-suggestion-mode
      (minuet--setup-auto-suggestion)
    (minuet--cleanup-auto-suggestion)))

(defvar minuet-active-mode-map
  (let ((map (make-sparse-keymap))) map)
  "Keymap used when `minuet-active-mode' is enabled.")

(define-minor-mode minuet-active-mode
  "Activated when there is an active suggestion in minuet."
  :init-value nil
  :keymap minuet-active-mode-map)

;;;###autoload
(defun minuet-configure-provider ()
  "Configure a minuet provider interactively.
This command offers an interactive approach to configuring provider
settings, as an alternative to manual configuration via `setq' and
`plist-put'.  When selecting either `openai-compatible' or
`openai-fim-compatible' providers, users will be prompted to specify
their endpoint and API key."
  (interactive)
  (let* ((providers '(("OpenAI" . openai)
                      ("Claude" . claude)
                      ("Codestral" . codestral)
                      ("OpenAI Compatible" . openai-compatible)
                      ("OpenAI FIM Compatible" . openai-fim-compatible)
                      ("Gemini" . gemini)))
         (provider-name (completing-read "Select provider: " providers nil t))
         (provider (alist-get provider-name providers nil nil #'equal))
         (options-sym (intern (format "minuet-%s-options" provider)))
         (options (symbol-value options-sym))
         (current-model (plist-get options :model))
         (model (read-string "Model: " (or current-model ""))))

    (plist-put options :model model)

    ;; For OpenAI compatible providers, also configure endpoint and API key
    (when (memq provider '(openai-compatible openai-fim-compatible))
      (let* ((current-endpoint (plist-get options :end-point))
             (current-api-key (plist-get options :api-key))
             (endpoint (read-string "Endpoint URL: " (or current-endpoint "")))
             (api-key (read-string "API Key Environment Variable or Function: "
                                   (cond ((stringp current-api-key) current-api-key)
                                         ((symbolp current-api-key) (symbol-name current-api-key))
                                         (t ""))))
             ;; If the user enters nothing via `read-string`, retain the current API key.
             (final-api-key (cond ((equal "" api-key) current-api-key)
                                  ((functionp (intern-soft api-key)) (intern-soft api-key))
                                  (t api-key))))
        (plist-put options :end-point endpoint)
        (plist-put options :api-key final-api-key)))

    (setq minuet-provider provider)
    (message "Minuet provider configured to %s with model %s" provider-name model)))

(provide 'minuet)
;;; minuet.el ends here
