Extend use of user + collection fields (#503)

* Collection+user fields in structured query

* User/collection in structured query & agent
This commit is contained in:
cybermaggedon 2025-09-08 18:28:38 +01:00 committed by GitHub
parent a92050c411
commit f22bf13aa6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 122 additions and 45 deletions

View file

@ -148,7 +148,8 @@ class Processor(AgentService):
elif impl_id == "structured-query":
impl = functools.partial(
StructuredQueryImpl,
collection=data.get("collection")
collection=data.get("collection"),
user=None # User will be provided dynamically via context
)
arguments = StructuredQueryImpl.get_arguments()
else:
@ -253,12 +254,26 @@ class Processor(AgentService):
logger.debug("Call React")
# Create user-aware context wrapper that preserves the flow interface
# but adds user information for tools that need it
class UserAwareContext:
def __init__(self, flow, user):
self._flow = flow
self._user = user
def __call__(self, service_name):
client = self._flow(service_name)
# For structured query clients, store user context
if service_name == "structured-query-request":
client._current_user = self._user
return client
act = await temp_agent.react(
question = request.question,
history = history,
think = think,
observe = observe,
context = flow,
context = UserAwareContext(flow, request.user),
)
logger.debug(f"Action: {act}")

View file

@ -87,9 +87,10 @@ class McpToolImpl:
# This tool implementation knows how to query structured data using natural language
class StructuredQueryImpl:
def __init__(self, context, collection=None):
def __init__(self, context, collection=None, user=None):
self.context = context
self.collection = collection # For multi-tenant scenarios
self.user = user # User context for multi-tenancy
@staticmethod
def get_arguments():
@ -105,8 +106,13 @@ class StructuredQueryImpl:
client = self.context("structured-query-request")
logger.debug("Structured query question...")
# Get user from client context if available, otherwise use instance user or default
user = getattr(client, '_current_user', self.user or "trustgraph")
result = await client.structured_query(
arguments.get("question")
question=arguments.get("question"),
user=user,
collection=self.collection or "default"
)
# Format the result for the agent

View file

@ -31,7 +31,7 @@ def filter_tools_by_group_and_state(
# Apply defaults as specified in tech spec
if requested_groups is None:
requested_groups = ["default"]
if current_state is None:
if current_state is None or current_state == "":
current_state = "undefined"
logger.info(f"Filtering tools with groups={requested_groups}, state={current_state}")

View file

@ -111,11 +111,10 @@ class Processor(FlowProcessor):
else:
variables_as_strings[key] = str(value)
# Use standard TrustGraph user/collection values
# These should eventually come from authentication/context
# Use user/collection values from request
objects_request = ObjectsQueryRequest(
user="trustgraph", # Standard TrustGraph user
collection="default", # Standard default collection
user=request.user,
collection=request.collection,
query=nlp_response.graphql_query,
variables=variables_as_strings,
operation_name=None