feat: add config-owned direct skill submit path

Add fixed direct-submit skill loading from configured staged skills and validate directSubmitSkill early so malformed configs fail before routing.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
木炎
2026-04-09 19:02:30 +08:00
parent 2ae71fb1c9
commit 4becf81066
11 changed files with 1962 additions and 97 deletions

View File

@@ -13,6 +13,7 @@ use crate::pipe::{Action, BrowserPipeTool, Transport};
pub struct BrowserScriptSkillTool<T: Transport> {
tool_name: String,
tool_description: String,
skill_root: PathBuf,
script_path: PathBuf,
args: HashMap<String, String>,
browser_tool: BrowserPipeTool<T>,
@@ -25,27 +26,13 @@ impl<T: Transport> BrowserScriptSkillTool<T> {
skill_root: &Path,
browser_tool: BrowserPipeTool<T>,
) -> anyhow::Result<Self> {
let script_path = skill_root.join(&tool.command);
let canonical_skill_root = skill_root
.canonicalize()
.unwrap_or_else(|_| skill_root.to_path_buf());
let canonical_script_path = script_path.canonicalize().map_err(|err| {
anyhow::anyhow!(
"failed to resolve browser script {}: {err}",
script_path.display()
)
})?;
if !canonical_script_path.starts_with(&canonical_skill_root) {
anyhow::bail!(
"browser script path escapes skill root: {}",
canonical_script_path.display()
);
}
let script_path = resolve_browser_script_path(skill_root, &tool.command)?;
Ok(Self {
tool_name: format!("{}.{}", skill_name, tool.name),
tool_description: tool.description.clone(),
script_path: canonical_script_path,
skill_root: skill_root.to_path_buf(),
script_path,
args: tool.args.clone(),
browser_tool,
})
@@ -97,82 +84,101 @@ impl<T: Transport + 'static> Tool for BrowserScriptSkillTool<T> {
}
async fn execute(&self, args: Value) -> anyhow::Result<ToolResult> {
let mut args = match args {
Value::Object(args) => args,
other => {
return Ok(failed_tool_result(format!(
"expected object arguments, got {other}"
)))
}
let tool = SkillTool {
name: self.tool_name.clone(),
description: self.tool_description.clone(),
kind: "browser_script".to_string(),
command: self.script_path.to_string_lossy().into_owned(),
args: self.args.clone(),
};
let raw_expected_domain = match args.remove("expected_domain") {
Some(Value::String(value)) if !value.trim().is_empty() => value,
Some(other) => {
return Ok(failed_tool_result(format!(
"expected_domain must be a non-empty string, got {other}"
)))
}
None => {
return Ok(failed_tool_result(
"missing required field expected_domain".to_string(),
))
}
};
let expected_domain = match normalize_domain_like(&raw_expected_domain) {
Some(value) => value,
None => {
return Ok(failed_tool_result(format!(
"expected_domain must resolve to a hostname, got {raw_expected_domain:?}"
)))
}
};
execute_browser_script_tool(&tool, &self.skill_root, self.browser_tool.clone(), args).await
}
}
for required_arg in self.args.keys() {
if !args.contains_key(required_arg) {
return Ok(failed_tool_result(format!(
"missing required field {required_arg}"
)));
}
pub async fn execute_browser_script_tool<T: Transport + 'static>(
tool: &SkillTool,
skill_root: &Path,
browser_tool: BrowserPipeTool<T>,
args: Value,
) -> anyhow::Result<ToolResult> {
if tool.kind != "browser_script" {
return Ok(failed_tool_result(format!(
"browser script tool kind must be browser_script, got {}",
tool.kind
)));
}
let script_path = resolve_browser_script_path(skill_root, &tool.command)?;
let mut args = match args {
Value::Object(args) => args,
other => return Ok(failed_tool_result(format!("expected object arguments, got {other}"))),
};
let raw_expected_domain = match args.remove("expected_domain") {
Some(Value::String(value)) if !value.trim().is_empty() => value,
Some(other) => {
return Ok(failed_tool_result(format!(
"expected_domain must be a non-empty string, got {other}"
)))
}
None => {
return Ok(failed_tool_result(
"missing required field expected_domain".to_string(),
))
}
};
let expected_domain = match normalize_domain_like(&raw_expected_domain) {
Some(value) => value,
None => {
return Ok(failed_tool_result(format!(
"expected_domain must resolve to a hostname, got {raw_expected_domain:?}"
)))
}
};
let script_body = match fs::read_to_string(&self.script_path) {
Ok(value) => value,
Err(err) => {
return Ok(failed_tool_result(format!(
"failed to read browser script {}: {err}",
self.script_path.display()
)))
}
};
let wrapped_script = wrap_browser_script(&script_body, &Value::Object(args.clone()));
let result = match self.browser_tool.invoke(
Action::Eval,
json!({ "script": wrapped_script }),
&expected_domain,
) {
Ok(result) => result,
Err(err) => return Ok(failed_tool_result(err.to_string())),
};
if !result.success {
return Ok(failed_tool_result(format_browser_script_error(
&result.data,
for required_arg in tool.args.keys() {
if !args.contains_key(required_arg) {
return Ok(failed_tool_result(format!(
"missing required field {required_arg}"
)));
}
let payload = result
.data
.get("text")
.cloned()
.unwrap_or_else(|| result.data.clone());
Ok(ToolResult {
success: true,
output: stringify_tool_payload(&payload)?,
error: None,
})
}
let script_body = match fs::read_to_string(&script_path) {
Ok(value) => value,
Err(err) => {
return Ok(failed_tool_result(format!(
"failed to read browser script {}: {err}",
script_path.display()
)))
}
};
let wrapped_script = wrap_browser_script(&script_body, &Value::Object(args.clone()));
let result = match browser_tool.invoke(
Action::Eval,
json!({ "script": wrapped_script }),
&expected_domain,
) {
Ok(result) => result,
Err(err) => return Ok(failed_tool_result(err.to_string())),
};
if !result.success {
return Ok(failed_tool_result(format_browser_script_error(&result.data)));
}
let payload = result
.data
.get("text")
.cloned()
.unwrap_or_else(|| result.data.clone());
Ok(ToolResult {
success: true,
output: stringify_tool_payload(&payload)?,
error: None,
})
}
pub fn build_browser_script_skill_tools<T: Transport + 'static>(
@@ -213,6 +219,32 @@ fn wrap_browser_script(script_body: &str, args: &Value) -> String {
)
}
fn resolve_browser_script_path(skill_root: &Path, command: &str) -> anyhow::Result<PathBuf> {
let script_path = PathBuf::from(command);
let script_path = if script_path.is_absolute() {
script_path
} else {
skill_root.join(script_path)
};
let canonical_skill_root = skill_root
.canonicalize()
.unwrap_or_else(|_| skill_root.to_path_buf());
let canonical_script_path = script_path.canonicalize().map_err(|err| {
anyhow::anyhow!(
"failed to resolve browser script {}: {err}",
script_path.display()
)
})?;
if !canonical_script_path.starts_with(&canonical_skill_root) {
anyhow::bail!(
"browser script path escapes skill root: {}",
canonical_script_path.display()
);
}
Ok(canonical_script_path)
}
fn stringify_tool_payload(payload: &Value) -> anyhow::Result<String> {
Ok(match payload {
Value::String(value) => value.clone(),

View File

@@ -0,0 +1,189 @@
use std::path::Path;
use reqwest::Url;
use serde_json::{Map, Value};
use zeroclaw::skills::load_skills_from_directory;
use crate::compat::browser_script_skill_tool::execute_browser_script_tool;
use crate::compat::config_adapter::resolve_skills_dir_from_sgclaw_settings;
use crate::compat::runtime::CompatTaskContext;
use crate::config::SgClawSettings;
use crate::pipe::{BrowserPipeTool, PipeError, Transport};
pub fn execute_direct_submit_skill<T: Transport + 'static>(
browser_tool: BrowserPipeTool<T>,
instruction: &str,
task_context: &CompatTaskContext,
workspace_root: &Path,
settings: &SgClawSettings,
) -> Result<String, PipeError> {
let configured_tool = settings
.direct_submit_skill
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty())
.ok_or_else(|| PipeError::Protocol("direct submit skill is not configured".to_string()))?;
let (skill_name, tool_name) = parse_configured_tool_name(configured_tool)?;
let expected_domain = derive_expected_domain(task_context)?;
let period = derive_period(instruction)?;
let skills_dir = resolve_skills_dir_from_sgclaw_settings(workspace_root, settings);
let skills = load_skills_from_directory(&skills_dir, true);
let skill = skills
.iter()
.find(|skill| skill.name == skill_name)
.ok_or_else(|| {
PipeError::Protocol(format!(
"direct submit skill {skill_name} was not found in {}",
skills_dir.display()
))
})?;
let tool = skill
.tools
.iter()
.find(|tool| tool.name == tool_name)
.ok_or_else(|| {
PipeError::Protocol(format!(
"direct submit tool {configured_tool} was not found"
))
})?;
if tool.kind != "browser_script" {
return Err(PipeError::Protocol(format!(
"direct submit tool {configured_tool} must be browser_script, got {}",
tool.kind
)));
}
let skill_root = skill
.location
.as_deref()
.and_then(Path::parent)
.ok_or_else(|| {
PipeError::Protocol(format!(
"direct submit skill {skill_name} is missing a resolvable location"
))
})?;
let mut args = Map::new();
args.insert("expected_domain".to_string(), Value::String(expected_domain));
args.insert("period".to_string(), Value::String(period));
let runtime = tokio::runtime::Runtime::new()
.map_err(|err| PipeError::Protocol(format!("failed to create tokio runtime: {err}")))?;
let result = runtime
.block_on(execute_browser_script_tool(
tool,
skill_root,
browser_tool,
Value::Object(args),
))
.map_err(|err| PipeError::Protocol(err.to_string()))?;
if result.success {
Ok(result.output)
} else {
Err(PipeError::Protocol(
result
.error
.unwrap_or_else(|| "direct submit skill execution failed".to_string()),
))
}
}
fn parse_configured_tool_name(configured_tool: &str) -> Result<(&str, &str), PipeError> {
let (skill_name, tool_name) = configured_tool.split_once('.').ok_or_else(|| {
PipeError::Protocol(format!(
"direct submit skill must use skill.tool format, got {configured_tool}"
))
})?;
let skill_name = skill_name.trim();
let tool_name = tool_name.trim();
if skill_name.is_empty() || tool_name.is_empty() {
return Err(PipeError::Protocol(format!(
"direct submit skill must use skill.tool format, got {configured_tool}"
)));
}
Ok((skill_name, tool_name))
}
fn derive_expected_domain(task_context: &CompatTaskContext) -> Result<String, PipeError> {
let page_url = task_context
.page_url
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty())
.ok_or_else(|| {
PipeError::Protocol(
"direct submit skill requires page_url so expected_domain can be derived"
.to_string(),
)
})?;
Url::parse(page_url)
.ok()
.and_then(|url| url.host_str().map(|host| host.to_ascii_lowercase()))
.ok_or_else(|| {
PipeError::Protocol(format!(
"direct submit skill could not derive expected_domain from page_url {page_url:?}"
))
})
}
fn derive_period(instruction: &str) -> Result<String, PipeError> {
let chars = instruction.chars().collect::<Vec<_>>();
if chars.len() < 7 {
return Err(PipeError::Protocol(
"direct submit skill requires an explicit YYYY-MM period in the instruction"
.to_string(),
));
}
for start in 0..=chars.len() - 7 {
let candidate = chars[start..start + 7].iter().collect::<String>();
if is_year_month(&candidate) {
return Ok(candidate);
}
}
Err(PipeError::Protocol(
"direct submit skill requires an explicit YYYY-MM period in the instruction"
.to_string(),
))
}
fn is_year_month(candidate: &str) -> bool {
let bytes = candidate.as_bytes();
bytes.len() == 7
&& bytes[0..4].iter().all(u8::is_ascii_digit)
&& bytes[4] == b'-'
&& bytes[5..7].iter().all(u8::is_ascii_digit)
&& matches!((bytes[5] - b'0') * 10 + (bytes[6] - b'0'), 1..=12)
}
#[cfg(test)]
mod tests {
use super::{derive_period, is_year_month, parse_configured_tool_name};
#[test]
fn parse_configured_tool_name_requires_skill_and_tool() {
assert_eq!(
parse_configured_tool_name("fault-details-report.collect_fault_details")
.unwrap(),
("fault-details-report", "collect_fault_details")
);
assert!(parse_configured_tool_name("fault-details-report").is_err());
}
#[test]
fn derive_period_requires_explicit_year_month() {
assert_eq!(derive_period("收集 2026-03 故障明细").unwrap(), "2026-03");
assert!(derive_period("收集三月故障明细").is_err());
}
#[test]
fn year_month_validation_rejects_invalid_month() {
assert!(is_year_month("2026-12"));
assert!(!is_year_month("2026-00"));
assert!(!is_year_month("2026-13"));
}
}

View File

@@ -2,6 +2,7 @@ pub mod browser_script_skill_tool;
pub mod browser_tool_adapter;
pub mod config_adapter;
pub mod cron_adapter;
pub mod direct_skill_runtime;
pub mod event_bridge;
pub mod memory_adapter;
pub mod openxml_office_tool;