AI rule node: fix wrapping in JSON object if response is not a valid JSON object
This commit is contained in:
parent
a4db17c859
commit
00d32c2374
@ -115,7 +115,7 @@ public final class TbAiNode extends TbAbstractExternalNode implements TbNode {
|
|||||||
addCallback(sendChatRequest(ctx, chatRequest), new FutureCallback<>() {
|
addCallback(sendChatRequest(ctx, chatRequest), new FutureCallback<>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(String response) {
|
public void onSuccess(String response) {
|
||||||
if (!isValidJson(response)) {
|
if (!isValidJsonObject(response)) {
|
||||||
response = wrapInJsonObject(response);
|
response = wrapInJsonObject(response);
|
||||||
}
|
}
|
||||||
tellSuccess(ctx, ackedMsg.transform()
|
tellSuccess(ctx, ackedMsg.transform()
|
||||||
@ -134,9 +134,10 @@ public final class TbAiNode extends TbAbstractExternalNode implements TbNode {
|
|||||||
return ctx.getExternalCallExecutor().submit(() -> chatModel.chat(chatRequest).aiMessage().text());
|
return ctx.getExternalCallExecutor().submit(() -> chatModel.chat(chatRequest).aiMessage().text());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isValidJson(String jsonString) {
|
private static boolean isValidJsonObject(String jsonString) {
|
||||||
try {
|
try {
|
||||||
return JacksonUtil.toJsonNode(jsonString) != null;
|
JsonNode result = JacksonUtil.toJsonNode(jsonString);
|
||||||
|
return result != null && result.isObject();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user