synth-code 0.0.1__py3-none-macosx_15_0_arm64.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.
- synth_code/__init__.py +0 -0
- synth_code/account.py +551 -0
- synth_code/account_http.py +86 -0
- synth_code/account_refusal.py +80 -0
- synth_code/accounting.py +148 -0
- synth_code/activity_grouping.py +149 -0
- synth_code/agent_panels.py +127 -0
- synth_code/approvals.py +397 -0
- synth_code/blobs.py +31 -0
- synth_code/cli.py +734 -0
- synth_code/client.py +413 -0
- synth_code/composer_delivery.py +139 -0
- synth_code/conversation.py +949 -0
- synth_code/daemon.py +34 -0
- synth_code/edition.py +306 -0
- synth_code/enrollment.py +304 -0
- synth_code/file_edit_facts.py +10 -0
- synth_code/freeze.py +234 -0
- synth_code/grant.py +59 -0
- synth_code/history.py +140 -0
- synth_code/history_generated.py +219 -0
- synth_code/history_wire.py +166 -0
- synth_code/image_input.py +109 -0
- synth_code/image_preview.py +105 -0
- synth_code/legacy_history.py +161 -0
- synth_code/live_turns.py +171 -0
- synth_code/model_picker.py +311 -0
- synth_code/native/manifest.json +1 -0
- synth_code/native/synth-code-runtime +0 -0
- synth_code/native.py +95 -0
- synth_code/oauth.py +284 -0
- synth_code/operator.py +116 -0
- synth_code/pack_source.py +65 -0
- synth_code/panel_request.py +291 -0
- synth_code/panels.py +40 -0
- synth_code/plugin_host.py +459 -0
- synth_code/protocol.py +35 -0
- synth_code/provider.py +313 -0
- synth_code/provider_fault.py +173 -0
- synth_code/provider_frames.py +571 -0
- synth_code/recommendation.py +324 -0
- synth_code/report.py +72 -0
- synth_code/research_events.py +44 -0
- synth_code/research_trace.py +449 -0
- synth_code/rex_view.py +58 -0
- synth_code/runtime_refusal.py +108 -0
- synth_code/schema/budgets.json +17 -0
- synth_code/schema/history-v2.json +1002 -0
- synth_code/session.py +291 -0
- synth_code/shoal.py +564 -0
- synth_code/side_panel.py +309 -0
- synth_code/skills.py +269 -0
- synth_code/slash.py +425 -0
- synth_code/source_view.py +135 -0
- synth_code/starter.json +13 -0
- synth_code/stdio.py +75 -0
- synth_code/stream.py +142 -0
- synth_code/subscriptions.py +267 -0
- synth_code/synth_authority.py +362 -0
- synth_code/synth_extra.py +32 -0
- synth_code/terminal.py +817 -0
- synth_code/terminal_journal.py +284 -0
- synth_code/terminal_layout.py +643 -0
- synth_code/terminal_surfaces.py +470 -0
- synth_code/terminal_viewport.py +125 -0
- synth_code/tool_render.py +86 -0
- synth_code/transcript_frames.py +212 -0
- synth_code/transcript_placement.py +63 -0
- synth_code/transcript_rows.py +456 -0
- synth_code/transport.py +243 -0
- synth_code/v001_history.py +468 -0
- synth_code/view.py +854 -0
- synth_code/views/__init__.py +93 -0
- synth_code/views/charts.py +196 -0
- synth_code/views/curses_adapter.py +116 -0
- synth_code/views/engine.py +170 -0
- synth_code/views/model.py +226 -0
- synth_code/views/primitives.py +270 -0
- synth_code/views/tiling.py +124 -0
- synth_code-0.0.1.dist-info/METADATA +50 -0
- synth_code-0.0.1.dist-info/RECORD +110 -0
- synth_code-0.0.1.dist-info/WHEEL +5 -0
- synth_code-0.0.1.dist-info/entry_points.txt +6 -0
- synth_code-0.0.1.dist-info/licenses/temper/LICENSE-APACHE +190 -0
- synth_code-0.0.1.dist-info/licenses/temper/LICENSE-MIT +21 -0
- synth_code-0.0.1.dist-info/licenses/temper/SOURCE.json +433 -0
- synth_code-0.0.1.dist-info/top_level.txt +2 -0
- synth_code_plugins/_sdk/__init__.py +19 -0
- synth_code_plugins/_sdk/callback_wire.py +240 -0
- synth_code_plugins/_sdk/callbacks.py +317 -0
- synth_code_plugins/_sdk/compliance.py +51 -0
- synth_code_plugins/_sdk/contracts.py +236 -0
- synth_code_plugins/_sdk/discovery.py +75 -0
- synth_code_plugins/_sdk/job_binding.py +71 -0
- synth_code_plugins/_sdk/lifecycle.py +50 -0
- synth_code_plugins/_sdk/lifecycle_generated.py +15 -0
- synth_code_plugins/_sdk/manifest.py +269 -0
- synth_code_plugins/_sdk/pack.py +332 -0
- synth_code_plugins/_sdk/push.py +98 -0
- synth_code_plugins/_sdk/records.py +26 -0
- synth_code_plugins/_sdk/registry.py +491 -0
- synth_code_plugins/_sdk/schemas.py +210 -0
- synth_code_plugins/_sdk/sources.py +230 -0
- synth_code_plugins/_sdk/surfaces.py +54 -0
- synth_code_plugins/_sdk/tools.py +172 -0
- synth_code_plugins/_sdk/worker_memory.py +49 -0
- synth_code_plugins/core/SKILL.md +6 -0
- synth_code_plugins/core/__init__.py +21 -0
- synth_code_plugins/core/plugin.toml +44 -0
- synth_code_plugins/core/source.py +18 -0
synth_code/oauth.py
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"""Origin-bound OAuth PKCE and RFC 8628 device sign-in with Synth Code scopes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import hashlib
|
|
7
|
+
import math
|
|
8
|
+
import secrets
|
|
9
|
+
import sys
|
|
10
|
+
import time
|
|
11
|
+
import webbrowser
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
14
|
+
from urllib.parse import urlencode
|
|
15
|
+
|
|
16
|
+
import httpx
|
|
17
|
+
from pydantic import ValidationError
|
|
18
|
+
|
|
19
|
+
# All 9 scopes are declared in production ConsentScopes() on https://mcp.usesynth.ai:
|
|
20
|
+
# - account:usage:read authorizes GET /v1/usage/summary in `synth-code account`
|
|
21
|
+
# - account:limits:read authorizes GET /v1/limits and checkout-key scope intersection
|
|
22
|
+
# - account:write authorizes DELETE /v1/api-keys/{key_id} so `synth-code logout`
|
|
23
|
+
# revokes the Shoal parent checkout key server-side instead of waiting for TTL expiry
|
|
24
|
+
SYNTH_CODE_OAUTH_SCOPES = (
|
|
25
|
+
"account:billing:read",
|
|
26
|
+
"account:billing:write",
|
|
27
|
+
"account:limits:read",
|
|
28
|
+
"account:usage:read",
|
|
29
|
+
"account:write",
|
|
30
|
+
"offline_access",
|
|
31
|
+
"synth:action",
|
|
32
|
+
"synth:read",
|
|
33
|
+
"user:org:read",
|
|
34
|
+
)
|
|
35
|
+
# Requested only by `synth-code login --with-index` in editions that ship Index.
|
|
36
|
+
INDEX_OAUTH_SCOPES = ("index:read", "index:search")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _oauth_error(response: httpx.Response) -> str:
|
|
40
|
+
try:
|
|
41
|
+
payload = response.json()
|
|
42
|
+
except ValueError:
|
|
43
|
+
return f"HTTP {response.status_code}"
|
|
44
|
+
if isinstance(payload, dict) and isinstance(payload.get("error"), str):
|
|
45
|
+
return payload["error"]
|
|
46
|
+
return f"HTTP {response.status_code}"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def show_authorization_url(url: str) -> None:
|
|
50
|
+
"""Tell the user where to sign in; stderr, because stdout carries command JSON."""
|
|
51
|
+
print(
|
|
52
|
+
"Could not open a browser. Open this URL on this machine to sign in:\n" + url,
|
|
53
|
+
file=sys.stderr,
|
|
54
|
+
flush=True,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def show_device_code(grant) -> None:
|
|
59
|
+
"""Tell the user where to enter the device code; stderr, like the PKCE URL."""
|
|
60
|
+
print(
|
|
61
|
+
"To sign in, open " + grant.verification_uri + " and enter code " + grant.user_code,
|
|
62
|
+
file=sys.stderr,
|
|
63
|
+
flush=True,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def browser_available() -> bool:
|
|
68
|
+
"""Whether this machine has a browser ``login_pkce`` could open."""
|
|
69
|
+
try:
|
|
70
|
+
webbrowser.get()
|
|
71
|
+
except webbrowser.Error:
|
|
72
|
+
return False
|
|
73
|
+
return True
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def login_pkce(
|
|
77
|
+
sdk,
|
|
78
|
+
base_url: str | None = None,
|
|
79
|
+
*,
|
|
80
|
+
scopes: tuple[str, ...] = SYNTH_CODE_OAUTH_SCOPES,
|
|
81
|
+
timeout: float = 180,
|
|
82
|
+
open_browser: Callable[[str], bool] | None = None,
|
|
83
|
+
show_url: Callable[[str], None] = show_authorization_url,
|
|
84
|
+
) -> None:
|
|
85
|
+
"""Interactive browser PKCE S256 sign-in requesting Synth Code's full scope set.
|
|
86
|
+
|
|
87
|
+
When no browser opens, the authorization URL is shown instead and the loopback
|
|
88
|
+
callback keeps waiting until the timeout.
|
|
89
|
+
"""
|
|
90
|
+
from synth_ai._auth_models import ClientRegistration, CredentialRecord, OAuthToken
|
|
91
|
+
from synth_ai.errors import SynthError
|
|
92
|
+
|
|
93
|
+
base = sdk.status(base_url).base_url
|
|
94
|
+
oauth = sdk.oauth_base(base)
|
|
95
|
+
state = secrets.token_urlsafe(32)
|
|
96
|
+
verifier = secrets.token_urlsafe(64)
|
|
97
|
+
challenge = (
|
|
98
|
+
base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest())
|
|
99
|
+
.decode()
|
|
100
|
+
.rstrip("=")
|
|
101
|
+
)
|
|
102
|
+
result: dict[str, str] = {}
|
|
103
|
+
|
|
104
|
+
class Callback(BaseHTTPRequestHandler):
|
|
105
|
+
def do_GET(self) -> None:
|
|
106
|
+
received = sdk.callback_result(self.path, state=state, issuer=oauth)
|
|
107
|
+
if received is None:
|
|
108
|
+
self.send_response(400)
|
|
109
|
+
self.end_headers()
|
|
110
|
+
return
|
|
111
|
+
result.update(received)
|
|
112
|
+
self.send_response(200)
|
|
113
|
+
self.send_header("Content-Type", "text/plain")
|
|
114
|
+
self.end_headers()
|
|
115
|
+
self.wfile.write(b"Sign-in complete. You can close this window.")
|
|
116
|
+
|
|
117
|
+
def log_message(self, format: str, *args: object) -> None:
|
|
118
|
+
return
|
|
119
|
+
|
|
120
|
+
with (
|
|
121
|
+
HTTPServer(("127.0.0.1", 0), Callback) as server,
|
|
122
|
+
httpx.Client(timeout=30, follow_redirects=False) as client,
|
|
123
|
+
):
|
|
124
|
+
redirect = f"http://127.0.0.1:{server.server_port}/callback"
|
|
125
|
+
metadata = sdk.discover(client, oauth)
|
|
126
|
+
try:
|
|
127
|
+
reg = client.post(
|
|
128
|
+
sdk.endpoint(metadata, "registration_endpoint", oauth),
|
|
129
|
+
json={
|
|
130
|
+
"client_name": "Synth Code CLI",
|
|
131
|
+
"redirect_uris": [redirect],
|
|
132
|
+
"grant_types": ["authorization_code", "refresh_token"],
|
|
133
|
+
"response_types": ["code"],
|
|
134
|
+
"token_endpoint_auth_method": "none",
|
|
135
|
+
},
|
|
136
|
+
)
|
|
137
|
+
except httpx.TransportError as error:
|
|
138
|
+
raise SynthError("OAuth client registration failed") from error
|
|
139
|
+
if not reg.is_success:
|
|
140
|
+
raise SynthError("OAuth client registration refused: " + _oauth_error(reg))
|
|
141
|
+
try:
|
|
142
|
+
client_id = ClientRegistration.model_validate_json(reg.content).client_id
|
|
143
|
+
except ValidationError as error:
|
|
144
|
+
raise SynthError("OAuth client registration response invalid") from error
|
|
145
|
+
authorization = (
|
|
146
|
+
sdk.endpoint(metadata, "authorization_endpoint", oauth)
|
|
147
|
+
+ "?"
|
|
148
|
+
+ urlencode(
|
|
149
|
+
{
|
|
150
|
+
"client_id": client_id,
|
|
151
|
+
"redirect_uri": redirect,
|
|
152
|
+
"response_type": "code",
|
|
153
|
+
"code_challenge": challenge,
|
|
154
|
+
"code_challenge_method": "S256",
|
|
155
|
+
"state": state,
|
|
156
|
+
"resource": oauth + "/mcp",
|
|
157
|
+
"scope": " ".join(scopes),
|
|
158
|
+
}
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
if not (open_browser or webbrowser.open)(authorization):
|
|
162
|
+
show_url(authorization)
|
|
163
|
+
deadline = time.monotonic() + timeout
|
|
164
|
+
server.timeout = 1
|
|
165
|
+
while not result and time.monotonic() < deadline:
|
|
166
|
+
server.handle_request()
|
|
167
|
+
if "error" in result:
|
|
168
|
+
raise SynthError("OAuth authorization refused: " + result["error"])
|
|
169
|
+
if "code" not in result:
|
|
170
|
+
raise SynthError("OAuth authorization denied or timed out")
|
|
171
|
+
try:
|
|
172
|
+
tok_resp = client.post(
|
|
173
|
+
sdk.endpoint(metadata, "token_endpoint", oauth),
|
|
174
|
+
data={
|
|
175
|
+
"grant_type": "authorization_code",
|
|
176
|
+
"client_id": client_id,
|
|
177
|
+
"code": result["code"],
|
|
178
|
+
"code_verifier": verifier,
|
|
179
|
+
"redirect_uri": redirect,
|
|
180
|
+
"resource": oauth + "/mcp",
|
|
181
|
+
},
|
|
182
|
+
)
|
|
183
|
+
except httpx.TransportError as error:
|
|
184
|
+
raise SynthError("OAuth token exchange failed") from error
|
|
185
|
+
if not tok_resp.is_success:
|
|
186
|
+
raise SynthError("OAuth token exchange refused: " + _oauth_error(tok_resp))
|
|
187
|
+
try:
|
|
188
|
+
parsed = OAuthToken.model_validate_json(tok_resp.content)
|
|
189
|
+
except ValidationError as error:
|
|
190
|
+
raise SynthError("OAuth token response invalid") from error
|
|
191
|
+
records = sdk.read_tokens()
|
|
192
|
+
records[base] = CredentialRecord(
|
|
193
|
+
access_token=parsed.access_token,
|
|
194
|
+
refresh_token=parsed.refresh_token,
|
|
195
|
+
client_id=client_id,
|
|
196
|
+
expires_at=time.time() + parsed.expires_in,
|
|
197
|
+
)
|
|
198
|
+
sdk.save_tokens(records)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def login_device(
|
|
202
|
+
sdk,
|
|
203
|
+
base_url: str | None = None,
|
|
204
|
+
*,
|
|
205
|
+
show,
|
|
206
|
+
scopes: tuple[str, ...] = SYNTH_CODE_OAUTH_SCOPES,
|
|
207
|
+
sleep=time.sleep,
|
|
208
|
+
clock=time.monotonic,
|
|
209
|
+
) -> None:
|
|
210
|
+
"""RFC 8628 device authorization sign-in requesting Synth Code's full scope set."""
|
|
211
|
+
from synth_ai._auth_models import CredentialRecord, DeviceAuthorization, OAuthToken
|
|
212
|
+
from synth_ai.errors import SynthError
|
|
213
|
+
|
|
214
|
+
base = sdk.status(base_url).base_url
|
|
215
|
+
oauth = sdk.oauth_base(base)
|
|
216
|
+
with httpx.Client(timeout=30, follow_redirects=False) as client:
|
|
217
|
+
metadata = sdk.discover(client, oauth)
|
|
218
|
+
if metadata.device_authorization_endpoint is None:
|
|
219
|
+
raise SynthError("This server does not offer device sign-in; use synth-code login")
|
|
220
|
+
try:
|
|
221
|
+
response = client.post(
|
|
222
|
+
sdk.endpoint(metadata, "device_authorization_endpoint", oauth),
|
|
223
|
+
data={
|
|
224
|
+
"client_id": sdk.DEVICE_CLIENT_ID,
|
|
225
|
+
"scope": " ".join(scopes),
|
|
226
|
+
"resource": oauth + "/mcp",
|
|
227
|
+
},
|
|
228
|
+
)
|
|
229
|
+
except httpx.TransportError as error:
|
|
230
|
+
raise SynthError("Device sign-in request failed") from error
|
|
231
|
+
if not response.is_success:
|
|
232
|
+
raise SynthError("Device sign-in refused: " + _oauth_error(response))
|
|
233
|
+
try:
|
|
234
|
+
device = DeviceAuthorization.model_validate_json(response.content)
|
|
235
|
+
except ValidationError as error:
|
|
236
|
+
raise SynthError("Device sign-in response invalid") from error
|
|
237
|
+
show(device)
|
|
238
|
+
token_url = sdk.endpoint(metadata, "token_endpoint", oauth)
|
|
239
|
+
interval = float(device.interval)
|
|
240
|
+
deadline = clock() + device.expires_in
|
|
241
|
+
max_polls = max(256, math.ceil(device.expires_in / max(interval, 0.001)) + 16)
|
|
242
|
+
for _ in range(max_polls):
|
|
243
|
+
sleep(interval)
|
|
244
|
+
if clock() >= deadline:
|
|
245
|
+
raise SynthError("Device sign-in code expired; run synth-code login --device again")
|
|
246
|
+
try:
|
|
247
|
+
response = client.post(
|
|
248
|
+
token_url,
|
|
249
|
+
data={
|
|
250
|
+
"grant_type": sdk.DEVICE_GRANT_TYPE,
|
|
251
|
+
"client_id": sdk.DEVICE_CLIENT_ID,
|
|
252
|
+
"device_code": device.device_code,
|
|
253
|
+
"resource": oauth + "/mcp",
|
|
254
|
+
},
|
|
255
|
+
)
|
|
256
|
+
except httpx.TransportError as error:
|
|
257
|
+
raise SynthError("Device sign-in poll failed") from error
|
|
258
|
+
if response.is_success:
|
|
259
|
+
break
|
|
260
|
+
err_code = _oauth_error(response)
|
|
261
|
+
if err_code == "authorization_pending":
|
|
262
|
+
continue
|
|
263
|
+
if err_code == "slow_down":
|
|
264
|
+
interval += 5
|
|
265
|
+
continue
|
|
266
|
+
if err_code == "access_denied":
|
|
267
|
+
raise SynthError("Device sign-in denied")
|
|
268
|
+
if err_code == "expired_token":
|
|
269
|
+
raise SynthError("Device sign-in code expired; run synth-code login --device again")
|
|
270
|
+
raise SynthError("Device sign-in failed: " + err_code)
|
|
271
|
+
else:
|
|
272
|
+
raise SynthError("Device sign-in exceeded maximum poll attempts")
|
|
273
|
+
try:
|
|
274
|
+
parsed = OAuthToken.model_validate_json(response.content)
|
|
275
|
+
except ValidationError as error:
|
|
276
|
+
raise SynthError("Device token response invalid") from error
|
|
277
|
+
records = sdk.read_tokens()
|
|
278
|
+
records[base] = CredentialRecord(
|
|
279
|
+
access_token=parsed.access_token,
|
|
280
|
+
refresh_token=parsed.refresh_token,
|
|
281
|
+
client_id=sdk.DEVICE_CLIENT_ID,
|
|
282
|
+
expires_at=time.time() + parsed.expires_in,
|
|
283
|
+
)
|
|
284
|
+
sdk.save_tokens(records)
|
synth_code/operator.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""Operations shared by the installed terminal and headless clients."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import difflib
|
|
6
|
+
import json
|
|
7
|
+
import shlex
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from uuid import uuid4
|
|
10
|
+
|
|
11
|
+
from . import approvals
|
|
12
|
+
from .edition import Feature, require_feature
|
|
13
|
+
from .session import edit, message, revise
|
|
14
|
+
from .skills import action
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def preview(client, identity):
|
|
18
|
+
return format_preview(client.send("preview", effect=identity))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def format_preview(data):
|
|
22
|
+
before = data["before_content"]
|
|
23
|
+
if before is None:
|
|
24
|
+
return f"{data['name']} · {data['digest']}\n" + data["content"]
|
|
25
|
+
diff = difflib.unified_diff(
|
|
26
|
+
before.splitlines(),
|
|
27
|
+
data["content"].splitlines(),
|
|
28
|
+
fromfile=data["name"] + " (before)",
|
|
29
|
+
tofile=data["name"] + " (proposed)",
|
|
30
|
+
lineterm="",
|
|
31
|
+
)
|
|
32
|
+
return f"{data['phase']} · {data['digest']}\n" + "\n".join(diff)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def operator(client, workspace, command, identity=None):
|
|
36
|
+
identity = identity or uuid4().hex
|
|
37
|
+
verb, _, rest = command.partition(" ")
|
|
38
|
+
rest = rest.strip()
|
|
39
|
+
if verb == "study":
|
|
40
|
+
research = require_feature(Feature.RESEARCH)
|
|
41
|
+
args = shlex.split(rest)
|
|
42
|
+
if len(args) not in (2, 3) or args[1] not in ("seed", "propose", "grade"):
|
|
43
|
+
raise ValueError("Expected study specification, stage and optional round")
|
|
44
|
+
return research.study(
|
|
45
|
+
client, Path(args[0]), args[1], int(args[2]) if len(args) > 2 else 1
|
|
46
|
+
)
|
|
47
|
+
if verb == "panel":
|
|
48
|
+
return require_feature(Feature.HOSTED_ARTIFACTS).panel_view(client, int(rest))
|
|
49
|
+
if verb in ("resume", "composition"):
|
|
50
|
+
return client.send(verb)
|
|
51
|
+
if verb == "checkpoint":
|
|
52
|
+
through, _, summary = rest.partition(" ")
|
|
53
|
+
return client.send(
|
|
54
|
+
"checkpoint", id=identity, through=int(through), summary=summary
|
|
55
|
+
)
|
|
56
|
+
if verb == "ask":
|
|
57
|
+
role, _, text = rest.partition(" ")
|
|
58
|
+
return message(client, role, text, identity)
|
|
59
|
+
if verb in ("edit", "create"):
|
|
60
|
+
words = shlex.split(rest)
|
|
61
|
+
if len(words) < 2:
|
|
62
|
+
raise ValueError("Expected file and edit request")
|
|
63
|
+
return edit(
|
|
64
|
+
client, identity, words[0], " ".join(words[1:]), create=verb == "create"
|
|
65
|
+
)
|
|
66
|
+
if verb == "review":
|
|
67
|
+
return preview(client, rest)
|
|
68
|
+
if verb in ("approve", "deny"):
|
|
69
|
+
words = rest.split()
|
|
70
|
+
if len(words) > 2:
|
|
71
|
+
raise ValueError("Expected effect and digest")
|
|
72
|
+
if len(words) == 1:
|
|
73
|
+
result = client.send(verb, effect="policy", digest=words[0])
|
|
74
|
+
return client.send("execute", effect="policy") if verb == "approve" else result
|
|
75
|
+
effect, digest = approvals.resolve_target(client, words)
|
|
76
|
+
decide = approvals.approve if verb == "approve" else approvals.deny
|
|
77
|
+
return decide(client, effect, digest)
|
|
78
|
+
if verb == "steer":
|
|
79
|
+
turn, _, text = rest.partition(" ")
|
|
80
|
+
return client.send("steer", id=turn, operation=identity, message=text)
|
|
81
|
+
if verb == "cancel":
|
|
82
|
+
return client.send("cancel", id=rest)
|
|
83
|
+
if verb == "skills":
|
|
84
|
+
args = shlex.split(rest)
|
|
85
|
+
if len(args) > 2:
|
|
86
|
+
raise ValueError("Expected skill action and name")
|
|
87
|
+
out = action(
|
|
88
|
+
client,
|
|
89
|
+
args[0] if args else "list",
|
|
90
|
+
args[1] if len(args) == 2 else None,
|
|
91
|
+
workspace,
|
|
92
|
+
identity,
|
|
93
|
+
)
|
|
94
|
+
return (
|
|
95
|
+
out
|
|
96
|
+
if isinstance(out, str)
|
|
97
|
+
else json.dumps(out, indent=2, ensure_ascii=False)
|
|
98
|
+
)
|
|
99
|
+
if verb == "note":
|
|
100
|
+
fields = shlex.split(rest)
|
|
101
|
+
if len(fields) < 5:
|
|
102
|
+
raise ValueError(
|
|
103
|
+
"Expected kind, scope, document ID, previous revision and text"
|
|
104
|
+
)
|
|
105
|
+
kind, scope, key, expected = fields[:4]
|
|
106
|
+
return revise(
|
|
107
|
+
client, kind, scope, key, int(expected), " ".join(fields[4:]), identity
|
|
108
|
+
)
|
|
109
|
+
if verb == "profile":
|
|
110
|
+
return client.send("configure", profile=json.loads(Path(rest).read_text()))
|
|
111
|
+
if verb == "status":
|
|
112
|
+
return client.send("snapshot")
|
|
113
|
+
if verb == "publish":
|
|
114
|
+
notebook, cursor = rest.split()
|
|
115
|
+
return require_feature(Feature.HOSTED_ARTIFACTS).publish(client, notebook, int(cursor))
|
|
116
|
+
raise ValueError("Unknown command")
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""Operator entrypoint for attaching a declared source to an authenticated run."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import fcntl
|
|
6
|
+
import os
|
|
7
|
+
from contextlib import contextmanager
|
|
8
|
+
from hashlib import sha256
|
|
9
|
+
|
|
10
|
+
from synth_code_plugins._sdk.job_binding import JobAnchor
|
|
11
|
+
from synth_code_plugins._sdk.push import publish
|
|
12
|
+
from synth_code_plugins._sdk.tools import packs
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def attach_source(
|
|
16
|
+
client,
|
|
17
|
+
plugin: str,
|
|
18
|
+
source: str,
|
|
19
|
+
endpoint: str | None,
|
|
20
|
+
*,
|
|
21
|
+
turn: str | None = None,
|
|
22
|
+
tool_id: str | None = None,
|
|
23
|
+
) -> None:
|
|
24
|
+
if (turn is None) != (tool_id is None):
|
|
25
|
+
raise ValueError("Source launch pins require both --turn and --tool-id")
|
|
26
|
+
available = packs()
|
|
27
|
+
if plugin not in available:
|
|
28
|
+
raise ValueError(f"Unknown bundled pack {plugin}")
|
|
29
|
+
pack = available[plugin]
|
|
30
|
+
if turn is not None and not pack.launch_receipts:
|
|
31
|
+
raise ValueError("This source does not follow external jobs")
|
|
32
|
+
anchor = (
|
|
33
|
+
JobAnchor(client.run, turn, tool_id)
|
|
34
|
+
if turn is not None and tool_id is not None
|
|
35
|
+
else None
|
|
36
|
+
)
|
|
37
|
+
with _feed_claim(client, source):
|
|
38
|
+
publish(client, pack, source, endpoint, pack.follow, anchor=anchor)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@contextmanager
|
|
42
|
+
def _feed_claim(client, source: str):
|
|
43
|
+
directory = client.state / "source-feeds"
|
|
44
|
+
directory.mkdir(mode=0o700, exist_ok=True)
|
|
45
|
+
isolate = sha256(f"{client.run}\0{source}".encode()).hexdigest()
|
|
46
|
+
root = os.open(directory, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
|
47
|
+
try:
|
|
48
|
+
fd = os.open(
|
|
49
|
+
isolate + ".lock",
|
|
50
|
+
os.O_WRONLY | os.O_CREAT | os.O_NOFOLLOW,
|
|
51
|
+
0o600,
|
|
52
|
+
dir_fd=root,
|
|
53
|
+
)
|
|
54
|
+
finally:
|
|
55
|
+
os.close(root)
|
|
56
|
+
try:
|
|
57
|
+
try:
|
|
58
|
+
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
59
|
+
except BlockingIOError as error:
|
|
60
|
+
raise ValueError(
|
|
61
|
+
f"Source {source} already has an attached local feed"
|
|
62
|
+
) from error
|
|
63
|
+
yield
|
|
64
|
+
finally:
|
|
65
|
+
os.close(fd)
|