summaryrefslogtreecommitdiffstats
path: root/src/agent.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/agent.rs39
1 files changed, 25 insertions, 14 deletions
diff --git a/src/agent.rs b/src/agent.rs
index 7d4c4ef..afd662c 100644
--- a/src/agent.rs
+++ b/src/agent.rs
@@ -217,14 +217,16 @@ pub fn run_turn(
217 Err(error) => return Err(error), 217 Err(error) => return Err(error),
218 }; 218 };
219 219
220 // Placeholders are dropped before the assistant message is written: 220 // A call with no name cannot be replayed to the provider at all, so it
221 // recording a call we refuse to run would leave it unanswered. 221 // is dropped. A *named* call with no arguments is kept: the model is told
222 // what arrived and can retry, which is better than silence.
222 let before = outcome.tool_calls.len(); 223 let before = outcome.tool_calls.len();
223 outcome 224 outcome.tool_calls.retain(|call| !call.name.trim().is_empty());
224 .tool_calls
225 .retain(|call| !tools::is_placeholder(&call.arguments));
226 if outcome.tool_calls.len() < before { 225 if outcome.tool_calls.len() < before {
227 eprintln!("agent686: ignored {} empty tool call(s) from the model", before - outcome.tool_calls.len()); 226 eprintln!(
227 "agent686: ignored {} tool call(s) with no name",
228 before - outcome.tool_calls.len()
229 );
228 } 230 }
229 231
230 // A missing id would make the tool result unattachable. 232 // A missing id would make the tool result unattachable.
@@ -247,7 +249,15 @@ pub fn run_turn(
247 let calls = outcome.tool_calls.clone(); 249 let calls = outcome.tool_calls.clone();
248 last = Some(outcome); 250 last = Some(outcome);
249 251
250 for call in calls { 252 let multiple = calls.len() > 1;
253 for (offset, call) in calls.into_iter().enumerate() {
254 // Thousands of steps are possible in a long turn; the label keeps
255 // each call's lines attributable when several run in one step.
256 let label = if multiple {
257 format!("{steps}.{}", offset + 1)
258 } else {
259 steps.to_string()
260 };
251 // Cancelling between calls still has to answer every tool call the 261 // Cancelling between calls still has to answer every tool call the
252 // assistant message announced: an unanswered id makes the session 262 // assistant message announced: an unanswered id makes the session
253 // permanently unusable, because history is never rewritten. 263 // permanently unusable, because history is never rewritten.
@@ -257,7 +267,7 @@ pub fn run_turn(
257 let result = if aborted { 267 let result = if aborted {
258 ToolOutcome::Failed("aborted by the user before this call ran".into()) 268 ToolOutcome::Failed("aborted by the user before this call ran".into())
259 } else { 269 } else {
260 execute(&call, approver, cancel, steps) 270 execute(&call, approver, cancel, &label)
261 }; 271 };
262 if result.denied() { 272 if result.denied() {
263 denied += 1; 273 denied += 1;
@@ -269,7 +279,7 @@ pub fn run_turn(
269 _ => {} 279 _ => {}
270 } 280 }
271 } 281 }
272 ui::tool_result(&result.summary(), aborted); 282 ui::tool_result(&label, &result.summary(), aborted);
273 let message = json!({ 283 let message = json!({
274 "role": "tool", 284 "role": "tool",
275 "tool_call_id": call.id, 285 "tool_call_id": call.id,
@@ -350,21 +360,22 @@ fn execute(
350 call: &provider::ToolCall, 360 call: &provider::ToolCall,
351 approver: &mut Approver, 361 approver: &mut Approver,
352 cancel: &AtomicBool, 362 cancel: &AtomicBool,
353 step: usize, 363 label: &str,
354) -> ToolOutcome { 364) -> ToolOutcome {
355 let args: Value = match serde_json::from_str(&call.arguments) { 365 let args: Value = match serde_json::from_str(&call.arguments) {
356 Ok(args) => args, 366 Ok(args) => args,
357 Err(error) => { 367 Err(error) => {
368 let raw = crate::provider::truncate(call.arguments.trim(), 200);
358 return ToolOutcome::Failed(format!( 369 return ToolOutcome::Failed(format!(
359 "arguments were not valid JSON ({error}); issue the call again with the \ 370 "arguments were not valid JSON ({error}). Received: {raw:?}. \
360 complete arguments" 371 Issue the call again with complete arguments"
361 )) 372 ));
362 } 373 }
363 }; 374 };
364 // The bash approval prompt prints the command; do not print it here too. 375 // The bash approval prompt prints the command; do not print it here too.
365 let repeat_detail = !(tools::approval(&call.name) == Approval::EveryCall 376 let repeat_detail = !(tools::approval(&call.name) == Approval::EveryCall
366 && approver.will_prompt_bash()); 377 && approver.will_prompt_bash());
367 ui::tool_line(step, &call.name, &args, repeat_detail); 378 ui::tool_line(label, &call.name, &args, repeat_detail);
368 if let Err(error) = tools::validate(&call.name, &args) { 379 if let Err(error) = tools::validate(&call.name, &args) {
369 return ToolOutcome::Failed(format!("{error}; the call was not run")); 380 return ToolOutcome::Failed(format!("{error}; the call was not run"));
370 } 381 }