feat: persist sgclaw browser conversations
This commit is contained in:
@@ -105,6 +105,7 @@ fn parse_browser_action_request(args: Value) -> Result<BrowserActionRequest, Bro
|
||||
let action_name = take_required_string(&mut args, "action")?;
|
||||
let expected_domain = take_required_string(&mut args, "expected_domain")?;
|
||||
let action = parse_action(&action_name)?;
|
||||
validate_action_params(&action_name, &args)?;
|
||||
|
||||
Ok(BrowserActionRequest {
|
||||
action,
|
||||
@@ -146,6 +147,37 @@ fn failed_tool_result(error: String) -> ToolResult {
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_action_params(
|
||||
action_name: &str,
|
||||
args: &Map<String, Value>,
|
||||
) -> Result<(), BrowserActionAdapterError> {
|
||||
match action_name {
|
||||
"click" | "getText" => require_non_empty_string(args, "selector", action_name),
|
||||
"type" => {
|
||||
require_non_empty_string(args, "selector", action_name)?;
|
||||
require_non_empty_string(args, "text", action_name)
|
||||
}
|
||||
"navigate" => require_non_empty_string(args, "url", action_name),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn require_non_empty_string(
|
||||
args: &Map<String, Value>,
|
||||
key: &'static str,
|
||||
action_name: &str,
|
||||
) -> Result<(), BrowserActionAdapterError> {
|
||||
match args.get(key) {
|
||||
Some(Value::String(value)) if !value.trim().is_empty() => Ok(()),
|
||||
Some(other) => Err(BrowserActionAdapterError::InvalidArguments(format!(
|
||||
"{action_name} requires a non-empty {key}, got {other}"
|
||||
))),
|
||||
None => Err(BrowserActionAdapterError::InvalidArguments(format!(
|
||||
"{action_name} requires {key}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
fn format_browser_action_error(data: &Value) -> String {
|
||||
if let Some(error) = data.get("error") {
|
||||
if let Some(message) = error.get("message").and_then(Value::as_str) {
|
||||
|
||||
@@ -19,12 +19,21 @@ use crate::compat::config_adapter::build_zeroclaw_config_from_settings;
|
||||
use crate::config::DeepSeekSettings;
|
||||
use crate::compat::event_bridge::log_entry_for_turn_event;
|
||||
use crate::compat::memory_adapter::build_memory;
|
||||
use crate::pipe::{BrowserPipeTool, PipeError, Transport};
|
||||
use crate::pipe::{BrowserPipeTool, ConversationMessage, PipeError, Transport};
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct CompatTaskContext {
|
||||
pub conversation_id: Option<String>,
|
||||
pub messages: Vec<ConversationMessage>,
|
||||
pub page_url: Option<String>,
|
||||
pub page_title: Option<String>,
|
||||
}
|
||||
|
||||
pub fn execute_task<T: Transport + 'static>(
|
||||
transport: &T,
|
||||
browser_tool: BrowserPipeTool<T>,
|
||||
instruction: &str,
|
||||
task_context: &CompatTaskContext,
|
||||
workspace_root: &Path,
|
||||
settings: &DeepSeekSettings,
|
||||
) -> Result<String, PipeError> {
|
||||
@@ -38,6 +47,7 @@ pub fn execute_task<T: Transport + 'static>(
|
||||
browser_tool,
|
||||
provider,
|
||||
instruction,
|
||||
task_context,
|
||||
config,
|
||||
))
|
||||
}
|
||||
@@ -47,9 +57,24 @@ pub async fn execute_task_with_provider<T: Transport + 'static>(
|
||||
browser_tool: BrowserPipeTool<T>,
|
||||
provider: Box<dyn Provider>,
|
||||
instruction: &str,
|
||||
task_context: &CompatTaskContext,
|
||||
config: ZeroClawConfig,
|
||||
) -> Result<String, PipeError> {
|
||||
let mut agent = build_agent(browser_tool, provider, &config)?;
|
||||
if let Some(conversation_id) = task_context
|
||||
.conversation_id
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
{
|
||||
agent.set_memory_session_id(Some(conversation_id.to_string()));
|
||||
}
|
||||
|
||||
let seed_messages = 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();
|
||||
|
||||
@@ -196,3 +221,25 @@ impl Provider for NonStreamingProvider {
|
||||
stream::empty().boxed()
|
||||
}
|
||||
}
|
||||
|
||||
fn build_seed_history(task_context: &CompatTaskContext) -> Vec<ChatMessage> {
|
||||
task_context
|
||||
.messages
|
||||
.iter()
|
||||
.filter_map(to_chat_message)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn to_chat_message(message: &ConversationMessage) -> Option<ChatMessage> {
|
||||
let content = message.content.trim();
|
||||
if content.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
match message.role.as_str() {
|
||||
"user" => Some(ChatMessage::user(content)),
|
||||
"assistant" => Some(ChatMessage::assistant(content)),
|
||||
"system" => Some(ChatMessage::system(content)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user