gabriel / musehub public

responses.py file-level

at sha256:f · View file ↗ · Intel ↗

History
1 files
1 commits
0 hotspots
0 🧊 dead
0 💥 blast risk
sha256:a Merge 'docs/database-architecture-canonical' into 'dev' — proposal: doc… · gabriel · Sep 8, 2026
1 """Protocol introspection response models and helpers."""
2
3 from pydantic import BaseModel
4
5 from muse.core.types import content_hash
6 from musehub.types.json_types import JSONValue
7 from musehub.protocol.version import MUSE_VERSION
8
9 class ProtocolInfoResponse(BaseModel):
10 """Response shape for ``GET /protocol``."""
11
12 version: str
13 protocol_hash: str
14 event_count: int
15 tool_count: int
16
17 def compute_protocol_hash(data: JSONValue) -> str:
18 """Return a stable ``sha256:``-prefixed content ID for any JSON-serialisable object.
19
20 Delegates to ``muse.core.types.content_hash`` for canonical compact JSON
21 serialisation (``sort_keys=True``, no whitespace, UTF-8) before hashing.
22
23 >>> compute_protocol_hash({"b": 2, "a": 1}) == compute_protocol_hash({"a": 1, "b": 2})
24 True
25 """
26 return content_hash(data)
27
28 def build_protocol_info(event_count: int, tool_count: int, schema: JSONValue) -> ProtocolInfoResponse:
29 """Construct a ``ProtocolInfoResponse`` from live event/tool counts and the full schema."""
30 return ProtocolInfoResponse(
31 version=MUSE_VERSION,
32 protocol_hash=compute_protocol_hash(schema),
33 event_count=event_count,
34 tool_count=tool_count,
35 )