Process markdown in AI responses

pull/215/head
M66B 7 months ago
parent b806ab84c9
commit c64df8a97c

@ -23,6 +23,7 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -43,7 +44,7 @@ public class AI {
return (OpenAI.isAvailable(context) || Gemini.isAvailable(context)); 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; String reply = null;
if (body == null || TextUtils.isEmpty(body.toString().trim())) { if (body == null || TextUtils.isEmpty(body.toString().trim())) {
body = "?"; body = "?";
@ -74,6 +75,7 @@ public class AI {
prompt = JsoupEx.parse(html).body().text(); prompt = JsoupEx.parse(html).body().text();
} }
StringBuilder sb = new StringBuilder();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (OpenAI.isAvailable(context)) { if (OpenAI.isAvailable(context)) {
String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL); String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL);
@ -100,7 +102,6 @@ public class AI {
OpenAI.Message[] completions = OpenAI.completeChat(context, OpenAI.Message[] completions = OpenAI.completeChat(context,
model, messages.toArray(new OpenAI.Message[0]), temperature, 1); model, messages.toArray(new OpenAI.Message[0]), temperature, 1);
StringBuilder sb = new StringBuilder();
for (OpenAI.Message completion : completions) for (OpenAI.Message completion : completions)
for (OpenAI.Content content : completion.getContent()) for (OpenAI.Content content : completion.getContent())
if (OpenAI.CONTENT_TEXT.equals(content.getType())) { if (OpenAI.CONTENT_TEXT.equals(content.getType())) {
@ -110,7 +111,6 @@ public class AI {
.replaceAll("^\\n+", "") .replaceAll("^\\n+", "")
.replaceAll("\\n+$", "")); .replaceAll("\\n+$", ""));
} }
return sb.toString();
} else if (Gemini.isAvailable(context)) { } else if (Gemini.isAvailable(context)) {
String model = prefs.getString("gemini_model", Gemini.DEFAULT_MODEL); String model = prefs.getString("gemini_model", Gemini.DEFAULT_MODEL);
float temperature = prefs.getFloat("gemini_temperature", Gemini.DEFAULT_TEMPERATURE); float temperature = prefs.getFloat("gemini_temperature", Gemini.DEFAULT_TEMPERATURE);
@ -129,7 +129,6 @@ public class AI {
Gemini.Message[] completions = Gemini.generate(context, Gemini.Message[] completions = Gemini.generate(context,
model, messages.toArray(new Gemini.Message[0]), temperature, 1); model, messages.toArray(new Gemini.Message[0]), temperature, 1);
StringBuilder sb = new StringBuilder();
for (Gemini.Message completion : completions) for (Gemini.Message completion : completions)
for (String result : completion.getContent()) { for (String result : completion.getContent()) {
if (sb.length() > 0) if (sb.length() > 0)
@ -138,9 +137,10 @@ public class AI {
.replaceAll("^\\n+", "") .replaceAll("^\\n+", "")
.replaceAll("\\n+$", "")); .replaceAll("\\n+$", ""));
} }
return sb.toString();
} else } else
throw new IllegalArgumentException("No AI available"); throw new IllegalArgumentException("No AI available");
return HtmlHelper.fromHtml(Markdown.toHtml(sb.toString()), context);
} }
static String getSummarizePrompt(Context context) { static String getSummarizePrompt(Context context) {

@ -2699,7 +2699,7 @@ public class FragmentCompose extends FragmentBase {
args.putCharSequence("body", body); args.putCharSequence("body", body);
args.putLong("template", template == null ? -1L : template); args.putLong("template", template == null ? -1L : template);
new SimpleTask<String>() { new SimpleTask<Spanned>() {
@Override @Override
protected void onPreExecute(Bundle args) { protected void onPreExecute(Bundle args) {
chatting = true; chatting = true;
@ -2713,7 +2713,7 @@ public class FragmentCompose extends FragmentBase {
} }
@Override @Override
protected String onExecute(Context context, Bundle args) throws Throwable { protected Spanned onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id"); long id = args.getLong("id");
CharSequence body = args.getCharSequence("body"); CharSequence body = args.getCharSequence("body");
long template = args.getLong("template"); long template = args.getLong("template");
@ -2722,7 +2722,7 @@ public class FragmentCompose extends FragmentBase {
} }
@Override @Override
protected void onExecuted(Bundle args, String completion) { protected void onExecuted(Bundle args, Spanned completion) {
if (completion == null) if (completion == null)
return; return;
@ -2742,7 +2742,8 @@ public class FragmentCompose extends FragmentBase {
if (index > 0 && edit.charAt(index - 1) != '\n') if (index > 0 && edit.charAt(index - 1) != '\n')
edit.insert(index++, "\n"); edit.insert(index++, "\n");
edit.insert(index, completion + "\n"); edit.insert(index, "\n");
edit.insert(index, completion);
etBody.setSelection(index + completion.length() + 1); etBody.setSelection(index + completion.length() + 1);
StyleHelper.markAsInserted(edit, index, index + completion.length() + 1); StyleHelper.markAsInserted(edit, index, index + completion.length() + 1);

Loading…
Cancel
Save