Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/quickstart/own-2026-11-21/own-ending.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
188 changes: 187 additions & 1 deletion openadapt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,38 @@ def __getattr__(self, name):
return getattr(self._stream, name)


def _is_login(argv: list[str]) -> bool:
"""``login`` or ``setup login``, not its help."""
if argv[:1] == ["login"]:
rest = argv[1:]
elif argv[:2] == ["setup", "login"]:
rest = argv[2:]
else:
return False
return not any(token in ("-h", "--help") for token in rest)


def _option_values(argv: list[str], name: str) -> Optional[list[str]]:
"""Every value of a repeatable ``--name VALUE`` option, or None."""
values = []
for index, token in enumerate(argv):
if token == name and index + 1 < len(argv):
values.append(argv[index + 1])
elif token.startswith(name + "="):
values.append(token[len(name) + 1 :])
return values or None


def _option_value(argv: list[str], name: str) -> Optional[str]:
"""The value of ``--name VALUE`` or ``--name=VALUE`` in ``argv``, if any."""
for index, token in enumerate(argv):
if token == name and index + 1 < len(argv):
return argv[index + 1]
if token.startswith(name + "="):
return token[len(name) + 1 :]
return None


def _invoke_flow(argv: list[str]) -> int:
"""Invoke the canonical engine once and return its exit code."""
if not _python_supported():
Expand All @@ -368,6 +400,17 @@ def _invoke_flow(argv: list[str]) -> int:
alias_context = suppress_alias_notices()
with alias_context:
_correct_released_flow_x11_sonames()
if _is_login(argv):
code = int(flow_main(argv))
if code == 0:
from openadapt.seal_claim import after_login

after_login(
_option_value(argv, "--host"),
destination_kind=_option_value(argv, "--destination-kind"),
trusted_hosts=_option_values(argv, "--trusted-host"),
)
return code
if argv[:1] != ["tutorial"]:
return int(flow_main(argv))
# The tutorial is the first-run path and ends by printing next steps.
Expand Down Expand Up @@ -709,6 +752,39 @@ def _quickstart_root(out: Optional[Path]) -> Path:
return root


def _quickstart_own(root: Path, url: Optional[str], watch: bool) -> None:
"""`openadapt quickstart --own`: see openadapt/quickstart/own.py."""
from functools import partial

from openadapt.quickstart.engine import display_available
from openadapt.quickstart.own import OwnTaskError, run_own, share_runs

def ask(prompt: str) -> str:
return input(prompt)

from contextlib import nullcontext

context = _synthetic_scrub_off() if url is None else nullcontext()
try:
with context:
run_own(
root,
url=url,
visible=_watch_window(watch),
can_record_here=sys.stdin.isatty() and display_available(),
ask=ask,
say=click.echo,
hint=_hint,
quote=_shell_quote,
share=partial(share_runs, ask=ask, say=click.echo, command=_hint),
)
except OwnTaskError as error:
raise click.ClickException(str(error)) from error
except KeyboardInterrupt:
click.echo(f"\nStopped. Anything written so far is in {root}", err=True)
raise click.exceptions.Exit(130)


@main.command(
"quickstart",
context_settings={
Expand Down Expand Up @@ -775,6 +851,19 @@ def _quickstart_root(out: Optional[Path]) -> Path:
# --headed is the older spelling of --watch. --headless was the opt-out when
# the window showed by default; hidden is the default now, so it changes
# nothing and is kept so older commands still run.
@click.option(
"--own",
is_flag=True,
help=(
"Your own task: record it once (in the sample app, or your web app "
"with --url), run it with new inputs, and end with an agent tool."
),
)
@click.option(
"--url",
default=None,
help="With --own: your web app's address. Recording opens a browser window.",
)
@click.option("--headed", is_flag=True, hidden=True)
@click.option("--headless", is_flag=True, hidden=True)
@click.option("--break-it", "break_it", is_flag=True, hidden=True)
Expand All @@ -788,6 +877,8 @@ def quickstart(
no_break: bool,
verbose: bool,
more: bool,
own: bool,
url: Optional[str],
headed: bool,
headless: bool,
break_it: bool,
Expand All @@ -814,6 +905,11 @@ def quickstart(
_quickstart_more(out, app)
return
_require_supported_python()
if url is not None and not own:
raise click.UsageError("--url works with --own.")
if own:
_quickstart_own(_quickstart_root(out), url, watch or headed)
return
_refuse_unknown_tutorial_options(command_ctx.args)
openemr = _openemr_checkout(command_ctx.args) if app == "openemr" else None
root = _quickstart_root(out)
Expand Down Expand Up @@ -1533,6 +1629,26 @@ def flow(command_ctx: click.Context) -> None:
)


@main.command(
"login",
context_settings={
"ignore_unknown_options": True,
"allow_extra_args": True,
"help_option_names": [],
},
)
@click.pass_context
def login(command_ctx: click.Context) -> None:
"""Sign in to OpenAdapt Cloud: `openadapt login` (same as `setup login`).

Every argument passes to `openadapt-flow login` unchanged. After a
successful sign-in, Seals this computer published are claimed into your
workspace when Cloud supports it.
"""
_require_supported_python()
_run_flow(["login", *command_ctx.args])


@main.command(
"pull",
context_settings={
Expand Down Expand Up @@ -2024,7 +2140,8 @@ def seal(command_ctx: click.Context) -> None:
[RUN_DIR]` uploads the run's public Seal (counts, outcomes and digests
only) after you say yes and prints its link; `openadapt seal unpublish
RUN_DIR` takes it down, and `openadapt seal verify <link>` checks a hosted
Seal's signature here. Also `openadapt seal keys pin` to pin Cloud's Seal
Seal's signature here. `openadapt seal claim` puts the Seals you published
in your Cloud workspace after `openadapt login` (login does it for you). Also `openadapt seal keys pin` to pin Cloud's Seal
keys after a person confirms them. Every other argument passes to
`openadapt-agent seal` unchanged.
"""
Expand All @@ -2036,6 +2153,8 @@ def seal(command_ctx: click.Context) -> None:
sys.exit(_publish_seal(args[1:]))
if args[:1] == ["unpublish"]:
sys.exit(_unpublish_seal(args[1:]))
if args[:1] == ["claim"]:
sys.exit(_claim_seals(args[1:]))
if len(args) >= 2 and args[0] == "verify" and _is_seal_link(args[1]):
sys.exit(_verify_seal_link(args[1], args[2:]))
if len(args) >= 2 and args[0] == "verify" and Path(args[1]).is_dir():
Expand Down Expand Up @@ -2254,6 +2373,73 @@ def _publish_seal(argv: list[str]) -> int:
return 0


def _claim_seals(argv: list[str]) -> int:
"""`openadapt seal claim [RUN_DIR...]`: claim published Seals into your workspace."""
from openadapt.seal_claim import claim_published, claims_live
from openadapt.seal_publish import host_of, urllib_transport

parser = argparse.ArgumentParser(
prog="openadapt seal claim",
description=(
"Claim the Seals this computer published into the Cloud workspace "
"you signed in to with `openadapt login`, so they show on the "
"dashboard. The proof is a signature by this computer's Seal key; "
"the delete token is never sent. `openadapt login` does this for "
"you. Name run folders (or a quickstart folder) to include Seals "
"published by an older openadapt."
),
allow_abbrev=False,
)
parser.add_argument(
"run_dirs", nargs="*", type=Path, help="published run folders to include"
)
parser.add_argument("--host", default=None, help="the Cloud you signed in to")
parser.add_argument(
"--destination-kind",
default=None,
help="as for `openadapt login`: the trust class of --host",
)
parser.add_argument(
"--trusted-host",
action="append",
default=None,
help="as for `openadapt login`: an allowlisted customer origin",
)
options = parser.parse_args(argv)
try:
from openadapt_flow.hosted import HostedError, resolve_host, resolve_token
except ImportError:
click.echo("This openadapt-flow cannot sign in to Cloud.", err=True)
return 1
from openadapt.seal_claim import check_destination

try:
origin = resolve_host(options.host)
check_destination(
origin,
destination_kind=options.destination_kind,
trusted_hosts=options.trusted_host,
)
token = resolve_token(None, host=origin)
except HostedError as exc:
click.echo(f"Not claimed: {exc}", err=True)
click.echo(f"Sign in first: {_hint('login')}", err=True)
return 1
if not claims_live(origin, transport=urllib_transport):
click.echo(f"{host_of(origin)} does not take Seal claims yet.", err=True)
return 1
try:
return claim_published(
origin,
token,
run_dirs=[p.expanduser() for p in options.run_dirs],
transport=urllib_transport,
)
except Exception as exc: # noqa: BLE001 - e.g. no Seal key on this computer
click.echo(f"Not claimed: {type(exc).__name__}: {exc}", err=True)
return 1


def _unpublish_seal(argv: list[str]) -> int:
"""`openadapt seal unpublish RUN_DIR`: delete the hosted Seal."""
from openadapt.seal_publish import (
Expand Down
Loading
Loading