mirror of https://github.com/M66B/FairEmail.git
Process markdown in AI responses
This commit is contained in:
parent
b806ab84c9
commit
c64df8a97c
|
@ -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) {
|
||||
|
|
|
@ -2699,7 +2699,7 @@ public class FragmentCompose extends FragmentBase {
|
|||
args.putCharSequence("body", body);
|
||||
args.putLong("template", template == null ? -1L : template);
|
||||
|
||||
new SimpleTask<String>() {
|
||||
new SimpleTask<Spanned>() {
|
||||
@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);
|
||||
|
|
Loading…
Reference in New Issue