AI media harness for planning, generating, and editing locally
TypeScript
use super::{
cli_provider_for,
compatible::{run_compatible_endpoint, validate_compatible_setting},
CliProvider,
};
use crate::agent::{
discovery::discover_executable,
process::run_subprocess,
types::{AgentEvent, ProviderId},
};
use crate::project::{ProjectConfig, ProviderSetting};
use std::{
path::{Path, PathBuf},
sync::{atomic::AtomicBool, Arc},
time::Duration,
};
pub(in crate::agent) struct TurnInput<'a> {
pub root: &'a Path,
pub config: &'a ProjectConfig,
pub prompt: &'a str,
pub images: &'a [crate::agent::capture::CapturedFrame],
pub cancellation: Arc<AtomicBool>,
pub timeout: Duration,
pub on_event: &'a dyn Fn(&AgentEvent),
}
/// One turn contract for CLI and HTTP transports. The runtime owns correction
/// policy and project validation; providers own execution and response envelopes.
pub(in crate::agent) trait AgentProvider {
fn run_turn(&self, input: TurnInput<'_>) -> Result<(Vec<AgentEvent>, String), String>;
}
struct CliSession {
id: ProviderId,
provider: &'static dyn CliProvider,
executable: PathBuf,
setting: Option<ProviderSetting>,
}
impl AgentProvider for CliSession {
fn run_turn(&self, input: TurnInput<'_>) -> Result<(Vec<AgentEvent>, String), String> {
let mut spec = self.provider.command_spec(
input.root,
&self.executable,
input.config,
input.prompt,
self.setting.as_ref(),
)?;
// Keep temporary Codex attachments alive until the child exits, including
// cancellation/error paths. Claude accepts image blocks over stdin.
let _attachments = super::images::attach_cli_images(self.id, &mut spec, input.images)?;
run_subprocess(
spec,
self.id,
input.cancellation,
input.timeout,
input.on_event,
)
}
}
struct EndpointSession {
id: ProviderId,
setting: ProviderSetting,
}
impl AgentProvider for EndpointSession {
fn run_turn(&self, input: TurnInput<'_>) -> Result<(Vec<AgentEvent>, String), String> {
run_compatible_endpoint(
self.id,
input.config,
input.prompt,
input.images,
&self.setting,
input.cancellation,
input.timeout,
input.on_event,
)
}
}
pub(in crate::agent) fn prepare(
id: ProviderId,
setting: Option<ProviderSetting>,
) -> Result<Box<dyn AgentProvider>, String> {
if let Some(provider) = cli_provider_for(id) {
let executable = discover_executable(provider.executable_name(), setting.as_ref())
.ok_or_else(|| format!("{} is not installed or is not on PATH.", provider.label()))?;
Ok(Box::new(CliSession {
id,
provider,
executable,
setting,
}))
} else {
validate_compatible_setting(id, setting.as_ref())?;
Ok(Box::new(EndpointSession {
id,
setting: setting.expect("validated endpoint settings"),
}))
}
}