From a50ae9d0cf15c10a7ee8e782943ec0ac559a28cf Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 10 Mar 2023 11:54:31 +0100 Subject: [PATCH] OpenAI: refactoring --- .../java/eu/faircode/email/FragmentCompose.java | 17 ++--------------- app/src/main/java/eu/faircode/email/OpenAI.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 43b7b29fd2..3fe7edffa2 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -2432,11 +2432,11 @@ public class FragmentCompose extends FragmentBase { Document parsed = JsoupEx.parse(inreplyto.get(0).getFile(context)); Document document = HtmlHelper.sanitizeView(context, parsed, false); Spanned spanned = HtmlHelper.fromDocument(context, document, null, null); - result.add(new OpenAI.Message(role, truncate(spanned.toString(), MAX_OPENAI_LEN))); + result.add(new OpenAI.Message(role, OpenAI.truncateParagraphs(spanned.toString(), MAX_OPENAI_LEN))); } if (!TextUtils.isEmpty(body)) - result.add(new OpenAI.Message("assistant", truncate(body, MAX_OPENAI_LEN))); + result.add(new OpenAI.Message("assistant", OpenAI.truncateParagraphs(body, MAX_OPENAI_LEN))); if (result.size() == 0) return null; @@ -2464,19 +2464,6 @@ public class FragmentCompose extends FragmentBase { return completions; } - @NonNull - private String truncate(@NonNull String text, int maxlen) { - String[] paragraphs = text.split("[\\r\\n]+"); - - int i = 0; - StringBuilder sb = new StringBuilder(); - while (i < paragraphs.length && - sb.length() + paragraphs[i].length() + 1 < maxlen) - sb.append(paragraphs[i++]).append('\n'); - - return sb.toString(); - } - @Override protected void onExecuted(Bundle args, OpenAI.Message[] messages) { if (messages == null || messages.length == 0) diff --git a/app/src/main/java/eu/faircode/email/OpenAI.java b/app/src/main/java/eu/faircode/email/OpenAI.java index 5266f4c2a8..5774ab2380 100644 --- a/app/src/main/java/eu/faircode/email/OpenAI.java +++ b/app/src/main/java/eu/faircode/email/OpenAI.java @@ -229,6 +229,18 @@ public class OpenAI { } } + static String truncateParagraphs(@NonNull String text, int maxlen) { + String[] paragraphs = text.split("[\\r\\n]+"); + + int i = 0; + StringBuilder sb = new StringBuilder(); + while (i < paragraphs.length && + sb.length() + paragraphs[i].length() + 1 < maxlen) + sb.append(paragraphs[i++]).append('\n'); + + return sb.toString(); + } + static class Message { private final String role; // // system, user, assistant private final String content;