diff --git a/public/scripts/extensions/caption/index.js b/public/scripts/extensions/caption/index.js index 9de9b6d87..0f357605c 100644 --- a/public/scripts/extensions/caption/index.js +++ b/public/scripts/extensions/caption/index.js @@ -257,6 +257,19 @@ async function onSelectImage(e, prompt, quiet) { return ''; } + const caption = await getCaptionForFile(file, prompt, quiet); + form && form.reset(); + return caption; +} + +/** + * Gets a caption for an image file. + * @param {File} file Input file + * @param {string} prompt Caption prompt + * @param {boolean} quiet Suppresses sending a message + * @returns {Promise} Generated caption + */ +async function getCaptionForFile(file, prompt, quiet) { try { setSpinnerIcon(); const context = getContext(); @@ -276,7 +289,6 @@ async function onSelectImage(e, prompt, quiet) { return ''; } finally { - form && form.reset(); setImageIcon(); } } @@ -291,9 +303,26 @@ function onRefineModeInput() { * @param {object} args Named parameters * @param {string} prompt Caption prompt */ -function captionCommandCallback(args, prompt) { +async function captionCommandCallback(args, prompt) { + const quiet = isTrueBoolean(args?.quiet); + const id = args?.id; + + if (!isNaN(Number(id))) { + const message = getContext().chat[id]; + if (message?.extra?.image) { + try { + const fetchResult = await fetch(message.extra.image); + const blob = await fetchResult.blob(); + const file = new File([blob], 'image.jpg', { type: blob.type }); + return await getCaptionForFile(file, prompt, quiet); + } catch (error) { + toastr.error('Failed to get image from the message. Make sure the image is accessible.'); + return ''; + } + } + } + return new Promise(resolve => { - const quiet = isTrueBoolean(args?.quiet); const input = document.createElement('input'); input.type = 'file'; input.accept = 'image/*'; @@ -502,6 +531,9 @@ jQuery(function () { new SlashCommandNamedArgument( 'quiet', 'suppress sending a captioned message', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false', ['true', 'false'], ), + new SlashCommandNamedArgument( + 'id', 'get image from a message with this ID', [ARGUMENT_TYPE.NUMBER], false, false, + ), ], unnamedArgumentList: [ new SlashCommandArgument( @@ -515,6 +547,9 @@ jQuery(function () {
Only multimodal sources support custom prompts.
+
+ Provide a message ID to get an image from a message instead of uploading one. +
Set the "quiet" argument to true to suppress sending a captioned message, default: false.