2024-02-19 12:56:28 -05:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2024-10-02 13:36:35 -04:00
|
|
|
import { extractError, popupAjaxError } from "discourse/lib/ajax-error";
|
2024-02-19 12:56:28 -05:00
|
|
|
import { apiInitializer } from "discourse/lib/api";
|
2024-10-21 17:48:10 -04:00
|
|
|
import {
|
|
|
|
getUploadMarkdown,
|
|
|
|
IMAGE_MARKDOWN_REGEX,
|
|
|
|
isImage,
|
|
|
|
} from "discourse/lib/uploads";
|
2024-02-19 12:56:28 -05:00
|
|
|
import I18n from "discourse-i18n";
|
|
|
|
|
|
|
|
export default apiInitializer("1.25.0", (api) => {
|
|
|
|
const buttonAttrs = {
|
|
|
|
label: I18n.t("discourse_ai.ai_helper.image_caption.button_label"),
|
|
|
|
icon: "discourse-sparkles",
|
|
|
|
class: "generate-caption",
|
|
|
|
};
|
2024-02-19 16:08:19 -05:00
|
|
|
const settings = api.container.lookup("service:site-settings");
|
2024-03-11 18:35:20 -04:00
|
|
|
const currentUser = api.getCurrentUser();
|
2024-02-19 12:56:28 -05:00
|
|
|
|
2024-03-11 18:35:20 -04:00
|
|
|
if (
|
|
|
|
!settings.ai_helper_enabled_features.includes("image_caption") ||
|
2024-03-12 04:40:30 -04:00
|
|
|
!currentUser?.can_use_assistant
|
2024-03-11 18:35:20 -04:00
|
|
|
) {
|
2024-02-19 16:08:19 -05:00
|
|
|
return;
|
|
|
|
}
|
2024-03-11 18:35:20 -04:00
|
|
|
|
2024-05-27 13:49:24 -04:00
|
|
|
api.addSaveableUserOptionField("auto_image_caption");
|
|
|
|
|
2024-02-19 12:56:28 -05:00
|
|
|
api.addComposerImageWrapperButton(
|
|
|
|
buttonAttrs.label,
|
|
|
|
buttonAttrs.class,
|
|
|
|
buttonAttrs.icon,
|
|
|
|
(event) => {
|
2024-02-28 16:32:45 -05:00
|
|
|
const imageCaptionPopup = api.container.lookup(
|
|
|
|
"service:imageCaptionPopup"
|
|
|
|
);
|
|
|
|
|
|
|
|
imageCaptionPopup.popupTrigger = event.target;
|
|
|
|
|
|
|
|
if (
|
|
|
|
imageCaptionPopup.popupTrigger.classList.contains("generate-caption")
|
|
|
|
) {
|
2024-02-19 12:56:28 -05:00
|
|
|
const buttonWrapper = event.target.closest(".button-wrapper");
|
|
|
|
const imageIndex = parseInt(
|
|
|
|
buttonWrapper.getAttribute("data-image-index"),
|
|
|
|
10
|
|
|
|
);
|
|
|
|
const imageSrc = event.target
|
|
|
|
.closest(".image-wrapper")
|
|
|
|
.querySelector("img")
|
|
|
|
.getAttribute("src");
|
|
|
|
|
2024-02-28 16:32:45 -05:00
|
|
|
imageCaptionPopup.toggleLoadingState(true);
|
2024-02-23 13:06:39 -05:00
|
|
|
|
2024-02-28 16:32:45 -05:00
|
|
|
const site = api.container.lookup("site:main");
|
2024-02-23 13:06:39 -05:00
|
|
|
if (!site.mobileView) {
|
|
|
|
imageCaptionPopup.showPopup = !imageCaptionPopup.showPopup;
|
|
|
|
}
|
2024-02-19 12:56:28 -05:00
|
|
|
|
2024-02-28 16:32:45 -05:00
|
|
|
imageCaptionPopup._request = ajax(
|
|
|
|
`/discourse-ai/ai-helper/caption_image`,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
data: {
|
|
|
|
image_url: imageSrc,
|
2024-05-27 13:49:24 -04:00
|
|
|
image_url_type: "long_url",
|
2024-02-28 16:32:45 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2024-02-22 12:31:25 -05:00
|
|
|
|
2024-02-28 16:32:45 -05:00
|
|
|
imageCaptionPopup._request
|
2024-02-19 12:56:28 -05:00
|
|
|
.then(({ caption }) => {
|
|
|
|
imageCaptionPopup.imageSrc = imageSrc;
|
|
|
|
imageCaptionPopup.imageIndex = imageIndex;
|
|
|
|
imageCaptionPopup.newCaption = caption;
|
2024-02-23 13:06:39 -05:00
|
|
|
|
|
|
|
if (site.mobileView) {
|
|
|
|
// Auto-saves caption on mobile view
|
2024-02-28 16:32:45 -05:00
|
|
|
imageCaptionPopup.updateCaption();
|
2024-02-23 13:06:39 -05:00
|
|
|
}
|
2024-02-19 12:56:28 -05:00
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => {
|
2024-02-28 16:32:45 -05:00
|
|
|
imageCaptionPopup.toggleLoadingState(false);
|
2024-02-19 12:56:28 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2024-05-27 13:49:24 -04:00
|
|
|
|
2024-08-14 14:26:56 -04:00
|
|
|
// Checks if image is small (≤ 0.1 MP)
|
2024-05-27 13:49:24 -04:00
|
|
|
function isSmallImage(width, height) {
|
|
|
|
const megapixels = (width * height) / 1000000;
|
2024-08-14 14:26:56 -04:00
|
|
|
return megapixels <= 0.1;
|
2024-05-27 13:49:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function needsImprovedCaption(caption) {
|
|
|
|
return caption.length < 20 || caption.split(" ").length === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUploadUrlFromMarkdown(markdown) {
|
|
|
|
const regex = /\(upload:\/\/([^)]+)\)/;
|
|
|
|
const match = markdown.match(regex);
|
|
|
|
return match ? `upload://${match[1]}` : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchImageCaption(imageUrl, urlType) {
|
|
|
|
try {
|
|
|
|
const response = await ajax(`/discourse-ai/ai-helper/caption_image`, {
|
|
|
|
method: "POST",
|
|
|
|
data: {
|
|
|
|
image_url: imageUrl,
|
|
|
|
image_url_type: urlType,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return response.caption;
|
|
|
|
} catch (error) {
|
2024-10-02 13:36:35 -04:00
|
|
|
toasts.error({
|
|
|
|
class: "ai-image-caption-error-toast",
|
|
|
|
duration: 3000,
|
|
|
|
data: {
|
|
|
|
message: extractError(error),
|
|
|
|
},
|
|
|
|
});
|
2024-05-27 13:49:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const autoCaptionAllowedGroups =
|
|
|
|
settings?.ai_auto_image_caption_allowed_groups
|
|
|
|
.split("|")
|
|
|
|
.map((id) => parseInt(id, 10));
|
|
|
|
const currentUserGroups = currentUser.groups.map((g) => g.id);
|
|
|
|
|
|
|
|
if (
|
|
|
|
!currentUserGroups.some((groupId) =>
|
|
|
|
autoCaptionAllowedGroups.includes(groupId)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-02 13:36:35 -04:00
|
|
|
const toasts = api.container.lookup("service:toasts");
|
2024-05-27 13:49:24 -04:00
|
|
|
// Automatically caption uploaded images
|
|
|
|
api.addComposerUploadMarkdownResolver(async (upload) => {
|
|
|
|
const autoCaptionEnabled = currentUser.get(
|
|
|
|
"user_option.auto_image_caption"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
!autoCaptionEnabled ||
|
|
|
|
!isImage(upload.url) ||
|
|
|
|
!needsImprovedCaption(upload.original_filename) ||
|
|
|
|
isSmallImage(upload.width, upload.height)
|
|
|
|
) {
|
|
|
|
return getUploadMarkdown(upload);
|
|
|
|
}
|
|
|
|
|
|
|
|
const caption = await fetchImageCaption(upload.url, "long_url");
|
2024-08-20 15:53:51 -04:00
|
|
|
if (!caption) {
|
|
|
|
return getUploadMarkdown(upload);
|
|
|
|
}
|
2024-05-27 13:49:24 -04:00
|
|
|
return `![${caption}|${upload.thumbnail_width}x${upload.thumbnail_height}](${upload.short_url})`;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Conditionally show dialog to auto image caption
|
|
|
|
api.composerBeforeSave(() => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const dialog = api.container.lookup("service:dialog");
|
|
|
|
const composer = api.container.lookup("service:composer");
|
|
|
|
const localePrefix =
|
|
|
|
"discourse_ai.ai_helper.image_caption.automatic_caption_dialog";
|
|
|
|
const autoCaptionEnabled = currentUser.get(
|
|
|
|
"user_option.auto_image_caption"
|
|
|
|
);
|
|
|
|
|
|
|
|
const imageUploads = composer.model.reply.match(IMAGE_MARKDOWN_REGEX);
|
|
|
|
const hasImageUploads = imageUploads?.length > 0;
|
2024-05-27 15:17:35 -04:00
|
|
|
|
|
|
|
if (!hasImageUploads) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
|
2024-05-27 13:49:24 -04:00
|
|
|
const imagesToCaption = imageUploads.filter((image) => {
|
|
|
|
const caption = image
|
|
|
|
.substring(image.indexOf("[") + 1, image.indexOf("]"))
|
|
|
|
.split("|")[0];
|
|
|
|
// We don't check if the image is small to show the prompt here
|
|
|
|
// because the width/height are the thumbnail sizes so the mp count
|
|
|
|
// is incorrect. It doesn't matter because the auto caption won't
|
|
|
|
// happen anyways if its small because that uses the actual upload dimensions
|
|
|
|
return needsImprovedCaption(caption);
|
|
|
|
});
|
|
|
|
|
|
|
|
const needsBetterCaptions = imagesToCaption?.length > 0;
|
|
|
|
|
|
|
|
const keyValueStore = api.container.lookup("service:key-value-store");
|
|
|
|
const imageCaptionPopup = api.container.lookup(
|
|
|
|
"service:imageCaptionPopup"
|
|
|
|
);
|
|
|
|
const autoCaptionPromptKey = "ai-auto-caption-seen";
|
|
|
|
const seenAutoCaptionPrompt = keyValueStore.getItem(autoCaptionPromptKey);
|
|
|
|
|
2024-05-27 15:17:35 -04:00
|
|
|
if (autoCaptionEnabled || !needsBetterCaptions || seenAutoCaptionPrompt) {
|
2024-05-27 13:49:24 -04:00
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
keyValueStore.setItem(autoCaptionPromptKey, true);
|
|
|
|
|
|
|
|
dialog.confirm({
|
|
|
|
message: I18n.t(`${localePrefix}.prompt`),
|
|
|
|
confirmButtonLabel: `${localePrefix}.confirm`,
|
|
|
|
cancelButtonLabel: `${localePrefix}.cancel`,
|
|
|
|
class: "ai-image-caption-prompt-dialog",
|
|
|
|
|
|
|
|
didConfirm: async () => {
|
|
|
|
try {
|
|
|
|
currentUser.set("user_option.auto_image_caption", true);
|
|
|
|
await currentUser.save(["auto_image_caption"]);
|
|
|
|
|
|
|
|
imagesToCaption.forEach(async (imageMarkdown) => {
|
|
|
|
const uploadUrl = getUploadUrlFromMarkdown(imageMarkdown);
|
|
|
|
imageCaptionPopup.showAutoCaptionLoader = true;
|
|
|
|
const caption = await fetchImageCaption(uploadUrl, "short_url");
|
|
|
|
|
|
|
|
// Find and replace the caption in the reply
|
|
|
|
const regex = new RegExp(
|
|
|
|
`(!\\[)[^|]+(\\|[^\\]]+\\]\\(${uploadUrl}\\))`
|
|
|
|
);
|
|
|
|
const newReply = composer.model.reply.replace(
|
|
|
|
regex,
|
|
|
|
`$1${caption}$2`
|
|
|
|
);
|
|
|
|
composer.model.set("reply", newReply);
|
|
|
|
imageCaptionPopup.showAutoCaptionLoader = false;
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
// Reject the promise if an error occurs
|
|
|
|
// Show an error saying unable to generate captions
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
didCancel: () => {
|
|
|
|
// Don't enable auto captions and continue with the save
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2024-02-19 12:56:28 -05:00
|
|
|
});
|