Files
2026-10-26 01:31:48 +01:00

369 lines
12 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 <string>
#include <string_view>
#include <unordered_map>
#include <functional>
#include <expected>
#include <nlohmann/json.hpp> // 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<std::string, std::string> execute(const Json& args, AgentContext& ctx) = 0;
};
class ToolRegistry {
public:
void add(std::unique_ptr<Tool> 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<Json> openai_tools() const {
std::vector<Json> 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<std::string, std::unique_ptr<Tool>> 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<GmailListTool>());
tools_.add(std::make_unique<GmailSendTool>());
tools_.add(std::make_unique<CalendarListTool>());
tools_.add(std::make_unique<DriveSearchTool>());
// ... 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<std::string> 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<ToolCall> tool_calls;
};
std::expected<LlmDecision, std::string> 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<std::string, std::string> 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 <string>
#include <vector>
#include <optional>
#include <nlohmann/json.hpp>
namespace molo::openai {
struct ChatMessage {
std::string role; // "system", "user", "assistant", "tool"
std::string content;
std::optional<std::string> name;
std::optional<std::string> tool_call_id;
std::optional<nlohmann::json> tool_calls;
};
struct ChatCompletionRequest {
std::string model;
std::vector<ChatMessage> messages;
std::optional<float> temperature;
std::optional<bool> stream;
std::optional<nlohmann::json> tools;
std::optional<std::string> 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<Choice> 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 <memory>
namespace molo::openai {
class ChatCompletionsHandler {
public:
explicit ChatCompletionsHandler(std::shared_ptr<agent::RootRouter> 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<void(std::string_view chunk)> 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<agent::RootRouter> root_;
};
} // namespace
```
### 5. Samenvoegen bij startup
```cpp
// main of app init
auto embedder = std::make_shared<YourEmbeddingProvider>();
std::vector<std::unique_ptr<agent::Agent>> domains;
domains.push_back(std::make_unique<agent::WorkspaceCoordinator>());
domains.push_back(std::make_unique<agent::VoiceCoordinator>());
domains.push_back(std::make_unique<agent::MapsCoordinator>());
domains.push_back(std::make_unique<agent::DataCoordinator>());
domains.push_back(std::make_unique<agent::LlmVendorCoordinator>());
domains.push_back(std::make_unique<agent::PlatformCoordinator>());
auto root = std::make_shared<agent::RootRouter>(std::move(domains), embedder);
root->warmup_embeddings();
auto handler = std::make_shared<openai::ChatCompletionsHandler>(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?