-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.cpp
More file actions
170 lines (133 loc) · 4.99 KB
/
Copy pathagent.cpp
File metadata and controls
170 lines (133 loc) · 4.99 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
#include "agent_loop.hpp"
#include "agent.hpp"
#include "chat_message.hpp"
#include "file_tool.hpp"
#include "ollama_client.hpp"
#include "tool_request_parser.hpp"
#include "execution_logger.hpp"
#include <cstddef>
#include <exception>
#include <iostream>
#include <string>
#include <vector>
namespace {
std::string join_arguments(
const std::vector<std::string>& arguments,
std::size_t start_index) {
std::string result;
for (std::size_t index = start_index; index < arguments.size(); ++index) {
if (!result.empty()) {
result += ' ';
}
result += arguments[index];
}
return result;
}
} // namespace
void Agent::run() {
ExecutionLogger logger(".agentforge/logs/events.jsonl");
logger.log("session_start");
OllamaClient client;
FileTool file_tool;
AgentLoop loop(client, file_tool, logger);
ToolRequestParser parser;
std::vector<ChatMessage> history;
constexpr std::size_t max_history_messages = 8;
std::cout << "AgentForge C++\n";
std::cout << "Enter a task, '/read <path>' to read a file, "
"'/askfile <path> <question>' to ask about a file, "
"'/clear' to clear history, or 'exit' to quit.\n\n";
while (true) {
std::cout << "agentforge> " << std::flush;
std::string task;
if (!std::getline(std::cin, task)) {
std::cout << '\n';
break;
}
if (task == "exit") {
break;
}
const auto tool_request = parser.parse(task);
if (tool_request.has_value()) {
const std::string& command = tool_request->name;
const auto& arguments = tool_request->arguments;
if (command == "clear") {
if (!arguments.empty()) {
std::cout << "Usage: /clear\n\n";
} else {
history.clear();
std::cout << "Conversation history cleared.\n\n";
}
continue;
}
if (command == "read") {
if (arguments.size() != 1) {
std::cout << "Usage: /read <path>\n\n";
continue;
}
try {
std::cout << file_tool.read(arguments[0]) << "\n\n";
} catch (const std::exception& error) {
std::cerr << "File error: "
<< error.what() << "\n\n";
}
continue;
}
if (command == "askfile") {
if (arguments.size() < 2) {
std::cout << "Usage: /askfile <path> <question>\n\n";
continue;
}
const std::string& path = arguments[0];
const std::string question = join_arguments(arguments, 1);
try {
const std::string file_contents = file_tool.read(path);
const std::string model_task =
question + "\n\nFile contents from " + path + ":\n" +
file_contents;
auto next_history = history;
next_history.push_back({"user", model_task});
std::cout << "Contacting local model...\n" << std::flush;
const std::string answer = client.chat(next_history);
next_history.push_back({"assistant", answer});
if (next_history.size() > max_history_messages) {
next_history.erase(
next_history.begin(),
next_history.begin() + 2
);
}
history.swap(next_history);
std::cout << "Agent: " << answer << "\n\n";
} catch (const std::exception& error) {
std::cerr << "File or model error: "
<< error.what() << "\n\n";
}
continue;
}
std::cout << "Unknown command: /" << command << "\n\n";
continue;
}
if (task.find_first_not_of(" \t\r\n") == std::string::npos) {
std::cout << "Please enter a non-empty task.\n\n";
continue;
}
try {
auto next_history = history;
next_history.push_back({"user", task});
const std::string answer = loop.run(history, task);
next_history.push_back({"assistant", answer});
if (next_history.size() > max_history_messages) {
next_history.erase(
next_history.begin(),
next_history.begin() + 2
);
}
history.swap(next_history);
std::cout << "Agent: " << answer << "\n\n";
} catch (const std::exception& error) {
std::cerr << "Error: " << error.what() << "\n\n";
}
}
logger.log("session_end");
std::cout << "Goodbye!\n";
}