mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-09-07 04:02:40 +00:00
Adds Google ADK (Agent Development Kit) support to Semantica. `semantica_kg_tools()` and `semantica_decision_tools()` expose entity/relation extraction, graph updates, and decision recording as ADK `FunctionTool`s. `SemanticaSessionService` implements ADK session storage on top of a Semantica `ContextGraph`, so session state, events, and knowledge graph data can live in the same graph instead of keeping sessions in memory. There were also a number of dependency and CI fixes needed to get the integration working reliably. `google-adk` is pinned to a range that avoids the CI `websockets` conflict, the deprecated `pinecone-client` dependency was replaced with `pinecone`, Windows-only dependencies now have the appropriate platform markers, and `requirements-ci.txt` was regenerated to match. A `pip-audit` pass also required updates to `google-adk` and `starlette` for known CVEs. Some unrelated `pyproject.toml` changes had slipped in during rebases, so the previous version, dependency bounds, `ingest-sap`/LangChain entries, and package-data settings were restored. A few bugs in the initial ADK implementation were fixed during review: * `extract_relations()` was calling `RelationExtractor.extract_entities()`, which doesn't exist on that extractor. The failure was being caught and returned in the tool's `error` field, leaving callers with an empty relation list. It now calls the correct extraction path. * The repo's top-level `mcp/` package shadowed the third-party `mcp` package imported by `google.adk`, causing `google.adk` imports to fail from a normal repo checkout. The local package was moved to `semantica_mcp/mcp/`. The MCP move needed a follow-up as well. `semantica/cli.py` and four existing tests were still importing from `mcp.*`, and the modules under `semantica_mcp/mcp/` still used the old absolute imports internally. `semantica_mcp` was also missing from the setuptools package include list and had no `__init__.py`, so it wouldn't have been included in an installed package. Those imports and packaging settings are fixed now. The session service and ADK tools also had a few other problems: * `list_sessions()` returned a plain list instead of ADK's `ListSessionsResponse`. The original import for that type doesn't work against the installed `google-adk` package, so it was silently falling back to a stub. `user_id` was also incorrectly required instead of being optional. * Session node IDs were built by joining `app_name`, `user_id`, and `session_id` with unescaped colons, which allowed different identities to produce the same graph node ID. Each component is now encoded before joining. * `kg_tools.py` and `decision_tools.py` each had their own lock registry and default graph instance. Sharing a graph between the two modules therefore didn't share the lock, and using both factories without an explicit graph produced two different defaults. The shared state now lives in one module used by both. * `add_to_graph` had a `TypeError` compatibility fallback that couldn't succeed with the current `RelationExtractor` API and could hide the original extraction error. That fallback was removed. * `append_event` persisted partial streaming events even though ADK's base session service skips them. * `get_session()` ignored its `config` argument, so `num_recent_events` and `after_timestamp` had no effect. * The async session-service methods performed synchronous graph scans while holding a `threading.RLock` on the event loop thread. That work now runs in worker threads with `asyncio.to_thread()` so a slow or contended graph operation doesn't block the loop. --- Co-authored-by: Zohaib Hassnain [109234410+ZohaibHassan16@users.noreply.github.com](mailto:109234410+ZohaibHassan16@users.noreply.github.com)
201 lines
4.7 KiB
Python
201 lines
4.7 KiB
Python
import asyncio
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
import importlib
|
|
from unittest.mock import patch
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def require_adk(request):
|
|
"""Skip tests if ADK is missing, unless testing missing dependency behavior."""
|
|
if "missing_adk" not in request.node.name:
|
|
pytest.importorskip("google.adk")
|
|
|
|
|
|
from google.adk.events import Event
|
|
|
|
from integrations.google_adk import (
|
|
SemanticaSessionService,
|
|
semantica_decision_tools,
|
|
semantica_kg_tools,
|
|
)
|
|
|
|
|
|
def test_all_integrations_share_same_graph():
|
|
from semantica.context import ContextGraph
|
|
|
|
graph = ContextGraph()
|
|
|
|
kg_tools = semantica_kg_tools(graph)
|
|
decision_tools = semantica_decision_tools(graph)
|
|
session_service = SemanticaSessionService(graph)
|
|
|
|
assert session_service.graph is graph
|
|
|
|
# FunctionTool closures capture the supplied shared graph.
|
|
assert len(kg_tools) == 4
|
|
assert len(decision_tools) == 2
|
|
|
|
|
|
def test_kg_tools_and_decision_tools_lock_the_same_graph():
|
|
"""kg_tools and decision_tools must serialize writes against each other
|
|
when handed the same graph, not just within their own module."""
|
|
from integrations.google_adk.decision_tools import _graph_lock as decision_graph_lock
|
|
from integrations.google_adk.kg_tools import _graph_lock as kg_graph_lock
|
|
from semantica.context import ContextGraph
|
|
|
|
graph = ContextGraph()
|
|
|
|
assert kg_graph_lock(graph) is decision_graph_lock(graph)
|
|
|
|
|
|
def test_shared_graph_session_and_decision_state():
|
|
from semantica.context import ContextGraph
|
|
|
|
graph = ContextGraph()
|
|
|
|
session_service = SemanticaSessionService(graph)
|
|
|
|
# Record a decision using the same graph.
|
|
from integrations.google_adk.decision_tools import _record_decision
|
|
|
|
decision_result = _record_decision(
|
|
category="shared-context",
|
|
scenario="Multi-agent workflow",
|
|
reasoning="Verify shared graph state.",
|
|
outcome="Shared graph works",
|
|
confidence=0.95,
|
|
decision_maker="test-agent",
|
|
entities=[],
|
|
source_documents=[],
|
|
graph=graph,
|
|
)
|
|
|
|
assert decision_result["decision_id"]
|
|
|
|
# Create a session using the same graph.
|
|
session = asyncio.run(
|
|
session_service.create_session(
|
|
app_name="shared-app",
|
|
user_id="shared-user",
|
|
state={
|
|
"decision_id": decision_result["decision_id"],
|
|
},
|
|
)
|
|
)
|
|
|
|
assert session.id
|
|
|
|
loaded = asyncio.run(
|
|
session_service.get_session(
|
|
app_name="shared-app",
|
|
user_id="shared-user",
|
|
session_id=session.id,
|
|
)
|
|
)
|
|
|
|
assert loaded is not None
|
|
assert loaded.state["decision_id"] == (
|
|
decision_result["decision_id"]
|
|
)
|
|
|
|
|
|
def test_shared_graph_event_and_knowledge_nodes():
|
|
from semantica.context import ContextGraph
|
|
|
|
graph = ContextGraph()
|
|
|
|
session_service = SemanticaSessionService(graph)
|
|
|
|
session = asyncio.run(
|
|
session_service.create_session(
|
|
app_name="shared-app",
|
|
user_id="shared-user",
|
|
)
|
|
)
|
|
|
|
event = Event(
|
|
author="researcher",
|
|
invocation_id="shared-invocation",
|
|
)
|
|
|
|
asyncio.run(
|
|
session_service.append_event(
|
|
session,
|
|
event,
|
|
)
|
|
)
|
|
|
|
nodes = graph.find_nodes()
|
|
|
|
session_nodes = [
|
|
node
|
|
for node in nodes
|
|
if isinstance(node, dict)
|
|
and node.get("type") == "ADKSession"
|
|
]
|
|
|
|
event_nodes = [
|
|
node
|
|
for node in nodes
|
|
if isinstance(node, dict)
|
|
and node.get("type") == "ADKEvent"
|
|
]
|
|
|
|
assert len(session_nodes) == 1
|
|
assert len(event_nodes) == 1
|
|
|
|
assert (
|
|
session_nodes[0]["metadata"]["session_id"]
|
|
== session.id
|
|
)
|
|
|
|
assert (
|
|
event_nodes[0]["metadata"]["session_id"]
|
|
== session.id
|
|
)
|
|
|
|
|
|
def test_shared_graph_supports_multiple_sessions():
|
|
from semantica.context import ContextGraph
|
|
|
|
graph = ContextGraph()
|
|
|
|
service = SemanticaSessionService(graph)
|
|
|
|
session1 = asyncio.run(
|
|
service.create_session(
|
|
app_name="multi-agent",
|
|
user_id="user-1",
|
|
)
|
|
)
|
|
|
|
session2 = asyncio.run(
|
|
service.create_session(
|
|
app_name="multi-agent",
|
|
user_id="user-2",
|
|
)
|
|
)
|
|
|
|
assert session1.id != session2.id
|
|
|
|
response1 = asyncio.run(
|
|
service.list_sessions(
|
|
app_name="multi-agent",
|
|
user_id="user-1",
|
|
)
|
|
)
|
|
|
|
response2 = asyncio.run(
|
|
service.list_sessions(
|
|
app_name="multi-agent",
|
|
user_id="user-2",
|
|
)
|
|
)
|
|
|
|
assert len(response1.sessions) == 1
|
|
assert len(response2.sessions) == 1
|
|
|
|
assert response1.sessions[0].id == session1.id
|
|
assert response2.sessions[0].id == session2.id |