Skip to content

api/api_server.hpp

Orchestrates the full inference pipeline (PTDS §9) More...

Namespaces

Name
sgns
sgns::neoswarm
sgns::neoswarm::network
sgns::neoswarm::api

Classes

Name
class sgns::neoswarm::api::ApiServer
Orchestrates the full inference pipeline.
struct sgns::neoswarm::api::ApiServer::Config

Detailed Description

Orchestrates the full inference pipeline (PTDS §9)

Date: 2026-05-08

Source code

#ifndef NEOSWARM_API_SERVER_HPP
#define NEOSWARM_API_SERVER_HPP

#include "common/error.hpp"
#include "common/types.hpp"
#include "core/engine/inference_engine.hpp"
#include "knowledge/context_injection.hpp"
#include "knowledge/fact_validation.hpp"
#include "knowledge/knowledge_retrieval.hpp"
#include "network/p2p_node.hpp"
#include "network/result_aggregation.hpp"
#include "reputation/reputation_crdt.hpp"
#include "reputation/reputation_scoring.hpp"
#include "reputation/reputation_storage.hpp"
#include "reputation/weighted_consensus.hpp"
#include "router/rule_based_router.hpp"
#include "security/node_identity.hpp"
#include "specialists/grammar_specialist.hpp"
#include "specialists/math_specialist.hpp"
#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <string>

namespace sgns::neoswarm::network
{
    class SGClient;
}

namespace sgns::neoswarm::api
{
    class ApiServer
    {
        public:
        struct Config
        {
            std::string m_modelPath;
            std::string m_grammarModelPath;
            std::string m_mathModelPath;
            std::string m_reputationDbPath = "./reputation.db";
            std::string m_knowledgeFacts = "";
            bool m_enableNetwork = false;
            bool m_enableKnowledge = true;
            int m_grpcPort = 50051;
            std::string m_nodeKeyFile = "./node.key";
            std::string m_nodeKeyPassphrase = "gnus-neo-swarm-default";
            bool m_enableSgProcessing = false;
            bool m_sgProcessingNetworkMode = false;
            std::string m_sgSdkBasePath = "./sdk";
            uint16_t m_sgBasePort = 40001;
        };

        explicit ApiServer( Config cfg );
        ~ApiServer();

        outcome::result<void> Initialize();

        outcome::result<InferenceResponse> Process( const Task& task );

        outcome::result<void> Serve();

        void Stop();

        bool IsRunning() const
        {
            return m_running.load();
        }

        bool IsSuperGeniusConnected() const noexcept;

        bool IsSuperGeniusNetworkEnabled() const noexcept
        {
            return m_cfg.m_sgProcessingNetworkMode;
        }

        private:
        Config m_cfg;
        std::atomic<bool> m_running{ false };
        std::condition_variable m_stopCondition;
        std::mutex m_stopMutex;

        std::shared_ptr<security::NodeIdentity> m_identity;
        std::shared_ptr<core::InferenceEngine> m_coreEngine;
        std::shared_ptr<specialists::GrammarSpecialist> m_grammarSpec;
        std::shared_ptr<specialists::MathSpecialist> m_mathSpec;
        std::unique_ptr<router::RuleBasedRouter> m_router;
        std::unique_ptr<reputation::WeightedConsensus> m_consensus;
        std::unique_ptr<reputation::ReputationScoring> m_scoring;
        std::unique_ptr<reputation::ReputationStorage> m_repStorage;
        std::unique_ptr<reputation::ReputationCRDT> m_repCrdt;
        std::unique_ptr<network::P2PNode> m_p2pNode;
        std::unique_ptr<network::ResultAggregation> m_aggregation;
        std::shared_ptr<knowledge::KnowledgeRetrieval> m_knowledge;
        std::unique_ptr<knowledge::ContextInjection> m_contextInj;
        std::unique_ptr<knowledge::FactValidation> m_factVal;
        std::unique_ptr<network::SGClient> m_sgClient;

        outcome::result<InferenceResponse> RunSingleNode( const Task& task, const RouteDecision& route );
        outcome::result<InferenceResponse> RunSpecialist( const Task& task, const RouteDecision& route );
        outcome::result<InferenceResponse> RunSwarm( const Task& task, const RouteDecision& route );

        void InitializeEngine();
        void InitializeNetwork();

        std::string AugmentPrompt( const std::string& prompt, std::vector<KnowledgeFact>& out_facts ) const;

        void UpdateReputation( const InferenceResponse& resp,
                               double median_latency_ms,
                               const std::string& m_consensusoutput );
    };

} // namespace sgns::neoswarm::api

#endif // NEOSWARM_API_SERVER_HPP

Updated on 2026-07-25 at 22:56:57 +0000