agents: multi-step agent support
Split agent prompts on === PROMPT === delimiter. Each step runs as
a new user message in the same LLM conversation, so context carries
forward naturally between steps. Single-step agents are unchanged.
- AgentDef.prompt -> AgentDef.prompts: Vec<String>
- AgentBatch.prompt -> AgentBatch.prompts: Vec<String>
- API layer injects next prompt after each text response
- {{conversation:N}} parameterized byte budget for conversation context
Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
parent
baf208281d
commit
77d1d39f3f
6 changed files with 166 additions and 65 deletions
|
|
@ -22,6 +22,8 @@ use std::path::PathBuf;
|
|||
pub struct AgentResult {
|
||||
pub output: String,
|
||||
pub node_keys: Vec<String>,
|
||||
/// Directory containing output() files from the agent run.
|
||||
pub output_dir: std::path::PathBuf,
|
||||
}
|
||||
|
||||
/// Run a single agent and return the result (no action application — tools handle that).
|
||||
|
|
@ -78,12 +80,16 @@ pub fn run_one_agent_with_keys(
|
|||
|
||||
log(&format!("targeting: {}", keys.join(", ")));
|
||||
let graph = store.build_graph();
|
||||
let (prompt, extra_keys) = super::defs::resolve_placeholders(
|
||||
&def.prompt, store, &graph, keys, count,
|
||||
);
|
||||
let mut resolved_prompts = Vec::new();
|
||||
let mut all_keys: Vec<String> = keys.to_vec();
|
||||
all_keys.extend(extra_keys);
|
||||
let agent_batch = super::prompts::AgentBatch { prompt, node_keys: all_keys };
|
||||
for prompt_template in &def.prompts {
|
||||
let (prompt, extra_keys) = super::defs::resolve_placeholders(
|
||||
prompt_template, store, &graph, keys, count,
|
||||
);
|
||||
all_keys.extend(extra_keys);
|
||||
resolved_prompts.push(prompt);
|
||||
}
|
||||
let agent_batch = super::prompts::AgentBatch { prompts: resolved_prompts, node_keys: all_keys };
|
||||
|
||||
// Record visits eagerly so concurrent agents pick different seeds
|
||||
if !agent_batch.node_keys.is_empty() {
|
||||
|
|
@ -130,43 +136,56 @@ fn run_one_agent_inner(
|
|||
_llm_tag: &str,
|
||||
log: &(dyn Fn(&str) + Sync),
|
||||
) -> Result<AgentResult, String> {
|
||||
let prompt_kb = agent_batch.prompt.len() / 1024;
|
||||
let tools_desc = if def.tools.is_empty() { "no tools".into() }
|
||||
else { format!("{} tools", def.tools.len()) };
|
||||
log(&format!("prompt {}KB, model={}, {}, {} nodes",
|
||||
prompt_kb, def.model, tools_desc, agent_batch.node_keys.len()));
|
||||
let n_steps = agent_batch.prompts.len();
|
||||
|
||||
// Guard: reject prompts that would exceed model context.
|
||||
// Rough estimate: 1 token ≈ 4 bytes. Reserve 16K tokens for output.
|
||||
let max_prompt_bytes = 800_000; // ~200K tokens, leaves room for output
|
||||
if agent_batch.prompt.len() > max_prompt_bytes {
|
||||
// Log the oversized prompt for debugging
|
||||
for key in &agent_batch.node_keys {
|
||||
log(&format!(" node: {}", key));
|
||||
}
|
||||
|
||||
// Guard: reject oversized first prompt (later steps grow via conversation)
|
||||
let max_prompt_bytes = 800_000;
|
||||
let first_len = agent_batch.prompts[0].len();
|
||||
if first_len > max_prompt_bytes {
|
||||
let prompt_kb = first_len / 1024;
|
||||
let oversize_dir = store::memory_dir().join("llm-logs").join("oversized");
|
||||
fs::create_dir_all(&oversize_dir).ok();
|
||||
let oversize_path = oversize_dir.join(format!("{}-{}.txt",
|
||||
agent_name, store::compact_timestamp()));
|
||||
let header = format!("=== OVERSIZED PROMPT ===\nagent: {}\nsize: {}KB (max {}KB)\nnodes: {:?}\n\n",
|
||||
agent_name, prompt_kb, max_prompt_bytes / 1024, agent_batch.node_keys);
|
||||
fs::write(&oversize_path, format!("{}{}", header, agent_batch.prompt)).ok();
|
||||
fs::write(&oversize_path, format!("{}{}", header, &agent_batch.prompts[0])).ok();
|
||||
log(&format!("oversized prompt logged to {}", oversize_path.display()));
|
||||
|
||||
return Err(format!(
|
||||
"prompt too large: {}KB (max {}KB) — seed nodes may be oversized",
|
||||
prompt_kb, max_prompt_bytes / 1024,
|
||||
));
|
||||
}
|
||||
for key in &agent_batch.node_keys {
|
||||
log(&format!(" node: {}", key));
|
||||
|
||||
// Output directory — use --state-dir if set, otherwise flat per-agent
|
||||
let output_dir = std::env::var("POC_AGENT_OUTPUT_DIR")
|
||||
.map(std::path::PathBuf::from)
|
||||
.unwrap_or_else(|_| store::memory_dir().join("agent-output").join(agent_name));
|
||||
fs::create_dir_all(&output_dir).ok();
|
||||
// Safe: agent runs single-threaded, env var read only by our dispatch code
|
||||
unsafe { std::env::set_var("POC_AGENT_OUTPUT_DIR", &output_dir); }
|
||||
|
||||
log(&format!("{} step(s), {}KB initial, model={}, {}, {} nodes, output={}",
|
||||
n_steps, first_len / 1024, def.model, tools_desc,
|
||||
agent_batch.node_keys.len(), output_dir.display()));
|
||||
|
||||
for (i, p) in agent_batch.prompts.iter().enumerate() {
|
||||
log(&format!("=== PROMPT {}/{} ===\n\n{}", i + 1, n_steps, p));
|
||||
}
|
||||
log("\n=== CALLING LLM ===");
|
||||
|
||||
log(&format!("=== PROMPT ===\n\n{}\n\n=== CALLING LLM ===", agent_batch.prompt));
|
||||
|
||||
let output = llm::call_for_def(def, &agent_batch.prompt, log)?;
|
||||
|
||||
let output = llm::call_for_def_multi(def, &agent_batch.prompts, log)?;
|
||||
|
||||
Ok(AgentResult {
|
||||
output,
|
||||
node_keys: agent_batch.node_keys,
|
||||
output_dir,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue