-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest_server_transport.h
More file actions
80 lines (64 loc) · 2.81 KB
/
Copy pathrest_server_transport.h
File metadata and controls
80 lines (64 loc) · 2.81 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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Vladimir Pavlov <mistervvp@outlook.com> (https://github.com/MisterVVP)
#pragma once
#include <chrono>
#include <functional>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include "a2a/core/result.h"
#include "a2a/server/required_extensions_validator.h"
#include "a2a/server/rest_transport.h"
#include "a2a/v1/a2a.pb.h"
namespace a2a::server {
class HttpByteTransport;
struct HttpServerRequest final {
std::string method;
std::string target;
std::unordered_map<std::string, std::string> headers;
std::string body;
std::string remote_address;
};
struct HttpServerResponse final {
static constexpr int kDefaultStatusCode = 500;
int status_code = kDefaultStatusCode;
std::unordered_map<std::string, std::string> headers;
std::string body;
std::function<core::Result<void>(HttpByteTransport&)> stream_writer;
};
struct RestServerTransportOptions final {
struct AgentCardCacheSettings final {
std::optional<std::string> cache_control;
std::optional<std::chrono::system_clock::time_point> last_modified;
};
std::string rest_api_base_path = "/";
bool require_version_header = true;
bool include_legacy_transport_fields = true;
std::optional<AgentCardCacheSettings> agent_card_cache_settings;
std::vector<std::string> required_extensions = {};
};
class RestServerTransport final {
public:
static constexpr std::string_view kAgentCardPath = "/.well-known/agent-card.json";
static constexpr std::string_view kLegacyAgentCardPath = "/.well-known/agent.json";
static constexpr std::string_view kExtendedAgentCardPath = "/extendedAgentCard";
RestServerTransport(Dispatcher* dispatcher, lf::a2a::v1::AgentCard agent_card,
RestServerTransportOptions options = {});
[[nodiscard]] core::Result<HttpServerResponse> Handle(const HttpServerRequest& request) const;
private:
[[nodiscard]] core::Result<RestRequest> BuildRestRequest(const HttpServerRequest& request) const;
[[nodiscard]] core::Result<void> ValidateVersionHeader(const HttpServerRequest& request) const;
[[nodiscard]] core::Result<HttpServerResponse> HandleAgentCard(const HttpServerRequest& request) const;
[[nodiscard]] core::Result<HttpServerResponse> HandleExtendedAgentCard(const HttpServerRequest& request) const;
[[nodiscard]] static HttpServerResponse ToHttpResponse(const RestResponse& response,
const std::vector<std::string>& activated_extensions);
static std::string NormalizeBasePath(std::string_view path);
Dispatcher* dispatcher_ = nullptr;
RestTransport transport_;
lf::a2a::v1::AgentCard agent_card_;
RestServerTransportOptions options_;
RequiredExtensionsValidator required_extensions_validator_;
};
} // namespace a2a::server