π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
fix(ensu): handle mixed attachments and image retrieval
setalp7af695be9aeb
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.ktindex fb40961c37..89cddf3df3 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.kt@@ -1860,6 +1860,15 @@ internal class ChatStoreActions(val builder = StringBuilder(text)val documents = attachments.filter { it.type == io.ente.ensu.chat.AttachmentType.Document }val images = attachments.filter { it.type == io.ente.ensu.chat.AttachmentType.Image }+ val imageFiles = images.mapNotNull { attachment ->+ val path = attachment.localPath ?: return@mapNotNull null+ java.io.File(path).takeIf { it.exists() }+ }++ if (imageFiles.isNotEmpty() && text.isBlank()) {+ builder.clear()+ builder.append("Describe the attached images.")+ }documents.forEachIndexed { index, attachment ->builder.append("\n\n----- BEGIN DOCUMENT: Document ${index + 1} -----\n")@@ -1867,16 +1876,7 @@ internal class ChatStoreActions(builder.append("----- END DOCUMENT: Document ${index + 1} -----")}- val imageFiles = images.mapNotNull { attachment ->- val path = attachment.localPath ?: return@mapNotNull null- java.io.File(path).takeIf { it.exists() }- }-if (imageFiles.isNotEmpty()) {- if (builder.isBlank()) {- builder.clear()- builder.append("Describe the attached images.")- }builder.append("\n\n[")builder.append(imageFiles.size)builder.append(" image attachment")diff --git a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swiftindex d438e318c0..a992d83679 100644--- a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift+++ b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift@@ -2529,6 +2529,11 @@ final class ChatViewModel: ObservableObject {var prompt = textlet documents = attachments.filter { $0.kind == .document }let images = attachments.filter { $0.kind == .image }+ let imageFiles = images.compactMap { $0.url }++ if !imageFiles.isEmpty && text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {+ prompt = "Describe the attached images."+ }for (index, attachment) in documents.enumerated() {prompt += "\n\n----- BEGIN DOCUMENT: Document \(index + 1) -----\n"@@ -2536,11 +2541,7 @@ final class ChatViewModel: ObservableObject {prompt += "----- END DOCUMENT: Document \(index + 1) -----"}- let imageFiles = images.compactMap { $0.url }if !imageFiles.isEmpty {- if prompt.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {- prompt = "Describe the attached images."- }let mediaMarker = "<__media__>"prompt += "\n\n[\(imageFiles.count) image attachment"if imageFiles.count > 1 { prompt += "s" }diff --git a/web/apps/ensu/src/pages/chat.tsx b/web/apps/ensu/src/pages/chat.tsxindex 10d18f20be..74e1747d95 100644--- a/web/apps/ensu/src/pages/chat.tsx+++ b/web/apps/ensu/src/pages/chat.tsx@@ -220,12 +220,17 @@ const IMAGE_SELECTOR_EXTENSIONS = [] as const;const MAX_IMAGE_ATTACHMENTS_PER_MESSAGE = 2;-const buildPromptWithImages = (text: string, imageCount: number) => {- if (imageCount <= 0) return text;+const buildPromptWithImages = (+ text: string,+ imageCount: number,+ documents: DocumentAttachment[],+) => {let prompt = text;- if (!prompt.trim()) {+ if (imageCount > 0 && !prompt.trim()) {prompt = "Describe the attached images.";}+ prompt = buildPromptWithDocuments(prompt, documents);+ if (imageCount <= 0) return prompt;prompt += `\n\n[${imageCount} image attachment${imageCount === 1 ? "" : "s"} provided]`;for (let index = 0; index < imageCount; index += 1) {prompt += `\n${MEDIA_MARKER}`;@@ -3188,7 +3193,7 @@ const Page: React.FC = () => {collection.status !== "error",).map((collection) => collection.id);- const knowledgeQuery = parseDocumentBlocks(promptText)+ const knowledgeQuery = parseDocumentBlocks(parentMessage.text).text.replaceAll(MEDIA_MARKER, "").replace(/\[\d+ image attachments? provided\]/gi, "").trim();@@ -4300,8 +4305,9 @@ const Page: React.FC = () => {await refreshSessions();}+ const userText = trimmed.replaceAll("\0", "");const messageText = buildPromptWithDocuments(- trimmed.replaceAll("\0", ""),+ userText,pendingDocuments,);const persistedAttachmentIds = new Set(@@ -4406,8 +4412,9 @@ const Page: React.FC = () => {}const promptText = buildPromptWithImages(- messageText,+ userText,inferenceImagePaths.length,+ pendingDocuments,);composerRef.current?.setText("");