use std::path::PathBuf;
use rho_sdk::{model::ModelUsage, CancellationToken};
use tokio::sync::watch;
use crate::{
acp_runtime::one_shot::{self, OneShotError, OneShotProgress},
agent::{OneShotPhase, OneShotUpdate, PromptPolicy},
};
use super::policy::{ClaudeAcpPolicy, ClaudeSessionShape, SessionPersistence};
pub(crate) struct ClaudeOneShotRequest {
pub(crate) system_prompt: &'static str,
pub(crate) input: String,
pub(crate) model: Option<String>,
pub(crate) reasoning: Option<rho_providers::reasoning::ReasoningLevel>,
pub(crate) cwd: PathBuf,
pub(crate) cancellation: CancellationToken,
}
pub(crate) struct ClaudeOneShotResult {
pub(crate) text: String,
pub(crate) usage: ModelUsage,
}
pub(crate) async fn run_one_shot(
request: ClaudeOneShotRequest,
updates: Option<watch::Sender<OneShotUpdate>>,
) -> Result<ClaudeOneShotResult, OneShotError> {
let publish = |phase, text: &str| {
if let Some(updates) = &updates {
let _ = updates.send(OneShotUpdate::new(phase, text));
}
};
publish(OneShotPhase::WaitingForProvider, "");
let policy = ClaudeAcpPolicy::headless(
ClaudeSessionShape {
system_prompt: PromptPolicy::Replace(request.system_prompt.to_owned()),
model: request.model,
reasoning: request.reasoning,
tools: Vec::new(),
max_turns: 1,
persistence: SessionPersistence::Discard,
},
request.cwd,
);
let mut answer_so_far = String::new();
let answer =
one_shot::run_one_shot(policy, &request.input, &request.cancellation, |progress| {
match progress {
OneShotProgress::Thinking => publish(OneShotPhase::Thinking, &answer_so_far),
OneShotProgress::Answer(text) => {
answer_so_far = text.to_owned();
publish(OneShotPhase::Responding, text);
}
}
})
.await?;
Ok(ClaudeOneShotResult {
text: answer.text.trim().to_owned(),
usage: ModelUsage {
cost_usd_micros: answer
.cost_usd
.filter(|cost| *cost > 0.0)
.map(|cost| (cost * 1_000_000.0).round() as u64),
..ModelUsage::default()
},
})
}