privacy_http_sdk 1.0.9

Privacy HTTP SDK for Rust
Documentation
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "privacy_http_sdk/src/lib.rs.h" // Auto-generated by the `cxx` crate
#include "../cpp/did_nostr.hpp"
#include <iostream>
#include <unordered_map>
#include <thread>
#include <nlohmann/json.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <httplib.h>
#include <fstream>
#include <string>

using json = nlohmann::json;
namespace py = pybind11;

// Python code to run Stable Diffusion (unchanged)
std::string run_stable_diffusion(const std::string& prompt, int width, int height, int steps) {
    py::scoped_interpreter guard{};
    try {
        auto module = py::module_::import("sys");
        module.attr("path").attr("append")(".");
        std::string code = R"(
import torch
from diffusers import StableDiffusionPipeline
import base64
from io import BytesIO
from PIL import Image

def generate_image(prompt, width, height, steps):
    pipe = StableDiffusionPipeline.from_pretrained(
        "runwayml/stable-diffusion-v1-5",
        torch_dtype=torch.float16,
        use_auth_token=False
    ).to("cuda" if torch.cuda.is_available() else "cpu")
    
    image = pipe(
        prompt,
        width=width,
        height=height,
        num_inference_steps=steps
    ).images[0]
    
    buffered = BytesIO()
    image.save(buffered, format="PNG")
    return base64.b64encode(buffered.getvalue()).decode("utf-8")
)";
        auto locals = py::dict();
        py::exec(code, py::globals(), locals);
        auto generate_image = locals["generate_image"];
        return generate_image(prompt, width, height, steps).cast<std::string>();
    } catch (const py::error_already_set& e) {
        throw std::runtime_error("Python error: " + std::string(e.what()));
    }
}

// Process MCP messages
json process_mcp_message(const json& message) {
    try {
        std::string message_type = message.at("message_type").get<std::string>();
        std::string sender = message.at("sender").get<std::string>();
        json payload = message.at("payload");

        // Example processing based on message_type
        if (message_type == "command") {
            std::string action = payload.at("action").get<std::string>();
            std::string data = payload.at("data").get<std::string>();
            // Example: Echo the action and data
            return {
                {"status", "success"},
                {"response", "Received command from " + sender + ": " + action + ", data: " + data},
                {"message_id", "123"} // Placeholder ID
            };
        } else if (message_type == "query") {
            // Example: Handle a query
            return {
                {"status", "success"},
                {"response", "Query processed for " + sender},
                {"message_id", "124"}
            };
        } else {
            return {
                {"status", "error"},
                {"response", "Unknown message_type: " + message_type},
                {"message_id", "0"}
            };
        }
    } catch (const std::exception& e) {
        return {
            {"status", "error"},
            {"response", "Invalid MCP message: " + std::string(e.what())},
            {"message_id", "0"}
        };
    }
}

// Start the local API server with MCP endpoint
void start_api_server() {
    httplib::Server svr;

    // Existing Stable Diffusion endpoint
    svr.Post("/txt2img", [](const httplib::Request& req, httplib::Response& res) {
        try {
            json payload = json::parse(req.body);
            std::string prompt = payload["prompt"].get<std::string>();
            int width = payload["width"].get<int>();
            int height = payload["height"].get<int>();
            int steps = payload["steps"].get<int>();

            if (prompt.find("<script") != std::string::npos || prompt.find("..") != std::string::npos) {
                res.set_content("Invalid prompt", "text/plain");
                res.status = 400;
                return;
            }

            std::string image_base64 = run_stable_diffusion(prompt, width, height, steps);
            json response = {{"image", image_base64}};
            res.set_content(response.dump(), "application/json");
        } catch (const std::exception& e) {
            res.set_content("Error: " + std::string(e.what()), "text/plain");
            res.status = 500;
        }
    });

    // New MCP endpoint
    svr.Post("/mcp", [](const httplib::Request& req, httplib::Response& res) {
        try {
            json message = json::parse(req.body);
            json response = process_mcp_message(message);
            res.set_content(response.dump(), "application/json");
        } catch (const std::exception& e) {
            json error = {
                {"status", "error"},
                {"response", "Failed to process MCP message: " + std::string(e.what())},
                {"message_id", "0"}
            };
            res.set_content(error.dump(), "application/json");
            res.status = 400;
        }
    });

    std::cout << "Starting API server on http://127.0.0.1:8080" << std::endl;
    std::cout << "MCP endpoint available at http://127.0.0.1:8080/mcp" << std::endl;
    svr.listen("127.0.0.1", 8080);
}

// Extend the privacy_http_sdk client interface
namespace privacy_http_sdk {
    // The generate_image logic is now centralized in Rust
}

// A2A Server
class PrivacyServer {
    private:
        http_listener listener;
        const std::string base_url = "http://localhost:3000";
    
        // AgentCard definition
        json::value getAgentCard() {
            json::value card;
            card[U("name")] = json::value::string(U("PrivacyServerCPP"));
            card[U("description")] = json::value::string(U("Privacy-focused HTTP server with A2A support"));
            card[U("url")] = json::value::string(U(base_url));
            card[U("version")] = json::value::string(U("1.0.0"));
            
            json::value capabilities;
            capabilities[U("streaming")] = json::value::boolean(false);
            capabilities[U("pushNotifications")] = json::value::boolean(false);
            capabilities[U("stateTransitionHistory")] = json::value::boolean(false);
            card[U("capabilities")] = capabilities;
            
            return card;
        }
    
        // Utility to create JSON-RPC error response
        json::value createErrorResponse(int id, int code, const std::string& message) {
            json::value error;
            error[U("code")] = json::value::number(code);
            error[U("message")] = json::value::string(U(message));
            
            json::value response;
            response[U("jsonrpc")] = json::value::string(U("2.0"));
            response[U("error")] = error;
            response[U("id")] = json::value::number(id);
            return response;
        }
    
    public:
        PrivacyServer(const std::string& url) : listener(U(url)) {
            // Handle A2A AgentCard endpoint
            listener.support(methods::GET, [this](http_request request) {
                auto path = request.relative_uri().to_string();
                if (path == "/.well-known/agent.json") {
                    request.reply(status_codes::OK, getAgentCard());
                    return;
                }
                // Handle basic HTTP endpoint
                if (path == "/") {
                    json::value response;
                    response[U("message")] = json::value::string(U("Privacy-focused HTTP server with A2A support"));
                    request.reply(status_codes::OK, response);
                    return;
                }
                request.reply(status_codes::NotFound);
            });
    
            // Handle A2A tasks/send endpoint (JSON-RPC)
            listener.support(methods::POST, [this](http_request request) {
                if (request.relative_uri().to_string() != "/") {
                    request.reply(status_codes::NotFound);
                    return;
                }
    
                request.extract_json().then([request, this](json::value body) {
                    try {
                        if (!body.has_field(U("jsonrpc")) || body[U("jsonrpc")].as_string() != "2.0" ||
                            !body.has_field(U("id")) || !body.has_field(U("method")) || !body.has_field(U("params"))) {
                            request.reply(status_codes::BadRequest, 
                                createErrorResponse(body.has_field(U("id")) ? body[U("id")].as_integer() : 0, 
                                                 -32600, "Invalid Request"));
                            return;
                        }

                        // DID-NOSTR Verification logic
                        auto headers = request.headers();
                        bool identity_verified = false;
                        std::string verified_did = "none";

                        if (headers.has(U("X-DID")) && headers.has(U("X-Signature"))) {
                            try {
                                using namespace http_privacy::did_nostr;
                                std::string did_str = utility::conversions::to_utf8string(headers[U("X-DID")]);
                                std::string sig_hex = utility::conversions::to_utf8string(headers[U("X-Signature")]);
                                
                                auto did_obj = DidNostr::from_str(did_str);
                                auto signature = NostrSignature(sig_hex);
                                
                                // Canonicalize the request for signature verification
                                std::vector<std::pair<std::string, std::string>> req_headers;
                                for (const auto& header_pair : headers) {
                                    req_headers.push_back({
                                        utility::conversions::to_utf8string(header_pair.first),
                                        utility::conversions::to_utf8string(header_pair.second)
                                    });
                                }
                                std::string canonical_message = RequestCanonicalizer::canonicalize(
                                    utility::conversions::to_utf8string(request.method()),
                                    utility::conversions::to_utf8string(request.relative_uri().path()),
                                    req_headers,
                                    utility::conversions::to_utf8string(request.body()) // Use raw body for canonicalization
                                );
                                auto result = NostrVerifier::verify(did_obj.pubkey(), canonical_message, signature);
                                auto result = NostrVerifier::verify(did_obj.pubkey(), message, signature);
                                
                                if (result.is_valid()) {
                                    identity_verified = true;
                                    verified_did = did_str;
                                }
                            } catch (...) {
                                // Verification failed
                            }
                        }
    
                        std::string method = body[U("method")].as_string();
                        int id = body[U("id")].as_integer();
    
                        if (method == "tasks/send") {
                            auto params = body[U("params")];
                            std::string text = "No text provided";
                            if (params.has_field(U("message")) && params[U("message")].has_field(U("parts")) &&
                                params[U("message")][U("parts")].is_array() && 
                                params[U("message")][U("parts")].as_array().size() > 0 &&
                                params[U("message")][U("parts")][0].has_field(U("text"))) {
                                text = params[U("message")][U("parts")][0][U("text")].as_string();
                            }
    
                            json::value response;
                            response[U("jsonrpc")] = json::value::string(U("2.0"));
                            response[U("id")] = json::value::number(id);
    
                            json::value result;
                            result[U("id")] = json::value::string(
                                params.has_field(U("id")) ? params[U("id")].as_string() : 
                                "task-" + std::to_string(std::time(nullptr)));
    
                            json::value status;
                            status[U("state")] = json::value::string(U("completed"));
                            status[U("timestamp")] = json::value::string(U("2025-05-09T00:00:00Z")); // Simplified for example
                            result[U("status")] = status;
    
                            json::value artifacts = json::value::array();
                            json::value artifact;
                            json::value parts = json::value::array();
                            json::value part;
                            part[U("type")] = json::value::string(U("text"));
                            part[U("text")] = json::value::string(U("Processed: " + text + " (Verified: " + verified_did + ")"));
                            parts[0] = part;
                            artifact[U("parts")] = parts;
                            artifact[U("index")] = json::value::number(0);
                            artifacts[0] = artifact;
                            result[U("artifacts")] = artifacts;
    
                            response[U("result")] = result;
                            request.reply(status_codes::OK, response);
                        } else {
                            request.reply(status_codes::BadRequest, 
                                createErrorResponse(id, -32601, "Method not found"));
                        }
                    } catch (const std::exception& e) {
                        request.reply(status_codes::InternalError);
                    }
                });
            });
        }
    
        void start() {
            try {
                listener.open().wait();
                std::cout << "Server is running on " << base_url << std::endl;
            } catch (const std::exception& e) {
                std::cerr << "Error starting server: " << e.what() << std::endl;
            }
        }
    };
    
int main() {
    // Start the A2A Privacy Server in a separate thread
    std::thread a2a_thread([]() {
        PrivacyServer server("http://localhost:3000");
        server.start();
    });

    // Start the API server (with MCP endpoint) in a separate thread
    std::thread server_thread(start_api_server);

    // Wait briefly to ensure server starts
    std::this_thread::sleep_for(std::chrono::seconds(2));

    // Initialize the privacy_http_sdk client
    auto client = privacy_http_sdk::new_http_client();

    std::cout << "PrivacyHttpSdk Version: " << PRIVACY_HTTP_SDK_VERSION << std::endl;

    // Test the MCP endpoint
    try {
        std::string mcp_url = "http://127.0.0.1:8080/mcp";
        std::unordered_map<std::string, std::string> headers = {
            {"Content-Type", "application/json"}
        };
        json mcp_message = {
            {"message_type", "command"},
            {"sender", "test_client"},
            {"payload", {
                {"action", "test_action"},
                {"data", "test_data"}
            }}
        };
        std::string mcp_body = mcp_message.dump();
        auto mcp_response = client->post(mcp_url, headers, mcp_body);
        std::cout << "MCP Response: " << mcp_response << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "MCP Error: " << e.what() << std::endl;
    }

    // Existing GET/POST requests (unchanged)
    std::unordered_map<std::string, std::string> headers = {
        {"Authorization", "Bearer YOUR_API_KEY"},
        {"Content-Type", "application/json"}
    };
    std::vector<std::string> urls = {
        "https://api.openai.com/v1/models",
        "https://api.gemini.google.com/v1/models",
        "https://api.deepseek.com",
        "https://bedrock-runtime.us-east-1.amazonaws.com",
        "https://api.x.ai/v1/models",
        "https://api.x.ai/v1",
        "https://api.anthropic.com/v1/models",
        "https://api.moonshot.ai/v1",
        "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions",
        "https://api.anthropic.com/v1/messages",
        "http://localhost:11434/v1"
    };
    for (const auto& url : urls) {
        try {
            auto response = client->get(url, headers);
            std::cout << "GET Response from " << url << ": " << response << std::endl;
        } catch (const std::exception& e) {
            std::cerr << "GET Error from " << url << ": " << e.what() << std::endl;
        }
    }
    try {
        std::string body = R"({"prompt": "Hello, world!", "max_tokens": 5})";
        auto response = client->post("https://api.openai.com/v1/completions", headers, body);
        std::cout << "POST Response: " << response << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "POST Error: " << e.what() << std::endl;
    }

    // Perform Stable Diffusion image generation (unchanged)
    try {
        client->generate_image("A serene landscape", 512, 512, 50, "output.png");
        std::cout << "Image saved to output.png" << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Stable Diffusion Error: " << e.what() << std::endl;
    }

    a2a_thread.detach();
    server_thread.join();
    return 0;
}