branch: externals/llm commit 03afb1d795d709821fe5e7019d6b3dcae8b39995 Author: Roman Scherer <ro...@burningswell.com> Commit: Roman Scherer <ro...@burningswell.com>
Use symbols for event types & improve timeout handling This changes the event type from a string to a symbol. This is more aligned wirh the Emacs Lisp way and the way we handle media type and subtype. --- llm-claude.el | 14 +++++++------- llm-openai.el | 4 ++-- plz-event-source.el | 19 +++++++++---------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/llm-claude.el b/llm-claude.el index 3e85c43b9d..30d3912947 100644 --- a/llm-claude.el +++ b/llm-claude.el @@ -119,19 +119,19 @@ STREAM is a boolean indicating whether the response should be streamed." ;; We ignore many types of messages; these might become important if Claude ;; sends a few different alternate contents, but for now they don't do ;; that. - `(("message_start" . ,(lambda (_))) - ("content_block_start" . ,(lambda (_))) - ("ping" . ,(lambda (_))) - ("message_stop" . ,(lambda (_))) - ("content_block_stop" . ,(lambda (_))) - ("content_block_delta" . + `((message_start . ,(lambda (_))) + (content_block_start . ,(lambda (_))) + (ping . ,(lambda (_))) + (message_stop . ,(lambda (_))) + (content_block_stop . ,(lambda (_))) + (content_block_delta . ,(lambda (data) (setq in-flight-message (concat in-flight-message (let* ((json (json-parse-string data :object-type 'alist)) (delta (assoc-default 'delta json)) (type (assoc-default 'type delta))) - (when (equal type "text_delta") + (when (eql type 'text_delta) (assoc-default 'text delta))))) (llm-request-plz-callback-in-buffer buf diff --git a/llm-openai.el b/llm-openai.el index 17eb3ce448..17ecc30d68 100644 --- a/llm-openai.el +++ b/llm-openai.el @@ -303,13 +303,13 @@ RESPONSE can be nil if the response is complete." :headers (llm-openai--headers provider) :data (llm-openai--chat-request (llm-openai-chat-model provider) prompt t) :event-stream-handlers - `(("message" . ,(lambda (data) + `((message . ,(lambda (data) (when (not (equal data "[DONE]")) (when-let ((response (llm-openai--get-partial-chat-response (json-read-from-string data)))) (when (stringp response) (llm-request-plz-callback-in-buffer buf partial-callback response)))))) - ("error" . ,(lambda (data) + (error . ,(lambda (data) (llm-request-plz-callback-in-buffer buf error-callback 'error data)))) :on-error (lambda (_ data) diff --git a/plz-event-source.el b/plz-event-source.el index 0bdb7ebf5e..c6861f7deb 100644 --- a/plz-event-source.el +++ b/plz-event-source.el @@ -33,7 +33,6 @@ (require 'cl-lib) (require 'eieio) -(require 'pcase) (require 'plz) (require 'plz-media-type) (require 'rx) @@ -61,9 +60,9 @@ (type :accessor plz-event-source-event-type :initarg :type - :initform "message" + :initform 'message :documentation "The event type." - :type string)) + :type symbol)) "The server sent event class.") ;; Parser @@ -145,8 +144,8 @@ last-event-id) :origin (buffer-name) :type (if (string-blank-p event-type-buffer) - "message" - event-type-buffer)))) + 'message + (intern event-type-buffer))))) (setf data-buffer "" event-type-buffer "") (setf events (cons event events)) @@ -339,7 +338,7 @@ "Open a connection to the URL of the event SOURCE." (with-slots (buffer errors options ready-state parser) source (with-current-buffer (get-buffer-create buffer) - (let ((event (plz-event-source-event :type "open"))) + (let ((event (plz-event-source-event :type 'open))) (setf ready-state 'connecting) (setf parser (plz-event-source-parser :buffer buffer @@ -351,7 +350,7 @@ (cl-defmethod plz-event-source-close ((source plz-buffer-event-source)) "Close the connection of the event SOURCE." (with-slots (buffer ready-state) source - (let ((event (plz-event-source-event :type "close"))) + (let ((event (plz-event-source-event :type 'close))) (setf ready-state 'closed) (plz-event-source-dispatch-event source event) source))) @@ -414,7 +413,7 @@ (cl-defmethod plz-media-type-else ((_ plz-media-type:text/event-stream) error) "Transform the ERROR into a format suitable for MEDIA-TYPE." (let* ((source plz-event-source--current) - (event (plz-event-source-event :type "error" :data error))) + (event (plz-event-source-event :type 'error :data error))) (plz-event-source-close source) (plz-event-source-dispatch-event source event) error)) @@ -433,11 +432,11 @@ (let ((type (car pair)) (handler (cdr pair))) (cond - ((equal "open" type) + ((equal 'open type) (cons type (lambda (source event) (setf (oref event data) response) (funcall handler source event)))) - ((equal "close" type) + ((equal 'close type) (cons type (lambda (source event) (setf (oref event data) response) (funcall handler source event))))