nai-cli 0.1.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.
Files changed (75) hide show
  1. nai_cli/__init__.py +3 -0
  2. nai_cli/__main__.py +5 -0
  3. nai_cli/api.py +529 -0
  4. nai_cli/auth.py +240 -0
  5. nai_cli/batch.py +126 -0
  6. nai_cli/caches.py +56 -0
  7. nai_cli/charge.py +231 -0
  8. nai_cli/cli.py +110 -0
  9. nai_cli/comfyui.py +194 -0
  10. nai_cli/commands/__init__.py +61 -0
  11. nai_cli/commands/account.py +79 -0
  12. nai_cli/commands/batch.py +142 -0
  13. nai_cli/commands/config.py +57 -0
  14. nai_cli/commands/context.py +201 -0
  15. nai_cli/commands/copies.py +138 -0
  16. nai_cli/commands/cost.py +70 -0
  17. nai_cli/commands/generate.py +257 -0
  18. nai_cli/commands/generation.py +550 -0
  19. nai_cli/commands/grid.py +99 -0
  20. nai_cli/commands/image_tools.py +248 -0
  21. nai_cli/commands/interrogate.py +77 -0
  22. nai_cli/commands/lint.py +24 -0
  23. nai_cli/commands/local.py +62 -0
  24. nai_cli/commands/meta.py +102 -0
  25. nai_cli/commands/models.py +62 -0
  26. nai_cli/commands/output.py +165 -0
  27. nai_cli/commands/redact.py +48 -0
  28. nai_cli/commands/references.py +87 -0
  29. nai_cli/commands/spec.py +245 -0
  30. nai_cli/commands/stats.py +54 -0
  31. nai_cli/commands/strip.py +15 -0
  32. nai_cli/commands/tags.py +32 -0
  33. nai_cli/commands/texts.py +71 -0
  34. nai_cli/commands/vibe.py +151 -0
  35. nai_cli/commands/watermark.py +70 -0
  36. nai_cli/config.py +369 -0
  37. nai_cli/cost.py +114 -0
  38. nai_cli/data/random_prompt.json +2536 -0
  39. nai_cli/decode.py +87 -0
  40. nai_cli/enhance.py +137 -0
  41. nai_cli/errors.py +40 -0
  42. nai_cli/fs.py +209 -0
  43. nai_cli/grid.py +244 -0
  44. nai_cli/images.py +250 -0
  45. nai_cli/import_image.py +235 -0
  46. nai_cli/inpaint.py +476 -0
  47. nai_cli/ledger.py +146 -0
  48. nai_cli/lint.py +302 -0
  49. nai_cli/local_models.py +135 -0
  50. nai_cli/meta.py +606 -0
  51. nai_cli/models.py +308 -0
  52. nai_cli/naming.py +426 -0
  53. nai_cli/parser.py +611 -0
  54. nai_cli/pixel_snap.py +525 -0
  55. nai_cli/presets.py +276 -0
  56. nai_cli/prompt.py +457 -0
  57. nai_cli/random_prompt.py +285 -0
  58. nai_cli/request.py +621 -0
  59. nai_cli/rerun.py +177 -0
  60. nai_cli/resample.py +133 -0
  61. nai_cli/sam.py +63 -0
  62. nai_cli/share.py +392 -0
  63. nai_cli/sheet.py +86 -0
  64. nai_cli/tagger.py +464 -0
  65. nai_cli/tags.py +44 -0
  66. nai_cli/tidy.py +176 -0
  67. nai_cli/tokenizer.py +313 -0
  68. nai_cli/values.py +114 -0
  69. nai_cli/vibe.py +464 -0
  70. nai_cli-0.1.0.dist-info/METADATA +105 -0
  71. nai_cli-0.1.0.dist-info/RECORD +75 -0
  72. nai_cli-0.1.0.dist-info/WHEEL +4 -0
  73. nai_cli-0.1.0.dist-info/entry_points.txt +2 -0
  74. nai_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
  75. nai_cli-0.1.0.dist-info/licenses/THIRD_PARTY_NOTICES.md +56 -0
nai_cli/auth.py ADDED
@@ -0,0 +1,240 @@
1
+ """Accounts: the config's tokens, and email logins kept as an access key and a token renewed from it."""
2
+
3
+ import base64
4
+ import hashlib
5
+ from collections.abc import Iterator
6
+ from contextlib import contextmanager
7
+ from dataclasses import dataclass
8
+ from datetime import UTC, datetime, timedelta
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ from argon2.low_level import Type, hash_secret_raw
13
+
14
+ from nai_cli import api, config, errors, fs, values
15
+
16
+ DEFAULT = config.DEFAULT_ACCOUNT
17
+ TOKEN_LIFETIME = timedelta(days=30)
18
+ # Renewed this long before it runs out, so a long batch does not lose it halfway
19
+ RENEW_BEFORE = timedelta(days=1)
20
+
21
+
22
+ @dataclass(frozen=True)
23
+ class Credentials:
24
+ name: str
25
+ token: str
26
+ endpoint: api.Endpoint
27
+
28
+
29
+ def access_key(email: str, password: str) -> str:
30
+ """As the official login derives it: Argon2id over the password, salted with a BLAKE2b hash of the start of
31
+ the password and the lowercased email."""
32
+ salt = hashlib.blake2b(f"{_js_prefix(password, 6)}{email.lower()}novelai_data_access_key".encode(), digest_size=16)
33
+ raw = hash_secret_raw(
34
+ password.encode(),
35
+ salt.digest(),
36
+ time_cost=2,
37
+ memory_cost=2_000_000 // 1024,
38
+ parallelism=1,
39
+ hash_len=64,
40
+ type=Type.ID,
41
+ )
42
+ return base64.urlsafe_b64encode(raw).decode().rstrip("=")[:64]
43
+
44
+
45
+ def _js_prefix(text: str, units: int) -> str:
46
+ """The first units UTF-16 code units, as String.slice cuts them; half a surrogate pair encodes as U+FFFD."""
47
+ out, used = [], 0
48
+ for ch in text:
49
+ size = 2 if ord(ch) > 0xFFFF else 1
50
+ if used + size > units:
51
+ if used < units:
52
+ out.append("�")
53
+ break
54
+ out.append(ch)
55
+ used += size
56
+ return "".join(out)
57
+
58
+
59
+ def credentials_path() -> Path:
60
+ return config.data_dir() / "accounts.json"
61
+
62
+
63
+ def login(name: str, email: str, password: str, cfg: config.Config, now: datetime) -> dict[str, Any]:
64
+ """Sign in to the official service and keep the access key and token; the password is not kept."""
65
+ if name == DEFAULT or name in cfg.accounts:
66
+ raise errors.NaiError(errors.BAD_ARGS, f"{name} is an account of the config file; choose another name")
67
+ key = access_key(email.strip(), password)
68
+ token = api.login(api.OFFICIAL, key)
69
+ saved = {"email": email.strip().lower(), "access_key": key, **_token(token, now)}
70
+ with _editing() as store:
71
+ store["logins"][name] = saved
72
+ return {"account": name, "email": saved["email"], "expires": saved["expires"]}
73
+
74
+
75
+ def logout(name: str, dry_run: bool = False) -> bool:
76
+ """With dry_run, only whether there is such a login to sign out of."""
77
+ if dry_run:
78
+ return name in _load()["logins"]
79
+ with _editing() as store:
80
+ if name not in _valid(store["logins"]):
81
+ return False
82
+ del store["logins"][name]
83
+ if store.get("current") == name:
84
+ store["current"] = None
85
+ return True
86
+
87
+
88
+ def use(name: str, cfg: config.Config, dry_run: bool = False) -> None:
89
+ if name not in names(cfg):
90
+ raise errors.NaiError(errors.BAD_ARGS, f"no account named {name}", choices=names(cfg))
91
+ if dry_run:
92
+ return
93
+ with _editing() as store:
94
+ # Checked again under the lock, as another nai may have signed out of it meanwhile
95
+ if name != DEFAULT and name not in cfg.accounts and name not in _valid(store["logins"]):
96
+ raise errors.NaiError(errors.BAD_ARGS, f"no account named {name}", choices=names(cfg))
97
+ store["current"] = None if name == DEFAULT else name
98
+
99
+
100
+ def current() -> str:
101
+ return _load()["current"] or DEFAULT
102
+
103
+
104
+ def names(cfg: config.Config) -> list[str]:
105
+ return [DEFAULT, *cfg.accounts, *(n for n in _load()["logins"] if n not in cfg.accounts)]
106
+
107
+
108
+ def listing(cfg: config.Config) -> list[dict[str, Any]]:
109
+ """Each account and how it signs in, never its token."""
110
+ logins = _load()["logins"]
111
+ active = current()
112
+ out = [{"account": DEFAULT, "kind": "token", "token_set": bool(cfg.novelai_token), "source": cfg.token_source}]
113
+ for name, account in cfg.accounts.items():
114
+ kind = "token" if account.endpoint.official else "relay"
115
+ entry: dict[str, Any] = {"account": name, "kind": kind}
116
+ if not account.endpoint.official:
117
+ entry.update(api_url=account.endpoint.api_base, image_url=account.endpoint.image_base)
118
+ out.append(entry)
119
+ for name, saved in logins.items():
120
+ if name not in cfg.accounts:
121
+ out.append({"account": name, "kind": "login", "email": saved.get("email"), "expires": saved.get("expires")})
122
+ for entry in out:
123
+ entry["current"] = entry["account"] == active
124
+ return out
125
+
126
+
127
+ def has_token(cfg: config.Config, name: str | None) -> bool:
128
+ chosen = name or current()
129
+ return bool(cfg.novelai_token) if chosen == DEFAULT else chosen in names(cfg)
130
+
131
+
132
+ def credentials(cfg: config.Config, name: str | None, now: datetime, renew: bool = True) -> Credentials:
133
+ """The token and addresses of an account, --account first, then the one chosen with nai account use; a
134
+ login's token is renewed from its access key when it is about to run out. Without renew, a token still valid
135
+ is used as it is and one run out is refused, so nothing is sent or written."""
136
+ chosen = name or current()
137
+ if chosen == DEFAULT:
138
+ return Credentials(DEFAULT, cfg.require_novelai(), api.OFFICIAL)
139
+ if chosen in cfg.accounts:
140
+ account = cfg.accounts[chosen]
141
+ return Credentials(chosen, account.token, account.endpoint)
142
+ saved = _load()["logins"].get(chosen)
143
+ if saved is None:
144
+ raise errors.NaiError(errors.BAD_ARGS, f"no account named {chosen}", choices=names(cfg))
145
+ left = _expires(saved) - now.astimezone(UTC)
146
+ if left >= RENEW_BEFORE:
147
+ return Credentials(chosen, saved["token"], api.OFFICIAL)
148
+ if not renew:
149
+ if left > timedelta(0):
150
+ return Credentials(chosen, saved["token"], api.OFFICIAL)
151
+ raise errors.NaiError(
152
+ errors.AUTH, f"the login of {chosen} has run out; a run that sends nothing paid does not renew it"
153
+ )
154
+ try:
155
+ token = api.login(api.OFFICIAL, saved["access_key"])
156
+ except errors.NaiError as e:
157
+ if e.code != errors.AUTH and left > timedelta(0):
158
+ # A token still valid is used while the network or server is down; the next command renews it
159
+ return Credentials(chosen, saved["token"], api.OFFICIAL)
160
+ hint = "; run nai account login" if e.code == errors.AUTH else ""
161
+ raise errors.NaiError(e.code, f"the login of {chosen} could not be renewed{hint}: {e}") from e
162
+ with _editing() as store:
163
+ entry = store["logins"].get(chosen)
164
+ # Signed out or signed in again meanwhile: that newer state stands, and this run still has its token
165
+ if isinstance(entry, dict) and entry.get("access_key") == saved["access_key"]:
166
+ entry.update(_token(token, now))
167
+ return Credentials(chosen, token, api.OFFICIAL)
168
+
169
+
170
+ def _token(token: str, now: datetime) -> dict[str, str]:
171
+ return {"token": token, "expires": (now.astimezone(UTC) + TOKEN_LIFETIME).isoformat(timespec="seconds")}
172
+
173
+
174
+ def _expires(saved: dict[str, Any]) -> datetime:
175
+ try:
176
+ expires = datetime.fromisoformat(saved["expires"])
177
+ except (KeyError, TypeError, ValueError):
178
+ expires = None
179
+ # Renew a login whose expiry cannot be read, or has no time zone to compare by
180
+ if expires is None or expires.tzinfo is None:
181
+ return datetime.min.replace(tzinfo=UTC)
182
+ return expires
183
+
184
+
185
+ def _load() -> dict[str, Any]:
186
+ """The logins that can be used and the one chosen."""
187
+ store = _read()
188
+ chosen = store.get("current")
189
+ return {"current": chosen if isinstance(chosen, str) else None, "logins": _valid(store["logins"])}
190
+
191
+
192
+ def _valid(logins: dict[str, Any]) -> dict[str, dict[str, Any]]:
193
+ return {
194
+ name: saved
195
+ for name, saved in logins.items()
196
+ if isinstance(saved, dict) and isinstance(saved.get("access_key"), str) and isinstance(saved.get("token"), str)
197
+ }
198
+
199
+
200
+ def _read() -> dict[str, Any]:
201
+ """The store as written, entries this version cannot use included, so that an edit keeps them."""
202
+ path = credentials_path()
203
+ try:
204
+ data = values.parse_json(path.read_text(encoding="utf-8"))
205
+ except FileNotFoundError:
206
+ data = {}
207
+ except (OSError, ValueError) as e:
208
+ raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: cannot read: {e}") from e
209
+ # Refused rather than read as empty, which the next save would write over the logins
210
+ if not isinstance(data, dict) or not isinstance(data.setdefault("logins", {}), dict):
211
+ raise errors.NaiError(errors.CONFIG_MISSING, f"{path} is not a login store; fix or delete it")
212
+ return data
213
+
214
+
215
+ @contextmanager
216
+ def _editing() -> Iterator[dict[str, Any]]:
217
+ """The store read and written back under a lock, so two processes editing it at once lose neither edit. Nothing
218
+ is sent while the lock is held."""
219
+ lock = credentials_path().with_name("accounts.lock")
220
+ try:
221
+ held = fs.acquire(lock, timeout=30, poll=0.1)
222
+ except TimeoutError:
223
+ raise errors.NaiError(errors.BUSY, f"another nai is still changing {credentials_path()}") from None
224
+ except OSError as e:
225
+ raise errors.NaiError(errors.CONFIG_MISSING, f"cannot lock {lock}: {e}") from e
226
+ try:
227
+ store = _read()
228
+ yield store
229
+ _save(store)
230
+ finally:
231
+ fs.release(held)
232
+
233
+
234
+ def _save(store: dict[str, Any]) -> None:
235
+ # Holds the access key, which signs in without the password
236
+ path = credentials_path()
237
+ try:
238
+ fs.replace(path, values.to_json(store, indent=2), private=True)
239
+ except OSError as e:
240
+ raise errors.NaiError(errors.CONFIG_MISSING, f"cannot write {path}: {e}") from e
nai_cli/batch.py ADDED
@@ -0,0 +1,126 @@
1
+ """Job lists and resuming them: which jobs a previous run finished, from every line it printed."""
2
+
3
+ from dataclasses import dataclass
4
+ from typing import Any
5
+
6
+ from nai_cli import errors, values
7
+
8
+ # The option of nai itself a job may give before its command; --config is refused, so the batch's limits hold
9
+ JOB_OPTION = "--account"
10
+
11
+
12
+ @dataclass(frozen=True)
13
+ class Job:
14
+ id: str
15
+ line: int
16
+ argv: list[str]
17
+
18
+
19
+ def read_jobs(text: str, source: str) -> list[Job]:
20
+ jobs: list[Job] = []
21
+ seen: set[str] = set()
22
+ for number, line in enumerate(_records(text), 1):
23
+ line = line.strip()
24
+ if not line or line.startswith("#"):
25
+ continue
26
+ where = f"line {number} of {source}"
27
+ try:
28
+ item = values.parse_json(line)
29
+ except ValueError as e:
30
+ raise errors.NaiError(errors.BAD_ARGS, f"{where} is not JSON: {e}") from None
31
+ job_id = str(number)
32
+ if isinstance(item, dict):
33
+ if "id" in item:
34
+ if not (isinstance(item["id"], str) or values.is_whole(item["id"])):
35
+ raise errors.NaiError(errors.BAD_ARGS, f"{where}: id must be a string or a number")
36
+ job_id = str(item["id"])
37
+ item = item.get("args")
38
+ if not isinstance(item, list) or not item or not all(isinstance(a, str) for a in item):
39
+ raise errors.NaiError(errors.BAD_ARGS, f"{where}: a job is a list of strings, or an object with args")
40
+ command = _command(item)
41
+ # Commands that print lines of their own cannot be one job's single result
42
+ if command is None or command.startswith("-") or command in ("batch", "grid"):
43
+ raise errors.NaiError(
44
+ errors.BAD_ARGS, f"{where}: a job is a command other than batch or grid, after --account if given"
45
+ )
46
+ if job_id in seen:
47
+ raise errors.NaiError(errors.BAD_ARGS, f"{where}: the id {job_id} is used twice")
48
+ seen.add(job_id)
49
+ jobs.append(Job(job_id, number, item))
50
+ return jobs
51
+
52
+
53
+ def succeeded(text: str) -> set[str]:
54
+ """The jobs a previous output shows succeeding, from any of several runs appended one after another; a dry run's
55
+ lines count for nothing."""
56
+ done: set[str] = set()
57
+ for line in _records(text):
58
+ try:
59
+ item = values.parse_json(line)
60
+ except ValueError:
61
+ continue
62
+ if isinstance(item, dict) and "job" in item and _succeeded(item):
63
+ done.add(str(item["job"]))
64
+ return done
65
+
66
+
67
+ def _records(text: str) -> list[str]:
68
+ # str.splitlines also breaks at U+2028 and the like, which JSON strings may hold unescaped
69
+ return text.split("\n")
70
+
71
+
72
+ def _succeeded(line: dict[str, Any]) -> bool:
73
+ return not line.get("dry_run") and (line.get("ok") is True or line.get("skipped") == "succeeded")
74
+
75
+
76
+ @dataclass
77
+ class Totals:
78
+ jobs: int
79
+ succeeded: int = 0
80
+ failed: int = 0
81
+ skipped: int = 0
82
+ spent: int = 0
83
+ unknown: int = 0
84
+ estimate: int = 0
85
+ # The exit code of the last job that failed
86
+ code: int = 0
87
+
88
+ def add(self, result: dict[str, Any], code: int) -> None:
89
+ cost_json = result.get("cost") if isinstance(result.get("cost"), dict) else {}
90
+ if "spent" in cost_json:
91
+ if cost_json["spent"] is None:
92
+ self.unknown += 1
93
+ else:
94
+ self.spent += cost_json["spent"]
95
+ # A preview's estimate is the request it shows, which it does not send
96
+ if result.get("dry_run") and "preview" not in result and values.is_whole(cost_json.get("estimate")):
97
+ self.estimate += max(cost_json["estimate"], 0)
98
+ if code == 0:
99
+ self.succeeded += 1
100
+ else:
101
+ self.failed += 1
102
+ self.code = code
103
+
104
+ def to_json(self, dry_run: bool) -> dict[str, Any]:
105
+ out: dict[str, Any] = {
106
+ "jobs": self.jobs,
107
+ "succeeded": self.succeeded,
108
+ "failed": self.failed,
109
+ "skipped": self.skipped,
110
+ "spent": self.spent,
111
+ }
112
+ if self.unknown:
113
+ out["jobs_with_unknown_spend"] = self.unknown
114
+ if dry_run:
115
+ out["estimate"] = self.estimate
116
+ return out
117
+
118
+
119
+ def _command(argv: list[str]) -> str | None:
120
+ rest = iter(argv)
121
+ for arg in rest:
122
+ if arg == JOB_OPTION:
123
+ next(rest, None)
124
+ elif not arg.startswith(f"{JOB_OPTION}="):
125
+ return arg
126
+ return None
nai_cli/caches.py ADDED
@@ -0,0 +1,56 @@
1
+ """Files kept in the data folder between runs: the official tokenizer files and the Vibe encodings paid for."""
2
+
3
+ import contextlib
4
+ import functools
5
+ import hashlib
6
+ from pathlib import Path
7
+
8
+ import httpx
9
+
10
+ from nai_cli import api, config, fs, tokenizer, vibe
11
+
12
+
13
+ def tokenizer_path(kind: str) -> Path:
14
+ return config.data_dir() / "tokenizers" / f"v{tokenizer.VERSION}" / tokenizer.FILES[kind]
15
+
16
+
17
+ @functools.cache
18
+ def load_tokenizer(kind: str) -> tokenizer.Tokenizer | None:
19
+ """Read from the data folder, downloading on first use; None when neither works, and counts are then estimated."""
20
+ path = tokenizer_path(kind)
21
+ try:
22
+ return tokenizer.parse(kind, path.read_bytes())
23
+ except (OSError, ValueError):
24
+ pass
25
+ try:
26
+ raw = api.fetch_tokenizer(tokenizer.FILES[kind], tokenizer.VERSION)
27
+ loaded = tokenizer.parse(kind, raw)
28
+ except (httpx.HTTPError, ValueError):
29
+ return None
30
+ # Counting still works for this run when the file cannot be kept; the next one downloads again
31
+ with contextlib.suppress(OSError):
32
+ fs.replace(path, raw)
33
+ return loaded
34
+
35
+
36
+ def vibe_encoding(source: vibe.VibeSource, model: str) -> str | None:
37
+ """The encoding the Vibe keeps, else one paid for before with the same settings; None when it needs encoding."""
38
+ stored = vibe.stored_encoding(source, model)
39
+ if stored is not None:
40
+ return stored
41
+ try:
42
+ cached = _vibe_path(source, model).read_text(encoding="utf-8").strip()
43
+ except (OSError, UnicodeDecodeError):
44
+ # Missing or unreadable, so the image is encoded again
45
+ return None
46
+ return cached if cached and vibe.is_encoding(cached) else None
47
+
48
+
49
+ def remember_vibe(source: vibe.VibeSource, model: str, encoding: str) -> None:
50
+ # Lookups outside the generation lock must never read half a file
51
+ fs.replace(_vibe_path(source, model), encoding)
52
+
53
+
54
+ def _vibe_path(source: vibe.VibeSource, model: str) -> Path:
55
+ key = vibe.encoding_key(source, model)
56
+ return config.data_dir() / "vibe-cache" / f"{hashlib.sha256(key.encode()).hexdigest()}.txt"
nai_cli/charge.py ADDED
@@ -0,0 +1,231 @@
1
+ """Paid requests: priced against the balance, confirmed above the per-request limit, and written to the ledger even
2
+ when they fail."""
3
+
4
+ from collections.abc import Callable, Iterator
5
+ from contextlib import contextmanager
6
+ from dataclasses import dataclass, field
7
+ from datetime import datetime
8
+ from functools import cached_property
9
+ from pathlib import Path
10
+ from typing import Any, NoReturn, TypeVar
11
+
12
+ from nai_cli import api, config, cost, errors, fs, ledger
13
+
14
+ T = TypeVar("T")
15
+
16
+
17
+ @dataclass
18
+ class Charge:
19
+ """Paid requests made under the generation lock, measured by the balance before and after them."""
20
+
21
+ client: api.Client
22
+ # Written to the ledger with the spend; callers add what they know before sending
23
+ entry: dict[str, Any]
24
+ warnings: list[str]
25
+ now: Callable[[], datetime]
26
+ after: api.Account | None = None
27
+ # Set once a paid request ran or may have run; one refused or never sent costs nothing
28
+ paid: bool = False
29
+ # Images go to the ledger; saved lists every file written, for the error output
30
+ files: list[str] = field(default_factory=list)
31
+ saved: list[str] = field(default_factory=list)
32
+
33
+ @cached_property
34
+ def before(self) -> api.Account:
35
+ # Read when first needed, so a run that finds nothing to pay for sends nothing
36
+ return self.client.account()
37
+
38
+ @property
39
+ def spent(self) -> int | None:
40
+ if not self.paid:
41
+ return 0
42
+ if self.after is None or not (self.before.measured and self.after.measured):
43
+ return None
44
+ return self.before.anlas - self.after.anlas
45
+
46
+ def pay(self, request: Callable[..., T], *args: Any) -> T:
47
+ # The spend is measured from here, so the opening balance must be known first
48
+ _ = self.before
49
+ try:
50
+ result = request(*args)
51
+ except errors.NaiError as e:
52
+ self.paid = self.paid or bool(e.detail.get("maybe_processed"))
53
+ raise
54
+ except BaseException:
55
+ self.paid = True
56
+ raise
57
+ self.paid = True
58
+ return result
59
+
60
+ def keep(self, data: bytes | str, path: Path, image: bool = True) -> str:
61
+ """Save a paid-for result under path or the next free number; a failure is save_failed, since the request
62
+ has already run."""
63
+ try:
64
+ written = fs.create_numbered(path, data)
65
+ except (OSError, ValueError) as e:
66
+ raise errors.NaiError(errors.SAVE_FAILED, f"the request ran, but its result could not be saved: {e}") from e
67
+ self.saved.append(str(written))
68
+ if image:
69
+ self.files.append(str(written))
70
+ return str(written)
71
+
72
+ def settle(self, error: BaseException | None) -> bool:
73
+ """Read the closing balance and write the ledger; returns whether Ctrl+C arrived meanwhile, which
74
+ neither step lets lose the spend."""
75
+ interrupted = False
76
+ try:
77
+ self.after = self.client.account()
78
+ except (errors.NaiError, KeyboardInterrupt) as e:
79
+ # Ctrl+C while waiting to retry the read arrives as an interrupted error
80
+ interrupted = isinstance(e, KeyboardInterrupt) or e.code == errors.INTERRUPTED
81
+ reason = "interrupted" if interrupted else e.message
82
+ self.warnings.append(f"the balance could not be read after the request, so the spend is unknown: {reason}")
83
+ else:
84
+ if self.paid and self.spent is None:
85
+ self.warnings.append("the account does not report its balance, so the spend is unknown")
86
+ try:
87
+ ledger.record(self._record(error), self.now())
88
+ except OSError as e:
89
+ self.warnings.append(f"the ledger could not be written: {e}")
90
+ except KeyboardInterrupt:
91
+ interrupted = True
92
+ self.warnings.append("interrupted while writing the ledger, so this request may be missing from it")
93
+ return interrupted
94
+
95
+ def _record(self, error: BaseException | None) -> dict[str, Any]:
96
+ record = {
97
+ **self.entry,
98
+ "files": self.files,
99
+ "anlas_spent": self.spent,
100
+ "quota_percent": self.after.quota_percent if self.after else None,
101
+ }
102
+ if error is not None:
103
+ record["error"] = error_code(error)
104
+ return record
105
+
106
+ def report(self) -> dict[str, Any]:
107
+ # The request ran, so sending it again may pay twice
108
+ detail: dict[str, Any] = {"cost": balance_json(None, self), "maybe_processed": True}
109
+ if self.saved:
110
+ detail["saved"] = list(self.saved)
111
+ if self.warnings:
112
+ detail["warnings"] = list(self.warnings)
113
+ return detail
114
+
115
+
116
+ def error_code(error: BaseException) -> str:
117
+ if isinstance(error, errors.NaiError):
118
+ return error.code
119
+ return errors.INTERRUPTED if isinstance(error, KeyboardInterrupt) else errors.INTERNAL
120
+
121
+
122
+ @contextmanager
123
+ def charged(
124
+ client: api.Client,
125
+ warnings: list[str],
126
+ entry: dict[str, Any],
127
+ now: Callable[[], datetime],
128
+ on_interrupt: Callable[[], None],
129
+ ) -> Iterator[Charge]:
130
+ """Hold the generation lock around paid requests so other processes cannot skew the spend. The opening balance
131
+ prices and confirms them; once one has run, the closing balance is read and the ledger written even if a later
132
+ step fails or is interrupted, and the error then reports the spend and the files already saved."""
133
+ with ledger.generation_lock():
134
+ charge = Charge(client, dict(entry), warnings, now)
135
+ try:
136
+ yield charge
137
+ except (Exception, KeyboardInterrupt) as e:
138
+ if not charge.paid:
139
+ raise
140
+ # The first failure keeps its code; Ctrl+C while settling stops the batch after it
141
+ if charge.settle(e):
142
+ on_interrupt()
143
+ detail = charge.report()
144
+ if isinstance(e, errors.NaiError):
145
+ e.detail.update(detail)
146
+ raise
147
+ message = "interrupted" if isinstance(e, KeyboardInterrupt) else f"{type(e).__name__}: {e}"
148
+ raise errors.NaiError(error_code(e), message, **detail) from e
149
+ if charge.paid and charge.settle(None):
150
+ raise errors.NaiError(errors.INTERRUPTED, "interrupted", **charge.report())
151
+
152
+
153
+ def authorize(
154
+ price: int, images: int, charge: Charge, cfg: config.Config, yes: bool, cost_json: dict[str, Any]
155
+ ) -> None:
156
+ """Before anything is sent: the balance must cover the estimate, so a Vibe is not encoded for a generation
157
+ that would be refused, and a large estimate needs --yes."""
158
+ charge.entry["anlas_estimate"] = price
159
+ charge.entry["images_requested"] = images
160
+ _allow(price, charge.before, cfg, yes, cost_json)
161
+
162
+
163
+ def refusal(
164
+ price: int, account: api.Account | None, cfg: config.Config, yes: bool, cost_json: dict[str, Any]
165
+ ) -> dict[str, Any] | None:
166
+ """For a dry run: the refusal the real run would meet before sending, by the balance read now if any."""
167
+ try:
168
+ _allow(price, account, cfg, yes, cost_json)
169
+ except errors.NaiError as e:
170
+ return {"code": e.code, "message": e.message}
171
+ return None
172
+
173
+
174
+ def _allow(price: int, account: api.Account | None, cfg: config.Config, yes: bool, cost_json: dict[str, Any]) -> None:
175
+ if account is not None and account.measured and price > account.anlas:
176
+ raise errors.NaiError(
177
+ errors.INSUFFICIENT_ANLAS,
178
+ f"estimated {price} Anlas, but the balance is {account.anlas}",
179
+ cost=cost_json,
180
+ )
181
+ if price > cfg.max_per_request and not yes:
182
+ raise errors.NaiError(
183
+ errors.NEED_CONFIRM,
184
+ f"estimated {price} Anlas exceeds max_per_request ({cfg.max_per_request})",
185
+ cost=cost_json,
186
+ )
187
+
188
+
189
+ def keep_usable(error: api.DamagedImages, save: Callable[[list[bytes | None]], Any], warnings: list[str]) -> NoReturn:
190
+ """Save the images of a partly damaged reply, then report the damage, or a save failure with the damage
191
+ noted in warnings."""
192
+ try:
193
+ save(error.images)
194
+ except errors.NaiError:
195
+ warnings.append(error.message)
196
+ raise
197
+ raise error
198
+
199
+
200
+ def balance_json(account: api.Account | None, charge: Charge | None, quota: bool = False) -> dict[str, Any]:
201
+ """After a paid request: the spend and the closing balance, either of which may be unknown."""
202
+ out: dict[str, Any] = {}
203
+ if charge is not None:
204
+ out["spent"] = charge.spent
205
+ account = charge.after
206
+ if account is None:
207
+ return out
208
+ if account is None:
209
+ out["assumed_opus"] = True
210
+ return out
211
+ if not account.measured:
212
+ out["balance_known"] = False
213
+ return out
214
+ out["anlas_left"] = account.anlas
215
+ if quota:
216
+ out["quota_percent"] = account.quota_percent
217
+ if account.quota_negative:
218
+ out["quota_exhausted"] = True
219
+ out["warning"] = "the V5 free quota is exhausted, so normally free images are charged"
220
+ return out
221
+
222
+
223
+ def cost_json(estimate: cost.Estimate, account: api.Account | None, charge: Charge | None = None) -> dict[str, Any]:
224
+ out: dict[str, Any] = {"estimate": estimate.anlas, "free_by_opus": estimate.free_by_opus}
225
+ if estimate.free_by_opus and estimate.if_quota_exhausted != estimate.anlas:
226
+ out["if_quota_exhausted"] = estimate.if_quota_exhausted
227
+ return {**out, **balance_json(account, charge, quota=True)}
228
+
229
+
230
+ def price_json(price: int, account: api.Account | None, charge: Charge | None = None) -> dict[str, Any]:
231
+ return {"estimate": price, **balance_json(account, charge)}