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
|
2025-07-30 23:18:38 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
# Module logger
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
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-09-18 15:57:52 +01:00
|
|
|
from . collection_management import CollectionManagementRequestor
|
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
|
2026-02-23 15:56:29 +00:00
|
|
|
from . rows_query import RowsQueryRequestor
|
2025-09-04 16:06:18 +01:00
|
|
|
from . nlp_query import NLPQueryRequestor
|
SPARQL query service (#754)
SPARQL 1.1 query service wrapping pub/sub triples interface
Add a backend-agnostic SPARQL query service that parses SPARQL
queries using rdflib, decomposes them into triple pattern lookups
via the existing TriplesClient pub/sub interface, and performs
in-memory joins, filters, and projections.
Includes:
- SPARQL parser, algebra evaluator, expression evaluator, solution
sequence operations (BGP, JOIN, OPTIONAL, UNION, FILTER, BIND,
VALUES, GROUP BY, ORDER BY, LIMIT/OFFSET, DISTINCT, aggregates)
- FlowProcessor service with TriplesClientSpec
- Gateway dispatcher, request/response translators, API spec
- Python SDK method (FlowInstance.sparql_query)
- CLI command (tg-invoke-sparql-query)
- Tech spec (docs/tech-specs/sparql-query.md)
New unit tests for SPARQL query
2026-04-02 17:21:39 +01:00
|
|
|
from . sparql_query import SparqlQueryRequestor
|
2025-09-04 16:06:18 +01:00
|
|
|
from . structured_query import StructuredQueryRequestor
|
2025-09-16 21:43:23 +01:00
|
|
|
from . structured_diag import StructuredDiagRequestor
|
2025-05-02 21:11:50 +01:00
|
|
|
from . embeddings import EmbeddingsRequestor
|
|
|
|
|
from . graph_embeddings_query import GraphEmbeddingsQueryRequestor
|
2026-02-04 14:10:30 +00:00
|
|
|
from . document_embeddings_query import DocumentEmbeddingsQueryRequestor
|
2026-02-23 21:52:56 +00:00
|
|
|
from . row_embeddings_query import RowEmbeddingsQueryRequestor
|
2025-07-07 23:52:23 +01:00
|
|
|
from . mcp_tool import McpToolRequestor
|
2025-05-02 21:11:50 +01:00
|
|
|
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
|
2026-02-23 15:56:29 +00:00
|
|
|
from . rows_import import RowsImport
|
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
|
2026-03-09 12:36:10 +00:00
|
|
|
from . document_stream import DocumentStreamExport
|
2025-05-29 16:33:21 +01:00
|
|
|
|
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,
|
2025-07-07 23:52:23 +01:00
|
|
|
"mcp-tool": McpToolRequestor,
|
2025-05-02 21:11:50 +01:00
|
|
|
"graph-rag": GraphRagRequestor,
|
|
|
|
|
"document-rag": DocumentRagRequestor,
|
|
|
|
|
"embeddings": EmbeddingsRequestor,
|
|
|
|
|
"graph-embeddings": GraphEmbeddingsQueryRequestor,
|
2026-02-04 14:10:30 +00:00
|
|
|
"document-embeddings": DocumentEmbeddingsQueryRequestor,
|
2025-05-03 10:39:53 +01:00
|
|
|
"triples": TriplesQueryRequestor,
|
2026-02-23 15:56:29 +00:00
|
|
|
"rows": RowsQueryRequestor,
|
2025-09-04 16:06:18 +01:00
|
|
|
"nlp-query": NLPQueryRequestor,
|
|
|
|
|
"structured-query": StructuredQueryRequestor,
|
2025-09-16 21:43:23 +01:00
|
|
|
"structured-diag": StructuredDiagRequestor,
|
2026-02-23 21:52:56 +00:00
|
|
|
"row-embeddings": RowEmbeddingsQueryRequestor,
|
SPARQL query service (#754)
SPARQL 1.1 query service wrapping pub/sub triples interface
Add a backend-agnostic SPARQL query service that parses SPARQL
queries using rdflib, decomposes them into triple pattern lookups
via the existing TriplesClient pub/sub interface, and performs
in-memory joins, filters, and projections.
Includes:
- SPARQL parser, algebra evaluator, expression evaluator, solution
sequence operations (BGP, JOIN, OPTIONAL, UNION, FILTER, BIND,
VALUES, GROUP BY, ORDER BY, LIMIT/OFFSET, DISTINCT, aggregates)
- FlowProcessor service with TriplesClientSpec
- Gateway dispatcher, request/response translators, API spec
- Python SDK method (FlowInstance.sparql_query)
- CLI command (tg-invoke-sparql-query)
- Tech spec (docs/tech-specs/sparql-query.md)
New unit tests for SPARQL query
2026-04-02 17:21:39 +01:00
|
|
|
"sparql": SparqlQueryRequestor,
|
2025-05-03 10:39:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
global_dispatchers = {
|
|
|
|
|
"config": ConfigRequestor,
|
|
|
|
|
"flow": FlowRequestor,
|
|
|
|
|
"librarian": LibrarianRequestor,
|
2025-05-06 23:44:10 +01:00
|
|
|
"knowledge": KnowledgeRequestor,
|
2025-09-18 15:57:52 +01:00
|
|
|
"collection-management": CollectionManagementRequestor,
|
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,
|
2026-02-23 15:56:29 +00:00
|
|
|
"rows": RowsImport,
|
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-12-17 21:40:43 +00:00
|
|
|
def __init__(self, backend, config_receiver, prefix="api-gateway",
|
2025-12-06 11:01:20 +00:00
|
|
|
queue_overrides=None):
|
2025-12-17 21:40:43 +00:00
|
|
|
self.backend = backend
|
2025-05-02 21:11:50 +01:00
|
|
|
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
|
|
|
|
2025-12-06 11:01:20 +00:00
|
|
|
# Store queue overrides for global services
|
|
|
|
|
# Format: {"config": {"request": "...", "response": "..."}, ...}
|
|
|
|
|
self.queue_overrides = queue_overrides or {}
|
|
|
|
|
|
2025-05-02 21:11:50 +01:00
|
|
|
self.flows = {}
|
|
|
|
|
self.dispatchers = {}
|
2026-04-06 15:43:59 +05:30
|
|
|
self.dispatcher_lock = asyncio.Lock()
|
2025-05-02 21:11:50 +01:00
|
|
|
|
|
|
|
|
async def start_flow(self, id, flow):
|
2025-07-30 23:18:38 +01:00
|
|
|
logger.info(f"Starting flow {id}")
|
2025-05-02 21:11:50 +01:00
|
|
|
self.flows[id] = flow
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
async def stop_flow(self, id, flow):
|
2025-07-30 23:18:38 +01:00
|
|
|
logger.info(f"Stopping flow {id}")
|
2025-05-02 21:11:50 +01:00
|
|
|
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)
|
|
|
|
|
|
2026-03-09 12:36:10 +00:00
|
|
|
def dispatch_document_stream(self):
|
|
|
|
|
return DispatcherWrapper(self.process_document_stream)
|
|
|
|
|
|
|
|
|
|
async def process_document_stream(self, data, error, ok, request):
|
|
|
|
|
|
|
|
|
|
ds = DocumentStreamExport(self.backend)
|
|
|
|
|
return await ds.process(data, error, ok, request)
|
|
|
|
|
|
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
|
|
|
|
2025-12-17 21:40:43 +00:00
|
|
|
ci = CoreImport(self.backend)
|
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
|
|
|
|
2025-12-17 21:40:43 +00:00
|
|
|
ce = CoreExport(self.backend)
|
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
|
|
|
|
2026-04-06 15:43:59 +05:30
|
|
|
if key not in self.dispatchers:
|
|
|
|
|
async with self.dispatcher_lock:
|
|
|
|
|
if key not in self.dispatchers:
|
|
|
|
|
request_queue = None
|
|
|
|
|
response_queue = None
|
|
|
|
|
if kind in self.queue_overrides:
|
|
|
|
|
request_queue = self.queue_overrides[kind].get("request")
|
|
|
|
|
response_queue = self.queue_overrides[kind].get("response")
|
|
|
|
|
|
|
|
|
|
dispatcher = global_dispatchers[kind](
|
|
|
|
|
backend = self.backend,
|
|
|
|
|
timeout = 120,
|
|
|
|
|
consumer = f"{self.prefix}-{kind}-request",
|
|
|
|
|
subscriber = f"{self.prefix}-{kind}-request",
|
|
|
|
|
request_queue = request_queue,
|
|
|
|
|
response_queue = response_queue,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await dispatcher.start()
|
|
|
|
|
self.dispatchers[key] = dispatcher
|
|
|
|
|
|
|
|
|
|
return await self.dispatchers[key].process(data, responder)
|
2025-05-02 21:11:50 +01:00
|
|
|
|
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](
|
2025-12-17 21:40:43 +00:00
|
|
|
backend = self.backend,
|
2025-05-02 21:11:50 +01:00
|
|
|
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](
|
2025-12-17 21:40:43 +00:00
|
|
|
backend = self.backend,
|
2025-05-02 21:11:50 +01:00
|
|
|
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)
|
|
|
|
|
|
2026-04-06 15:43:59 +05:30
|
|
|
if key not in self.dispatchers:
|
|
|
|
|
async with self.dispatcher_lock:
|
|
|
|
|
if key not in self.dispatchers:
|
|
|
|
|
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](
|
|
|
|
|
backend = self.backend,
|
|
|
|
|
request_queue = qconfig["request"],
|
|
|
|
|
response_queue = qconfig["response"],
|
|
|
|
|
timeout = 120,
|
|
|
|
|
consumer = f"{self.prefix}-{flow}-{kind}-request",
|
|
|
|
|
subscriber = f"{self.prefix}-{flow}-{kind}-request",
|
|
|
|
|
)
|
|
|
|
|
elif kind in sender_dispatchers:
|
|
|
|
|
dispatcher = sender_dispatchers[kind](
|
|
|
|
|
backend = self.backend,
|
|
|
|
|
queue = qconfig,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
raise RuntimeError("Invalid kind")
|
|
|
|
|
|
|
|
|
|
await dispatcher.start()
|
|
|
|
|
self.dispatchers[key] = dispatcher
|
|
|
|
|
|
|
|
|
|
return await self.dispatchers[key].process(data, responder)
|
2025-05-02 21:11:50 +01:00
|
|
|
|