defs.rs: async conversion, remove block_in_place
Convert resolve(), resolve_placeholders(), run_agent() to async. Use memory_render/memory_query directly with .await instead of block_in_place wrappers. Propagate async to callers: - config.rs: resolve(), load_session(), reload_for_model() - identity.rs: load_memory_files(), assemble_context_message() - oneshot.rs: run_one_agent() - prompts.rs: agent_prompt() Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
9bb07bc26a
commit
359955f838
10 changed files with 44 additions and 64 deletions
|
|
@ -14,6 +14,7 @@
|
|||
//
|
||||
// The query selects what to operate on; placeholders pull in context.
|
||||
|
||||
use crate::agent::tools::memory::memory_render;
|
||||
use crate::graph::Graph;
|
||||
use crate::store::Store;
|
||||
|
||||
|
|
@ -198,7 +199,7 @@ struct Resolved {
|
|||
|
||||
/// Resolve a single {{placeholder}} by name.
|
||||
/// Returns the replacement text and any node keys it produced (for visit tracking).
|
||||
fn resolve(
|
||||
async fn resolve(
|
||||
name: &str,
|
||||
store: &Store,
|
||||
graph: &Graph,
|
||||
|
|
@ -211,10 +212,13 @@ fn resolve(
|
|||
let mut text = String::new();
|
||||
let mut result_keys = Vec::new();
|
||||
for key in keys {
|
||||
if let Some(r) = resolve_tool(&format!("memory_render {}", key)) {
|
||||
if !text.is_empty() { text.push_str("\n\n---\n\n"); }
|
||||
text.push_str(&format!("## {}\n\n{}", key, r.text));
|
||||
result_keys.push(key.clone());
|
||||
match memory_render(None, key, None).await {
|
||||
Ok(c) if !c.trim().is_empty() => {
|
||||
if !text.is_empty() { text.push_str("\n\n---\n\n"); }
|
||||
text.push_str(&format!("## {}\n\n{}", key, c));
|
||||
result_keys.push(key.clone());
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
if text.is_empty() { return None; }
|
||||
|
|
@ -227,12 +231,7 @@ fn resolve(
|
|||
let mut result_keys = Vec::new();
|
||||
|
||||
for key in keys {
|
||||
let content = tokio::task::block_in_place(|| {
|
||||
tokio::runtime::Handle::current().block_on(
|
||||
crate::agent::tools::memory::memory_render(None, key, None)
|
||||
)
|
||||
});
|
||||
match content {
|
||||
match memory_render(None, key, None).await {
|
||||
Ok(c) if !c.trim().is_empty() => {
|
||||
text.push_str(&format!("#### {}\n\n{}\n\n---\n\n", key, c));
|
||||
result_keys.push(key.clone());
|
||||
|
|
@ -305,12 +304,7 @@ fn resolve(
|
|||
let mut keys = Vec::new();
|
||||
for group in &cfg.context_groups {
|
||||
if !group.agent { continue; }
|
||||
// Bridge sync→async using block_in_place (same as resolve_tool)
|
||||
let entries = tokio::task::block_in_place(|| {
|
||||
tokio::runtime::Handle::current().block_on(
|
||||
crate::cli::node::get_group_content(group, &cfg)
|
||||
)
|
||||
});
|
||||
let entries = crate::cli::node::get_group_content(group, &cfg).await;
|
||||
for (key, content) in entries {
|
||||
use std::fmt::Write;
|
||||
writeln!(text, "--- {} ({}) ---", key, group.label).ok();
|
||||
|
|
@ -366,7 +360,7 @@ fn resolve(
|
|||
// tool:NAME ARGS — run a tool call and include its output
|
||||
_ if name.starts_with("tool:") => {
|
||||
let spec = name[5..].trim();
|
||||
resolve_tool(spec)
|
||||
resolve_tool(spec).await
|
||||
}
|
||||
|
||||
// bash:COMMAND — run a shell command and include its stdout
|
||||
|
|
@ -529,9 +523,8 @@ fn resolve_memory_ratio() -> String {
|
|||
pct, keys.len(), memory_bytes / 1024, transcript_size / 1024)
|
||||
}
|
||||
|
||||
/// Resolve a {{tool: name {args}}} placeholder by calling the tool
|
||||
/// handler from the registry. Uses block_in_place to bridge sync→async.
|
||||
fn resolve_tool(spec: &str) -> Option<Resolved> {
|
||||
/// Resolve a {{tool: name {args}}} placeholder by calling the tool handler.
|
||||
async fn resolve_tool(spec: &str) -> Option<Resolved> {
|
||||
// Parse "tool_name {json args}" or "tool_name arg"
|
||||
let (name, args) = match spec.find('{') {
|
||||
Some(i) => {
|
||||
|
|
@ -552,13 +545,7 @@ fn resolve_tool(spec: &str) -> Option<Resolved> {
|
|||
let tools = crate::agent::tools::tools();
|
||||
let tool = tools.iter().find(|t| t.name == name)?;
|
||||
|
||||
let result = tokio::task::block_in_place(|| {
|
||||
tokio::runtime::Handle::current().block_on(
|
||||
(tool.handler)(None, args.clone())
|
||||
)
|
||||
});
|
||||
|
||||
match result {
|
||||
match (tool.handler)(None, args.clone()).await {
|
||||
Ok(text) => Some(Resolved { text, keys: vec![] }),
|
||||
Err(e) => {
|
||||
eprintln!("[defs] {{{{tool: {}}}}} failed: {}", name, e);
|
||||
|
|
@ -569,7 +556,7 @@ fn resolve_tool(spec: &str) -> Option<Resolved> {
|
|||
|
||||
/// Resolve all {{placeholder}} patterns in a prompt template.
|
||||
/// Returns the resolved text and all node keys collected from placeholders.
|
||||
pub fn resolve_placeholders(
|
||||
pub async fn resolve_placeholders(
|
||||
template: &str,
|
||||
store: &Store,
|
||||
keys: &[String],
|
||||
|
|
@ -585,7 +572,7 @@ pub fn resolve_placeholders(
|
|||
let Some(rel_end) = result[start + 2..].find("}}") else { break };
|
||||
let end = start + 2 + rel_end;
|
||||
let name = result[start + 2..end].trim().to_lowercase();
|
||||
match resolve(&name, store, &graph, keys, count) {
|
||||
match resolve(&name, store, &graph, keys, count).await {
|
||||
Some(resolved) => {
|
||||
let len = resolved.text.len();
|
||||
extra_keys.extend(resolved.keys);
|
||||
|
|
@ -606,7 +593,7 @@ pub fn resolve_placeholders(
|
|||
/// Run a config-driven agent: query → resolve placeholders → prompt.
|
||||
/// `exclude` filters out nodes (and their neighborhoods) already being
|
||||
/// worked on by other agents, preventing concurrent collisions.
|
||||
pub fn run_agent(
|
||||
pub async fn run_agent(
|
||||
store: &Store,
|
||||
def: &AgentDef,
|
||||
count: usize,
|
||||
|
|
@ -621,11 +608,9 @@ pub fn run_agent(
|
|||
} else {
|
||||
format!("{} | limit:{}", def.query, padded)
|
||||
};
|
||||
let result = tokio::task::block_in_place(|| {
|
||||
tokio::runtime::Handle::current().block_on(
|
||||
crate::agent::tools::memory::memory_query(None, &query, None)
|
||||
)
|
||||
}).map_err(|e| e.to_string())?;
|
||||
let result = crate::agent::tools::memory::memory_query(None, &query, None)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let filtered: Vec<String> = result.lines()
|
||||
.filter(|l| !l.is_empty() && *l != "no results")
|
||||
.map(|s| s.to_string())
|
||||
|
|
@ -650,7 +635,7 @@ pub fn run_agent(
|
|||
.replace("{agent_name}", &def.agent)
|
||||
.replace("{user_name}", &cfg.user_name)
|
||||
.replace("{assistant_name}", &cfg.assistant_name);
|
||||
let (prompt, extra_keys) = resolve_placeholders(&template, store, &all_keys, count);
|
||||
let (prompt, extra_keys) = resolve_placeholders(&template, store, &all_keys, count).await;
|
||||
all_keys.extend(extra_keys);
|
||||
resolved_steps.push(super::prompts::ResolvedStep {
|
||||
prompt,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue