From 4ef9d8f494562a32c846548f3b1b288187575735 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Fri, 5 Jan 2024 20:44:24 +0100 Subject: [PATCH 01/17] Add ability to insert summaries at point --- kagi.el | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/kagi.el b/kagi.el index ac8add1..9874297 100644 --- a/kagi.el +++ b/kagi.el @@ -281,6 +281,11 @@ list of conses." (text-mode) (display-buffer buffer-name))) +(defun kagi--insert-summary (summary) + "Insert the SUMMARY at point." + (save-excursion + (insert (substring-no-properties summary)))) + (defun kagi--process-prompt (prompt) "Submit a PROMPT to FastGPT and process the API response. @@ -370,11 +375,15 @@ Shows the summary in a new window." (kagi--summary-buffer-name (buffer-name)))) ;;;###autoload -(defun kagi-summarize-url (url) +(defun kagi-summarize-url (url &optional insert) "Show the summary of the content behind the given URL. -According to the API documentation, the following media types are -supported: +By default, the summary is shown in a new buffer. With prefix +argument INSERT, insert the summary at point in the current +buffer. + +According to the Kagi API documentation, the following media +types are supported: - Text web pages, articles, and forum threads - PDF documents (.pdf) @@ -383,10 +392,14 @@ supported: - Audio files (mp3/wav) - YouTube URLs - Scanned PDFs and images (OCR)" - (interactive "sURL: ") - (kagi--display-summary - (kagi-summarize url) - (kagi--summary-buffer-name (kagi--get-domain-name url)))) + (interactive "sURL: \np") + (message "%"d) + (let ((summary (kagi-summarize url))) + (if (eql insert 4) + (kagi--insert-summary summary) + (kagi--display-summary + summary + (kagi--summary-buffer-name (kagi--get-domain-name url)))))) (provide 'kagi) From af326af4f60b973b1fa939d41f1a58fe54e9397a Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Fri, 5 Jan 2024 20:50:45 +0100 Subject: [PATCH 02/17] Remove stray (message) call --- kagi.el | 1 - 1 file changed, 1 deletion(-) diff --git a/kagi.el b/kagi.el index 9874297..d738e17 100644 --- a/kagi.el +++ b/kagi.el @@ -393,7 +393,6 @@ types are supported: - YouTube URLs - Scanned PDFs and images (OCR)" (interactive "sURL: \np") - (message "%"d) (let ((summary (kagi-summarize url))) (if (eql insert 4) (kagi--insert-summary summary) From 919de252c5b426f04dcee2046ca53e815f002de3 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Fri, 5 Jan 2024 21:03:39 +0100 Subject: [PATCH 03/17] Interpret the prefix argument differently --- kagi.el | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/kagi.el b/kagi.el index d738e17..d3a98ac 100644 --- a/kagi.el +++ b/kagi.el @@ -375,12 +375,13 @@ Shows the summary in a new window." (kagi--summary-buffer-name (buffer-name)))) ;;;###autoload -(defun kagi-summarize-url (url &optional insert) +(defun kagi-summarize-url (url &optional prefix) "Show the summary of the content behind the given URL. -By default, the summary is shown in a new buffer. With prefix -argument INSERT, insert the summary at point in the current -buffer. +By default, the summary is shown in a new buffer. With a single +PREFIX argument, insert the summary at point in the current +buffer. In case the current buffer is read-only, the summary will +be shown in a separate buffer anyway. According to the Kagi API documentation, the following media types are supported: @@ -392,9 +393,9 @@ types are supported: - Audio files (mp3/wav) - YouTube URLs - Scanned PDFs and images (OCR)" - (interactive "sURL: \np") + (interactive "sURL: \nP") (let ((summary (kagi-summarize url))) - (if (eql insert 4) + (if (and prefix (not buffer-read-only)) (kagi--insert-summary summary) (kagi--display-summary summary From be2fe9dd3a2d2cd9c45531c300973523793fc905 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Fri, 5 Jan 2024 21:47:47 +0100 Subject: [PATCH 04/17] Move out the languages or the engines outside the (defcustom) clause --- kagi.el | 82 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/kagi.el b/kagi.el index d3a98ac..84988dd 100644 --- a/kagi.el +++ b/kagi.el @@ -73,6 +73,14 @@ https://kagi.com/settings?p=api" :type '(choice string function) :group 'kagi) +(defvar kagi--summarizer-engines '("agnes" + "cecil" + "daphne" + "muriel") + "List of Kagi Summarizer engines. + +See `kagi-summarizer-engine' for a brief description per engine.") + (defcustom kagi-summarizer-engine "cecil" "Which summary engine to use. @@ -84,46 +92,48 @@ https://kagi.com/settings?p=api" Note that the muriel model is enterprise grade and has different pricing. Refer to the API documentation for more info at https://help.kagi.com/kagi/api/summarizer.html." - :type '(choice - (const "agnes") - (const "cecil") - (const "daphne") - (const "muriel")) + :type (append '(choice) + (mapcar (lambda (engine) `(const ,engine)) + kagi--summarizer-engines)) :group 'kagi) +(defvar kagi--summarizer-languages '(("Document language" . nil) + ("Bulgarian" . "BG") + ("Czech" . "CZ") + ("Danish" . "DA") + ("German" . "DE") + ("Greek" . "EL") + ("English" . "EN") + ("Spanish" . "ES") + ("Estonian" . "ET") + ("Finnish" . "FI") + ("French" . "FR") + ("Hungarian" . "HU") + ("Indonesian" . "ID") + ("Italian" . "IT") + ("Japanese" . "JA") + ("Korean" . "KA") + ("Lithuanian" . "LT") + ("Latvian" . "LV") + ("Norwegian" . "NB") + ("Dutch" . "NL") + ("Polish" . "PL") + ("Portuguese" . "PT") + ("Romanian" . "RO") + ("Russian" . "RU") + ("Slovak" . "SK") + ("Slovenian" . "SL") + ("Swedish" . "SV") + ("Turkish" . "TR") + ("Ukrainian" . "UK") + ("Chinese (simplified)" . "ZH")) + "Supported languages by the Kagi Universal Summarizer.") + (defcustom kagi-summarize-default-language nil "Default target language of the summary." - :type '(choice - (const :tag "Document language" nil) - (const :tag "Bulgarian" "BG") - (const :tag "Czech" "CZ") - (const :tag "Danish" "DA") - (const :tag "German" "DE") - (const :tag "Greek" "EL") - (const :tag "English" "EN") - (const :tag "Spanish" "ES") - (const :tag "Estonian" "ET") - (const :tag "Finnish" "FI") - (const :tag "French" "FR") - (const :tag "Hungarian" "HU") - (const :tag "Indonesian" "ID") - (const :tag "Italian" "IT") - (const :tag "Japanese" "JA") - (const :tag "Korean" "KO") - (const :tag "Lithuanian" "LT") - (const :tag "Latvian" "LV") - (const :tag "Norwegian" "NB") - (const :tag "Dutch" "NL") - (const :tag "Polish" "PL") - (const :tag "Portuguese" "PT") - (const :tag "Romanian" "RO") - (const :tag "Russian" "RU") - (const :tag "Slovak" "SK") - (const :tag "Slovenian" "SL") - (const :tag "Swedish" "SV") - (const :tag "Turkish" "TR") - (const :tag "Ukrainian" "UK") - (const :tag "Chinese (simplified)" "ZH")) + :type (append '(choice) + (mapcar (lambda (lang) `(const :tag ,(car lang) ,(cdr lang))) + kagi--summarizer-languages)) :group 'kagi) (defcustom kagi-summarizer-cache t From 79455947672b4ea6940a601b8d7fe5ec35774e46 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Fri, 5 Jan 2024 22:32:42 +0100 Subject: [PATCH 05/17] Rename variable for consistence --- kagi.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kagi.el b/kagi.el index 84988dd..97f3c5c 100644 --- a/kagi.el +++ b/kagi.el @@ -129,7 +129,7 @@ https://help.kagi.com/kagi/api/summarizer.html." ("Chinese (simplified)" . "ZH")) "Supported languages by the Kagi Universal Summarizer.") -(defcustom kagi-summarize-default-language nil +(defcustom kagi-summarizer-default-language nil "Default target language of the summary." :type (append '(choice) (mapcar (lambda (lang) `(const :tag ,(car lang) ,(cdr lang))) From 4524ed72a22d4458b9744041e333daedc0041198 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Fri, 5 Jan 2024 22:48:46 +0100 Subject: [PATCH 06/17] Allow to change the language and the summarizer engine interactively To be extended to the other interactive functions. --- kagi.el | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/kagi.el b/kagi.el index 97f3c5c..236e1e1 100644 --- a/kagi.el +++ b/kagi.el @@ -385,7 +385,7 @@ Shows the summary in a new window." (kagi--summary-buffer-name (buffer-name)))) ;;;###autoload -(defun kagi-summarize-url (url &optional prefix) +(defun kagi-summarize-url (url &optional insert language engine) "Show the summary of the content behind the given URL. By default, the summary is shown in a new buffer. With a single @@ -403,9 +403,20 @@ types are supported: - Audio files (mp3/wav) - YouTube URLs - Scanned PDFs and images (OCR)" - (interactive "sURL: \nP") + (interactive + (list + (read-string (format-prompt "URL" "")) + (or (equal current-prefix-arg '(4)) + (and (equal current-prefix-arg '(16)) (y-or-n-p "Insert summary at point?"))) + (when (equal current-prefix-arg '(16)) + (completing-read (format-prompt "Output language" "") + kagi--summarizer-languages nil t)) + (when (equal current-prefix-arg '(16)) + (completing-read (format-prompt "Engine" "") + kagi--summarizer-engines nil t kagi-summarizer-engine)))) + (let ((summary (kagi-summarize url))) - (if (and prefix (not buffer-read-only)) + (if (and insert (not buffer-read-only)) (kagi--insert-summary summary) (kagi--display-summary summary From 71a626e69185cd2e62df5c08f9986f91acbc04a7 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Fri, 5 Jan 2024 23:04:52 +0100 Subject: [PATCH 07/17] Add language codes to each car of the language list --- kagi.el | 58 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/kagi.el b/kagi.el index 236e1e1..e2befef 100644 --- a/kagi.el +++ b/kagi.el @@ -98,35 +98,35 @@ https://help.kagi.com/kagi/api/summarizer.html." :group 'kagi) (defvar kagi--summarizer-languages '(("Document language" . nil) - ("Bulgarian" . "BG") - ("Czech" . "CZ") - ("Danish" . "DA") - ("German" . "DE") - ("Greek" . "EL") - ("English" . "EN") - ("Spanish" . "ES") - ("Estonian" . "ET") - ("Finnish" . "FI") - ("French" . "FR") - ("Hungarian" . "HU") - ("Indonesian" . "ID") - ("Italian" . "IT") - ("Japanese" . "JA") - ("Korean" . "KA") - ("Lithuanian" . "LT") - ("Latvian" . "LV") - ("Norwegian" . "NB") - ("Dutch" . "NL") - ("Polish" . "PL") - ("Portuguese" . "PT") - ("Romanian" . "RO") - ("Russian" . "RU") - ("Slovak" . "SK") - ("Slovenian" . "SL") - ("Swedish" . "SV") - ("Turkish" . "TR") - ("Ukrainian" . "UK") - ("Chinese (simplified)" . "ZH")) + ("Bulgarian [BG]" . "BG") + ("Czech [CZ]" . "CZ") + ("Danish [DA]" . "DA") + ("German [DE]" . "DE") + ("Greek [EL]" . "EL") + ("English [EN]" . "EN") + ("Spanish [ES]" . "ES") + ("Estonian [ET]" . "ET") + ("Finnish [FI]" . "FI") + ("French [FR]" . "FR") + ("Hungarian [HU]" . "HU") + ("Indonesian [ID]" . "ID") + ("Italian [IT]" . "IT") + ("Japanese [JA]" . "JA") + ("Korean [KO]" . "KA") + ("Lithuanian [LT]" . "LT") + ("Latvian [LV]" . "LV") + ("Norwegian [NB]" . "NB") + ("Dutch [NL]" . "NL") + ("Polish [PL]" . "PL") + ("Portuguese [PT]" . "PT") + ("Romanian [RO]" . "RO") + ("Russian [RU]" . "RU") + ("Slovak [SK]" . "SK") + ("Slovenian [SL]" . "SL") + ("Swedish [SV]" . "SV") + ("Turkish [TR]" . "TR") + ("Ukrainian [UK]" . "UK") + ("Chinese (simplified) [ZH]" . "ZH")) "Supported languages by the Kagi Universal Summarizer.") (defcustom kagi-summarizer-default-language nil From 4e70777b11f6d7ef0d50d72af074430ece7f8bc7 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Fri, 5 Jan 2024 23:50:13 +0100 Subject: [PATCH 08/17] Put engine descriptions in the customize type specification --- kagi.el | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/kagi.el b/kagi.el index e2befef..85ddbe6 100644 --- a/kagi.el +++ b/kagi.el @@ -73,10 +73,11 @@ https://kagi.com/settings?p=api" :type '(choice string function) :group 'kagi) -(defvar kagi--summarizer-engines '("agnes" - "cecil" - "daphne" - "muriel") +(defvar kagi--summarizer-engines + '(("agnes" . "Friendly, descriptive, fast summary.") + ("cecil" . "Formal, technical, analytical summary.") + ("daphne" . "Informal, creative, friendly summary.") + ("muriel" . "Best-in-class summary using Kagi's enterprise-grade model (at different pricing).")) "List of Kagi Summarizer engines. See `kagi-summarizer-engine' for a brief description per engine.") @@ -84,16 +85,11 @@ See `kagi-summarizer-engine' for a brief description per engine.") (defcustom kagi-summarizer-engine "cecil" "Which summary engine to use. -- cicil :: Friendly, descriptive, fast summary. -- agnes :: Formal, technical, analytical summary. -- daphne :: Informal, creative, friendly summary. -- muriel :: Best-in-class summary using our enterprise-grade model. - Note that the muriel model is enterprise grade and has different pricing. Refer to the API documentation for more info at https://help.kagi.com/kagi/api/summarizer.html." :type (append '(choice) - (mapcar (lambda (engine) `(const ,engine)) + (mapcar (lambda (engine) `(const :doc ,(cdr engine) ,(car engine))) kagi--summarizer-engines)) :group 'kagi) From 79fcc316f0c874614b4037261708c0aa2cba5ad3 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Sun, 7 Jan 2024 22:35:28 +0100 Subject: [PATCH 09/17] Fix formatting --- kagi.el | 58 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/kagi.el b/kagi.el index 85ddbe6..47aaa9d 100644 --- a/kagi.el +++ b/kagi.el @@ -94,35 +94,35 @@ https://help.kagi.com/kagi/api/summarizer.html." :group 'kagi) (defvar kagi--summarizer-languages '(("Document language" . nil) - ("Bulgarian [BG]" . "BG") - ("Czech [CZ]" . "CZ") - ("Danish [DA]" . "DA") - ("German [DE]" . "DE") - ("Greek [EL]" . "EL") - ("English [EN]" . "EN") - ("Spanish [ES]" . "ES") - ("Estonian [ET]" . "ET") - ("Finnish [FI]" . "FI") - ("French [FR]" . "FR") - ("Hungarian [HU]" . "HU") - ("Indonesian [ID]" . "ID") - ("Italian [IT]" . "IT") - ("Japanese [JA]" . "JA") - ("Korean [KO]" . "KA") - ("Lithuanian [LT]" . "LT") - ("Latvian [LV]" . "LV") - ("Norwegian [NB]" . "NB") - ("Dutch [NL]" . "NL") - ("Polish [PL]" . "PL") - ("Portuguese [PT]" . "PT") - ("Romanian [RO]" . "RO") - ("Russian [RU]" . "RU") - ("Slovak [SK]" . "SK") - ("Slovenian [SL]" . "SL") - ("Swedish [SV]" . "SV") - ("Turkish [TR]" . "TR") - ("Ukrainian [UK]" . "UK") - ("Chinese (simplified) [ZH]" . "ZH")) + ("Bulgarian [BG]" . "BG") + ("Czech [CZ]" . "CZ") + ("Danish [DA]" . "DA") + ("German [DE]" . "DE") + ("Greek [EL]" . "EL") + ("English [EN]" . "EN") + ("Spanish [ES]" . "ES") + ("Estonian [ET]" . "ET") + ("Finnish [FI]" . "FI") + ("French [FR]" . "FR") + ("Hungarian [HU]" . "HU") + ("Indonesian [ID]" . "ID") + ("Italian [IT]" . "IT") + ("Japanese [JA]" . "JA") + ("Korean [KO]" . "KA") + ("Lithuanian [LT]" . "LT") + ("Latvian [LV]" . "LV") + ("Norwegian [NB]" . "NB") + ("Dutch [NL]" . "NL") + ("Polish [PL]" . "PL") + ("Portuguese [PT]" . "PT") + ("Romanian [RO]" . "RO") + ("Russian [RU]" . "RU") + ("Slovak [SK]" . "SK") + ("Slovenian [SL]" . "SL") + ("Swedish [SV]" . "SV") + ("Turkish [TR]" . "TR") + ("Ukrainian [UK]" . "UK") + ("Chinese (simplified) [ZH]" . "ZH")) "Supported languages by the Kagi Universal Summarizer.") (defcustom kagi-summarizer-default-language nil From f5316c1c4efe95ab504dc157accc157f517fc499 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Sat, 13 Jan 2024 21:01:24 +0100 Subject: [PATCH 10/17] Allow the user to select target language and engine, ask for insertion With a prefix argument, kagi.el will ask the possibilities one by one. --- kagi.el | 77 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/kagi.el b/kagi.el index e01361c..582e17c 100644 --- a/kagi.el +++ b/kagi.el @@ -408,37 +408,63 @@ Returns a formatted string to be displayed by the shell." (string-match-p (rx (seq bos "http" (? "s") "://" (+ (not space)) eos)) s)) ;;;###autoload -(defun kagi-summarize (text-or-url) +(defun kagi-summarize (text-or-url &optional language engine) "Return the summary of the given TEXT-OR-URL." - (if-let* ((response (if (kagi--url-p text-or-url) - (kagi--call-url-summarizer text-or-url) - (kagi--call-text-summarizer text-or-url))) - (parsed-response (json-parse-string response)) - (output (kagi--gethash parsed-response "data" "output"))) - (kagi--format-output output) - (if-let ((firsterror (aref (kagi--gethash parsed-response "error") 0))) - (error (format "%s (%s)" - (gethash "msg" firsterror) - (gethash "code" firsterror))) - (error "An error occurred while requesting a summary")))) + + (let* ((kagi-summarizer-default-language + (or language kagi-summarizer-default-language)) + (kagi-summarizer-engine + (or engine kagi-summarizer-engine))) + (if-let* ((response (if (kagi--url-p text-or-url) + (kagi--call-url-summarizer text-or-url) + (kagi--call-text-summarizer text-or-url))) + (parsed-response (json-parse-string response)) + (output (kagi--gethash parsed-response "data" "output"))) + (kagi--format-output output) + (if-let ((firsterror (aref (kagi--gethash parsed-response "error") 0))) + (error (format "%s (%s)" + (gethash "msg" firsterror) + (gethash "code" firsterror))) + (error "An error occurred while requesting a summary"))))) ;;;###autoload -(defun kagi-summarize-buffer (buffer) +(defun kagi-summarize-buffer (buffer &optional insert language engine) "Summarize the BUFFER's content and show it in a new window." - (interactive "b") - (with-current-buffer buffer - (kagi--display-summary - (kagi-summarize (buffer-string)) - (kagi--summary-buffer-name (buffer-name))))) + (interactive (list + (read-buffer (format-prompt "Buffer" "") nil t) + (and (equal current-prefix-arg '(4)) (y-or-n-p "Insert summary at point?")) + (when (equal current-prefix-arg '(4)) + (completing-read (format-prompt "Output language" "") + kagi--summarizer-languages nil t)) + (when (equal current-prefix-arg '(4)) + (completing-read (format-prompt "Engine" "") + kagi--summarizer-engines nil t kagi-summarizer-engine)))) + (let ((summary (with-current-buffer buffer + (kagi-summarize (buffer-string) language engine))) + (summary-buffer-name (with-current-buffer buffer + (kagi--summary-buffer-name (buffer-name))))) + (if (and insert (not buffer-read-only)) + (kagi--insert-summary summary) + (kagi--display-summary summary summary-buffer-name)))) ;;;###autoload -(defun kagi-summarize-region (begin end) +(defun kagi-summarize-region (begin end &optional language engine) "Summarize the region's content marked by BEGIN and END positions. Shows the summary in a new window." - (interactive "r") + (interactive (list + (region-beginning) + (region-end) + (when (equal current-prefix-arg '(4)) + (completing-read (format-prompt "Output language" "") + kagi--summarizer-languages nil t)) + (when (equal current-prefix-arg '(4)) + (completing-read (format-prompt "Engine" "") + kagi--summarizer-engines nil t kagi-summarizer-engine)))) (kagi--display-summary - (kagi-summarize (buffer-substring-no-properties begin end)) + (kagi-summarize (buffer-substring-no-properties begin end) + language + engine) (kagi--summary-buffer-name (buffer-name)))) ;;;###autoload @@ -463,16 +489,15 @@ types are supported: (interactive (list (read-string (format-prompt "URL" "")) - (or (equal current-prefix-arg '(4)) - (and (equal current-prefix-arg '(16)) (y-or-n-p "Insert summary at point?"))) - (when (equal current-prefix-arg '(16)) + (and (equal current-prefix-arg '(4)) (y-or-n-p "Insert summary at point?")) + (when (equal current-prefix-arg '(4)) (completing-read (format-prompt "Output language" "") kagi--summarizer-languages nil t)) - (when (equal current-prefix-arg '(16)) + (when (equal current-prefix-arg '(4)) (completing-read (format-prompt "Engine" "") kagi--summarizer-engines nil t kagi-summarizer-engine)))) - (let ((summary (kagi-summarize url))) + (let ((summary (kagi-summarize url language engine))) (if (and insert (not buffer-read-only)) (kagi--insert-summary summary) (kagi--display-summary From f519f245d3ba8e23a9f558b0439c7c7de6820b39 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Sun, 14 Jan 2024 08:29:08 +0100 Subject: [PATCH 11/17] Convert arguments to lowercase --- kagi.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kagi.el b/kagi.el index 582e17c..00dedcf 100644 --- a/kagi.el +++ b/kagi.el @@ -412,9 +412,9 @@ Returns a formatted string to be displayed by the shell." "Return the summary of the given TEXT-OR-URL." (let* ((kagi-summarizer-default-language - (or language kagi-summarizer-default-language)) + (downcase (or language kagi-summarizer-default-language))) (kagi-summarizer-engine - (or engine kagi-summarizer-engine))) + (downcase (or engine kagi-summarizer-engine)))) (if-let* ((response (if (kagi--url-p text-or-url) (kagi--call-url-summarizer text-or-url) (kagi--call-text-summarizer text-or-url))) From 204e1b254a547337a2044599836a7abefd7709a6 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Sun, 14 Jan 2024 09:11:09 +0100 Subject: [PATCH 12/17] Look up correct language code from kagi--summarizer-languages --- kagi.el | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/kagi.el b/kagi.el index 00dedcf..84dcc24 100644 --- a/kagi.el +++ b/kagi.el @@ -409,7 +409,14 @@ Returns a formatted string to be displayed by the shell." ;;;###autoload (defun kagi-summarize (text-or-url &optional language engine) - "Return the summary of the given TEXT-OR-URL." + "Return the summary of the given TEXT-OR-URL. + +LANGUAGE is a supported two letter abbreviation of the language, +as defined in `kagi--summarizer-languages'. When nil, the target +is automatically determined. + +ENGINE is the name of a supported summarizer engine, as +defined in `kagi--summarizer-engines'." (let* ((kagi-summarizer-default-language (downcase (or language kagi-summarizer-default-language))) @@ -434,8 +441,13 @@ Returns a formatted string to be displayed by the shell." (read-buffer (format-prompt "Buffer" "") nil t) (and (equal current-prefix-arg '(4)) (y-or-n-p "Insert summary at point?")) (when (equal current-prefix-arg '(4)) - (completing-read (format-prompt "Output language" "") - kagi--summarizer-languages nil t)) + (alist-get + (completing-read (format-prompt "Output language" "") + kagi--summarizer-languages nil t) + kagi--summarizer-languages + (or kagi-summarizer-default-language "EN") + nil + #'string=)) (when (equal current-prefix-arg '(4)) (completing-read (format-prompt "Engine" "") kagi--summarizer-engines nil t kagi-summarizer-engine)))) From 796018bc23977c1d1f5ce9569764bbbf3502f90b Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Sun, 14 Jan 2024 09:36:59 +0100 Subject: [PATCH 13/17] Add universal prefix support for the summarizer commands These prompt the user whether the summary should be inserted at point, which language to use and which summarizer engine to use. --- kagi.el | 114 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 37 deletions(-) diff --git a/kagi.el b/kagi.el index 84dcc24..01179f3 100644 --- a/kagi.el +++ b/kagi.el @@ -434,23 +434,53 @@ defined in `kagi--summarizer-engines'." (gethash "code" firsterror))) (error "An error occurred while requesting a summary"))))) +(defun kagi--get-summarizer-parameters (&optional prompt-insert-p) + "Return a list of interactively obtained summarizer parameters. + +Not all commands need to insert a summary, so only prompt for +this when PROMPT-INSERT-P is non-nil." + (append + (list + (and prompt-insert-p + (equal current-prefix-arg '(4)) + (y-or-n-p "Insert summary at point?"))) + (list + (when (equal current-prefix-arg '(4)) + (alist-get + (completing-read (format-prompt "Output language" "") + kagi--summarizer-languages nil t) + kagi--summarizer-languages + (or kagi-summarizer-default-language "EN") + nil + #'string=))) + (list + (when (equal current-prefix-arg '(4)) + (completing-read (format-prompt "Engine" "") + kagi--summarizer-engines nil t kagi-summarizer-engine))))) + ;;;###autoload (defun kagi-summarize-buffer (buffer &optional insert language engine) - "Summarize the BUFFER's content and show it in a new window." - (interactive (list + "Summarize the BUFFER's content and show it in a new window. + +By default, the summary is shown in a new buffer. + +When INSERT is non-nil, the summary will be inserted at point. In +case the current buffer is read-only, the summary will be shown +in a separate buffer anyway. + +LANGUAGE is a supported two letter abbreviation of the language, +as defined in `kagi--summarizer-languages'. When nil, the target +is automatically determined. + +ENGINE is the name of a supported summarizer engine, as +defined in `kagi--summarizer-engines'. + +With a single universal prefix argument (`C-u'), the user is +prompted whether the summary has to be inserted at point, which +target LANGUAGE to use and which summarizer ENGINE to use." + (interactive (cons (read-buffer (format-prompt "Buffer" "") nil t) - (and (equal current-prefix-arg '(4)) (y-or-n-p "Insert summary at point?")) - (when (equal current-prefix-arg '(4)) - (alist-get - (completing-read (format-prompt "Output language" "") - kagi--summarizer-languages nil t) - kagi--summarizer-languages - (or kagi-summarizer-default-language "EN") - nil - #'string=)) - (when (equal current-prefix-arg '(4)) - (completing-read (format-prompt "Engine" "") - kagi--summarizer-engines nil t kagi-summarizer-engine)))) + (kagi--get-summarizer-parameters t))) (let ((summary (with-current-buffer buffer (kagi-summarize (buffer-string) language engine))) (summary-buffer-name (with-current-buffer buffer @@ -463,16 +493,21 @@ defined in `kagi--summarizer-engines'." (defun kagi-summarize-region (begin end &optional language engine) "Summarize the region's content marked by BEGIN and END positions. -Shows the summary in a new window." - (interactive (list - (region-beginning) - (region-end) - (when (equal current-prefix-arg '(4)) - (completing-read (format-prompt "Output language" "") - kagi--summarizer-languages nil t)) - (when (equal current-prefix-arg '(4)) - (completing-read (format-prompt "Engine" "") - kagi--summarizer-engines nil t kagi-summarizer-engine)))) +The summary is always shown in a new buffer. + +LANGUAGE is a supported two letter abbreviation of the language, +as defined in `kagi--summarizer-languages'. When nil, the target +is automatically determined. + +ENGINE is the name of a supported summarizer engine, as +defined in `kagi--summarizer-engines'. + +With a single universal prefix argument (`C-u'), the user is +prompted for which target LANGUAGE to use and which summarizer +ENGINE to use." + (interactive (append + (list (region-beginning) (region-end)) + (kagi--get-summarizer-parameters))) (kagi--display-summary (kagi-summarize (buffer-substring-no-properties begin end) language @@ -483,10 +518,22 @@ Shows the summary in a new window." (defun kagi-summarize-url (url &optional insert language engine) "Show the summary of the content behind the given URL. -By default, the summary is shown in a new buffer. With a single -PREFIX argument, insert the summary at point in the current -buffer. In case the current buffer is read-only, the summary will -be shown in a separate buffer anyway. +By default, the summary is shown in a new buffer. + +When INSERT is non-nil, the summary will be inserted at point. In +case the current buffer is read-only, the summary will be shown +in a separate buffer anyway. + +LANGUAGE is a supported two letter abbreviation of the language, +as defined in `kagi--summarizer-languages'. When nil, the target +is automatically determined. + +ENGINE is the name of a supported summarizer engine, as +defined in `kagi--summarizer-engines'. + +With a single universal prefix argument (`C-u'), the user is +prompted whether the summary has to be inserted at point, which +target LANGUAGE to use and which summarizer ENGINE to use. According to the Kagi API documentation, the following media types are supported: @@ -499,16 +546,9 @@ types are supported: - YouTube URLs - Scanned PDFs and images (OCR)" (interactive - (list + (cons (read-string (format-prompt "URL" "")) - (and (equal current-prefix-arg '(4)) (y-or-n-p "Insert summary at point?")) - (when (equal current-prefix-arg '(4)) - (completing-read (format-prompt "Output language" "") - kagi--summarizer-languages nil t)) - (when (equal current-prefix-arg '(4)) - (completing-read (format-prompt "Engine" "") - kagi--summarizer-engines nil t kagi-summarizer-engine)))) - + (kagi--get-summarizer-parameters t))) (let ((summary (kagi-summarize url language engine))) (if (and insert (not buffer-read-only)) (kagi--insert-summary summary) From 113f98500da3c32656da41b43bbb71c7904cb9f1 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Sun, 14 Jan 2024 09:43:32 +0100 Subject: [PATCH 14/17] Remove redundant newline --- kagi.el | 1 - 1 file changed, 1 deletion(-) diff --git a/kagi.el b/kagi.el index 01179f3..d3a3ec0 100644 --- a/kagi.el +++ b/kagi.el @@ -313,7 +313,6 @@ list of conses." (when kagi-summarizer-default-language `(("target_language" . ,kagi-summarizer-default-language))))) - (defconst kagi--summarizer-min-input-words 50 "The minimal amount of words that the text input should have.") From ad737301a339f25a86b837a32dde2554343a032f Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Sun, 14 Jan 2024 09:47:09 +0100 Subject: [PATCH 15/17] Language codes should be upcased instead of downcased --- kagi.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kagi.el b/kagi.el index d3a3ec0..ee2957a 100644 --- a/kagi.el +++ b/kagi.el @@ -418,7 +418,7 @@ ENGINE is the name of a supported summarizer engine, as defined in `kagi--summarizer-engines'." (let* ((kagi-summarizer-default-language - (downcase (or language kagi-summarizer-default-language))) + (upcase (or language kagi-summarizer-default-language))) (kagi-summarizer-engine (downcase (or engine kagi-summarizer-engine)))) (if-let* ((response (if (kagi--url-p text-or-url) From 38c68d96fcba282bf9e81ac94c147bc6c07ea105 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Sun, 14 Jan 2024 10:05:07 +0100 Subject: [PATCH 16/17] Update README with new summary --- README.org | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/README.org b/README.org index a9bf827..04ad941 100644 --- a/README.org +++ b/README.org @@ -25,11 +25,16 @@ The FastGPT functionality has only one command: =kagi-fastgpt-shell=. This opens ** Universal Summarizer -- =kagi-summarize-buffer= :: Summarizes the content of a buffer and displays it in a separate buffer. -- =kagi-summarize-region= :: Similarly, the text inside the region is summarized and shown in a separate buffer. +- =kagi-summarize-buffer= :: Summarizes the content of a buffer. +- =kagi-summarize-region= :: Similarly, the text inside the region is summarized. - =kagi-summarize-url= :: Prompts for a URL of which a summary is composed and displayed. - =kagi-summarize= :: Function to retrieve a summary from a text or URL, to be used from Lisp code. +The summarize commands accept a single universal prefix, which allows you to: +- insert the summary at point; +- choose a (different) target language +- choose which summary engine to use + Note that texts submitted to Kagi are subject to their [[https://kagi.com/privacy#Summarizer][Privacy Policy]]. * Installation and configuration @@ -114,6 +119,8 @@ The code to generate the table of configuration items was inspired by an idea of ** Examples of custom functions +The =kagi-summarize= function allows you to summarize texts or URLs from Emacs Lisp. + By overriding a variable with a =let= construct you can (temporarily) deviate from the default / configured value. A few examples are shown below: *** Language override @@ -127,6 +134,8 @@ To obtain a Dutch summary of a video you may want to define the following functi (kagi-summarize text-or-url))) #+end_src +Note that, when you invoke the summarizer functionality interactively, you can also temporarily choose a different target language with the universal prefix (=C-u=) on one of the =kagi-summarize-*= commands. + *** Caching override The [[https://help.kagi.com/kagi/api/summarizer.html][Summarizer API]] comes with the following note: @@ -180,6 +189,16 @@ If you recognize this confusion, you may want to add the following line to your * Changelog +** 0.3pre + +*** New + +The summarizer commands =kagi-summarize-*= now accept a universal prefix. This allows you to: + +- insert the summary at point (instead of a separate buffer) +- choose a different target language +- choose a different summarizer engine + ** 0.2pre *** Breaking changes From 3a5d8e2181609f25c37b46e4f0fb1f1156005a36 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Sun, 14 Jan 2024 10:11:45 +0100 Subject: [PATCH 17/17] Minor formatting --- README.org | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.org b/README.org index 04ad941..0677108 100644 --- a/README.org +++ b/README.org @@ -32,8 +32,8 @@ The FastGPT functionality has only one command: =kagi-fastgpt-shell=. This opens The summarize commands accept a single universal prefix, which allows you to: - insert the summary at point; -- choose a (different) target language -- choose which summary engine to use +- choose a (different) target language; +- choose which summary engine to use. Note that texts submitted to Kagi are subject to their [[https://kagi.com/privacy#Summarizer][Privacy Policy]]. @@ -195,9 +195,9 @@ If you recognize this confusion, you may want to add the following line to your The summarizer commands =kagi-summarize-*= now accept a universal prefix. This allows you to: -- insert the summary at point (instead of a separate buffer) -- choose a different target language -- choose a different summarizer engine +- insert the summary at point (instead of a separate buffer); +- choose a different target language; +- choose a different summarizer engine. ** 0.2pre