Print3D 0.1.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.
- print3d/__init__.py +12 -0
- print3d/assets.py +487 -0
- print3d/bambu.py +373 -0
- print3d/blender.py +314 -0
- print3d/blender_policy.py +72 -0
- print3d/cad.py +302 -0
- print3d/cad_worker.py +263 -0
- print3d/capabilities.py +338 -0
- print3d/cli.py +466 -0
- print3d/cloud.py +642 -0
- print3d/config.py +511 -0
- print3d/drawing.py +239 -0
- print3d/drawing_worker.py +347 -0
- print3d/errors.py +157 -0
- print3d/images.py +369 -0
- print3d/instructions.py +64 -0
- print3d/jobs.py +368 -0
- print3d/local_mesh.py +271 -0
- print3d/mesh.py +92 -0
- print3d/mesh_fit.py +194 -0
- print3d/preview.py +251 -0
- print3d/printability.py +313 -0
- print3d/printers.py +1106 -0
- print3d/processes.py +494 -0
- print3d/registry.py +102 -0
- print3d/runner.py +74 -0
- print3d/runtime.py +78 -0
- print3d/script_policy.py +217 -0
- print3d/server.py +165 -0
- print3d/slicers.py +1435 -0
- print3d/tools/__init__.py +33 -0
- print3d/tools/analysis.py +144 -0
- print3d/tools/catalog.py +224 -0
- print3d/tools/common.py +333 -0
- print3d/tools/generating.py +832 -0
- print3d/tools/images.py +189 -0
- print3d/tools/models.py +584 -0
- print3d/tools/printers.py +543 -0
- print3d/tools/slicing.py +508 -0
- print3d/tools/system.py +34 -0
- print3d/viewer.py +325 -0
- print3d/viewer_template.html +607 -0
- print3d/winacl.py +179 -0
- print3d/workspace.py +151 -0
- print3d-0.1.0.dist-info/METADATA +181 -0
- print3d-0.1.0.dist-info/RECORD +49 -0
- print3d-0.1.0.dist-info/WHEEL +4 -0
- print3d-0.1.0.dist-info/entry_points.txt +2 -0
- print3d-0.1.0.dist-info/licenses/LICENSE +21 -0
print3d/__init__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Print3D — a self-hosted 3D printing MCP server.
|
|
2
|
+
|
|
3
|
+
The package is organised around a small core (config, workspace, jobs, registry)
|
|
4
|
+
and a set of pluggable adapters (providers, CAD, render, slicing, printers).
|
|
5
|
+
Core code only depends on the adapter protocols, never on a vendor module.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
__all__ = ["__version__"]
|
|
11
|
+
|
|
12
|
+
__version__ = "0.1.0"
|
print3d/assets.py
ADDED
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
"""Asset store: versioned model scripts and their provenance.
|
|
2
|
+
|
|
3
|
+
Layout (inside the workspace)::
|
|
4
|
+
|
|
5
|
+
assets/<asset_id>/asset.json # manifest, one per asset
|
|
6
|
+
assets/<asset_id>/v001/model.py # the CAD script
|
|
7
|
+
assets/<asset_id>/v001/params.json # prompt, model, checks, usage
|
|
8
|
+
|
|
9
|
+
Revisions are append-only: ``v001``, ``v002`` … so a prompt tweak never
|
|
10
|
+
destroys the version that was printed.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import re
|
|
17
|
+
import secrets
|
|
18
|
+
import shutil
|
|
19
|
+
from datetime import UTC, datetime
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
from .errors import InvalidArgument, NotFound
|
|
24
|
+
from .jobs import sha256_file
|
|
25
|
+
from .workspace import Workspace, atomic_write_text
|
|
26
|
+
|
|
27
|
+
ASSET_ID_RE = re.compile(r"^a_[A-Za-z0-9_]{6,64}$")
|
|
28
|
+
NAME_RE = re.compile(r"^[a-z0-9][a-z0-9_-]{1,40}$")
|
|
29
|
+
MANIFEST = "asset.json"
|
|
30
|
+
SCRIPT = "model.py"
|
|
31
|
+
PARAMS = "params.json"
|
|
32
|
+
TRACE = "agent_trace.json"
|
|
33
|
+
INPUT_STEP = "input.step"
|
|
34
|
+
STEP_SUFFIXES = frozenset({".step", ".stp"})
|
|
35
|
+
MAX_INPUT_BYTES = 64 * 1024 * 1024
|
|
36
|
+
OUTPUT_KEY_RE = re.compile(r"^[a-z][a-z0-9_]{1,24}$")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def new_asset_id(now: datetime | None = None) -> str:
|
|
40
|
+
moment = (now or datetime.now(UTC)).strftime("%Y%m%dT%H%M%SZ")
|
|
41
|
+
return f"a_{moment}_{secrets.token_hex(3)}"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def validate_asset_id(asset_id: str) -> str:
|
|
45
|
+
if not isinstance(asset_id, str) or not ASSET_ID_RE.match(asset_id):
|
|
46
|
+
raise InvalidArgument(
|
|
47
|
+
f"Invalid asset id: {asset_id!r}",
|
|
48
|
+
hint="Asset ids look like a_20260101T000000Z_ab12cd.",
|
|
49
|
+
)
|
|
50
|
+
return asset_id
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def validate_name(name: str) -> str:
|
|
54
|
+
if not isinstance(name, str) or not NAME_RE.match(name):
|
|
55
|
+
raise InvalidArgument(
|
|
56
|
+
f"Invalid asset name: {name!r}",
|
|
57
|
+
hint="Names are 2-41 chars of lowercase letters, digits, '-' or '_'.",
|
|
58
|
+
)
|
|
59
|
+
return name
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _iso() -> str:
|
|
63
|
+
return datetime.now(UTC).isoformat(timespec="seconds")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _iso_from_mtime(path: Path) -> str:
|
|
67
|
+
return datetime.fromtimestamp(path.stat().st_mtime, UTC).isoformat(timespec="seconds")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class AssetStore:
|
|
71
|
+
def __init__(self, workspace: Workspace) -> None:
|
|
72
|
+
self.workspace = workspace
|
|
73
|
+
|
|
74
|
+
# -- paths -----------------------------------------------------------
|
|
75
|
+
def asset_dir(self, asset_id: str) -> Path:
|
|
76
|
+
return self.workspace.asset_dir(validate_asset_id(asset_id))
|
|
77
|
+
|
|
78
|
+
def manifest_path(self, asset_id: str) -> Path:
|
|
79
|
+
return self.asset_dir(asset_id) / MANIFEST
|
|
80
|
+
|
|
81
|
+
def revision_dir(self, asset_id: str, revision: str) -> Path:
|
|
82
|
+
if not re.match(r"^v\d{3,}$", revision):
|
|
83
|
+
raise InvalidArgument(
|
|
84
|
+
f"Invalid revision: {revision!r}", hint="Revisions look like v001."
|
|
85
|
+
)
|
|
86
|
+
return self.asset_dir(asset_id) / revision
|
|
87
|
+
|
|
88
|
+
# -- lifecycle -------------------------------------------------------
|
|
89
|
+
def create(self, name: str, *, kind: str = "build123d") -> dict[str, Any]:
|
|
90
|
+
name = validate_name(name)
|
|
91
|
+
asset_id = new_asset_id()
|
|
92
|
+
directory = self.asset_dir(asset_id)
|
|
93
|
+
directory.mkdir(parents=True, exist_ok=False)
|
|
94
|
+
manifest = {
|
|
95
|
+
"asset_id": asset_id,
|
|
96
|
+
"name": name,
|
|
97
|
+
"kind": kind,
|
|
98
|
+
"created_at": _iso(),
|
|
99
|
+
"updated_at": _iso(),
|
|
100
|
+
"revisions": [],
|
|
101
|
+
}
|
|
102
|
+
self._write_manifest(asset_id, manifest)
|
|
103
|
+
return manifest
|
|
104
|
+
|
|
105
|
+
def load(self, asset_id: str) -> dict[str, Any]:
|
|
106
|
+
path = self.manifest_path(asset_id)
|
|
107
|
+
if not path.is_file():
|
|
108
|
+
raise NotFound(
|
|
109
|
+
f"Unknown asset: {asset_id}",
|
|
110
|
+
hint="Run `print3d asset list` to see available assets.",
|
|
111
|
+
)
|
|
112
|
+
return json.loads(path.read_text(encoding="utf-8"))
|
|
113
|
+
|
|
114
|
+
def next_revision(self, manifest: dict[str, Any]) -> str:
|
|
115
|
+
count = len(manifest.get("revisions", []))
|
|
116
|
+
return f"v{count + 1:03d}"
|
|
117
|
+
|
|
118
|
+
def latest_revision(self, manifest: dict[str, Any]) -> str | None:
|
|
119
|
+
revisions = manifest.get("revisions") or []
|
|
120
|
+
return revisions[-1]["revision"] if revisions else None
|
|
121
|
+
|
|
122
|
+
def read_script(self, asset_id: str, revision: str) -> tuple[Path, str]:
|
|
123
|
+
path = self.revision_dir(asset_id, revision) / SCRIPT
|
|
124
|
+
if not path.is_file():
|
|
125
|
+
raise NotFound(
|
|
126
|
+
f"Revision {revision} of {asset_id} has no script",
|
|
127
|
+
hint="Use `model_write` to add a revision, or check the revision id.",
|
|
128
|
+
)
|
|
129
|
+
return path, path.read_text(encoding="utf-8")
|
|
130
|
+
|
|
131
|
+
def record_build(
|
|
132
|
+
self,
|
|
133
|
+
asset_id: str,
|
|
134
|
+
revision: str,
|
|
135
|
+
*,
|
|
136
|
+
build: dict[str, Any],
|
|
137
|
+
artifacts: dict[str, Path] | None = None,
|
|
138
|
+
) -> dict[str, Any]:
|
|
139
|
+
"""Attach a build result (and its files) to an existing revision."""
|
|
140
|
+
|
|
141
|
+
manifest = self.load(asset_id)
|
|
142
|
+
directory = self.revision_dir(asset_id, revision)
|
|
143
|
+
if not directory.is_dir():
|
|
144
|
+
raise NotFound(
|
|
145
|
+
f"Unknown revision: {revision}",
|
|
146
|
+
hint="Revisions are listed in the asset manifest.",
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
stored: dict[str, str] = {}
|
|
150
|
+
for stored_name, source in (artifacts or {}).items():
|
|
151
|
+
source_path = Path(source)
|
|
152
|
+
if not source_path.is_file():
|
|
153
|
+
continue
|
|
154
|
+
target = directory / Path(stored_name).name
|
|
155
|
+
if source_path.resolve() != target.resolve():
|
|
156
|
+
shutil.copyfile(source_path, target)
|
|
157
|
+
target.chmod(0o644)
|
|
158
|
+
stored[Path(stored_name).name] = sha256_file(target)
|
|
159
|
+
|
|
160
|
+
params_path = directory / PARAMS
|
|
161
|
+
try:
|
|
162
|
+
params = json.loads(params_path.read_text(encoding="utf-8"))
|
|
163
|
+
except (OSError, json.JSONDecodeError):
|
|
164
|
+
params = {}
|
|
165
|
+
params["build"] = build
|
|
166
|
+
params["artifacts"] = {**params.get("artifacts", {}), **stored}
|
|
167
|
+
params["built_at"] = _iso()
|
|
168
|
+
atomic_write_text(
|
|
169
|
+
params_path,
|
|
170
|
+
json.dumps(params, indent=2, ensure_ascii=False) + "\n",
|
|
171
|
+
mode=0o644,
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
record: dict[str, Any] | None = None
|
|
175
|
+
for item in manifest.get("revisions", []):
|
|
176
|
+
if item.get("revision") == revision:
|
|
177
|
+
item["build"] = build
|
|
178
|
+
item["artifacts"] = {**item.get("artifacts", {}), **stored}
|
|
179
|
+
item["built_at"] = params["built_at"]
|
|
180
|
+
record = item
|
|
181
|
+
break
|
|
182
|
+
if record is None:
|
|
183
|
+
raise NotFound(
|
|
184
|
+
f"Unknown revision: {revision}",
|
|
185
|
+
hint="Revisions are listed in the asset manifest.",
|
|
186
|
+
)
|
|
187
|
+
manifest["updated_at"] = params["built_at"]
|
|
188
|
+
self._write_manifest(asset_id, manifest)
|
|
189
|
+
return record
|
|
190
|
+
|
|
191
|
+
def store_input(
|
|
192
|
+
self,
|
|
193
|
+
asset_id: str,
|
|
194
|
+
revision: str,
|
|
195
|
+
source: str | Path,
|
|
196
|
+
) -> dict[str, Any]:
|
|
197
|
+
"""Copy a STEP file into the revision so later builds stay reproducible."""
|
|
198
|
+
|
|
199
|
+
directory = self.revision_dir(asset_id, revision)
|
|
200
|
+
if not directory.is_dir():
|
|
201
|
+
raise NotFound(
|
|
202
|
+
f"Unknown revision: {revision}",
|
|
203
|
+
hint="Revisions are listed in the asset manifest.",
|
|
204
|
+
)
|
|
205
|
+
path = Path(source).expanduser()
|
|
206
|
+
if not path.is_absolute():
|
|
207
|
+
path = Path.cwd() / path
|
|
208
|
+
path = path.resolve()
|
|
209
|
+
if not path.is_file():
|
|
210
|
+
raise NotFound(
|
|
211
|
+
f"input file not found: {source}",
|
|
212
|
+
hint="Paths are resolved against the server's working directory.",
|
|
213
|
+
)
|
|
214
|
+
if path.suffix.lower() not in STEP_SUFFIXES:
|
|
215
|
+
raise InvalidArgument(
|
|
216
|
+
"input must be a .step or .stp file",
|
|
217
|
+
details={"suffix": path.suffix, "accepted": sorted(STEP_SUFFIXES)},
|
|
218
|
+
)
|
|
219
|
+
size = path.stat().st_size
|
|
220
|
+
if size <= 0:
|
|
221
|
+
raise InvalidArgument("input file is empty", details={"path": str(path)})
|
|
222
|
+
if size > MAX_INPUT_BYTES:
|
|
223
|
+
raise InvalidArgument(
|
|
224
|
+
f"input file is larger than {MAX_INPUT_BYTES // (1024 * 1024)} MiB",
|
|
225
|
+
details={"bytes": size, "limit_bytes": MAX_INPUT_BYTES},
|
|
226
|
+
)
|
|
227
|
+
with path.open("rb") as handle:
|
|
228
|
+
head = handle.read(4096)
|
|
229
|
+
if b"ISO-10303-21" not in head:
|
|
230
|
+
raise InvalidArgument(
|
|
231
|
+
"input does not look like a STEP file",
|
|
232
|
+
hint="STEP files start with an ISO-10303-21 header; export STEP AP214/AP242.",
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
target = directory / INPUT_STEP
|
|
236
|
+
if path != target.resolve():
|
|
237
|
+
shutil.copyfile(path, target)
|
|
238
|
+
target.chmod(0o644)
|
|
239
|
+
return {
|
|
240
|
+
"file": INPUT_STEP,
|
|
241
|
+
"original_name": path.name,
|
|
242
|
+
"bytes": target.stat().st_size,
|
|
243
|
+
"sha256": sha256_file(target),
|
|
244
|
+
"stored_at": _iso(),
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
def stored_input(self, asset_id: str, revision: str) -> dict[str, Any] | None:
|
|
248
|
+
"""Describe the STEP file already stored on this revision, if any."""
|
|
249
|
+
|
|
250
|
+
target = self.revision_dir(asset_id, revision) / INPUT_STEP
|
|
251
|
+
if not target.is_file():
|
|
252
|
+
return None
|
|
253
|
+
meta: dict[str, Any] = {
|
|
254
|
+
"file": INPUT_STEP,
|
|
255
|
+
"original_name": INPUT_STEP,
|
|
256
|
+
"bytes": target.stat().st_size,
|
|
257
|
+
"sha256": sha256_file(target),
|
|
258
|
+
"stored_at": _iso_from_mtime(target),
|
|
259
|
+
}
|
|
260
|
+
for item in self.load(asset_id).get("revisions", []):
|
|
261
|
+
if item.get("revision") != revision:
|
|
262
|
+
continue
|
|
263
|
+
recorded = (item.get("build") or {}).get("input") or {}
|
|
264
|
+
meta.update({key: value for key, value in recorded.items() if value})
|
|
265
|
+
break
|
|
266
|
+
return meta
|
|
267
|
+
|
|
268
|
+
def record_output(
|
|
269
|
+
self,
|
|
270
|
+
asset_id: str,
|
|
271
|
+
revision: str,
|
|
272
|
+
*,
|
|
273
|
+
key: str,
|
|
274
|
+
meta: dict[str, Any] | None = None,
|
|
275
|
+
files: dict[str, Path] | None = None,
|
|
276
|
+
) -> dict[str, Any]:
|
|
277
|
+
"""Attach derived files (a drawing, a render …) to a revision under ``key``."""
|
|
278
|
+
|
|
279
|
+
if not OUTPUT_KEY_RE.fullmatch(key):
|
|
280
|
+
raise InvalidArgument(
|
|
281
|
+
"output key must be a short lower-case identifier",
|
|
282
|
+
details={"key": key},
|
|
283
|
+
)
|
|
284
|
+
manifest = self.load(asset_id)
|
|
285
|
+
directory = self.revision_dir(asset_id, revision)
|
|
286
|
+
if not directory.is_dir():
|
|
287
|
+
raise NotFound(
|
|
288
|
+
f"Unknown revision: {revision}",
|
|
289
|
+
hint="Revisions are listed in the asset manifest.",
|
|
290
|
+
)
|
|
291
|
+
stored: dict[str, str] = {}
|
|
292
|
+
for stored_name, source in (files or {}).items():
|
|
293
|
+
source_path = Path(source)
|
|
294
|
+
if not source_path.is_file():
|
|
295
|
+
continue
|
|
296
|
+
target = directory / Path(stored_name).name
|
|
297
|
+
if source_path.resolve() != target.resolve():
|
|
298
|
+
shutil.copyfile(source_path, target)
|
|
299
|
+
target.chmod(0o644)
|
|
300
|
+
stored[Path(stored_name).name] = sha256_file(target)
|
|
301
|
+
|
|
302
|
+
payload = {**(meta or {}), "files": stored, "created_at": _iso()}
|
|
303
|
+
params_path = directory / PARAMS
|
|
304
|
+
try:
|
|
305
|
+
params = json.loads(params_path.read_text(encoding="utf-8"))
|
|
306
|
+
except (OSError, json.JSONDecodeError):
|
|
307
|
+
params = {}
|
|
308
|
+
params[key] = payload
|
|
309
|
+
atomic_write_text(
|
|
310
|
+
params_path,
|
|
311
|
+
json.dumps(params, indent=2, ensure_ascii=False) + "\n",
|
|
312
|
+
mode=0o644,
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
record: dict[str, Any] | None = None
|
|
316
|
+
for item in manifest.get("revisions", []):
|
|
317
|
+
if item.get("revision") == revision:
|
|
318
|
+
item[key] = payload
|
|
319
|
+
record = item
|
|
320
|
+
break
|
|
321
|
+
if record is None:
|
|
322
|
+
raise NotFound(
|
|
323
|
+
f"Unknown revision: {revision}",
|
|
324
|
+
hint="Revisions are listed in the asset manifest.",
|
|
325
|
+
)
|
|
326
|
+
manifest["updated_at"] = payload["created_at"]
|
|
327
|
+
self._write_manifest(asset_id, manifest)
|
|
328
|
+
return record
|
|
329
|
+
|
|
330
|
+
def add_revision(
|
|
331
|
+
self,
|
|
332
|
+
asset_id: str,
|
|
333
|
+
script: str,
|
|
334
|
+
*,
|
|
335
|
+
prompt: str,
|
|
336
|
+
model: str,
|
|
337
|
+
modeler: str,
|
|
338
|
+
usage: dict[str, Any] | None = None,
|
|
339
|
+
checks: dict[str, Any] | None = None,
|
|
340
|
+
build: dict[str, Any] | None = None,
|
|
341
|
+
agent: dict[str, Any] | None = None,
|
|
342
|
+
artifacts: dict[str, Path] | None = None,
|
|
343
|
+
reference: Path | None = None,
|
|
344
|
+
reference_meta: dict[str, Any] | None = None,
|
|
345
|
+
kind: str = "build123d",
|
|
346
|
+
) -> dict[str, Any]:
|
|
347
|
+
"""Write the script, provenance and build artifacts; return the revision.
|
|
348
|
+
|
|
349
|
+
``artifacts`` maps a stored file name (e.g. ``model.stl``) to a path in a
|
|
350
|
+
scratch directory; each file is copied in so the revision is
|
|
351
|
+
self-contained. ``agent`` carries the tool-call trace of the modelling
|
|
352
|
+
loop and is written as ``agent_trace.json``. ``reference`` is a concept
|
|
353
|
+
image (``text -> image -> model`` path): it is copied next to the script
|
|
354
|
+
as ``reference.<ext>`` and its provenance is kept in ``params.json``.
|
|
355
|
+
"""
|
|
356
|
+
|
|
357
|
+
manifest = self.load(asset_id)
|
|
358
|
+
revision = self.next_revision(manifest)
|
|
359
|
+
directory = self.revision_dir(asset_id, revision)
|
|
360
|
+
directory.mkdir(parents=True, exist_ok=False)
|
|
361
|
+
|
|
362
|
+
script_path = directory / SCRIPT
|
|
363
|
+
atomic_write_text(script_path, script, mode=0o644)
|
|
364
|
+
digest = sha256_file(script_path)
|
|
365
|
+
|
|
366
|
+
stored: dict[str, str] = {}
|
|
367
|
+
for stored_name, source in (artifacts or {}).items():
|
|
368
|
+
source_path = Path(source)
|
|
369
|
+
if not source_path.is_file():
|
|
370
|
+
continue
|
|
371
|
+
target = directory / Path(stored_name).name
|
|
372
|
+
shutil.copyfile(source_path, target)
|
|
373
|
+
target.chmod(0o644)
|
|
374
|
+
stored[Path(stored_name).name] = sha256_file(target)
|
|
375
|
+
|
|
376
|
+
reference_record: dict[str, Any] | None = None
|
|
377
|
+
if reference is not None:
|
|
378
|
+
source_path = Path(reference)
|
|
379
|
+
if not source_path.is_file():
|
|
380
|
+
raise NotFound(
|
|
381
|
+
f"Reference image not found: {source_path}",
|
|
382
|
+
hint="Call image_generate first, then pass the returned image_path.",
|
|
383
|
+
)
|
|
384
|
+
target = directory / f"reference{source_path.suffix.lower() or '.png'}"
|
|
385
|
+
shutil.copyfile(source_path, target)
|
|
386
|
+
target.chmod(0o644)
|
|
387
|
+
reference_record = {
|
|
388
|
+
"file": target.name,
|
|
389
|
+
"sha256": sha256_file(target),
|
|
390
|
+
"meta": reference_meta or {},
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if agent and agent.get("trace"):
|
|
394
|
+
atomic_write_text(
|
|
395
|
+
directory / TRACE,
|
|
396
|
+
json.dumps(agent, indent=2, ensure_ascii=False) + "\n",
|
|
397
|
+
mode=0o644,
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
params = {
|
|
401
|
+
"asset_id": asset_id,
|
|
402
|
+
"revision": revision,
|
|
403
|
+
"prompt": prompt,
|
|
404
|
+
"model": model,
|
|
405
|
+
"modeler": modeler,
|
|
406
|
+
"kind": kind,
|
|
407
|
+
"created_at": _iso(),
|
|
408
|
+
"script_sha256": digest,
|
|
409
|
+
"usage": usage or {},
|
|
410
|
+
"checks": checks or {},
|
|
411
|
+
"build": build or {},
|
|
412
|
+
"agent": {k: v for k, v in (agent or {}).items() if k != "trace"},
|
|
413
|
+
"artifacts": stored,
|
|
414
|
+
"reference": reference_record or {},
|
|
415
|
+
}
|
|
416
|
+
atomic_write_text(
|
|
417
|
+
directory / PARAMS,
|
|
418
|
+
json.dumps(params, indent=2, ensure_ascii=False) + "\n",
|
|
419
|
+
mode=0o644,
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
record = {
|
|
423
|
+
"revision": revision,
|
|
424
|
+
"created_at": params["created_at"],
|
|
425
|
+
"model": model,
|
|
426
|
+
"modeler": modeler,
|
|
427
|
+
"kind": kind,
|
|
428
|
+
"script": f"{revision}/{SCRIPT}",
|
|
429
|
+
"sha256": digest,
|
|
430
|
+
"prompt": prompt,
|
|
431
|
+
"usage": usage or {},
|
|
432
|
+
"checks": checks or {},
|
|
433
|
+
"build": build or {},
|
|
434
|
+
"artifacts": stored,
|
|
435
|
+
"reference": reference_record or {},
|
|
436
|
+
}
|
|
437
|
+
manifest.setdefault("revisions", []).append(record)
|
|
438
|
+
manifest["updated_at"] = record["created_at"]
|
|
439
|
+
self._write_manifest(asset_id, manifest)
|
|
440
|
+
return record
|
|
441
|
+
|
|
442
|
+
def list(self, *, limit: int = 50) -> list[dict[str, Any]]:
|
|
443
|
+
items: list[dict[str, Any]] = []
|
|
444
|
+
if not self.workspace.assets_dir.is_dir():
|
|
445
|
+
return items
|
|
446
|
+
for path in sorted(self.workspace.assets_dir.iterdir(), reverse=True):
|
|
447
|
+
if not path.is_dir() or not (path / MANIFEST).is_file():
|
|
448
|
+
continue
|
|
449
|
+
try:
|
|
450
|
+
manifest = json.loads((path / MANIFEST).read_text(encoding="utf-8"))
|
|
451
|
+
except (OSError, json.JSONDecodeError):
|
|
452
|
+
continue
|
|
453
|
+
revisions = manifest.get("revisions", [])
|
|
454
|
+
items.append(
|
|
455
|
+
{
|
|
456
|
+
"asset_id": manifest.get("asset_id", path.name),
|
|
457
|
+
"name": manifest.get("name"),
|
|
458
|
+
"kind": manifest.get("kind", "build123d"),
|
|
459
|
+
"revisions": len(revisions),
|
|
460
|
+
"latest_revision": revisions[-1].get("revision") if revisions else None,
|
|
461
|
+
"updated_at": manifest.get("updated_at"),
|
|
462
|
+
}
|
|
463
|
+
)
|
|
464
|
+
if len(items) >= limit:
|
|
465
|
+
break
|
|
466
|
+
return items
|
|
467
|
+
|
|
468
|
+
def _write_manifest(self, asset_id: str, manifest: dict[str, Any]) -> None:
|
|
469
|
+
atomic_write_text(
|
|
470
|
+
self.manifest_path(asset_id),
|
|
471
|
+
json.dumps(manifest, indent=2, ensure_ascii=False) + "\n",
|
|
472
|
+
mode=0o644,
|
|
473
|
+
)
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
__all__ = [
|
|
477
|
+
"ASSET_ID_RE",
|
|
478
|
+
"INPUT_STEP",
|
|
479
|
+
"MAX_INPUT_BYTES",
|
|
480
|
+
"NAME_RE",
|
|
481
|
+
"OUTPUT_KEY_RE",
|
|
482
|
+
"STEP_SUFFIXES",
|
|
483
|
+
"AssetStore",
|
|
484
|
+
"new_asset_id",
|
|
485
|
+
"validate_asset_id",
|
|
486
|
+
"validate_name",
|
|
487
|
+
]
|