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

@@ -219,6 +219,31 @@ pub fn handle_browser_message_with_context<T: Transport + 'static>(
settings.runtime_profile, settings.skills_prompt_mode
),
});
if settings
.direct_submit_skill
.as_deref()
.map(str::trim)
.is_some_and(|value| !value.is_empty())
{
let _ = send_mode_log(transport, "direct_skill_primary");
let completion = match crate::compat::direct_skill_runtime::execute_direct_submit_skill(
browser_tool.clone(),
&instruction,
&task_context,
&context.workspace_root,
&settings,
) {
Ok(summary) => AgentMessage::TaskComplete {
success: true,
summary,
},
Err(err) => AgentMessage::TaskComplete {
success: false,
summary: err.to_string(),
},
};
return transport.send(&completion);
}
if crate::compat::orchestration::should_use_primary_orchestration(
&instruction,
task_context.page_url.as_deref(),

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;

View File

@@ -10,6 +10,10 @@ pub use zeroclaw::config::SkillsPromptInjectionMode as SkillsPromptMode;
const DEFAULT_DEEPSEEK_BASE_URL: &str = "https://api.deepseek.com";
const DEFAULT_DEEPSEEK_MODEL: &str = "deepseek-chat";
const DEFAULT_PROVIDER_ID: &str = "deepseek";
const DIRECT_SUBMIT_PROVIDER_ID: &str = "direct-submit";
const DIRECT_SUBMIT_BASE_URL: &str = "http://127.0.0.1/direct-submit";
const DIRECT_SUBMIT_MODEL: &str = "direct-submit-placeholder-model";
const DIRECT_SUBMIT_API_KEY: &str = "direct-submit-placeholder-key";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlannerMode {
@@ -66,6 +70,19 @@ impl ProviderSettings {
})
}
fn direct_submit_placeholder() -> Self {
Self {
id: DIRECT_SUBMIT_PROVIDER_ID.to_string(),
provider: DIRECT_SUBMIT_PROVIDER_ID.to_string(),
api_key: DIRECT_SUBMIT_API_KEY.to_string(),
base_url: Some(DIRECT_SUBMIT_BASE_URL.to_string()),
model: DIRECT_SUBMIT_MODEL.to_string(),
api_path: None,
wire_api: None,
requires_openai_auth: false,
}
}
fn from_raw(raw: RawProviderSettings) -> Result<Self, ConfigError> {
let id = raw.id.trim().to_string();
if id.is_empty() {
@@ -125,6 +142,7 @@ pub struct SgClawSettings {
pub provider_base_url: String,
pub provider_model: String,
pub skills_dir: Option<PathBuf>,
pub direct_submit_skill: Option<String>,
pub skills_prompt_mode: SkillsPromptMode,
pub runtime_profile: RuntimeProfile,
pub planner_mode: PlannerMode,
@@ -163,6 +181,7 @@ impl SgClawSettings {
None,
None,
None,
None,
Vec::new(),
None,
None,
@@ -198,6 +217,7 @@ impl SgClawSettings {
None,
None,
None,
None,
Vec::new(),
None,
None,
@@ -278,6 +298,7 @@ impl SgClawSettings {
config.base_url,
config.model,
resolve_configured_skills_dir(config.skills_dir, config_dir),
config.direct_submit_skill,
skills_prompt_mode,
runtime_profile,
planner_mode,
@@ -294,6 +315,7 @@ impl SgClawSettings {
base_url: String,
model: String,
skills_dir: Option<PathBuf>,
direct_submit_skill: Option<String>,
skills_prompt_mode: Option<SkillsPromptMode>,
runtime_profile: Option<RuntimeProfile>,
planner_mode: Option<PlannerMode>,
@@ -302,10 +324,15 @@ impl SgClawSettings {
browser_backend: Option<BrowserBackend>,
office_backend: Option<OfficeBackend>,
) -> Result<Self, ConfigError> {
let direct_submit_skill = normalize_direct_submit_skill(direct_submit_skill)?;
let providers = if providers.is_empty() {
vec![ProviderSettings::from_legacy_deepseek(
api_key, base_url, model,
)?]
if direct_submit_skill.is_some() {
vec![ProviderSettings::direct_submit_placeholder()]
} else {
vec![ProviderSettings::from_legacy_deepseek(
api_key, base_url, model,
)?]
}
} else {
providers
};
@@ -329,6 +356,7 @@ impl SgClawSettings {
.unwrap_or_default(),
provider_model: active_provider_settings.model.clone(),
skills_dir,
direct_submit_skill,
skills_prompt_mode: skills_prompt_mode.unwrap_or(SkillsPromptMode::Compact),
runtime_profile: runtime_profile.unwrap_or(RuntimeProfile::BrowserAttached),
planner_mode: planner_mode.unwrap_or(PlannerMode::ZeroclawPlanFirst),
@@ -447,6 +475,29 @@ fn normalize_optional_value(raw: Option<String>) -> Option<String> {
.filter(|value| !value.is_empty())
}
fn normalize_direct_submit_skill(raw: Option<String>) -> Result<Option<String>, ConfigError> {
let value = normalize_optional_value(raw);
let Some(value) = value.as_deref() else {
return Ok(None);
};
let Some((skill_name, tool_name)) = value.split_once('.') else {
return Err(ConfigError::InvalidValue(
"directSubmitSkill",
format!("must use skill.tool format, got {value}"),
));
};
if skill_name.trim().is_empty() || tool_name.trim().is_empty() {
return Err(ConfigError::InvalidValue(
"directSubmitSkill",
format!("must use skill.tool format, got {value}"),
));
}
Ok(Some(value.to_string()))
}
fn normalize_base_url(raw: String) -> String {
let trimmed = raw.trim();
if trimmed.is_empty() {
@@ -483,6 +534,8 @@ struct RawSgClawSettings {
model: String,
#[serde(rename = "skillsDir", alias = "skills_dir", default)]
skills_dir: Option<String>,
#[serde(rename = "directSubmitSkill", alias = "direct_submit_skill", default)]
direct_submit_skill: Option<String>,
#[serde(rename = "skillsPromptMode", alias = "skills_prompt_mode", default)]
skills_prompt_mode: Option<String>,
#[serde(rename = "runtimeProfile", alias = "runtime_profile", default)]