use std::collections::{HashMap, HashSet};
use std::time::Instant;
use nexil::ConduitError;
use nexil::tape::InMemoryTapeStore;
use serde_json::Value;
use crate::builtin::agent::Agent;
use crate::builtin::store::ForkTapeStore;
use crate::builtin::tape::TapeService;
use crate::types::{PromptValue, RUNTIME_WORKSPACE_KEY};
pub struct FallbackResult {
pub content: String,
pub duration_ms: u64,
}
pub async fn run_in_process(
prompt: &str,
workspace: &str,
model_override: Option<&str>,
) -> Result<FallbackResult, ConduitError> {
let start = Instant::now();
let mut agent = Agent::new();
let mem_store = InMemoryTapeStore::new();
let fork_store = ForkTapeStore::from_sync(mem_store);
let tapes_dir = std::env::temp_dir().join("eli-fallback-tapes");
let tapes = TapeService::new(tapes_dir, fork_store);
agent.set_tapes(tapes);
let session_id = format!("fallback-{}", &uuid::Uuid::new_v4().to_string()[..8]);
let mut state: HashMap<String, Value> = HashMap::new();
state.insert(
RUNTIME_WORKSPACE_KEY.to_owned(),
Value::String(workspace.to_owned()),
);
let blocked = HashSet::from(["agent".to_owned(), "subagent".to_owned()]);
let all_tools: HashSet<String> = {
let reg = crate::tools::REGISTRY.lock();
reg.keys()
.filter(|k| !blocked.contains(k.as_str()))
.cloned()
.collect()
};
let content = agent
.run(
&session_id,
PromptValue::Text(prompt.to_owned()),
&state,
model_override,
None,
Some(&all_tools),
)
.await?;
Ok(FallbackResult {
content,
duration_ms: start.elapsed().as_millis() as u64,
})
}