-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathagent_callbacks_builder.cpp
More file actions
298 lines (276 loc) · 13.2 KB
/
Copy pathagent_callbacks_builder.cpp
File metadata and controls
298 lines (276 loc) · 13.2 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "tui/agent_callbacks_builder.hpp"
// !! setup_agent_callbacks 目前全仓库无调用点(v0.5.5 时代从 main.cpp 提取,
// 为 TuiContext 重构预留,从未接线)—— 实际生效的是 main.cpp 内联注册的
// 同款回调。修改 main.cpp 的 on_delta / on_usage / on_busy_changed /
// on_stream_retry_reset 语义时必须同步本文件(先例:renderable_tool_summary_line
// 双实现)。若重构落地或决定放弃,请删除本文件而不是让两份继续漂移。
#include <chrono>
#include <mutex>
#include <string>
#include <nlohmann/json.hpp>
#include "agent_loop.hpp"
#include "commands/compact.hpp"
#include "tui/model_retry_status.hpp"
#include "tui/tool_row_format.hpp"
#include "tui/tui_helpers.hpp"
#include "tui/clipboard_helpers.hpp"
#include "session/todo_state.hpp"
#include "remote_control/remote_control_service.hpp"
#include "utils/power_inhibitor.hpp"
namespace acecode { namespace tui {
void setup_agent_callbacks(TuiContext& ctx) {
auto& state = ctx.state;
auto& screen = ctx.screen;
auto& agent_aborting = ctx.agent_aborting;
AgentCallbacks callbacks;
callbacks.on_message = [&state, &screen](const std::string& role, const std::string& content, bool is_tool) {
std::lock_guard<std::mutex> lk(state.mu);
if (!is_tool && role == "assistant" &&
!state.conversation.empty() &&
state.conversation.back().role == "assistant" &&
!state.conversation.back().is_tool) {
state.conversation.back().content = content;
} else {
TuiState::Message m{role, content, is_tool};
if (role == "tool_call") {
// 与 main.cpp on_message 一致:push 时算好紧凑预览,执行中
// 的工具行即显示 `● Bash(args)` 而非原始 JSON。
const auto parts = parse_tool_row(content, std::string());
if (!parts.name.empty()) {
m.display_override = ToolExecutor::build_tool_call_preview(
parts.name, parts.args);
}
}
state.conversation.push_back(std::move(m));
}
state.chat_follow_tail = true;
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_tool_confirm = [&state, &screen, &agent_aborting](const std::string& tool_name, const std::string& args) -> PermissionResult {
{
std::lock_guard<std::mutex> lk(state.mu);
state.confirm_pending = true;
state.confirm_tool_name = tool_name;
state.confirm_tool_args = args;
state.confirm_focus = 2;
}
screen.PostEvent(ftxui::Event::Custom);
std::unique_lock<std::mutex> lk(state.mu);
state.confirm_cv.wait(lk, [&state, &agent_aborting] {
return !state.confirm_pending || agent_aborting.load();
});
if (agent_aborting.load()) return PermissionResult::Deny;
return state.confirm_result;
};
callbacks.on_delta = [&state, &screen](const std::string& token) {
std::lock_guard<std::mutex> lk(state.mu);
if (state.conversation.empty() ||
state.conversation.back().role != "assistant" ||
state.conversation.back().is_tool) {
state.conversation.push_back({"assistant", "", false});
}
state.conversation.back().content += token;
state.streaming_output_chars += token.size();
state.chat_follow_tail = true;
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_tool_result = [&state, &screen](const ChatMessage& call_msg, const std::string&, const ToolResult& result) {
std::lock_guard<std::mutex> lk(state.mu);
for (auto it = state.conversation.rbegin(); it != state.conversation.rend(); ++it) {
if (it->role == "tool_result" && !it->summary.has_value()) {
it->summary = result.summary;
it->hunks = result.hunks;
break;
}
}
if (!call_msg.display_override.empty()) {
for (auto it = state.conversation.rbegin(); it != state.conversation.rend(); ++it) {
if (it->role == "tool_call" && it->display_override.empty()) {
it->display_override = call_msg.display_override;
break;
}
}
}
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_usage = [&state, &screen](const TokenUsage& usage) {
std::lock_guard<std::mutex> lk(state.mu);
state.token_status = std::to_string(usage.total_tokens);
// 心跳读数走回合累计:入账 + 清估算基数,与 main.cpp 内联版同步。
state.turn_completion_tokens_confirmed += usage.completion_tokens;
state.streaming_output_chars = 0;
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_goal_status = [&state, &screen](const std::string& status) {
std::lock_guard<std::mutex> lk(state.mu);
state.goal_status = status;
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_todo_updated = [&state, &screen](const nlohmann::json& payload) {
std::lock_guard<std::mutex> lk(state.mu);
if (payload.is_object() && payload.contains("todos")) {
state.todos = todo_items_from_json(payload["todos"]);
} else {
state.todos.clear();
}
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_transcript_replace = [&state, &screen](const std::vector<ChatMessage>&, const CompactResult& result) {
if (!result.performed || result.summary_text.empty()) return;
std::lock_guard<std::mutex> lk(state.mu);
state.conversation.push_back({"system", "--- [Compact Checkpoint] ---", false});
state.conversation.push_back({"system", "[Conversation summary]\n" + result.summary_text, false});
state.chat_follow_tail = true;
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_stream_retry_reset = [&state, &screen]() {
std::lock_guard<std::mutex> lk(state.mu);
if (!state.conversation.empty() &&
state.conversation.back().role == "assistant" &&
!state.conversation.back().is_tool) {
state.conversation.pop_back();
}
state.streaming_output_chars = 0;
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_model_retry = [&state, &screen](
const ProviderErrorInfo& info) {
std::lock_guard<std::mutex> lk(state.mu);
state.current_thinking_phrase =
model_retry_wait_phrase(
is_user_chinese(state), info.retry_delay_ms);
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_model_retry_resume = [&state, &screen]() {
std::lock_guard<std::mutex> lk(state.mu);
state.current_thinking_phrase =
model_retry_resume_phrase(is_user_chinese(state));
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_tool_progress_start = [&state, &screen](const std::string& tool_name, const std::string& cmd_preview) {
{
std::lock_guard<std::mutex> lk(state.mu);
state.tool_running = true;
state.tool_progress = {};
state.tool_progress.tool_name = tool_name;
state.tool_progress.command_preview = cmd_preview;
state.tool_progress.start_time = std::chrono::steady_clock::now();
state.last_tool_post_event_time = std::chrono::steady_clock::now();
}
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_tool_progress_update = [&state, &screen](const std::vector<std::string>& tail_snapshot, const std::string& current_partial, size_t total_bytes, int total_lines) {
bool should_post = false;
{
std::lock_guard<std::mutex> lk(state.mu);
state.tool_progress.tail_lines = tail_snapshot;
state.tool_progress.current_partial = current_partial;
state.tool_progress.total_bytes = total_bytes;
state.tool_progress.total_lines = total_lines;
auto now = std::chrono::steady_clock::now();
if (now - state.last_tool_post_event_time > std::chrono::milliseconds(150)) {
state.last_tool_post_event_time = now;
should_post = true;
}
}
if (should_post) screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_tool_progress_end = [&state, &screen]() {
{
std::lock_guard<std::mutex> lk(state.mu);
state.tool_running = false;
state.tool_progress = {};
}
screen.PostEvent(ftxui::Event::Custom);
};
callbacks.on_busy_changed = [&state, &screen, &ctx](bool busy) {
acecode::note_process_session_busy("tui-main", busy);
std::unique_lock<std::mutex> lk(state.mu);
if (busy && !state.is_waiting) {
state.current_thinking_phrase = get_random_thinking_phrase(is_user_chinese(state));
state.thinking_start_time = std::chrono::steady_clock::now();
state.streaming_output_chars = 0;
state.turn_completion_tokens_confirmed = 0;
}
const bool was_waiting = state.is_waiting;
state.is_waiting = busy;
// inline-thinking-heartbeat:回合正常收尾追加 "Done for Ns" 显示端
// 伪行,与 main.cpp 内联版同步;队列续跑分支在其后,保证 Done 行
// 先于下一条 user 行入列。
if (was_waiting && !busy) {
const bool interrupted = state.turn_interrupted_by_user;
state.turn_interrupted_by_user = false;
if (!interrupted &&
state.thinking_start_time.time_since_epoch().count() != 0) {
const long done_secs = static_cast<long>(
std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::steady_clock::now() -
state.thinking_start_time).count());
if (done_secs >= 1) {
state.conversation.push_back({"turn_done",
"Done for " + std::to_string(done_secs) + "s", false});
state.chat_follow_tail = true;
}
}
}
if (!busy) {
auto& rc_hub = acecode::rc::remote_control_service().hub();
if (rc_hub.enabled()) {
std::size_t cursor = rc_hub.forward_cursor();
if (cursor > state.conversation.size()) cursor = state.conversation.size();
for (std::size_t i = cursor; i < state.conversation.size(); ++i) {
const auto& m = state.conversation[i];
if (m.role == "assistant" && !m.is_tool) rc_hub.notify_assistant_text(m.content);
}
rc_hub.set_forward_cursor(state.conversation.size());
}
}
if (!busy && !state.pending_queue.empty()) {
std::string next_prompt = state.pending_queue.front();
state.pending_queue.erase(state.pending_queue.begin());
UserInput next_input;
bool has_structured_input = false;
if (!state.pending_structured_queue.empty() &&
state.pending_structured_queue.front().display_text == next_prompt) {
next_input = state.pending_structured_queue.front();
state.pending_structured_queue.pop_front();
has_structured_input = true;
}
state.conversation.push_back({"user", next_prompt, false});
if (state.drag_scrollbar_phase == TuiState::DragScrollbarPhase::Idle) state.chat_follow_tail = true;
state.current_thinking_phrase = get_random_thinking_phrase(is_user_chinese(state));
state.thinking_start_time = std::chrono::steady_clock::now();
state.streaming_output_chars = 0;
state.turn_completion_tokens_confirmed = 0;
state.is_waiting = true;
lk.unlock();
ctx.coordinate_mcp_before_first_turn();
lk.lock();
if (has_structured_input) ctx.agent_loop.submit(next_input);
else ctx.agent_loop.submit(next_prompt);
}
screen.PostEvent(ftxui::Event::Custom);
};
ctx.agent_loop.set_callbacks(callbacks);
acecode::rc::remote_control_service().hub().set_inbound_submit(
[&state, &screen, &ctx](const std::string& text) {
std::unique_lock<std::mutex> lk(state.mu);
if (state.is_waiting) {
state.pending_queue.push_back(text);
} else {
state.conversation.push_back({"user", text, false});
state.chat_follow_tail = true;
state.current_thinking_phrase = get_random_thinking_phrase(is_user_chinese(state));
state.thinking_start_time = std::chrono::steady_clock::now();
state.streaming_output_chars = 0;
state.turn_completion_tokens_confirmed = 0;
state.is_waiting = true;
lk.unlock();
ctx.coordinate_mcp_before_first_turn();
lk.lock();
ctx.agent_loop.submit(text);
}
screen.PostEvent(ftxui::Event::Custom);
});
}
}} // namespace acecode::tui