| 1182 | } |
| 1183 | |
| 1184 | nlohmann::json CloudTunnel::HandleRequest(const nlohmann::json& request) { |
| 1185 | const std::string id = request.value("id", GenerateUuid()); |
| 1186 | const std::string type = request.value("type", ""); |
| 1187 | const std::string vm_id = request.value("vm_id", ""); |
| 1188 | nlohmann::json payload; |
| 1189 | |
| 1190 | if (type == "host.resources" || type == "device.capabilities") { |
| 1191 | payload = HostResourcesPayload(); |
| 1192 | } else if (type == "image.cached.list") { |
| 1193 | payload = ImageListPayload(); |
| 1194 | } else if (type == "image.cached.delete") { |
| 1195 | payload = DeleteCachedImage(request.value("payload", nlohmann::json::object())); |
| 1196 | } else if (type == "image.download.start") { |
| 1197 | payload = StartImageDownload(request.value("payload", nlohmann::json::object())); |
| 1198 | } else if (type == "image.download.list") { |
| 1199 | payload = ImageDownloadList(); |
| 1200 | } else if (type == "image.download.status") { |
| 1201 | payload = ImageDownloadStatus(request.value("payload", nlohmann::json::object())); |
| 1202 | } else if (type == "image.download.cancel") { |
| 1203 | payload = CancelImageDownload(request.value("payload", nlohmann::json::object())); |
| 1204 | } else if (type == "remote_session.create") { |
| 1205 | payload = CreateRemoteSession(vm_id, request.value("payload", nlohmann::json::object())); |
| 1206 | } else if (type == "remote_session.resize") { |
| 1207 | payload = ResizeRemoteSession(vm_id, request.value("payload", nlohmann::json::object())); |
| 1208 | } else if (type == "remote_session.close") { |
| 1209 | payload = CloseRemoteSession(vm_id, request.value("payload", nlohmann::json::object())); |
| 1210 | } else if (type == "remote_session.configure" || |
| 1211 | type.rfind("remote_signal.", 0) == 0) { |
| 1212 | payload = HandleRemoteSignal(vm_id, type, request.value("payload", nlohmann::json::object())); |
| 1213 | } else if (type == "vm.list") { |
| 1214 | payload = VmListPayload(); |
| 1215 | } else if (type == "vm.create") { |
| 1216 | payload = CreateVm(request.value("payload", nlohmann::json::object())); |
| 1217 | } else if (type == "vm.edit") { |
| 1218 | payload = EditVm(vm_id, request.value("payload", nlohmann::json::object())); |
| 1219 | } else if (type == "vm.delete") { |
| 1220 | payload = DeleteVm(vm_id); |
| 1221 | } else if (type == "host.llm_proxy.get") { |
| 1222 | payload = GetLlmProxySettings(); |
| 1223 | } else if (type == "host.llm_proxy.set") { |
| 1224 | payload = UpdateLlmProxySettings(request.value("payload", nlohmann::json::object())); |
| 1225 | } else if (type == "vm.start") { |
| 1226 | std::string error; |
| 1227 | if (!runtime_manager_.StartVm(vm_id, &error)) { |
| 1228 | // Pull the structured failure code from the store so the cloud |
| 1229 | // sees `kvm_unsupported` / `kernel_missing` / `port_conflict` |
| 1230 | // instead of the generic `vm_start_failed` umbrella. |
| 1231 | std::string code = "vm_start_failed"; |
| 1232 | if (auto record = store_.Get(vm_id); record && record->runtime.last_failure) { |
| 1233 | code = record->runtime.last_failure->code; |
| 1234 | } |
| 1235 | return {{"id", id}, {"type", type + ".response"}, {"host_id", host_id_}, |
| 1236 | {"vm_id", vm_id}, {"payload", Error(code, error)}}; |
| 1237 | } |
| 1238 | payload = nlohmann::json::object(); |
| 1239 | } else if (type == "vm.stop" || type == "vm.shutdown") { |
| 1240 | std::string error; |
| 1241 | if (!runtime_manager_.StopVm(vm_id, &error)) { |