@withgateway/sdk 0.4.0 → 0.9.1
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.
- package/dist/agents/index.d.mts +573 -0
- package/dist/agents/index.d.ts +573 -0
- package/dist/agents/index.js +1443 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/index.mjs +1394 -0
- package/dist/agents/index.mjs.map +1 -0
- package/dist/apiError-DHpfbmdx.d.mts +17 -0
- package/dist/apiError-DHpfbmdx.d.ts +17 -0
- package/dist/cli.js +5655 -1890
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +5769 -2005
- package/dist/cli.mjs.map +1 -1
- package/dist/files/index.d.mts +3 -1
- package/dist/files/index.d.ts +3 -1
- package/dist/files/index.js +418 -120
- package/dist/files/index.js.map +1 -1
- package/dist/files/index.mjs +418 -120
- package/dist/files/index.mjs.map +1 -1
- package/dist/hooks/index.js +346 -136
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +346 -136
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/{index-DUHmS74p.d.ts → index-DIN14Lhg.d.ts} +27 -4
- package/dist/{index-mJhG3vZU.d.mts → index-DuB-eQ0Z.d.mts} +27 -4
- package/dist/index.d.mts +7 -4
- package/dist/index.d.ts +7 -4
- package/dist/index.js +2862 -438
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2820 -431
- package/dist/index.mjs.map +1 -1
- package/dist/media-CQUC7TkS.d.mts +39 -0
- package/dist/media-DQnBloaX.d.ts +39 -0
- package/dist/replay/index.d.mts +1 -1
- package/dist/replay/index.d.ts +1 -1
- package/dist/replay/index.js.map +1 -1
- package/dist/replay/index.mjs.map +1 -1
- package/dist/traces/index.d.mts +3 -1
- package/dist/traces/index.d.ts +3 -1
- package/dist/traces/index.js +418 -120
- package/dist/traces/index.js.map +1 -1
- package/dist/traces/index.mjs +418 -120
- package/dist/traces/index.mjs.map +1 -1
- package/dist/tracing/index.d.mts +3 -2
- package/dist/tracing/index.d.ts +3 -2
- package/dist/tracing/index.js +257 -16
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +257 -16
- package/dist/tracing/index.mjs.map +1 -1
- package/dist/transport-CpRe7zgd.d.mts +123 -0
- package/dist/transport-CpRe7zgd.d.ts +123 -0
- package/dist/worlds/index.d.mts +2086 -1749
- package/dist/worlds/index.d.ts +2086 -1749
- package/dist/worlds/index.js +1471 -269
- package/dist/worlds/index.js.map +1 -1
- package/dist/worlds/index.mjs +1466 -265
- package/dist/worlds/index.mjs.map +1 -1
- package/package.json +6 -1
- package/runtime/gatewaysdk/environment/_bundle.py +4 -6
- package/runtime/gatewaysdk/environment/_world.py +24 -1
- package/runtime/gatewaysdk/environment/bundle_manifest.py +243 -0
- package/runtime/gatewaysdk/environment/runtime.py +45 -2
- package/runtime/gatewaysdk/environment/schema/__main__.py +2 -2
- package/runtime/gatewaysdk/environment/schema/_slack_fidelity.py +8 -23
- package/runtime/gatewaysdk/environment/schema/api.py +27 -7
- package/runtime/gatewaysdk/environment/schema/assertions.py +7 -1
- package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +2 -0
- package/runtime/gatewaysdk/environment/schema/call_capture.py +13 -1
- package/runtime/gatewaysdk/environment/schema/call_log.py +66 -0
- package/runtime/gatewaysdk/environment/schema/call_redaction.py +6 -0
- package/runtime/gatewaysdk/environment/schema/compiler.py +4 -1
- package/runtime/gatewaysdk/environment/schema/conform.py +32 -8
- package/runtime/gatewaysdk/environment/schema/connector.py +5 -0
- package/runtime/gatewaysdk/environment/schema/connectors/custom/handlers.py +2 -1
- package/runtime/gatewaysdk/environment/schema/connectors/github/api/handlers.py +217 -58
- package/runtime/gatewaysdk/environment/schema/connectors/github/connector.toml +9 -19
- package/runtime/gatewaysdk/environment/schema/connectors/github/scenarios/default/seed/github_webhooks.json +10 -2
- package/runtime/gatewaysdk/environment/schema/connectors/github/tests/webhooks.json +47 -0
- package/runtime/gatewaysdk/environment/schema/connectors/github/world.json +21 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/api/handlers.py +185 -18
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/connector.toml +29 -14
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/provenance.json +43 -12
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/scenarios/default/seed/jamf_webhooks.json +2 -2
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/scope.toml +4 -17
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/tests/webhooks.json +107 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/vendor-openapi.json +1 -1
- package/runtime/gatewaysdk/environment/schema/connectors/linear/api/handlers.py +85 -1
- package/runtime/gatewaysdk/environment/schema/connectors/linear/connector.toml +19 -31
- package/runtime/gatewaysdk/environment/schema/connectors/linear/provenance.json +18 -8
- package/runtime/gatewaysdk/environment/schema/connectors/linear/scenarios/default/seed/webhooks.json +19 -3
- package/runtime/gatewaysdk/environment/schema/connectors/linear/tests/test_webhooks.py +71 -0
- package/runtime/gatewaysdk/environment/schema/database/base.py +2 -2
- package/runtime/gatewaysdk/environment/schema/database/guard.py +2 -1
- package/runtime/gatewaysdk/environment/schema/handler_errors.py +27 -1
- package/runtime/gatewaysdk/environment/schema/host.py +74 -11
- package/runtime/gatewaysdk/environment/schema/host_browser.py +48 -0
- package/runtime/gatewaysdk/environment/schema/host_control.py +33 -2
- package/runtime/gatewaysdk/environment/schema/host_handoff.py +4 -1
- package/runtime/gatewaysdk/environment/schema/host_requests.py +14 -0
- package/runtime/gatewaysdk/environment/schema/host_session.py +13 -2
- package/runtime/gatewaysdk/environment/schema/host_surfaces.py +6 -0
- package/runtime/gatewaysdk/environment/schema/host_versions.py +96 -19
- package/runtime/gatewaysdk/environment/schema/journal.py +10 -4
- package/runtime/gatewaysdk/environment/schema/local.py +33 -7
- package/runtime/gatewaysdk/environment/schema/openapi_schemas.py +20 -5
- package/runtime/gatewaysdk/environment/schema/page_checks.py +123 -0
- package/runtime/gatewaysdk/environment/schema/platform.py +33 -480
- package/runtime/gatewaysdk/environment/schema/platform_templates.py +332 -0
- package/runtime/gatewaysdk/environment/schema/request_ledger.py +4 -1
- package/runtime/gatewaysdk/environment/schema/run_parameters.py +25 -0
- package/runtime/gatewaysdk/environment/schema/runtime.py +49 -4
- package/runtime/gatewaysdk/environment/schema/scaffold.py +369 -218
- package/runtime/gatewaysdk/environment/schema/session_files.py +13 -2
- package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +1 -1
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/CONNECTORS.md +3 -3
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +20 -24
- package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +11 -11
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +9 -9
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +45 -12
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +40 -9
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/journal-checks.md +17 -2
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/new-world-sandbox.md +1 -1
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/ui-map.md +3 -2
- package/runtime/gatewaysdk/environment/schema/slack.py +55 -10
- package/runtime/gatewaysdk/environment/schema/snapshot.py +30 -4
- package/runtime/gatewaysdk/environment/schema/tasks.py +25 -2
- package/runtime/gatewaysdk/environment/schema/template_record.py +131 -0
- package/runtime/gatewaysdk/environment/schema/tools_world.py +26 -5
- package/runtime/gatewaysdk/environment/schema/ui.py +54 -33
- package/runtime/gatewaysdk/environment/schema/ui_interactions.py +419 -0
- package/runtime/gatewaysdk/environment/schema/ui_recorder.js +895 -0
- package/runtime/gatewaysdk/environment/schema/ui_replay.py +366 -0
- package/runtime/gatewaysdk/environment/schema/ui_replay_record.LICENSE +25 -0
- package/runtime/gatewaysdk/environment/schema/ui_replay_record.js +109 -0
- package/runtime/gatewaysdk/environment/schema/validation.py +10 -1
- package/runtime/gatewaysdk/environment/schema/verbs.py +6 -0
- package/runtime/gatewaysdk/environment/schema/webhooks.py +687 -0
- package/runtime/gatewaysdk/environment/schema/worker.py +5 -1
- package/runtime/gatewaysdk/environment/schema/workers.py +8 -1
- package/runtime/gatewaysdk/environment/schema/world_host.py +64 -18
- package/runtime/gatewaysdk/environment/schema/world_tests.py +74 -0
- package/runtime/gatewaysdk/environment/secret_values.py +1 -1
- package/runtime/gatewaysdk/environment/world.py +34 -43
- package/runtime/gatewaysdk/environment/world_session_contract.py +7 -34
- package/skills/benchmarks-getting-started/SKILL.md +1 -1
- package/skills/connector-schema-authoring/CONNECTORS.md +3 -3
- package/skills/connector-schema-authoring/SKILL.md +20 -24
- package/skills/connector-schema-authoring/connector-worlds.md +155 -13
- package/skills/connector-schema-authoring/world-schema.md +36 -29
- package/skills/world-data-ingestion/SKILL.md +11 -11
- package/skills/worlds-getting-started/SKILL.md +9 -9
- package/skills/worlds-getting-started/references/cli-mcp-map.md +45 -12
- package/skills/worlds-getting-started/references/gotchas.md +40 -9
- package/skills/worlds-getting-started/references/journal-checks.md +17 -2
- package/skills/worlds-getting-started/references/new-world-sandbox.md +1 -1
- package/skills/worlds-getting-started/references/ui-map.md +3 -2
- package/dist/media-BIApiPzS.d.mts +0 -102
- package/dist/media-BIApiPzS.d.ts +0 -102
|
@@ -1,25 +1,33 @@
|
|
|
1
|
-
"""Initialize a schema world from a connector template or the custom
|
|
1
|
+
"""Initialize a schema world from a connector template's files, or from the blank ``custom`` scaffold.
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
Vendor templates are official worlds on the platform: each is a schema world built from its
|
|
4
|
+
template (:func:`init_bundle_from_files`), and a world made from one is that world copied under
|
|
5
|
+
its own name and compiled again (:func:`init_bundle_from_world`), never a vendor directory.
|
|
6
|
+
``custom`` is the one template that ships in code. Initializing places the authored files,
|
|
7
|
+
compiles them and writes an empty initial batch; the world starts with no records.
|
|
7
8
|
"""
|
|
8
9
|
|
|
9
10
|
from __future__ import annotations
|
|
10
11
|
|
|
11
12
|
import base64
|
|
12
13
|
import binascii
|
|
14
|
+
import hashlib
|
|
13
15
|
import json
|
|
16
|
+
import re
|
|
14
17
|
import shutil
|
|
15
18
|
from pathlib import Path
|
|
19
|
+
from typing import Any, Callable, Mapping
|
|
16
20
|
|
|
17
|
-
from
|
|
21
|
+
from .._toml import tomllib
|
|
22
|
+
from .compiler import CompilePlan, Contract, compile_bundle, retarget_journals
|
|
18
23
|
from .store_contract import describe_store
|
|
19
|
-
from .
|
|
24
|
+
from .template_record import RECORD as TEMPLATE_RECORD, TemplateRecord
|
|
25
|
+
from .ui import UiDeclaration
|
|
26
|
+
from .validation import ContractError, encoded, fail, parse_json, tree_path
|
|
20
27
|
|
|
21
28
|
TEMPLATES = Path(__file__).resolve().parent / "connectors"
|
|
22
29
|
CUSTOM = "custom"
|
|
30
|
+
TEMPLATE_SLUG = re.compile(r"[a-z][a-z0-9-]*")
|
|
23
31
|
MANIFEST = """[environment]
|
|
24
32
|
name = "{name}"
|
|
25
33
|
kind = "schema"
|
|
@@ -61,13 +69,12 @@ hosted call into such a handler answers 400 {error: "handler", detail, handler}:
|
|
|
61
69
|
{store}
|
|
62
70
|
|
|
63
71
|
A handler that raises AttributeError, TypeError or NameError is answered 400 with
|
|
64
|
-
that structured body;
|
|
72
|
+
that structured body; `raise Refusal(404, body)` (from gateway_env) answers that 4xx
|
|
73
|
+
and body as the vendor's refusal; any other exception is answered 500 {error: "engine", ...}.
|
|
65
74
|
`schema init --connector custom --with-example-tool` writes the example in full.
|
|
66
75
|
"""
|
|
67
76
|
'''
|
|
68
77
|
|
|
69
|
-
|
|
70
|
-
#: Where the template module's docstring ends and its example tools begin.
|
|
71
78
|
_DOCSTRING_END = '\n"""\n'
|
|
72
79
|
|
|
73
80
|
|
|
@@ -93,10 +100,26 @@ def custom_handlers_source(template_handlers: bytes, with_example_tool: bool) ->
|
|
|
93
100
|
return (doc + end).encode("utf-8")
|
|
94
101
|
|
|
95
102
|
|
|
103
|
+
SUPPORTING_SUFFIXES = {".json", ".md", ".toml"}
|
|
104
|
+
GENERATED_NAMES = {"base.json", "runtime.json", "lock.json", "README.md", "tool-schemas.json", "template.json"}
|
|
105
|
+
#: Template subdirectories copied verbatim to the world root: the connection's
|
|
106
|
+
#: Python (``api/handlers.py``), its served scenarios (``scenarios/…``), the
|
|
107
|
+
#: tests it ships for its tools (``tests/…``, run by ``gateway worlds test``) and its ``ui/``.
|
|
108
|
+
TEMPLATE_DIRECTORIES = {"api", "scenarios", "tests", "ui"}
|
|
109
|
+
#: Template files compile does not lay out, copied to the world root after it, so a world keeps every
|
|
110
|
+
#: file its template brought: the twin of ``splitCarriedTemplateFiles``
|
|
111
|
+
#: (gateway/packages/shared/src/domain/worldSchema.ts).
|
|
112
|
+
CARRIED_DIRECTORIES = {"verifiers", "openapi", "readers", "evidence", "reports"}
|
|
113
|
+
CARRIED_FILES = {"tasks.json"}
|
|
114
|
+
SCHEMA_CONTRACT = "schema/world.json"
|
|
115
|
+
WORLD_MANIFEST = "gateway-env.toml"
|
|
116
|
+
HANDLER_PATHS = ("handlers.py", "tools/handlers.py")
|
|
117
|
+
PLACED_ELSEWHERE = {"world.json", *HANDLER_PATHS}
|
|
118
|
+
|
|
119
|
+
|
|
96
120
|
def list_templates() -> list[str]:
|
|
97
|
-
"""
|
|
98
|
-
|
|
99
|
-
return [s for s in slugs if s != CUSTOM] + [CUSTOM]
|
|
121
|
+
"""The templates this runtime holds: the blank ``custom`` scaffold. Vendor templates live on the platform."""
|
|
122
|
+
return [CUSTOM]
|
|
100
123
|
|
|
101
124
|
|
|
102
125
|
def _slug(value: str, location: str) -> str:
|
|
@@ -105,44 +128,33 @@ def _slug(value: str, location: str) -> str:
|
|
|
105
128
|
return value
|
|
106
129
|
|
|
107
130
|
|
|
131
|
+
def refuse_official(connector: str, door: str, location: str = "/init/connector") -> str:
|
|
132
|
+
"""``connector`` when it names the ``custom`` scaffold; any other slug is an official
|
|
133
|
+
template this runtime does not hold, refused naming the ``door`` that takes its files."""
|
|
134
|
+
slug = _slug(connector, location)
|
|
135
|
+
if slug != CUSTOM:
|
|
136
|
+
fail("template", f"{slug} is an official template on the platform: send its files with {door}", location)
|
|
137
|
+
return slug
|
|
138
|
+
|
|
139
|
+
|
|
108
140
|
def _resource(path: Path) -> Path:
|
|
109
141
|
if path.is_symlink() or path.parent.is_symlink() or not path.is_file():
|
|
110
142
|
fail("template", "Template resources must be regular files, not symlinks", str(path))
|
|
111
143
|
return path
|
|
112
144
|
|
|
113
145
|
|
|
114
|
-
SUPPORTING_SUFFIXES = {".json", ".md", ".toml"}
|
|
115
|
-
GENERATED_NAMES = {"base.json", "runtime.json", "lock.json", "README.md", "tool-schemas.json"}
|
|
116
|
-
#: Template files that are not schema artifacts but travel with a workspace connector.
|
|
117
|
-
TEMPLATE_CODE = {"handlers.py", "api/handlers.py"}
|
|
118
|
-
#: Template subdirectories copied verbatim to the world root: the connection's
|
|
119
|
-
#: Python (``api/handlers.py``), its served scenarios (``scenarios/…``) and the
|
|
120
|
-
#: tests it ships for its tools (``tests/…``, run by ``gateway worlds test``).
|
|
121
|
-
TEMPLATE_DIRECTORIES = {"api", "scenarios", "tests", "ui"}
|
|
122
|
-
#: Template subdirectories that stay with the connector package and never enter a
|
|
123
|
-
#: world: authoring evidence, fidelity reports, and ``readers/`` — how the platform
|
|
124
|
-
#: fills a world on this contract from a live connection (``readers/merge.toml``
|
|
125
|
-
#: names the tools to pull, ``readers/merge.ingest.toml`` maps their payloads).
|
|
126
|
-
CONNECTOR_ONLY_DIRECTORIES = {"evidence", "reports", "readers"}
|
|
127
|
-
|
|
128
|
-
|
|
129
146
|
def _template_directory_files(directory: Path) -> list[Path]:
|
|
130
|
-
|
|
131
|
-
for path in
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
continue
|
|
136
|
-
files.append(_resource(path))
|
|
137
|
-
return files
|
|
138
|
-
|
|
147
|
+
paths = sorted(directory.rglob("*"))
|
|
148
|
+
link = next((path for path in paths if path.is_symlink() or path.parent.is_symlink()), None)
|
|
149
|
+
if link is not None:
|
|
150
|
+
fail("template", "Template resources must be regular files, not symlinks", str(link))
|
|
151
|
+
return [path for path in paths if path.is_file() and "__pycache__" not in path.parts]
|
|
139
152
|
|
|
140
|
-
def template_digest(files: dict[str, bytes]) -> str:
|
|
141
|
-
"""The same digest the platform computes over an installed template directory:
|
|
142
|
-
sha256 over sorted ``path\0bytes\0``. A workspace connector's commit and an
|
|
143
|
-
installed template with identical files therefore pin identically."""
|
|
144
|
-
import hashlib
|
|
145
153
|
|
|
154
|
+
def template_digest(files: Mapping[str, bytes]) -> str:
|
|
155
|
+
"""The same digest the platform computes over a template's files: sha256 over sorted
|
|
156
|
+
``path\0bytes\0``. A workspace connector's commit and an official template with
|
|
157
|
+
identical files therefore pin identically."""
|
|
146
158
|
digest = hashlib.sha256()
|
|
147
159
|
for path in sorted(files):
|
|
148
160
|
digest.update(path.encode("utf-8"))
|
|
@@ -152,65 +164,150 @@ def template_digest(files: dict[str, bytes]) -> str:
|
|
|
152
164
|
return digest.hexdigest()
|
|
153
165
|
|
|
154
166
|
|
|
155
|
-
def
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
"""Create and compile a schema world from a template held in memory: a workspace
|
|
159
|
-
connector's files (``world.json``, ``connector.toml``, ``provenance.json``,
|
|
160
|
-
``scope.toml``, ``handlers.py``, ``api/handlers.py``, ``scenarios/…``, ``tests/…``), exactly
|
|
161
|
-
what :func:`init_bundle` reads from an installed template directory.
|
|
162
|
-
|
|
163
|
-
``connector`` names the template for the manifest; ``name`` the world."""
|
|
164
|
-
destination = Path(destination)
|
|
165
|
-
connector = _slug(connector, "/init/connector")
|
|
166
|
-
name = _slug(name if name is not None else f"{connector}-world", "/init/name")
|
|
167
|
-
if destination.exists() and (not destination.is_dir() or any(destination.iterdir())):
|
|
168
|
-
fail("state_exists", "Destination must be a new or empty directory", "/init")
|
|
169
|
-
if "world.json" not in files:
|
|
170
|
-
fail("template", "A connector template needs world.json", "/init/files")
|
|
171
|
-
if not all(isinstance(k, str) and k and not k.startswith("/") and ".." not in k.split("/") for k in files):
|
|
172
|
-
fail("template", "Template paths must be relative and stay inside the template", "/init/files")
|
|
173
|
-
try:
|
|
174
|
-
world = json.loads(files["world.json"].decode("utf-8"))
|
|
175
|
-
except (UnicodeDecodeError, ValueError):
|
|
176
|
-
fail("template", "world.json is not valid JSON", "/init/files/world.json")
|
|
177
|
-
if not isinstance(world, dict) or not isinstance(world.get("x-gateway"), dict):
|
|
178
|
-
fail("template", "world.json is not a gateway-world document", "/init/files/world.json")
|
|
179
|
-
world["x-gateway"]["package"]["id"] = name
|
|
180
|
-
tools = sorted(world["x-gateway"].get("tools", {}))
|
|
167
|
+
def template_json(files: Mapping[str, bytes], name: str, location: str = "/init/files") -> Any:
|
|
168
|
+
if name not in files:
|
|
169
|
+
fail("template", f"The template carries no {name}", f"{location}/{name}")
|
|
181
170
|
try:
|
|
171
|
+
return parse_json(files[name].decode("utf-8"), name)
|
|
172
|
+
except ContractError:
|
|
173
|
+
raise
|
|
174
|
+
except ValueError:
|
|
175
|
+
fail("template", f"{name} is not valid JSON", f"{location}/{name}")
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _pretty(value) -> bytes:
|
|
179
|
+
return (json.dumps(value, indent=2) + "\n").encode("utf-8")
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _published_slack_capabilities(files: Mapping[str, bytes]) -> bytes:
|
|
183
|
+
from ._slack_fidelity import load_capabilities
|
|
184
|
+
|
|
185
|
+
return _pretty(load_capabilities(files, "capabilities.json"))
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
#: Authored files a template publishes in another form: Slack's capabilities are
|
|
189
|
+
#: expanded, so no reader of a world resolves the authoring defaults.
|
|
190
|
+
RENDERED: dict[tuple[str, str], Callable[[Mapping[str, bytes]], bytes]] = {
|
|
191
|
+
("slack", "capabilities.json"): _published_slack_capabilities,
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
class WorldTemplate:
|
|
196
|
+
"""A connector template held in memory, ``{template-relative path: bytes}``: what a world
|
|
197
|
+
is initialized from (``world.json``, ``connector.toml``, ``handlers.py``, ``provenance.json``,
|
|
198
|
+
``scope.toml``, ``api/…``, ``scenarios/…``, ``tests/…``, ``ui/…``). ``location`` names the
|
|
199
|
+
files in refusals."""
|
|
200
|
+
|
|
201
|
+
def __init__(self, files: Mapping[str, bytes], location: str = "/init/files") -> None:
|
|
202
|
+
self.files = dict(files)
|
|
203
|
+
self.location = location
|
|
204
|
+
|
|
205
|
+
@property
|
|
206
|
+
def digest(self) -> str:
|
|
207
|
+
return template_digest(self.files)
|
|
208
|
+
|
|
209
|
+
def world(self) -> dict:
|
|
210
|
+
if "world.json" not in self.files:
|
|
211
|
+
fail("template", "A connector template needs world.json", self.location)
|
|
212
|
+
world = template_json(self.files, "world.json", self.location)
|
|
213
|
+
if not isinstance(world, dict) or not isinstance(world.get("x-gateway"), dict):
|
|
214
|
+
fail("template", "world.json is not a gateway-world document", f"{self.location}/world.json")
|
|
215
|
+
return world
|
|
216
|
+
|
|
217
|
+
def summary(self, slug: str) -> dict:
|
|
218
|
+
"""What a template listing shows; never runs the template's Python."""
|
|
219
|
+
world = self.world()
|
|
220
|
+
gateway = world["x-gateway"]
|
|
221
|
+
return {
|
|
222
|
+
"slug": slug,
|
|
223
|
+
"name": world.get("title") if isinstance(world.get("title"), str) else None,
|
|
224
|
+
"description": world.get("description") if isinstance(world.get("description"), str) else None,
|
|
225
|
+
"entities": self._names(gateway, "entities"),
|
|
226
|
+
"tools": self._names(gateway, "tools"),
|
|
227
|
+
"hasConnection": "connector.toml" in self.files,
|
|
228
|
+
"ui": UiDeclaration.kind_of(self.files.get("connector.toml")),
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
def _names(self, gateway: dict, key: str) -> list[str]:
|
|
232
|
+
value = gateway.get(key) or {}
|
|
233
|
+
names = list(value) if isinstance(value, (dict, list)) else None
|
|
234
|
+
if names is None or not all(isinstance(name, str) for name in names):
|
|
235
|
+
fail(
|
|
236
|
+
"template",
|
|
237
|
+
f'"x-gateway".{key} must be an object or a list of names',
|
|
238
|
+
f"{self.location}/world.json",
|
|
239
|
+
)
|
|
240
|
+
return sorted(names)
|
|
241
|
+
|
|
242
|
+
def write(self, destination: str | Path, *, connector: str, name: str | None = None) -> Contract:
|
|
243
|
+
"""Lay the template out as a world at ``destination`` (new or empty) and compile it.
|
|
244
|
+
``connector`` names the template in the manifest; ``name`` the world and its package
|
|
245
|
+
id. The domain ``$id`` stays the template's, so worlds built from one template share
|
|
246
|
+
a domain identity."""
|
|
247
|
+
destination = _fresh(destination)
|
|
248
|
+
connector = _slug(connector, "/init/connector")
|
|
249
|
+
name = _slug(name if name is not None else f"{connector}-world", "/init/name")
|
|
250
|
+
world = self._named(self.world(), name)
|
|
251
|
+
placed = self._placed(connector)
|
|
252
|
+
if not world["x-gateway"].get("entities"):
|
|
253
|
+
fail("template", "world.json declares no entities", f"{self.location}/world.json")
|
|
254
|
+
try:
|
|
255
|
+
return self._lay_out(destination, world, placed, name=name, connector=connector)
|
|
256
|
+
except BaseException:
|
|
257
|
+
shutil.rmtree(destination, ignore_errors=True)
|
|
258
|
+
raise
|
|
259
|
+
|
|
260
|
+
def _named(self, world: dict, name: str) -> dict:
|
|
261
|
+
if not isinstance(world["x-gateway"].get("package"), dict):
|
|
262
|
+
fail("template", "world.json declares no x-gateway.package", f"{self.location}/world.json")
|
|
263
|
+
world["x-gateway"]["package"]["id"] = name
|
|
264
|
+
return world
|
|
265
|
+
|
|
266
|
+
def _handlers(self) -> bytes:
|
|
267
|
+
handlers = self.files.get("handlers.py") or self.files.get("tools/handlers.py")
|
|
268
|
+
return handlers if handlers is not None else b""
|
|
269
|
+
|
|
270
|
+
def _target(self, path: str) -> str | None:
|
|
271
|
+
"""Where an authored file lands in the world; None when it stays with the template."""
|
|
272
|
+
parts = path.split("/")
|
|
273
|
+
if path in PLACED_ELSEWHERE or path in GENERATED_NAMES or "__pycache__" in parts:
|
|
274
|
+
return None
|
|
275
|
+
if path == "connector.toml" or _carried(path) or (parts[0] in TEMPLATE_DIRECTORIES and len(parts) > 1):
|
|
276
|
+
return path
|
|
277
|
+
if len(parts) == 1 and Path(path).suffix in SUPPORTING_SUFFIXES and path not in GENERATED_NAMES:
|
|
278
|
+
return f"schema/{path}"
|
|
279
|
+
fail("template", "Unsupported or generated template artifact", f"{self.location}/{path}")
|
|
280
|
+
|
|
281
|
+
def _placed(self, connector: str) -> dict[str, bytes]:
|
|
282
|
+
_confined(self.files, self.location)
|
|
283
|
+
placed = {}
|
|
284
|
+
for path in sorted(self.files):
|
|
285
|
+
target = self._target(path)
|
|
286
|
+
if target is not None:
|
|
287
|
+
placed[target] = self._content(connector, path)
|
|
288
|
+
return placed
|
|
289
|
+
|
|
290
|
+
def _content(self, connector: str, path: str) -> bytes:
|
|
291
|
+
render = RENDERED.get((connector, path))
|
|
292
|
+
return render(self.files) if render is not None else self.files[path]
|
|
293
|
+
|
|
294
|
+
def _lay_out(
|
|
295
|
+
self, destination: Path, world: dict, placed: dict[str, bytes], *, name: str, connector: str
|
|
296
|
+
) -> Contract:
|
|
297
|
+
tools = sorted(world["x-gateway"].get("tools") or {})
|
|
298
|
+
entity = min(world["x-gateway"]["entities"])
|
|
182
299
|
(destination / "schema").mkdir(parents=True)
|
|
183
300
|
(destination / "schema/world.json").write_bytes(_pretty(world))
|
|
184
301
|
shutil.copyfile(_resource(TEMPLATES / "base.json"), destination / "schema/base.json")
|
|
185
302
|
(destination / "tools").mkdir()
|
|
186
|
-
|
|
187
|
-
(destination
|
|
188
|
-
for path, content in sorted(files.items()):
|
|
189
|
-
if path in {"world.json", "handlers.py", "tools/handlers.py"} or path.startswith("__pycache__"):
|
|
190
|
-
continue
|
|
191
|
-
if path == "connector.toml":
|
|
192
|
-
target = destination / "connector.toml"
|
|
193
|
-
elif path.startswith(("api/", "scenarios/", "tests/", "ui/")):
|
|
194
|
-
target = destination / path
|
|
195
|
-
elif path.split("/", 1)[0] in CONNECTOR_ONLY_DIRECTORIES:
|
|
196
|
-
continue # evidence, reports and reader plans stay with the connector, not the world
|
|
197
|
-
elif (
|
|
198
|
-
Path(path).suffix in SUPPORTING_SUFFIXES and Path(path).name not in GENERATED_NAMES and "/" not in path
|
|
199
|
-
):
|
|
200
|
-
target = destination / "schema" / path
|
|
201
|
-
else:
|
|
202
|
-
fail("template", "Unsupported or generated template artifact", f"/init/files/{path}")
|
|
203
|
-
target.parent.mkdir(parents=True, exist_ok=True)
|
|
204
|
-
target.write_bytes(content)
|
|
303
|
+
(destination / "tools/handlers.py").write_bytes(self._handlers())
|
|
304
|
+
_write(destination, {target: content for target, content in placed.items() if not _carried(target)})
|
|
205
305
|
(destination / "gateway-env.toml").write_text(
|
|
206
306
|
MANIFEST.format(name=name, connector=connector, serve=", ".join(f'"{t}"' for t in tools)),
|
|
207
307
|
encoding="utf-8",
|
|
208
308
|
)
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
fail("template", "world.json declares no entities", "/init/files/world.json")
|
|
212
|
-
(destination / "verifiers").mkdir()
|
|
213
|
-
(destination / "verifiers/state_present.sql").write_text(VERIFIER.format(entity=entities[0]), encoding="utf-8")
|
|
309
|
+
(destination / "verifiers").mkdir(exist_ok=True)
|
|
310
|
+
(destination / "verifiers/state_present.sql").write_text(VERIFIER.format(entity=entity), encoding="utf-8")
|
|
214
311
|
contract = compile_bundle(destination)
|
|
215
312
|
(destination / "data").mkdir()
|
|
216
313
|
(destination / "data/initial.json").write_bytes(
|
|
@@ -224,10 +321,151 @@ def init_bundle_from_files(
|
|
|
224
321
|
}
|
|
225
322
|
)
|
|
226
323
|
)
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
324
|
+
_write(destination, {target: content for target, content in placed.items() if _carried(target)})
|
|
325
|
+
TemplateRecord.of_world(destination, connector=connector, digest=self.digest).write(destination)
|
|
326
|
+
return contract
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
class TemplateWorld(WorldTemplate):
|
|
330
|
+
"""An official world's head as stored: a schema world built from a connector template, whose
|
|
331
|
+
``schema/template.json`` (``TemplateRecord``) names the template's digest and the sha256 of every
|
|
332
|
+
file the world holds besides the compiler's outputs. A world made from it is a copy of every file
|
|
333
|
+
it holds under the new world's name, compiled again, so nothing is rebuilt from the template and
|
|
334
|
+
the digest its worlds pin is the one recorded when it was built."""
|
|
335
|
+
|
|
336
|
+
def world(self) -> dict:
|
|
337
|
+
world = template_json(self.files, SCHEMA_CONTRACT, self.location)
|
|
338
|
+
if not isinstance(world, dict) or not isinstance(world.get("x-gateway"), dict):
|
|
339
|
+
fail("template", f"{SCHEMA_CONTRACT} is not a gateway-world document", f"{self.location}/{SCHEMA_CONTRACT}")
|
|
340
|
+
return world
|
|
341
|
+
|
|
342
|
+
@property
|
|
343
|
+
def record(self) -> TemplateRecord:
|
|
344
|
+
return TemplateRecord.parse(self.files, self.location)
|
|
345
|
+
|
|
346
|
+
@property
|
|
347
|
+
def digest(self) -> str:
|
|
348
|
+
return self.record.digest
|
|
349
|
+
|
|
350
|
+
def write(self, destination: str | Path, *, connector: str | None = None, name: str | None = None) -> Contract:
|
|
351
|
+
"""Copy the world to ``destination`` (new or empty) as ``name`` and compile it there; its data follows
|
|
352
|
+
the renamed contract. Built snapshots are outputs of the official world, not part of the copy."""
|
|
353
|
+
from .snapshot import is_snapshot_output
|
|
354
|
+
|
|
355
|
+
_confined(self.files, self.location)
|
|
356
|
+
record = self.record
|
|
357
|
+
record.verify(self.files, self.location)
|
|
358
|
+
destination = _fresh(destination)
|
|
359
|
+
connector = _slug(connector if connector is not None else record.connector, "/init/connector")
|
|
360
|
+
name = _slug(name if name is not None else f"{connector}-world", "/init/name")
|
|
361
|
+
files = {
|
|
362
|
+
**{path: data for path, data in self.files.items() if not is_snapshot_output(path)},
|
|
363
|
+
SCHEMA_CONTRACT: _pretty(self._named(self.world(), name)),
|
|
364
|
+
WORLD_MANIFEST: self._renamed_manifest(name),
|
|
365
|
+
}
|
|
366
|
+
try:
|
|
367
|
+
_write(destination, files)
|
|
368
|
+
plan = CompilePlan.of(destination)
|
|
369
|
+
retarget_journals(destination, plan.contract)
|
|
370
|
+
contract = plan.write()
|
|
371
|
+
TemplateRecord.of_world(destination, connector=record.connector, digest=record.digest).write(destination)
|
|
372
|
+
return contract
|
|
373
|
+
except BaseException:
|
|
374
|
+
shutil.rmtree(destination, ignore_errors=True)
|
|
375
|
+
raise
|
|
376
|
+
|
|
377
|
+
def _renamed_manifest(self, name: str) -> bytes:
|
|
378
|
+
location = f"{self.location}/{WORLD_MANIFEST}"
|
|
379
|
+
if WORLD_MANIFEST not in self.files:
|
|
380
|
+
fail("template", f"The world carries no {WORLD_MANIFEST}", location)
|
|
381
|
+
text = self.files[WORLD_MANIFEST].decode("utf-8")
|
|
382
|
+
current = tomllib.loads(text).get("environment", {}).get("name")
|
|
383
|
+
line = f'name = "{current}"\n'
|
|
384
|
+
if not isinstance(current, str) or text.count(line) != 1:
|
|
385
|
+
fail("template", "The manifest must name its world once, as [environment] name", location)
|
|
386
|
+
return text.replace(line, f'name = "{name}"\n').encode("utf-8")
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def _fresh(destination: str | Path) -> Path:
|
|
390
|
+
destination = Path(destination)
|
|
391
|
+
if destination.exists() and (not destination.is_dir() or any(destination.iterdir())):
|
|
392
|
+
fail("state_exists", "Destination must be a new or empty directory", "/init")
|
|
393
|
+
return destination
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def _carried(path: str) -> bool:
|
|
397
|
+
parts = path.split("/")
|
|
398
|
+
return path in CARRIED_FILES or (parts[0] in CARRIED_DIRECTORIES and len(parts) > 1)
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def _confined(files: Mapping[str, bytes], location: str) -> None:
|
|
402
|
+
for path in files:
|
|
403
|
+
tree_path(path, location)
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
def _write(destination: Path, files: Mapping[str, bytes]) -> None:
|
|
407
|
+
for target, content in files.items():
|
|
408
|
+
written = destination.joinpath(*tree_path(target).parts)
|
|
409
|
+
written.parent.mkdir(parents=True, exist_ok=True)
|
|
410
|
+
written.write_bytes(content)
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
class CustomScaffold(WorldTemplate):
|
|
414
|
+
"""The blank page, read from code: not a shared vendor model, so its ``$id`` is the
|
|
415
|
+
world's own name (``urn:gateway:connector:<name>``), and its example tools, with the
|
|
416
|
+
tests that call them, enter the world only when ``with_example_tool`` asks."""
|
|
417
|
+
|
|
418
|
+
def __init__(self, with_example_tool: bool = False) -> None:
|
|
419
|
+
directory = TEMPLATES / CUSTOM
|
|
420
|
+
files = {
|
|
421
|
+
path.relative_to(directory).as_posix(): path.read_bytes() for path in _template_directory_files(directory)
|
|
422
|
+
}
|
|
423
|
+
super().__init__(files)
|
|
424
|
+
self.with_example_tool = with_example_tool
|
|
425
|
+
|
|
426
|
+
def _named(self, world: dict, name: str) -> dict:
|
|
427
|
+
world = super()._named(world, name)
|
|
428
|
+
world["$id"] = f"urn:gateway:connector:{name}"
|
|
429
|
+
if not self.with_example_tool:
|
|
430
|
+
world["x-gateway"]["tools"] = {}
|
|
431
|
+
return world
|
|
432
|
+
|
|
433
|
+
def _handlers(self) -> bytes:
|
|
434
|
+
return custom_handlers_source(super()._handlers(), self.with_example_tool)
|
|
435
|
+
|
|
436
|
+
def _target(self, path: str) -> str | None:
|
|
437
|
+
if path in GENERATED_NAMES:
|
|
438
|
+
fail("template", "Unsupported or generated template artifact", f"{self.location}/{path}")
|
|
439
|
+
if path.startswith("tests/") and not self.with_example_tool:
|
|
440
|
+
return None
|
|
441
|
+
return super()._target(path)
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def init_bundle_from_files(
|
|
445
|
+
destination: str | Path, files: Mapping[str, bytes], *, connector: str, name: str | None = None
|
|
446
|
+
) -> Contract:
|
|
447
|
+
"""Create and compile a schema world from a template's files: an official template the
|
|
448
|
+
platform serves, a workspace connector, or a document an importer rendered.
|
|
449
|
+
|
|
450
|
+
``connector`` names the template for the manifest; ``name`` the world."""
|
|
451
|
+
return WorldTemplate(files).write(destination, connector=connector, name=name)
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
def init_official_world(destination: str | Path, template_directory: str | Path) -> Contract:
|
|
455
|
+
"""The schema world the official organization stores for a template directory: the template built under its
|
|
456
|
+
own slug. A push leaves its checkout record under ``.gateway/``; no dot entry, at any depth, is a template file."""
|
|
457
|
+
directory = Path(template_directory)
|
|
458
|
+
files = {
|
|
459
|
+
path.relative_to(directory).as_posix(): path.read_bytes()
|
|
460
|
+
for path in _template_directory_files(directory)
|
|
461
|
+
if not any(part.startswith(".") for part in path.relative_to(directory).parts)
|
|
462
|
+
}
|
|
463
|
+
return init_bundle_from_files(destination, files, connector=directory.name, name=directory.name)
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
def init_bundle_from_world(destination: str | Path, files: Mapping[str, bytes], *, name: str | None = None) -> Contract:
|
|
467
|
+
"""Create and compile a world from an official world's files: a copy under ``name``."""
|
|
468
|
+
return TemplateWorld(files).write(destination, name=name)
|
|
231
469
|
|
|
232
470
|
|
|
233
471
|
def init_bundle(
|
|
@@ -237,131 +475,22 @@ def init_bundle(
|
|
|
237
475
|
name: str | None = None,
|
|
238
476
|
with_example_tool: bool = False,
|
|
239
477
|
) -> Contract:
|
|
240
|
-
"""Create and compile
|
|
241
|
-
|
|
242
|
-
The destination must not exist or must be an empty directory. ``name``
|
|
243
|
-
becomes the environment name and package id; the domain ``$id`` stays the
|
|
244
|
-
connector's so worlds built from one connector share a domain identity.
|
|
245
|
-
|
|
246
|
-
The ``custom`` scaffold is the exception: it is a blank page, not a shared
|
|
247
|
-
vendor model, so its ``$id`` is derived from the name
|
|
248
|
-
(``urn:gateway:connector:<name>``) and its example tools are left out unless
|
|
249
|
-
``with_example_tool`` asks for them. Authors used to rewrite the ``$id`` and
|
|
250
|
-
delete the placeholder tools by hand on every world (Brex retrospective).
|
|
251
|
-
"""
|
|
252
|
-
destination = Path(destination)
|
|
253
|
-
connector = _slug(connector, "/init/connector")
|
|
254
|
-
if connector not in list_templates():
|
|
255
|
-
fail(
|
|
256
|
-
"template",
|
|
257
|
-
f"Unknown connector template; choose one of {', '.join(list_templates())}",
|
|
258
|
-
"/init/connector",
|
|
259
|
-
)
|
|
260
|
-
name = _slug(name if name is not None else f"{connector}-world", "/init/name")
|
|
261
|
-
if destination.exists() and (not destination.is_dir() or any(destination.iterdir())):
|
|
262
|
-
fail("state_exists", "Destination must be a new or empty directory", "/init")
|
|
263
|
-
template = TEMPLATES / connector
|
|
264
|
-
# Templates may carry sibling schema dependencies and model evidence. Keep
|
|
265
|
-
# every authored artifact in initialized worlds; never silently drop files.
|
|
266
|
-
supporting_files = []
|
|
267
|
-
code_files = []
|
|
268
|
-
for path in sorted(template.iterdir()):
|
|
269
|
-
# pip compiles the shipped handler module during installation.
|
|
270
|
-
if path.name == "__pycache__" and path.is_dir() and not path.is_symlink():
|
|
271
|
-
continue
|
|
272
|
-
if path.name in TEMPLATE_DIRECTORIES and path.is_dir() and not path.is_symlink():
|
|
273
|
-
# The blank custom world has no tools for the shipped tests to call.
|
|
274
|
-
if not (path.name == "tests" and connector == CUSTOM and not with_example_tool):
|
|
275
|
-
code_files.extend(_template_directory_files(path))
|
|
276
|
-
continue
|
|
277
|
-
if path.name in CONNECTOR_ONLY_DIRECTORIES and path.is_dir() and not path.is_symlink():
|
|
278
|
-
continue
|
|
279
|
-
_resource(path)
|
|
280
|
-
if path.name in {"world.json", "handlers.py"}:
|
|
281
|
-
continue
|
|
282
|
-
if path.suffix not in {".json", ".md", ".toml"} or path.name in {
|
|
283
|
-
"base.json",
|
|
284
|
-
"runtime.json",
|
|
285
|
-
"lock.json",
|
|
286
|
-
"README.md",
|
|
287
|
-
"tool-schemas.json",
|
|
288
|
-
}:
|
|
289
|
-
fail("template", "Unsupported or generated template artifact", str(path))
|
|
290
|
-
supporting_files.append(path)
|
|
291
|
-
world = read_json(_resource(template / "world.json"))
|
|
292
|
-
world["x-gateway"]["package"]["id"] = name
|
|
293
|
-
handlers = _resource(template / "handlers.py").read_bytes()
|
|
294
|
-
if connector == CUSTOM:
|
|
295
|
-
world["$id"] = f"urn:gateway:connector:{name}"
|
|
296
|
-
if not with_example_tool:
|
|
297
|
-
world["x-gateway"]["tools"] = {}
|
|
298
|
-
handlers = custom_handlers_source(handlers, with_example_tool)
|
|
299
|
-
tools = sorted(world["x-gateway"]["tools"])
|
|
300
|
-
try:
|
|
301
|
-
(destination / "schema").mkdir(parents=True)
|
|
302
|
-
(destination / "schema/world.json").write_bytes(_pretty(world))
|
|
303
|
-
shutil.copyfile(_resource(TEMPLATES / "base.json"), destination / "schema/base.json")
|
|
304
|
-
for path in supporting_files:
|
|
305
|
-
# The connection spec sits at the world root, where `worlds connector` and
|
|
306
|
-
# `worlds serve` look for it; everything else describes the schema.
|
|
307
|
-
target = destination / path.name if path.name == "connector.toml" else destination / "schema" / path.name
|
|
308
|
-
if connector == "slack" and path.name == "capabilities.json":
|
|
309
|
-
from ._slack_fidelity import expand_capabilities
|
|
310
|
-
|
|
311
|
-
target.write_bytes(_pretty(expand_capabilities(read_json(_resource(path)))))
|
|
312
|
-
else:
|
|
313
|
-
shutil.copyfile(_resource(path), target)
|
|
314
|
-
for path in code_files:
|
|
315
|
-
target = destination / path.relative_to(template)
|
|
316
|
-
target.parent.mkdir(parents=True, exist_ok=True)
|
|
317
|
-
shutil.copyfile(path, target)
|
|
318
|
-
(destination / "tools").mkdir()
|
|
319
|
-
(destination / "tools/handlers.py").write_bytes(handlers)
|
|
320
|
-
(destination / "gateway-env.toml").write_text(
|
|
321
|
-
MANIFEST.format(
|
|
322
|
-
name=name,
|
|
323
|
-
connector=connector,
|
|
324
|
-
serve=", ".join(f'"{t}"' for t in tools),
|
|
325
|
-
),
|
|
326
|
-
encoding="utf-8",
|
|
327
|
-
)
|
|
328
|
-
first_entity = sorted(world["x-gateway"]["entities"])[0]
|
|
329
|
-
(destination / "verifiers").mkdir()
|
|
330
|
-
(destination / "verifiers/state_present.sql").write_text(VERIFIER.format(entity=first_entity), encoding="utf-8")
|
|
331
|
-
contract = compile_bundle(destination)
|
|
332
|
-
# Only the journal: nothing reads a `data/records.json`, and an empty one
|
|
333
|
-
# read as a leftover to clean up (Brex retrospective).
|
|
334
|
-
(destination / "data").mkdir()
|
|
335
|
-
(destination / "data/initial.json").write_bytes(
|
|
336
|
-
encoded(
|
|
337
|
-
{
|
|
338
|
-
"id": f"{name}.initial.v1",
|
|
339
|
-
"target": contract.target,
|
|
340
|
-
"mapping": {"id": "empty", "version": "1"},
|
|
341
|
-
"coverage": "partial",
|
|
342
|
-
"mutations": [],
|
|
343
|
-
}
|
|
344
|
-
)
|
|
345
|
-
)
|
|
346
|
-
except BaseException:
|
|
347
|
-
shutil.rmtree(destination, ignore_errors=True)
|
|
348
|
-
raise
|
|
349
|
-
return contract
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
def _pretty(value) -> bytes:
|
|
353
|
-
return (json.dumps(value, indent=2) + "\n").encode("utf-8")
|
|
478
|
+
"""Create and compile the blank ``custom`` world at ``destination`` (new or empty).
|
|
354
479
|
|
|
480
|
+
``name`` becomes the environment name and package id. Any other ``connector`` is an
|
|
481
|
+
official template on the platform: fetch its files and build with
|
|
482
|
+
:func:`init_bundle_from_files`."""
|
|
483
|
+
refuse_official(connector, "init_bundle_from_files")
|
|
484
|
+
return CustomScaffold(with_example_tool).write(destination, connector=CUSTOM, name=name)
|
|
355
485
|
|
|
356
|
-
__all__ = ["CUSTOM", "TEMPLATES", "ContractError", "init_bundle", "list_templates"]
|
|
357
486
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
"""The ``files: [{path, content}]`` of a bridge request as ``{path: bytes}``:
|
|
487
|
+
def request_files(request: dict, key: str = "files", location: str | None = None) -> dict[str, bytes]:
|
|
488
|
+
"""The ``[{path, content}]`` under ``key`` of a bridge request as ``{path: bytes}``:
|
|
361
489
|
what ``init_bundle_from_files`` and the template readers consume."""
|
|
362
|
-
|
|
490
|
+
where = location if location is not None else f"/{key}"
|
|
491
|
+
listed = request.get(key)
|
|
363
492
|
if not isinstance(listed, list) or not listed:
|
|
364
|
-
fail("input", "Expected the template's files as [{path, content}]")
|
|
493
|
+
fail("input", "Expected the template's files as [{path, content}]", where)
|
|
365
494
|
files: dict[str, bytes] = {}
|
|
366
495
|
for entry in listed:
|
|
367
496
|
if (
|
|
@@ -369,9 +498,31 @@ def request_files(request: dict) -> dict[str, bytes]:
|
|
|
369
498
|
or not isinstance(entry.get("path"), str)
|
|
370
499
|
or not isinstance(entry.get("content"), str)
|
|
371
500
|
):
|
|
372
|
-
fail("input", "Each template file is {path, content} with base64 content")
|
|
501
|
+
fail("input", "Each template file is {path, content} with base64 content", where)
|
|
502
|
+
tree_path(entry["path"], where)
|
|
503
|
+
if entry["path"] in files:
|
|
504
|
+
fail("path", f"{entry['path']} is sent twice", where)
|
|
373
505
|
try:
|
|
374
506
|
files[entry["path"]] = base64.b64decode(entry["content"], validate=True)
|
|
375
507
|
except (ValueError, binascii.Error):
|
|
376
|
-
fail("input", "Template file content must be base64")
|
|
508
|
+
fail("input", "Template file content must be base64", where)
|
|
377
509
|
return files
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
__all__ = [
|
|
513
|
+
"CUSTOM",
|
|
514
|
+
"TEMPLATES",
|
|
515
|
+
"TEMPLATE_SLUG",
|
|
516
|
+
"ContractError",
|
|
517
|
+
"CustomScaffold",
|
|
518
|
+
"TemplateWorld",
|
|
519
|
+
"WorldTemplate",
|
|
520
|
+
"init_bundle",
|
|
521
|
+
"init_bundle_from_files",
|
|
522
|
+
"init_bundle_from_world",
|
|
523
|
+
"init_official_world",
|
|
524
|
+
"list_templates",
|
|
525
|
+
"refuse_official",
|
|
526
|
+
"request_files",
|
|
527
|
+
"template_digest",
|
|
528
|
+
]
|