-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.cpp
More file actions
161 lines (138 loc) · 3.5 KB
/
Copy pathFileReader.cpp
File metadata and controls
161 lines (138 loc) · 3.5 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
/**
*
* @file FileReader.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/workspace/FileReader.hpp>
#include <string>
#include <string_view>
#include <utility>
#include <vix/ai/agent/AgentError.hpp>
#include <vix/fs/Exists.hpp>
#include <vix/fs/FsEntry.hpp>
#include <vix/fs/IsFile.hpp>
#include <vix/fs/ReadText.hpp>
#include <vix/fs/Size.hpp>
#include <vix/path/Filename.hpp>
#include <vix/path/Extension.hpp>
namespace vix::ai::agent
{
namespace
{
[[nodiscard]] AgentResult<vix::fs::FsEntry> build_entry(
const AgentWorkspace &workspace,
std::string_view path)
{
auto resolved = workspace.resolve_inside(path);
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,
"file does not exist");
}
auto is_file = vix::fs::is_file(resolved.value());
if (!is_file)
{
return is_file.error();
}
if (!is_file.value())
{
return make_agent_error(
AgentErrorCode::InvalidWorkspace,
"path is not a regular file");
}
auto file_size = vix::fs::size(resolved.value());
if (!file_size)
{
return file_size.error();
}
auto file_name = vix::path::filename(resolved.value());
if (!file_name)
{
return file_name.error();
}
vix::fs::FsEntry entry;
entry.path = resolved.value();
entry.name = file_name.value();
entry.type = vix::fs::FsEntryType::File;
entry.size = file_size.value();
entry.hidden = !entry.name.empty() && entry.name.front() == '.';
return entry;
}
} // namespace
FileReader::FileReader(
AgentWorkspace workspace,
FileScanPolicy policy)
: workspace_(std::move(workspace)),
policy_(std::move(policy))
{
}
AgentResult<ReadFileResult> FileReader::read_text(
std::string_view path) const
{
auto entry = build_entry(workspace_, path);
if (!entry)
{
return entry.error();
}
if (!policy_.should_read_file(entry.value()))
{
return make_agent_error(
AgentErrorCode::ToolNotAllowed,
"file is not allowed by the agent file scan policy");
}
auto content = vix::fs::read_text(entry.value().path);
if (!content)
{
return content.error();
}
auto relative = workspace_.relative_to_root(entry.value().path);
if (!relative)
{
return relative.error();
}
ReadFileResult result;
result.path = entry.value().path;
result.relative_path = relative.value();
result.content = content.value();
result.size = entry.value().size;
return result;
}
AgentResult<bool> FileReader::can_read(
std::string_view path) const
{
auto entry = build_entry(workspace_, path);
if (!entry)
{
return entry.error();
}
return policy_.should_read_file(entry.value());
}
const AgentWorkspace &FileReader::workspace() const noexcept
{
return workspace_;
}
const FileScanPolicy &FileReader::policy() const noexcept
{
return policy_;
}
} // namespace vix::ai::agent