Optimize the agent tool execution loop
This commit is contained in:
1 parent
572371d7af
commit
4291adfdfb
7 files changed
+110
-7
No files matched your search
@@ -13,6 +13,10 @@ import org.springframework.stereotype.Component;
|
||||
@EnableScheduling
|
||||
@ConditionalOnProperty(name = "app.worker-enabled", havingValue = "true")
|
||||
public class AgentWorker {
|
||||
// Adapted from claude-quickstarts computer_use_demo/loop.py (4a4224bdf0e9).
|
||||
// See META-INF/anthropic-claude-quickstarts-LICENSE.txt.
|
||||
private static final String NOT_EXECUTED_ERROR =
|
||||
"Not executed: an earlier tool in this turn failed.";
|
||||
private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(AgentWorker.class);
|
||||
private final TaskService tasks;
|
||||
private final ModelGateway gateway;
|
||||
@@ -258,18 +262,27 @@ public class AgentWorker {
|
||||
tasks.finish(id, lease, "PAUSED", "工具输出达到模型上限,费用已结算,未执行本轮工具;请检查模型输出上限后继续", false);
|
||||
return;
|
||||
}
|
||||
boolean earlierToolFailed = false;
|
||||
for (var call : result.calls()) {
|
||||
if ("CANCEL".equals(tasks.config(id).get("requested_action"))) {
|
||||
tasks.finish(id, lease, "CANCELLED", "用户取消,未继续执行后续工具", true);
|
||||
return;
|
||||
}
|
||||
if (earlierToolFailed) {
|
||||
tasks.tool(
|
||||
id,
|
||||
lease,
|
||||
call,
|
||||
json.createObjectNode().put("error", NOT_EXECUTED_ERROR).toString());
|
||||
continue;
|
||||
}
|
||||
tasks.heartbeat(id, lease);
|
||||
external = call.argumentError() == null;
|
||||
String output =
|
||||
call.argumentError() == null
|
||||
? sandbox.execute(project, call)
|
||||
: json.writeValueAsString(Map.of("error", call.argumentError()));
|
||||
tasks.tool(id, lease, call, output);
|
||||
earlierToolFailed = tasks.tool(id, lease, call, output);
|
||||
}
|
||||
external = false;
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ public class ModelGateway {
|
||||
ArrayNode messages = body.putArray("messages");
|
||||
for (JsonNode m : history) {
|
||||
ObjectNode copy = ((ObjectNode) m).deepCopy();
|
||||
copy.remove(List.of("native", "nativeType"));
|
||||
copy.remove(List.of("native", "nativeType", "is_error"));
|
||||
messages.add(copy);
|
||||
}
|
||||
if (withTools) body.set("tools", definitions);
|
||||
@@ -135,6 +135,7 @@ public class ModelGateway {
|
||||
block.put("type", "tool_result");
|
||||
block.put("tool_use_id", m.path("tool_call_id").asText());
|
||||
block.put("content", m.path("content").asText());
|
||||
if (m.path("is_error").asBoolean(false)) block.put("is_error", true);
|
||||
} else if (role.equals("assistant") && m.path("nativeType").asText().equals(type)) {
|
||||
content.addAll((ArrayNode) m.get("native"));
|
||||
} else {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
@@ -374,12 +375,24 @@ public class TaskService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void tool(UUID id, String lease, ModelGateway.Call call, String output) {
|
||||
public boolean tool(UUID id, String lease, ModelGateway.Call call, String output)
|
||||
throws JsonProcessingException {
|
||||
locked(id, lease);
|
||||
message(
|
||||
id,
|
||||
Map.of("role", "tool", "tool_call_id", call.id(), "name", call.name(), "content", output));
|
||||
var result = json.readTree(output);
|
||||
boolean failed =
|
||||
result.hasNonNull("error")
|
||||
|| result.path("timedOut").asBoolean(false)
|
||||
|| result.path("exitCode").asInt(0) != 0;
|
||||
var payload =
|
||||
json.createObjectNode()
|
||||
.put("role", "tool")
|
||||
.put("tool_call_id", call.id())
|
||||
.put("name", call.name())
|
||||
.put("content", output);
|
||||
if (failed) payload.put("is_error", true);
|
||||
message(id, payload);
|
||||
event(id, "TOOL_RESULT", call.name() + ": " + output);
|
||||
return failed;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
||||
Reference in new issue
Block a user