From dc63ec78fd076742951b5928db3ac2090b92a476 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 14 May 2024 20:44:36 +0200 Subject: [PATCH] Refactoring --- .../email/FragmentDialogSummarize.java | 42 +------------------ .../main/java/eu/faircode/email/OpenAI.java | 39 +++++++++++++++++ 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogSummarize.java b/app/src/main/java/eu/faircode/email/FragmentDialogSummarize.java index 1285249199..0476c391fa 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogSummarize.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogSummarize.java @@ -106,7 +106,6 @@ public class FragmentDialogSummarize extends FragmentDialogBase { d = HtmlHelper.sanitizeView(context, d, false); HtmlHelper.removeSignatures(d); d.select("blockquote").remove(); - HtmlHelper.truncate(d, HtmlHelper.MAX_TRANSLATABLE_TEXT_SIZE); if (OpenAI.isAvailable(context)) { String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL); @@ -116,46 +115,9 @@ public class FragmentDialogSummarize extends FragmentDialogBase { List input = new ArrayList<>(); input.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{new OpenAI.Content(OpenAI.CONTENT_TEXT, prompt)})); - SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, d, null, null); - - DB db = DB.getInstance(context); - List contents = new ArrayList<>(); - int start = 0; - while (start < ssb.length()) { - int end = ssb.nextSpanTransition(start, ssb.length(), ImageSpanEx.class); - String text = ssb.subSequence(start, end).toString(); - Log.i("MMM " + start + "..." + end + " len=" + ssb.length() + " text=" + text.replace('\n', '|')); - contents.add(new OpenAI.Content(OpenAI.CONTENT_TEXT, text)); - if (end < ssb.length()) { - ImageSpanEx[] spans = ssb.getSpans(end, end, ImageSpanEx.class); - if (spans.length == 1) { - int s = ssb.getSpanStart(spans[0]); - int e = ssb.getSpanEnd(spans[0]); - String src = spans[0].getSource(); - - String url = null; - if (src.startsWith("cid:")) { - String cid = '<' + src.substring(4) + '>'; - EntityAttachment attachment = db.attachment().getAttachment(id, cid); - if (attachment != null && attachment.available) - try { - url = ImageHelper.getDataUri(attachment.getFile(context), attachment.type); - } catch (Throwable ex) { - Log.w(ex); - } - } else - url = src; - - Log.i("MMM image " + s + "..." + e + " url=" + url); - if (url != null) - contents.add(new OpenAI.Content(OpenAI.CONTENT_IMAGE, url)); - end = e; - } - } - start = end; - } - input.add(new OpenAI.Message(OpenAI.USER, contents.toArray(new OpenAI.Content[0]))); + SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, d, null, null); + input.add(new OpenAI.Message(OpenAI.USER, OpenAI.getContent(ssb, id, context))); OpenAI.Message[] result = OpenAI.completeChat(context, model, input.toArray(new OpenAI.Message[0]), temperature, 1); diff --git a/app/src/main/java/eu/faircode/email/OpenAI.java b/app/src/main/java/eu/faircode/email/OpenAI.java index e4c3cc11c8..095a447311 100644 --- a/app/src/main/java/eu/faircode/email/OpenAI.java +++ b/app/src/main/java/eu/faircode/email/OpenAI.java @@ -22,6 +22,7 @@ package eu.faircode.email; import android.content.Context; import android.content.SharedPreferences; import android.net.Uri; +import android.text.Spannable; import android.text.TextUtils; import androidx.annotation.NonNull; @@ -258,6 +259,44 @@ public class OpenAI { return sb.toString(); } + static Content[] getContent(Spannable ssb, long id, Context context) { + DB db = DB.getInstance(context); + List contents = new ArrayList<>(); + int start = 0; + while (start < ssb.length()) { + int end = ssb.nextSpanTransition(start, ssb.length(), ImageSpanEx.class); + String text = ssb.subSequence(start, end).toString(); + contents.add(new OpenAI.Content(OpenAI.CONTENT_TEXT, text)); + if (end < ssb.length()) { + ImageSpanEx[] spans = ssb.getSpans(end, end, ImageSpanEx.class); + if (spans.length == 1) { + int e = ssb.getSpanEnd(spans[0]); + String src = spans[0].getSource(); + + String url = null; + if (src != null && src.startsWith("cid:")) { + String cid = '<' + src.substring(4) + '>'; + EntityAttachment attachment = db.attachment().getAttachment(id, cid); + if (attachment != null && attachment.available) + try { + url = ImageHelper.getDataUri(attachment.getFile(context), attachment.type); + } catch (Throwable ex) { + Log.w(ex); + } + } else + url = src; + + if (url != null) + contents.add(new OpenAI.Content(OpenAI.CONTENT_IMAGE, url)); + end = e; + } + } + start = end; + } + + return contents.toArray(new OpenAI.Content[0]); + } + static class Content { private String type; private String content;