mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-29 13:21:18 -08:00
63 lines
2.0 KiB
Rust
63 lines
2.0 KiB
Rust
//! What a loop needs to start an agent session and read its console state: the
|
|
//! shared context, `start_session` and `read_session_state`. Both the
|
|
//! Implementation Loop and the Review Loop call it.
|
|
|
|
use std::path::PathBuf;
|
|
use std::sync::Arc;
|
|
|
|
use crate::agent_manager::AgentManager;
|
|
use crate::loop_driver::{self, Hooks, Launch, Limits, LoopSlot, Strength};
|
|
use crate::models::StrengthRow;
|
|
use crate::session_reader;
|
|
|
|
/// What the watch thread needs, shared by every step of one loop.
|
|
#[derive(Clone)]
|
|
pub struct Ctx {
|
|
pub slot: LoopSlot,
|
|
pub agent: Arc<AgentManager>,
|
|
pub console_dir: PathBuf,
|
|
pub home: PathBuf,
|
|
pub launch: Launch,
|
|
pub run_id: u64,
|
|
pub hooks: Arc<Hooks>,
|
|
pub limits: Limits,
|
|
}
|
|
|
|
/// What a started session reports: the agent's generation and the model and
|
|
/// effort it started on.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct SessionStart {
|
|
pub generation: u64,
|
|
pub model: Option<String>,
|
|
pub effort: Option<String>,
|
|
}
|
|
|
|
/// Start a fresh agent on `prompt`. With `strength` (the word and its row) the
|
|
/// agent starts on that row's model and effort with `PLAN2CODE_LOOP_STRENGTH`
|
|
/// set; without it, on the agent's launch choice.
|
|
pub fn start_session(
|
|
ctx: &Ctx,
|
|
prompt: &str,
|
|
strength: Option<(Strength, &StrengthRow)>,
|
|
) -> Result<SessionStart, String> {
|
|
match strength {
|
|
Some((word, row)) => {
|
|
loop_driver::start_phase_with(&ctx.agent, &ctx.launch, prompt.to_string(), row, word)?
|
|
}
|
|
None => loop_driver::start_with(&ctx.agent, &ctx.launch, prompt.to_string())?,
|
|
}
|
|
let snapshot = ctx.agent.snapshot();
|
|
Ok(SessionStart {
|
|
generation: snapshot.generation,
|
|
model: snapshot.model,
|
|
effort: snapshot.effort,
|
|
})
|
|
}
|
|
|
|
/// The console state of session `sid`, or `None` when it cannot be read now.
|
|
pub fn read_session_state(ctx: &Ctx, sid: &str) -> Option<serde_json::Value> {
|
|
session_reader::load(&ctx.home, sid)
|
|
.ok()
|
|
.map(|session| session.state)
|
|
}
|