2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
import asyncio
|
2025-05-29 16:33:21 +01:00
|
|
|
from aiohttp import web
|
2025-05-02 21:11:50 +01:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
from . config import ConfigRequestor
|
|
|
|
|
from . flow import FlowRequestor
|
|
|
|
|
from . librarian import LibrarianRequestor
|
2025-05-06 23:44:10 +01:00
|
|
|
from . knowledge import KnowledgeRequestor
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
from . embeddings import EmbeddingsRequestor
|
|
|
|
|
from . agent import AgentRequestor
|
|
|
|
|
from . text_completion import TextCompletionRequestor
|
|
|
|
|
from . prompt import PromptRequestor
|
|
|
|
|
from . graph_rag import GraphRagRequestor
|
|
|
|
|
from . document_rag import DocumentRagRequestor
|
|
|
|
|
from . triples_query import TriplesQueryRequestor
|
|
|
|
|
from . embeddings import EmbeddingsRequestor
|
|
|
|
|
from . graph_embeddings_query import GraphEmbeddingsQueryRequestor
|
|
|
|
|
from . prompt import PromptRequestor
|
|
|
|
|
from . text_load import TextLoad
|
|
|
|
|
from . document_load import DocumentLoad
|
|
|
|
|
|
|
|
|
|
from . triples_export import TriplesExport
|
|
|
|
|
from . graph_embeddings_export import GraphEmbeddingsExport
|
|
|
|
|
from . document_embeddings_export import DocumentEmbeddingsExport
|
2025-05-17 13:25:09 +01:00
|
|
|
from . entity_contexts_export import EntityContextsExport
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
from . triples_import import TriplesImport
|
|
|
|
|
from . graph_embeddings_import import GraphEmbeddingsImport
|
|
|
|
|
from . document_embeddings_import import DocumentEmbeddingsImport
|
2025-05-17 13:25:09 +01:00
|
|
|
from . entity_contexts_import import EntityContextsImport
|
2025-05-02 21:11:50 +01:00
|
|
|
|
2025-05-29 16:33:21 +01:00
|
|
|
from . core_export import CoreExport
|
|
|
|
|
from . core_import import CoreImport
|
|
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
from . mux import Mux
|
|
|
|
|
|
2025-05-02 21:11:50 +01:00
|
|
|
request_response_dispatchers = {
|
|
|
|
|
"agent": AgentRequestor,
|
|
|
|
|
"text-completion": TextCompletionRequestor,
|
|
|
|
|
"prompt": PromptRequestor,
|
|
|
|
|
"graph-rag": GraphRagRequestor,
|
|
|
|
|
"document-rag": DocumentRagRequestor,
|
|
|
|
|
"embeddings": EmbeddingsRequestor,
|
|
|
|
|
"graph-embeddings": GraphEmbeddingsQueryRequestor,
|
2025-05-03 10:39:53 +01:00
|
|
|
"triples": TriplesQueryRequestor,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
global_dispatchers = {
|
|
|
|
|
"config": ConfigRequestor,
|
|
|
|
|
"flow": FlowRequestor,
|
|
|
|
|
"librarian": LibrarianRequestor,
|
2025-05-06 23:44:10 +01:00
|
|
|
"knowledge": KnowledgeRequestor,
|
2025-05-02 21:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sender_dispatchers = {
|
|
|
|
|
"text-load": TextLoad,
|
|
|
|
|
"document-load": DocumentLoad,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export_dispatchers = {
|
|
|
|
|
"triples": TriplesExport,
|
|
|
|
|
"graph-embeddings": GraphEmbeddingsExport,
|
|
|
|
|
"document-embeddings": DocumentEmbeddingsExport,
|
2025-05-17 13:25:09 +01:00
|
|
|
"entity-contexts": EntityContextsExport,
|
2025-05-02 21:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
import_dispatchers = {
|
|
|
|
|
"triples": TriplesImport,
|
|
|
|
|
"graph-embeddings": GraphEmbeddingsImport,
|
|
|
|
|
"document-embeddings": DocumentEmbeddingsImport,
|
2025-05-17 13:25:09 +01:00
|
|
|
"entity-contexts": EntityContextsImport,
|
2025-05-02 21:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DispatcherWrapper:
|
2025-05-03 10:39:53 +01:00
|
|
|
def __init__(self, handler):
|
|
|
|
|
self.handler = handler
|
|
|
|
|
async def process(self, *args):
|
|
|
|
|
return await self.handler(*args)
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
class DispatcherManager:
|
|
|
|
|
|
2025-06-24 11:19:20 +01:00
|
|
|
def __init__(self, pulsar_client, config_receiver, prefix="api-gateway"):
|
2025-05-02 21:11:50 +01:00
|
|
|
self.pulsar_client = pulsar_client
|
|
|
|
|
self.config_receiver = config_receiver
|
|
|
|
|
self.config_receiver.add_handler(self)
|
2025-06-24 11:19:20 +01:00
|
|
|
self.prefix = prefix
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
self.flows = {}
|
|
|
|
|
self.dispatchers = {}
|
|
|
|
|
|
|
|
|
|
async def start_flow(self, id, flow):
|
|
|
|
|
print("Start flow", id)
|
|
|
|
|
self.flows[id] = flow
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
async def stop_flow(self, id, flow):
|
|
|
|
|
print("Stop flow", id)
|
|
|
|
|
del self.flows[id]
|
|
|
|
|
return
|
|
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
def dispatch_global_service(self):
|
|
|
|
|
return DispatcherWrapper(self.process_global_service)
|
2025-05-02 21:11:50 +01:00
|
|
|
|
2025-05-29 16:33:21 +01:00
|
|
|
def dispatch_core_export(self):
|
|
|
|
|
return DispatcherWrapper(self.process_core_export)
|
|
|
|
|
|
|
|
|
|
def dispatch_core_import(self):
|
|
|
|
|
return DispatcherWrapper(self.process_core_import)
|
|
|
|
|
|
2025-05-29 19:56:04 +01:00
|
|
|
async def process_core_import(self, data, error, ok, request):
|
2025-05-29 16:33:21 +01:00
|
|
|
|
|
|
|
|
ci = CoreImport(self.pulsar_client)
|
2025-05-29 19:56:04 +01:00
|
|
|
return await ci.process(data, error, ok, request)
|
2025-05-29 16:33:21 +01:00
|
|
|
|
2025-05-29 19:56:04 +01:00
|
|
|
async def process_core_export(self, data, error, ok, request):
|
2025-05-29 16:33:21 +01:00
|
|
|
|
|
|
|
|
ce = CoreExport(self.pulsar_client)
|
2025-05-29 19:56:04 +01:00
|
|
|
return await ce.process(data, error, ok, request)
|
2025-05-29 16:33:21 +01:00
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
async def process_global_service(self, data, responder, params):
|
2025-05-02 21:11:50 +01:00
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
kind = params.get("kind")
|
|
|
|
|
return await self.invoke_global_service(data, responder, kind)
|
2025-05-02 21:11:50 +01:00
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
async def invoke_global_service(self, data, responder, kind):
|
2025-05-02 21:11:50 +01:00
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
key = (None, kind)
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
if key in self.dispatchers:
|
|
|
|
|
return await self.dispatchers[key].process(data, responder)
|
|
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
dispatcher = global_dispatchers[kind](
|
|
|
|
|
pulsar_client = self.pulsar_client,
|
|
|
|
|
timeout = 120,
|
2025-06-24 11:19:20 +01:00
|
|
|
consumer = f"{self.prefix}-{kind}-request",
|
|
|
|
|
subscriber = f"{self.prefix}-{kind}-request",
|
2025-05-02 21:11:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await dispatcher.start()
|
|
|
|
|
|
|
|
|
|
self.dispatchers[key] = dispatcher
|
|
|
|
|
|
|
|
|
|
return await dispatcher.process(data, responder)
|
|
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
def dispatch_flow_import(self):
|
|
|
|
|
return self.process_flow_import
|
|
|
|
|
|
|
|
|
|
def dispatch_flow_export(self):
|
|
|
|
|
return self.process_flow_export
|
2025-05-02 21:11:50 +01:00
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
def dispatch_socket(self):
|
|
|
|
|
return self.process_socket
|
2025-05-02 21:11:50 +01:00
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
def dispatch_flow_service(self):
|
|
|
|
|
return DispatcherWrapper(self.process_flow_service)
|
2025-05-02 21:11:50 +01:00
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
async def process_flow_import(self, ws, running, params):
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
flow = params.get("flow")
|
|
|
|
|
kind = params.get("kind")
|
|
|
|
|
|
|
|
|
|
if flow not in self.flows:
|
|
|
|
|
raise RuntimeError("Invalid flow")
|
|
|
|
|
|
|
|
|
|
if kind not in import_dispatchers:
|
|
|
|
|
raise RuntimeError("Invalid kind")
|
|
|
|
|
|
|
|
|
|
key = (flow, kind)
|
|
|
|
|
|
|
|
|
|
intf_defs = self.flows[flow]["interfaces"]
|
|
|
|
|
|
2025-05-17 13:25:09 +01:00
|
|
|
# FIXME: The -store bit, does it make sense?
|
|
|
|
|
if kind == "entity-contexts":
|
|
|
|
|
int_kind = kind + "-load"
|
|
|
|
|
else:
|
|
|
|
|
int_kind = kind + "-store"
|
|
|
|
|
|
|
|
|
|
if int_kind not in intf_defs:
|
2025-05-02 21:11:50 +01:00
|
|
|
raise RuntimeError("This kind not supported by flow")
|
|
|
|
|
|
|
|
|
|
# FIXME: The -store bit, does it make sense?
|
2025-05-17 13:25:09 +01:00
|
|
|
qconfig = intf_defs[int_kind]
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
id = str(uuid.uuid4())
|
|
|
|
|
dispatcher = import_dispatchers[kind](
|
|
|
|
|
pulsar_client = self.pulsar_client,
|
|
|
|
|
ws = ws,
|
|
|
|
|
running = running,
|
|
|
|
|
queue = qconfig,
|
|
|
|
|
)
|
|
|
|
|
|
2025-05-17 13:01:52 +01:00
|
|
|
await dispatcher.start()
|
|
|
|
|
|
2025-05-02 21:11:50 +01:00
|
|
|
return dispatcher
|
|
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
async def process_flow_export(self, ws, running, params):
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
flow = params.get("flow")
|
|
|
|
|
kind = params.get("kind")
|
|
|
|
|
|
|
|
|
|
if flow not in self.flows:
|
|
|
|
|
raise RuntimeError("Invalid flow")
|
|
|
|
|
|
|
|
|
|
if kind not in export_dispatchers:
|
|
|
|
|
raise RuntimeError("Invalid kind")
|
|
|
|
|
|
|
|
|
|
key = (flow, kind)
|
|
|
|
|
|
|
|
|
|
intf_defs = self.flows[flow]["interfaces"]
|
|
|
|
|
|
2025-05-17 13:25:09 +01:00
|
|
|
# FIXME: The -store bit, does it make sense?
|
|
|
|
|
if kind == "entity-contexts":
|
|
|
|
|
int_kind = kind + "-load"
|
|
|
|
|
else:
|
|
|
|
|
int_kind = kind + "-store"
|
|
|
|
|
|
|
|
|
|
if int_kind not in intf_defs:
|
2025-05-02 21:11:50 +01:00
|
|
|
raise RuntimeError("This kind not supported by flow")
|
|
|
|
|
|
2025-05-17 13:25:09 +01:00
|
|
|
qconfig = intf_defs[int_kind]
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
id = str(uuid.uuid4())
|
|
|
|
|
dispatcher = export_dispatchers[kind](
|
|
|
|
|
pulsar_client = self.pulsar_client,
|
|
|
|
|
ws = ws,
|
|
|
|
|
running = running,
|
|
|
|
|
queue = qconfig,
|
2025-06-24 11:19:20 +01:00
|
|
|
consumer = f"{self.prefix}-{id}",
|
|
|
|
|
subscriber = f"{self.prefix}-{id}",
|
2025-05-02 21:11:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return dispatcher
|
|
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
async def process_socket(self, ws, running, params):
|
|
|
|
|
|
|
|
|
|
dispatcher = Mux(self, ws, running)
|
|
|
|
|
|
|
|
|
|
return dispatcher
|
|
|
|
|
|
|
|
|
|
async def process_flow_service(self, data, responder, params):
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
flow = params.get("flow")
|
|
|
|
|
kind = params.get("kind")
|
|
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
return await self.invoke_flow_service(data, responder, flow, kind)
|
|
|
|
|
|
|
|
|
|
async def invoke_flow_service(self, data, responder, flow, kind):
|
|
|
|
|
|
2025-05-02 21:11:50 +01:00
|
|
|
if flow not in self.flows:
|
|
|
|
|
raise RuntimeError("Invalid flow")
|
|
|
|
|
|
|
|
|
|
key = (flow, kind)
|
|
|
|
|
|
|
|
|
|
if key in self.dispatchers:
|
|
|
|
|
return await self.dispatchers[key].process(data, responder)
|
|
|
|
|
|
|
|
|
|
intf_defs = self.flows[flow]["interfaces"]
|
|
|
|
|
|
|
|
|
|
if kind not in intf_defs:
|
|
|
|
|
raise RuntimeError("This kind not supported by flow")
|
|
|
|
|
|
|
|
|
|
qconfig = intf_defs[kind]
|
|
|
|
|
|
|
|
|
|
if kind in request_response_dispatchers:
|
|
|
|
|
dispatcher = request_response_dispatchers[kind](
|
|
|
|
|
pulsar_client = self.pulsar_client,
|
|
|
|
|
request_queue = qconfig["request"],
|
|
|
|
|
response_queue = qconfig["response"],
|
|
|
|
|
timeout = 120,
|
2025-06-24 11:19:20 +01:00
|
|
|
consumer = f"{self.prefix}-{flow}-{kind}-request",
|
|
|
|
|
subscriber = f"{self.prefix}-{flow}-{kind}-request",
|
2025-05-02 21:11:50 +01:00
|
|
|
)
|
|
|
|
|
elif kind in sender_dispatchers:
|
|
|
|
|
dispatcher = sender_dispatchers[kind](
|
|
|
|
|
pulsar_client = self.pulsar_client,
|
|
|
|
|
queue = qconfig,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
raise RuntimeError("Invalid kind")
|
|
|
|
|
|
|
|
|
|
await dispatcher.start()
|
|
|
|
|
|
|
|
|
|
self.dispatchers[key] = dispatcher
|
|
|
|
|
|
|
|
|
|
return await dispatcher.process(data, responder)
|
|
|
|
|
|