vease 4.0.0__py3-none-any.whl
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.
- vease/__init__.py +228 -0
- vease/__main__.py +211 -0
- vease/client.py +139 -0
- vease/installer.py +263 -0
- vease/launcher.py +211 -0
- vease/py.typed +0 -0
- vease/resources/__init__.py +40 -0
- vease/resources/data.py +82 -0
- vease/resources/export.py +40 -0
- vease/resources/filter.py +96 -0
- vease/resources/horizon_stack.py +36 -0
- vease/resources/style.py +522 -0
- vease/resources/view.py +96 -0
- vease/resources/viewer.py +24 -0
- vease/session.py +73 -0
- vease/version.py +175 -0
- vease-4.0.0.dist-info/METADATA +175 -0
- vease-4.0.0.dist-info/RECORD +20 -0
- vease-4.0.0.dist-info/WHEEL +5 -0
- vease-4.0.0.dist-info/top_level.txt +1 -0
vease/__init__.py
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"""Drive the Vease app from Python.
|
|
2
|
+
|
|
3
|
+
from vease import Vease
|
|
4
|
+
|
|
5
|
+
with Vease.launch() as vease: # starts Vease, closes it at the end of the block
|
|
6
|
+
loaded = vease.data.load("model.og_brep")["data"]
|
|
7
|
+
model_id = loaded[0]["id"]
|
|
8
|
+
vease.style.model.surfaces.coloring(model_id, "random")
|
|
9
|
+
vease.view.orient("zplus")
|
|
10
|
+
vease.export.screenshot("model.png")
|
|
11
|
+
|
|
12
|
+
Vease.launch() runs the Vease found in folder=, installed by `python -m vease install`, or
|
|
13
|
+
on PATH; launch(install=True) installs the Vease release this package drives first, if needed.
|
|
14
|
+
|
|
15
|
+
This package X.Y.Z drives Vease X.Y.Z only: launch() and connect() raise
|
|
16
|
+
VersionMismatchError for another Vease (check_version=False skips that), and log a warning
|
|
17
|
+
when a newer release is out. Update with `pip install -U vease`, then
|
|
18
|
+
`python -m vease install` (or launch(install=True)).
|
|
19
|
+
|
|
20
|
+
To keep one Vease open across scripts, start it once from a terminal:
|
|
21
|
+
|
|
22
|
+
python -m vease start # then: status, stop
|
|
23
|
+
|
|
24
|
+
Vease.connect() then connects to it, and leaves it open on close(); so does
|
|
25
|
+
Vease.connect("http://127.0.0.1:3000") for any running Vease.
|
|
26
|
+
|
|
27
|
+
Entry points (help() on each gives its parameters and what it returns):
|
|
28
|
+
|
|
29
|
+
Vease.launch, Vease.connect, install(version=None), __version__
|
|
30
|
+
|
|
31
|
+
vease.data list, details, load, create, rename, delete
|
|
32
|
+
details(id) tells what a data can be styled with: start there
|
|
33
|
+
vease.style .image / .mesh / .model trees, one route per property, and colormaps()
|
|
34
|
+
attribute(ids, name) color by an attribute, wherever it is stored
|
|
35
|
+
distinct_colors(ids) tell data apart
|
|
36
|
+
color_collections(model_id, collection_ids, color="random")
|
|
37
|
+
vease.view reset, focus, orient, camera (azimuth, elevation, zoom...), save,
|
|
38
|
+
restore, scene; each returns once the view is final
|
|
39
|
+
vease.filter shrink, explode, slice, clip, threshold
|
|
40
|
+
vease.viewer state: camera, saved positions, z scaling
|
|
41
|
+
vease.export screenshot, project
|
|
42
|
+
vease.horizon_stack create, info
|
|
43
|
+
|
|
44
|
+
Every call raises VeaseError (status_code, message) when Vease refuses it.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
from __future__ import annotations
|
|
48
|
+
|
|
49
|
+
import os
|
|
50
|
+
from types import TracebackType
|
|
51
|
+
from typing import Self
|
|
52
|
+
|
|
53
|
+
from .client import (
|
|
54
|
+
DEFAULT_READY_TIMEOUT,
|
|
55
|
+
DEFAULT_REQUEST_TIMEOUT,
|
|
56
|
+
VeaseClient,
|
|
57
|
+
VeaseError,
|
|
58
|
+
)
|
|
59
|
+
from .installer import Installation, install
|
|
60
|
+
|
|
61
|
+
# Vease.launch() has an install flag, which hides install() in its body
|
|
62
|
+
from .installer import install as install_vease
|
|
63
|
+
from .launcher import DEFAULT_START_TIMEOUT, VeaseProcess, find_vease
|
|
64
|
+
from .resources.data import DataResource
|
|
65
|
+
from .resources.export import ExportResource
|
|
66
|
+
from .resources.filter import FilterResource
|
|
67
|
+
from .resources.horizon_stack import HorizonStackResource
|
|
68
|
+
from .resources.style import StyleResource
|
|
69
|
+
from .resources.view import ViewResource
|
|
70
|
+
from .resources.viewer import ViewerResource
|
|
71
|
+
from .session import running_session
|
|
72
|
+
from .version import (
|
|
73
|
+
VersionMismatchError,
|
|
74
|
+
check_version,
|
|
75
|
+
is_development,
|
|
76
|
+
matching_vease_version,
|
|
77
|
+
package_version,
|
|
78
|
+
warn_if_outdated,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
__version__ = package_version()
|
|
82
|
+
|
|
83
|
+
__all__ = [
|
|
84
|
+
"Installation",
|
|
85
|
+
"Vease",
|
|
86
|
+
"VeaseClient",
|
|
87
|
+
"VeaseError",
|
|
88
|
+
"VeaseProcess",
|
|
89
|
+
"VersionMismatchError",
|
|
90
|
+
"__version__",
|
|
91
|
+
"install",
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class Vease:
|
|
96
|
+
"""Entry point: vease.data, .style, .view, .filter, .viewer, .export and .horizon_stack.
|
|
97
|
+
|
|
98
|
+
Get one with Vease.connect() (a running Vease) or Vease.launch() (start one).
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
def __init__(self, client: VeaseClient, process: VeaseProcess | None = None):
|
|
102
|
+
"""Wrap a ready client; process is the Vease to close on close(), if owned."""
|
|
103
|
+
self.client = client
|
|
104
|
+
self.process = process
|
|
105
|
+
self.data = DataResource(client)
|
|
106
|
+
self.style = StyleResource(client)
|
|
107
|
+
self.view = ViewResource(client)
|
|
108
|
+
self.filter = FilterResource(client)
|
|
109
|
+
self.viewer = ViewerResource(client)
|
|
110
|
+
self.export = ExportResource(client)
|
|
111
|
+
self.horizon_stack = HorizonStackResource(client)
|
|
112
|
+
|
|
113
|
+
@classmethod
|
|
114
|
+
def connect(
|
|
115
|
+
cls,
|
|
116
|
+
url: str | None = None,
|
|
117
|
+
*,
|
|
118
|
+
ready_timeout: float = DEFAULT_READY_TIMEOUT,
|
|
119
|
+
request_timeout: float | None = DEFAULT_REQUEST_TIMEOUT,
|
|
120
|
+
check_version: bool = True,
|
|
121
|
+
) -> Self:
|
|
122
|
+
"""Connect to a running Vease, which close() leaves open.
|
|
123
|
+
|
|
124
|
+
url: e.g. "http://127.0.0.1:3000"; without it, the Vease `python -m vease start`
|
|
125
|
+
started. Raises ConnectionError when no url is given and none was started, and
|
|
126
|
+
VersionMismatchError when that Vease is not the release this package drives, unless
|
|
127
|
+
check_version=False.
|
|
128
|
+
"""
|
|
129
|
+
if url is None:
|
|
130
|
+
session = running_session()
|
|
131
|
+
if session is None:
|
|
132
|
+
raise ConnectionError(
|
|
133
|
+
"No running Vease was started with `python -m vease start`: "
|
|
134
|
+
"start one so, pass its url, or use Vease.launch()"
|
|
135
|
+
)
|
|
136
|
+
url = session.url
|
|
137
|
+
if not url.startswith(("http://", "https://")):
|
|
138
|
+
raise ValueError(
|
|
139
|
+
f"url must start with http:// or https://, got {url!r}; "
|
|
140
|
+
"to start Vease from a folder, use Vease.launch(folder)"
|
|
141
|
+
)
|
|
142
|
+
client = VeaseClient(url, request_timeout)
|
|
143
|
+
try:
|
|
144
|
+
client.wait_until_ready(ready_timeout)
|
|
145
|
+
_check(client, check_version)
|
|
146
|
+
except BaseException:
|
|
147
|
+
client.close()
|
|
148
|
+
raise
|
|
149
|
+
return cls(client)
|
|
150
|
+
|
|
151
|
+
@classmethod
|
|
152
|
+
def launch(
|
|
153
|
+
cls,
|
|
154
|
+
folder: str | os.PathLike[str] | None = None,
|
|
155
|
+
*,
|
|
156
|
+
install: bool = False,
|
|
157
|
+
log_path: str | os.PathLike[str] | None = None,
|
|
158
|
+
start_timeout: float = DEFAULT_START_TIMEOUT,
|
|
159
|
+
ready_timeout: float = DEFAULT_READY_TIMEOUT,
|
|
160
|
+
request_timeout: float | None = DEFAULT_REQUEST_TIMEOUT,
|
|
161
|
+
check_version: bool = True,
|
|
162
|
+
) -> Self:
|
|
163
|
+
"""Start Vease, and close it on close() or at the end of a with block.
|
|
164
|
+
|
|
165
|
+
Runs the Vease executable in folder; without folder, the installed Vease this
|
|
166
|
+
package drives, else the one on PATH, else the newest installed one.
|
|
167
|
+
install=True first installs the Vease release this package drives, unless it is
|
|
168
|
+
already (a download of several hundred MB), and removes the other installed
|
|
169
|
+
versions. Raises VersionMismatchError when the started Vease is not that release,
|
|
170
|
+
unless check_version=False. Its output goes to log_path, a new temporary file by
|
|
171
|
+
default.
|
|
172
|
+
"""
|
|
173
|
+
if install and folder is None:
|
|
174
|
+
_install_matching()
|
|
175
|
+
process = VeaseProcess(folder, log_path, start_timeout)
|
|
176
|
+
client = VeaseClient(process.url, request_timeout)
|
|
177
|
+
try:
|
|
178
|
+
client.wait_until_ready(ready_timeout, process.is_alive)
|
|
179
|
+
_check(client, check_version)
|
|
180
|
+
except BaseException:
|
|
181
|
+
client.close()
|
|
182
|
+
process.stop()
|
|
183
|
+
raise
|
|
184
|
+
return cls(client, process)
|
|
185
|
+
|
|
186
|
+
@property
|
|
187
|
+
def url(self) -> str:
|
|
188
|
+
"""Root URL of the Vease this object talks to."""
|
|
189
|
+
return self.client.app_url
|
|
190
|
+
|
|
191
|
+
@property
|
|
192
|
+
def mcp_url(self) -> str:
|
|
193
|
+
"""MCP endpoint of this Vease, for agents that drive it through tools."""
|
|
194
|
+
return self.client.mcp_url
|
|
195
|
+
|
|
196
|
+
def close(self) -> None:
|
|
197
|
+
"""Close the connection, and Vease itself if this object started it."""
|
|
198
|
+
self.client.close()
|
|
199
|
+
if self.process is not None:
|
|
200
|
+
self.process.stop()
|
|
201
|
+
|
|
202
|
+
def __enter__(self) -> Self:
|
|
203
|
+
return self
|
|
204
|
+
|
|
205
|
+
def __exit__(
|
|
206
|
+
self,
|
|
207
|
+
exc_type: type[BaseException] | None,
|
|
208
|
+
exc: BaseException | None,
|
|
209
|
+
traceback: TracebackType | None,
|
|
210
|
+
) -> None:
|
|
211
|
+
self.close()
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _install_matching() -> None:
|
|
215
|
+
if not is_development(matching_vease_version()):
|
|
216
|
+
install_vease()
|
|
217
|
+
return
|
|
218
|
+
# A package installed from a checkout matches no release: any installed Vease will do
|
|
219
|
+
try:
|
|
220
|
+
find_vease()
|
|
221
|
+
except FileNotFoundError:
|
|
222
|
+
install_vease()
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _check(client: VeaseClient, check: bool) -> None:
|
|
226
|
+
if check:
|
|
227
|
+
check_version(client)
|
|
228
|
+
warn_if_outdated()
|
vease/__main__.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"""Install Vease, and start, check or stop a Vease that scripts connect to.
|
|
2
|
+
|
|
3
|
+
python -m vease install [--version 3.2.0 | --latest] # into ~/.vease/app/<version>
|
|
4
|
+
python -m vease start [--folder FOLDER] [--install] # prints the URLs to reach it
|
|
5
|
+
python -m vease status
|
|
6
|
+
python -m vease stop
|
|
7
|
+
|
|
8
|
+
While it runs, Vease.connect() connects to it.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import argparse
|
|
14
|
+
import json
|
|
15
|
+
import logging
|
|
16
|
+
import sys
|
|
17
|
+
from collections.abc import Sequence
|
|
18
|
+
|
|
19
|
+
from . import Vease
|
|
20
|
+
from .client import DEFAULT_READY_TIMEOUT
|
|
21
|
+
from .installer import install as install_vease
|
|
22
|
+
from .launcher import DEFAULT_START_TIMEOUT, stop_vease
|
|
23
|
+
from .session import (
|
|
24
|
+
Session,
|
|
25
|
+
clear_session,
|
|
26
|
+
running_session,
|
|
27
|
+
session_path,
|
|
28
|
+
write_session,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _describe(session: Session) -> dict[str, object]:
|
|
33
|
+
return {
|
|
34
|
+
"url": session.url,
|
|
35
|
+
"mcp_url": session.mcp_url,
|
|
36
|
+
"pid": session.pid,
|
|
37
|
+
"log_path": session.log_path,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _print(state: str, session: Session | None = None) -> None:
|
|
42
|
+
print(
|
|
43
|
+
json.dumps(
|
|
44
|
+
{"state": state, **(_describe(session) if session else {})}, indent=2
|
|
45
|
+
)
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _print_progress(done: int, total: int) -> None:
|
|
50
|
+
if total:
|
|
51
|
+
print(f"\rDownloading Vease: {100 * done // total}%", end="", file=sys.stderr)
|
|
52
|
+
if done >= total:
|
|
53
|
+
print(file=sys.stderr)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def install(
|
|
57
|
+
version: str | None, force: bool, latest: bool = False, keep_others: bool = False
|
|
58
|
+
) -> int:
|
|
59
|
+
"""Install a Vease release, the one this package drives by default, and report where."""
|
|
60
|
+
installation = install_vease(
|
|
61
|
+
version,
|
|
62
|
+
latest=latest,
|
|
63
|
+
force=force,
|
|
64
|
+
keep_others=keep_others,
|
|
65
|
+
progress=_print_progress,
|
|
66
|
+
)
|
|
67
|
+
print(
|
|
68
|
+
json.dumps(
|
|
69
|
+
{
|
|
70
|
+
"state": "installed",
|
|
71
|
+
"version": installation.version,
|
|
72
|
+
"executable": str(installation.executable),
|
|
73
|
+
},
|
|
74
|
+
indent=2,
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
return 0
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def start(
|
|
81
|
+
folder: str | None,
|
|
82
|
+
start_timeout: float,
|
|
83
|
+
ready_timeout: float,
|
|
84
|
+
install: bool = False,
|
|
85
|
+
check_version: bool = True,
|
|
86
|
+
) -> int:
|
|
87
|
+
"""Start Vease and record it, or report the one already running.
|
|
88
|
+
|
|
89
|
+
install: install the Vease release this package drives first, if needed.
|
|
90
|
+
check_version: fail when the started Vease is not that release.
|
|
91
|
+
"""
|
|
92
|
+
session = running_session()
|
|
93
|
+
if session is not None:
|
|
94
|
+
_print("already running", session)
|
|
95
|
+
return 0
|
|
96
|
+
vease = Vease.launch(
|
|
97
|
+
folder,
|
|
98
|
+
install=install,
|
|
99
|
+
start_timeout=start_timeout,
|
|
100
|
+
ready_timeout=ready_timeout,
|
|
101
|
+
check_version=check_version,
|
|
102
|
+
)
|
|
103
|
+
# Only the connection closes: Vease keeps running for later scripts
|
|
104
|
+
vease.client.close()
|
|
105
|
+
process = vease.process
|
|
106
|
+
assert process is not None
|
|
107
|
+
session = Session(
|
|
108
|
+
url=process.url, pid=process.process.pid, log_path=str(process.log_path)
|
|
109
|
+
)
|
|
110
|
+
write_session(session)
|
|
111
|
+
_print("started", session)
|
|
112
|
+
return 0
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def status() -> int:
|
|
116
|
+
"""Report the recorded Vease if it still answers; exit code 1 if none."""
|
|
117
|
+
session = running_session()
|
|
118
|
+
if session is None:
|
|
119
|
+
_print("not running")
|
|
120
|
+
return 1
|
|
121
|
+
_print("running", session)
|
|
122
|
+
return 0
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def stop() -> int:
|
|
126
|
+
"""Close the recorded Vease, only if it still answers at its URL."""
|
|
127
|
+
session = running_session()
|
|
128
|
+
if session is None:
|
|
129
|
+
# Never signal a recorded pid that no longer answers: the system may have reused it
|
|
130
|
+
clear_session()
|
|
131
|
+
_print("not running")
|
|
132
|
+
return 0
|
|
133
|
+
stop_vease(session.pid)
|
|
134
|
+
clear_session()
|
|
135
|
+
_print("stopped", session)
|
|
136
|
+
return 0
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
140
|
+
"""Command line entry point: start, status or stop."""
|
|
141
|
+
parser = argparse.ArgumentParser(
|
|
142
|
+
prog="python -m vease",
|
|
143
|
+
description=f"Install Vease; manage the Vease scripts connect to ({session_path()}).",
|
|
144
|
+
)
|
|
145
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
146
|
+
start_parser = commands.add_parser(
|
|
147
|
+
"start", help="start Vease, or report the running one"
|
|
148
|
+
)
|
|
149
|
+
start_parser.add_argument(
|
|
150
|
+
"--folder", help="folder of the Vease executable (default: found on PATH)"
|
|
151
|
+
)
|
|
152
|
+
start_parser.add_argument(
|
|
153
|
+
"--install",
|
|
154
|
+
action="store_true",
|
|
155
|
+
help="install the Vease release this package drives first, if needed",
|
|
156
|
+
)
|
|
157
|
+
start_parser.add_argument(
|
|
158
|
+
"--skip-version-check",
|
|
159
|
+
action="store_true",
|
|
160
|
+
help="start a Vease other than the release this package drives",
|
|
161
|
+
)
|
|
162
|
+
start_parser.add_argument(
|
|
163
|
+
"--start-timeout", type=float, default=DEFAULT_START_TIMEOUT, help="seconds"
|
|
164
|
+
)
|
|
165
|
+
start_parser.add_argument(
|
|
166
|
+
"--ready-timeout", type=float, default=DEFAULT_READY_TIMEOUT, help="seconds"
|
|
167
|
+
)
|
|
168
|
+
commands.add_parser("status", help="report the running Vease; exit code 1 if none")
|
|
169
|
+
commands.add_parser("stop", help="close the running Vease")
|
|
170
|
+
install_parser = commands.add_parser(
|
|
171
|
+
"install", help="download a Vease release from GitHub into ~/.vease/app"
|
|
172
|
+
)
|
|
173
|
+
release = install_parser.add_mutually_exclusive_group()
|
|
174
|
+
release.add_argument(
|
|
175
|
+
"--version",
|
|
176
|
+
help="release to install, e.g. 3.2.0 (default: the one this package drives)",
|
|
177
|
+
)
|
|
178
|
+
release.add_argument(
|
|
179
|
+
"--latest", action="store_true", help="install the latest release"
|
|
180
|
+
)
|
|
181
|
+
install_parser.add_argument(
|
|
182
|
+
"--force", action="store_true", help="reinstall even if already installed"
|
|
183
|
+
)
|
|
184
|
+
install_parser.add_argument(
|
|
185
|
+
"--keep-others",
|
|
186
|
+
action="store_true",
|
|
187
|
+
help="keep the other installed versions (removed by default)",
|
|
188
|
+
)
|
|
189
|
+
args = parser.parse_args(argv)
|
|
190
|
+
logging.basicConfig(level=logging.WARNING)
|
|
191
|
+
try:
|
|
192
|
+
if args.command == "install":
|
|
193
|
+
return install(args.version, args.force, args.latest, args.keep_others)
|
|
194
|
+
if args.command == "start":
|
|
195
|
+
return start(
|
|
196
|
+
args.folder,
|
|
197
|
+
args.start_timeout,
|
|
198
|
+
args.ready_timeout,
|
|
199
|
+
args.install,
|
|
200
|
+
not args.skip_version_check,
|
|
201
|
+
)
|
|
202
|
+
if args.command == "status":
|
|
203
|
+
return status()
|
|
204
|
+
return stop()
|
|
205
|
+
except (OSError, RuntimeError, TimeoutError, ValueError) as error:
|
|
206
|
+
print(f"error: {error}", file=sys.stderr)
|
|
207
|
+
return 2
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
if __name__ == "__main__":
|
|
211
|
+
sys.exit(main())
|
vease/client.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""HTTP client of a running Vease, and the error it raises."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import time
|
|
7
|
+
from collections.abc import Callable
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
import requests
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
READY_ROUTE = "/api/microservice/app/get_is_app_ready"
|
|
15
|
+
CONTROLLER_ROUTE = "/api/controller"
|
|
16
|
+
MCP_ROUTE = "/mcp"
|
|
17
|
+
# Loading a large file waits on the back: the server itself gives up after 2 minutes
|
|
18
|
+
DEFAULT_REQUEST_TIMEOUT = 300.0
|
|
19
|
+
DEFAULT_READY_TIMEOUT = 120.0
|
|
20
|
+
READY_POLL_INTERVAL = 1.0
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class VeaseError(RuntimeError):
|
|
24
|
+
"""A request Vease refused or could not run; message is the reason Vease gave."""
|
|
25
|
+
|
|
26
|
+
def __init__(self, status_code: int | None, message: str, data: Any = None):
|
|
27
|
+
super().__init__(
|
|
28
|
+
f"{status_code}: {message}" if status_code is not None else message
|
|
29
|
+
)
|
|
30
|
+
self.status_code = status_code
|
|
31
|
+
self.message = message
|
|
32
|
+
self.data = data
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def error_message(payload: Any) -> str | None:
|
|
36
|
+
"""Reason of a Vease error body, whichever server layer produced it."""
|
|
37
|
+
if not isinstance(payload, dict):
|
|
38
|
+
return None
|
|
39
|
+
# Typed routes send {code, name, description}; h3 errors {statusMessage, message, data}
|
|
40
|
+
for key in ("description", "message", "statusMessage"):
|
|
41
|
+
value = payload.get(key)
|
|
42
|
+
if isinstance(value, str) and value:
|
|
43
|
+
return value
|
|
44
|
+
return error_message(payload.get("data"))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class VeaseClient:
|
|
48
|
+
"""HTTP client of a running Vease app; app_url is its root, e.g. http://localhost:3000."""
|
|
49
|
+
|
|
50
|
+
def __init__(
|
|
51
|
+
self, app_url: str, request_timeout: float | None = DEFAULT_REQUEST_TIMEOUT
|
|
52
|
+
):
|
|
53
|
+
self.app_url = app_url.rstrip("/")
|
|
54
|
+
self.base_url = self.app_url + CONTROLLER_ROUTE
|
|
55
|
+
self.request_timeout = request_timeout
|
|
56
|
+
self.session = requests.Session()
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def mcp_url(self) -> str:
|
|
60
|
+
"""MCP endpoint of this Vease, for agents that drive it through tools."""
|
|
61
|
+
return self.app_url + MCP_ROUTE
|
|
62
|
+
|
|
63
|
+
def is_ready(self) -> bool:
|
|
64
|
+
"""Whether Vease is open and can run commands; False while it cannot be reached."""
|
|
65
|
+
try:
|
|
66
|
+
response = self.session.get(
|
|
67
|
+
self.app_url + READY_ROUTE, timeout=READY_POLL_INTERVAL * 5
|
|
68
|
+
)
|
|
69
|
+
status = response.json() if response.ok else None
|
|
70
|
+
except (requests.RequestException, ValueError):
|
|
71
|
+
return False
|
|
72
|
+
return isinstance(status, dict) and bool(status.get("isReady"))
|
|
73
|
+
|
|
74
|
+
def wait_until_ready(
|
|
75
|
+
self,
|
|
76
|
+
timeout: float = DEFAULT_READY_TIMEOUT,
|
|
77
|
+
is_alive: Callable[[], bool] | None = None,
|
|
78
|
+
) -> None:
|
|
79
|
+
"""Block until Vease can run commands.
|
|
80
|
+
|
|
81
|
+
Raises TimeoutError after timeout seconds, or VeaseError as soon as is_alive (the
|
|
82
|
+
launched process check) returns False.
|
|
83
|
+
"""
|
|
84
|
+
deadline = time.monotonic() + timeout
|
|
85
|
+
while not self.is_ready():
|
|
86
|
+
if is_alive is not None and not is_alive():
|
|
87
|
+
raise VeaseError(None, "Vease exited before it was ready")
|
|
88
|
+
if time.monotonic() >= deadline:
|
|
89
|
+
raise TimeoutError(
|
|
90
|
+
f"Vease at {self.app_url} was not ready after {timeout:g}s"
|
|
91
|
+
)
|
|
92
|
+
logger.info("Vease not ready yet, retrying...")
|
|
93
|
+
time.sleep(READY_POLL_INTERVAL)
|
|
94
|
+
logger.info("Vease is ready")
|
|
95
|
+
|
|
96
|
+
def close(self) -> None:
|
|
97
|
+
"""Close the HTTP session; Vease itself keeps running."""
|
|
98
|
+
self.session.close()
|
|
99
|
+
|
|
100
|
+
def _url(self, path: str) -> str:
|
|
101
|
+
return f"{self.base_url}/{path.lstrip('/')}"
|
|
102
|
+
|
|
103
|
+
def _request(self, method: str, path: str, **kwargs: Any) -> Any:
|
|
104
|
+
url = self._url(path)
|
|
105
|
+
try:
|
|
106
|
+
response = self.session.request(
|
|
107
|
+
method, url, timeout=self.request_timeout, **kwargs
|
|
108
|
+
)
|
|
109
|
+
except requests.RequestException as error:
|
|
110
|
+
raise VeaseError(
|
|
111
|
+
None, f"Could not reach Vease at {url}: {error}"
|
|
112
|
+
) from error
|
|
113
|
+
logger.debug("Request: %s %s => %s", method, url, response.status_code)
|
|
114
|
+
try:
|
|
115
|
+
data = response.json()
|
|
116
|
+
except ValueError:
|
|
117
|
+
data = response.text
|
|
118
|
+
|
|
119
|
+
if not response.ok:
|
|
120
|
+
message = error_message(data) or (data if isinstance(data, str) else "")
|
|
121
|
+
raise VeaseError(response.status_code, message or "Vease API error", data)
|
|
122
|
+
|
|
123
|
+
# Every controller schema declares this {statusCode, response} wrapper
|
|
124
|
+
if isinstance(data, dict) and "response" in data and "statusCode" in data:
|
|
125
|
+
return data["response"]
|
|
126
|
+
return data
|
|
127
|
+
|
|
128
|
+
def get(self, path: str, params: dict[str, Any] | None = None) -> Any:
|
|
129
|
+
"""GET a controller route (path relative to /api/controller); returns its response."""
|
|
130
|
+
return self._request("GET", path, params=params)
|
|
131
|
+
|
|
132
|
+
def post(
|
|
133
|
+
self,
|
|
134
|
+
path: str,
|
|
135
|
+
json: dict[str, Any] | None = None,
|
|
136
|
+
files: dict[str, Any] | None = None,
|
|
137
|
+
) -> Any:
|
|
138
|
+
"""POST to a controller route with a JSON body or files; returns its response."""
|
|
139
|
+
return self._request("POST", path, json=json, files=files)
|