redberry-cli 0.1.0__tar.gz → 0.3.0__tar.gz

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.
Files changed (34) hide show
  1. {redberry_cli-0.1.0/src/redberry_cli.egg-info → redberry_cli-0.3.0}/PKG-INFO +31 -4
  2. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/README.md +30 -3
  3. redberry_cli-0.3.0/src/redberry_cli/_channel.py +6 -0
  4. redberry_cli-0.3.0/src/redberry_cli/_version.py +1 -0
  5. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli/main.py +179 -16
  6. redberry_cli-0.3.0/src/redberry_cli/upgrade.py +184 -0
  7. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli/vercel.py +15 -2
  8. {redberry_cli-0.1.0 → redberry_cli-0.3.0/src/redberry_cli.egg-info}/PKG-INFO +31 -4
  9. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli.egg-info/SOURCES.txt +3 -0
  10. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/tests/test_cli.py +23 -5
  11. redberry_cli-0.3.0/tests/test_cli_extract.py +253 -0
  12. redberry_cli-0.3.0/tests/test_cli_upgrade.py +190 -0
  13. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/tests/test_cli_vercel.py +14 -0
  14. redberry_cli-0.1.0/src/redberry_cli/_channel.py +0 -5
  15. redberry_cli-0.1.0/src/redberry_cli/_version.py +0 -1
  16. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/LICENSE +0 -0
  17. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/NOTICE +0 -0
  18. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/pyproject.toml +0 -0
  19. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/setup.cfg +0 -0
  20. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli/__init__.py +0 -0
  21. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli/_cloud.py +0 -0
  22. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli/_edits.py +0 -0
  23. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli/_scan.py +0 -0
  24. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli/_ts_tools.py +0 -0
  25. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli/aws_lambda.py +0 -0
  26. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli/azure_functions.py +0 -0
  27. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli/instrument.py +0 -0
  28. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli.egg-info/dependency_links.txt +0 -0
  29. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli.egg-info/entry_points.txt +0 -0
  30. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli.egg-info/requires.txt +0 -0
  31. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/src/redberry_cli.egg-info/top_level.txt +0 -0
  32. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/tests/test_cli_aws.py +0 -0
  33. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/tests/test_cli_azure.py +0 -0
  34. {redberry_cli-0.1.0 → redberry_cli-0.3.0}/tests/test_scan.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: redberry-cli
3
- Version: 0.1.0
3
+ Version: 0.3.0
4
4
  Summary: The redberry command: sign in to Redberry and wire an agent's code to send its spans to your Redberry install.
5
5
  Author: Redberry Labs
6
6
  License-Expression: LicenseRef-PolyForm-Shield-1.0.0
@@ -51,8 +51,10 @@ or `pipx install redberry-cli`. Python 3.11 or later.
51
51
  | `redberry connect --platform aws-lambda` | Wires a Node.js or Python Lambda function (below). |
52
52
  | `redberry connect --platform azure-functions` | Wires a Node.js (v4) or Python (v2) Azure function app (below). |
53
53
  | `redberry connect --platform vercel` | Wires a Next.js app on the Vercel AI SDK, and sets the project's `REDBERRY_*` variables (below). |
54
+ | `redberry upgrade` | Your company's Redberry installs and whether each runs the latest release; `redberry upgrade <id>` upgrades one (below). |
54
55
 
55
- The API is `https://api.redberry.sbs`; `--api` or `REDBERRY_API_URL` selects another. `--json` prints a
56
+ The API is `https://api.redberry.sbs`; `--api` or `REDBERRY_API_URL` selects another. Against any API other than
57
+ production, every command that calls it first says so on stderr (`Redberry dev (…)` or `Redberry custom (…)`). `--json` prints a
56
58
  command's result for scripts.
57
59
 
58
60
  ## What `connect --platform` does
@@ -110,8 +112,33 @@ to the form above when you run `connect` again.
110
112
  An app connected by an earlier version with the in-process hasher gets the new `instrumentation.ts`; remove the
111
113
  old variables it set (`REDBERRY_SIGNING_KEY`, `REDBERRY_PROJECT_TOKEN` and the others) from the project.
112
114
 
113
- `--extract` writes the agent's manifest (`.redberry/manifest.json`) with `redberry-extract` when that tool is
114
- installed. It is not part of this release; without it, write the manifest by hand.
115
+ ## `connect --extract`: the agent's manifest
116
+
117
+ `--extract` writes the agent's manifest (`.redberry/manifest.json`) with `redberry-extract` and the LLM key already
118
+ in your environment (`ANTHROPIC_API_KEY`, else `OPENAI_API_KEY`). The agent's code is sent only to that key's
119
+ provider. Without `--extract`, `connect` asks first.
120
+
121
+ A `redberry-extract` on PATH is used as it is. Otherwise `connect` installs it:
122
+
123
+ - It needs `redberry login` (the extractor is licensed to your company) and
124
+ [uv](https://docs.astral.sh/uv/) (`curl -LsSf https://astral.sh/uv/install.sh | sh` or `pipx install uv`).
125
+ - The Redberry Extractor Licence is shown and accepted once per company, and again when a new version is
126
+ published. `--yes` does not accept it; in a script or with `--json`, pass `--accept-extractor-licence`.
127
+ - It is installed with uv into its own environment in `~/.redberry/extractor/<version>/` (`$REDBERRY_HOME`
128
+ overrides `~/.redberry`), from Redberry's package index with a short-lived download token that is passed to uv
129
+ in its environment only. Later runs reuse that install.
130
+
131
+ ## `upgrade`: take Redberry's latest release
132
+
133
+ `redberry upgrade` lists your company's installs, the release each last reported, and whether a newer one is
134
+ out. `redberry upgrade <id>` upgrades one: it fetches the cloud's install script from the Redberry portal and runs
135
+ it on this machine with `--upgrade`, through the cloud's CLI as you are signed in to it (`gcloud`, `aws` or `az`;
136
+ on AWS, signed in to the install's account). The script keeps every choice the install was made with, asks
137
+ nothing and needs no install token, then reports the new release to Redberry. It is the same command the portal
138
+ shows, which you can also paste into the cloud's own shell.
139
+
140
+ It asks before it runs (`--yes` skips the question). An install that is already up to date is left alone unless
141
+ you pass `--force`. A stopped or revoked install is not upgraded: install it again from the portal.
115
142
 
116
143
  ## License
117
144
 
@@ -24,8 +24,10 @@ or `pipx install redberry-cli`. Python 3.11 or later.
24
24
  | `redberry connect --platform aws-lambda` | Wires a Node.js or Python Lambda function (below). |
25
25
  | `redberry connect --platform azure-functions` | Wires a Node.js (v4) or Python (v2) Azure function app (below). |
26
26
  | `redberry connect --platform vercel` | Wires a Next.js app on the Vercel AI SDK, and sets the project's `REDBERRY_*` variables (below). |
27
+ | `redberry upgrade` | Your company's Redberry installs and whether each runs the latest release; `redberry upgrade <id>` upgrades one (below). |
27
28
 
28
- The API is `https://api.redberry.sbs`; `--api` or `REDBERRY_API_URL` selects another. `--json` prints a
29
+ The API is `https://api.redberry.sbs`; `--api` or `REDBERRY_API_URL` selects another. Against any API other than
30
+ production, every command that calls it first says so on stderr (`Redberry dev (…)` or `Redberry custom (…)`). `--json` prints a
29
31
  command's result for scripts.
30
32
 
31
33
  ## What `connect --platform` does
@@ -83,8 +85,33 @@ to the form above when you run `connect` again.
83
85
  An app connected by an earlier version with the in-process hasher gets the new `instrumentation.ts`; remove the
84
86
  old variables it set (`REDBERRY_SIGNING_KEY`, `REDBERRY_PROJECT_TOKEN` and the others) from the project.
85
87
 
86
- `--extract` writes the agent's manifest (`.redberry/manifest.json`) with `redberry-extract` when that tool is
87
- installed. It is not part of this release; without it, write the manifest by hand.
88
+ ## `connect --extract`: the agent's manifest
89
+
90
+ `--extract` writes the agent's manifest (`.redberry/manifest.json`) with `redberry-extract` and the LLM key already
91
+ in your environment (`ANTHROPIC_API_KEY`, else `OPENAI_API_KEY`). The agent's code is sent only to that key's
92
+ provider. Without `--extract`, `connect` asks first.
93
+
94
+ A `redberry-extract` on PATH is used as it is. Otherwise `connect` installs it:
95
+
96
+ - It needs `redberry login` (the extractor is licensed to your company) and
97
+ [uv](https://docs.astral.sh/uv/) (`curl -LsSf https://astral.sh/uv/install.sh | sh` or `pipx install uv`).
98
+ - The Redberry Extractor Licence is shown and accepted once per company, and again when a new version is
99
+ published. `--yes` does not accept it; in a script or with `--json`, pass `--accept-extractor-licence`.
100
+ - It is installed with uv into its own environment in `~/.redberry/extractor/<version>/` (`$REDBERRY_HOME`
101
+ overrides `~/.redberry`), from Redberry's package index with a short-lived download token that is passed to uv
102
+ in its environment only. Later runs reuse that install.
103
+
104
+ ## `upgrade`: take Redberry's latest release
105
+
106
+ `redberry upgrade` lists your company's installs, the release each last reported, and whether a newer one is
107
+ out. `redberry upgrade <id>` upgrades one: it fetches the cloud's install script from the Redberry portal and runs
108
+ it on this machine with `--upgrade`, through the cloud's CLI as you are signed in to it (`gcloud`, `aws` or `az`;
109
+ on AWS, signed in to the install's account). The script keeps every choice the install was made with, asks
110
+ nothing and needs no install token, then reports the new release to Redberry. It is the same command the portal
111
+ shows, which you can also paste into the cloud's own shell.
112
+
113
+ It asks before it runs (`--yes` skips the question). An install that is already up to date is left alone unless
114
+ you pass `--force`. A stopped or revoked install is not upgraded: install it again from the portal.
88
115
 
89
116
  ## License
90
117
 
@@ -0,0 +1,6 @@
1
+ """Which Redberry API this build of the CLI talks to by default, and the known environments by address, so every
2
+ command says which one it uses. REDBERRY_API_URL and --api override the default."""
3
+
4
+ CHANNEL = "prod"
5
+
6
+ APIS = {"prod": "https://api.redberry.sbs", "dev": "https://api-dev.redberry.sbs"}
@@ -0,0 +1 @@
1
+ __version__ = "0.3.0"
@@ -1,4 +1,5 @@
1
- """``redberry login | whoami | logout | connect``: sign in, then wire an agent to the customer's Redberry install.
1
+ """``redberry login | whoami | logout | connect | upgrade``: sign in, then wire an agent to the customer's Redberry
2
+ install.
2
3
 
3
4
  **login** — a device-code sign-in, as ``gh auth login`` does it. The CLI prints a short code and a link;
4
5
  someone signed in to the Redberry portal for your company opens it and approves the code (it need
@@ -22,11 +23,15 @@ serverless platforms:
22
23
 
23
24
  Each shows the diff and asks before it changes a file (``--yes`` applies without asking), sets
24
25
  ``temperature`` 1 on each model call it finds (pricing needs exactly 1), and never deploys.
25
- ``--extract`` writes the agent's manifest (``.redberry/manifest.json``) with ``redberry-extract`` when that
26
- is installed.
26
+ ``--extract`` writes the agent's manifest (``.redberry/manifest.json``) with ``redberry-extract``: the one on
27
+ PATH, else one installed with uv into ``~/.redberry/extractor`` once the company's extractor licence is accepted.
27
28
 
28
- The API is ``$REDBERRY_API_URL`` or ``--api`` (default ``https://api.redberry.sbs``). Every prompt has a
29
- flag (``--name``, ``--yes``), and ``--json`` prints the result for scripts and coding assistants.
29
+ **upgrade** — the company's installs and whether each runs Redberry's latest release; with an id, upgrades that
30
+ install by running its cloud's install script here with ``--upgrade`` (``redberry_cli.upgrade``).
31
+
32
+ The API is ``$REDBERRY_API_URL`` or ``--api`` (default ``https://api.redberry.sbs``). Off production, every command
33
+ that calls it first says so on stderr (``Redberry dev (…)`` or ``Redberry custom (…)``). Every prompt has a flag
34
+ (``--name``, ``--yes``), and ``--json`` prints the result for scripts and coding assistants.
30
35
  """
31
36
 
32
37
  from __future__ import annotations
@@ -101,6 +106,12 @@ def session(api: str) -> dict[str, Any]:
101
106
  return s
102
107
 
103
108
 
109
+ def environment(api: str) -> str:
110
+ """``prod``, ``dev``, or ``custom`` for any other address."""
111
+ api = api.rstrip("/")
112
+ return next((name for name, url in _channel.APIS.items() if url == api), "custom")
113
+
114
+
104
115
  def _detail(body: dict[str, Any]) -> str:
105
116
  return str(body.get("detail") or body.get("error") or body)
106
117
 
@@ -116,7 +127,6 @@ def _cli_version() -> str:
116
127
  def cmd_login(a, out=print, sleep=time.sleep) -> int:
117
128
  api = a.api.rstrip("/")
118
129
  machine = _machine_name()
119
- out(f"Redberry: {api}")
120
130
  status, start = call(api, "POST", "/api/cli/device/start", {"machine": machine} if machine else {})
121
131
  if status == 404:
122
132
  raise CliError(f"{api} doesn't support `redberry login`: check --api / REDBERRY_API_URL")
@@ -235,20 +245,22 @@ def cmd_connect(a, out=print, run=None) -> int:
235
245
  return 0
236
246
 
237
247
 
248
+ def cmd_upgrade(a, out=print, run=None) -> int:
249
+ from redberry_cli import upgrade # the install script does the work, in the customer's cloud
250
+
251
+ return upgrade.cmd_upgrade(a, out=out, run=run)
252
+
253
+
238
254
  _EXTRACT_BACKENDS = (("ANTHROPIC_API_KEY", "Anthropic", []),
239
255
  ("OPENAI_API_KEY", "OpenAI", ["--backend", "openai_compatible", "--base-url",
240
256
  "https://api.openai.com/v1", "--model", "gpt-4.1-mini"]))
241
257
 
242
258
 
243
259
  def _extract_manifest(a, repo: Path, manifest: Path, name: str, out, run) -> None:
244
- """No manifest: write one with redberry-extract and the LLM key already in the environment, when
245
- redberry-extract is installed. It shows the agent's code to that provider, so this asks unless --extract
246
- said yes."""
260
+ """No manifest: write one with redberry-extract and the LLM key already in the environment. It shows the
261
+ agent's code to that provider, so this asks unless --extract said yes. ``redberry-extract`` on PATH is used
262
+ as it is; otherwise it is installed for this machine (``_install_extractor``)."""
247
263
  _require_agent_code(repo)
248
- if not shutil.which("redberry-extract"):
249
- raise CliError(f"no manifest at {manifest}, and the extractor (redberry-extract) is not available in this "
250
- "release. Write the manifest by hand; the tools and model calls connect's own scan finds "
251
- "(redberry_cli._scan) are the place to start.")
252
264
  found = next(((env, who, extra) for env, who, extra in _EXTRACT_BACKENDS if os.environ.get(env)), None)
253
265
  how = f"redberry-extract --repo {repo} --agent-id {name} (needs ANTHROPIC_API_KEY or OPENAI_API_KEY)"
254
266
  if found is None:
@@ -261,12 +273,152 @@ def _extract_manifest(a, repo: Path, manifest: Path, name: str, out, run) -> Non
261
273
  answer = input(f"No manifest yet. Describe this agent with your {env}? Its code is sent to {who}. [y/N] ")
262
274
  if answer.strip().lower() not in ("y", "yes"):
263
275
  raise CliError(f"no manifest. Write it yourself: {how}")
276
+ extractor = "redberry-extract" if shutil.which("redberry-extract") else str(_install_extractor(a, out, run))
264
277
  out(f"Describing the agent with {who} (redberry-extract) ...")
265
- cmd = ["redberry-extract", "--repo", str(repo), "--agent-id", name, "--output", str(manifest), *extra]
278
+ cmd = [extractor, "--repo", str(repo), "--agent-id", name, "--output", str(manifest), *extra]
266
279
  if run(cmd, cwd=repo).returncode != 0 or not manifest.exists():
267
280
  raise CliError(f"redberry-extract did not write {manifest}; run it yourself: {how}")
268
281
 
269
282
 
283
+ # ── the extractor: licence, download token, install with uv ─────────────────────────────────
284
+
285
+ #: The private index's name for uv: its credential is read from ``UV_INDEX_<NAME>_USERNAME`` / ``_PASSWORD``.
286
+ EXTRACTOR_INDEX = "redberry-extractor"
287
+ _INDEX_ENV = "UV_INDEX_" + EXTRACTOR_INDEX.upper().replace("-", "_")
288
+ _INSTALLED = ".redberry-installed"
289
+
290
+
291
+ def _redberry_home() -> Path:
292
+ return Path(os.environ.get("REDBERRY_HOME") or Path.home() / ".redberry")
293
+
294
+
295
+ def _extractor_root() -> Path:
296
+ return _redberry_home() / "extractor"
297
+
298
+
299
+ def _extractor_exe(venv: Path) -> Path:
300
+ if os.name == "nt":
301
+ return venv / "Scripts" / "redberry-extract.exe"
302
+ return venv / "bin" / "redberry-extract"
303
+
304
+
305
+ def _installed_extractor() -> Path | None:
306
+ """The install recorded as current, when it is complete."""
307
+ root = _extractor_root()
308
+ with contextlib.suppress(OSError):
309
+ venv = root / (root / "current").read_text(encoding="utf-8").strip()
310
+ if venv.parent == root and (venv / _INSTALLED).exists() and _extractor_exe(venv).exists():
311
+ return venv
312
+ return None
313
+
314
+
315
+ def _install_extractor(a, out, run) -> Path:
316
+ """redberry-extract for this machine: after the company's extractor licence is accepted, installed with uv
317
+ from Redberry's private index into ``~/.redberry/extractor/<version>/`` (``$REDBERRY_HOME``), or the
318
+ install already there. The download token is short-lived and reaches uv only in its environment."""
319
+ try:
320
+ s = session(a.api)
321
+ except CliError:
322
+ raise CliError("the extractor is installed for your company: sign in first with `redberry login`, "
323
+ "then run connect again") from None
324
+ _accept_licence(a, s, out)
325
+ venv = _installed_extractor()
326
+ if venv is not None:
327
+ return _extractor_exe(venv)
328
+ uv = shutil.which("uv")
329
+ if not uv:
330
+ raise CliError("installing the extractor needs uv: `curl -LsSf https://astral.sh/uv/install.sh | sh` "
331
+ "or `pipx install uv`, then run connect again")
332
+ status, grant = call(a.api, "POST", "/api/cli/extractor-token", token=s["token"])
333
+ if status != 200:
334
+ raise CliError(_token_error(status, grant))
335
+ secret = grant["access_token"]
336
+ pin = str(grant.get("version_pin") or "").strip()
337
+ spec = "" if not pin else pin if pin[0] in "=<>!~" else f"=={pin}"
338
+ version = "".join(c for c in pin.lstrip("=<>!~ ") if c.isalnum() or c in ".-_+") or "latest"
339
+ root = _extractor_root()
340
+ venv = root / version
341
+ shutil.rmtree(venv, ignore_errors=True) # an earlier install that did not finish
342
+ root.mkdir(parents=True, exist_ok=True)
343
+ out(f"Installing the Redberry extractor ({version}) into {venv} ...")
344
+ python = f"{sys.version_info.major}.{sys.version_info.minor}"
345
+ plain = {k: v for k, v in os.environ.items() if not k.startswith(_INDEX_ENV)}
346
+ # The token goes to uv in its environment only: never in argv (visible to other users in ps), a file or a log.
347
+ authed = {**plain, f"{_INDEX_ENV}_USERNAME": grant.get("username") or "oauth2accesstoken",
348
+ f"{_INDEX_ENV}_PASSWORD": secret}
349
+ private = f"{EXTRACTOR_INDEX}={grant['index_url']}"
350
+ steps = [
351
+ ([uv, "venv", str(venv), "--python", python], plain),
352
+ # the extractor itself from the private index alone, so a package of the same name elsewhere never wins
353
+ ([uv, "pip", "install", "--python", str(venv), "--no-deps", "--default-index", private,
354
+ f"redberry-extractor{spec}"], authed),
355
+ # then its dependencies: the private index first, PyPI as the default
356
+ ([uv, "pip", "install", "--python", str(venv), "--index-strategy", "first-index", "--index", private,
357
+ f"redberry-extractor[llm]{spec}"], authed),
358
+ ]
359
+ for cmd, env in steps:
360
+ try:
361
+ r = run(cmd, capture_output=True, text=True, env=env)
362
+ except OSError as e:
363
+ shutil.rmtree(venv, ignore_errors=True)
364
+ raise CliError(f"could not run uv: {str(e).replace(secret, '***')}") from None
365
+ if r.returncode != 0:
366
+ shutil.rmtree(venv, ignore_errors=True)
367
+ detail = ((r.stderr or "") + (r.stdout or "")).replace(secret, "***").strip()[-2000:]
368
+ raise CliError(f"installing the extractor failed (`{' '.join(cmd[1:3])}`): {detail}")
369
+ if not _extractor_exe(venv).exists():
370
+ shutil.rmtree(venv, ignore_errors=True)
371
+ raise CliError(f"the extractor installed without its command ({_extractor_exe(venv).name})")
372
+ (venv / _INSTALLED).write_text(version + "\n", encoding="utf-8")
373
+ (root / "current").write_text(version + "\n", encoding="utf-8")
374
+ return _extractor_exe(venv)
375
+
376
+
377
+ def _accept_licence(a, s: dict[str, Any], out) -> None:
378
+ """The Redberry Extractor Licence, accepted once per company (and again for a new version). ``--yes`` does
379
+ not accept it: without a terminal, ``--accept-extractor-licence`` does."""
380
+ status, lic = call(a.api, "GET", "/api/cli/extractor-licence", token=s["token"])
381
+ if status == 401:
382
+ raise CliError("your Redberry session has ended: run `redberry login`")
383
+ if status != 200:
384
+ raise CliError(f"could not read the extractor licence ({status}): {_detail(lic)}")
385
+ if lic.get("accepted"):
386
+ return
387
+ company = s.get("company") or {}
388
+ company = company.get("name") or company.get("id") or "your company"
389
+ version = lic.get("version")
390
+ if not getattr(a, "accept_extractor_licence", False):
391
+ if not sys.stdin.isatty() or a.json:
392
+ raise CliError(f"the Redberry Extractor Licence (version {version}) is not yet accepted for {company}. "
393
+ "Read it in the Redberry portal or run connect in a terminal, and pass "
394
+ "--accept-extractor-licence to accept it without a prompt (--yes does not)")
395
+ out(lic.get("text") or "")
396
+ answer = input(f"Accept the Redberry Extractor Licence for {company} (version {version})? [y/N] ")
397
+ if answer.strip().lower() not in ("y", "yes"):
398
+ raise CliError("the extractor licence was not accepted; write the manifest by hand instead")
399
+ status, body = call(a.api, "POST", "/api/cli/extractor-licence", {"version": version}, token=s["token"])
400
+ if status == 409:
401
+ raise CliError("the extractor licence changed while you read it: run connect again to see the new version")
402
+ if status != 200:
403
+ raise CliError(f"could not accept the extractor licence ({status}): {_detail(body)}")
404
+ out(f"Accepted the Redberry Extractor Licence (version {version}) for {company}")
405
+
406
+
407
+ def _token_error(status: int, body: dict[str, Any]) -> str:
408
+ detail = body.get("detail")
409
+ message = detail.get("message") if isinstance(detail, dict) else None
410
+ if status == 401:
411
+ return "your Redberry session has ended: run `redberry login`"
412
+ if status == 403:
413
+ return ("the extractor licence is not accepted for your company"
414
+ + (f" ({message})" if message else "") + ": run connect again to accept it")
415
+ if status == 429:
416
+ return "too many extractor downloads for now: try again in a few minutes"
417
+ if status == 502:
418
+ return "Redberry could not issue an extractor download token right now: try again shortly"
419
+ return f"could not get an extractor download token ({status}): {message or _detail(body)}"
420
+
421
+
270
422
  def _require_agent_code(repo: Path) -> None:
271
423
  """Before anything is described: ``repo`` holds an agent. Run from a home folder by mistake, the
272
424
  extractor described it (0 tools, 0 models)."""
@@ -301,8 +453,10 @@ def parser() -> argparse.ArgumentParser:
301
453
  con.add_argument("--manifest", default=".redberry/manifest.json", help="relative to --repo, or absolute")
302
454
  con.add_argument("--yes", action="store_true", help="apply the changes without asking")
303
455
  con.add_argument("--extract", action="store_true",
304
- help="with no manifest, write one with redberry-extract (when installed) and your LLM key, "
305
- "without asking")
456
+ help="with no manifest, write one with redberry-extract and your LLM key, without asking "
457
+ "(installs the extractor with uv when it is not on PATH)")
458
+ con.add_argument("--accept-extractor-licence", action="store_true",
459
+ help="accept the Redberry Extractor Licence for your company without a prompt")
306
460
  con.add_argument("--json", action="store_true", help="print the result as JSON")
307
461
  az = con.add_argument_group("Azure Functions (--platform azure-functions: a Node.js v4 or Python v2 function app)")
308
462
  az.add_argument("--app", help="the function app's name")
@@ -317,6 +471,12 @@ def parser() -> argparse.ArgumentParser:
317
471
  vc.add_argument("--no-env", action="store_true",
318
472
  help="print the REDBERRY_* variables instead of setting them with the Vercel CLI")
319
473
  con.set_defaults(fn=cmd_connect)
474
+ up = sub.add_parser("upgrade", help="your installs' releases; with an id, upgrade that install to the latest")
475
+ up.add_argument("installation", nargs="?", help="the installation to upgrade (without it: list them)")
476
+ up.add_argument("--yes", action="store_true", help="upgrade without asking")
477
+ up.add_argument("--force", action="store_true", help="run the upgrade even when the install is up to date")
478
+ up.add_argument("--json", action="store_true", help="list the installations as JSON")
479
+ up.set_defaults(fn=cmd_upgrade)
320
480
  return p
321
481
 
322
482
 
@@ -326,6 +486,9 @@ def main(argv: list[str] | None = None) -> int:
326
486
  with contextlib.suppress(AttributeError, ValueError):
327
487
  sys.stdout.reconfigure(line_buffering=True)
328
488
  args = parser().parse_args(argv)
489
+ env = environment(args.api)
490
+ if env != "prod" and (args.cmd != "connect" or args.extract): # off production, say where, on stderr
491
+ print(f"Redberry {env} ({args.api.rstrip('/')})", file=sys.stderr)
329
492
  try:
330
493
  return args.fn(args)
331
494
  except (CliError, _cloud.SetupError) as e:
@@ -0,0 +1,184 @@
1
+ """``redberry upgrade``: the company's Redberry installs, whether each runs Redberry's latest release, and the
2
+ upgrade of one.
3
+
4
+ Without an id it lists the installs of the signed-in company: where each is, the release it last reported and
5
+ whether a newer one is out. With an id it upgrades that install: it fetches the cloud's install script from the
6
+ Redberry portal and runs it here with ``--upgrade``, through the cloud's CLI as it is signed in on this machine
7
+ (``gcloud``, ``aws`` or ``az``). The script keeps every choice the install was made with, asks nothing, needs no
8
+ install token, and reports the new release to Redberry. It is the same command the portal shows, which can also
9
+ be pasted into the cloud's own shell.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import json
15
+ import shutil
16
+ import subprocess
17
+ import sys
18
+ import tempfile
19
+ import time
20
+ import urllib.error
21
+ import urllib.parse
22
+ import urllib.request
23
+ from pathlib import Path
24
+ from typing import Any
25
+
26
+ from redberry_cli import _cloud
27
+ from redberry_cli.main import CliError, _detail, call, session
28
+
29
+ #: The cloud's CLI the install script runs, and where to get it.
30
+ CLOUD_CLIS = {
31
+ "gcp": ("gcloud", "https://cloud.google.com/sdk/docs/install, then `gcloud auth login`"),
32
+ "aws": ("aws", "https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html, then "
33
+ "`aws configure` or `aws sso login`"),
34
+ "azure": ("az", "https://learn.microsoft.com/cli/azure/install-azure-cli, then `az login`"),
35
+ }
36
+
37
+
38
+ def installations(api: str, token: str) -> list[dict[str, Any]]:
39
+ status, body = call(api, "GET", "/api/cli/installations", token=token)
40
+ if status == 401:
41
+ raise CliError("your Redberry session has ended: run `redberry login`")
42
+ if status == 404:
43
+ raise CliError(f"{api} does not list installations yet: check --api / REDBERRY_API_URL")
44
+ if status != 200:
45
+ raise CliError(f"could not list your installations ({status}): {_detail(body)}")
46
+ return list(body.get("installations") or [])
47
+
48
+
49
+ def where(i: dict[str, Any]) -> str:
50
+ if i.get("cloud") == "aws":
51
+ return f"account {i.get('account_id') or '?'}"
52
+ if i.get("cloud") == "azure":
53
+ return f"subscription {i.get('subscription_id') or '?'}"
54
+ return f"project {i.get('project_id') or '?'}"
55
+
56
+
57
+ def state(i: dict[str, Any]) -> str:
58
+ if i.get("status") != "active":
59
+ return str(i.get("status") or "unknown")
60
+ if i.get("behind") is True:
61
+ return "update available"
62
+ if i.get("behind") is False:
63
+ return "up to date"
64
+ return "release not known" if not i.get("release") else "latest not known"
65
+
66
+
67
+ def _reported(i: dict[str, Any]) -> str:
68
+ release = i.get("release") or {}
69
+ if not release:
70
+ return "no release reported"
71
+ when = release.get("reported_at")
72
+ day = time.strftime("%Y-%m-%d", time.gmtime(when / 1000)) if isinstance(when, (int, float)) else None
73
+ return f"{release.get('tag') or '?'}" + (f", reported {day}" if day else "")
74
+
75
+
76
+ def cmd_upgrade(a, out=print, run=None, fetch=None) -> int:
77
+ s = session(a.api)
78
+ rows = installations(a.api, s["token"])
79
+ if not a.installation:
80
+ return _list(a, rows, out)
81
+ found = [i for i in rows if i.get("id") == a.installation]
82
+ if not found:
83
+ known = ", ".join(i["id"] for i in rows if i.get("id")) or "none"
84
+ raise CliError(f"no installation {a.installation} for your company (yours: {known})")
85
+ i = found[0]
86
+ hint = i.get("upgrade")
87
+ if i.get("status") != "active" or not hint:
88
+ raise CliError(f"{i['id']} is {i.get('status') or 'not active'}: a stopped or revoked install is not "
89
+ "upgraded; install again with a new command from the Redberry portal")
90
+ if i.get("behind") is False and not a.force:
91
+ out(f"{i['id']} is up to date ({_reported(i)}); --force runs the upgrade anyway")
92
+ return 0
93
+ return _run_upgrade(a, i, hint, out, run or subprocess.run, fetch or _fetch)
94
+
95
+
96
+ def _list(a, rows: list[dict[str, Any]], out) -> int:
97
+ if a.json:
98
+ out(json.dumps({"installations": rows}, indent=2))
99
+ return 0
100
+ if not rows:
101
+ out("No Redberry installs for your company yet: the Redberry portal gives the install command.")
102
+ return 0
103
+ for i in rows:
104
+ out(f"{i.get('id')} {i.get('cloud')} {where(i)} {i.get('region') or '?'} "
105
+ f"{state(i)} ({_reported(i)})")
106
+ behind = [i for i in rows if i.get("behind") is True and i.get("upgrade")]
107
+ if behind:
108
+ out("")
109
+ out("Upgrade one with `redberry upgrade <id>`, here, signed in to its cloud; or run its command in the "
110
+ "cloud's shell:")
111
+ for i in behind:
112
+ out(f" {i['id']}: {i['upgrade']['command']}")
113
+ return 0
114
+
115
+
116
+ def _run_upgrade(a, i: dict[str, Any], hint: dict[str, Any], out, run, fetch) -> int:
117
+ cloud = i.get("cloud")
118
+ if cloud not in CLOUD_CLIS:
119
+ raise CliError(f"{i['id']}: no upgrade for cloud {cloud!r} in this version of the CLI")
120
+ elsewhere = f"or run it in the cloud's shell: {hint['command']}"
121
+ bash = shutil.which("bash")
122
+ if not bash:
123
+ raise CliError(f"the upgrade runs a bash script and there is no bash here; {elsewhere}")
124
+ name, how = CLOUD_CLIS[cloud]
125
+ binary = {"aws": _cloud.aws_binary, "azure": _cloud.az_binary}.get(cloud, lambda: shutil.which("gcloud"))()
126
+ if not binary or not shutil.which(binary):
127
+ raise CliError(f"the upgrade runs the {name} CLI, which is not installed: {how}; {elsewhere}")
128
+ if cloud == "aws":
129
+ _check_aws_account(i, run, elsewhere)
130
+ args = [str(x) for x in hint.get("args") or []]
131
+ if "--upgrade" not in args:
132
+ args.append("--upgrade")
133
+ if i.get("behind") is None:
134
+ out(f"{i['id']}: its release is not known yet ({_reported(i)}); the upgrade takes the latest anyway")
135
+ if not a.yes:
136
+ if not sys.stdin.isatty():
137
+ raise CliError("the upgrade changes your cloud install: pass --yes to run it without a prompt")
138
+ answer = input(f"Upgrade {i['id']} ({cloud}, {where(i)}, {i.get('region')}) with {name} as signed in "
139
+ f"here, running Redberry's install script with {' '.join(args)}? [y/N] ")
140
+ if answer.strip().lower() not in ("y", "yes"):
141
+ out("Not upgraded")
142
+ return 1
143
+ script = fetch(hint["script_url"])
144
+ with tempfile.TemporaryDirectory(prefix="redberry-upgrade-") as tmp:
145
+ path = Path(tmp) / f"install-{cloud}.sh"
146
+ path.write_bytes(script)
147
+ out(f"Upgrading {i['id']} with {hint['script_url']} {' '.join(args)}")
148
+ try:
149
+ code = run([bash, str(path), *args]).returncode
150
+ except OSError as e:
151
+ raise CliError(f"could not run bash: {e}") from None
152
+ if code != 0:
153
+ raise CliError(f"the upgrade did not finish (exit {code}): the script's output above says why. Running it "
154
+ "again is safe")
155
+ out(f"Upgraded {i['id']}. `redberry upgrade` shows the release it reported.")
156
+ return 0
157
+
158
+
159
+ def _check_aws_account(i: dict[str, Any], run, elsewhere: str) -> None:
160
+ """The AWS script installs into the account the aws CLI is signed in to: it must be the install's."""
161
+ want = i.get("account_id")
162
+ r = _cloud.aws(run, ["aws", "sts", "get-caller-identity", "--query", "Account", "--output", "text"])
163
+ if r.returncode != 0:
164
+ raise CliError(f"the aws CLI is not signed in ({(r.stderr or '').strip()[-300:]}): `aws configure` or "
165
+ f"`aws sso login`; {elsewhere}")
166
+ have = (r.stdout or "").strip()
167
+ if want and have != want:
168
+ raise CliError(f"the aws CLI is signed in to account {have}, and {i['id']} is in account {want}: sign in "
169
+ f"to {want} (AWS_PROFILE), {elsewhere}")
170
+
171
+
172
+ def _fetch(url: str) -> bytes:
173
+ """The install script, over https (http only from this machine, for tests)."""
174
+ parts = urllib.parse.urlsplit(url)
175
+ if parts.scheme != "https" and not (parts.scheme == "http" and parts.hostname in ("127.0.0.1", "localhost")):
176
+ raise CliError(f"refusing to fetch the install script over {parts.scheme or 'no scheme'}: {url}")
177
+ try:
178
+ with urllib.request.urlopen(url, timeout=30) as r:
179
+ data = r.read()
180
+ except (urllib.error.URLError, TimeoutError, OSError) as e:
181
+ raise CliError(f"could not fetch the install script {url}: {e}") from None
182
+ if not data.startswith(b"#!"):
183
+ raise CliError(f"{url} is not a shell script")
184
+ return data
@@ -283,6 +283,15 @@ def _given(a, environ: dict[str, str]) -> dict[str, str]:
283
283
  return given
284
284
 
285
285
 
286
+ def multi_route_note(agent: str, routes: list[Route]) -> str | None:
287
+ """When more than one route calls a model: they are one agent (one name per app), so say so."""
288
+ if len(routes) < 2:
289
+ return None
290
+ return (f"This app has model calls in {len(routes)} routes ({', '.join(r.path for r in routes)}); they are "
291
+ f"recorded and priced as one agent, {agent}. To price agents separately, deploy them as separate "
292
+ "Vercel projects.")
293
+
294
+
286
295
  def cmd_connect(a, out=print, run=None, ask=None, environ=None) -> int:
287
296
  import os
288
297
  import shutil
@@ -312,6 +321,9 @@ def cmd_connect(a, out=print, run=None, ask=None, environ=None) -> int:
312
321
  "Vercel project: not linked here (.vercel/project.json; `vercel link` links it)")
313
322
  say(f" agent {agent}: {', '.join(r.path for r in routes)}")
314
323
  say(f" tools: {', '.join(t.name for t in tools) or 'none found by the static reader'}")
324
+ routes_note = multi_route_note(agent, routes)
325
+ if routes_note:
326
+ say(routes_note)
315
327
 
316
328
  edits, edit_notes = plan_edits(repo, pkg, pkg_text, agent, routes)
317
329
  notes += edit_notes
@@ -354,7 +366,8 @@ def cmd_connect(a, out=print, run=None, ask=None, environ=None) -> int:
354
366
  result = {
355
367
  "platform": PLATFORM, "vercel_project": description.get("vercel_project"), "agent_name": agent,
356
368
  "routes": description["routes"], "edited": [str(e.path.relative_to(repo)) for e in edits],
357
- "env": env, "env_set": env_set, "notes": notes, "next": [install, "commit and deploy"],
369
+ "env": env, "env_set": env_set, "notes": notes, "routes_note": routes_note,
370
+ "next": [install, "commit and deploy"],
358
371
  }
359
372
  if a.json:
360
373
  out(json.dumps(result, indent=2))
@@ -381,4 +394,4 @@ def cmd_connect(a, out=print, run=None, ask=None, environ=None) -> int:
381
394
 
382
395
 
383
396
  __all__ = ["DEPENDENCIES", "INSTRUMENTATION", "agent_routes", "check_app", "cmd_connect", "env_values",
384
- "linked_project", "plan_edits", "read_env_file", "set_vercel_env"]
397
+ "linked_project", "multi_route_note", "plan_edits", "read_env_file", "set_vercel_env"]