use std::sync::Arc;
use std::time::Duration;
use chrono::{DateTime, Utc};
use tokio::sync::{broadcast, RwLock};
use tracing::warn;
use crate::thought::bridge_runner::BridgeRunner;
use crate::thought::emitter_client::EmitterClient;
use crate::thought::protocol::{SyncRequestSequence, ThoughtDeliveryState};
use crate::thought::runtime_config::ThoughtConfig;
use crate::types::{ControlEvent, RestState, SessionState, ThoughtSource, ThoughtState};
#[derive(Clone)]
pub struct SessionInfo {
pub session_id: String,
pub state: SessionState,
pub exited: bool,
pub tool: Option<String>,
pub cwd: String,
pub replay_text: String,
pub thought: Option<String>,
pub thought_state: ThoughtState,
pub thought_source: ThoughtSource,
pub rest_state: RestState,
pub commit_candidate: bool,
pub objective_fingerprint: Option<String>,
pub thought_updated_at: Option<DateTime<Utc>>,
pub token_count: u64,
pub context_limit: u64,
pub last_activity_at: DateTime<Utc>,
}
pub trait SessionProvider: Send + Sync {
fn session_snapshots(&self) -> Vec<SessionInfo>;
fn persist_thought(
&self,
_session_id: &str,
_thought: Option<&str>,
_token_count: u64,
_context_limit: u64,
_thought_state: ThoughtState,
_thought_source: ThoughtSource,
_rest_state: RestState,
_commit_candidate: bool,
_updated_at: DateTime<Utc>,
_delivery: ThoughtDeliveryState,
_objective_changed_at: Option<DateTime<Utc>>,
_objective_fingerprint: Option<String>,
) {
}
fn thought_delivery_states(&self) -> std::collections::HashMap<String, ThoughtDeliveryState> {
std::collections::HashMap::new()
}
}
pub struct ThoughtLoopRunner {
tick_ms: u64,
event_tx: broadcast::Sender<ControlEvent>,
runtime_config: Arc<RwLock<ThoughtConfig>>,
request_sequence: Arc<SyncRequestSequence>,
}
impl ThoughtLoopRunner {
pub fn with_runtime_config(
tick_ms: u64,
event_tx: broadcast::Sender<ControlEvent>,
runtime_config: Arc<RwLock<ThoughtConfig>>,
request_sequence: Arc<SyncRequestSequence>,
) -> Self {
Self {
tick_ms,
event_tx,
runtime_config,
request_sequence,
}
}
pub fn spawn<P: SessionProvider + 'static>(
self,
provider: Arc<P>,
) -> tokio::task::JoinHandle<()> {
warn!("legacy inproc thought backend selected; delegating to clawgs daemon bridge");
BridgeRunner::with_tick(
self.event_tx,
Duration::from_millis(self.tick_ms),
self.runtime_config,
)
.spawn(
provider,
EmitterClient::with_request_sequence(self.request_sequence),
)
}
}