Metadata-Version: 2.5
Name: toolframe-graph-relate
Version: 0.1.1
Summary: Persistent local graph storage for Toolframe plugins using Relate and parameterized Cypher
Project-URL: Repository, https://github.com/toolframe/toolframe-graph-relate
Project-URL: Toolframe, https://github.com/toolframe/toolframe-core
Project-URL: Relate, https://github.com/relatedb/relate
Author-email: byfb <62798989+byfb@users.noreply.github.com>
Requires-Python: >=3.14
Requires-Dist: relate-db<2,>=1.5.0
Requires-Dist: toolframe-sdk<1,>=0.4.1
Description-Content-Type: text/markdown

# Toolframe Relate Graph

Store and traverse a private, local graph for each Toolframe plugin. The
embedded Relate engine executes parameterized Cypher and saves each caller's
nodes and relationships in a separate `.rl` file.

## Install

Use a running Toolframe application with Python 3.14+ and a compatible
`relate-db` wheel. Replace `APP` with your application home.

```sh
toolframe --home APP plugin add pypi:toolframe-graph-relate==0.1.1 --enable
```

## Find out who Mira mentors

In the consuming plugin's definition, declare the service dependency.

```python
from toolframe_sdk import Requirement

# PluginDefinition(..., requires=requires)
requires = {"graph": Requirement(service="toolframe.graph", version=1)}
```

Mira mentors Nilo. In a service handler, create a `Person` node for each of them
and a `MENTORS` relationship from Mira to Nilo. Then follow Mira's outgoing
relationships to find her mentees.

```python
await self.context.call("graph", "execute", {
    "statement": """
        MERGE (mentor:Person {name: $mentor})
        MERGE (mentee:Person {name: $mentee})
        MERGE (mentor)-[:MENTORS]->(mentee)
    """,
    "parameters": {"mentor": "Mira", "mentee": "Nilo"},
})

result = await self.context.call("graph", "query", {
    "statement": """
        MATCH (:Person {name: $mentor})-[:MENTORS]->(person:Person)
        RETURN person.name AS mentee ORDER BY mentee
    """,
    "parameters": {"mentor": "Mira"},
})
print(result["rows"])  # [{"mentee": "Nilo"}]
```

## Service contract

| Method | Behavior |
| --- | --- |
| `query` | Runs one Cypher statement on a read-only handle. The first read sees an empty graph. |
| `execute` | Runs one statement in a transaction. The service checks the result before committing. |

Both methods accept a `statement` and an optional `parameters` object and
return `columns`, `rows`, and Relate's mutation counters in `stats`. A row is
keyed by column name. Returned nodes keep their IDs, labels and properties.
Relationships keep their endpoints and type. Paths contain ordered nodes and
relationships. Temporal values use ISO strings, points include their SRID and
coordinates, and vectors include their coordinate type and values.

Use `LIMIT` and project only the properties you need. Each call can return up
to 1,000 rows and 900,000 bytes of JSON. An oversized result raises
`result_too_large` and rolls back the statement. For writes that may be retried
after losing a response, use stable keys and `MERGE`.

## Data and access

Toolframe supplies the application and caller identities. Each caller has its
own Relate bundle at `data/graphs/<sha256-of-application-id>/<plugin-id>.rl`
under the application home. The provider serializes calls and closes the bundle
after each operation. Keep the application ID, consuming plugin ID and graph
files together across restarts, reloads and restores. To back up the graph,
stop the application and copy its configuration and `data/graphs/` directory.

The service is internal. A consuming plugin controls which operations it
exposes to Dashboard, HTTP or MCP clients. Graphs must fit in memory when open.
