Skip to main content

vv_agent/sdk/session/
state.rs

1use std::collections::{BTreeMap, VecDeque};
2use std::path::PathBuf;
3use std::sync::{Arc, Mutex};
4
5use serde_json::Value;
6
7use crate::runtime::sub_task_manager::SubTaskManager;
8use crate::runtime::{
9    BeforeCycleMessageProvider, CancellationToken, InterruptionMessageProvider, StreamCallback,
10};
11use crate::types::Metadata;
12
13use super::AgentSession;
14use crate::sdk::types::AgentRun;
15
16#[derive(Debug, Clone)]
17pub struct AgentSessionState {
18    pub running: bool,
19    pub session_id: String,
20    pub workspace: PathBuf,
21    pub messages: Vec<crate::types::Message>,
22    pub shared_state: Metadata,
23    pub latest_run: Option<AgentRun>,
24}
25
26pub type SessionEventHandler = Arc<dyn Fn(&str, &BTreeMap<String, Value>) + Send + Sync + 'static>;
27pub type SessionListenerId = u64;
28
29#[derive(Clone, Default)]
30pub struct AgentSessionRunRequest {
31    pub prompt: String,
32    pub task_name: Option<String>,
33    pub workspace: Option<PathBuf>,
34    pub initial_messages: Vec<crate::types::Message>,
35    pub shared_state: Metadata,
36    pub metadata: Metadata,
37    pub runtime_event_handler: Option<SessionEventHandler>,
38    pub before_cycle_messages: Option<BeforeCycleMessageProvider>,
39    pub interruption_messages: Option<InterruptionMessageProvider>,
40    pub steering_queue: Option<Arc<Mutex<VecDeque<String>>>>,
41    pub cancellation_token: Option<CancellationToken>,
42    pub stream_callback: Option<StreamCallback>,
43    pub sub_task_manager: Option<SubTaskManager>,
44}
45
46impl AgentSessionRunRequest {
47    pub fn new(prompt: impl Into<String>) -> Self {
48        Self {
49            prompt: prompt.into(),
50            task_name: None,
51            workspace: None,
52            initial_messages: Vec::new(),
53            shared_state: Metadata::new(),
54            metadata: Metadata::new(),
55            runtime_event_handler: None,
56            before_cycle_messages: None,
57            interruption_messages: None,
58            steering_queue: None,
59            cancellation_token: None,
60            stream_callback: None,
61            sub_task_manager: None,
62        }
63    }
64}
65
66impl AgentSession {
67    pub fn state(&self) -> AgentSessionState {
68        AgentSessionState {
69            running: self.running,
70            session_id: self.session_id.clone(),
71            workspace: self.workspace.clone(),
72            messages: self.messages.clone(),
73            shared_state: self.shared_state.clone(),
74            latest_run: self.latest_run.clone(),
75        }
76    }
77}