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
sinapsis_cli/__init__.py
ADDED
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""Per-account, per-endpoint local selection of saved immutable agent versions.
|
|
2
|
+
|
|
3
|
+
JWT contents are used only to namespace local convenience state, NEVER to authorize
|
|
4
|
+
an API call. The backend remains responsible for validating every request.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import base64
|
|
8
|
+
import binascii
|
|
9
|
+
import hashlib
|
|
10
|
+
import json
|
|
11
|
+
from uuid import UUID
|
|
12
|
+
|
|
13
|
+
import typer
|
|
14
|
+
|
|
15
|
+
from sinapsis_cli.execution import ExecutionContext
|
|
16
|
+
from sinapsis_cli.state import get_saved_agent, record_saved_agent
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def selection_scope(execution: ExecutionContext, client) -> str:
|
|
20
|
+
"""Scope CLI selection by Cloud profile, API endpoint, and credential identity.
|
|
21
|
+
|
|
22
|
+
JWTs use their stable subject across access-token refreshes; opaque API keys
|
|
23
|
+
use a digest of the key, so changing accounts/keys never reuses stale state.
|
|
24
|
+
Do not persist access tokens, raw identity claims, or API keys.
|
|
25
|
+
"""
|
|
26
|
+
# The Cloud SDK already owns credential refresh; retrieve its active token.
|
|
27
|
+
token = await client._auth.get_access_token()
|
|
28
|
+
principal = _principal_id(token)
|
|
29
|
+
canonical = json.dumps(
|
|
30
|
+
[execution.profile_name, execution.api_url.rstrip("/").lower(), principal],
|
|
31
|
+
separators=(",", ":"),
|
|
32
|
+
)
|
|
33
|
+
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _principal_id(token: str) -> str:
|
|
37
|
+
"""Identify the local credential without trusting it as authorization."""
|
|
38
|
+
segments = token.split(".")
|
|
39
|
+
if len(segments) == 3:
|
|
40
|
+
try:
|
|
41
|
+
encoded = segments[1]
|
|
42
|
+
payload = json.loads(base64.urlsafe_b64decode(encoded + "=" * (-len(encoded) % 4)))
|
|
43
|
+
if isinstance(payload, dict):
|
|
44
|
+
subject = payload.get("sub")
|
|
45
|
+
if isinstance(subject, str):
|
|
46
|
+
# Normal Cloud JWT identities are UUIDs.
|
|
47
|
+
parsed = UUID(subject)
|
|
48
|
+
issuer = payload.get("iss", "")
|
|
49
|
+
if not isinstance(issuer, str):
|
|
50
|
+
issuer = ""
|
|
51
|
+
return f"jwt:{issuer}:{parsed}"
|
|
52
|
+
except (ValueError, UnicodeDecodeError, TypeError, binascii.Error):
|
|
53
|
+
pass
|
|
54
|
+
return "opaque:" + hashlib.sha256(token.encode("utf-8")).hexdigest()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def remember_agent(scope: str, agent_id: UUID, version_id: UUID) -> None:
|
|
58
|
+
"""Record a verified selection after a successful Management API request."""
|
|
59
|
+
record_saved_agent(scope, str(agent_id), str(version_id))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def newest_version(client, agent_id: UUID, *, allow_empty: bool = False):
|
|
63
|
+
"""Return the newest visible version, failing if the API page is incomplete."""
|
|
64
|
+
versions = await client.agents.list_versions(agent_id)
|
|
65
|
+
if not versions:
|
|
66
|
+
if allow_empty:
|
|
67
|
+
return None
|
|
68
|
+
raise typer.BadParameter(f"Agent {agent_id} has no versions. Publish one with 'agents publish'.")
|
|
69
|
+
# Management currently caps its versions response at 200 without pagination.
|
|
70
|
+
# Selecting 'latest' from a truncated list would be unsafe.
|
|
71
|
+
if len(versions) >= 200:
|
|
72
|
+
raise typer.BadParameter(
|
|
73
|
+
"The server returned 200 versions; latest cannot be determined safely. "
|
|
74
|
+
"Supply --agent-version-id explicitly."
|
|
75
|
+
)
|
|
76
|
+
return max(versions, key=lambda version: (version.created_at, str(version.id)))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
async def resolve_schedule_agent(
|
|
80
|
+
client, execution: ExecutionContext, agent_id: UUID | None, version_id: UUID | None
|
|
81
|
+
) -> tuple[UUID, UUID, str]:
|
|
82
|
+
"""Resolve optional CLI flags to immutable IDs before creating a schedule."""
|
|
83
|
+
scope = await selection_scope(execution, client)
|
|
84
|
+
selected = get_saved_agent(scope)
|
|
85
|
+
explicit_agent = agent_id is not None
|
|
86
|
+
|
|
87
|
+
if agent_id is None:
|
|
88
|
+
if selected is None:
|
|
89
|
+
raise typer.BadParameter(
|
|
90
|
+
"No agent selected for this Cloud account/profile. "
|
|
91
|
+
"Run 'sinx-cloud agents register FILE.yaml' or supply --agent-id "
|
|
92
|
+
"and --agent-version-id."
|
|
93
|
+
)
|
|
94
|
+
agent_id = UUID(selected.agent_id)
|
|
95
|
+
|
|
96
|
+
if version_id is None:
|
|
97
|
+
if explicit_agent:
|
|
98
|
+
version = await newest_version(client, agent_id)
|
|
99
|
+
version_id = version.id
|
|
100
|
+
else:
|
|
101
|
+
assert selected is not None
|
|
102
|
+
version_id = UUID(selected.agent_version_id)
|
|
103
|
+
elif not explicit_agent:
|
|
104
|
+
# The API has no global reverse lookup for a version UUID.
|
|
105
|
+
# Use the locally selected agent, and verify the association.
|
|
106
|
+
version = await client.agents.get_version(agent_id, version_id)
|
|
107
|
+
if version.agent_id != agent_id:
|
|
108
|
+
raise typer.BadParameter(
|
|
109
|
+
"The version does not belong to the selected agent. Supply both --agent-id and --agent-version-id."
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
return agent_id, version_id, scope
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from rich import print
|
|
2
|
+
from rich.table import Table
|
|
3
|
+
from sinapsis_contracts.cloud_api.models.secrets.responses import UserSecretListResponse
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def print_user_secrets(data: UserSecretListResponse):
|
|
7
|
+
table = Table(title="User Registered Secrets", caption="Secret values not shown")
|
|
8
|
+
table.add_column("Label".title(), style="bold cyan")
|
|
9
|
+
table.add_column("Metadata ID".title(), style="bold cyan")
|
|
10
|
+
|
|
11
|
+
# Add rows from the nested list
|
|
12
|
+
for secret in data.secrets:
|
|
13
|
+
table.add_row(secret.label, str(secret.id))
|
|
14
|
+
|
|
15
|
+
print(table)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Render user-owned webhook integrations."""
|
|
2
|
+
|
|
3
|
+
from rich import print
|
|
4
|
+
from rich.table import Table
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def print_webhooks(data) -> None:
|
|
8
|
+
table = Table(title="Webhook Integrations", caption=f"Total: {len(data.items)}")
|
|
9
|
+
for heading in ("ID", "Name", "Provider", "Active", "Public URL"):
|
|
10
|
+
table.add_column(heading)
|
|
11
|
+
for item in data.items:
|
|
12
|
+
table.add_row(
|
|
13
|
+
str(item.webhook_id),
|
|
14
|
+
item.integration_name,
|
|
15
|
+
item.provider,
|
|
16
|
+
str(item.is_active),
|
|
17
|
+
item.webhook_url,
|
|
18
|
+
)
|
|
19
|
+
print(table)
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""Explicit saved-agent registration and authenticated version discovery."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import json
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from uuid import UUID
|
|
7
|
+
|
|
8
|
+
import typer
|
|
9
|
+
from sinapsis.agent import load_yaml_config
|
|
10
|
+
|
|
11
|
+
from sinapsis_cli.execution import ExecutionContext, create_api_client
|
|
12
|
+
from sinapsis_cli.state import get_agent_selection, record_agent_selection
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _load_definition(path: Path) -> dict:
|
|
16
|
+
"""Forward unmodified YAML; the server's Sinapsis builder validates it."""
|
|
17
|
+
return load_yaml_config(path)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _show_version(version, *, name: str | None = None) -> None:
|
|
21
|
+
"""Never abbreviate UUIDs or rely on terminal-width table rendering."""
|
|
22
|
+
prefix = f"{name} | " if name else ""
|
|
23
|
+
typer.echo(
|
|
24
|
+
f"{prefix}agent={version.agent_id} version_id={version.id} "
|
|
25
|
+
f"label={version.version} created={version.created_at.isoformat()}"
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def create_app(execution: ExecutionContext) -> typer.Typer:
|
|
30
|
+
app = typer.Typer(name="agents", help="Register and discover saved Cloud agents.", no_args_is_help=True)
|
|
31
|
+
|
|
32
|
+
@app.command("register")
|
|
33
|
+
def register(
|
|
34
|
+
file: Path = typer.Argument(..., exists=True, file_okay=True, dir_okay=False),
|
|
35
|
+
version: str = typer.Option(..., "--version"),
|
|
36
|
+
name: str | None = typer.Option(None, "--name"),
|
|
37
|
+
) -> None:
|
|
38
|
+
"""Register a reusable agent without starting a job."""
|
|
39
|
+
definition = _load_definition(file)
|
|
40
|
+
|
|
41
|
+
async def operation() -> None:
|
|
42
|
+
async with create_api_client(execution) as client:
|
|
43
|
+
registered = await client.agents.register(definition, version=version, name=name)
|
|
44
|
+
record_agent_selection(execution.profile_name, registered.agent_id, registered.agent_version_id)
|
|
45
|
+
typer.echo(f"Agent ID: {registered.agent_id}")
|
|
46
|
+
typer.echo(f"Version ID: {registered.agent_version_id} ({registered.version})")
|
|
47
|
+
|
|
48
|
+
asyncio.run(operation())
|
|
49
|
+
|
|
50
|
+
@app.command("publish")
|
|
51
|
+
def publish(
|
|
52
|
+
agent_id: UUID = typer.Argument(..., help="Existing saved agent UUID."),
|
|
53
|
+
file: Path = typer.Argument(..., exists=True, file_okay=True, dir_okay=False),
|
|
54
|
+
version: str = typer.Option(..., "--version"),
|
|
55
|
+
) -> None:
|
|
56
|
+
"""Publish an immutable revision to an already registered agent."""
|
|
57
|
+
definition = _load_definition(file)
|
|
58
|
+
|
|
59
|
+
async def operation() -> None:
|
|
60
|
+
async with create_api_client(execution) as client:
|
|
61
|
+
saved = await client.agents.publish(agent_id, definition, version=version)
|
|
62
|
+
record_agent_selection(execution.profile_name, saved.agent_id, saved.id)
|
|
63
|
+
_show_version(saved)
|
|
64
|
+
|
|
65
|
+
asyncio.run(operation())
|
|
66
|
+
|
|
67
|
+
@app.command("list")
|
|
68
|
+
def list_agents(
|
|
69
|
+
include_all: bool = typer.Option(False, "--all", help="Include execution-created and unaudited legacy agents."),
|
|
70
|
+
) -> None:
|
|
71
|
+
"""List owned, explicitly registered agents by default."""
|
|
72
|
+
|
|
73
|
+
async def operation() -> None:
|
|
74
|
+
async with create_api_client(execution) as client:
|
|
75
|
+
offset = 0
|
|
76
|
+
while True:
|
|
77
|
+
page = await client.agents.list(include_execution=include_all, offset=offset, limit=200)
|
|
78
|
+
for agent in page:
|
|
79
|
+
typer.echo(
|
|
80
|
+
f"{agent.id} | {agent.name} | {agent.registration_source.value} "
|
|
81
|
+
f"| created={agent.created_at.isoformat()}"
|
|
82
|
+
)
|
|
83
|
+
if len(page) < 200:
|
|
84
|
+
break
|
|
85
|
+
offset += len(page)
|
|
86
|
+
|
|
87
|
+
asyncio.run(operation())
|
|
88
|
+
|
|
89
|
+
@app.command("versions")
|
|
90
|
+
def versions(
|
|
91
|
+
agent_id: UUID | None = typer.Argument(None, help="Optional agent UUID; omit to enumerate registered agents."),
|
|
92
|
+
include_all: bool = typer.Option(False, "--all", help="Include execution-created and unaudited legacy agents."),
|
|
93
|
+
) -> None:
|
|
94
|
+
"""List immutable versions with complete UUIDs."""
|
|
95
|
+
|
|
96
|
+
async def operation() -> None:
|
|
97
|
+
async with create_api_client(execution) as client:
|
|
98
|
+
if agent_id is not None:
|
|
99
|
+
found = await client.agents.versions(agent_id)
|
|
100
|
+
for version in found:
|
|
101
|
+
_show_version(version)
|
|
102
|
+
if found:
|
|
103
|
+
latest = max(found, key=lambda v: (v.created_at, str(v.id)))
|
|
104
|
+
record_agent_selection(execution.profile_name, agent_id, latest.id)
|
|
105
|
+
else:
|
|
106
|
+
for agent, rows in await client.agents.all_versions(include_execution=include_all):
|
|
107
|
+
for version in rows:
|
|
108
|
+
_show_version(version, name=agent.name)
|
|
109
|
+
|
|
110
|
+
asyncio.run(operation())
|
|
111
|
+
|
|
112
|
+
@app.command("version")
|
|
113
|
+
def show_version(
|
|
114
|
+
agent_id: UUID | None = typer.Argument(None, help="Agent UUID; defaults to last selection."),
|
|
115
|
+
version_id: UUID | None = typer.Argument(None, help="Version UUID; defaults to last selected or newest."),
|
|
116
|
+
) -> None:
|
|
117
|
+
"""Inspect one saved definition without changing the immutable revision."""
|
|
118
|
+
selected = get_agent_selection(execution.profile_name)
|
|
119
|
+
resolved_agent = agent_id or (selected[0] if selected else None)
|
|
120
|
+
if resolved_agent is None:
|
|
121
|
+
raise typer.BadParameter("No selected agent; specify AGENT_ID or register an agent first")
|
|
122
|
+
|
|
123
|
+
async def operation() -> None:
|
|
124
|
+
async with create_api_client(execution) as client:
|
|
125
|
+
if version_id is None and selected and selected[0] == resolved_agent:
|
|
126
|
+
found = await client.agents.version(resolved_agent, selected[1])
|
|
127
|
+
elif version_id is None:
|
|
128
|
+
found = await client.agents.latest_version(resolved_agent)
|
|
129
|
+
else:
|
|
130
|
+
found = await client.agents.version(resolved_agent, version_id)
|
|
131
|
+
record_agent_selection(execution.profile_name, found.agent_id, found.id)
|
|
132
|
+
_show_version(found)
|
|
133
|
+
typer.echo(json.dumps(found.definition, indent=2))
|
|
134
|
+
|
|
135
|
+
asyncio.run(operation())
|
|
136
|
+
|
|
137
|
+
return app
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""API key commands for the Sinapsis CLI."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import re
|
|
5
|
+
from datetime import UTC, datetime, timedelta
|
|
6
|
+
|
|
7
|
+
import typer
|
|
8
|
+
from sinapsis_contracts.cloud_api.models.api_key_permissions import ApiKeyPermission
|
|
9
|
+
|
|
10
|
+
from sinapsis_cli.execution import ExecutionContext, create_api_client, get_execution_context
|
|
11
|
+
|
|
12
|
+
app = typer.Typer(name="api-keys", help="Manage API keys.", no_args_is_help=True)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
_EXPIRATION_DURATION = re.compile(r"(?P<quantity>[1-9][0-9]*)(?P<unit>[dhm])", re.IGNORECASE)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _parse_expiration(value: str | None) -> datetime | None:
|
|
19
|
+
"""Parse a relative duration or an absolute, timezone-aware ISO timestamp.
|
|
20
|
+
|
|
21
|
+
The server remains authoritative for validity and maximum lifetime.
|
|
22
|
+
"""
|
|
23
|
+
if value is None:
|
|
24
|
+
return None # Management's existing 90-day default.
|
|
25
|
+
|
|
26
|
+
value = value.strip()
|
|
27
|
+
match = _EXPIRATION_DURATION.fullmatch(value)
|
|
28
|
+
if match:
|
|
29
|
+
try:
|
|
30
|
+
quantity = int(match.group("quantity"))
|
|
31
|
+
unit = match.group("unit").lower()
|
|
32
|
+
duration = timedelta(seconds=quantity * {"d": 86400, "h": 3600, "m": 60}[unit])
|
|
33
|
+
return datetime.now(UTC) + duration
|
|
34
|
+
except (OverflowError, ValueError):
|
|
35
|
+
raise typer.BadParameter("Expiration duration is too large") from None
|
|
36
|
+
|
|
37
|
+
try:
|
|
38
|
+
expires_at = datetime.fromisoformat(value.replace("Z", "+00:00"))
|
|
39
|
+
except ValueError:
|
|
40
|
+
raise typer.BadParameter(
|
|
41
|
+
"Use a duration such as 7d, 12h, 30m, or an ISO 8601 timestamp "
|
|
42
|
+
"with a timezone (e.g. 2026-12-01T09:00:00+00:00)"
|
|
43
|
+
) from None
|
|
44
|
+
if expires_at.utcoffset() is None:
|
|
45
|
+
raise typer.BadParameter("Absolute expiration must include a timezone offset")
|
|
46
|
+
return expires_at
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@app.command("permissions")
|
|
50
|
+
def permissions() -> None:
|
|
51
|
+
"""List valid --scope values without requiring an API connection."""
|
|
52
|
+
for permission in ApiKeyPermission:
|
|
53
|
+
typer.echo(permission.value)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@app.command()
|
|
57
|
+
def create(
|
|
58
|
+
ctx: typer.Context,
|
|
59
|
+
name: str = typer.Option(..., prompt=True),
|
|
60
|
+
scope: list[str] | None = typer.Option(
|
|
61
|
+
None, "--scope", help="Repeat for each scope, e.g. files:read (see api-keys permissions)."
|
|
62
|
+
),
|
|
63
|
+
agent_id: list[str] | None = typer.Option(None, "--agent-id", help="Allowed agent UUID; repeatable."),
|
|
64
|
+
all_user_agents: bool = typer.Option(False, "--all-user-agents", help="Dynamically allow owned agents."),
|
|
65
|
+
expire: str | None = typer.Option(
|
|
66
|
+
None,
|
|
67
|
+
"--expire",
|
|
68
|
+
help="Expiry: 7d, 12h, 30m, or timezone-aware ISO 8601. Defaults to 90d; max 365d.",
|
|
69
|
+
),
|
|
70
|
+
) -> None:
|
|
71
|
+
"""Create a scoped API key; its secret is shown only once."""
|
|
72
|
+
expires_at = _parse_expiration(expire)
|
|
73
|
+
asyncio.run(
|
|
74
|
+
_create_key(
|
|
75
|
+
get_execution_context(ctx),
|
|
76
|
+
name,
|
|
77
|
+
scope,
|
|
78
|
+
agent_id,
|
|
79
|
+
all_user_agents,
|
|
80
|
+
expires_at=expires_at,
|
|
81
|
+
)
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
async def _create_key(
|
|
86
|
+
execution: ExecutionContext,
|
|
87
|
+
name: str,
|
|
88
|
+
scope: list[str] | None = None,
|
|
89
|
+
agent_id: list[str] | None = None,
|
|
90
|
+
all_user_agents: bool = False,
|
|
91
|
+
*,
|
|
92
|
+
expires_at: datetime | None = None,
|
|
93
|
+
) -> None:
|
|
94
|
+
async with create_api_client(execution) as client:
|
|
95
|
+
response = await client.api_keys.generate(
|
|
96
|
+
name,
|
|
97
|
+
scopes=scope,
|
|
98
|
+
agent_ids=agent_id or [],
|
|
99
|
+
all_user_agents=all_user_agents,
|
|
100
|
+
expires_at=expires_at,
|
|
101
|
+
)
|
|
102
|
+
typer.echo(f"API Key {response.api_key_name!r} created (ID: {response.metadata.id}).")
|
|
103
|
+
expires = response.metadata.expires_at
|
|
104
|
+
typer.echo(f"Expires: {expires.isoformat() if expires else 'not specified'}")
|
|
105
|
+
typer.echo("Copy this secret now; it cannot be retrieved later:")
|
|
106
|
+
typer.echo(response.api_key)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@app.command()
|
|
110
|
+
def copy(ctx: typer.Context, masked_api_key: str = typer.Option(..., prompt=True)) -> None:
|
|
111
|
+
"""Explain why existing API key values cannot be retrieved."""
|
|
112
|
+
asyncio.run(_copy_key(get_execution_context(ctx), masked_api_key))
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
async def _copy_key(execution: ExecutionContext, masked_api_key: str) -> None:
|
|
116
|
+
del execution, masked_api_key
|
|
117
|
+
typer.echo(
|
|
118
|
+
"API key values are only shown at creation. Create a replacement key and revoke the old one.",
|
|
119
|
+
err=True,
|
|
120
|
+
)
|
|
121
|
+
raise typer.Exit(code=1)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@app.command("list")
|
|
125
|
+
def list_keys(ctx: typer.Context) -> None:
|
|
126
|
+
"""List active API keys."""
|
|
127
|
+
asyncio.run(_list_keys(get_execution_context(ctx)))
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
async def _list_keys(execution: ExecutionContext) -> None:
|
|
131
|
+
async with create_api_client(execution) as client:
|
|
132
|
+
key_list = await client.api_keys.list_all()
|
|
133
|
+
|
|
134
|
+
for key in key_list.keys:
|
|
135
|
+
expires = key.expires_at.isoformat() if key.expires_at else "not specified"
|
|
136
|
+
typer.echo(
|
|
137
|
+
f"Name: {key.name} ID: {key.id} Key: {key.api_key} "
|
|
138
|
+
f"Scopes: {', '.join(key.scopes)} Expires: {expires}"
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@app.command()
|
|
143
|
+
def delete(ctx: typer.Context, key_id: str = typer.Option(..., prompt=True)) -> None:
|
|
144
|
+
"""Delete an API key."""
|
|
145
|
+
if not typer.confirm(f"Delete API key {key_id!r}?"):
|
|
146
|
+
raise typer.Abort()
|
|
147
|
+
|
|
148
|
+
asyncio.run(_delete(get_execution_context(ctx), key_id))
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
async def _delete(execution: ExecutionContext, key_id: str) -> None:
|
|
152
|
+
async with create_api_client(execution) as client:
|
|
153
|
+
await client.api_keys.delete(key_id)
|
|
154
|
+
|
|
155
|
+
typer.echo(f"Deleted API key: {key_id}")
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""File commands 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, create_api_client, get_execution_context
|
|
9
|
+
|
|
10
|
+
app = typer.Typer(name="files", help="Manage files.", no_args_is_help=True)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@app.command()
|
|
14
|
+
def upload(
|
|
15
|
+
ctx: typer.Context,
|
|
16
|
+
file: Path = typer.Argument(
|
|
17
|
+
...,
|
|
18
|
+
exists=True,
|
|
19
|
+
file_okay=True,
|
|
20
|
+
dir_okay=False,
|
|
21
|
+
readable=True,
|
|
22
|
+
help="Path to the file to upload.",
|
|
23
|
+
),
|
|
24
|
+
) -> None:
|
|
25
|
+
"""Upload a file to the selected Sinapsis API profile."""
|
|
26
|
+
asyncio.run(_upload(get_execution_context(ctx), file))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
async def _upload(execution: ExecutionContext, file: Path) -> None:
|
|
30
|
+
async with create_api_client(execution) as client:
|
|
31
|
+
result = await client.files.upload(file)
|
|
32
|
+
|
|
33
|
+
typer.echo(result)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@app.command()
|
|
37
|
+
def get(
|
|
38
|
+
ctx: typer.Context,
|
|
39
|
+
path: str = typer.Argument(..., help="ID of the file to fetch."),
|
|
40
|
+
output: Path = typer.Option(
|
|
41
|
+
...,
|
|
42
|
+
"--output",
|
|
43
|
+
"-o",
|
|
44
|
+
exists=False,
|
|
45
|
+
file_okay=True,
|
|
46
|
+
dir_okay=False,
|
|
47
|
+
writable=True,
|
|
48
|
+
help="Path where the downloaded file will be saved.",
|
|
49
|
+
),
|
|
50
|
+
) -> None:
|
|
51
|
+
"""Fetch a file from the selected Sinapsis API profile."""
|
|
52
|
+
asyncio.run(_get(get_execution_context(ctx), path, output))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
async def _get(execution: ExecutionContext, path: str, output: Path) -> None:
|
|
56
|
+
async with create_api_client(execution) as client:
|
|
57
|
+
content = await client.files.get(path)
|
|
58
|
+
|
|
59
|
+
output.write_bytes(content)
|
|
60
|
+
typer.echo(f"File saved to {output}")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@app.command("list")
|
|
64
|
+
def list_files(ctx: typer.Context, root_path: Path = typer.Argument(default="/", help="Root path.")) -> None:
|
|
65
|
+
"""List files for the selected API profile."""
|
|
66
|
+
asyncio.run(_list_files(get_execution_context(ctx), root_path))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
async def _list_files(execution: ExecutionContext, root_path: Path) -> None:
|
|
70
|
+
async with create_api_client(execution) as client:
|
|
71
|
+
result = await client.files.list(str(root_path))
|
|
72
|
+
|
|
73
|
+
_print_files(result)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _print_files(result, key_name: str = "entries") -> None:
|
|
77
|
+
for file_entry in result.get(key_name, []):
|
|
78
|
+
typer.echo()
|
|
79
|
+
typer.echo(f"File path: {file_entry.get('path')}")
|
|
80
|
+
typer.echo(f"File size: {file_entry.get('size')}")
|
|
81
|
+
typer.echo(f"Last modified: {file_entry.get('last_modified')}")
|
|
82
|
+
if version := file_entry.get("version"):
|
|
83
|
+
typer.echo(f"Version: {version}")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@app.command()
|
|
87
|
+
def fetch(
|
|
88
|
+
ctx: typer.Context,
|
|
89
|
+
agent_id: str = typer.Argument(..., help="ID for the agent that produced the staging file."),
|
|
90
|
+
file_id: str = typer.Argument(..., help="ID of the file to fetch."),
|
|
91
|
+
output: Path = typer.Option(
|
|
92
|
+
...,
|
|
93
|
+
"--output",
|
|
94
|
+
"-o",
|
|
95
|
+
exists=False,
|
|
96
|
+
file_okay=True,
|
|
97
|
+
dir_okay=False,
|
|
98
|
+
writable=True,
|
|
99
|
+
help="Path where the downloaded file will be saved.",
|
|
100
|
+
),
|
|
101
|
+
) -> None:
|
|
102
|
+
"""Fetch an agent staging file."""
|
|
103
|
+
asyncio.run(_get_staging_file(agent_id, file_id, output, execution=get_execution_context(ctx)))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
async def _get_staging_file(
|
|
107
|
+
agent_id: str,
|
|
108
|
+
file_id: str,
|
|
109
|
+
output: Path,
|
|
110
|
+
*,
|
|
111
|
+
execution: ExecutionContext,
|
|
112
|
+
api_url: str | None = None,
|
|
113
|
+
) -> None:
|
|
114
|
+
"""Fetch an agent staging file from a profile API."""
|
|
115
|
+
async with create_api_client(execution, api_url=api_url) as client:
|
|
116
|
+
content = await client.files.fetch(agent_id, file_id)
|
|
117
|
+
|
|
118
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
119
|
+
output.write_bytes(content)
|
|
120
|
+
typer.echo(f"File saved to {output}")
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@app.command()
|
|
124
|
+
def search(ctx: typer.Context, file_name: str = typer.Argument(..., help="Name of the file to search for.")) -> None:
|
|
125
|
+
"""Search for a file in the selected API profile."""
|
|
126
|
+
asyncio.run(_search_file(get_execution_context(ctx), file_name))
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
async def _search_file(execution: ExecutionContext, file_name: str) -> None:
|
|
130
|
+
async with create_api_client(execution) as client:
|
|
131
|
+
content = await client.files.search(file_name)
|
|
132
|
+
|
|
133
|
+
typer.echo()
|
|
134
|
+
typer.echo(f"File search: {content.get('query')}")
|
|
135
|
+
_print_files(content, "matches")
|