-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentWorkspace.cpp
More file actions
333 lines (279 loc) · 6.71 KB
/
Copy pathAgentWorkspace.cpp
File metadata and controls
333 lines (279 loc) · 6.71 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
*
* @file AgentWorkspace.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira.
* All rights reserved.
* https://github.com/vixcpp/vix
*
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/ai/agent/AgentWorkspace.hpp>
#include <string>
#include <string_view>
#include <utility>
#include <vix/ai/agent/AgentError.hpp>
#include <vix/fs/EnsureDirectory.hpp>
#include <vix/fs/Exists.hpp>
#include <vix/fs/IsDirectory.hpp>
#include <vix/fs/CurrentPath.hpp>
#include <vix/path/Absolute.hpp>
#include <vix/path/IsAbsolute.hpp>
#include <vix/path/Join.hpp>
#include <vix/path/Normalize.hpp>
#include <vix/path/Relative.hpp>
namespace vix::ai::agent
{
namespace
{
[[nodiscard]] bool starts_with_path(
std::string_view value,
std::string_view prefix) noexcept
{
if (value.size() < prefix.size())
{
return false;
}
if (value.substr(0, prefix.size()) != prefix)
{
return false;
}
if (value.size() == prefix.size())
{
return true;
}
const char next = value[prefix.size()];
return next == '/' || next == '\\';
}
[[nodiscard]] AgentResult<std::string> absolute_from_current(
std::string_view path)
{
auto current = vix::fs::current_path();
if (!current)
{
return current.error();
}
auto resolved = vix::path::absolute(path, current.value());
if (!resolved)
{
return resolved.error();
}
return resolved.value();
}
} // namespace
AgentWorkspace::AgentWorkspace(std::string root, AgentConfig config)
: root_(std::move(root)),
config_(std::move(config))
{
}
AgentResult<AgentWorkspace> AgentWorkspace::open(
std::string_view root,
const AgentConfig &config)
{
const std::string_view requested_root = root.empty() ? "." : root;
auto resolved = absolute_from_current(requested_root);
if (!resolved)
{
return resolved.error();
}
auto exists = vix::fs::exists(resolved.value());
if (!exists)
{
return exists.error();
}
if (!exists.value())
{
return make_agent_error(
AgentErrorCode::InvalidWorkspace,
"workspace path does not exist");
}
auto is_dir = vix::fs::is_directory(resolved.value());
if (!is_dir)
{
return is_dir.error();
}
if (!is_dir.value())
{
return make_agent_error(
AgentErrorCode::InvalidWorkspace,
"workspace path is not a directory");
}
AgentWorkspace workspace(resolved.value(), config);
auto layout_error = workspace.ensure_layout();
if (layout_error)
{
return layout_error;
}
return workspace;
}
vix::error::Error AgentWorkspace::ensure_layout() const
{
auto memory = memory_dir();
if (!memory)
{
return memory.error();
}
auto cache = cache_dir();
if (!cache)
{
return cache.error();
}
auto runs = runs_dir();
if (!runs)
{
return runs.error();
}
auto logs = logs_dir();
if (!logs)
{
return logs.error();
}
auto ok = vix::fs::ensure_directory(memory.value());
if (!ok)
{
return ok.error();
}
ok = vix::fs::ensure_directory(cache.value());
if (!ok)
{
return ok.error();
}
ok = vix::fs::ensure_directory(runs.value());
if (!ok)
{
return ok.error();
}
ok = vix::fs::ensure_directory(logs.value());
if (!ok)
{
return ok.error();
}
return {};
}
const std::string &AgentWorkspace::root() const noexcept
{
return root_;
}
bool AgentWorkspace::valid() const noexcept
{
return !root_.empty();
}
AgentResult<std::string> AgentWorkspace::resolve_inside(
std::string_view path) const
{
if (!valid())
{
return make_agent_error(
AgentErrorCode::InvalidWorkspace,
"workspace is not initialized");
}
if (path.empty())
{
return make_agent_error(
AgentErrorCode::PathOutsideWorkspace,
"path cannot be empty");
}
AgentResult<std::string> resolved =
vix::path::is_absolute(path)
? vix::path::normalize(path)
: vix::path::absolute(path, root_);
if (!resolved)
{
return resolved.error();
}
if (!contains(resolved.value()))
{
return make_agent_error(
AgentErrorCode::PathOutsideWorkspace,
"path is outside the agent workspace");
}
return resolved.value();
}
AgentResult<std::string> AgentWorkspace::relative_to_root(
std::string_view path) const
{
auto resolved = resolve_inside(path);
if (!resolved)
{
return resolved.error();
}
auto relative = vix::path::relative(resolved.value(), root_);
if (!relative)
{
return relative.error();
}
return relative.value();
}
bool AgentWorkspace::contains(std::string_view path) const
{
if (root_.empty() || path.empty())
{
return false;
}
auto normalized = vix::path::normalize(path);
if (!normalized)
{
return false;
}
return starts_with_path(normalized.value(), root_);
}
AgentResult<std::string> AgentWorkspace::memory_dir() const
{
return resolve_config_path(config_.memory_dir);
}
AgentResult<std::string> AgentWorkspace::cache_dir() const
{
return resolve_config_path(config_.cache_dir);
}
AgentResult<std::string> AgentWorkspace::runs_dir() const
{
return resolve_config_path(config_.runs_dir);
}
AgentResult<std::string> AgentWorkspace::logs_dir() const
{
return resolve_config_path(config_.logs_dir);
}
AgentResult<std::string> AgentWorkspace::run_dir(
std::string_view run_id) const
{
if (run_id.empty())
{
return make_agent_error(
AgentErrorCode::InvalidWorkspace,
"run id cannot be empty");
}
auto base = runs_dir();
if (!base)
{
return base.error();
}
auto joined = vix::path::join(base.value(), run_id);
if (!joined)
{
return joined.error();
}
if (!contains(joined.value()))
{
return make_agent_error(
AgentErrorCode::PathOutsideWorkspace,
"run directory is outside the workspace");
}
return joined.value();
}
AgentResult<std::string> AgentWorkspace::resolve_config_path(
std::string_view configured_path) const
{
if (configured_path.empty())
{
return make_agent_error(
AgentErrorCode::InvalidWorkspace,
"configured workspace path cannot be empty");
}
return resolve_inside(configured_path);
}
} // namespace vix::ai::agent