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:
189
src/compat/direct_skill_runtime.rs
Normal file
189
src/compat/direct_skill_runtime.rs
Normal 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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user