Skip to content

Commit

Permalink
feat(chrome-athena): add observe machenism add fix chat (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
sadadw1 authored Feb 24, 2025
2 parents 4502465 + c866fa1 commit 76f44e5
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public OpenTabFunction(ObjectMapper objectMapper) {
},
"auto": {
"type": "boolean",
"description": "是否自动执行"
"description": "是否自动执行,通常为true,除非显式指定"
},
"desc": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ public OperationFunction(ObjectMapper objectMapper) {
"tabId": {
"type": "string",
"description": "标签页ID"
},
"waiting": {
"type": "boolean",
"description": "是否需要接收回调信息,通常为true,除非显式指定"
}
},
"required": ["type", "name", "elementId", "tabId"]
"required": ["type", "name", "elementId", "tabId", "waiting"]
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static List<Map<String, Object>> getMcpInfo(String from) {
</use_mcp_tool>
## Chat
## chat
Description: A tool for handling general conversations and chat interactions. This tool should be used when the user's input is conversational in nature and doesn't require specific functional tools. It enables natural dialogue-based interactions in scenarios where other specialized tools are not applicable. Use this tool for engaging in general discussions, providing information, or offering support through conversation.
Parameters:
- message: (required) The chat message to respond to the user. The message should be natural, friendly, and maintain coherence and relevance with the user's input.
Expand Down Expand Up @@ -185,6 +185,7 @@ <command>Command to demonstrate result (optional)</command>
- New terminal output in reaction to the changes, which you may need to consider or act upon.
- Any other relevant feedback or information related to the tool use.
6. ALWAYS wait for user confirmation after each tool use before proceeding. Never assume the success of a tool use without explicit confirmation of the result from the user.
7. When you can not decide which tool to use, you can use the chat tool to ask the user for help. If you are using the chat tool, you must return the message in Chinese中文.
It is crucial to proceed step-by-step, waiting for the user's message after each tool use before moving forward with the task. This approach allows you to:
1. Confirm the success of each step before proceeding.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class ChromeAthena extends Role {
}
%>
===========
请帮我判断使用那个TOOL\n
请帮我判断使用哪个TOOL\n
""";

private String rolePrompt = """
Expand Down Expand Up @@ -138,18 +138,21 @@ protected int observe() {
|| !obj.get("urlChanged").getAsBoolean())
&& (!obj.has("contentChanged") || !obj.get("contentChanged").getAsBoolean())) {
TimeUnit.SECONDS.sleep(1);
this.rc.getNews().put(Message.builder().content("Nothing changed since last action, try to take other action if possible").build());
return 1; // keep observing
this.rc.getNews().put(Message.builder().role("user").content("Nothing changed since last action, try to take other action if possible").build());
} else if (obj.has("error")) {
this.rc.getNews().put(Message.builder().role("user").content("Action failed, with error: " + obj.get("error").getAsString() + "; Please try again.").build());
}
return 1; // keep observing
}

if (msg.getType().equals("json")) {
JsonObject obj = JsonParser.parseString(msg.getContent()).getAsJsonObject();
text = JsonUtils.getValueOrDefault(obj, "text", "");
JsonArray imgs = obj.getAsJsonArray("img");
if (imgs != null) {
if (imgs != null && imgs.size() > 0) {
images = getImageStrings(imgs);
msg.setImages(images);
// msg.setRole("assistant");
}
code = JsonUtils.getValueOrDefault(obj, "code", "");
tabs = JsonUtils.getValueOrDefault(obj, "tabs", "");
Expand All @@ -161,9 +164,7 @@ protected int observe() {
if (!CollectionUtils.isEmpty(images)) {
text = text + "\nimages:\n [图片占位符]";
}

msg.setContent(text);
msg.setRole("assistant");
}

this.getRc().getMemory().add(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,25 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message)

//chrome直接返回的(目前只有购物)
if (cmd.equals("shopping")) {
chromeAthena.getRc().news.put(Message.builder().type("json").content(data).build());
chromeAthena.getRc().news.put(Message.builder().type("json").role("user").content(data).build());
return;
}

if (cmd.equals("reply")) {
chromeAthena.getRc().news.put(Message.builder().type("reply").role("user").content(data).build());
JsonObject obj = JsonParser.parseString(data).getAsJsonObject();
// action不成功,需要交给Role处理
if (!obj.has("success") || !obj.get("success").getAsBoolean()) {
log.info("action:{} failed", obj.get("actionType"));
chromeAthena.getRc().news.put(Message.builder().type("reply").role("user").content(data).build());
} else {
log.info("action:{} success", obj.get("actionType"));
}
return;
}


chromeAthena.getRc().news.put(Message.builder().type("json").role("user").content(data).build());
new Thread(() -> chromeAthena.run()).start();
Thread.ofVirtual().factory().newThread(() -> chromeAthena.run()).start();
return;
}

Expand Down
Loading

0 comments on commit 76f44e5

Please sign in to comment.