diff --git a/org-ai-openai-image-query.el b/org-ai-openai-image-query.el index d31a28c..fad6464 100644 --- a/org-ai-openai-image-query.el +++ b/org-ai-openai-image-query.el @@ -60,13 +60,13 @@ Calls CALLBACK with the response." (defun org-ai--get-image-path-or-url () "Prompt the user for a non-empty image path or URL." - ;; TODO make file path or url nicer to use - (let ((image-path-or-url "")) - (while (or (string-empty-p image-path-or-url) - (and (not (string-match-p "^https?://" image-path-or-url)) - (not (file-exists-p image-path-or-url)))) - (setq image-path-or-url (read-string "Image file path or URL (must be non-empty): "))) - image-path-or-url)) + ;; TODO ensure non-nil + (let ((default (or (thing-at-point 'url) + (thing-at-point 'filename)))) + (read-string (if default + (format "Enter image URL or file path (default %s): " default) + "Enter image URL or file path: ") + nil 'minibuffer-history default))) (defun org-ai--get-question () "Prompt the user for a non-empty question." @@ -78,10 +78,16 @@ Calls CALLBACK with the response." ;;;###autoload (defun org-ai-query-image () "Query OpenAI API with a BASE64 encoded image path or URL and a QUESTION." - (interactive (list - (read-file-name "Image file path: " nil nil nil (or (thing-at-point 'existing-filename) - (thing-at-point 'url))) - (read-string "Question: " nil 'minibuffer-history)))) + (interactive) + (let ((image-path-or-url (org-ai--get-image-path-or-url)) + (question (org-ai--get-question))) + (if (string-match-p "^https?://" image-path-or-url) + (org-ai--send-url-image-query image-path-or-url question #'org-ai--handle-openai-response) + (let ((base64-image (with-temp-buffer + (insert-file-contents-literally image-path-or-url) + (base64-encode-region (point-min) (point-max)) + (buffer-string)))) + (org-ai--send-base64-image-query base64-image question #'org-ai--handle-openai-response))))) (provide 'org-ai-openai-image-query) diff --git a/org-ai.el b/org-ai.el index d96eb54..ee1d76f 100644 --- a/org-ai.el +++ b/org-ai.el @@ -187,10 +187,10 @@ It's designed to \"do the right thing\": ;; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -(defun org-ai-query-image-and-display (image-path question) +(defun org-ai-query-image-and-display () "Query OpenAI API with a BASE64 encoded IMAGE-PATH and a QUESTION, and display the result." - (interactive "fImage file path: \nsQuestion: ") - (org-ai-query-image image-path question)) + (interactive) + (org-ai-query-image)) (defvar org-ai-mode-map (make-sparse-keymap) "Keymap for `org-ai-mode'.")