sinapsis_cli 1.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.
- sinapsis_cli/__init__.py +13 -0
- sinapsis_cli/commands/__init__.py +0 -0
- sinapsis_cli/commands/cloud/__init__.py +0 -0
- sinapsis_cli/commands/cloud/_agent_selection.py +112 -0
- sinapsis_cli/commands/cloud/_secrets_render.py +15 -0
- sinapsis_cli/commands/cloud/_webhook_render.py +19 -0
- sinapsis_cli/commands/cloud/agents.py +137 -0
- sinapsis_cli/commands/cloud/api_keys.py +155 -0
- sinapsis_cli/commands/cloud/files.py +135 -0
- sinapsis_cli/commands/cloud/jobs.py +532 -0
- sinapsis_cli/commands/cloud/packages.py +164 -0
- sinapsis_cli/commands/cloud/schedules.py +392 -0
- sinapsis_cli/commands/cloud/secrets.py +96 -0
- sinapsis_cli/commands/cloud/templates.py +95 -0
- sinapsis_cli/commands/cloud/webhooks.py +156 -0
- sinapsis_cli/commands/common/__init__.py +0 -0
- sinapsis_cli/commands/common/auth.py +219 -0
- sinapsis_cli/commands/common/profiles.py +156 -0
- sinapsis_cli/commands/common/telemetry.py +150 -0
- sinapsis_cli/commands/local_api/__init__.py +0 -0
- sinapsis_cli/commands/local_api/jobs.py +284 -0
- sinapsis_cli/commands/local_venv/__init__.py +0 -0
- sinapsis_cli/commands/local_venv/_templates_render.py +56 -0
- sinapsis_cli/commands/local_venv/jobs.py +76 -0
- sinapsis_cli/commands/local_venv/templates.py +47 -0
- sinapsis_cli/composition.py +215 -0
- sinapsis_cli/config.py +446 -0
- sinapsis_cli/execution.py +77 -0
- sinapsis_cli/main.py +185 -0
- sinapsis_cli/main_multi.py +152 -0
- sinapsis_cli/shared/__init__.py +3 -0
- sinapsis_cli/shared/jobs.py +134 -0
- sinapsis_cli/shared/terminal.py +8 -0
- sinapsis_cli/shared/writer.py +427 -0
- sinapsis_cli/state.py +310 -0
- sinapsis_cli/telemetry/__init__.py +7 -0
- sinapsis_cli/telemetry/collector.py +84 -0
- sinapsis_cli/telemetry/core.py +331 -0
- sinapsis_cli/telemetry/scheduler.py +234 -0
- sinapsis_cli/telemetry/transport.py +150 -0
- sinapsis_cli-1.0.0.dist-info/METADATA +386 -0
- sinapsis_cli-1.0.0.dist-info/RECORD +46 -0
- sinapsis_cli-1.0.0.dist-info/WHEEL +4 -0
- sinapsis_cli-1.0.0.dist-info/entry_points.txt +5 -0
- sinapsis_cli-1.0.0.dist-info/licenses/LICENSE-APACHE +202 -0
- sinapsis_cli-1.0.0.dist-info/licenses/LICENSE-MIT +21 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"""Local API job backend for the Sinapsis CLI."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
import typer
|
|
8
|
+
from sinapsis.agent import generic_agent_schema_builder
|
|
9
|
+
from sinapsis_contracts.local_api.models import JobResponse
|
|
10
|
+
from sinapsis_protocol import SSEEnvelope
|
|
11
|
+
|
|
12
|
+
from sinapsis_cli.execution import ExecutionContext, create_api_client
|
|
13
|
+
from sinapsis_cli.shared.jobs import register_history_commands, require_job_target, resolve_job_id
|
|
14
|
+
from sinapsis_cli.shared.terminal import divider
|
|
15
|
+
from sinapsis_cli.shared.writer import ResultWriter
|
|
16
|
+
from sinapsis_cli.state import record_job
|
|
17
|
+
|
|
18
|
+
TERMINAL_JOB_STATUSES = frozenset({"completed", "failed", "cancelled"})
|
|
19
|
+
LOCAL_API_POLL_INTERVAL_SECONDS = 0.25
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def create_app(execution: ExecutionContext) -> typer.Typer:
|
|
23
|
+
"""Create the local-API jobs command tree."""
|
|
24
|
+
app = typer.Typer(name="jobs", help="Manage jobs through the local Sinapsis API.", no_args_is_help=True)
|
|
25
|
+
|
|
26
|
+
@app.command()
|
|
27
|
+
def run(
|
|
28
|
+
agent_definition: Path = typer.Argument(
|
|
29
|
+
...,
|
|
30
|
+
exists=True,
|
|
31
|
+
file_okay=True,
|
|
32
|
+
dir_okay=False,
|
|
33
|
+
readable=True,
|
|
34
|
+
help="Path to the agent definition.",
|
|
35
|
+
),
|
|
36
|
+
wait: bool = typer.Option(
|
|
37
|
+
False, "--wait", help="Wait for the job to finish and retrieve its retained results."
|
|
38
|
+
),
|
|
39
|
+
result: bool = typer.Option(False, "--result", help="Retrieve retained results after submission."),
|
|
40
|
+
stream: bool = typer.Option(False, "--stream", help="Stream job events until the job finishes."),
|
|
41
|
+
save_dir: Path = typer.Option(
|
|
42
|
+
Path("results"),
|
|
43
|
+
"--save-dir",
|
|
44
|
+
"-o",
|
|
45
|
+
help="Directory where results and artifacts are saved.",
|
|
46
|
+
),
|
|
47
|
+
print_only: bool = typer.Option(False, "--print-only", help="Print results instead of saving artifacts."),
|
|
48
|
+
) -> None:
|
|
49
|
+
"""Create an agent and submit a job through the local API."""
|
|
50
|
+
selected = sum(bool(value) for value in (wait, result, stream))
|
|
51
|
+
if selected > 1:
|
|
52
|
+
typer.echo("Options --wait, --result, and --stream are mutually exclusive.", err=True)
|
|
53
|
+
raise typer.Exit(code=1)
|
|
54
|
+
|
|
55
|
+
asyncio.run(
|
|
56
|
+
run_local_api_job(
|
|
57
|
+
execution,
|
|
58
|
+
agent_definition,
|
|
59
|
+
wait=wait,
|
|
60
|
+
result=result,
|
|
61
|
+
stream=stream,
|
|
62
|
+
save_dir=save_dir,
|
|
63
|
+
print_only=print_only,
|
|
64
|
+
)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
@app.command()
|
|
68
|
+
def status(
|
|
69
|
+
job_id: str | None = typer.Argument(None, help="ID of the job. Defaults to this profile's most recent job."),
|
|
70
|
+
) -> None:
|
|
71
|
+
"""Show the current status of a local-API job."""
|
|
72
|
+
resolved_job_id = resolve_job_id(job_id, profile_name=execution.profile_name)
|
|
73
|
+
asyncio.run(get_local_api_status(execution, resolved_job_id))
|
|
74
|
+
|
|
75
|
+
@app.command("result")
|
|
76
|
+
def result_command(
|
|
77
|
+
job_id: str | None = typer.Argument(None, help="ID of the job. Defaults to this profile's most recent job."),
|
|
78
|
+
save_dir: Path = typer.Option(
|
|
79
|
+
Path("results"),
|
|
80
|
+
"--save-dir",
|
|
81
|
+
"-o",
|
|
82
|
+
help="Directory where results and artifacts are saved.",
|
|
83
|
+
),
|
|
84
|
+
print_only: bool = typer.Option(False, "--print-only", help="Print results instead of saving artifacts."),
|
|
85
|
+
) -> None:
|
|
86
|
+
"""Replay retained results for a local-API job."""
|
|
87
|
+
resolved_job_id = resolve_job_id(job_id, profile_name=execution.profile_name)
|
|
88
|
+
asyncio.run(get_local_api_result(execution, resolved_job_id, save_dir=save_dir, print_only=print_only))
|
|
89
|
+
|
|
90
|
+
@app.command("stream")
|
|
91
|
+
def stream_command(
|
|
92
|
+
job_id: str | None = typer.Argument(None, help="ID of the job. Defaults to this profile's most recent job."),
|
|
93
|
+
save_dir: Path = typer.Option(
|
|
94
|
+
Path("results"),
|
|
95
|
+
"--save-dir",
|
|
96
|
+
"-o",
|
|
97
|
+
help="Directory where streamed results and artifacts are saved.",
|
|
98
|
+
),
|
|
99
|
+
print_only: bool = typer.Option(
|
|
100
|
+
False, "--print-only", help="Print streamed results instead of saving artifacts."
|
|
101
|
+
),
|
|
102
|
+
) -> None:
|
|
103
|
+
"""Stream events from a running local-API job."""
|
|
104
|
+
resolved_job_id = resolve_job_id(job_id, profile_name=execution.profile_name)
|
|
105
|
+
asyncio.run(stream_local_api_job(execution, resolved_job_id, save_dir=save_dir, print_only=print_only))
|
|
106
|
+
|
|
107
|
+
@app.command()
|
|
108
|
+
def cancel(
|
|
109
|
+
job_id: str | None = typer.Argument(None, help="ID of the job. Defaults to this profile's most recent job."),
|
|
110
|
+
) -> None:
|
|
111
|
+
"""Cancel a running local-API job."""
|
|
112
|
+
resolved_job_id = resolve_job_id(job_id, profile_name=execution.profile_name)
|
|
113
|
+
asyncio.run(cancel_local_api_job(execution, resolved_job_id))
|
|
114
|
+
|
|
115
|
+
register_history_commands(app, execution)
|
|
116
|
+
return app
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
async def run_local_api_job(
|
|
120
|
+
execution: ExecutionContext,
|
|
121
|
+
agent_definition: Path,
|
|
122
|
+
*,
|
|
123
|
+
wait: bool,
|
|
124
|
+
result: bool,
|
|
125
|
+
stream: bool,
|
|
126
|
+
save_dir: Path,
|
|
127
|
+
print_only: bool,
|
|
128
|
+
) -> None:
|
|
129
|
+
"""Submit a job to the local Sinapsis API."""
|
|
130
|
+
api_url = execution.api_url
|
|
131
|
+
definition = generic_agent_schema_builder(agent_definition)
|
|
132
|
+
|
|
133
|
+
async with create_api_client(execution) as client:
|
|
134
|
+
submission = await client.jobs.submit(definition)
|
|
135
|
+
|
|
136
|
+
job_id = submission.job_id
|
|
137
|
+
record_job(
|
|
138
|
+
job_id,
|
|
139
|
+
source=agent_definition,
|
|
140
|
+
target=api_url,
|
|
141
|
+
profile=execution.profile_name,
|
|
142
|
+
backend=execution.profile.backend.value,
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
typer.echo(f"Job ID: {job_id}")
|
|
146
|
+
|
|
147
|
+
if stream:
|
|
148
|
+
await _stream_local_api_job(execution, job_id, api_url=api_url, save_dir=save_dir, print_only=print_only)
|
|
149
|
+
return
|
|
150
|
+
|
|
151
|
+
if wait:
|
|
152
|
+
await _wait_for_local_api_job(execution, job_id, api_url=api_url, save_dir=save_dir, print_only=print_only)
|
|
153
|
+
return
|
|
154
|
+
|
|
155
|
+
if result:
|
|
156
|
+
await _get_local_api_result(execution, job_id, api_url=api_url, save_dir=save_dir, print_only=print_only)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
async def get_local_api_job(execution: ExecutionContext, job_id: str, *, api_url: str) -> JobResponse:
|
|
160
|
+
"""Retrieve local API job state."""
|
|
161
|
+
async with create_api_client(execution, api_url=api_url) as client:
|
|
162
|
+
return await client.jobs.get_status(job_id)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
async def _wait_for_local_api_job(
|
|
166
|
+
execution: ExecutionContext, job_id: str, *, api_url: str, save_dir: Path, print_only: bool
|
|
167
|
+
) -> None:
|
|
168
|
+
"""Wait for a local API job to reach a terminal state."""
|
|
169
|
+
typer.echo("Waiting for job to finish...")
|
|
170
|
+
|
|
171
|
+
async with create_api_client(execution, api_url=api_url) as client:
|
|
172
|
+
while True:
|
|
173
|
+
job = await client.jobs.get_status(job_id)
|
|
174
|
+
print_local_api_status(job)
|
|
175
|
+
|
|
176
|
+
if job.status in TERMINAL_JOB_STATUSES:
|
|
177
|
+
break
|
|
178
|
+
|
|
179
|
+
await asyncio.sleep(LOCAL_API_POLL_INTERVAL_SECONDS)
|
|
180
|
+
|
|
181
|
+
if job.status == "failed":
|
|
182
|
+
return
|
|
183
|
+
|
|
184
|
+
await _get_local_api_result(execution, job_id, api_url=api_url, save_dir=save_dir, print_only=print_only)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
async def _stream_local_api_job(
|
|
188
|
+
execution: ExecutionContext, job_id: str, *, api_url: str, save_dir: Path, print_only: bool
|
|
189
|
+
) -> None:
|
|
190
|
+
"""Stream decoded local API job events."""
|
|
191
|
+
async with create_api_client(execution, api_url=api_url) as client:
|
|
192
|
+
async for event in client.jobs.stream_results(job_id):
|
|
193
|
+
handle_local_envelope(event.envelope, metadata=event.metadata, save_dir=save_dir, print_only=print_only)
|
|
194
|
+
|
|
195
|
+
job = await client.jobs.get_status(job_id)
|
|
196
|
+
if job.status in TERMINAL_JOB_STATUSES:
|
|
197
|
+
typer.echo(f"Job {job_id} {job.status}.")
|
|
198
|
+
return
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
async def _get_local_api_result(
|
|
202
|
+
execution: ExecutionContext, job_id: str, *, api_url: str, save_dir: Path, print_only: bool
|
|
203
|
+
) -> None:
|
|
204
|
+
"""Replay retained local API events."""
|
|
205
|
+
async with create_api_client(execution, api_url=api_url) as client:
|
|
206
|
+
events = await client.jobs.get_result(job_id)
|
|
207
|
+
|
|
208
|
+
for event in events:
|
|
209
|
+
handle_local_envelope(event.envelope, metadata=event.metadata, save_dir=save_dir, print_only=print_only)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def handle_local_envelope(envelope: SSEEnvelope, *, metadata: dict[str, Any], save_dir: Path, print_only: bool) -> None:
|
|
213
|
+
"""Print or save one decoded local API event."""
|
|
214
|
+
if print_only:
|
|
215
|
+
print_local_envelope(envelope, metadata=metadata)
|
|
216
|
+
return
|
|
217
|
+
|
|
218
|
+
if envelope.data is None:
|
|
219
|
+
return
|
|
220
|
+
|
|
221
|
+
writer = ResultWriter(save_dir)
|
|
222
|
+
written = writer.write_container(
|
|
223
|
+
envelope.data,
|
|
224
|
+
agent_id=envelope.agent_id or metadata.get("agent_id"),
|
|
225
|
+
stream_name=envelope.job_id or metadata.get("job_id"),
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
if written:
|
|
229
|
+
typer.echo(f"Saved {len(written)} file(s) to {save_dir}")
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def print_local_envelope(envelope: SSEEnvelope, *, metadata: dict[str, Any]) -> None:
|
|
233
|
+
"""Print one local API SSEEnvelope."""
|
|
234
|
+
typer.echo(f"Cursor: {metadata.get('cursor', '-')}")
|
|
235
|
+
typer.echo(f"Agent: {envelope.agent_id or metadata.get('agent_id', '-')}")
|
|
236
|
+
typer.echo(f"Job: {envelope.job_id or metadata.get('job_id', '-')}")
|
|
237
|
+
typer.echo(f"Event: {envelope.event_type}")
|
|
238
|
+
typer.echo(f"Status: {envelope.agent_status or '-'}")
|
|
239
|
+
|
|
240
|
+
if envelope.errors:
|
|
241
|
+
typer.echo(f"Errors: {envelope.errors}")
|
|
242
|
+
|
|
243
|
+
if envelope.data is not None:
|
|
244
|
+
typer.echo(f"Container: {envelope.data.container_id}")
|
|
245
|
+
|
|
246
|
+
divider(0)
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def print_local_api_status(job: JobResponse) -> None:
|
|
250
|
+
"""Print local API job state."""
|
|
251
|
+
typer.echo(f"Job ID: {job.job_id}")
|
|
252
|
+
typer.echo(f"State: {job.status}")
|
|
253
|
+
typer.echo(f"Agent: {job.agent_id}")
|
|
254
|
+
typer.echo(f"Instance: {job.instance_id or '-'}")
|
|
255
|
+
typer.echo(f"Error: {job.error or '-'}")
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
async def get_local_api_status(execution: ExecutionContext, job_id: str) -> None:
|
|
259
|
+
"""Show the current status of a local API job."""
|
|
260
|
+
api_url = require_job_target(job_id, profile_name=execution.profile_name, default_target=execution.api_url)
|
|
261
|
+
job = await get_local_api_job(execution, job_id, api_url=api_url)
|
|
262
|
+
print_local_api_status(job)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
async def get_local_api_result(execution: ExecutionContext, job_id: str, *, save_dir: Path, print_only: bool) -> None:
|
|
266
|
+
"""Retrieve a local API job result from retained events."""
|
|
267
|
+
api_url = require_job_target(job_id, profile_name=execution.profile_name, default_target=execution.api_url)
|
|
268
|
+
await _get_local_api_result(execution, job_id, api_url=api_url, save_dir=save_dir, print_only=print_only)
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
async def stream_local_api_job(execution: ExecutionContext, job_id: str, *, save_dir: Path, print_only: bool) -> None:
|
|
272
|
+
"""Stream a local API job from its recorded target."""
|
|
273
|
+
api_url = require_job_target(job_id, profile_name=execution.profile_name, default_target=execution.api_url)
|
|
274
|
+
await _stream_local_api_job(execution, job_id, api_url=api_url, save_dir=save_dir, print_only=print_only)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
async def cancel_local_api_job(execution: ExecutionContext, job_id: str) -> None:
|
|
278
|
+
"""Cancel a local API job."""
|
|
279
|
+
api_url = require_job_target(job_id, profile_name=execution.profile_name, default_target=execution.api_url)
|
|
280
|
+
|
|
281
|
+
async with create_api_client(execution, api_url=api_url) as client:
|
|
282
|
+
await client.jobs.cancel(job_id)
|
|
283
|
+
|
|
284
|
+
typer.echo(f"Cancelled job {job_id}.")
|
|
File without changes
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
|
|
3
|
+
import sinapsis.templates as templates
|
|
4
|
+
import typer
|
|
5
|
+
import yaml
|
|
6
|
+
from rich.console import Console
|
|
7
|
+
from rich.syntax import Syntax
|
|
8
|
+
from sinapsis.cli import (
|
|
9
|
+
get_template,
|
|
10
|
+
make_example_template_config,
|
|
11
|
+
parse_attributes_base_model,
|
|
12
|
+
parse_template_doc_string,
|
|
13
|
+
)
|
|
14
|
+
from sinapsis.template import Template
|
|
15
|
+
|
|
16
|
+
from sinapsis_cli.shared.terminal import divider
|
|
17
|
+
|
|
18
|
+
console = Console()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def print_config(template_name: str, output: str = "yaml") -> None:
|
|
22
|
+
config_dict = make_example_template_config(template_name, templates)
|
|
23
|
+
if output == "json":
|
|
24
|
+
console.print_json(data=config_dict)
|
|
25
|
+
|
|
26
|
+
elif output == "yaml":
|
|
27
|
+
yaml_text = yaml.safe_dump(config_dict, sort_keys=False)
|
|
28
|
+
console.print(Syntax(yaml_text, "yaml", theme="ansi_dark", line_numbers=False))
|
|
29
|
+
else:
|
|
30
|
+
console.print(config_dict)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def display_single_template_info(template_name: str) -> None:
|
|
34
|
+
template_cls: Template = get_template(template_name, templates)
|
|
35
|
+
if template_cls:
|
|
36
|
+
typer.echo(inspect.getfile(template_cls))
|
|
37
|
+
typer.echo(parse_template_doc_string(template_name, template_cls))
|
|
38
|
+
typer.echo(f"\n{template_name} attributes: ")
|
|
39
|
+
for att_line in parse_attributes_base_model(template_cls.AttributesBaseModel):
|
|
40
|
+
typer.echo(att_line)
|
|
41
|
+
divider(0)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def display_all(names_only):
|
|
45
|
+
if names_only:
|
|
46
|
+
divider(0)
|
|
47
|
+
typer.echo("All templates available:")
|
|
48
|
+
for template_name in templates.__all__:
|
|
49
|
+
typer.echo(f" {template_name}")
|
|
50
|
+
raise typer.Exit(code=1)
|
|
51
|
+
|
|
52
|
+
for template_name in templates.__all__:
|
|
53
|
+
try:
|
|
54
|
+
display_single_template_info(template_name)
|
|
55
|
+
except (ModuleNotFoundError, ImportError) as err:
|
|
56
|
+
typer.echo(f"Unable to import {template_name} due to", err=err)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Direct local job backend for the Sinapsis CLI."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
import typer
|
|
7
|
+
|
|
8
|
+
from sinapsis_cli.execution import ExecutionContext
|
|
9
|
+
from sinapsis_cli.shared.jobs import register_history_commands
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def create_app(execution: ExecutionContext) -> typer.Typer:
|
|
13
|
+
"""Create the direct-local jobs command tree.
|
|
14
|
+
|
|
15
|
+
Direct local execution is synchronous from the CLI user's perspective, so
|
|
16
|
+
API-specific options such as ``--wait``, ``--result``, ``--stream`` and
|
|
17
|
+
``--refresh`` are deliberately not registered.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
execution: Resolved execution context for the current invocation.
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
Typer application containing direct-local job commands.
|
|
24
|
+
"""
|
|
25
|
+
app = typer.Typer(
|
|
26
|
+
name="jobs",
|
|
27
|
+
help="Run and manage jobs in the local virtual environment.",
|
|
28
|
+
no_args_is_help=True,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
@app.command()
|
|
32
|
+
def run(
|
|
33
|
+
agent_definition: Path = typer.Argument(
|
|
34
|
+
...,
|
|
35
|
+
exists=True,
|
|
36
|
+
file_okay=True,
|
|
37
|
+
dir_okay=False,
|
|
38
|
+
readable=True,
|
|
39
|
+
help="Path to the agent definition.",
|
|
40
|
+
),
|
|
41
|
+
save_dir: Path = typer.Option(
|
|
42
|
+
Path("results"),
|
|
43
|
+
"--save-dir",
|
|
44
|
+
"-o",
|
|
45
|
+
help="Directory where results and artifacts are saved.",
|
|
46
|
+
),
|
|
47
|
+
print_only: bool = typer.Option(False, "--print-only", help="Print results instead of saving artifacts."),
|
|
48
|
+
) -> None:
|
|
49
|
+
"""Run an agent directly in the selected local environment."""
|
|
50
|
+
asyncio.run(run_local_job(execution, agent_definition, save_dir=save_dir, print_only=print_only))
|
|
51
|
+
|
|
52
|
+
register_history_commands(app, execution)
|
|
53
|
+
return app
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
async def run_local_job(
|
|
57
|
+
execution: ExecutionContext, agent_definition: Path, *, save_dir: Path, print_only: bool
|
|
58
|
+
) -> None:
|
|
59
|
+
"""Run an agent directly in the configured virtual environment."""
|
|
60
|
+
from sinapsis.agent import AgentMode
|
|
61
|
+
from sinapsis.cli import generic_agent_builder
|
|
62
|
+
|
|
63
|
+
agent = generic_agent_builder(agent_definition)
|
|
64
|
+
|
|
65
|
+
agent_mode = agent.agent_attributes.agent_mode
|
|
66
|
+
|
|
67
|
+
if agent_mode == AgentMode.GENERATOR:
|
|
68
|
+
for result in agent(container=None):
|
|
69
|
+
typer.echo(result)
|
|
70
|
+
else:
|
|
71
|
+
result = agent(container=None)
|
|
72
|
+
typer.echo(result)
|
|
73
|
+
|
|
74
|
+
# Successful generator and non-generator executions must leave with status 0.
|
|
75
|
+
# A hard-coded failure made CLI telemetry report successful jobs as errors.
|
|
76
|
+
return
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Local template inspection commands."""
|
|
2
|
+
|
|
3
|
+
from enum import Enum
|
|
4
|
+
|
|
5
|
+
import typer
|
|
6
|
+
|
|
7
|
+
from ._templates_render import display_all, display_single_template_info, print_config
|
|
8
|
+
|
|
9
|
+
app = typer.Typer(
|
|
10
|
+
name="templates",
|
|
11
|
+
help="Inspect templates available in the local environment.",
|
|
12
|
+
no_args_is_help=True,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class OutputFormat(str, Enum):
|
|
17
|
+
"""Supported template configuration output formats."""
|
|
18
|
+
|
|
19
|
+
PRETTY = "pretty"
|
|
20
|
+
JSON = "json"
|
|
21
|
+
YAML = "yaml"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@app.command()
|
|
25
|
+
def docstring(template_name: str = typer.Argument(..., help="The name of the template.")) -> None:
|
|
26
|
+
"""Show the docstring for a locally installed template."""
|
|
27
|
+
display_single_template_info(template_name)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@app.command("show-all")
|
|
31
|
+
def all_names(
|
|
32
|
+
names_only: bool = typer.Option(False, "--names-only", help="Show only template names."),
|
|
33
|
+
) -> None:
|
|
34
|
+
"""Show locally installed templates."""
|
|
35
|
+
display_all(names_only)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@app.command("example-config")
|
|
39
|
+
def example_config(
|
|
40
|
+
template_name: str = typer.Argument(..., help="The name of the template."),
|
|
41
|
+
output: OutputFormat = typer.Option(
|
|
42
|
+
OutputFormat.YAML, "--output", "-o", help="Output format: pretty, json, or yaml."
|
|
43
|
+
),
|
|
44
|
+
) -> None:
|
|
45
|
+
"""Print an example configuration for a local template."""
|
|
46
|
+
print_config(template_name, output)
|
|
47
|
+
typer.echo()
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"""Profile-driven CLI command composition."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
import typer
|
|
7
|
+
|
|
8
|
+
from sinapsis_cli.config import ExecutionBackend
|
|
9
|
+
from sinapsis_cli.execution import ExecutionContext
|
|
10
|
+
|
|
11
|
+
AUTH_PANEL = "\n[bold #00F5FF on #2E1065] 🔐 AUTHENTICATION [/bold #00F5FF on #2E1065]"
|
|
12
|
+
CORE_PANEL = "\n[bold #00F5FF on #2E1065] ⚡ CORE SERVICES [/bold #00F5FF on #2E1065]"
|
|
13
|
+
DEV_PANEL = "\n[bold #00F5FF on #2E1065] 🛠️ DEVELOPER TOOLS [/bold #00F5FF on #2E1065]"
|
|
14
|
+
SEC_PANEL = "\n[bold #00F5FF on #2E1065] 🛡️ SECURITY & KEYS [/bold #00F5FF on #2E1065]"
|
|
15
|
+
CONFIG_PANEL = "\n[bold #00F5FF on #2E1065] ⚙️ CONFIGURATION [/bold #00F5FF on #2E1065]"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
RegisterCommand = Callable[[typer.Typer, ExecutionContext], None]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class CommandProvider:
|
|
23
|
+
"""A named command-tree contribution that can be composed into a profile."""
|
|
24
|
+
|
|
25
|
+
name: str
|
|
26
|
+
register: RegisterCommand
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _register_local_venv_jobs(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
30
|
+
"""Register the local virtual-environment jobs command."""
|
|
31
|
+
from sinapsis_cli.commands.local_venv.jobs import create_app as create_jobs_app
|
|
32
|
+
|
|
33
|
+
app.add_typer(create_jobs_app(execution), name="jobs", rich_help_panel=CORE_PANEL)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _register_local_api_jobs(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
37
|
+
"""Register the local API jobs command."""
|
|
38
|
+
from sinapsis_cli.commands.local_api.jobs import create_app as create_jobs_app
|
|
39
|
+
|
|
40
|
+
app.add_typer(create_jobs_app(execution), name="jobs", rich_help_panel=CORE_PANEL)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _register_cloud_agents(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
44
|
+
"""Register Cloud saved-agent discovery and version management."""
|
|
45
|
+
from sinapsis_cli.commands.cloud.agents import create_app
|
|
46
|
+
|
|
47
|
+
app.add_typer(create_app(execution), name="agents", rich_help_panel=CORE_PANEL)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _register_cloud_jobs(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
51
|
+
"""Register the cloud jobs command."""
|
|
52
|
+
from sinapsis_cli.commands.cloud.jobs import create_app as create_jobs_app
|
|
53
|
+
|
|
54
|
+
app.add_typer(create_jobs_app(execution), name="jobs", rich_help_panel=CORE_PANEL)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _register_templates(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
58
|
+
"""Register local virtual-environment template commands."""
|
|
59
|
+
del execution
|
|
60
|
+
|
|
61
|
+
from sinapsis_cli.commands.local_venv import templates
|
|
62
|
+
|
|
63
|
+
app.add_typer(templates.app, name="templates", rich_help_panel=CORE_PANEL)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _register_cloud_templates(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
67
|
+
"""Register public user-facing Cloud catalog commands."""
|
|
68
|
+
del execution
|
|
69
|
+
from sinapsis_cli.commands.cloud import templates
|
|
70
|
+
|
|
71
|
+
app.add_typer(templates.app, name="templates", rich_help_panel=CORE_PANEL)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _register_files(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
75
|
+
"""Register cloud file commands."""
|
|
76
|
+
del execution
|
|
77
|
+
|
|
78
|
+
from sinapsis_cli.commands.cloud import files
|
|
79
|
+
|
|
80
|
+
app.add_typer(files.app, name="files", rich_help_panel=CORE_PANEL)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _register_webhooks(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
84
|
+
"""Register cloud webhook commands."""
|
|
85
|
+
del execution
|
|
86
|
+
|
|
87
|
+
from sinapsis_cli.commands.cloud import webhooks
|
|
88
|
+
|
|
89
|
+
app.add_typer(webhooks.app, name="webhooks", rich_help_panel=DEV_PANEL)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _register_api_keys(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
93
|
+
"""Register cloud API key commands."""
|
|
94
|
+
del execution
|
|
95
|
+
|
|
96
|
+
from sinapsis_cli.commands.cloud import api_keys
|
|
97
|
+
|
|
98
|
+
app.add_typer(api_keys.app, name="api-keys", rich_help_panel=SEC_PANEL)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _register_packages(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
102
|
+
"""Provide native uv credential integration for Cloud users."""
|
|
103
|
+
del execution
|
|
104
|
+
from sinapsis_cli.commands.cloud import packages
|
|
105
|
+
app.add_typer(packages.app, name="packages", rich_help_panel=DEV_PANEL)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _register_secrets(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
109
|
+
"""Register cloud secret commands."""
|
|
110
|
+
del execution
|
|
111
|
+
|
|
112
|
+
from sinapsis_cli.commands.cloud import secrets
|
|
113
|
+
|
|
114
|
+
app.add_typer(secrets.app, name="secrets", rich_help_panel=SEC_PANEL)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
_COMMAND_PROVIDERS: dict[str, CommandProvider] = {
|
|
118
|
+
"local-venv.jobs": CommandProvider("local-venv.jobs", _register_local_venv_jobs),
|
|
119
|
+
"local-venv.templates": CommandProvider("local-venv.templates", _register_templates),
|
|
120
|
+
"local-api.jobs": CommandProvider("local-api.jobs", _register_local_api_jobs),
|
|
121
|
+
"cloud.jobs": CommandProvider("cloud.jobs", _register_cloud_jobs),
|
|
122
|
+
"cloud.agents": CommandProvider("cloud.agents", _register_cloud_agents),
|
|
123
|
+
"cloud.templates": CommandProvider("cloud.templates", _register_cloud_templates),
|
|
124
|
+
"cloud.files": CommandProvider("cloud.files", _register_files),
|
|
125
|
+
"cloud.webhooks": CommandProvider("cloud.webhooks", _register_webhooks),
|
|
126
|
+
"cloud.api-keys": CommandProvider("cloud.api-keys", _register_api_keys),
|
|
127
|
+
"cloud.packages": CommandProvider("cloud.packages", _register_packages),
|
|
128
|
+
"cloud.secrets": CommandProvider("cloud.secrets", _register_secrets),
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
_BASE_COMMAND_SETS: dict[ExecutionBackend, tuple[str, ...]] = {
|
|
133
|
+
ExecutionBackend.LOCAL_VENV: ("local-venv.jobs", "local-venv.templates"),
|
|
134
|
+
ExecutionBackend.LOCAL_API: ("local-api.jobs",),
|
|
135
|
+
ExecutionBackend.CLOUD: (
|
|
136
|
+
"cloud.jobs",
|
|
137
|
+
"cloud.agents",
|
|
138
|
+
"cloud.templates",
|
|
139
|
+
"cloud.files",
|
|
140
|
+
"cloud.webhooks",
|
|
141
|
+
"cloud.api-keys",
|
|
142
|
+
"cloud.packages",
|
|
143
|
+
"cloud.secrets",
|
|
144
|
+
),
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def register_command_provider(provider: CommandProvider) -> None:
|
|
149
|
+
"""Register an additive command provider.
|
|
150
|
+
|
|
151
|
+
Args:
|
|
152
|
+
provider: Command provider to register.
|
|
153
|
+
|
|
154
|
+
Raises:
|
|
155
|
+
ValueError: If a provider with the same name is already registered.
|
|
156
|
+
"""
|
|
157
|
+
if provider.name in _COMMAND_PROVIDERS:
|
|
158
|
+
raise ValueError(f"Command provider {provider.name!r} is already registered.")
|
|
159
|
+
|
|
160
|
+
_COMMAND_PROVIDERS[provider.name] = provider
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def register_global_commands(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
164
|
+
"""Register commands that manage the CLI rather than an execution backend."""
|
|
165
|
+
del execution
|
|
166
|
+
|
|
167
|
+
from sinapsis_cli.commands.common import auth, profiles, telemetry
|
|
168
|
+
|
|
169
|
+
app.command(name="configure", rich_help_panel=AUTH_PANEL)(auth.configure)
|
|
170
|
+
|
|
171
|
+
app.add_typer(profiles.app, name="profiles", rich_help_panel=CONFIG_PANEL)
|
|
172
|
+
|
|
173
|
+
config_app = typer.Typer(name="config", help="Configure local Sinapsis CLI features.", no_args_is_help=True)
|
|
174
|
+
config_app.add_typer(telemetry.app, name="telemetry")
|
|
175
|
+
app.add_typer(config_app, name="config", rich_help_panel=CONFIG_PANEL)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def compose_profile_commands(app: typer.Typer, execution: ExecutionContext) -> None:
|
|
179
|
+
"""Compose the command tree for a selected profile.
|
|
180
|
+
|
|
181
|
+
The selected backend contributes its base command providers. Additional
|
|
182
|
+
providers configured on the profile are then appended, allowing profiles
|
|
183
|
+
to extend a backend without modifying its base command tree.
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
app: Root Typer application.
|
|
187
|
+
execution: Resolved execution context for the selected profile.
|
|
188
|
+
|
|
189
|
+
Raises:
|
|
190
|
+
ValueError: If the backend has no registered command set or the profile
|
|
191
|
+
references an unknown additive command provider.
|
|
192
|
+
"""
|
|
193
|
+
try:
|
|
194
|
+
base_provider_names = _BASE_COMMAND_SETS[execution.profile.backend]
|
|
195
|
+
except KeyError as exception:
|
|
196
|
+
raise ValueError(
|
|
197
|
+
f"No command composition is registered for backend {execution.profile.backend.value!r}."
|
|
198
|
+
) from exception
|
|
199
|
+
|
|
200
|
+
provider_names = [*base_provider_names, *execution.profile.command_sets]
|
|
201
|
+
|
|
202
|
+
for provider_name in dict.fromkeys(provider_names):
|
|
203
|
+
try:
|
|
204
|
+
provider = _COMMAND_PROVIDERS[provider_name]
|
|
205
|
+
except KeyError as exception:
|
|
206
|
+
raise ValueError(
|
|
207
|
+
f"Profile {execution.profile_name!r} references unknown command set {provider_name!r}."
|
|
208
|
+
) from exception
|
|
209
|
+
|
|
210
|
+
provider.register(app, execution)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def command_provider_names() -> tuple[str, ...]:
|
|
214
|
+
"""Return the currently registered provider names."""
|
|
215
|
+
return tuple(_COMMAND_PROVIDERS)
|