-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexecution_mode.rs
More file actions
233 lines (216 loc) · 7.42 KB
/
Copy pathexecution_mode.rs
File metadata and controls
233 lines (216 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use super::{AgentEvent, AgentLoop, AgentResult};
use crate::hooks::ErrorType;
use crate::llm::Message;
use crate::planning::{LlmPlanner, PreAnalysis};
use crate::prompts::{AgentStyle, PlanningMode};
use anyhow::Result;
use tokio::sync::mpsc;
struct ExecutionRoute {
style: AgentStyle,
use_planning: bool,
effective_prompt: String,
pre_analysis: Option<PreAnalysis>,
}
impl AgentLoop {
pub(super) fn preserve_original_prompt_for_execution(
original_prompt: &str,
optimized_input: &str,
) -> String {
let original = original_prompt.trim();
let optimized = optimized_input.trim();
if original.is_empty() {
return optimized.to_string();
}
if optimized.is_empty() || optimized == original {
return original.to_string();
}
if optimized.contains(original) {
return optimized.to_string();
}
format!("Original user request:\n{original}\n\nPlanner-optimized request:\n{optimized}")
}
pub(super) fn should_run_pre_analysis(&self) -> bool {
match self.config.planning_mode {
PlanningMode::Disabled => false,
PlanningMode::Enabled => true,
PlanningMode::Auto => true,
}
}
/// Execute the agent loop for a prompt with session context
///
/// Takes the conversation history, user prompt, and optional session ID.
/// When session_id is provided, context providers can use it for session-specific context.
pub async fn execute_with_session(
&self,
history: &[Message],
prompt: &str,
session_id: Option<&str>,
event_tx: Option<mpsc::Sender<AgentEvent>>,
cancel_token: Option<&tokio_util::sync::CancellationToken>,
) -> Result<AgentResult> {
let default_token = tokio_util::sync::CancellationToken::new();
let token = cancel_token.unwrap_or(&default_token);
tracing::info!(
a3s.session.id = session_id.unwrap_or("none"),
a3s.agent.max_turns = self.config.max_tool_rounds,
"a3s.agent.execute started"
);
let route = self.resolve_execution_route(prompt).await;
let mut effective_prompt = route.effective_prompt.clone();
let mut auto_tool_calls_count = 0;
if !route.use_planning {
if let Some(outcome) = self
.maybe_apply_auto_delegation(&effective_prompt, session_id, &event_tx)
.await?
{
effective_prompt = outcome.prompt;
auto_tool_calls_count = outcome.tool_calls_count;
}
}
let mut result = if route.use_planning {
self.execute_with_planning(
history,
&effective_prompt,
session_id,
event_tx,
route.pre_analysis,
)
.await
} else {
self.execute_loop(
history,
&effective_prompt,
route.style,
session_id,
event_tx,
token,
true,
)
.await
};
if let Ok(result) = &mut result {
result.tool_calls_count += auto_tool_calls_count;
}
self.record_execution_result(session_id, &result).await;
result
}
async fn resolve_execution_route(&self, prompt: &str) -> ExecutionRoute {
let pre_analysis = self.run_pre_analysis(prompt).await;
let style = self.resolve_execution_style(prompt, pre_analysis.as_ref());
let use_planning = self.resolve_planning_decision(style, pre_analysis.as_ref());
let effective_prompt = pre_analysis
.as_ref()
.map(|analysis| {
Self::preserve_original_prompt_for_execution(prompt, &analysis.optimized_input)
})
.unwrap_or_else(|| prompt.to_string());
ExecutionRoute {
style,
use_planning,
effective_prompt,
pre_analysis,
}
}
async fn run_pre_analysis(&self, prompt: &str) -> Option<PreAnalysis> {
if !self.should_run_pre_analysis() {
return None;
}
match LlmPlanner::pre_analyze(&self.llm_client.clone(), prompt).await {
Ok(analysis) => {
tracing::debug!(
intent = ?analysis.intent,
requires_planning = analysis.requires_planning,
plan_steps = analysis.execution_plan.steps.len(),
"Pre-analysis completed"
);
Some(analysis)
}
Err(e) => {
tracing::warn!(error = %e, "Pre-analysis failed; using local style fallback");
None
}
}
}
fn resolve_execution_style(
&self,
prompt: &str,
pre_analysis: Option<&PreAnalysis>,
) -> AgentStyle {
if let Some(analysis) = pre_analysis {
return analysis.intent;
}
let (style, confidence) = AgentStyle::detect_with_confidence(prompt);
tracing::debug!(
intent.classification = ?style,
intent.confidence = ?confidence,
intent.source = "local_fallback",
"Intent classified locally"
);
style
}
fn resolve_planning_decision(
&self,
style: AgentStyle,
pre_analysis: Option<&PreAnalysis>,
) -> bool {
match self.config.planning_mode {
PlanningMode::Disabled => false,
PlanningMode::Enabled => true,
PlanningMode::Auto => pre_analysis
.map(|analysis| analysis.requires_planning)
.unwrap_or_else(|| style.requires_planning()),
}
}
async fn record_execution_result(
&self,
session_id: Option<&str>,
result: &Result<AgentResult>,
) {
match result {
Ok(r) => {
tracing::info!(
a3s.agent.tool_calls_count = r.tool_calls_count,
a3s.llm.total_tokens = r.usage.total_tokens,
"a3s.agent.execute completed"
);
self.config.rl_trajectory_recorder.record_execution_end(
session_id.unwrap_or(""),
true,
Some(&r.text),
Some(&r.usage),
Some(r.tool_calls_count),
None,
);
self.fire_post_response(
session_id.unwrap_or(""),
&r.text,
r.tool_calls_count,
&r.usage,
0,
)
.await;
}
Err(e) => {
tracing::warn!(
error = %e,
"a3s.agent.execute failed"
);
self.config.rl_trajectory_recorder.record_execution_end(
session_id.unwrap_or(""),
false,
None,
None,
None,
Some(&e.to_string()),
);
self.fire_on_error(
session_id.unwrap_or(""),
ErrorType::Other,
&e.to_string(),
serde_json::json!({"phase": "execute"}),
)
.await;
}
}
}
}