2025-12-04 17:38:57 +00:00
|
|
|
|
|
|
|
|
import json
|
2026-03-26 16:46:28 +00:00
|
|
|
import asyncio
|
2025-12-04 17:38:57 +00:00
|
|
|
import websockets
|
|
|
|
|
from typing import Optional, Dict, Any, AsyncIterator, Union
|
|
|
|
|
|
Expose LLM token usage across all service layers (#782)
Expose LLM token usage (in_token, out_token, model) across all
service layers
Propagate token counts from LLM services through the prompt,
text-completion, graph-RAG, document-RAG, and agent orchestrator
pipelines to the API gateway and Python SDK. All fields are Optional
— None means "not available", distinguishing from a real zero count.
Key changes:
- Schema: Add in_token/out_token/model to TextCompletionResponse,
PromptResponse, GraphRagResponse, DocumentRagResponse,
AgentResponse
- TextCompletionClient: New TextCompletionResult return type. Split
into text_completion() (non-streaming) and
text_completion_stream() (streaming with per-chunk handler
callback)
- PromptClient: New PromptResult with response_type
(text/json/jsonl), typed fields (text/object/objects), and token
usage. All callers updated.
- RAG services: Accumulate token usage across all prompt calls
(extract-concepts, edge-scoring, edge-reasoning,
synthesis). Non-streaming path sends single combined response
instead of chunk + end_of_session.
- Agent orchestrator: UsageTracker accumulates tokens across
meta-router, pattern prompt calls, and react reasoning. Attached
to end_of_dialog.
- Translators: Encode token fields when not None (is not None, not truthy)
- Python SDK: RAG and text-completion methods return
TextCompletionResult (non-streaming) or RAGChunk/AgentAnswer with
token fields (streaming)
- CLI: --show-usage flag on tg-invoke-llm, tg-invoke-prompt,
tg-invoke-graph-rag, tg-invoke-document-rag, tg-invoke-agent
2026-04-13 14:38:34 +01:00
|
|
|
from . types import AgentThought, AgentObservation, AgentAnswer, RAGChunk, TextCompletionResult
|
2025-12-04 17:38:57 +00:00
|
|
|
from . exceptions import ProtocolException, ApplicationException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AsyncSocketClient:
|
2026-03-26 16:46:28 +00:00
|
|
|
"""Asynchronous WebSocket client with persistent connection.
|
|
|
|
|
|
|
|
|
|
Maintains a single websocket connection and multiplexes requests
|
|
|
|
|
by ID, routing responses via a background reader task.
|
|
|
|
|
|
|
|
|
|
Use as an async context manager for proper lifecycle management:
|
|
|
|
|
|
|
|
|
|
async with AsyncSocketClient(url, timeout, token) as client:
|
|
|
|
|
result = await client._send_request(...)
|
|
|
|
|
|
|
|
|
|
Or call connect()/aclose() manually.
|
|
|
|
|
"""
|
2025-12-04 17:38:57 +00:00
|
|
|
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
def __init__(
|
|
|
|
|
self, url: str, timeout: int, token: Optional[str],
|
|
|
|
|
workspace: str = "default",
|
|
|
|
|
):
|
2025-12-04 17:38:57 +00:00
|
|
|
self.url = self._convert_to_ws_url(url)
|
|
|
|
|
self.timeout = timeout
|
|
|
|
|
self.token = token
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
self.workspace = workspace
|
2025-12-04 17:38:57 +00:00
|
|
|
self._request_counter = 0
|
2026-03-26 16:46:28 +00:00
|
|
|
self._socket = None
|
|
|
|
|
self._connect_cm = None
|
|
|
|
|
self._reader_task = None
|
|
|
|
|
self._pending = {} # request_id -> asyncio.Queue
|
|
|
|
|
self._connected = False
|
2025-12-04 17:38:57 +00:00
|
|
|
|
|
|
|
|
def _convert_to_ws_url(self, url: str) -> str:
|
|
|
|
|
"""Convert HTTP URL to WebSocket URL"""
|
|
|
|
|
if url.startswith("http://"):
|
|
|
|
|
return url.replace("http://", "ws://", 1)
|
|
|
|
|
elif url.startswith("https://"):
|
|
|
|
|
return url.replace("https://", "wss://", 1)
|
|
|
|
|
elif url.startswith("ws://") or url.startswith("wss://"):
|
|
|
|
|
return url
|
|
|
|
|
else:
|
|
|
|
|
return f"ws://{url}"
|
|
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
def _build_ws_url(self):
|
|
|
|
|
ws_url = f"{self.url.rstrip('/')}/api/v1/socket"
|
|
|
|
|
if self.token:
|
|
|
|
|
ws_url = f"{ws_url}?token={self.token}"
|
|
|
|
|
return ws_url
|
|
|
|
|
|
|
|
|
|
async def connect(self):
|
|
|
|
|
"""Establish the persistent websocket connection."""
|
|
|
|
|
if self._connected:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
ws_url = self._build_ws_url()
|
|
|
|
|
self._connect_cm = websockets.connect(
|
|
|
|
|
ws_url, ping_interval=20, ping_timeout=self.timeout
|
|
|
|
|
)
|
|
|
|
|
self._socket = await self._connect_cm.__aenter__()
|
|
|
|
|
self._connected = True
|
|
|
|
|
self._reader_task = asyncio.create_task(self._reader())
|
|
|
|
|
|
|
|
|
|
async def __aenter__(self):
|
|
|
|
|
await self.connect()
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
|
await self.aclose()
|
|
|
|
|
|
|
|
|
|
async def _ensure_connected(self):
|
|
|
|
|
"""Lazily connect if not already connected."""
|
|
|
|
|
if not self._connected:
|
|
|
|
|
await self.connect()
|
|
|
|
|
|
|
|
|
|
async def _reader(self):
|
|
|
|
|
"""Background task to read responses and route by request ID."""
|
|
|
|
|
try:
|
|
|
|
|
async for raw_message in self._socket:
|
|
|
|
|
response = json.loads(raw_message)
|
|
|
|
|
request_id = response.get("id")
|
|
|
|
|
|
|
|
|
|
if request_id and request_id in self._pending:
|
|
|
|
|
await self._pending[request_id].put(response)
|
|
|
|
|
# Ignore messages for unknown request IDs
|
|
|
|
|
|
|
|
|
|
except websockets.exceptions.ConnectionClosed:
|
|
|
|
|
pass
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# Signal error to all pending requests
|
|
|
|
|
for queue in self._pending.values():
|
|
|
|
|
try:
|
|
|
|
|
await queue.put({"error": str(e)})
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
finally:
|
|
|
|
|
self._connected = False
|
|
|
|
|
|
|
|
|
|
def _next_request_id(self):
|
|
|
|
|
self._request_counter += 1
|
|
|
|
|
return f"req-{self._request_counter}"
|
|
|
|
|
|
2025-12-04 17:38:57 +00:00
|
|
|
def flow(self, flow_id: str):
|
|
|
|
|
"""Get async flow instance for WebSocket operations"""
|
|
|
|
|
return AsyncSocketFlowInstance(self, flow_id)
|
|
|
|
|
|
|
|
|
|
async def _send_request(self, service: str, flow: Optional[str], request: Dict[str, Any]):
|
2026-03-26 16:46:28 +00:00
|
|
|
"""Send a request and wait for a single response."""
|
|
|
|
|
await self._ensure_connected()
|
2025-12-04 17:38:57 +00:00
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
request_id = self._next_request_id()
|
|
|
|
|
queue = asyncio.Queue()
|
|
|
|
|
self._pending[request_id] = queue
|
2025-12-04 17:38:57 +00:00
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
try:
|
|
|
|
|
message = {
|
|
|
|
|
"id": request_id,
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
"workspace": self.workspace,
|
2026-03-26 16:46:28 +00:00
|
|
|
"service": service,
|
|
|
|
|
"request": request
|
|
|
|
|
}
|
|
|
|
|
if flow:
|
|
|
|
|
message["flow"] = flow
|
2025-12-04 17:38:57 +00:00
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
await self._socket.send(json.dumps(message))
|
2025-12-04 17:38:57 +00:00
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
response = await queue.get()
|
2025-12-04 17:38:57 +00:00
|
|
|
|
|
|
|
|
if "error" in response:
|
|
|
|
|
raise ApplicationException(response["error"])
|
|
|
|
|
|
|
|
|
|
if "response" not in response:
|
2026-03-26 16:46:28 +00:00
|
|
|
raise ProtocolException("Missing response in message")
|
2025-12-04 17:38:57 +00:00
|
|
|
|
|
|
|
|
return response["response"]
|
|
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
finally:
|
|
|
|
|
self._pending.pop(request_id, None)
|
2025-12-04 17:38:57 +00:00
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
async def _send_request_streaming(self, service: str, flow: Optional[str], request: Dict[str, Any]):
|
|
|
|
|
"""Send a request and yield streaming response chunks."""
|
|
|
|
|
await self._ensure_connected()
|
2025-12-04 17:38:57 +00:00
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
request_id = self._next_request_id()
|
|
|
|
|
queue = asyncio.Queue()
|
|
|
|
|
self._pending[request_id] = queue
|
2025-12-04 17:38:57 +00:00
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
try:
|
|
|
|
|
message = {
|
|
|
|
|
"id": request_id,
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
"workspace": self.workspace,
|
2026-03-26 16:46:28 +00:00
|
|
|
"service": service,
|
|
|
|
|
"request": request
|
|
|
|
|
}
|
|
|
|
|
if flow:
|
|
|
|
|
message["flow"] = flow
|
2025-12-04 17:38:57 +00:00
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
await self._socket.send(json.dumps(message))
|
2025-12-04 17:38:57 +00:00
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
while True:
|
|
|
|
|
response = await queue.get()
|
2025-12-04 17:38:57 +00:00
|
|
|
|
|
|
|
|
if "error" in response:
|
|
|
|
|
raise ApplicationException(response["error"])
|
|
|
|
|
|
|
|
|
|
if "response" in response:
|
|
|
|
|
resp = response["response"]
|
|
|
|
|
|
|
|
|
|
chunk = self._parse_chunk(resp)
|
2026-03-26 16:46:28 +00:00
|
|
|
if chunk is not None:
|
GraphRAG Query-Time Explainability (#677)
Implements full explainability pipeline for GraphRAG queries, enabling
traceability from answers back to source documents.
Renamed throughout for clarity:
- provenance_callback → explain_callback
- provenance_id → explain_id
- provenance_collection → explain_collection
- message_type "provenance" → "explain"
- Queue name "provenance" → "explainability"
GraphRAG queries now emit explainability events as they execute:
1. Session - query text and timestamp
2. Retrieval - edges retrieved from subgraph
3. Selection - selected edges with LLM reasoning (JSONL with id +
reasoning)
4. Answer - reference to synthesized response
Events stream via explain_callback during query(), enabling
real-time UX.
- Answers stored in librarian service (not inline in graph - too large)
- Document ID as URN: urn:trustgraph:answer:{session_id}
- Graph stores tg:document reference (IRI) to librarian document
- Added librarian producer/consumer to graph-rag service
- get_labelgraph() now returns (labeled_edges, uri_map)
- uri_map maps edge_id(label_s, label_p, label_o) →
(uri_s, uri_p, uri_o)
- Explainability data stores original URIs, not labels
- Enables tracing edges back to reifying statements via tg:reifies
- Added serialize_triple() to query service (matches storage format)
- get_term_value() now handles TRIPLE type terms
- Enables querying by quoted triple in object position:
?stmt tg:reifies <<s p o>>
- Displays real-time explainability events during query
- Resolves rdfs:label for edge components (s, p, o)
- Traces source chain via prov:wasDerivedFrom to root document
- Output: "Source: Chunk 1 → Page 2 → Document Title"
- Label caching to avoid repeated queries
GraphRagResponse:
- explain_id: str | None
- explain_collection: str | None
- message_type: str ("chunk" or "explain")
- end_of_session: bool
trustgraph-base/trustgraph/provenance/:
- namespaces.py - Added TG_DOCUMENT predicate
- triples.py - answer_triples() supports document_id reference
- uris.py - Added edge_selection_uri()
trustgraph-base/trustgraph/schema/services/retrieval.py:
- GraphRagResponse with explain_id, explain_collection, end_of_session
trustgraph-flow/trustgraph/retrieval/graph_rag/:
- graph_rag.py - URI preservation, streaming answer accumulation
- rag.py - Librarian integration, real-time explain emission
trustgraph-flow/trustgraph/query/triples/cassandra/service.py:
- Quoted triple serialization for query matching
trustgraph-cli/trustgraph/cli/invoke_graph_rag.py:
- Full explainability display with label resolution and source tracing
2026-03-10 10:00:01 +00:00
|
|
|
yield chunk
|
|
|
|
|
|
|
|
|
|
if resp.get("end_of_session") or resp.get("end_of_dialog") or response.get("complete"):
|
2025-12-04 17:38:57 +00:00
|
|
|
break
|
|
|
|
|
|
2026-03-26 16:46:28 +00:00
|
|
|
finally:
|
|
|
|
|
self._pending.pop(request_id, None)
|
|
|
|
|
|
2025-12-04 17:38:57 +00:00
|
|
|
def _parse_chunk(self, resp: Dict[str, Any]):
|
GraphRAG Query-Time Explainability (#677)
Implements full explainability pipeline for GraphRAG queries, enabling
traceability from answers back to source documents.
Renamed throughout for clarity:
- provenance_callback → explain_callback
- provenance_id → explain_id
- provenance_collection → explain_collection
- message_type "provenance" → "explain"
- Queue name "provenance" → "explainability"
GraphRAG queries now emit explainability events as they execute:
1. Session - query text and timestamp
2. Retrieval - edges retrieved from subgraph
3. Selection - selected edges with LLM reasoning (JSONL with id +
reasoning)
4. Answer - reference to synthesized response
Events stream via explain_callback during query(), enabling
real-time UX.
- Answers stored in librarian service (not inline in graph - too large)
- Document ID as URN: urn:trustgraph:answer:{session_id}
- Graph stores tg:document reference (IRI) to librarian document
- Added librarian producer/consumer to graph-rag service
- get_labelgraph() now returns (labeled_edges, uri_map)
- uri_map maps edge_id(label_s, label_p, label_o) →
(uri_s, uri_p, uri_o)
- Explainability data stores original URIs, not labels
- Enables tracing edges back to reifying statements via tg:reifies
- Added serialize_triple() to query service (matches storage format)
- get_term_value() now handles TRIPLE type terms
- Enables querying by quoted triple in object position:
?stmt tg:reifies <<s p o>>
- Displays real-time explainability events during query
- Resolves rdfs:label for edge components (s, p, o)
- Traces source chain via prov:wasDerivedFrom to root document
- Output: "Source: Chunk 1 → Page 2 → Document Title"
- Label caching to avoid repeated queries
GraphRagResponse:
- explain_id: str | None
- explain_collection: str | None
- message_type: str ("chunk" or "explain")
- end_of_session: bool
trustgraph-base/trustgraph/provenance/:
- namespaces.py - Added TG_DOCUMENT predicate
- triples.py - answer_triples() supports document_id reference
- uris.py - Added edge_selection_uri()
trustgraph-base/trustgraph/schema/services/retrieval.py:
- GraphRagResponse with explain_id, explain_collection, end_of_session
trustgraph-flow/trustgraph/retrieval/graph_rag/:
- graph_rag.py - URI preservation, streaming answer accumulation
- rag.py - Librarian integration, real-time explain emission
trustgraph-flow/trustgraph/query/triples/cassandra/service.py:
- Quoted triple serialization for query matching
trustgraph-cli/trustgraph/cli/invoke_graph_rag.py:
- Full explainability display with label resolution and source tracing
2026-03-10 10:00:01 +00:00
|
|
|
"""Parse response chunk into appropriate type. Returns None for non-content messages."""
|
|
|
|
|
message_type = resp.get("message_type")
|
|
|
|
|
|
|
|
|
|
# Handle new GraphRAG message format with message_type
|
|
|
|
|
if message_type == "provenance":
|
|
|
|
|
return None
|
2025-12-04 17:38:57 +00:00
|
|
|
|
Add agent explainability instrumentation and unify envelope field naming (#795)
Addresses recommendations from the UX developer's agent experience report.
Adds provenance predicates, DAG structure changes, error resilience, and
a published OWL ontology.
Explainability additions:
- Tool candidates: tg:toolCandidate on Analysis events lists the tools
visible to the LLM for each iteration (names only, descriptions in config)
- Termination reason: tg:terminationReason on Conclusion/Synthesis events
(final-answer, plan-complete, subagents-complete)
- Step counter: tg:stepNumber on iteration events
- Pattern decision: new tg:PatternDecision entity in the DAG between
session and first iteration, carrying tg:pattern and tg:taskType
- Latency: tg:llmDurationMs on Analysis events, tg:toolDurationMs on
Observation events
- Token counts on events: tg:inToken/tg:outToken/tg:llmModel on
Grounding, Focus, Synthesis, and Analysis events
- Tool/parse errors: tg:toolError on Observation events with tg:Error
mixin type. Parse failures return as error observations instead of
crashing the agent, giving it a chance to retry.
Envelope unification:
- Rename chunk_type to message_type across AgentResponse schema,
translator, SDK types, socket clients, CLI, and all tests.
Agent and RAG services now both use message_type on the wire.
Ontology:
- specs/ontology/trustgraph.ttl — OWL vocabulary covering all 26 classes,
7 object properties, and 36+ datatype properties including new predicates.
DAG structure tests:
- tests/unit/test_provenance/test_dag_structure.py verifies the
wasDerivedFrom chain for GraphRAG, DocumentRAG, and all three agent
patterns (react, plan, supervisor) including the pattern-decision link.
2026-04-13 16:16:42 +01:00
|
|
|
if message_type == "thought":
|
2025-12-04 17:38:57 +00:00
|
|
|
return AgentThought(
|
|
|
|
|
content=resp.get("content", ""),
|
|
|
|
|
end_of_message=resp.get("end_of_message", False)
|
|
|
|
|
)
|
Add agent explainability instrumentation and unify envelope field naming (#795)
Addresses recommendations from the UX developer's agent experience report.
Adds provenance predicates, DAG structure changes, error resilience, and
a published OWL ontology.
Explainability additions:
- Tool candidates: tg:toolCandidate on Analysis events lists the tools
visible to the LLM for each iteration (names only, descriptions in config)
- Termination reason: tg:terminationReason on Conclusion/Synthesis events
(final-answer, plan-complete, subagents-complete)
- Step counter: tg:stepNumber on iteration events
- Pattern decision: new tg:PatternDecision entity in the DAG between
session and first iteration, carrying tg:pattern and tg:taskType
- Latency: tg:llmDurationMs on Analysis events, tg:toolDurationMs on
Observation events
- Token counts on events: tg:inToken/tg:outToken/tg:llmModel on
Grounding, Focus, Synthesis, and Analysis events
- Tool/parse errors: tg:toolError on Observation events with tg:Error
mixin type. Parse failures return as error observations instead of
crashing the agent, giving it a chance to retry.
Envelope unification:
- Rename chunk_type to message_type across AgentResponse schema,
translator, SDK types, socket clients, CLI, and all tests.
Agent and RAG services now both use message_type on the wire.
Ontology:
- specs/ontology/trustgraph.ttl — OWL vocabulary covering all 26 classes,
7 object properties, and 36+ datatype properties including new predicates.
DAG structure tests:
- tests/unit/test_provenance/test_dag_structure.py verifies the
wasDerivedFrom chain for GraphRAG, DocumentRAG, and all three agent
patterns (react, plan, supervisor) including the pattern-decision link.
2026-04-13 16:16:42 +01:00
|
|
|
elif message_type == "observation":
|
2025-12-04 17:38:57 +00:00
|
|
|
return AgentObservation(
|
|
|
|
|
content=resp.get("content", ""),
|
|
|
|
|
end_of_message=resp.get("end_of_message", False)
|
|
|
|
|
)
|
Add agent explainability instrumentation and unify envelope field naming (#795)
Addresses recommendations from the UX developer's agent experience report.
Adds provenance predicates, DAG structure changes, error resilience, and
a published OWL ontology.
Explainability additions:
- Tool candidates: tg:toolCandidate on Analysis events lists the tools
visible to the LLM for each iteration (names only, descriptions in config)
- Termination reason: tg:terminationReason on Conclusion/Synthesis events
(final-answer, plan-complete, subagents-complete)
- Step counter: tg:stepNumber on iteration events
- Pattern decision: new tg:PatternDecision entity in the DAG between
session and first iteration, carrying tg:pattern and tg:taskType
- Latency: tg:llmDurationMs on Analysis events, tg:toolDurationMs on
Observation events
- Token counts on events: tg:inToken/tg:outToken/tg:llmModel on
Grounding, Focus, Synthesis, and Analysis events
- Tool/parse errors: tg:toolError on Observation events with tg:Error
mixin type. Parse failures return as error observations instead of
crashing the agent, giving it a chance to retry.
Envelope unification:
- Rename chunk_type to message_type across AgentResponse schema,
translator, SDK types, socket clients, CLI, and all tests.
Agent and RAG services now both use message_type on the wire.
Ontology:
- specs/ontology/trustgraph.ttl — OWL vocabulary covering all 26 classes,
7 object properties, and 36+ datatype properties including new predicates.
DAG structure tests:
- tests/unit/test_provenance/test_dag_structure.py verifies the
wasDerivedFrom chain for GraphRAG, DocumentRAG, and all three agent
patterns (react, plan, supervisor) including the pattern-decision link.
2026-04-13 16:16:42 +01:00
|
|
|
elif message_type == "answer" or message_type == "final-answer":
|
2025-12-04 17:38:57 +00:00
|
|
|
return AgentAnswer(
|
|
|
|
|
content=resp.get("content", ""),
|
|
|
|
|
end_of_message=resp.get("end_of_message", False),
|
Expose LLM token usage across all service layers (#782)
Expose LLM token usage (in_token, out_token, model) across all
service layers
Propagate token counts from LLM services through the prompt,
text-completion, graph-RAG, document-RAG, and agent orchestrator
pipelines to the API gateway and Python SDK. All fields are Optional
— None means "not available", distinguishing from a real zero count.
Key changes:
- Schema: Add in_token/out_token/model to TextCompletionResponse,
PromptResponse, GraphRagResponse, DocumentRagResponse,
AgentResponse
- TextCompletionClient: New TextCompletionResult return type. Split
into text_completion() (non-streaming) and
text_completion_stream() (streaming with per-chunk handler
callback)
- PromptClient: New PromptResult with response_type
(text/json/jsonl), typed fields (text/object/objects), and token
usage. All callers updated.
- RAG services: Accumulate token usage across all prompt calls
(extract-concepts, edge-scoring, edge-reasoning,
synthesis). Non-streaming path sends single combined response
instead of chunk + end_of_session.
- Agent orchestrator: UsageTracker accumulates tokens across
meta-router, pattern prompt calls, and react reasoning. Attached
to end_of_dialog.
- Translators: Encode token fields when not None (is not None, not truthy)
- Python SDK: RAG and text-completion methods return
TextCompletionResult (non-streaming) or RAGChunk/AgentAnswer with
token fields (streaming)
- CLI: --show-usage flag on tg-invoke-llm, tg-invoke-prompt,
tg-invoke-graph-rag, tg-invoke-document-rag, tg-invoke-agent
2026-04-13 14:38:34 +01:00
|
|
|
end_of_dialog=resp.get("end_of_dialog", False),
|
|
|
|
|
in_token=resp.get("in_token"),
|
|
|
|
|
out_token=resp.get("out_token"),
|
|
|
|
|
model=resp.get("model"),
|
2025-12-04 17:38:57 +00:00
|
|
|
)
|
Add agent explainability instrumentation and unify envelope field naming (#795)
Addresses recommendations from the UX developer's agent experience report.
Adds provenance predicates, DAG structure changes, error resilience, and
a published OWL ontology.
Explainability additions:
- Tool candidates: tg:toolCandidate on Analysis events lists the tools
visible to the LLM for each iteration (names only, descriptions in config)
- Termination reason: tg:terminationReason on Conclusion/Synthesis events
(final-answer, plan-complete, subagents-complete)
- Step counter: tg:stepNumber on iteration events
- Pattern decision: new tg:PatternDecision entity in the DAG between
session and first iteration, carrying tg:pattern and tg:taskType
- Latency: tg:llmDurationMs on Analysis events, tg:toolDurationMs on
Observation events
- Token counts on events: tg:inToken/tg:outToken/tg:llmModel on
Grounding, Focus, Synthesis, and Analysis events
- Tool/parse errors: tg:toolError on Observation events with tg:Error
mixin type. Parse failures return as error observations instead of
crashing the agent, giving it a chance to retry.
Envelope unification:
- Rename chunk_type to message_type across AgentResponse schema,
translator, SDK types, socket clients, CLI, and all tests.
Agent and RAG services now both use message_type on the wire.
Ontology:
- specs/ontology/trustgraph.ttl — OWL vocabulary covering all 26 classes,
7 object properties, and 36+ datatype properties including new predicates.
DAG structure tests:
- tests/unit/test_provenance/test_dag_structure.py verifies the
wasDerivedFrom chain for GraphRAG, DocumentRAG, and all three agent
patterns (react, plan, supervisor) including the pattern-decision link.
2026-04-13 16:16:42 +01:00
|
|
|
elif message_type == "action":
|
2025-12-04 20:42:25 +00:00
|
|
|
return AgentThought(
|
|
|
|
|
content=resp.get("content", ""),
|
|
|
|
|
end_of_message=resp.get("end_of_message", False)
|
|
|
|
|
)
|
2025-12-04 17:38:57 +00:00
|
|
|
else:
|
2025-12-04 20:42:25 +00:00
|
|
|
content = resp.get("response", resp.get("chunk", resp.get("text", "")))
|
2025-12-04 17:38:57 +00:00
|
|
|
return RAGChunk(
|
2025-12-04 20:42:25 +00:00
|
|
|
content=content,
|
2025-12-04 17:38:57 +00:00
|
|
|
end_of_stream=resp.get("end_of_stream", False),
|
Expose LLM token usage across all service layers (#782)
Expose LLM token usage (in_token, out_token, model) across all
service layers
Propagate token counts from LLM services through the prompt,
text-completion, graph-RAG, document-RAG, and agent orchestrator
pipelines to the API gateway and Python SDK. All fields are Optional
— None means "not available", distinguishing from a real zero count.
Key changes:
- Schema: Add in_token/out_token/model to TextCompletionResponse,
PromptResponse, GraphRagResponse, DocumentRagResponse,
AgentResponse
- TextCompletionClient: New TextCompletionResult return type. Split
into text_completion() (non-streaming) and
text_completion_stream() (streaming with per-chunk handler
callback)
- PromptClient: New PromptResult with response_type
(text/json/jsonl), typed fields (text/object/objects), and token
usage. All callers updated.
- RAG services: Accumulate token usage across all prompt calls
(extract-concepts, edge-scoring, edge-reasoning,
synthesis). Non-streaming path sends single combined response
instead of chunk + end_of_session.
- Agent orchestrator: UsageTracker accumulates tokens across
meta-router, pattern prompt calls, and react reasoning. Attached
to end_of_dialog.
- Translators: Encode token fields when not None (is not None, not truthy)
- Python SDK: RAG and text-completion methods return
TextCompletionResult (non-streaming) or RAGChunk/AgentAnswer with
token fields (streaming)
- CLI: --show-usage flag on tg-invoke-llm, tg-invoke-prompt,
tg-invoke-graph-rag, tg-invoke-document-rag, tg-invoke-agent
2026-04-13 14:38:34 +01:00
|
|
|
error=None,
|
|
|
|
|
in_token=resp.get("in_token"),
|
|
|
|
|
out_token=resp.get("out_token"),
|
|
|
|
|
model=resp.get("model"),
|
2025-12-04 17:38:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def aclose(self):
|
2026-03-26 16:46:28 +00:00
|
|
|
"""Close the persistent WebSocket connection cleanly."""
|
|
|
|
|
# Wait for reader to finish (socket close will cause it to exit)
|
|
|
|
|
if self._reader_task:
|
|
|
|
|
self._reader_task.cancel()
|
|
|
|
|
try:
|
|
|
|
|
await self._reader_task
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
pass
|
|
|
|
|
self._reader_task = None
|
|
|
|
|
|
|
|
|
|
# Exit the websockets context manager — this cleanly shuts down
|
|
|
|
|
# the connection and its keepalive task
|
|
|
|
|
if self._connect_cm:
|
|
|
|
|
try:
|
|
|
|
|
await self._connect_cm.__aexit__(None, None, None)
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
self._connect_cm = None
|
|
|
|
|
|
|
|
|
|
self._socket = None
|
|
|
|
|
self._connected = False
|
|
|
|
|
self._pending.clear()
|
2025-12-04 17:38:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class AsyncSocketFlowInstance:
|
|
|
|
|
"""Asynchronous WebSocket flow instance"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, client: AsyncSocketClient, flow_id: str):
|
|
|
|
|
self.client = client
|
|
|
|
|
self.flow_id = flow_id
|
|
|
|
|
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
async def agent(self, question: str, state: Optional[Dict[str, Any]] = None,
|
2025-12-04 17:38:57 +00:00
|
|
|
group: Optional[str] = None, history: Optional[list] = None,
|
|
|
|
|
streaming: bool = False, **kwargs) -> Union[Dict[str, Any], AsyncIterator]:
|
|
|
|
|
"""Agent with optional streaming"""
|
|
|
|
|
request = {
|
|
|
|
|
"question": question,
|
|
|
|
|
"streaming": streaming
|
|
|
|
|
}
|
|
|
|
|
if state is not None:
|
|
|
|
|
request["state"] = state
|
|
|
|
|
if group is not None:
|
|
|
|
|
request["group"] = group
|
|
|
|
|
if history is not None:
|
|
|
|
|
request["history"] = history
|
|
|
|
|
request.update(kwargs)
|
|
|
|
|
|
|
|
|
|
if streaming:
|
|
|
|
|
return self.client._send_request_streaming("agent", self.flow_id, request)
|
|
|
|
|
else:
|
|
|
|
|
return await self.client._send_request("agent", self.flow_id, request)
|
|
|
|
|
|
|
|
|
|
async def text_completion(self, system: str, prompt: str, streaming: bool = False, **kwargs):
|
Expose LLM token usage across all service layers (#782)
Expose LLM token usage (in_token, out_token, model) across all
service layers
Propagate token counts from LLM services through the prompt,
text-completion, graph-RAG, document-RAG, and agent orchestrator
pipelines to the API gateway and Python SDK. All fields are Optional
— None means "not available", distinguishing from a real zero count.
Key changes:
- Schema: Add in_token/out_token/model to TextCompletionResponse,
PromptResponse, GraphRagResponse, DocumentRagResponse,
AgentResponse
- TextCompletionClient: New TextCompletionResult return type. Split
into text_completion() (non-streaming) and
text_completion_stream() (streaming with per-chunk handler
callback)
- PromptClient: New PromptResult with response_type
(text/json/jsonl), typed fields (text/object/objects), and token
usage. All callers updated.
- RAG services: Accumulate token usage across all prompt calls
(extract-concepts, edge-scoring, edge-reasoning,
synthesis). Non-streaming path sends single combined response
instead of chunk + end_of_session.
- Agent orchestrator: UsageTracker accumulates tokens across
meta-router, pattern prompt calls, and react reasoning. Attached
to end_of_dialog.
- Translators: Encode token fields when not None (is not None, not truthy)
- Python SDK: RAG and text-completion methods return
TextCompletionResult (non-streaming) or RAGChunk/AgentAnswer with
token fields (streaming)
- CLI: --show-usage flag on tg-invoke-llm, tg-invoke-prompt,
tg-invoke-graph-rag, tg-invoke-document-rag, tg-invoke-agent
2026-04-13 14:38:34 +01:00
|
|
|
"""Text completion with optional streaming.
|
|
|
|
|
|
|
|
|
|
Non-streaming: returns a TextCompletionResult with text and token counts.
|
|
|
|
|
Streaming: returns an async iterator of RAGChunk (with token counts on the final chunk).
|
|
|
|
|
"""
|
2025-12-04 17:38:57 +00:00
|
|
|
request = {
|
|
|
|
|
"system": system,
|
|
|
|
|
"prompt": prompt,
|
|
|
|
|
"streaming": streaming
|
|
|
|
|
}
|
|
|
|
|
request.update(kwargs)
|
|
|
|
|
|
|
|
|
|
if streaming:
|
|
|
|
|
return self._text_completion_streaming(request)
|
|
|
|
|
else:
|
|
|
|
|
result = await self.client._send_request("text-completion", self.flow_id, request)
|
Expose LLM token usage across all service layers (#782)
Expose LLM token usage (in_token, out_token, model) across all
service layers
Propagate token counts from LLM services through the prompt,
text-completion, graph-RAG, document-RAG, and agent orchestrator
pipelines to the API gateway and Python SDK. All fields are Optional
— None means "not available", distinguishing from a real zero count.
Key changes:
- Schema: Add in_token/out_token/model to TextCompletionResponse,
PromptResponse, GraphRagResponse, DocumentRagResponse,
AgentResponse
- TextCompletionClient: New TextCompletionResult return type. Split
into text_completion() (non-streaming) and
text_completion_stream() (streaming with per-chunk handler
callback)
- PromptClient: New PromptResult with response_type
(text/json/jsonl), typed fields (text/object/objects), and token
usage. All callers updated.
- RAG services: Accumulate token usage across all prompt calls
(extract-concepts, edge-scoring, edge-reasoning,
synthesis). Non-streaming path sends single combined response
instead of chunk + end_of_session.
- Agent orchestrator: UsageTracker accumulates tokens across
meta-router, pattern prompt calls, and react reasoning. Attached
to end_of_dialog.
- Translators: Encode token fields when not None (is not None, not truthy)
- Python SDK: RAG and text-completion methods return
TextCompletionResult (non-streaming) or RAGChunk/AgentAnswer with
token fields (streaming)
- CLI: --show-usage flag on tg-invoke-llm, tg-invoke-prompt,
tg-invoke-graph-rag, tg-invoke-document-rag, tg-invoke-agent
2026-04-13 14:38:34 +01:00
|
|
|
return TextCompletionResult(
|
|
|
|
|
text=result.get("response", ""),
|
|
|
|
|
in_token=result.get("in_token"),
|
|
|
|
|
out_token=result.get("out_token"),
|
|
|
|
|
model=result.get("model"),
|
|
|
|
|
)
|
2025-12-04 17:38:57 +00:00
|
|
|
|
|
|
|
|
async def _text_completion_streaming(self, request):
|
Expose LLM token usage across all service layers (#782)
Expose LLM token usage (in_token, out_token, model) across all
service layers
Propagate token counts from LLM services through the prompt,
text-completion, graph-RAG, document-RAG, and agent orchestrator
pipelines to the API gateway and Python SDK. All fields are Optional
— None means "not available", distinguishing from a real zero count.
Key changes:
- Schema: Add in_token/out_token/model to TextCompletionResponse,
PromptResponse, GraphRagResponse, DocumentRagResponse,
AgentResponse
- TextCompletionClient: New TextCompletionResult return type. Split
into text_completion() (non-streaming) and
text_completion_stream() (streaming with per-chunk handler
callback)
- PromptClient: New PromptResult with response_type
(text/json/jsonl), typed fields (text/object/objects), and token
usage. All callers updated.
- RAG services: Accumulate token usage across all prompt calls
(extract-concepts, edge-scoring, edge-reasoning,
synthesis). Non-streaming path sends single combined response
instead of chunk + end_of_session.
- Agent orchestrator: UsageTracker accumulates tokens across
meta-router, pattern prompt calls, and react reasoning. Attached
to end_of_dialog.
- Translators: Encode token fields when not None (is not None, not truthy)
- Python SDK: RAG and text-completion methods return
TextCompletionResult (non-streaming) or RAGChunk/AgentAnswer with
token fields (streaming)
- CLI: --show-usage flag on tg-invoke-llm, tg-invoke-prompt,
tg-invoke-graph-rag, tg-invoke-document-rag, tg-invoke-agent
2026-04-13 14:38:34 +01:00
|
|
|
"""Helper for streaming text completion. Yields RAGChunk objects."""
|
2025-12-04 17:38:57 +00:00
|
|
|
async for chunk in self.client._send_request_streaming("text-completion", self.flow_id, request):
|
Expose LLM token usage across all service layers (#782)
Expose LLM token usage (in_token, out_token, model) across all
service layers
Propagate token counts from LLM services through the prompt,
text-completion, graph-RAG, document-RAG, and agent orchestrator
pipelines to the API gateway and Python SDK. All fields are Optional
— None means "not available", distinguishing from a real zero count.
Key changes:
- Schema: Add in_token/out_token/model to TextCompletionResponse,
PromptResponse, GraphRagResponse, DocumentRagResponse,
AgentResponse
- TextCompletionClient: New TextCompletionResult return type. Split
into text_completion() (non-streaming) and
text_completion_stream() (streaming with per-chunk handler
callback)
- PromptClient: New PromptResult with response_type
(text/json/jsonl), typed fields (text/object/objects), and token
usage. All callers updated.
- RAG services: Accumulate token usage across all prompt calls
(extract-concepts, edge-scoring, edge-reasoning,
synthesis). Non-streaming path sends single combined response
instead of chunk + end_of_session.
- Agent orchestrator: UsageTracker accumulates tokens across
meta-router, pattern prompt calls, and react reasoning. Attached
to end_of_dialog.
- Translators: Encode token fields when not None (is not None, not truthy)
- Python SDK: RAG and text-completion methods return
TextCompletionResult (non-streaming) or RAGChunk/AgentAnswer with
token fields (streaming)
- CLI: --show-usage flag on tg-invoke-llm, tg-invoke-prompt,
tg-invoke-graph-rag, tg-invoke-document-rag, tg-invoke-agent
2026-04-13 14:38:34 +01:00
|
|
|
if isinstance(chunk, RAGChunk):
|
|
|
|
|
yield chunk
|
2025-12-04 17:38:57 +00:00
|
|
|
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
async def graph_rag(self, query: str, collection: str,
|
2025-12-04 17:38:57 +00:00
|
|
|
max_subgraph_size: int = 1000, max_subgraph_count: int = 5,
|
|
|
|
|
max_entity_distance: int = 3, streaming: bool = False, **kwargs):
|
|
|
|
|
"""Graph RAG with optional streaming"""
|
|
|
|
|
request = {
|
2025-12-17 21:40:43 +00:00
|
|
|
"query": query,
|
2025-12-04 17:38:57 +00:00
|
|
|
"collection": collection,
|
|
|
|
|
"max-subgraph-size": max_subgraph_size,
|
|
|
|
|
"max-subgraph-count": max_subgraph_count,
|
|
|
|
|
"max-entity-distance": max_entity_distance,
|
|
|
|
|
"streaming": streaming
|
|
|
|
|
}
|
|
|
|
|
request.update(kwargs)
|
|
|
|
|
|
|
|
|
|
if streaming:
|
|
|
|
|
return self._graph_rag_streaming(request)
|
|
|
|
|
else:
|
|
|
|
|
result = await self.client._send_request("graph-rag", self.flow_id, request)
|
|
|
|
|
return result.get("response", "")
|
|
|
|
|
|
|
|
|
|
async def _graph_rag_streaming(self, request):
|
|
|
|
|
"""Helper for streaming graph RAG"""
|
|
|
|
|
async for chunk in self.client._send_request_streaming("graph-rag", self.flow_id, request):
|
|
|
|
|
if hasattr(chunk, 'content'):
|
|
|
|
|
yield chunk.content
|
|
|
|
|
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
async def document_rag(self, query: str, collection: str,
|
2025-12-04 17:38:57 +00:00
|
|
|
doc_limit: int = 10, streaming: bool = False, **kwargs):
|
|
|
|
|
"""Document RAG with optional streaming"""
|
|
|
|
|
request = {
|
2025-12-17 21:40:43 +00:00
|
|
|
"query": query,
|
2025-12-04 17:38:57 +00:00
|
|
|
"collection": collection,
|
|
|
|
|
"doc-limit": doc_limit,
|
|
|
|
|
"streaming": streaming
|
|
|
|
|
}
|
|
|
|
|
request.update(kwargs)
|
|
|
|
|
|
|
|
|
|
if streaming:
|
|
|
|
|
return self._document_rag_streaming(request)
|
|
|
|
|
else:
|
|
|
|
|
result = await self.client._send_request("document-rag", self.flow_id, request)
|
|
|
|
|
return result.get("response", "")
|
|
|
|
|
|
|
|
|
|
async def _document_rag_streaming(self, request):
|
|
|
|
|
"""Helper for streaming document RAG"""
|
|
|
|
|
async for chunk in self.client._send_request_streaming("document-rag", self.flow_id, request):
|
|
|
|
|
if hasattr(chunk, 'content'):
|
|
|
|
|
yield chunk.content
|
|
|
|
|
|
|
|
|
|
async def prompt(self, id: str, variables: Dict[str, str], streaming: bool = False, **kwargs):
|
|
|
|
|
"""Execute prompt with optional streaming"""
|
|
|
|
|
request = {
|
|
|
|
|
"id": id,
|
|
|
|
|
"variables": variables,
|
|
|
|
|
"streaming": streaming
|
|
|
|
|
}
|
|
|
|
|
request.update(kwargs)
|
|
|
|
|
|
|
|
|
|
if streaming:
|
|
|
|
|
return self._prompt_streaming(request)
|
|
|
|
|
else:
|
|
|
|
|
result = await self.client._send_request("prompt", self.flow_id, request)
|
|
|
|
|
return result.get("response", "")
|
|
|
|
|
|
|
|
|
|
async def _prompt_streaming(self, request):
|
|
|
|
|
"""Helper for streaming prompt"""
|
|
|
|
|
async for chunk in self.client._send_request_streaming("prompt", self.flow_id, request):
|
|
|
|
|
if hasattr(chunk, 'content'):
|
|
|
|
|
yield chunk.content
|
|
|
|
|
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
async def graph_embeddings_query(self, text: str, collection: str, limit: int = 10, **kwargs):
|
2025-12-04 17:38:57 +00:00
|
|
|
"""Query graph embeddings for semantic search"""
|
2026-03-08 19:41:52 +00:00
|
|
|
emb_result = await self.embeddings(texts=[text])
|
2026-03-09 10:53:44 +00:00
|
|
|
vector = emb_result.get("vectors", [[]])[0]
|
2026-02-04 14:10:30 +00:00
|
|
|
|
2025-12-04 17:38:57 +00:00
|
|
|
request = {
|
2026-03-09 10:53:44 +00:00
|
|
|
"vector": vector,
|
2025-12-04 17:38:57 +00:00
|
|
|
"collection": collection,
|
|
|
|
|
"limit": limit
|
|
|
|
|
}
|
|
|
|
|
request.update(kwargs)
|
|
|
|
|
|
|
|
|
|
return await self.client._send_request("graph-embeddings", self.flow_id, request)
|
|
|
|
|
|
2026-03-08 19:41:52 +00:00
|
|
|
async def embeddings(self, texts: list, **kwargs):
|
2025-12-04 17:38:57 +00:00
|
|
|
"""Generate text embeddings"""
|
2026-03-08 19:41:52 +00:00
|
|
|
request = {"texts": texts}
|
2025-12-04 17:38:57 +00:00
|
|
|
request.update(kwargs)
|
|
|
|
|
|
|
|
|
|
return await self.client._send_request("embeddings", self.flow_id, request)
|
|
|
|
|
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
async def triples_query(self, s=None, p=None, o=None, collection=None, limit=100, **kwargs):
|
2025-12-04 17:38:57 +00:00
|
|
|
"""Triple pattern query"""
|
|
|
|
|
request = {"limit": limit}
|
|
|
|
|
if s is not None:
|
|
|
|
|
request["s"] = str(s)
|
|
|
|
|
if p is not None:
|
|
|
|
|
request["p"] = str(p)
|
|
|
|
|
if o is not None:
|
|
|
|
|
request["o"] = str(o)
|
|
|
|
|
if collection is not None:
|
|
|
|
|
request["collection"] = collection
|
|
|
|
|
request.update(kwargs)
|
|
|
|
|
|
|
|
|
|
return await self.client._send_request("triples", self.flow_id, request)
|
|
|
|
|
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
async def rows_query(self, query: str, collection: str, variables: Optional[Dict] = None,
|
2026-02-23 15:56:29 +00:00
|
|
|
operation_name: Optional[str] = None, **kwargs):
|
|
|
|
|
"""GraphQL query against structured rows"""
|
2025-12-04 17:38:57 +00:00
|
|
|
request = {
|
|
|
|
|
"query": query,
|
|
|
|
|
"collection": collection
|
|
|
|
|
}
|
|
|
|
|
if variables:
|
|
|
|
|
request["variables"] = variables
|
|
|
|
|
if operation_name:
|
|
|
|
|
request["operationName"] = operation_name
|
|
|
|
|
request.update(kwargs)
|
|
|
|
|
|
2026-02-23 15:56:29 +00:00
|
|
|
return await self.client._send_request("rows", self.flow_id, request)
|
2025-12-04 17:38:57 +00:00
|
|
|
|
|
|
|
|
async def mcp_tool(self, name: str, parameters: Dict[str, Any], **kwargs):
|
|
|
|
|
"""Execute MCP tool"""
|
|
|
|
|
request = {
|
|
|
|
|
"name": name,
|
|
|
|
|
"parameters": parameters
|
|
|
|
|
}
|
|
|
|
|
request.update(kwargs)
|
|
|
|
|
|
|
|
|
|
return await self.client._send_request("mcp-tool", self.flow_id, request)
|
2026-02-23 21:52:56 +00:00
|
|
|
|
|
|
|
|
async def row_embeddings_query(
|
feat: workspace-based multi-tenancy, replacing user as tenancy axis
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
2026-04-18 23:07:26 +01:00
|
|
|
self, text: str, schema_name: str,
|
2026-02-23 21:52:56 +00:00
|
|
|
collection: str = "default", index_name: Optional[str] = None,
|
|
|
|
|
limit: int = 10, **kwargs
|
|
|
|
|
):
|
|
|
|
|
"""Query row embeddings for semantic search on structured data"""
|
2026-03-08 19:41:52 +00:00
|
|
|
emb_result = await self.embeddings(texts=[text])
|
2026-03-09 10:53:44 +00:00
|
|
|
vector = emb_result.get("vectors", [[]])[0]
|
2026-02-23 21:52:56 +00:00
|
|
|
|
|
|
|
|
request = {
|
2026-03-09 10:53:44 +00:00
|
|
|
"vector": vector,
|
2026-02-23 21:52:56 +00:00
|
|
|
"schema_name": schema_name,
|
|
|
|
|
"collection": collection,
|
|
|
|
|
"limit": limit
|
|
|
|
|
}
|
|
|
|
|
if index_name:
|
|
|
|
|
request["index_name"] = index_name
|
|
|
|
|
request.update(kwargs)
|
|
|
|
|
|
|
|
|
|
return await self.client._send_request("row-embeddings", self.flow_id, request)
|