-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent.cpp
More file actions
211 lines (186 loc) · 8.38 KB
/
Copy pathagent.cpp
File metadata and controls
211 lines (186 loc) · 8.38 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
// agent.cpp —— 入口与 REPL;实现见 agent_core.h / agent_util.cpp / agent_net.cpp / agent_computer.cpp / agent_text.cpp
#include "agent_core.h"
// ============================================================
// 主函数
// ============================================================
int main(int argc, char *argv[])
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
SetConsoleTitleA("YCode Agent v2.0 - World Domination Edition");
AddVectoredExceptionHandler(1, CrashVectoredHandler);
std::set_terminate(terminateHandler);
// 高 DPI 感知:确保截图/鼠标坐标使用物理像素(与 OCR 坐标一致)
typedef BOOL(WINAPI *SetDpiAwarenessContextFn)(void *);
HMODULE user32 = GetModuleHandleA("user32.dll");
if (user32)
{
auto fn = (SetDpiAwarenessContextFn)GetProcAddress(user32, "SetProcessDpiAwarenessContext");
if (fn)
fn((void *)-4); // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
}
#endif
std::cout << "========================================" << std::endl;
std::cout << " YCode Agent v2.0 - 世界征服版" << std::endl;
std::cout << " 多工具 | API重试 | 会话持久化 | 自进化分派 | 电脑控制" << std::endl;
std::cout << "========================================" << std::endl;
// --tool 调试模式:agent.exe --tool <name> <json-args>,直接执行工具并打印结果(无需 API Key)。
// 用 CommandLineToArgvW 解析 Unicode 命令行,避免中文参数被 ANSI 编码破坏。
int wargc = 0;
LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);
if (wargc >= 3 && wargv && wcscmp(wargv[1], L"--tool") == 0)
{
std::string toolName = wstringToUtf8(wargv[2]);
json args = json::object();
if (wargc >= 4)
{
try { args = json::parse(wstringToUtf8(wargv[3])); }
catch (const std::exception &e)
{
// 原先静默退化成空对象,后续 args["key"] 会断言崩溃;这里直接报错返回
std::cerr << "参数 JSON 解析失败: " << e.what() << std::endl;
LocalFree(wargv);
return 2;
}
}
if (wargv) LocalFree(wargv);
DeepSeekAgent agent("", ".");
std::cout << agent.runTool(toolName, args) << std::endl;
return 0;
}
if (wargv) LocalFree(wargv);
std::string apiKey;
{
const char *envKey = std::getenv("DEEPSEEK_API_KEY");
if (envKey && *envKey) apiKey = envKey;
}
if (apiKey.empty())
{
std::cout << "请输入 DeepSeek API Key: ";
std::getline(std::cin, apiKey);
}
if (apiKey.empty())
{
std::cerr << "Error: API Key 不能为空" << std::endl;
return 1;
}
std::string projectDir = ".";
if (argc > 1) projectDir = argv[1];
DeepSeekAgent agent(apiKey, projectDir);
// 托管调用方(如 YiyangzaiBot)可设置 YCODE_NO_SESSION=1 禁用会话持久化,
// 避免并发/重复调用时 agent_session.json 被多进程读写损坏。
const char *noSessionEnv = std::getenv("YCODE_NO_SESSION");
bool noSession = noSessionEnv && std::string(noSessionEnv) == "1";
std::string input;
std::vector<json> conversationHistory = noSession ? std::vector<json>() : agent.loadSession();
// 托管模式下由 Qt 客户端负责展示用户/助手角色,跳过 REPL 提示符
const char *managedEnv = std::getenv("YCODE_MANAGED");
bool managed = managedEnv && std::string(managedEnv) == "1";
std::cout << "\n命令: /exit /clear /save /load /restart /self-update /apply-self-changes /temp 0.5 /model name /allow-dangerous /deny-dangerous /allow-control /deny-control /help" << std::endl;
std::cout << "----------------------------------------" << std::endl;
while (true)
{
if (!managed)
std::cout << "\n你: ";
std::getline(std::cin, input);
if (std::cin.eof())
break; // 标准输入关闭(如客户端退出),干净退出避免空转
if (input == "/exit" || input == "/quit")
{
if (!noSession && !conversationHistory.empty()) agent.saveSession(conversationHistory);
std::cout << "再见!" << std::endl;
break;
}
if (input == "/clear")
{
system("cls");
conversationHistory.clear();
std::cout << "对话已清空" << std::endl;
continue;
}
// ★ /restart — 发送信号给 YCode 客户端(如果是托管模式)
if (input == "/restart")
{
if (!noSession && !conversationHistory.empty())
{
agent.saveSession(conversationHistory);
std::cout << "会话已保存。" << std::endl;
}
std::cout << "正在重启 Agent..." << std::endl;
restartAgent(false); // 托管模式:打印 SIGNAL:RESTART_AGENT 并 exit(0)
continue;
}
if (input == "/self-update" || input == "/rebuild-restart")
{
if (!noSession && !conversationHistory.empty())
{
agent.saveSession(conversationHistory);
std::cout << "会话已保存。" << std::endl;
}
std::cout << "正在请求 YCode 完整自更新..." << std::endl;
requestYCodeRebuildAndRestart();
continue;
}
if (input == "/apply-self-changes")
{
std::cout << agent.chat("请调用 apply_self_changes 应用已记录的 YCode 自身变更。", conversationHistory) << std::endl;
continue;
}
if (input == "/save") { if (!noSession) agent.saveSession(conversationHistory); std::cout << "已保存" << std::endl; continue; }
if (input == "/load") { conversationHistory = noSession ? std::vector<json>() : agent.loadSession(); continue; }
if (input.substr(0, 6) == "/temp ")
{
try
{
double t = std::stod(input.substr(6));
if (t >= 0.0 && t <= 1.5) { agent.setTemperature(t); std::cout << "温度已设为 " << t << std::endl; }
else { std::cout << "温度需在 0.0-1.5 之间" << std::endl; }
}
catch (...) { std::cout << "无效的温度值" << std::endl; }
continue;
}
if (input.substr(0, 7) == "/model ")
{
std::string m = input.substr(7);
agent.setModel(m);
std::cout << "模型已设为 " << m << std::endl;
continue;
}
if (input == "/allow-dangerous")
{
agent.setAllowDangerousCommands(true);
agent.setAllowComputerControl(true);
std::cout << "已授权执行危险命令和电脑控制(用 /deny-dangerous 撤销)。请谨慎使用。" << std::endl;
continue;
}
if (input == "/deny-dangerous")
{
agent.setAllowDangerousCommands(false);
agent.setAllowComputerControl(false);
std::cout << "已关闭危险命令与电脑控制授权。" << std::endl;
continue;
}
if (input == "/allow-control")
{
agent.setAllowComputerControl(true);
std::cout << "已授权电脑控制(鼠标/键盘/窗口/启动程序)。用 /deny-control 撤销。" << std::endl;
continue;
}
if (input == "/deny-control")
{
agent.setAllowComputerControl(false);
std::cout << "已关闭电脑控制授权。" << std::endl;
continue;
}
if (input == "/help") { std::cout << "YCode Agent v2.0 - " << agent.toolCount() << "个工具的全能编程助手 | apply_self_changes 按路径热加载/重建/重启 | git_commit/git_push 提交推送 | /restart 重启 Agent | /self-update 重建并重启 YCode | /allow-dangerous 授权危险命令 | /allow-control 授权电脑控制 | /deny-dangerous 撤销授权" << std::endl; continue; }
if (input.empty()) continue;
if (!managed)
std::cout << "Agent: " << std::flush;
std::string response = agent.chat(input, conversationHistory);
std::cout << response << std::endl;
if (!noSession)
agent.saveSession(conversationHistory); // 每次响应后自动保存,避免崩溃丢失历史
}
return 0;
}