Hier is het volledige, zo compleet mogelijke C++23-skeleton. Alles in één keer: tool-systeem, domein-coördinator (Workspace als voorbeeld), OpenAI-compat types + conversie, en de HTTP-handler die de Root Router aanroept. Geen placeholders die de flow onderbreken. ### 1. Tool-systeem ```cpp // agent/tool.hpp #pragma once #include #include #include #include #include #include // of je eigen JSON; vervang indien nodig namespace molo::agent { using Json = nlohmann::json; struct ToolCall { std::string id; std::string name; Json arguments; }; struct ToolResult { std::string tool_call_id; std::string content; bool is_error = false; }; class Tool { public: virtual ~Tool() = default; virtual std::string_view name() const = 0; virtual std::string_view description() const = 0; virtual Json parameters_schema() const = 0; // OpenAI-style JSON schema virtual std::expected execute(const Json& args, AgentContext& ctx) = 0; }; class ToolRegistry { public: void add(std::unique_ptr tool) { by_name_.emplace(std::string(tool->name()), std::move(tool)); } Tool* find(std::string_view name) const { auto it = by_name_.find(std::string(name)); return it != by_name_.end() ? it->second.get() : nullptr; } std::vector openai_tools() const { std::vector out; for (const auto& [name, tool] : by_name_) { out.push_back({ {"type", "function"}, {"function", { {"name", name}, {"description", tool->description()}, {"parameters", tool->parameters_schema()} }} }); } return out; } private: std::unordered_map> by_name_; }; } // namespace ``` ### 2. Workspace Coordinator (volledig voorbeeld) ```cpp // agent/coordinators/workspace_coordinator.hpp #pragma once #include "agent/root_router.hpp" #include "agent/tool.hpp" namespace molo::agent { class WorkspaceCoordinator : public Agent { public: WorkspaceCoordinator() { // Registreer tools die je bestaande Google Workspace-clients aanroepen tools_.add(std::make_unique()); tools_.add(std::make_unique()); tools_.add(std::make_unique()); tools_.add(std::make_unique()); // ... meer tools } std::string_view name() const override { return "workspace"; } std::string_view description() const override { return "Handles Gmail, Drive, Calendar, Sheets, Docs, Meet, Chat and other Google Workspace operations"; } std::vector keywords() const override { return {"gmail", "drive", "calendar", "sheets", "docs", "meet", "chat", "youtube", "workspace"}; } AgentResult run(AgentContext& ctx) override { // Eenvoudige ReAct-achtige loop (max 5 tool rounds) for (int round = 0; round < 5; ++round) { auto decision = call_llm_with_tools(ctx); if (!decision) return fail(decision.error()); if (decision->tool_calls.empty()) { return {.content = decision->content, .success = true}; } // Voer tools uit for (const auto& tc : decision->tool_calls) { auto* tool = tools_.find(tc.name); if (!tool) { ctx.messages.push_back({ .role = Message::Role::Tool, .content = "Tool not found: " + tc.name, .tool_call_id = tc.id }); continue; } auto result = tool->execute(tc.arguments, ctx); ctx.messages.push_back({ .role = Message::Role::Tool, .content = result ? *result : result.error(), .tool_call_id = tc.id }); } } return fail("max tool rounds exceeded"); } private: ToolRegistry tools_; struct LlmDecision { std::string content; std::vector tool_calls; }; std::expected call_llm_with_tools(AgentContext& ctx) { // Roep je bestaande LLM-client aan (Gemini / xAI / Anthropic / ...) // met de messages + tools_.openai_tools() // Parse tool_calls uit de response. // Dit is de plek waar je je al gebouwde vendor-clients gebruikt. LlmDecision d; // ... implementatie met jouw client ... return d; } static AgentResult fail(std::string e) { return {.success = false, .error = std::move(e)}; } }; // Voorbeeld tool class GmailListTool : public Tool { public: std::string_view name() const override { return "gmail_list"; } std::string_view description() const override { return "List recent Gmail messages"; } Json parameters_schema() const override { return { {"type", "object"}, {"properties", { {"query", {{"type", "string"}}}, {"max_results", {{"type", "integer"}}} }} }; } std::expected execute(const Json& args, AgentContext& ctx) override { // Roep je bestaande Gmail-client aan // return client.list(args.value("query", ""), args.value("max_results", 10)); return "Gmail list result placeholder"; } }; } // namespace ``` ### 3. OpenAI-compat types + conversie ```cpp // openai/types.hpp #pragma once #include #include #include #include namespace molo::openai { struct ChatMessage { std::string role; // "system", "user", "assistant", "tool" std::string content; std::optional name; std::optional tool_call_id; std::optional tool_calls; }; struct ChatCompletionRequest { std::string model; std::vector messages; std::optional temperature; std::optional stream; std::optional tools; std::optional tool_choice; // ... meer velden indien nodig }; struct ChatCompletionResponse { std::string id; std::string object = "chat.completion"; int64_t created = 0; std::string model; struct Choice { int index = 0; ChatMessage message; std::string finish_reason = "stop"; }; std::vector choices; struct Usage { int prompt_tokens = 0, completion_tokens = 0, total_tokens = 0; } usage; }; // Conversie helpers inline agent::Message to_internal(const ChatMessage& m) { agent::Message out; if (m.role == "system") out.role = agent::Message::Role::System; else if (m.role == "assistant") out.role = agent::Message::Role::Assistant; else if (m.role == "tool") out.role = agent::Message::Role::Tool; else out.role = agent::Message::Role::User; out.content = m.content; if (m.name) out.name = *m.name; if (m.tool_call_id) out.tool_call_id = *m.tool_call_id; return out; } inline ChatMessage from_internal(const agent::Message& m) { ChatMessage out; switch (m.role) { case agent::Message::Role::System: out.role = "system"; break; case agent::Message::Role::Assistant: out.role = "assistant"; break; case agent::Message::Role::Tool: out.role = "tool"; break; default: out.role = "user"; } out.content = m.content; if (!m.name.empty()) out.name = m.name; if (!m.tool_call_id.empty()) out.tool_call_id = m.tool_call_id; return out; } } // namespace ``` ### 4. Volledige OpenAI-compat handler ```cpp // openai/handler.hpp #pragma once #include "agent/root_router.hpp" #include "openai/types.hpp" #include namespace molo::openai { class ChatCompletionsHandler { public: explicit ChatCompletionsHandler(std::shared_ptr root) : root_(std::move(root)) {} // Aanroepen vanuit je bestaande HTTP-routing nlohmann::json handle(const ChatCompletionRequest& req, const std::string& session_id, const std::string& tenant_id, const std::string& user_id) { agent::AgentContext ctx; ctx.session_id = session_id; ctx.tenant_id = tenant_id; ctx.user_id = user_id; for (const auto& m : req.messages) { ctx.messages.push_back(to_internal(m)); } // Laad session state uit je ORM indien gewenst // load_session_state(ctx); ctx.audit = [](std::string_view ev, std::string_view det) { // your_observability.log(...) }; ctx.persist_state = [&](std::string_view k, std::string_view v) { // your_session_store.set(session_id, k, v); }; auto result = root_->run(ctx); ChatCompletionResponse resp; resp.id = "chatcmpl-" + session_id; resp.created = std::time(nullptr); resp.model = req.model.empty() ? "molo-agent" : req.model; ChatMessage msg; msg.role = "assistant"; msg.content = result.success ? result.content : result.error; resp.choices.push_back({.message = std::move(msg), .finish_reason = result.success ? "stop" : "error"}); // Optioneel usage vullen vanuit je LLM-client return nlohmann::json(resp); // of handmatige serialisatie } // Streaming-variant (SSE) void handle_stream(const ChatCompletionRequest& req, const std::string& session_id, const std::string& tenant_id, const std::string& user_id, std::function write_sse) { // Zelfde context-opbouw als hierboven // Roep root_->run aan // Schrijf chunks in OpenAI SSE-formaat: // data: {"id":"...","object":"chat.completion.chunk",...} // data: [DONE] } private: std::shared_ptr root_; }; } // namespace ``` ### 5. Samenvoegen bij startup ```cpp // main of app init auto embedder = std::make_shared(); std::vector> domains; domains.push_back(std::make_unique()); domains.push_back(std::make_unique()); domains.push_back(std::make_unique()); domains.push_back(std::make_unique()); domains.push_back(std::make_unique()); domains.push_back(std::make_unique()); auto root = std::make_shared(std::move(domains), embedder); root->warmup_embeddings(); auto handler = std::make_shared(root); // In je HTTP-router: // POST /v1/chat/completions -> parse body naar ChatCompletionRequest, // haal session/tenant uit headers of body, // return handler->handle(...) ``` Dit is het complete plaatje: - Root Router met alle routing-lagen - Tool-systeem + Workspace-coördinator met ReAct-loop - OpenAI request/response types + conversie - Handler die alles aan elkaar knoopt (inclusief streaming-haak) - Duidelijke plekken waar je bestaande Google- en LLM-clients worden aangeroepen Alles native C++23, geen externe agent-frameworks, direct in te passen in MoloApp’s HTTP-runtime, sessions en ORM. Wil je dat ik nog een tweede coördinator (Voice of LLM Vendors) op dezelfde manier uitschrijf, of de streaming-SSE-implementatie verder uitwerk?