-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_mount.cpp
More file actions
374 lines (360 loc) · 9.63 KB
/
Copy pathhttp_mount.cpp
File metadata and controls
374 lines (360 loc) · 9.63 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "http_mount.hpp"
#include "host.hpp"
// This file plus tools::MiniHttpServer is droidcli's gateway (the Edge-tier
// role ZeroClaw's zeroclaw-gateway plays - see ARCHITECTURE.md's crate
// comparison): the one thing external clients (curl, scripts, future
// dashboards) talk to. A future messaging-channel connector (Phase 4 of the
// extension plan) is the other Edge-tier piece, once one is actually
// requested - it does not belong in this file.
namespace droidcli::cli {
namespace {
void set_json(net::HttpResponse& response, const core::String& body, net::HttpStatus status = net::HttpStatus::Ok)
{
response.status = status;
response.content_type = "application/json";
response.body = body;
}
// Extracts a single "key=value" pair from a raw query string
// ("a=1&key=value&b=2"). Local to this file - net::handlers.cpp has its own
// equivalent in an anonymous namespace, not exported for reuse here.
core::String query_param(const core::String& query_string, const core::String& key)
{
const core::String needle = key + "=";
size_t cursor = 0;
while (cursor < query_string.size())
{
const size_t amp = query_string.find('&', cursor);
const core::String pair = query_string.substr(cursor, amp == core::String::npos ? core::String::npos : amp - cursor);
if (pair.rfind(needle, 0) == 0)
{
return pair.substr(needle.size());
}
if (amp == core::String::npos)
{
break;
}
cursor = amp + 1;
}
return {};
}
// Splits "/api/connectors/<id>/<rest>" into id + rest ("" if there's no rest
// segment). prefix must include the trailing slash, e.g. "/api/connectors/".
bool split_id_and_rest(
const core::String& path,
const core::String& prefix,
core::String& out_id,
core::String& out_rest)
{
if (path.rfind(prefix, 0) != 0)
{
return false;
}
const core::String remainder = path.substr(prefix.size());
const size_t slash = remainder.find('/');
if (slash == core::String::npos)
{
out_id = remainder;
out_rest.clear();
}
else
{
out_id = remainder.substr(0, slash);
out_rest = remainder.substr(slash);
}
return !out_id.empty();
}
} // namespace
tools::CustomRouteFn make_droidcli_route_dispatch(DroidHost& host)
{
return [&host](const net::HttpRequest& request, net::HttpResponse& response) -> bool
{
const core::String& path = request.path;
const bool is_get = request.method == net::HttpMethod::Get;
const bool is_post = request.method == net::HttpMethod::Post;
if (is_get && path == "/api/status")
{
set_json(response, host.build_status_json());
return true;
}
if (is_get && path == "/api/network/status")
{
set_json(response, host.build_network_status_json());
return true;
}
if (is_get && path == "/api/config")
{
set_json(response, host.build_config_json());
return true;
}
if (is_post && path == "/api/config")
{
set_json(response, host.update_config(request.body));
return true;
}
if (is_get && path == "/api/notify/log")
{
set_json(response, host.build_notify_log_json());
return true;
}
if (is_get && path == "/api/app/log")
{
set_json(response, host.build_app_log_json());
return true;
}
if (is_get && path == "/api/ollama/status")
{
set_json(response, host.build_ollama_status_json());
return true;
}
if (is_post && path == "/api/ollama/config")
{
set_json(response, host.update_ollama_config(request.body));
return true;
}
if (is_get && path == "/api/ollama/setup-status")
{
set_json(response, host.ollama_setup_status_json());
return true;
}
if (is_post && path == "/api/ollama/install")
{
set_json(response, host.install_ollama());
return true;
}
if (is_post && path == "/api/ollama/start")
{
set_json(response, host.start_ollama());
return true;
}
if (is_post && path == "/api/ollama/pull")
{
set_json(response, host.pull_ollama_model(request.body));
return true;
}
if (is_get && path == "/api/process/status")
{
set_json(response, host.build_process_status_json());
return true;
}
if (is_post && path == "/api/run")
{
set_json(response, host.run_command(request.body));
return true;
}
if (is_post && path == "/api/ffmpeg/run")
{
set_json(response, host.run_ffmpeg_json(request.body));
return true;
}
if (is_post && path == "/api/open")
{
set_json(response, host.open_application(request.body));
return true;
}
if (is_post && path == "/api/apps/find")
{
set_json(response, host.find_applications_json(request.body));
return true;
}
if (is_get && path == "/api/apps/open")
{
set_json(response, host.list_open_windows_json());
return true;
}
// Filesystem-aware agent tools - droidcli executes these itself, no
// external process or MCP server involved.
if (is_post && path == "/api/fs/read")
{
set_json(response, host.read_file(request.body));
return true;
}
if (is_post && path == "/api/fs/write")
{
set_json(response, host.write_file(request.body));
return true;
}
if (is_post && path == "/api/fs/list")
{
set_json(response, host.list_dir(request.body));
return true;
}
if (is_post && path == "/api/fs/stat")
{
set_json(response, host.stat_path(request.body));
return true;
}
if (is_get && path == "/api/fs/cwd")
{
set_json(response, host.get_cwd_json());
return true;
}
if (is_post && path == "/api/fs/which")
{
set_json(response, host.which_executable_json(request.body));
return true;
}
if (is_post && path == "/api/fs/copy")
{
set_json(response, host.copy_file_json(request.body));
return true;
}
if (is_post && path == "/api/fs/move")
{
set_json(response, host.move_path_json(request.body));
return true;
}
if (is_post && path == "/api/fs/delete")
{
set_json(response, host.delete_file_json(request.body));
return true;
}
if (is_post && path == "/api/fs/mkdir")
{
set_json(response, host.create_directory_json(request.body));
return true;
}
if (is_get && path == "/api/clipboard")
{
set_json(response, host.read_clipboard_json());
return true;
}
if (is_post && path == "/api/clipboard")
{
set_json(response, host.write_clipboard_json(request.body));
return true;
}
if (is_get && path == "/api/system")
{
set_json(response, host.build_system_info_json());
return true;
}
if (is_get && path == "/api/hardware")
{
set_json(response, host.build_hardware_info_json());
return true;
}
if (is_get && path == "/api/agent/tools")
{
set_json(response, host.build_agent_tools_json());
return true;
}
if (is_get && path == "/api/agent/self_status")
{
set_json(response, host.build_self_status_json());
return true;
}
if (is_post && path == "/api/agent/turn")
{
set_json(response, host.agent_turn(request.body));
return true;
}
if (is_post && path == "/api/agent/tool_decision")
{
set_json(response, host.agent_tool_decision(request.body));
return true;
}
if (is_post && path == "/api/agent/lessons")
{
set_json(response, host.record_command_fix_json(request.body));
return true;
}
if (is_post && path == "/api/agent/lessons/search")
{
set_json(response, host.search_command_fixes_json(request.body));
return true;
}
if (is_post && path == "/api/locations")
{
set_json(response, host.remember_location_json(request.body));
return true;
}
if (is_get && path == "/api/locations")
{
set_json(response, host.list_known_locations_json());
return true;
}
if (is_get && path == "/api/agent/history")
{
set_json(response, host.build_agent_history_json(query_param(request.query_string, "session_id")));
return true;
}
if (is_get && path == "/api/agent/sessions")
{
set_json(response, host.build_agent_sessions_json());
return true;
}
if (is_post && path == "/api/agent/sessions/delete")
{
set_json(response, host.delete_agent_session_json(request.body));
return true;
}
// Connectors.
if (is_get && path == "/api/connectors")
{
set_json(response, host.list_connectors_json());
return true;
}
if (is_post && path == "/api/connectors")
{
set_json(response, host.register_connector(request.body));
return true;
}
core::String connector_id;
core::String rest;
if (split_id_and_rest(path, "/api/connectors/", connector_id, rest))
{
if (is_get && rest == "/status")
{
set_json(response, host.connector_status_json(connector_id));
return true;
}
if (is_post && rest == "/launch")
{
set_json(response, host.launch_connector(connector_id));
return true;
}
if (is_post && rest == "/stop")
{
set_json(response, host.stop_connector(connector_id));
return true;
}
if (is_post && rest == "/call")
{
const core::String call_path = net::extract_json_string_field(request.body, "path");
const core::String method = net::extract_json_string_field(request.body, "method");
const core::String payload = net::extract_json_string_field(request.body, "payload_json");
set_json(response, host.call_connector(
connector_id, call_path, method.empty() ? "POST" : method, payload));
return true;
}
}
// Tasks.
if (is_get && path == "/api/tasks")
{
set_json(response, host.list_tasks_json());
return true;
}
if (is_post && path == "/api/tasks")
{
set_json(response, host.enqueue_task(request.body));
return true;
}
core::String task_id;
core::String task_rest;
if (split_id_and_rest(path, "/api/tasks/", task_id, task_rest))
{
if (is_get && task_rest.empty())
{
set_json(response, host.task_status_json(task_id));
return true;
}
if (is_post && task_rest == "/cancel")
{
set_json(response, host.cancel_task_json(task_id));
return true;
}
}
return false;
};
}
} // namespace droidcli::cli