From c64df8a97c4c2a9cf8f5640b998df1599feb44ed Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 29 May 2024 12:46:29 +0200 Subject: [PATCH] Process markdown in AI responses --- app/src/main/java/eu/faircode/email/AI.java | 10 +++++----- .../main/java/eu/faircode/email/FragmentCompose.java | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AI.java b/app/src/main/java/eu/faircode/email/AI.java index 1cb8e97a17..22d9c3e7d1 100644 --- a/app/src/main/java/eu/faircode/email/AI.java +++ b/app/src/main/java/eu/faircode/email/AI.java @@ -23,6 +23,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.text.Spannable; import android.text.SpannableStringBuilder; +import android.text.Spanned; import android.text.TextUtils; import androidx.preference.PreferenceManager; @@ -43,7 +44,7 @@ public class AI { return (OpenAI.isAvailable(context) || Gemini.isAvailable(context)); } - static String completeChat(Context context, long id, CharSequence body, long template) throws JSONException, IOException { + static Spanned completeChat(Context context, long id, CharSequence body, long template) throws JSONException, IOException { String reply = null; if (body == null || TextUtils.isEmpty(body.toString().trim())) { body = "?"; @@ -74,6 +75,7 @@ public class AI { prompt = JsoupEx.parse(html).body().text(); } + StringBuilder sb = new StringBuilder(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); if (OpenAI.isAvailable(context)) { String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL); @@ -100,7 +102,6 @@ public class AI { OpenAI.Message[] completions = OpenAI.completeChat(context, model, messages.toArray(new OpenAI.Message[0]), temperature, 1); - StringBuilder sb = new StringBuilder(); for (OpenAI.Message completion : completions) for (OpenAI.Content content : completion.getContent()) if (OpenAI.CONTENT_TEXT.equals(content.getType())) { @@ -110,7 +111,6 @@ public class AI { .replaceAll("^\\n+", "") .replaceAll("\\n+$", "")); } - return sb.toString(); } else if (Gemini.isAvailable(context)) { String model = prefs.getString("gemini_model", Gemini.DEFAULT_MODEL); float temperature = prefs.getFloat("gemini_temperature", Gemini.DEFAULT_TEMPERATURE); @@ -129,7 +129,6 @@ public class AI { Gemini.Message[] completions = Gemini.generate(context, model, messages.toArray(new Gemini.Message[0]), temperature, 1); - StringBuilder sb = new StringBuilder(); for (Gemini.Message completion : completions) for (String result : completion.getContent()) { if (sb.length() > 0) @@ -138,9 +137,10 @@ public class AI { .replaceAll("^\\n+", "") .replaceAll("\\n+$", "")); } - return sb.toString(); } else throw new IllegalArgumentException("No AI available"); + + return HtmlHelper.fromHtml(Markdown.toHtml(sb.toString()), context); } static String getSummarizePrompt(Context context) { diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 4a1d53c961..6eb67cd33c 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -2699,7 +2699,7 @@ public class FragmentCompose extends FragmentBase { args.putCharSequence("body", body); args.putLong("template", template == null ? -1L : template); - new SimpleTask() { + new SimpleTask() { @Override protected void onPreExecute(Bundle args) { chatting = true; @@ -2713,7 +2713,7 @@ public class FragmentCompose extends FragmentBase { } @Override - protected String onExecute(Context context, Bundle args) throws Throwable { + protected Spanned onExecute(Context context, Bundle args) throws Throwable { long id = args.getLong("id"); CharSequence body = args.getCharSequence("body"); long template = args.getLong("template"); @@ -2722,7 +2722,7 @@ public class FragmentCompose extends FragmentBase { } @Override - protected void onExecuted(Bundle args, String completion) { + protected void onExecuted(Bundle args, Spanned completion) { if (completion == null) return; @@ -2742,7 +2742,8 @@ public class FragmentCompose extends FragmentBase { if (index > 0 && edit.charAt(index - 1) != '\n') edit.insert(index++, "\n"); - edit.insert(index, completion + "\n"); + edit.insert(index, "\n"); + edit.insert(index, completion); etBody.setSelection(index + completion.length() + 1); StyleHelper.markAsInserted(edit, index, index + completion.length() + 1);