mod core;
pub mod prompts;
pub mod registry;
pub mod session;
mod views;
use serde::{Deserialize, Serialize};
pub use core::{Agent, AgentConfig, PromptConfig};
pub use prompts::{AgentMessagePrompt, SystemPrompt};
pub use registry::AgentRegistry;
pub use session::AgentSession;
pub use views::{ActionView, BrowserStateView, HistoryView, StepView};
use thiserror::Error;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionModel {
pub action: String,
pub parameters: std::collections::HashMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionResult {
pub action: String,
pub success: bool,
pub extracted_content: Option<String>,
pub error: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrowserExtractTextResponse {
pub success: bool,
pub text: String,
pub length: usize,
#[serde(default)]
pub selector: Option<String>,
pub source: String,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrowserScreenshotResponse {
pub success: bool,
pub image: String,
pub format: String,
pub size_bytes: usize,
#[serde(default)]
pub selector: Option<String>,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentLLMResponse {
pub current_state: CurrentState,
pub action: Vec<ActionModel>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CurrentState {
pub prev_action_evaluation: String,
pub important_contents: String,
pub task_progress: String,
pub future_plans: String,
pub thought: String,
pub summary: String,
}
#[derive(Error, Debug)]
pub enum AgentError {
#[error("LLM error: {0}")]
LlmError(String),
#[error("Browser error: {0}")]
BrowserError(String),
#[error("JSON parse error: {0}")]
JsonParseError(String),
#[error("Step failed: {0}")]
StepFailed(String),
#[error("Agent stopped")]
Stopped,
#[error("Channel closed: {0}")]
ChannelClosed(String),
#[error("Unexpected error: {0}")]
UnexpectedError(String),
}
pub type AgentResult<T> = Result<T, AgentError>;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentOutput {
pub current_state: CurrentState,
pub action: Vec<ActionModel>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentHistory {
pub step: usize,
pub output: AgentOutput,
pub timestamp: chrono::DateTime<chrono::Utc>,
pub is_complete: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentHistoryList {
pub steps: Vec<AgentHistory>,
}
impl AgentHistoryList {
pub fn new() -> Self {
Self { steps: Vec::new() }
}
pub fn add_step(&mut self, output: AgentOutput) {
let step = AgentHistory {
step: self.steps.len(),
output,
timestamp: chrono::Utc::now(),
is_complete: false,
};
self.steps.push(step);
}
pub fn add_step_with_completion(&mut self, output: AgentOutput, is_complete: bool) {
let step = AgentHistory {
step: self.steps.len(),
output,
timestamp: chrono::Utc::now(),
is_complete,
};
self.steps.push(step);
}
pub fn is_complete(&self) -> bool {
self.steps.iter().any(|s| s.is_complete)
}
pub fn final_result(&self) -> Option<String> {
self.steps.iter().rev().find(|s| s.is_complete).map(|last| {
format!(
"Task completed at step {}. Summary: {}",
last.step, last.output.current_state.summary
)
})
}
}
impl Default for AgentHistoryList {
fn default() -> Self {
Self::new()
}
}