sgclaw: move runtime policy into config
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use futures_util::{stream, StreamExt};
|
||||
use zeroclaw::agent::dispatcher::NativeToolDispatcher;
|
||||
use zeroclaw::agent::{Agent, TurnEvent};
|
||||
use zeroclaw::agent::TurnEvent;
|
||||
use zeroclaw::config::Config as ZeroClawConfig;
|
||||
use zeroclaw::observability::{NoopObserver, Observer};
|
||||
use zeroclaw::providers::{
|
||||
self, ChatMessage, ChatRequest, ChatResponse, Provider,
|
||||
};
|
||||
@@ -14,12 +11,17 @@ use zeroclaw::providers::traits::{
|
||||
ProviderCapabilities, StreamEvent, StreamOptions, StreamResult,
|
||||
};
|
||||
|
||||
use crate::compat::browser_tool_adapter::{ZeroClawBrowserTool, BROWSER_ACTION_TOOL_NAME};
|
||||
use crate::compat::config_adapter::build_zeroclaw_config_from_settings;
|
||||
use crate::config::DeepSeekSettings;
|
||||
use crate::compat::browser_tool_adapter::ZeroClawBrowserTool;
|
||||
use crate::compat::config_adapter::{
|
||||
build_zeroclaw_config_from_sgclaw_settings,
|
||||
resolve_skills_dir_from_sgclaw_settings,
|
||||
};
|
||||
use crate::compat::openxml_office_tool::OpenXmlOfficeTool;
|
||||
use crate::compat::screen_html_export_tool::ScreenHtmlExportTool;
|
||||
use crate::config::{DeepSeekSettings, OfficeBackend, SgClawSettings};
|
||||
use crate::compat::event_bridge::log_entry_for_turn_event;
|
||||
use crate::compat::memory_adapter::build_memory;
|
||||
use crate::pipe::{BrowserPipeTool, ConversationMessage, PipeError, Transport};
|
||||
use crate::runtime::RuntimeEngine;
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct CompatTaskContext {
|
||||
@@ -37,7 +39,27 @@ pub fn execute_task<T: Transport + 'static>(
|
||||
workspace_root: &Path,
|
||||
settings: &DeepSeekSettings,
|
||||
) -> Result<String, PipeError> {
|
||||
let config = build_zeroclaw_config_from_settings(workspace_root, settings);
|
||||
let sgclaw_settings = SgClawSettings::from(settings);
|
||||
execute_task_with_sgclaw_settings(
|
||||
transport,
|
||||
browser_tool,
|
||||
instruction,
|
||||
task_context,
|
||||
workspace_root,
|
||||
&sgclaw_settings,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn execute_task_with_sgclaw_settings<T: Transport + 'static>(
|
||||
transport: &T,
|
||||
browser_tool: BrowserPipeTool<T>,
|
||||
instruction: &str,
|
||||
task_context: &CompatTaskContext,
|
||||
workspace_root: &Path,
|
||||
settings: &SgClawSettings,
|
||||
) -> Result<String, PipeError> {
|
||||
let config = build_zeroclaw_config_from_sgclaw_settings(workspace_root, settings);
|
||||
let skills_dir = resolve_skills_dir_from_sgclaw_settings(workspace_root, settings);
|
||||
let provider = build_provider(&config)?;
|
||||
let runtime = tokio::runtime::Runtime::new()
|
||||
.map_err(|err| PipeError::Protocol(format!("failed to create tokio runtime: {err}")))?;
|
||||
@@ -49,6 +71,8 @@ pub fn execute_task<T: Transport + 'static>(
|
||||
instruction,
|
||||
task_context,
|
||||
config,
|
||||
skills_dir,
|
||||
settings.clone(),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -59,8 +83,58 @@ pub async fn execute_task_with_provider<T: Transport + 'static>(
|
||||
instruction: &str,
|
||||
task_context: &CompatTaskContext,
|
||||
config: ZeroClawConfig,
|
||||
skills_dir: PathBuf,
|
||||
settings: SgClawSettings,
|
||||
) -> Result<String, PipeError> {
|
||||
let mut agent = build_agent(browser_tool, provider, &config)?;
|
||||
let engine = RuntimeEngine::new(settings.runtime_profile);
|
||||
let browser_surface_present = engine.browser_surface_enabled();
|
||||
if let Some(preview) = crate::agent::planner::build_execution_preview(
|
||||
settings.planner_mode,
|
||||
instruction,
|
||||
task_context.page_url.as_deref(),
|
||||
task_context.page_title.as_deref(),
|
||||
) {
|
||||
let mut message = preview.summary;
|
||||
if !preview.steps.is_empty() {
|
||||
message.push('\n');
|
||||
message.push_str(&preview.steps.join("\n"));
|
||||
}
|
||||
transport.send(&crate::pipe::AgentMessage::LogEntry {
|
||||
level: "plan".to_string(),
|
||||
message,
|
||||
})?;
|
||||
}
|
||||
let loaded_skill_names = engine.loaded_skill_names(&config, &skills_dir);
|
||||
if !loaded_skill_names.is_empty() {
|
||||
transport.send(&crate::pipe::AgentMessage::LogEntry {
|
||||
level: "info".to_string(),
|
||||
message: format!("loaded skills: {}", loaded_skill_names.join(", ")),
|
||||
})?;
|
||||
}
|
||||
let mut tools: Vec<Box<dyn zeroclaw::tools::Tool>> = if browser_surface_present {
|
||||
vec![
|
||||
Box::new(ZeroClawBrowserTool::new_superrpa(browser_tool.clone())),
|
||||
Box::new(ZeroClawBrowserTool::new(browser_tool)),
|
||||
]
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
if matches!(settings.office_backend, OfficeBackend::OpenXml) &&
|
||||
engine.should_attach_openxml_office_tool(instruction)
|
||||
{
|
||||
tools.push(Box::new(OpenXmlOfficeTool::new(config.workspace_dir.clone())));
|
||||
}
|
||||
if engine.should_attach_screen_html_export_tool(instruction) {
|
||||
tools.push(Box::new(ScreenHtmlExportTool::new(config.workspace_dir.clone())));
|
||||
}
|
||||
let mut agent = engine.build_agent(
|
||||
provider,
|
||||
&config,
|
||||
&skills_dir,
|
||||
tools,
|
||||
browser_surface_present,
|
||||
instruction,
|
||||
)?;
|
||||
if let Some(conversation_id) = task_context
|
||||
.conversation_id
|
||||
.as_deref()
|
||||
@@ -70,13 +144,19 @@ pub async fn execute_task_with_provider<T: Transport + 'static>(
|
||||
agent.set_memory_session_id(Some(conversation_id.to_string()));
|
||||
}
|
||||
|
||||
let seed_messages = build_seed_history(task_context);
|
||||
let mut seed_messages = Vec::new();
|
||||
seed_messages.extend(build_seed_history(task_context));
|
||||
if !seed_messages.is_empty() {
|
||||
agent.seed_history(&seed_messages);
|
||||
}
|
||||
|
||||
let (event_tx, mut event_rx) = tokio::sync::mpsc::channel::<TurnEvent>(32);
|
||||
let instruction = instruction.to_string();
|
||||
let instruction = engine.build_instruction(
|
||||
instruction,
|
||||
task_context.page_url.as_deref(),
|
||||
task_context.page_title.as_deref(),
|
||||
browser_surface_present,
|
||||
);
|
||||
|
||||
let task = tokio::spawn(async move { agent.turn_streamed(&instruction, event_tx).await });
|
||||
|
||||
@@ -91,36 +171,6 @@ pub async fn execute_task_with_provider<T: Transport + 'static>(
|
||||
.map_err(|err| PipeError::Protocol(err.to_string()))
|
||||
}
|
||||
|
||||
fn build_agent<T: Transport + 'static>(
|
||||
browser_tool: BrowserPipeTool<T>,
|
||||
provider: Box<dyn Provider>,
|
||||
config: &ZeroClawConfig,
|
||||
) -> Result<Agent, PipeError> {
|
||||
let memory = build_memory(config).map_err(map_anyhow_to_pipe_error)?;
|
||||
let observer: Arc<dyn Observer> = Arc::new(NoopObserver);
|
||||
let tools: Vec<Box<dyn zeroclaw::tools::Tool>> =
|
||||
vec![Box::new(ZeroClawBrowserTool::new(browser_tool))];
|
||||
|
||||
Agent::builder()
|
||||
.provider(provider)
|
||||
.tools(tools)
|
||||
.memory(Arc::from(memory))
|
||||
.observer(observer)
|
||||
.tool_dispatcher(Box::new(NativeToolDispatcher))
|
||||
.config(config.agent.clone())
|
||||
.model_name(
|
||||
config
|
||||
.default_model
|
||||
.clone()
|
||||
.unwrap_or_else(|| "deepseek-chat".to_string()),
|
||||
)
|
||||
.temperature(config.default_temperature)
|
||||
.workspace_dir(config.workspace_dir.clone())
|
||||
.allowed_tools(Some(vec![BROWSER_ACTION_TOOL_NAME.to_string()]))
|
||||
.build()
|
||||
.map_err(map_anyhow_to_pipe_error)
|
||||
}
|
||||
|
||||
fn build_provider(config: &ZeroClawConfig) -> Result<Box<dyn Provider>, PipeError> {
|
||||
let provider_name = config.default_provider.as_deref().unwrap_or("deepseek");
|
||||
let model_name = config
|
||||
|
||||
Reference in New Issue
Block a user