Service Interfaces (Protocols)

The interfaces module defines the structural contracts that all UI code must program against. Using typing.Protocol with @runtime_checkable means:

  1. Any object with the right methods satisfies the interface — no inheritance required.
  2. isinstance(obj, IGraphService) works at runtime for defensive checks.
  3. mypy enforces correct usage at development time.

IGraphService

The primary service contract for all graph operations. UI widgets receive an IGraphService instance and use only the methods defined here.

Graph Lifecycle

Method Description
create_graph(workspace_id, type_name, name) Create a new graph in a workspace
get_graph(graph_id) Load a graph by ID
list_graphs(workspace_id) Return all graphs in a workspace
delete_graph(graph_id) Permanently delete a graph and all its contents

Node Operations

Method Description
add_node(graph_id, type_name, attributes) Validate and add a new node
update_node(node_id, attributes) Merge new attributes into an existing node
remove_node(graph_id, node_id) Delete a node and all incident edges

Edge Operations

Method Description
add_edge(graph_id, source_id, target_id, type_name, attributes) Validate and add a directed edge
remove_edge(graph_id, edge_id) Delete an edge

IWorkspaceService

The contract for workspace lifecycle operations.

Method Description
create_workspace(name) Create a new workspace
get_workspace(workspace_id) Return a workspace by ID (KeyError if not found)
list_workspaces() Return all known workspaces

API Reference

knowledge_platform.services.interfaces.IGraphService

Bases: Protocol

Contract for all graph-mutation and graph-query operations.

Source code in src/knowledge_platform/services/interfaces.py
@runtime_checkable
class IGraphService(Protocol):
    """Contract for all graph-mutation and graph-query operations."""

    def create_graph(
        self,
        workspace_id: WorkspaceId,
        type_name: str,
        name: str,
    ) -> Graph:
        """Create a new graph in the given workspace.

        Args:
            workspace_id: Owning workspace.
            type_name: Registered graph type name.
            name: Human-readable display name.

        Returns:
            Newly created :class:`~knowledge_platform.core.graph.Graph`.
        """
        ...

    def get_graph(self, graph_id: GraphId) -> Graph:
        """Retrieve a graph by ID.

        Args:
            graph_id: Target identifier.

        Returns:
            The :class:`~knowledge_platform.core.graph.Graph`.

        Raises:
            KeyError: If not found.
        """
        ...

    def list_graphs(self, workspace_id: WorkspaceId) -> list[Graph]:
        """List all graphs in a workspace.

        Args:
            workspace_id: Owning workspace.

        Returns:
            Possibly empty list of graphs.
        """
        ...

    def delete_graph(self, graph_id: GraphId) -> None:
        """Permanently delete a graph.

        Args:
            graph_id: Target identifier.
        """
        ...

    def add_node(
        self,
        graph_id: GraphId,
        type_name: str,
        attributes: dict[str, object],
    ) -> Node:
        """Create and add a node to a graph.

        Args:
            graph_id: Owning graph.
            type_name: Node type registered on the graph's type.
            attributes: Initial attribute payload.

        Returns:
            The created :class:`~knowledge_platform.core.node.Node`.
        """
        ...

    def update_node(self, node_id: NodeId, attributes: dict[str, object]) -> Node:
        """Update attributes on an existing node.

        Args:
            node_id: Target node.
            attributes: Attributes to merge (partial update).

        Returns:
            Updated :class:`~knowledge_platform.core.node.Node`.
        """
        ...

    def remove_node(self, graph_id: GraphId, node_id: NodeId) -> None:
        """Remove a node and all its incident edges.

        Args:
            graph_id: Owning graph.
            node_id: Target node.
        """
        ...

    def add_edge(
        self,
        graph_id: GraphId,
        source_id: NodeId,
        target_id: NodeId,
        type_name: str,
        attributes: dict[str, object],
    ) -> Edge:
        """Create and add a directed edge between two nodes.

        Args:
            graph_id: Owning graph.
            source_id: Origin node.
            target_id: Destination node.
            type_name: Edge type registered on the graph's type.
            attributes: Initial attribute payload.

        Returns:
            The created :class:`~knowledge_platform.core.edge.Edge`.
        """
        ...

    def remove_edge(self, graph_id: GraphId, edge_id: EdgeId) -> None:
        """Remove an edge.

        Args:
            graph_id: Owning graph.
            edge_id: Target edge.
        """
        ...

Functions

add_edge
add_edge(
    graph_id: GraphId,
    source_id: NodeId,
    target_id: NodeId,
    type_name: str,
    attributes: dict[str, object],
) -> Edge

Create and add a directed edge between two nodes.

Parameters:

Name Type Description Default
graph_id GraphId

Owning graph.

required
source_id NodeId

Origin node.

required
target_id NodeId

Destination node.

required
type_name str

Edge type registered on the graph's type.

required
attributes dict[str, object]

Initial attribute payload.

required

Returns:

Type Description
Edge

The created :class:~knowledge_platform.core.edge.Edge.

Source code in src/knowledge_platform/services/interfaces.py
def add_edge(
    self,
    graph_id: GraphId,
    source_id: NodeId,
    target_id: NodeId,
    type_name: str,
    attributes: dict[str, object],
) -> Edge:
    """Create and add a directed edge between two nodes.

    Args:
        graph_id: Owning graph.
        source_id: Origin node.
        target_id: Destination node.
        type_name: Edge type registered on the graph's type.
        attributes: Initial attribute payload.

    Returns:
        The created :class:`~knowledge_platform.core.edge.Edge`.
    """
    ...
add_node
add_node(
    graph_id: GraphId,
    type_name: str,
    attributes: dict[str, object],
) -> Node

Create and add a node to a graph.

Parameters:

Name Type Description Default
graph_id GraphId

Owning graph.

required
type_name str

Node type registered on the graph's type.

required
attributes dict[str, object]

Initial attribute payload.

required

Returns:

Type Description
Node

The created :class:~knowledge_platform.core.node.Node.

Source code in src/knowledge_platform/services/interfaces.py
def add_node(
    self,
    graph_id: GraphId,
    type_name: str,
    attributes: dict[str, object],
) -> Node:
    """Create and add a node to a graph.

    Args:
        graph_id: Owning graph.
        type_name: Node type registered on the graph's type.
        attributes: Initial attribute payload.

    Returns:
        The created :class:`~knowledge_platform.core.node.Node`.
    """
    ...
create_graph
create_graph(
    workspace_id: WorkspaceId, type_name: str, name: str
) -> Graph

Create a new graph in the given workspace.

Parameters:

Name Type Description Default
workspace_id WorkspaceId

Owning workspace.

required
type_name str

Registered graph type name.

required
name str

Human-readable display name.

required

Returns:

Type Description
Graph

Newly created :class:~knowledge_platform.core.graph.Graph.

Source code in src/knowledge_platform/services/interfaces.py
def create_graph(
    self,
    workspace_id: WorkspaceId,
    type_name: str,
    name: str,
) -> Graph:
    """Create a new graph in the given workspace.

    Args:
        workspace_id: Owning workspace.
        type_name: Registered graph type name.
        name: Human-readable display name.

    Returns:
        Newly created :class:`~knowledge_platform.core.graph.Graph`.
    """
    ...
delete_graph
delete_graph(graph_id: GraphId) -> None

Permanently delete a graph.

Parameters:

Name Type Description Default
graph_id GraphId

Target identifier.

required
Source code in src/knowledge_platform/services/interfaces.py
def delete_graph(self, graph_id: GraphId) -> None:
    """Permanently delete a graph.

    Args:
        graph_id: Target identifier.
    """
    ...
get_graph
get_graph(graph_id: GraphId) -> Graph

Retrieve a graph by ID.

Parameters:

Name Type Description Default
graph_id GraphId

Target identifier.

required

Returns:

Name Type Description
The Graph

class:~knowledge_platform.core.graph.Graph.

Raises:

Type Description
KeyError

If not found.

Source code in src/knowledge_platform/services/interfaces.py
def get_graph(self, graph_id: GraphId) -> Graph:
    """Retrieve a graph by ID.

    Args:
        graph_id: Target identifier.

    Returns:
        The :class:`~knowledge_platform.core.graph.Graph`.

    Raises:
        KeyError: If not found.
    """
    ...
list_graphs
list_graphs(workspace_id: WorkspaceId) -> list[Graph]

List all graphs in a workspace.

Parameters:

Name Type Description Default
workspace_id WorkspaceId

Owning workspace.

required

Returns:

Type Description
list[Graph]

Possibly empty list of graphs.

Source code in src/knowledge_platform/services/interfaces.py
def list_graphs(self, workspace_id: WorkspaceId) -> list[Graph]:
    """List all graphs in a workspace.

    Args:
        workspace_id: Owning workspace.

    Returns:
        Possibly empty list of graphs.
    """
    ...
remove_edge
remove_edge(graph_id: GraphId, edge_id: EdgeId) -> None

Remove an edge.

Parameters:

Name Type Description Default
graph_id GraphId

Owning graph.

required
edge_id EdgeId

Target edge.

required
Source code in src/knowledge_platform/services/interfaces.py
def remove_edge(self, graph_id: GraphId, edge_id: EdgeId) -> None:
    """Remove an edge.

    Args:
        graph_id: Owning graph.
        edge_id: Target edge.
    """
    ...
remove_node
remove_node(graph_id: GraphId, node_id: NodeId) -> None

Remove a node and all its incident edges.

Parameters:

Name Type Description Default
graph_id GraphId

Owning graph.

required
node_id NodeId

Target node.

required
Source code in src/knowledge_platform/services/interfaces.py
def remove_node(self, graph_id: GraphId, node_id: NodeId) -> None:
    """Remove a node and all its incident edges.

    Args:
        graph_id: Owning graph.
        node_id: Target node.
    """
    ...
update_node
update_node(
    node_id: NodeId, attributes: dict[str, object]
) -> Node

Update attributes on an existing node.

Parameters:

Name Type Description Default
node_id NodeId

Target node.

required
attributes dict[str, object]

Attributes to merge (partial update).

required

Returns:

Name Type Description
Updated Node

class:~knowledge_platform.core.node.Node.

Source code in src/knowledge_platform/services/interfaces.py
def update_node(self, node_id: NodeId, attributes: dict[str, object]) -> Node:
    """Update attributes on an existing node.

    Args:
        node_id: Target node.
        attributes: Attributes to merge (partial update).

    Returns:
        Updated :class:`~knowledge_platform.core.node.Node`.
    """
    ...

knowledge_platform.services.interfaces.IWorkspaceService

Bases: Protocol

Contract for workspace lifecycle operations.

Source code in src/knowledge_platform/services/interfaces.py
@runtime_checkable
class IWorkspaceService(Protocol):
    """Contract for workspace lifecycle operations."""

    def create_workspace(self, name: str) -> Workspace:
        """Create a new local workspace.

        Args:
            name: Display name.

        Returns:
            Newly created :class:`~knowledge_platform.workspace.Workspace`.
        """
        ...

    def get_workspace(self, workspace_id: WorkspaceId) -> Workspace:
        """Retrieve a workspace by ID.

        Args:
            workspace_id: Target identifier.

        Returns:
            The :class:`~knowledge_platform.workspace.Workspace`.

        Raises:
            KeyError: If not found.
        """
        ...

    def list_workspaces(self) -> list[Workspace]:
        """Return all known workspaces.

        Returns:
            Possibly empty list.
        """
        ...

Functions

create_workspace
create_workspace(name: str) -> Workspace

Create a new local workspace.

Parameters:

Name Type Description Default
name str

Display name.

required

Returns:

Type Description
Workspace

Newly created :class:~knowledge_platform.workspace.Workspace.

Source code in src/knowledge_platform/services/interfaces.py
def create_workspace(self, name: str) -> Workspace:
    """Create a new local workspace.

    Args:
        name: Display name.

    Returns:
        Newly created :class:`~knowledge_platform.workspace.Workspace`.
    """
    ...
get_workspace
get_workspace(workspace_id: WorkspaceId) -> Workspace

Retrieve a workspace by ID.

Parameters:

Name Type Description Default
workspace_id WorkspaceId

Target identifier.

required

Returns:

Name Type Description
The Workspace

class:~knowledge_platform.workspace.Workspace.

Raises:

Type Description
KeyError

If not found.

Source code in src/knowledge_platform/services/interfaces.py
def get_workspace(self, workspace_id: WorkspaceId) -> Workspace:
    """Retrieve a workspace by ID.

    Args:
        workspace_id: Target identifier.

    Returns:
        The :class:`~knowledge_platform.workspace.Workspace`.

    Raises:
        KeyError: If not found.
    """
    ...
list_workspaces
list_workspaces() -> list[Workspace]

Return all known workspaces.

Returns:

Type Description
list[Workspace]

Possibly empty list.

Source code in src/knowledge_platform/services/interfaces.py
def list_workspaces(self) -> list[Workspace]:
    """Return all known workspaces.

    Returns:
        Possibly empty list.
    """
    ...