feat: add config-owned direct submit runtime

Keep browser-attached workflows on the configured direct-skill path and align the Zhihu export/browser regression contracts with the current ws merge state.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
木炎
2026-04-11 15:45:42 +08:00
29 changed files with 5218 additions and 585 deletions

View File

@@ -95,8 +95,18 @@ pub fn handle_browser_message_with_context<T: Transport + 'static>(
page_url: normalize_optional_submit_field(page_url),
page_title: normalize_optional_submit_field(page_title),
};
let browser_backend = browser_backend_for_submit(browser_tool, context, &request)?;
run_submit_task_with_browser_backend(transport, transport, browser_backend, context, request)
if configured_browser_ws_url(context).is_some() {
let browser_backend = browser_backend_for_submit(browser_tool, context, &request)?;
run_submit_task_with_browser_backend(
transport,
transport,
browser_backend,
context,
request,
)
} else {
run_submit_task(transport, transport, browser_tool, context, request)
}
}
BrowserMessage::Init { .. } => {
eprintln!("ignoring duplicate init after handshake");

View File

@@ -198,6 +198,37 @@ pub fn run_submit_task<T: Transport + 'static>(
settings.runtime_profile, settings.skills_prompt_mode
),
});
if settings.direct_submit_skill.is_some() {
match crate::compat::direct_skill_runtime::execute_direct_submit_skill(
browser_tool.clone(),
&instruction,
&task_context,
&context.workspace_root,
&settings,
) {
Ok(outcome) => {
let _ = send_mode_log(sink, "direct_skill_primary");
return sink.send(&AgentMessage::TaskComplete {
success: outcome.success,
summary: outcome.summary,
});
}
Err(PipeError::Protocol(message))
if message.contains("must use skill.tool format") =>
{
return sink.send(&AgentMessage::TaskComplete {
success: false,
summary: message,
});
}
Err(err) => {
return sink.send(&AgentMessage::TaskComplete {
success: false,
summary: err.to_string(),
});
}
}
}
if RuntimeEngine::new(settings.runtime_profile).browser_surface_enabled()
&& crate::compat::orchestration::should_use_primary_orchestration(
&instruction,

View File

@@ -667,7 +667,7 @@ fn normalize_callback_result(
}))
}
"eval" if result.callback == EVAL_CALLBACK_NAME => {
let value = result.payload.get("value").and_then(Value::as_str)?;
let value = result.payload.get("value")?.clone();
Some(BrowserCallbackResponse::Success(BrowserCallbackSuccess {
success: true,
data: json!({ "text": value }),
@@ -1403,4 +1403,36 @@ mod tests {
other => panic!("expected Success, got {other:?}"),
}
}
#[test]
fn normalize_callback_result_path_a_eval_accepts_structured_value_payload() {
let request = make_request("eval");
let result = CallbackResult {
callback: "sgclawOnEval".to_string(),
request_url: "http://127.0.0.1:17888/sgclaw/browser-helper.html".to_string(),
target_url: Some("https://www.zhihu.com/hot".to_string()),
action: Some("sgBrowserExcuteJsCodeByDomain".to_string()),
payload: json!({
"value": {
"source": "https://www.zhihu.com/hot",
"rows": [[1, "问题一", "344万"]]
}
}),
};
let response = normalize_callback_result(&request, result, Duration::from_millis(10));
assert!(response.is_some(), "Path A eval should accept structured values");
match response.unwrap() {
super::super::callback_backend::BrowserCallbackResponse::Success(s) => {
assert_eq!(
s.data.get("text").unwrap(),
&json!({
"source": "https://www.zhihu.com/hot",
"rows": [[1, "问题一", "344万"]]
})
);
}
other => panic!("expected Success, got {other:?}"),
}
}
}

View File

@@ -12,47 +12,15 @@ use zeroclaw::tools::{Tool, ToolResult};
use crate::browser::BrowserBackend;
use crate::pipe::Action;
pub struct BrowserScriptInvocation<'a> {
pub tool: &'a SkillTool,
pub skill_root: &'a Path,
}
pub struct BrowserScriptSkillTool {
tool_name: String,
tool_description: String,
tool: SkillTool,
skill_root: PathBuf,
script_path: PathBuf,
args: HashMap<String, String>,
browser_tool: Arc<dyn BrowserBackend>,
}
impl BrowserScriptInvocation<'_> {
fn script_path(&self) -> PathBuf {
self.skill_root.join(&self.tool.command)
}
fn canonical_script_path(&self) -> anyhow::Result<PathBuf> {
let script_path = self.script_path();
let canonical_skill_root = self
.skill_root
.canonicalize()
.unwrap_or_else(|_| self.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)
}
}
impl BrowserScriptSkillTool {
pub fn new(
skill_name: &str,
@@ -60,14 +28,13 @@ impl BrowserScriptSkillTool {
skill_root: &Path,
browser_tool: Arc<dyn BrowserBackend>,
) -> anyhow::Result<Self> {
let invocation = BrowserScriptInvocation { tool, skill_root };
invocation.canonical_script_path()?;
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(),
tool: tool.clone(),
skill_root: skill_root.to_path_buf(),
script_path,
args: tool.args.clone(),
browser_tool,
})
@@ -119,12 +86,15 @@ impl Tool for BrowserScriptSkillTool {
}
async fn execute(&self, args: Value) -> anyhow::Result<ToolResult> {
execute_browser_script_impl(
&self.tool,
&self.skill_root,
self.browser_tool.clone(),
args,
)
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(),
};
execute_browser_script_tool(&tool, &self.skill_root, self.browser_tool.as_ref(), args).await
}
}
@@ -165,20 +135,26 @@ pub fn build_browser_script_skill_tools(
pub async fn execute_browser_script_tool(
tool: &SkillTool,
skill_root: &Path,
browser_tool: Arc<dyn BrowserBackend>,
browser_tool: &dyn BrowserBackend,
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
)));
}
execute_browser_script_impl(tool, skill_root, browser_tool, args)
}
fn execute_browser_script_impl(
tool: &SkillTool,
skill_root: &Path,
browser_tool: Arc<dyn BrowserBackend>,
browser_tool: &dyn BrowserBackend,
args: Value,
) -> anyhow::Result<ToolResult> {
let invocation = BrowserScriptInvocation { tool, skill_root };
let script_path = invocation.canonical_script_path()?;
let script_path = resolve_browser_script_path(skill_root, &tool.command)?;
let mut args = match args {
Value::Object(args) => args,
@@ -263,6 +239,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,341 @@
use std::path::Path;
use reqwest::Url;
use serde_json::{Map, Value};
use zeroclaw::skills::load_skills_from_directory;
use crate::browser::PipeBrowserBackend;
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};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DirectSubmitOutcome {
pub success: bool,
pub summary: String,
}
pub fn execute_direct_submit_skill<T: Transport + 'static>(
browser_tool: BrowserPipeTool<T>,
instruction: &str,
task_context: &CompatTaskContext,
workspace_root: &Path,
settings: &SgClawSettings,
) -> Result<DirectSubmitOutcome, 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 browser_backend = PipeBrowserBackend::from_inner(browser_tool);
let result = runtime
.block_on(execute_browser_script_tool(
tool,
skill_root,
&browser_backend,
Value::Object(args),
))
.map_err(|err| PipeError::Protocol(err.to_string()))?;
if result.success {
Ok(interpret_direct_submit_output(&result.output))
} else {
Err(PipeError::Protocol(
result
.error
.unwrap_or_else(|| "direct submit skill execution failed".to_string()),
))
}
}
fn interpret_direct_submit_output(output: &str) -> DirectSubmitOutcome {
let Some(payload) = serde_json::from_str::<Value>(output).ok() else {
return DirectSubmitOutcome {
success: true,
summary: output.to_string(),
};
};
let Some(artifact) = payload.as_object() else {
return DirectSubmitOutcome {
success: true,
summary: output.to_string(),
};
};
if artifact.get("type").and_then(Value::as_str) != Some("report-artifact") {
return DirectSubmitOutcome {
success: true,
summary: output.to_string(),
};
}
let status = artifact
.get("status")
.and_then(Value::as_str)
.unwrap_or("ok");
let success = matches!(status, "ok" | "partial" | "empty");
let report_name = artifact
.get("report_name")
.and_then(Value::as_str)
.unwrap_or("report-artifact");
let period = artifact
.get("period")
.and_then(Value::as_str)
.unwrap_or("");
let detail_rows = count_rows(artifact.get("counts"), artifact.get("rows"), "detail_rows");
let summary_rows = count_summary_rows(artifact.get("counts"), artifact.get("sections"));
let partial_reasons = artifact
.get("partial_reasons")
.and_then(Value::as_array)
.map(|reasons| {
reasons
.iter()
.filter_map(Value::as_str)
.filter(|value| !value.trim().is_empty())
.collect::<Vec<_>>()
})
.unwrap_or_default();
let mut parts = vec![report_name.to_string()];
if !period.trim().is_empty() {
parts.push(period.to_string());
}
parts.push(format!("status={status}"));
parts.push(format!("detail_rows={detail_rows}"));
parts.push(format!("summary_rows={summary_rows}"));
if !partial_reasons.is_empty() {
parts.push(format!("partial_reasons={}", partial_reasons.join(",")));
}
DirectSubmitOutcome {
success,
summary: parts.join(" "),
}
}
fn count_rows(counts: Option<&Value>, rows: Option<&Value>, key: &str) -> usize {
counts
.and_then(Value::as_object)
.and_then(|counts| counts.get(key))
.and_then(Value::as_u64)
.map(|count| count as usize)
.or_else(|| rows.and_then(Value::as_array).map(Vec::len))
.unwrap_or(0)
}
fn count_summary_rows(counts: Option<&Value>, sections: Option<&Value>) -> usize {
counts
.and_then(Value::as_object)
.and_then(|counts| counts.get("summary_rows"))
.and_then(Value::as_u64)
.map(|count| count as usize)
.or_else(|| {
sections
.and_then(Value::as_array)
.and_then(|sections| {
sections.iter().find_map(|section| {
section
.as_object()
.and_then(|section| section.get("rows"))
.and_then(Value::as_array)
.map(Vec::len)
})
})
})
.unwrap_or(0)
}
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::{
count_rows, count_summary_rows, derive_period, interpret_direct_submit_output,
is_year_month, parse_configured_tool_name,
};
use serde_json::json;
#[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"));
}
#[test]
fn interpret_direct_submit_output_maps_report_artifact_statuses() {
let partial = interpret_direct_submit_output(
&json!({
"type": "report-artifact",
"report_name": "fault-details-report",
"period": "2026-03",
"counts": { "detail_rows": 1, "summary_rows": 1 },
"status": "partial",
"partial_reasons": ["report_log_failed"]
})
.to_string(),
);
assert!(partial.success);
assert!(partial.summary.contains("status=partial"));
assert!(partial.summary.contains("report_log_failed"));
let blocked = interpret_direct_submit_output(
&json!({
"type": "report-artifact",
"report_name": "fault-details-report",
"status": "blocked",
"partial_reasons": ["selected_range_unavailable"]
})
.to_string(),
);
assert!(!blocked.success);
assert!(blocked.summary.contains("status=blocked"));
}
#[test]
fn row_count_helpers_fall_back_to_payload_shapes() {
assert_eq!(
count_rows(None, Some(&json!([{ "qxdbh": "QX-1" }, { "qxdbh": "QX-2" }])), "detail_rows"),
2
);
assert_eq!(
count_summary_rows(None, Some(&json!([{ "name": "summary-sheet", "rows": [{ "index": 1 }] }]))),
1
);
}
}

View File

@@ -3,6 +3,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

@@ -4,12 +4,12 @@ use serde_json::{json, Value};
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::fs;
use std::io::Write;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use zeroclaw::tools::{Tool, ToolResult};
use zip::write::SimpleFileOptions;
use zip::write::FileOptions;
use zip::{CompressionMethod, ZipWriter};
const OPENXML_OFFICE_TOOL_NAME: &str = "openxml_office";
@@ -131,9 +131,8 @@ impl Tool for OpenXmlOfficeTool {
write_payload_json(&payload_path, &normalized_rows)?;
write_request_json(&request_path, &template_path, &payload_path, &output_path)?;
let rendered = run_openxml_cli(&request_path).or_else(|_| {
render_locally(&template_path, &payload_path, &output_path)
})?;
let rendered = run_openxml_cli(&request_path)
.or_else(|_| render_locally(&template_path, &payload_path, &output_path))?;
let artifact_path = rendered["data"]["artifact"]["path"]
.as_str()
.map(str::to_string)
@@ -163,9 +162,7 @@ fn failed_tool_result(error: String) -> ToolResult {
fn create_job_root(workspace_root: &Path) -> anyhow::Result<PathBuf> {
let nanos = SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos();
let path = workspace_root
.join(".sgclaw-openxml")
.join(format!("{nanos}"));
let path = workspace_root.join(".sgclaw-openxml").join(format!("{nanos}"));
fs::create_dir_all(&path)?;
Ok(path)
}
@@ -223,10 +220,7 @@ fn canonicalize_column_name(value: &str) -> Option<&'static str> {
}
fn reorder_row(row: &[Value], column_order: &[usize]) -> Vec<Value> {
column_order
.iter()
.map(|index| row[*index].clone())
.collect()
column_order.iter().map(|index| row[*index].clone()).collect()
}
fn write_payload_json(path: &Path, rows: &[Vec<Value>]) -> anyhow::Result<()> {
@@ -285,18 +279,8 @@ fn run_openxml_cli(request_path: &Path) -> anyhow::Result<Value> {
.parent()
.map(|path| path.join("openxml_cli").join("Cargo.toml"))
.ok_or_else(|| anyhow::anyhow!("failed to resolve openxml_cli manifest path"))?;
let binary_name = if cfg!(windows) {
"openxml-cli.exe"
} else {
"openxml-cli"
};
let binary_path = manifest_path
.parent()
.map(|path| path.join("target").join("debug").join(binary_name))
.ok_or_else(|| anyhow::anyhow!("failed to resolve openxml_cli binary path"))?;
let output = if binary_path.exists() {
Command::new(&binary_path)
let output = if let Some(binary_path) = resolve_openxml_cli_binary(&manifest_path) {
Command::new(binary_path)
.args([
"template",
"render",
@@ -358,14 +342,11 @@ fn worksheet_xml_from_xlsx(path: &Path) -> anyhow::Result<String> {
let mut archive = zip::ZipArchive::new(file)?;
let mut sheet = archive.by_name("xl/worksheets/sheet1.xml")?;
let mut xml = String::new();
std::io::Read::read_to_string(&mut sheet, &mut xml)?;
sheet.read_to_string(&mut xml)?;
Ok(xml)
}
fn render_template_xml(
template: &str,
variables: &serde_json::Map<String, Value>,
) -> String {
fn render_template_xml(template: &str, variables: &serde_json::Map<String, Value>) -> String {
let mut rendered = template.to_string();
for (key, value) in variables {
let placeholder = format!("{{{{{key}}}}}");
@@ -392,7 +373,7 @@ fn write_rendered_xlsx(
let mut archive = zip::ZipArchive::new(input)?;
let output = fs::File::create(output_path)?;
let mut writer = ZipWriter::new(output);
let options = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
let options = FileOptions::default().compression_method(CompressionMethod::Stored);
for index in 0..archive.len() {
let mut entry = archive.by_index(index)?;
@@ -416,6 +397,34 @@ fn xml_escape(value: &str) -> String {
.replace('>', "&gt;")
}
fn resolve_openxml_cli_binary(manifest_path: &Path) -> Option<PathBuf> {
let cli_dir = manifest_path.parent()?;
openxml_cli_candidate_paths(cli_dir)
.into_iter()
.find(|path| path.exists())
}
fn openxml_cli_candidate_paths(cli_dir: &Path) -> Vec<PathBuf> {
let mut paths = Vec::new();
for profile in ["release", "debug"] {
paths.push(
cli_dir
.join("target")
.join(profile)
.join(openxml_cli_binary_name()),
);
}
paths
}
fn openxml_cli_binary_name() -> &'static str {
if cfg!(windows) {
"openxml-cli.exe"
} else {
"openxml-cli"
}
}
fn value_to_string(value: &Value) -> String {
match value {
Value::String(text) => text.clone(),
@@ -427,34 +436,39 @@ fn value_to_string(value: &Value) -> String {
}
fn write_hotlist_template(path: &Path, row_count: usize) -> anyhow::Result<()> {
write_zip_file(&path, &[Content {
path: "[Content_Types].xml",
body: content_types_xml().to_string(),
},
Content {
path: "_rels/.rels",
body: root_rels_xml().to_string(),
},
Content {
path: "docProps/app.xml",
body: app_xml().to_string(),
},
Content {
path: "docProps/core.xml",
body: core_xml().to_string(),
},
Content {
path: "xl/workbook.xml",
body: workbook_xml().to_string(),
},
Content {
path: "xl/_rels/workbook.xml.rels",
body: workbook_rels_xml().to_string(),
},
Content {
path: "xl/worksheets/sheet1.xml",
body: worksheet_xml(row_count),
}])?;
write_zip_file(
&path,
&[
Content {
path: "[Content_Types].xml",
body: content_types_xml().to_string(),
},
Content {
path: "_rels/.rels",
body: root_rels_xml().to_string(),
},
Content {
path: "docProps/app.xml",
body: app_xml().to_string(),
},
Content {
path: "docProps/core.xml",
body: core_xml().to_string(),
},
Content {
path: "xl/workbook.xml",
body: workbook_xml().to_string(),
},
Content {
path: "xl/_rels/workbook.xml.rels",
body: workbook_rels_xml().to_string(),
},
Content {
path: "xl/worksheets/sheet1.xml",
body: worksheet_xml(row_count),
},
],
)?;
Ok(())
}
@@ -473,7 +487,7 @@ fn write_zip_file(path: &Path, entries: &[Content<'_>]) -> anyhow::Result<()> {
let file = fs::File::create(path)?;
let mut zip = ZipWriter::new(file);
let options = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
let options = FileOptions::default().compression_method(CompressionMethod::Stored);
for entry in entries {
zip.start_file(entry.path, options)?;
zip.write_all(entry.body.as_bytes())?;
@@ -482,6 +496,42 @@ fn write_zip_file(path: &Path, entries: &[Content<'_>]) -> anyhow::Result<()> {
Ok(())
}
#[cfg(test)]
mod tests {
use super::{openxml_cli_binary_name, openxml_cli_candidate_paths, zip_entry_name};
use std::path::Path;
#[test]
fn openxml_cli_candidates_prefer_release_before_debug() {
let paths = openxml_cli_candidate_paths(Path::new("E:\\coding\\codex\\openxml_cli"));
assert_eq!(paths.len(), 2);
assert_eq!(
paths[0],
Path::new("E:\\coding\\codex\\openxml_cli")
.join("target")
.join("release")
.join(openxml_cli_binary_name())
);
assert_eq!(
paths[1],
Path::new("E:\\coding\\codex\\openxml_cli")
.join("target")
.join("debug")
.join(openxml_cli_binary_name())
);
}
#[test]
fn zip_entry_name_normalizes_windows_separators() {
let rel = Path::new("xl\\worksheets\\sheet1.xml");
assert_eq!(zip_entry_name(rel), "xl/worksheets/sheet1.xml");
}
}
fn zip_entry_name(path: &Path) -> String {
path.to_string_lossy().replace('\\', "/")
}
fn worksheet_xml(row_count: usize) -> String {
let mut rows = Vec::new();
rows.push(

View File

@@ -2,6 +2,7 @@ use std::path::Path;
use std::sync::Arc;
use crate::browser::BrowserBackend;
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};
@@ -36,6 +37,7 @@ pub fn execute_task_with_browser_backend(
workspace_root: &Path,
settings: &SgClawSettings,
) -> Result<String, PipeError> {
let skills_dir = resolve_skills_dir_from_sgclaw_settings(workspace_root, settings);
let route = crate::compat::workflow_executor::detect_route(
instruction,
task_context.page_url.as_deref(),
@@ -47,6 +49,7 @@ pub fn execute_task_with_browser_backend(
transport,
browser_backend.clone(),
workspace_root,
&skills_dir,
instruction,
task_context,
route,
@@ -73,6 +76,7 @@ pub fn execute_task_with_browser_backend(
transport,
browser_backend,
workspace_root,
&skills_dir,
instruction,
task_context,
route,
@@ -84,6 +88,7 @@ pub fn execute_task_with_browser_backend(
transport,
browser_backend,
workspace_root,
&skills_dir,
instruction,
task_context,
route,
@@ -101,6 +106,7 @@ pub fn execute_task_with_sgclaw_settings<T: Transport + 'static>(
workspace_root: &Path,
settings: &SgClawSettings,
) -> Result<String, PipeError> {
let skills_dir = resolve_skills_dir_from_sgclaw_settings(workspace_root, settings);
let route = crate::compat::workflow_executor::detect_route(
instruction,
task_context.page_url.as_deref(),
@@ -112,6 +118,7 @@ pub fn execute_task_with_sgclaw_settings<T: Transport + 'static>(
transport,
&browser_tool,
workspace_root,
&skills_dir,
instruction,
task_context,
route,
@@ -138,6 +145,7 @@ pub fn execute_task_with_sgclaw_settings<T: Transport + 'static>(
transport,
&browser_tool,
workspace_root,
&skills_dir,
instruction,
task_context,
route,
@@ -149,6 +157,7 @@ pub fn execute_task_with_sgclaw_settings<T: Transport + 'static>(
transport,
&browser_tool,
workspace_root,
&skills_dir,
instruction,
task_context,
route,

View File

@@ -12,10 +12,10 @@ const SCREEN_HTML_EXPORT_TOOL_NAME: &str = "screen_html_export";
const DEFAULT_SCREEN_TITLE: &str = "知乎热榜主题分类分析大屏";
const TEMPLATE: &str = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../skill_lib/skills/zhihu-hotlist-screen/assets/zhihu-hotlist-echarts.html"
"/resources/zhihu-hotlist-echarts.html"
));
const PAYLOAD_START_MARKER: &str = " const defaultPayload = ";
const PAYLOAD_END_MARKER: &str = "\n\n const themeMeta = {";
const PAYLOAD_END_MARKER: &str = "const themeMeta = {";
pub struct ScreenHtmlExportTool {
workspace_root: PathBuf,

View File

@@ -132,6 +132,7 @@ pub fn execute_route_with_browser_backend(
transport: &dyn crate::agent::AgentEventSink,
browser_backend: Arc<dyn BrowserBackend>,
workspace_root: &Path,
skills_dir: &Path,
instruction: &str,
task_context: &CompatTaskContext,
route: WorkflowRoute,
@@ -140,7 +141,13 @@ pub fn execute_route_with_browser_backend(
match route {
WorkflowRoute::ZhihuHotlistExportXlsx | WorkflowRoute::ZhihuHotlistScreen => {
let top_n = extract_top_n(instruction);
let items = collect_hotlist_items(transport, browser_backend.as_ref(), top_n, task_context)?;
let items = collect_hotlist_items(
transport,
browser_backend.as_ref(),
skills_dir,
top_n,
task_context,
)?;
if items.is_empty() {
return Err(PipeError::Protocol(
"知乎热榜采集失败:未能从页面文本中解析到热榜条目".to_string(),
@@ -155,11 +162,12 @@ pub fn execute_route_with_browser_backend(
}
}
WorkflowRoute::ZhihuArticleEntry => {
execute_zhihu_article_entry_route(transport, browser_backend.as_ref())
execute_zhihu_article_entry_route(transport, browser_backend.as_ref(), skills_dir)
}
WorkflowRoute::ZhihuArticleDraft => execute_zhihu_article_route(
transport,
browser_backend.as_ref(),
skills_dir,
instruction,
task_context,
false,
@@ -169,6 +177,7 @@ pub fn execute_route_with_browser_backend(
WorkflowRoute::ZhihuArticlePublish => execute_zhihu_article_route(
transport,
browser_backend.as_ref(),
skills_dir,
instruction,
task_context,
true,
@@ -179,6 +188,7 @@ pub fn execute_route_with_browser_backend(
execute_generated_zhihu_article_publish_route(
transport,
browser_backend.as_ref(),
skills_dir,
instruction,
task_context,
workspace_root,
@@ -192,6 +202,7 @@ pub fn execute_route<T: Transport + 'static>(
transport: &T,
browser_tool: &BrowserPipeTool<T>,
workspace_root: &Path,
skills_dir: &Path,
instruction: &str,
task_context: &CompatTaskContext,
route: WorkflowRoute,
@@ -203,6 +214,7 @@ pub fn execute_route<T: Transport + 'static>(
transport,
browser_backend,
workspace_root,
skills_dir,
instruction,
task_context,
route,
@@ -213,10 +225,13 @@ pub fn execute_route<T: Transport + 'static>(
fn collect_hotlist_items(
transport: &dyn crate::agent::AgentEventSink,
browser_tool: &dyn BrowserBackend,
skills_dir: &Path,
top_n: usize,
task_context: &CompatTaskContext,
) -> Result<Vec<HotlistItem>, PipeError> {
if let Some(items) = ensure_hotlist_page_ready(transport, browser_tool, top_n, task_context)? {
if let Some(items) =
ensure_hotlist_page_ready(transport, browser_tool, skills_dir, top_n, task_context)?
{
return Ok(items);
}
transport.send(&AgentMessage::LogEntry {
@@ -225,7 +240,7 @@ fn collect_hotlist_items(
})?;
let response = browser_tool.invoke(
Action::Eval,
json!({ "script": load_hotlist_extractor_script(top_n)? }),
json!({ "script": load_hotlist_extractor_script(skills_dir, top_n)? }),
ZHIHU_DOMAIN,
)?;
if !response.success {
@@ -246,6 +261,7 @@ fn collect_hotlist_items(
fn ensure_hotlist_page_ready(
transport: &dyn crate::agent::AgentEventSink,
browser_tool: &dyn BrowserBackend,
skills_dir: &Path,
top_n: usize,
task_context: &CompatTaskContext,
) -> Result<Option<Vec<HotlistItem>>, PipeError> {
@@ -268,7 +284,7 @@ fn ensure_hotlist_page_ready(
// Best-effort wait for content to appear; ignore the boolean result
// we always follow up with the probe.
let _ = poll_for_hotlist_readiness(browser_tool);
if let Some(items) = probe_hotlist_extractor(transport, browser_tool, top_n)? {
if let Some(items) = probe_hotlist_extractor(transport, browser_tool, skills_dir, top_n)? {
return Ok(Some(items));
}
}
@@ -277,7 +293,7 @@ fn ensure_hotlist_page_ready(
for attempt in 0..2 {
navigate_hotlist_page(transport, browser_tool)?;
let _ = poll_for_hotlist_readiness(browser_tool);
if let Some(items) = probe_hotlist_extractor(transport, browser_tool, top_n)? {
if let Some(items) = probe_hotlist_extractor(transport, browser_tool, skills_dir, top_n)? {
return Ok(Some(items));
}
last_error = Some(format!(
@@ -304,6 +320,7 @@ fn ensure_hotlist_page_ready(
/// reports "editor_unavailable".
fn poll_for_editor_readiness(
browser_tool: &dyn BrowserBackend,
skills_dir: &Path,
desired_mode: &str,
) -> Result<Value, PipeError> {
let args = json!({ "desired_mode": desired_mode });
@@ -312,6 +329,7 @@ fn poll_for_editor_readiness(
for attempt in 0..EDITOR_READY_POLL_ATTEMPTS {
match execute_browser_skill_script(
browser_tool,
skills_dir,
"zhihu-write",
"prepare_article_editor.js",
args.clone(),
@@ -325,9 +343,7 @@ fn poll_for_editor_readiness(
last_state = Some(state);
}
Err(PipeError::PipeClosed) => return Err(PipeError::PipeClosed),
Err(_) => {
// Script may fail while the page is still navigating; tolerate.
}
Err(_) => {}
}
if attempt + 1 < EDITOR_READY_POLL_ATTEMPTS {
@@ -335,12 +351,11 @@ fn poll_for_editor_readiness(
}
}
// Return the last observed state so the caller can surface the
// "editor_unavailable" message; or make one final attempt.
match last_state {
Some(state) => Ok(state),
None => execute_browser_skill_script(
browser_tool,
skills_dir,
"zhihu-write",
"prepare_article_editor.js",
args,
@@ -352,6 +367,7 @@ fn poll_for_editor_readiness(
fn probe_hotlist_extractor(
transport: &dyn crate::agent::AgentEventSink,
browser_tool: &dyn BrowserBackend,
skills_dir: &Path,
top_n: usize,
) -> Result<Option<Vec<HotlistItem>>, PipeError> {
transport.send(&AgentMessage::LogEntry {
@@ -360,7 +376,7 @@ fn probe_hotlist_extractor(
})?;
let response = browser_tool.invoke(
Action::Eval,
json!({ "script": load_hotlist_extractor_script(top_n)? }),
json!({ "script": load_hotlist_extractor_script(skills_dir, top_n)? }),
ZHIHU_DOMAIN,
)?;
if !response.success {
@@ -535,6 +551,7 @@ pub fn finalize_screen_export(
fn execute_zhihu_article_route(
transport: &dyn crate::agent::AgentEventSink,
browser_tool: &dyn BrowserBackend,
skills_dir: &Path,
instruction: &str,
task_context: &CompatTaskContext,
publish_mode: bool,
@@ -559,6 +576,7 @@ fn execute_zhihu_article_route(
})?;
let creator_state = execute_browser_skill_script(
browser_tool,
skills_dir,
"zhihu-navigate",
"open_creator_entry.js",
json!({ "desired_target": "article_editor" }),
@@ -582,6 +600,7 @@ fn execute_zhihu_article_route(
})?;
let editor_state = poll_for_editor_readiness(
browser_tool,
skills_dir,
if publish_mode { "publish" } else { "draft" },
)?;
if is_login_required_payload(&editor_state) {
@@ -600,10 +619,11 @@ fn execute_zhihu_article_route(
message: "call zhihu-write.fill_article_draft".to_string(),
})?;
let fill_result = if browser_tool.supports_live_input() {
execute_zhihu_fill_via_live_input(browser_tool, &article, publish_mode)?
execute_zhihu_fill_via_live_input(browser_tool, skills_dir, &article, publish_mode)?
} else {
execute_browser_skill_script(
browser_tool,
skills_dir,
"zhihu-write",
"fill_article_draft.js",
json!({
@@ -641,6 +661,7 @@ fn execute_zhihu_article_route(
fn execute_generated_zhihu_article_publish_route(
transport: &dyn crate::agent::AgentEventSink,
browser_tool: &dyn BrowserBackend,
skills_dir: &Path,
instruction: &str,
task_context: &CompatTaskContext,
workspace_root: &Path,
@@ -661,6 +682,7 @@ fn execute_generated_zhihu_article_publish_route(
execute_zhihu_article_route(
transport,
browser_tool,
skills_dir,
instruction,
task_context,
true,
@@ -701,6 +723,7 @@ fn task_requests_zhihu_generated_article_publish(
fn execute_zhihu_article_entry_route(
transport: &dyn crate::agent::AgentEventSink,
browser_tool: &dyn BrowserBackend,
skills_dir: &Path,
) -> Result<String, PipeError> {
navigate_zhihu_page(transport, browser_tool, ZHIHU_CREATOR_URL)?;
transport.send(&AgentMessage::LogEntry {
@@ -709,6 +732,7 @@ fn execute_zhihu_article_entry_route(
})?;
let creator_state = execute_browser_skill_script(
browser_tool,
skills_dir,
"zhihu-navigate",
"open_creator_entry.js",
json!({ "desired_target": "article_editor" }),
@@ -730,10 +754,7 @@ fn execute_zhihu_article_entry_route(
level: "info".to_string(),
message: "call zhihu-write.prepare_article_editor".to_string(),
})?;
let editor_state = poll_for_editor_readiness(
browser_tool,
"draft",
)?;
let editor_state = poll_for_editor_readiness(browser_tool, skills_dir, "draft")?;
if is_login_required_payload(&editor_state) {
return Ok(build_login_block_message(payload_current_url(
&editor_state,
@@ -748,8 +769,9 @@ fn execute_zhihu_article_entry_route(
)))
}
fn load_hotlist_extractor_script(top_n: usize) -> Result<String, PipeError> {
fn load_hotlist_extractor_script(skills_dir: &Path, top_n: usize) -> Result<String, PipeError> {
load_browser_skill_script(
skills_dir,
"zhihu-hotlist",
"extract_hotlist.js",
json!({ "top_n": top_n.to_string() }),
@@ -834,12 +856,14 @@ fn navigate_zhihu_page(
fn execute_browser_skill_script(
browser_tool: &dyn BrowserBackend,
skills_dir: &Path,
skill_name: &str,
script_name: &str,
args: Value,
expected_domain: &str,
) -> Result<Value, PipeError> {
let wrapped_script = load_browser_skill_script(skill_name, script_name, args)?;
let wrapped_script =
load_browser_skill_script(skills_dir, skill_name, script_name, args)?;
let response = browser_tool.invoke(
Action::Eval,
json!({ "script": wrapped_script }),
@@ -866,6 +890,7 @@ fn live_input_probe_script(selector_candidates: &[&str]) -> String {
fn execute_zhihu_fill_via_live_input(
browser_tool: &dyn BrowserBackend,
skills_dir: &Path,
article: &ArticleDraft,
publish_mode: bool,
) -> Result<Value, PipeError> {
@@ -1003,6 +1028,7 @@ return JSON.stringify({{status:'ok',chunks:chunks.length}});
// enable the button after the content fill updates the editor state.
let fill_result = execute_browser_skill_script(
browser_tool,
skills_dir,
"zhihu-write",
"fill_article_draft.js",
json!({
@@ -1107,6 +1133,10 @@ mod tests {
.unwrap()
}
fn test_skills_dir() -> &'static Path {
Path::new("D:/data/ideaSpace/rust/sgClaw/claw/claw/skills")
}
struct MockWorkflowTransport {
sent: Mutex<Vec<AgentMessage>>,
responses: Mutex<VecDeque<BrowserMessage>>,
@@ -1266,6 +1296,7 @@ mod tests {
transport.as_ref(),
backend.clone(),
Path::new("."),
test_skills_dir(),
"打开知乎写文章页面",
&CompatTaskContext::default(),
WorkflowRoute::ZhihuArticleEntry,
@@ -1286,6 +1317,7 @@ mod tests {
Action::Eval,
json!({
"script": load_browser_skill_script(
test_skills_dir(),
"zhihu-navigate",
"open_creator_entry.js",
json!({ "desired_target": "article_editor" })
@@ -1298,6 +1330,7 @@ mod tests {
Action::Eval,
json!({
"script": load_browser_skill_script(
test_skills_dir(),
"zhihu-write",
"prepare_article_editor.js",
json!({ "desired_mode": "draft" })
@@ -1370,6 +1403,7 @@ mod tests {
transport.as_ref(),
backend.clone(),
Path::new("."),
test_skills_dir(),
"打开知乎写文章页面",
&CompatTaskContext::default(),
WorkflowRoute::ZhihuArticleEntry,
@@ -1390,6 +1424,7 @@ mod tests {
Action::Eval,
json!({
"script": load_browser_skill_script(
test_skills_dir(),
"zhihu-navigate",
"open_creator_entry.js",
json!({ "desired_target": "article_editor" })
@@ -1407,6 +1442,7 @@ mod tests {
Action::Eval,
json!({
"script": load_browser_skill_script(
test_skills_dir(),
"zhihu-write",
"prepare_article_editor.js",
json!({ "desired_mode": "draft" })
@@ -1495,6 +1531,7 @@ mod tests {
let summary = execute_zhihu_article_route(
transport.as_ref(),
backend.as_ref(),
test_skills_dir(),
"标题:测试标题\n正文:第一段内容",
&CompatTaskContext::default(),
false,
@@ -1625,6 +1662,7 @@ mod tests {
let summary = execute_zhihu_article_route(
transport.as_ref(),
backend.as_ref(),
test_skills_dir(),
"标题:测试标题\n正文:第一段内容",
&CompatTaskContext::default(),
false,
@@ -1655,6 +1693,7 @@ mod tests {
assert_eq!(invocations[8].0, Action::Eval);
assert_eq!(invocations[8].1["script"], json!(
load_browser_skill_script(
test_skills_dir(),
"zhihu-write",
"fill_article_draft.js",
json!({
@@ -1753,6 +1792,7 @@ mod tests {
let _ = execute_zhihu_article_route(
transport.as_ref(),
backend.as_ref(),
test_skills_dir(),
"标题:测试标题\n正文:第一段内容\n第二段内容",
&CompatTaskContext::default(),
false,
@@ -1771,6 +1811,7 @@ mod tests {
#[test]
fn zhihu_fill_script_checks_live_input_before_dom_fill_fallback() {
let script = load_browser_skill_script(
test_skills_dir(),
"zhihu-write",
"fill_article_draft.js",
json!({
@@ -1805,6 +1846,7 @@ mod tests {
#[test]
fn zhihu_fill_script_live_input_uses_editor_content_instead_of_whole_page_text() {
let script = load_browser_skill_script(
test_skills_dir(),
"zhihu-write",
"fill_article_draft.js",
json!({
@@ -1897,6 +1939,7 @@ mod tests {
transport.as_ref(),
backend.clone(),
Path::new("."),
test_skills_dir(),
"打开知乎写文章页面",
&CompatTaskContext::default(),
WorkflowRoute::ZhihuArticleEntry,
@@ -1917,6 +1960,7 @@ mod tests {
Action::Eval,
json!({
"script": load_browser_skill_script(
test_skills_dir(),
"zhihu-navigate",
"open_creator_entry.js",
json!({ "desired_target": "article_editor" })
@@ -1934,6 +1978,7 @@ mod tests {
Action::Eval,
json!({
"script": load_browser_skill_script(
test_skills_dir(),
"zhihu-write",
"prepare_article_editor.js",
json!({ "desired_mode": "draft" })
@@ -1975,7 +2020,13 @@ mod tests {
};
let browser_backend = PipeBrowserBackend::from_inner(browser_tool);
let items = collect_hotlist_items(transport.as_ref(), &browser_backend, 10, &task_context)
let items = collect_hotlist_items(
transport.as_ref(),
&browser_backend,
test_skills_dir(),
10,
&task_context,
)
.expect("hotlist collection should succeed");
assert_eq!(items.len(), 2);
@@ -2029,7 +2080,13 @@ mod tests {
};
let browser_backend = PipeBrowserBackend::from_inner(browser_tool);
let items = collect_hotlist_items(transport.as_ref(), &browser_backend, 10, &task_context)
let items = collect_hotlist_items(
transport.as_ref(),
&browser_backend,
test_skills_dir(),
10,
&task_context,
)
.expect("hotlist collection should succeed after readiness polling");
assert_eq!(items.len(), 1);
@@ -2098,7 +2155,13 @@ mod tests {
};
let browser_backend = PipeBrowserBackend::from_inner(browser_tool);
let items = collect_hotlist_items(transport.as_ref(), &browser_backend, 10, &task_context)
let items = collect_hotlist_items(
transport.as_ref(),
&browser_backend,
test_skills_dir(),
10,
&task_context,
)
.expect("hotlist collection should succeed after one navigation retry");
assert_eq!(items.len(), 1);
@@ -2165,7 +2228,13 @@ mod tests {
};
let browser_backend = PipeBrowserBackend::from_inner(browser_tool);
let items = collect_hotlist_items(transport.as_ref(), &browser_backend, 10, &task_context)
let items = collect_hotlist_items(
transport.as_ref(),
&browser_backend,
test_skills_dir(),
10,
&task_context,
)
.expect("hotlist collection should succeed via extractor probe");
assert_eq!(items.len(), 1);
@@ -2184,15 +2253,12 @@ mod tests {
}
fn load_browser_skill_script(
skills_dir: &Path,
skill_name: &str,
script_name: &str,
args: Value,
) -> Result<String, PipeError> {
let script_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap_or_else(|| Path::new(env!("CARGO_MANIFEST_DIR")))
.join("skill_lib")
.join("skills")
let script_path = skills_dir
.join(skill_name)
.join("scripts")
.join(script_name);

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,
@@ -165,6 +183,7 @@ impl SgClawSettings {
None,
None,
None,
None,
Vec::new(),
None,
None,
@@ -202,6 +221,7 @@ impl SgClawSettings {
None,
None,
None,
None,
Vec::new(),
None,
None,
@@ -284,6 +304,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,
@@ -302,6 +323,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>,
@@ -312,10 +334,15 @@ impl SgClawSettings {
browser_ws_url: Option<String>,
service_ws_listen_addr: Option<String>,
) -> 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
};
@@ -339,6 +366,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),
@@ -452,6 +480,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() {
@@ -488,6 +539,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)]