refactor: Optimize response handler usage in BaseAgent for improved readability and performance
This commit is contained in:
@@ -100,13 +100,15 @@ public interface BaseAgent {
|
|||||||
|
|
||||||
boolean parseable = false;
|
boolean parseable = false;
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
|
final var responseHandler = this.responseHandler();
|
||||||
|
|
||||||
Completion completion = null;
|
Completion completion = null;
|
||||||
if (split.context().isStreaming()) {
|
if (split.context().isStreaming()) {
|
||||||
if (this.responseHandler() instanceof final StreamedAgentResponseHandler streamedResponseHandler) {
|
if (responseHandler instanceof final StreamedAgentResponseHandler streamedResponseHandler) {
|
||||||
streamedResponseHandler.beforeStream(split.context());
|
streamedResponseHandler.beforeStream(split.context());
|
||||||
}
|
}
|
||||||
completion = client.streamConversation(this.modelCategory(split.context()), conversation, chunk -> {
|
completion = client.streamConversation(this.modelCategory(split.context()), conversation, chunk -> {
|
||||||
if (this.responseHandler() instanceof final StreamedAgentResponseHandler streamedResponseHandler) {
|
if (responseHandler instanceof final StreamedAgentResponseHandler streamedResponseHandler) {
|
||||||
if (streamedResponseHandler.handleChunk(chunk, split.context()) && split.context().getAgentContextUpdateConsumer() != null) {
|
if (streamedResponseHandler.handleChunk(chunk, split.context()) && split.context().getAgentContextUpdateConsumer() != null) {
|
||||||
split.context().getAgentContextUpdateConsumer().accept(split.context());
|
split.context().getAgentContextUpdateConsumer().accept(split.context());
|
||||||
}
|
}
|
||||||
@@ -120,7 +122,7 @@ public interface BaseAgent {
|
|||||||
split.context().getRequestStatistic().add(completion);
|
split.context().getRequestStatistic().add(completion);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!this.responseHandler().handle(completion, split.context())) {
|
if (!responseHandler.handle(completion, split.context())) {
|
||||||
split.context().getGraph().add("⚠");
|
split.context().getGraph().add("⚠");
|
||||||
LOGGER.warn("{} Cannot parse response: {}", this.name(), completion.response());
|
LOGGER.warn("{} Cannot parse response: {}", this.name(), completion.response());
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user