fix: add target_url param for Action::Eval in browser_script_skill_tool

🤖 Generated with [Qoder][https://qoder.com]
This commit is contained in:
木炎
2026-04-13 15:03:49 +08:00
parent 0ebe060484
commit a957712590

View File

@@ -154,11 +154,26 @@ fn execute_browser_script_impl(
browser_tool: &dyn BrowserBackend, browser_tool: &dyn BrowserBackend,
args: Value, args: Value,
) -> anyhow::Result<ToolResult> { ) -> anyhow::Result<ToolResult> {
eprintln!("[execute_browser_script_impl] 开始执行");
eprintln!("[execute_browser_script_impl] tool.name: {}", tool.name);
eprintln!("[execute_browser_script_impl] tool.command: {}", tool.command);
eprintln!("[execute_browser_script_impl] skill_root: {:?}", skill_root);
eprintln!("[execute_browser_script_impl] args: {:?}", args);
let script_path = resolve_browser_script_path(skill_root, &tool.command)?; let script_path = resolve_browser_script_path(skill_root, &tool.command)?;
eprintln!("[execute_browser_script_impl] script_path: {:?}", script_path);
// 检查脚本文件是否存在
if !script_path.exists() {
eprintln!("[execute_browser_script_impl] 脚本文件不存在!");
} else {
eprintln!("[execute_browser_script_impl] 脚本文件存在");
}
let mut args = match args { let mut args = match args {
Value::Object(args) => args, Value::Object(args) => args,
other => { other => {
eprintln!("[execute_browser_script_impl] args 不是 Object: {:?}", other);
return Ok(failed_tool_result(format!( return Ok(failed_tool_result(format!(
"expected object arguments, got {other}" "expected object arguments, got {other}"
))) )))
@@ -168,27 +183,34 @@ fn execute_browser_script_impl(
let raw_expected_domain = match args.remove("expected_domain") { let raw_expected_domain = match args.remove("expected_domain") {
Some(Value::String(value)) if !value.trim().is_empty() => value, Some(Value::String(value)) if !value.trim().is_empty() => value,
Some(other) => { Some(other) => {
eprintln!("[execute_browser_script_impl] expected_domain 格式错误: {:?}", other);
return Ok(failed_tool_result(format!( return Ok(failed_tool_result(format!(
"expected_domain must be a non-empty string, got {other}" "expected_domain must be a non-empty string, got {other}"
))) )))
} }
None => { None => {
eprintln!("[execute_browser_script_impl] 缺少 expected_domain");
return Ok(failed_tool_result( return Ok(failed_tool_result(
"missing required field expected_domain".to_string(), "missing required field expected_domain".to_string(),
)) ))
} }
}; };
eprintln!("[execute_browser_script_impl] raw_expected_domain: {}", raw_expected_domain);
let expected_domain = match normalize_domain_like(&raw_expected_domain) { let expected_domain = match normalize_domain_like(&raw_expected_domain) {
Some(value) => value, Some(value) => value,
None => { None => {
eprintln!("[execute_browser_script_impl] expected_domain 解析失败");
return Ok(failed_tool_result(format!( return Ok(failed_tool_result(format!(
"expected_domain must resolve to a hostname, got {raw_expected_domain:?}" "expected_domain must resolve to a hostname, got {raw_expected_domain:?}"
))) )))
} }
}; };
eprintln!("[execute_browser_script_impl] expected_domain: {}", expected_domain);
for required_arg in tool.args.keys() { for required_arg in tool.args.keys() {
if !args.contains_key(required_arg) { if !args.contains_key(required_arg) {
eprintln!("[execute_browser_script_impl] 缺少必需参数: {}", required_arg);
return Ok(failed_tool_result(format!( return Ok(failed_tool_result(format!(
"missing required field {required_arg}" "missing required field {required_arg}"
))); )));
@@ -196,8 +218,12 @@ fn execute_browser_script_impl(
} }
let script_body = match fs::read_to_string(&script_path) { let script_body = match fs::read_to_string(&script_path) {
Ok(value) => value, Ok(value) => {
eprintln!("[execute_browser_script_impl] 脚本读取成功, 长度: {} 字节", value.len());
value
}
Err(err) => { Err(err) => {
eprintln!("[execute_browser_script_impl] 脚本读取失败: {}", err);
return Ok(failed_tool_result(format!( return Ok(failed_tool_result(format!(
"failed to read browser script {}: {err}", "failed to read browser script {}: {err}",
script_path.display() script_path.display()
@@ -206,16 +232,30 @@ fn execute_browser_script_impl(
}; };
let wrapped_script = wrap_browser_script(&script_body, &Value::Object(args.clone())); let wrapped_script = wrap_browser_script(&script_body, &Value::Object(args.clone()));
eprintln!("[execute_browser_script_impl] 包装后脚本长度: {} 字节", wrapped_script.len());
eprintln!("[execute_browser_script_impl] 调用 browser_tool.invoke(Action::Eval)...");
let target_url = format!("http://{}", expected_domain);
let result = match browser_tool.invoke( let result = match browser_tool.invoke(
Action::Eval, Action::Eval,
json!({ "script": wrapped_script }), json!({
"script": wrapped_script,
"target_url": target_url,
}),
&expected_domain, &expected_domain,
) { ) {
Ok(result) => result, Ok(result) => {
Err(err) => return Ok(failed_tool_result(err.to_string())), eprintln!("[execute_browser_script_impl] invoke 成功, result.success: {}", result.success);
result
}
Err(err) => {
eprintln!("[execute_browser_script_impl] invoke 失败: {}", err);
return Ok(failed_tool_result(err.to_string()))
}
}; };
if !result.success { if !result.success {
eprintln!("[execute_browser_script_impl] result.success=false, data: {:?}", result.data);
return Ok(failed_tool_result(format_browser_script_error(&result.data))); return Ok(failed_tool_result(format_browser_script_error(&result.data)));
} }
@@ -224,6 +264,7 @@ fn execute_browser_script_impl(
.get("text") .get("text")
.cloned() .cloned()
.unwrap_or_else(|| result.data.clone()); .unwrap_or_else(|| result.data.clone());
eprintln!("[execute_browser_script_impl] 返回成功, payload 长度: {:?}", payload.to_string().len());
Ok(ToolResult { Ok(ToolResult {
success: true, success: true,
output: stringify_tool_payload(&payload)?, output: stringify_tool_payload(&payload)?,