ecosemantic-view 0.1.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ecosemantic_view-0.1.8/.gitignore +24 -0
- ecosemantic_view-0.1.8/LICENSE +73 -0
- ecosemantic_view-0.1.8/NOTICE +13 -0
- ecosemantic_view-0.1.8/PKG-INFO +60 -0
- ecosemantic_view-0.1.8/README.md +26 -0
- ecosemantic_view-0.1.8/ecosemantic/view/__init__.py +1 -0
- ecosemantic_view-0.1.8/ecosemantic/view/activity.py +99 -0
- ecosemantic_view-0.1.8/ecosemantic/view/api.py +303 -0
- ecosemantic_view-0.1.8/ecosemantic/view/app.py +159 -0
- ecosemantic_view-0.1.8/ecosemantic/view/credentials.py +81 -0
- ecosemantic_view-0.1.8/ecosemantic/view/handling.py +146 -0
- ecosemantic_view-0.1.8/ecosemantic/view/installs.py +125 -0
- ecosemantic_view-0.1.8/ecosemantic/view/olca.py +62 -0
- ecosemantic_view-0.1.8/ecosemantic/view/py.typed +0 -0
- ecosemantic_view-0.1.8/ecosemantic/view/static/THIRD_PARTY_NOTICES.txt +2707 -0
- ecosemantic_view-0.1.8/ecosemantic/view/static/assets/graph-DcSFYMYX.js +7 -0
- ecosemantic_view-0.1.8/ecosemantic/view/static/assets/index-CF8_VBnk.js +49 -0
- ecosemantic_view-0.1.8/ecosemantic/view/static/assets/index-D9m29h7N.css +1 -0
- ecosemantic_view-0.1.8/ecosemantic/view/static/assets/logo-BTOAr_MC.svg +1 -0
- ecosemantic_view-0.1.8/ecosemantic/view/static/assets/react-Di0xI-E3.js +3 -0
- ecosemantic_view-0.1.8/ecosemantic/view/static/assets/streamWorker-9cS8ciPJ.js +1 -0
- ecosemantic_view-0.1.8/ecosemantic/view/static/index.html +15 -0
- ecosemantic_view-0.1.8/ecosemantic/view/stream.py +61 -0
- ecosemantic_view-0.1.8/ecosemantic/view/tables.py +128 -0
- ecosemantic_view-0.1.8/pyproject.toml +52 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
.venv/
|
|
4
|
+
venv/
|
|
5
|
+
*.egg-info/
|
|
6
|
+
dist/
|
|
7
|
+
build/
|
|
8
|
+
node_modules/
|
|
9
|
+
.pytest_cache/
|
|
10
|
+
.ruff_cache/
|
|
11
|
+
.mypy_cache/
|
|
12
|
+
.coverage
|
|
13
|
+
coverage.json
|
|
14
|
+
htmlcov/
|
|
15
|
+
|
|
16
|
+
.env
|
|
17
|
+
.env.*
|
|
18
|
+
!.env.example
|
|
19
|
+
|
|
20
|
+
.DS_Store
|
|
21
|
+
graphify-out
|
|
22
|
+
knowledge/
|
|
23
|
+
.okf-cache.json
|
|
24
|
+
**/ecosemantic/view/static/
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# PolyForm Noncommercial License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
|
|
12
|
+
|
|
13
|
+
## Distribution License
|
|
14
|
+
|
|
15
|
+
The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
|
|
16
|
+
|
|
17
|
+
## Notices
|
|
18
|
+
|
|
19
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
|
|
20
|
+
|
|
21
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
22
|
+
|
|
23
|
+
## Changes and New Works License
|
|
24
|
+
|
|
25
|
+
The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
|
|
26
|
+
|
|
27
|
+
## Patent License
|
|
28
|
+
|
|
29
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
30
|
+
|
|
31
|
+
## Noncommercial Purposes
|
|
32
|
+
|
|
33
|
+
Any noncommercial purpose is a permitted purpose.
|
|
34
|
+
|
|
35
|
+
## Personal Uses
|
|
36
|
+
|
|
37
|
+
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
|
|
38
|
+
|
|
39
|
+
## Noncommercial Organizations
|
|
40
|
+
|
|
41
|
+
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
|
|
42
|
+
|
|
43
|
+
## Fair Use
|
|
44
|
+
|
|
45
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
46
|
+
|
|
47
|
+
## No Other Rights
|
|
48
|
+
|
|
49
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
50
|
+
|
|
51
|
+
## Patent Defense
|
|
52
|
+
|
|
53
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
54
|
+
|
|
55
|
+
## Violations
|
|
56
|
+
|
|
57
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
58
|
+
|
|
59
|
+
## No Liability
|
|
60
|
+
|
|
61
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
62
|
+
|
|
63
|
+
## Definitions
|
|
64
|
+
|
|
65
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
66
|
+
|
|
67
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
68
|
+
|
|
69
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
70
|
+
|
|
71
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
72
|
+
|
|
73
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
EcoSemantic
|
|
2
|
+
Required Notice: Copyright 2026 Carlos Gaete-Morales (https://github.com/cdgaete/ecosemantic)
|
|
3
|
+
|
|
4
|
+
EcoSemantic is licensed under the PolyForm Noncommercial License 1.0.0, in LICENSE
|
|
5
|
+
and at https://polyformproject.org/licenses/noncommercial/1.0.0. Any noncommercial
|
|
6
|
+
purpose is permitted. Commercial use needs a separate licence from Carlos
|
|
7
|
+
Gaete-Morales: cdgaete@gmail.com.
|
|
8
|
+
|
|
9
|
+
The page that ecosemantic-view carries bundles third-party software under its
|
|
10
|
+
own licences; their notices are in ecosemantic/view/static/THIRD_PARTY_NOTICES.txt
|
|
11
|
+
inside that distribution. Components of the page adapt shadcn/ui, Copyright (c)
|
|
12
|
+
2023 shadcn, under the MIT licence, as page/PROVENANCE.md in the source
|
|
13
|
+
repository records.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: ecosemantic-view
|
|
3
|
+
Version: 0.1.8
|
|
4
|
+
Summary: The API of the EcoSemantic page, and the built page.
|
|
5
|
+
Project-URL: Homepage, https://github.com/cdgaete/ecosemantic
|
|
6
|
+
Project-URL: Repository, https://github.com/cdgaete/ecosemantic
|
|
7
|
+
Project-URL: Issues, https://github.com/cdgaete/ecosemantic/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/cdgaete/ecosemantic/blob/main/CHANGELOG.md
|
|
9
|
+
Author-email: Carlos Gaete-Morales <cdgaete@gmail.com>
|
|
10
|
+
License-Expression: PolyForm-Noncommercial-1.0.0
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
License-File: NOTICE
|
|
13
|
+
Keywords: LCA,MCP,life cycle assessment,review,web page
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.12
|
|
24
|
+
Requires-Dist: ecosemantic-core==0.1.8
|
|
25
|
+
Requires-Dist: ecosemantic-engine==0.1.8
|
|
26
|
+
Requires-Dist: ecosemantic-mcp==0.1.8
|
|
27
|
+
Requires-Dist: fastmcp-slim<5,>=4
|
|
28
|
+
Requires-Dist: fastmcp<5,>=4
|
|
29
|
+
Requires-Dist: mcp<3,>=2.2.0
|
|
30
|
+
Requires-Dist: pydantic<3,>=2.7
|
|
31
|
+
Requires-Dist: sse-starlette<4,>=3.4.11
|
|
32
|
+
Requires-Dist: starlette<2,>=1.6.0
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# ecosemantic-view
|
|
36
|
+
|
|
37
|
+
The HTTP API of the EcoSemantic page and the page itself: where a person watches an agent build a product system, reads its change log and results, and posts requests the agent answers.
|
|
38
|
+
|
|
39
|
+
## Names it publishes
|
|
40
|
+
|
|
41
|
+
- `ecosemantic.view.app` — the view application: the token gate, the routes and the page.
|
|
42
|
+
- `ecosemantic.view.api` — the routes over the tools, each calling the tool the agent calls.
|
|
43
|
+
- `ecosemantic.view.stream` — the event streams: what happens to one product system, and what happens in the whole process, as it happens.
|
|
44
|
+
- `ecosemantic.view.credentials` — the page's credential routes, answering a peer on this machine only, since the page's token travels over plain HTTP.
|
|
45
|
+
- `ecosemantic.view.activity`, `installs`, `olca`, `tables` and `handling` — the activity routes and the trail of a log entry, the routes over the install tools, the openLCA hand-off, the table routes, and what every route handler shares: its error boundary and its uploads.
|
|
46
|
+
- The built page, under `ecosemantic/view/static/`: its HTML, its scripts and `THIRD_PARTY_NOTICES.txt`, which lists what the page bundles and under which licence.
|
|
47
|
+
|
|
48
|
+
The page's source is `page/`, a workspace of its own built with `pnpm build`, and the build writes that directory into this distribution's wheel and sdist.
|
|
49
|
+
|
|
50
|
+
## What it depends on
|
|
51
|
+
|
|
52
|
+
`ecosemantic-engine`, `ecosemantic-core` and `ecosemantic-mcp` at the same version, and fastmcp, fastmcp-slim, mcp, pydantic, starlette and sse-starlette.
|
|
53
|
+
|
|
54
|
+
## Installing it
|
|
55
|
+
|
|
56
|
+
`pip install ecosemantic` installs this distribution with the other five and the `ecosemantic` command that serves the page beside the MCP endpoint. `pip install ecosemantic-view` installs the API and the page alone, for a server of someone else's.
|
|
57
|
+
|
|
58
|
+
## Licence
|
|
59
|
+
|
|
60
|
+
PolyForm Noncommercial License 1.0.0, copyright Carlos Gaete-Morales: `LICENSE` and `NOTICE` in this distribution. Any noncommercial purpose is permitted, by individuals and by charities, schools, public research and government bodies; commercial use needs a separate licence from him (cdgaete@gmail.com). EcoSemantic is his trademark, and the licence grants no right to use that name (`TRADEMARK.md` in the repository). The page bundles third-party software under its own licences, listed in `ecosemantic/view/static/THIRD_PARTY_NOTICES.txt`. The code is published from https://github.com/cdgaete/ecosemantic.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# ecosemantic-view
|
|
2
|
+
|
|
3
|
+
The HTTP API of the EcoSemantic page and the page itself: where a person watches an agent build a product system, reads its change log and results, and posts requests the agent answers.
|
|
4
|
+
|
|
5
|
+
## Names it publishes
|
|
6
|
+
|
|
7
|
+
- `ecosemantic.view.app` — the view application: the token gate, the routes and the page.
|
|
8
|
+
- `ecosemantic.view.api` — the routes over the tools, each calling the tool the agent calls.
|
|
9
|
+
- `ecosemantic.view.stream` — the event streams: what happens to one product system, and what happens in the whole process, as it happens.
|
|
10
|
+
- `ecosemantic.view.credentials` — the page's credential routes, answering a peer on this machine only, since the page's token travels over plain HTTP.
|
|
11
|
+
- `ecosemantic.view.activity`, `installs`, `olca`, `tables` and `handling` — the activity routes and the trail of a log entry, the routes over the install tools, the openLCA hand-off, the table routes, and what every route handler shares: its error boundary and its uploads.
|
|
12
|
+
- The built page, under `ecosemantic/view/static/`: its HTML, its scripts and `THIRD_PARTY_NOTICES.txt`, which lists what the page bundles and under which licence.
|
|
13
|
+
|
|
14
|
+
The page's source is `page/`, a workspace of its own built with `pnpm build`, and the build writes that directory into this distribution's wheel and sdist.
|
|
15
|
+
|
|
16
|
+
## What it depends on
|
|
17
|
+
|
|
18
|
+
`ecosemantic-engine`, `ecosemantic-core` and `ecosemantic-mcp` at the same version, and fastmcp, fastmcp-slim, mcp, pydantic, starlette and sse-starlette.
|
|
19
|
+
|
|
20
|
+
## Installing it
|
|
21
|
+
|
|
22
|
+
`pip install ecosemantic` installs this distribution with the other five and the `ecosemantic` command that serves the page beside the MCP endpoint. `pip install ecosemantic-view` installs the API and the page alone, for a server of someone else's.
|
|
23
|
+
|
|
24
|
+
## Licence
|
|
25
|
+
|
|
26
|
+
PolyForm Noncommercial License 1.0.0, copyright Carlos Gaete-Morales: `LICENSE` and `NOTICE` in this distribution. Any noncommercial purpose is permitted, by individuals and by charities, schools, public research and government bodies; commercial use needs a separate licence from him (cdgaete@gmail.com). EcoSemantic is his trademark, and the licence grants no right to use that name (`TRADEMARK.md` in the repository). The page bundles third-party software under its own licences, listed in `ecosemantic/view/static/THIRD_PARTY_NOTICES.txt`. The code is published from https://github.com/cdgaete/ecosemantic.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""The browser view the server serves: its API, its event stream, its page and its panels."""
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""The activity routes: the agents, their calls, the trail of a log entry, an agent's open
|
|
2
|
+
trail, a call run again, and a system's calculations."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
|
|
9
|
+
from fastmcp import FastMCP
|
|
10
|
+
from pydantic import BaseModel
|
|
11
|
+
from starlette.requests import Request
|
|
12
|
+
from starlette.responses import JSONResponse, Response
|
|
13
|
+
from starlette.routing import Route
|
|
14
|
+
|
|
15
|
+
from ecosemantic.errors import Invalid
|
|
16
|
+
from ecosemantic.mcp.context import ServerContext
|
|
17
|
+
from ecosemantic.mcp.envelopes import as_uuid
|
|
18
|
+
from ecosemantic.mcp.recording import current_hash
|
|
19
|
+
from ecosemantic.ports.types import open_trail, trail
|
|
20
|
+
from ecosemantic.view.api import IMPACT_TOOL, structured
|
|
21
|
+
from ecosemantic.view.handling import answering
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _listed(items: Sequence[BaseModel]) -> JSONResponse:
|
|
25
|
+
return JSONResponse({"value": [item.model_dump(mode="json") for item in items]})
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _time(text: str | None) -> datetime | None:
|
|
29
|
+
if text is None:
|
|
30
|
+
return None
|
|
31
|
+
try:
|
|
32
|
+
return datetime.fromisoformat(text)
|
|
33
|
+
except ValueError as error:
|
|
34
|
+
raise Invalid(f"after={text!r} is not an ISO 8601 time") from error
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def routes(server: FastMCP, context: ServerContext) -> list[Route]:
|
|
38
|
+
"""The six activity routes."""
|
|
39
|
+
|
|
40
|
+
@answering
|
|
41
|
+
async def agents(request: Request) -> Response:
|
|
42
|
+
return _listed(await context.activity.list_agents())
|
|
43
|
+
|
|
44
|
+
@answering
|
|
45
|
+
async def calls(request: Request) -> Response:
|
|
46
|
+
query = request.query_params
|
|
47
|
+
named = query.get("product_system_id")
|
|
48
|
+
return _listed(
|
|
49
|
+
await context.activity.list_calls(
|
|
50
|
+
agent=query.get("agent"),
|
|
51
|
+
product_system_id=as_uuid(named) if named is not None else None,
|
|
52
|
+
after=_time(query.get("after")),
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
@answering
|
|
57
|
+
async def entry_trail(request: Request) -> Response:
|
|
58
|
+
system_id = as_uuid(request.path_params["id"])
|
|
59
|
+
await context.storage.get_product_system(system_id)
|
|
60
|
+
recorded = await context.activity.list_calls()
|
|
61
|
+
return _listed(trail(recorded, system_id, request.path_params["seq"]))
|
|
62
|
+
|
|
63
|
+
@answering
|
|
64
|
+
async def calculations(request: Request) -> Response:
|
|
65
|
+
system_id = as_uuid(request.path_params["id"])
|
|
66
|
+
await context.storage.get_product_system(system_id)
|
|
67
|
+
recorded = await context.activity.list_calls(product_system_id=system_id)
|
|
68
|
+
return _listed([call for call in reversed(recorded) if call.tool == IMPACT_TOOL])
|
|
69
|
+
|
|
70
|
+
@answering
|
|
71
|
+
async def agent_trail(request: Request) -> Response:
|
|
72
|
+
recorded = await context.activity.list_calls()
|
|
73
|
+
named = request.query_params.get("product_system_id")
|
|
74
|
+
system_id = as_uuid(named) if named is not None else None
|
|
75
|
+
return _listed(open_trail(recorded, request.path_params["agent"], system_id))
|
|
76
|
+
|
|
77
|
+
@answering
|
|
78
|
+
async def rerun(request: Request) -> Response:
|
|
79
|
+
call = await context.activity.get_call(as_uuid(request.path_params["call_id"]))
|
|
80
|
+
if call.effect in ("write", "delete"):
|
|
81
|
+
raise Invalid(f"{call.tool} changes a model; the page reruns reads and calculations")
|
|
82
|
+
result = await structured(server, call.tool, call.arguments, started=True)
|
|
83
|
+
hash_now = (
|
|
84
|
+
await current_hash(context.storage, call.product_system_id)
|
|
85
|
+
if call.product_system_id is not None
|
|
86
|
+
else None
|
|
87
|
+
)
|
|
88
|
+
return JSONResponse(
|
|
89
|
+
{"value": {"result": result, "at_hash": call.at_hash, "hash_now": hash_now}}
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
return [
|
|
93
|
+
Route("/api/agents", agents),
|
|
94
|
+
Route("/api/agents/{agent}/trail", agent_trail),
|
|
95
|
+
Route("/api/activity", calls),
|
|
96
|
+
Route("/api/activity/{call_id}/rerun", rerun, methods=["POST"]),
|
|
97
|
+
Route("/api/product-systems/{id}/log/{seq:int}/trail", entry_trail),
|
|
98
|
+
Route("/api/product-systems/{id}/calculations", calculations),
|
|
99
|
+
]
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"""The routes over the tools: each calls the tool the agent calls, through the in-process client."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Awaitable, Callable, Mapping
|
|
6
|
+
from importlib import metadata
|
|
7
|
+
|
|
8
|
+
from fastmcp import Client, FastMCP
|
|
9
|
+
from fastmcp.exceptions import ToolError
|
|
10
|
+
from mcp.shared.exceptions import MCPError
|
|
11
|
+
from mcp.types import INVALID_PARAMS, Implementation
|
|
12
|
+
from pydantic import JsonValue, ValidationError
|
|
13
|
+
from starlette.requests import Request
|
|
14
|
+
from starlette.responses import JSONResponse, Response
|
|
15
|
+
from starlette.routing import Route
|
|
16
|
+
|
|
17
|
+
from ecosemantic.errors import Invalid
|
|
18
|
+
from ecosemantic.mcp.context import ServerContext
|
|
19
|
+
from ecosemantic.mcp.envelopes import Answer, Listing, as_uuid, envelope_of
|
|
20
|
+
from ecosemantic.mcp.recording import STARTED, VIEW
|
|
21
|
+
from ecosemantic.model.validation import anchors
|
|
22
|
+
from ecosemantic.ports.types import Layout, LogEntry, RequestDraft
|
|
23
|
+
from ecosemantic.view.handling import (
|
|
24
|
+
ToolRefused,
|
|
25
|
+
answering,
|
|
26
|
+
created,
|
|
27
|
+
envelope_response,
|
|
28
|
+
error_response,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
GATE_PARAMETER = "token"
|
|
32
|
+
IMPACT_TOOL = "calculate_impact"
|
|
33
|
+
STARTED_PARAMETER = "started"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
async def structured(
|
|
37
|
+
server: FastMCP, name: str, arguments: Mapping[str, JsonValue], started: bool = False
|
|
38
|
+
) -> dict[str, JsonValue]:
|
|
39
|
+
"""A tool's structured content, or ToolRefused carrying its error as the envelope. A call the
|
|
40
|
+
server refuses as invalid parameters carries the invalid envelope. A call a person started is
|
|
41
|
+
marked so, and the recorder keeps such a calculation."""
|
|
42
|
+
try:
|
|
43
|
+
async with Client(
|
|
44
|
+
server,
|
|
45
|
+
client_info=Implementation(name=VIEW, version=metadata.version("ecosemantic-view")),
|
|
46
|
+
) as client:
|
|
47
|
+
result = await client.call_tool(
|
|
48
|
+
name, dict(arguments), meta={STARTED: True} if started else None
|
|
49
|
+
)
|
|
50
|
+
except MCPError as error:
|
|
51
|
+
if error.code != INVALID_PARAMS:
|
|
52
|
+
raise
|
|
53
|
+
raise ToolRefused(error_response(Invalid(f"{name}: {error.message}"))) from error
|
|
54
|
+
except ToolError as error:
|
|
55
|
+
raise ToolRefused(envelope_response(envelope_of(error))) from error
|
|
56
|
+
return result.structured_content
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
async def call_tool(
|
|
60
|
+
server: FastMCP, name: str, arguments: Mapping[str, JsonValue], started: bool = False
|
|
61
|
+
) -> Response:
|
|
62
|
+
"""One tool call rendered as an HTTP response: its structured content, or its error."""
|
|
63
|
+
try:
|
|
64
|
+
return JSONResponse(await structured(server, name, arguments, started))
|
|
65
|
+
except ToolRefused as refused:
|
|
66
|
+
return refused.response
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
async def json_object(request: Request) -> dict[str, JsonValue]:
|
|
70
|
+
"""The request body as a JSON object; Invalid when it is not one."""
|
|
71
|
+
try:
|
|
72
|
+
body = await request.json()
|
|
73
|
+
except ValueError as error:
|
|
74
|
+
raise Invalid("the body is not JSON") from error
|
|
75
|
+
if not isinstance(body, dict):
|
|
76
|
+
raise Invalid("the body is not a JSON object")
|
|
77
|
+
return body
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def arguments(query: Mapping[str, str]) -> dict[str, JsonValue]:
|
|
81
|
+
"""Query parameters as tool arguments; the token stays out, the tool's schema types the rest."""
|
|
82
|
+
return {key: value for key, value in query.items() if key != GATE_PARAMETER}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def routes(server: FastMCP, context: ServerContext) -> list[Route]:
|
|
86
|
+
"""Every JSON route under /api, in the order the spec lists them."""
|
|
87
|
+
|
|
88
|
+
async def databases(request: Request) -> Response:
|
|
89
|
+
return await call_tool(server, "list_databases", {})
|
|
90
|
+
|
|
91
|
+
async def vocabulary(request: Request) -> Response:
|
|
92
|
+
params = request.path_params
|
|
93
|
+
return await call_tool(
|
|
94
|
+
server, "list_vocabulary", {"database": params["name"], "kind": params["kind"]}
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
async def processes(request: Request) -> Response:
|
|
98
|
+
args = arguments(request.query_params)
|
|
99
|
+
return await call_tool(
|
|
100
|
+
server, "search_processes", {"database": request.path_params["name"], **args}
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
async def elementary_flows(request: Request) -> Response:
|
|
104
|
+
args = arguments(request.query_params)
|
|
105
|
+
return await call_tool(
|
|
106
|
+
server, "search_elementary_flows", {"database": request.path_params["name"], **args}
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
async def methods(request: Request) -> Response:
|
|
110
|
+
args = arguments(request.query_params)
|
|
111
|
+
return await call_tool(
|
|
112
|
+
server, "search_methods", {"database": request.path_params["name"], **args}
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
async def process(request: Request) -> Response:
|
|
116
|
+
params = request.path_params
|
|
117
|
+
return await call_tool(
|
|
118
|
+
server,
|
|
119
|
+
"get_background_process",
|
|
120
|
+
{"database": params["name"], "process_id": params["ref"], "exchanges": True},
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
async def elementary_flow(request: Request) -> Response:
|
|
124
|
+
params = request.path_params
|
|
125
|
+
return await call_tool(
|
|
126
|
+
server, "get_elementary_flow", {"database": params["name"], "flow_id": params["ref"]}
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
async def systems(request: Request) -> Response:
|
|
130
|
+
return await call_tool(server, "list_product_systems", {})
|
|
131
|
+
|
|
132
|
+
async def system(request: Request) -> Response:
|
|
133
|
+
args = arguments(request.query_params)
|
|
134
|
+
return await call_tool(
|
|
135
|
+
server,
|
|
136
|
+
"get_product_system",
|
|
137
|
+
{"product_system_id": request.path_params["id"], "variant": args.get("variant")},
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
async def parameters(request: Request) -> Response:
|
|
141
|
+
args = arguments(request.query_params)
|
|
142
|
+
content_hash = args.pop("hash", None)
|
|
143
|
+
return await call_tool(
|
|
144
|
+
server,
|
|
145
|
+
"get_parameters",
|
|
146
|
+
{"product_system_id": request.path_params["id"], "content_hash": content_hash},
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
@answering
|
|
150
|
+
async def log(request: Request) -> Response:
|
|
151
|
+
entries: list[LogEntry] = []
|
|
152
|
+
cursor: str | None = None
|
|
153
|
+
while True:
|
|
154
|
+
page = Listing[LogEntry].model_validate(
|
|
155
|
+
await structured(
|
|
156
|
+
server,
|
|
157
|
+
"get_product_system_log",
|
|
158
|
+
{"product_system_id": request.path_params["id"], "cursor": cursor},
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
entries.extend(page.items)
|
|
162
|
+
cursor = page.next_cursor
|
|
163
|
+
if cursor is None:
|
|
164
|
+
return JSONResponse(Answer[list[LogEntry]](value=entries).model_dump(mode="json"))
|
|
165
|
+
|
|
166
|
+
@answering
|
|
167
|
+
async def version(request: Request) -> Response:
|
|
168
|
+
params = request.path_params
|
|
169
|
+
stored = await context.storage.get_version(as_uuid(params["id"]), params["hash"])
|
|
170
|
+
return JSONResponse(stored.model_dump(mode="json"))
|
|
171
|
+
|
|
172
|
+
@answering
|
|
173
|
+
async def system_anchors(request: Request) -> Response:
|
|
174
|
+
stored = await context.storage.get_product_system(as_uuid(request.path_params["id"]))
|
|
175
|
+
return JSONResponse(Answer[list[str]](value=anchors(stored)).model_dump(mode="json"))
|
|
176
|
+
|
|
177
|
+
async def validation(request: Request) -> Response:
|
|
178
|
+
args = arguments(request.query_params)
|
|
179
|
+
return await call_tool(
|
|
180
|
+
server,
|
|
181
|
+
"validate_product_system",
|
|
182
|
+
{"product_system_id": request.path_params["id"], **args},
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
async def server_details(request: Request) -> Response:
|
|
186
|
+
"""The client that introduced itself, this package's version and the two directories."""
|
|
187
|
+
return JSONResponse(
|
|
188
|
+
{
|
|
189
|
+
"client": context.client or None,
|
|
190
|
+
"version": metadata.version("ecosemantic"),
|
|
191
|
+
"data_dir": str(context.data_dir),
|
|
192
|
+
"systems_dir": str(context.systems_dir),
|
|
193
|
+
}
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
def calculation(tool: str) -> Callable[[Request], Awaitable[Response]]:
|
|
197
|
+
@answering
|
|
198
|
+
async def handler(request: Request) -> Response:
|
|
199
|
+
body = await json_object(request)
|
|
200
|
+
return await call_tool(
|
|
201
|
+
server,
|
|
202
|
+
tool,
|
|
203
|
+
{"product_system_id": request.path_params["id"], **body},
|
|
204
|
+
started=request.query_params.get(STARTED_PARAMETER) == "true",
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
return handler
|
|
208
|
+
|
|
209
|
+
async def runs_listed(request: Request) -> Response:
|
|
210
|
+
return await call_tool(
|
|
211
|
+
server, "list_runs", {"product_system_id": request.path_params["id"]}
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
async def run_read(request: Request) -> Response:
|
|
215
|
+
return await call_tool(server, "get_run", {"run_id": request.path_params["run_id"]})
|
|
216
|
+
|
|
217
|
+
async def run_cancelled(request: Request) -> Response:
|
|
218
|
+
return await call_tool(server, "cancel_run", {"run_id": request.path_params["run_id"]})
|
|
219
|
+
|
|
220
|
+
async def requests_listed(request: Request) -> Response:
|
|
221
|
+
args = arguments(request.query_params)
|
|
222
|
+
return await call_tool(
|
|
223
|
+
server,
|
|
224
|
+
"list_requests",
|
|
225
|
+
{"product_system_id": request.path_params["id"], "open_only": False, **args},
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
@answering
|
|
229
|
+
async def request_posted(request: Request) -> Response:
|
|
230
|
+
body = await json_object(request)
|
|
231
|
+
system_id = as_uuid(request.path_params["id"])
|
|
232
|
+
try:
|
|
233
|
+
draft = RequestDraft.model_validate(
|
|
234
|
+
{
|
|
235
|
+
"actor": {"kind": "person", "name": str(body.get("actor", ""))},
|
|
236
|
+
"kind": body.get("kind"),
|
|
237
|
+
"at_hash": body.get("at_hash", ""),
|
|
238
|
+
"message": body.get("message", ""),
|
|
239
|
+
"entity": body.get("entity"),
|
|
240
|
+
"entity_id": body.get("entity_id"),
|
|
241
|
+
}
|
|
242
|
+
)
|
|
243
|
+
except ValidationError as exc:
|
|
244
|
+
raise Invalid(str(exc)) from exc
|
|
245
|
+
posted = await context.storage.post_request(system_id, draft)
|
|
246
|
+
return created(posted)
|
|
247
|
+
|
|
248
|
+
async def layout(request: Request) -> Response:
|
|
249
|
+
args = arguments(request.query_params)
|
|
250
|
+
return await call_tool(
|
|
251
|
+
server,
|
|
252
|
+
"graph_layout",
|
|
253
|
+
{"product_system_id": request.path_params["id"], "variant": args.get("variant")},
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
@answering
|
|
257
|
+
async def place(request: Request) -> Response:
|
|
258
|
+
body = await json_object(request)
|
|
259
|
+
return await call_tool(
|
|
260
|
+
server, "arrange_graph", {"product_system_id": request.path_params["id"], **body}
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
@answering
|
|
264
|
+
async def release(request: Request) -> Response:
|
|
265
|
+
system_id = as_uuid(request.path_params["id"])
|
|
266
|
+
await context.storage.put_layout(system_id, Layout())
|
|
267
|
+
args = arguments(request.query_params)
|
|
268
|
+
return await call_tool(
|
|
269
|
+
server,
|
|
270
|
+
"graph_layout",
|
|
271
|
+
{"product_system_id": str(system_id), "variant": args.get("variant")},
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
base = "/api/product-systems/{id}"
|
|
275
|
+
return [
|
|
276
|
+
Route("/api/server", server_details),
|
|
277
|
+
Route("/api/databases", databases),
|
|
278
|
+
Route("/api/databases/{name}/vocabulary/{kind}", vocabulary),
|
|
279
|
+
Route("/api/databases/{name}/processes", processes),
|
|
280
|
+
Route("/api/databases/{name}/elementary-flows", elementary_flows),
|
|
281
|
+
Route("/api/databases/{name}/elementary-flows/{ref}", elementary_flow),
|
|
282
|
+
Route("/api/databases/{name}/methods", methods),
|
|
283
|
+
Route("/api/databases/{name}/processes/{ref}", process),
|
|
284
|
+
Route("/api/product-systems", systems),
|
|
285
|
+
Route(base, system),
|
|
286
|
+
Route(f"{base}/parameters", parameters),
|
|
287
|
+
Route(f"{base}/log", log),
|
|
288
|
+
Route(f"{base}/versions/{{hash}}", version),
|
|
289
|
+
Route(f"{base}/validation", validation),
|
|
290
|
+
Route(f"{base}/anchors", system_anchors),
|
|
291
|
+
Route(f"{base}/impact", calculation(IMPACT_TOOL), methods=["POST"]),
|
|
292
|
+
Route(f"{base}/contributions", calculation("contribution_analysis"), methods=["POST"]),
|
|
293
|
+
Route(f"{base}/inventory", calculation("calculate_inventory"), methods=["POST"]),
|
|
294
|
+
Route(f"{base}/runs", runs_listed),
|
|
295
|
+
Route(f"{base}/runs", calculation("start_monte_carlo"), methods=["POST"]),
|
|
296
|
+
Route("/api/runs/{run_id}", run_read),
|
|
297
|
+
Route("/api/runs/{run_id}/cancel", run_cancelled, methods=["POST"]),
|
|
298
|
+
Route(f"{base}/requests", requests_listed),
|
|
299
|
+
Route(f"{base}/requests", request_posted, methods=["POST"]),
|
|
300
|
+
Route(f"{base}/layout", layout),
|
|
301
|
+
Route(f"{base}/layout", place, methods=["POST"]),
|
|
302
|
+
Route(f"{base}/layout", release, methods=["DELETE"]),
|
|
303
|
+
]
|