forked from kent/consciousness
TurnResult: remove text field, simplify oneshot loop
- Remove TurnResult.text (was dead code - Agent::turn handles text internally) - Simplify run_with_backend to just iterate over steps (Agent::turn loops for tool calls and handles empty responses internally) - Change run/run_shared/run_forked_shared to return Result<(), String> - Remove AgentResult.output field (no callers used it) - Stub out legacy text-parsing code (audit, compare) that needs redesign - Update digest.rs to not depend on text return - Add level parameter to journal_new/journal_update for digest support Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
ef80398466
commit
f00532bdb7
11 changed files with 82 additions and 422 deletions
|
|
@ -244,7 +244,7 @@ impl AutoAgent {
|
|||
pub async fn run(
|
||||
&mut self,
|
||||
bail_fn: Option<&(dyn Fn(usize) -> Result<(), String> + Sync)>,
|
||||
) -> Result<String, String> {
|
||||
) -> Result<(), String> {
|
||||
let config = crate::config::get();
|
||||
let base_url = config.api_base_url.as_deref().unwrap_or("");
|
||||
let api_key = config.api_key.as_deref().unwrap_or("");
|
||||
|
|
@ -288,7 +288,7 @@ impl AutoAgent {
|
|||
pub async fn run_shared(
|
||||
&mut self,
|
||||
agent: &std::sync::Arc<Agent>,
|
||||
) -> Result<String, String> {
|
||||
) -> Result<(), String> {
|
||||
let mut backend = Backend(agent.clone());
|
||||
self.run_with_backend(&mut backend, None).await
|
||||
}
|
||||
|
|
@ -301,7 +301,7 @@ impl AutoAgent {
|
|||
memory_keys: &[String],
|
||||
state: &std::collections::BTreeMap<String, String>,
|
||||
recently_written: &[String],
|
||||
) -> Result<String, String> {
|
||||
) -> Result<(), String> {
|
||||
let resolved_steps: Vec<AutoStep> = self.steps.iter().map(|s| AutoStep {
|
||||
prompt: resolve_prompt(&s.prompt, memory_keys, state, recently_written),
|
||||
phase: s.phase.clone(),
|
||||
|
|
@ -347,67 +347,23 @@ impl AutoAgent {
|
|||
&mut self,
|
||||
backend: &mut Backend,
|
||||
bail_fn: Option<&(dyn Fn(usize) -> Result<(), String> + Sync)>,
|
||||
) -> Result<String, String> {
|
||||
) -> Result<(), String> {
|
||||
dbglog!("[auto] {} starting, {} steps", self.name, self.steps.len());
|
||||
self.turn = 0;
|
||||
self.current_phase = self.steps.first()
|
||||
.map(|s| s.phase.clone()).unwrap_or_default();
|
||||
let mut next_step = 0;
|
||||
|
||||
if next_step < self.steps.len() {
|
||||
backend.push_node(
|
||||
AstNode::system_msg(&self.steps[next_step].prompt)).await;
|
||||
next_step += 1;
|
||||
for (i, step) in self.steps.iter().enumerate() {
|
||||
self.turn = i + 1;
|
||||
self.current_phase = step.phase.clone();
|
||||
|
||||
if let Some(ref check) = bail_fn {
|
||||
check(i)?;
|
||||
}
|
||||
|
||||
backend.push_node(AstNode::system_msg(&step.prompt)).await;
|
||||
Agent::turn(backend.0.clone()).await
|
||||
.map_err(|e| format!("{}: {}", self.name, e))?;
|
||||
}
|
||||
|
||||
let max_turns = 50 * self.steps.len().max(1);
|
||||
|
||||
for _ in 0..max_turns {
|
||||
self.turn += 1;
|
||||
|
||||
let result = match Agent::turn(backend.0.clone()).await {
|
||||
Ok(r) => r,
|
||||
Err(e) if super::context::is_context_overflow(&e) => {
|
||||
dbglog!("[auto] {} context full, stopping gracefully", self.name);
|
||||
return Ok(String::new());
|
||||
}
|
||||
Err(e) => return Err(format!("{}: {}", self.name, e)),
|
||||
};
|
||||
|
||||
if result.had_tool_calls {
|
||||
continue;
|
||||
}
|
||||
|
||||
let text = result.text;
|
||||
if text.is_empty() {
|
||||
dbglog!("[auto] {} empty response, retrying", self.name);
|
||||
backend.push_node(AstNode::system_msg(
|
||||
"Your previous response was empty. \
|
||||
Please respond with text or use a tool."
|
||||
)).await;
|
||||
continue;
|
||||
}
|
||||
|
||||
dbglog!("[auto] {} response: {}",
|
||||
self.name, &text[..text.floor_char_boundary(text.len().min(200))]);
|
||||
|
||||
if next_step < self.steps.len() {
|
||||
if let Some(ref check) = bail_fn {
|
||||
check(next_step)?;
|
||||
}
|
||||
self.current_phase = self.steps[next_step].phase.clone();
|
||||
backend.push_node(
|
||||
AstNode::system_msg(&self.steps[next_step].prompt)).await;
|
||||
next_step += 1;
|
||||
dbglog!("[auto] {} step {}/{}",
|
||||
self.name, next_step, self.steps.len());
|
||||
continue;
|
||||
}
|
||||
|
||||
return Ok(text);
|
||||
}
|
||||
|
||||
Err(format!("{}: exceeded {} tool turns", self.name, max_turns))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -418,7 +374,6 @@ impl AutoAgent {
|
|||
|
||||
/// Result of running a single agent.
|
||||
pub struct AgentResult {
|
||||
pub output: String,
|
||||
pub node_keys: Vec<String>,
|
||||
/// Directory containing output() files from the agent run.
|
||||
pub state_dir: PathBuf,
|
||||
|
|
@ -556,12 +511,11 @@ pub fn run_one_agent(
|
|||
Ok(())
|
||||
};
|
||||
|
||||
let output = call_api_with_tools_sync(
|
||||
call_api_with_tools_sync(
|
||||
agent_name, &prompts, &step_phases, def.temperature, def.priority,
|
||||
&effective_tools, Some(&bail_fn))?;
|
||||
|
||||
Ok(AgentResult {
|
||||
output,
|
||||
node_keys: agent_batch.node_keys,
|
||||
state_dir,
|
||||
})
|
||||
|
|
@ -581,7 +535,7 @@ pub async fn call_api_with_tools(
|
|||
priority: i32,
|
||||
tools: &[agent_tools::Tool],
|
||||
bail_fn: Option<&(dyn Fn(usize) -> Result<(), String> + Sync)>,
|
||||
) -> Result<String, String> {
|
||||
) -> Result<(), String> {
|
||||
let steps: Vec<AutoStep> = prompts.iter().zip(
|
||||
phases.iter().map(String::as_str)
|
||||
.chain(std::iter::repeat(""))
|
||||
|
|
@ -610,7 +564,7 @@ pub fn call_api_with_tools_sync(
|
|||
priority: i32,
|
||||
tools: &[agent_tools::Tool],
|
||||
bail_fn: Option<&(dyn Fn(usize) -> Result<(), String> + Sync)>,
|
||||
) -> Result<String, String> {
|
||||
) -> Result<(), String> {
|
||||
std::thread::scope(|s| {
|
||||
s.spawn(|| {
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue