Added AI interference to expression conditions

This commit is contained in:
M66B
2024-11-01 09:15:58 +01:00
parent a2f1febfe7
commit 9effd8a61e
4 changed files with 64 additions and 22 deletions

View File

@@ -115,6 +115,7 @@ public class ExpressionHelper {
JsoupFunction fJsoup = new JsoupFunction(context, message);
SizeFunction fSize = new SizeFunction();
KnownFunction fKnown = new KnownFunction(context, message);
AIFunction fAI = new AIFunction(context, doc);
ContainsOperator oContains = new ContainsOperator(false);
ContainsOperator oMatches = new ContainsOperator(true);
@@ -130,6 +131,7 @@ public class ExpressionHelper {
configuration.getFunctionDictionary().addFunction("Jsoup", fJsoup);
configuration.getFunctionDictionary().addFunction("Size", fSize);
configuration.getFunctionDictionary().addFunction("knownContact", fKnown);
configuration.getFunctionDictionary().addFunction("AI", fAI);
configuration.getOperatorDictionary().addOperator("Contains", oContains);
configuration.getOperatorDictionary().addOperator("Matches", oMatches);
@@ -179,6 +181,17 @@ public class ExpressionHelper {
for (String variable : expression.getUsedVariables())
if ("text".equalsIgnoreCase(variable))
return true;
expression.validate();
for (ASTNode node : expression.getAllASTNodes()) {
Token token = node.getToken();
Log.i("EXPR token=" + token.getType() + ":" + token.getValue());
if (token.getType() == Token.TokenType.FUNCTION &&
"AI".equalsIgnoreCase(token.getValue())) {
Log.i("EXPR needs body");
return true;
}
}
} catch (Throwable ex) {
Log.e("EXPR", ex);
}
@@ -444,6 +457,36 @@ public class ExpressionHelper {
}
}
@FunctionParameter(name = "value")
public static class AIFunction extends AbstractFunction {
private final Context context;
private final Document doc;
AIFunction(Context context, Document doc) {
this.context = context;
this.doc = doc;
}
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues) {
String result = null;
try {
if (doc != null && parameterValues.length == 1) {
String prompt = parameterValues[0].getStringValue();
if (!TextUtils.isEmpty(prompt))
result = AI.completeChat(context, -1L, doc.text(), null, prompt).toString();
}
} catch (Throwable ex) {
Log.w(ex);
}
Log.i("EXPR AI()=" + result);
return expression.convertValue(result);
}
}
@InfixOperator(precedence = OPERATOR_PRECEDENCE_COMPARISON)
public static class ContainsOperator extends AbstractOperator {
private final boolean regex;