mirror of
https://github.com/outbackdingo/optimclaw.git
synced 2026-08-25 14:53:34 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f347d1f63 |
@@ -33,12 +33,16 @@ impl Agent {
|
|||||||
/// Returns `AgenticLoopResult::Response` on completion, or
|
/// Returns `AgenticLoopResult::Response` on completion, or
|
||||||
/// `AgenticLoopResult::NeedApproval` if a tool requires user approval.
|
/// `AgenticLoopResult::NeedApproval` if a tool requires user approval.
|
||||||
///
|
///
|
||||||
|
/// When `resume_after_tool` is true the loop already knows a tool was
|
||||||
|
/// executed earlier in this turn (e.g. an approved tool), so it won't
|
||||||
|
/// force the LLM to use tools if it responds with text.
|
||||||
pub(super) async fn run_agentic_loop(
|
pub(super) async fn run_agentic_loop(
|
||||||
&self,
|
&self,
|
||||||
message: &IncomingMessage,
|
message: &IncomingMessage,
|
||||||
session: Arc<Mutex<Session>>,
|
session: Arc<Mutex<Session>>,
|
||||||
thread_id: Uuid,
|
thread_id: Uuid,
|
||||||
initial_messages: Vec<ChatMessage>,
|
initial_messages: Vec<ChatMessage>,
|
||||||
|
resume_after_tool: bool,
|
||||||
) -> Result<AgenticLoopResult, Error> {
|
) -> Result<AgenticLoopResult, Error> {
|
||||||
// Load workspace system prompt (identity files: AGENTS.md, SOUL.md, etc.)
|
// Load workspace system prompt (identity files: AGENTS.md, SOUL.md, etc.)
|
||||||
let system_prompt = if let Some(ws) = self.workspace() {
|
let system_prompt = if let Some(ws) = self.workspace() {
|
||||||
@@ -110,6 +114,8 @@ impl Agent {
|
|||||||
|
|
||||||
const MAX_TOOL_ITERATIONS: usize = 10;
|
const MAX_TOOL_ITERATIONS: usize = 10;
|
||||||
let mut iteration = 0;
|
let mut iteration = 0;
|
||||||
|
let mut tools_executed = resume_after_tool;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
iteration += 1;
|
iteration += 1;
|
||||||
if iteration > MAX_TOOL_ITERATIONS {
|
if iteration > MAX_TOOL_ITERATIONS {
|
||||||
@@ -193,12 +199,30 @@ impl Agent {
|
|||||||
|
|
||||||
match output.result {
|
match output.result {
|
||||||
RespondResult::Text(text) => {
|
RespondResult::Text(text) => {
|
||||||
|
// If no tools have been executed yet, prompt the LLM to use tools
|
||||||
|
// This handles the case where the model explains what it will do
|
||||||
|
// instead of actually calling tools
|
||||||
|
if !tools_executed && iteration < 3 {
|
||||||
|
tracing::debug!(
|
||||||
|
"No tools executed yet (iteration {}), prompting for tool use",
|
||||||
|
iteration
|
||||||
|
);
|
||||||
|
context_messages.push(ChatMessage::assistant(&text));
|
||||||
|
context_messages.push(ChatMessage::user(
|
||||||
|
"Please proceed and use the available tools to complete this task.",
|
||||||
|
));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tools have been executed or we've tried multiple times, return response
|
||||||
return Ok(AgenticLoopResult::Response(text));
|
return Ok(AgenticLoopResult::Response(text));
|
||||||
}
|
}
|
||||||
RespondResult::ToolCalls {
|
RespondResult::ToolCalls {
|
||||||
tool_calls,
|
tool_calls,
|
||||||
content,
|
content,
|
||||||
} => {
|
} => {
|
||||||
|
tools_executed = true;
|
||||||
|
|
||||||
// Add the assistant message with tool_calls to context.
|
// Add the assistant message with tool_calls to context.
|
||||||
// OpenAI protocol requires this before tool-result messages.
|
// OpenAI protocol requires this before tool-result messages.
|
||||||
context_messages.push(ChatMessage::assistant_with_tool_calls(
|
context_messages.push(ChatMessage::assistant_with_tool_calls(
|
||||||
|
|||||||
@@ -278,7 +278,7 @@ impl Agent {
|
|||||||
|
|
||||||
// Run the agentic tool execution loop
|
// Run the agentic tool execution loop
|
||||||
let result = self
|
let result = self
|
||||||
.run_agentic_loop(message, session.clone(), thread_id, turn_messages)
|
.run_agentic_loop(message, session.clone(), thread_id, turn_messages, false)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// Re-acquire lock and check if interrupted
|
// Re-acquire lock and check if interrupted
|
||||||
@@ -1057,7 +1057,7 @@ impl Agent {
|
|||||||
|
|
||||||
// Continue the agentic loop (a tool was already executed this turn)
|
// Continue the agentic loop (a tool was already executed this turn)
|
||||||
let result = self
|
let result = self
|
||||||
.run_agentic_loop(message, session.clone(), thread_id, context_messages)
|
.run_agentic_loop(message, session.clone(), thread_id, context_messages, true)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// Handle the result
|
// Handle the result
|
||||||
|
|||||||
Reference in New Issue
Block a user