[emacs] Fix some errors that accrued while hacking
The problem with interactive development in lisp is if you delete a defun you might never notice until it's too late.
This commit is contained in:
parent
fec8b0006d
commit
77ae3cdae6
1 changed files with 22 additions and 68 deletions
|
|
@ -93,61 +93,28 @@
|
||||||
right-margin-width 2)))
|
right-margin-width 2)))
|
||||||
buffer))
|
buffer))
|
||||||
|
|
||||||
(defun claude-send-message (prompt &optional system-prompt tools)
|
(defun claude-send-request (data &optional callback error-callback)
|
||||||
"Send PROMPT to Claude and display the response.
|
"Send request with DATA to Claude API.
|
||||||
If SYSTEM-PROMPT is provided, include it in the request.
|
If CALLBACK is provided, call it with the response data.
|
||||||
If TOOLS is provided, enable tool use."
|
If ERROR-CALLBACK is provided, call it with any error."
|
||||||
(let ((data `((model . ,claude-model)
|
(let ((api-key (claude-get-api-key)))
|
||||||
(max_tokens . ,claude-max-tokens)
|
(request
|
||||||
(messages . [((role . "user")
|
"https://api.anthropic.com/v1/messages"
|
||||||
(content . ,prompt))]))))
|
:type "POST"
|
||||||
|
:headers `(("Content-Type" . "application/json")
|
||||||
;; Add system prompt if provided
|
("x-api-key" . ,api-key)
|
||||||
(when system-prompt
|
("anthropic-version" . "2023-06-01"))
|
||||||
(setq data (append data `((system . ,system-prompt)))))
|
:data (json-encode data)
|
||||||
|
:parser 'json-read
|
||||||
;; Add tools if provided
|
:success (cl-function
|
||||||
(when tools
|
(lambda (&key data &allow-other-keys)
|
||||||
(setq data (append data `((tools . ,tools)
|
(when callback
|
||||||
(tool_choice . "auto")))))
|
(funcall callback data))))
|
||||||
|
:error (cl-function
|
||||||
;; Store the request data for refresh functionality
|
(lambda (&key error-thrown &allow-other-keys)
|
||||||
(setq claude-last-request (list :prompt prompt
|
(if error-callback
|
||||||
:system-prompt system-prompt
|
(funcall error-callback error-thrown)
|
||||||
:tools tools))
|
(message "Claude API error: %s" error-thrown)))))))
|
||||||
|
|
||||||
;; Display the buffer with a loading message
|
|
||||||
(let ((buffer (claude-ensure-buffer)))
|
|
||||||
(with-current-buffer buffer
|
|
||||||
(let ((inhibit-read-only t))
|
|
||||||
(erase-buffer)
|
|
||||||
(insert "Sending request to Claude...\n\n")))
|
|
||||||
(when claude-auto-display-results
|
|
||||||
(display-buffer buffer)))
|
|
||||||
|
|
||||||
;; Send request
|
|
||||||
(claude-send-request
|
|
||||||
data
|
|
||||||
(lambda (data)
|
|
||||||
(if tools
|
|
||||||
(claude-handle-tool-response data (claude-ensure-buffer))
|
|
||||||
(claude-display-response data)))
|
|
||||||
(lambda (error-thrown)
|
|
||||||
(let ((buffer (claude-ensure-buffer)))
|
|
||||||
(with-current-buffer buffer
|
|
||||||
(let ((inhibit-read-only t))
|
|
||||||
(erase-buffer)
|
|
||||||
(insert (format "Error: %s" error-thrown)))))))))
|
|
||||||
|
|
||||||
(defun claude-extract-text-content (message)
|
|
||||||
"Extract text content from MESSAGE returned by the API."
|
|
||||||
(let ((content-items (cdr (assoc 'content message)))
|
|
||||||
(result ""))
|
|
||||||
(dolist (item content-items)
|
|
||||||
(let ((type (cdr (assoc 'type item))))
|
|
||||||
(when (string= type "text")
|
|
||||||
(setq result (concat result (cdr (assoc 'text item)) "\n\n")))))
|
|
||||||
result))
|
|
||||||
|
|
||||||
(defun claude-display-response (data)
|
(defun claude-display-response (data)
|
||||||
"Display the response DATA from Claude with nice formatting."
|
"Display the response DATA from Claude with nice formatting."
|
||||||
|
|
@ -193,19 +160,6 @@ If TOOLS is provided, enable tool use."
|
||||||
(unless (get-buffer-window buffer)
|
(unless (get-buffer-window buffer)
|
||||||
(display-buffer-other-window buffer)))))
|
(display-buffer-other-window buffer)))))
|
||||||
|
|
||||||
;; (defun claude-display-response (data)
|
|
||||||
;; "Display the response DATA from Claude."
|
|
||||||
;; (let* ((message (aref (cdr (assoc 'messages data)) 0))
|
|
||||||
;; (content (claude-extract-text-content message))
|
|
||||||
;; (buffer (claude-ensure-buffer)))
|
|
||||||
;; (with-current-buffer buffer
|
|
||||||
;; (let ((inhibit-read-only t))
|
|
||||||
;; (erase-buffer)
|
|
||||||
;; (insert content)
|
|
||||||
;; (goto-char (point-min))))
|
|
||||||
;; (when claude-auto-display-results
|
|
||||||
;; (display-buffer buffer))))
|
|
||||||
|
|
||||||
(defun claude-send-message (prompt &optional system-prompt tools)
|
(defun claude-send-message (prompt &optional system-prompt tools)
|
||||||
"Send PROMPT to Claude and display the response.
|
"Send PROMPT to Claude and display the response.
|
||||||
If SYSTEM-PROMPT is provided, include it in the request.
|
If SYSTEM-PROMPT is provided, include it in the request.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue