mlai-code 0.3.2 → 0.3.4

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 (157) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/checkpoints.mjs +217 -0
  4. package/src/cli.mjs +178 -31
  5. package/src/cloud.mjs +2 -0
  6. package/src/engine.mjs +1 -1
  7. package/src/headless.mjs +25 -9
  8. package/src/home.mjs +3 -2
  9. package/src/host.mjs +141 -13
  10. package/src/ide-quick.mjs +147 -0
  11. package/src/ide.mjs +182 -0
  12. package/src/netbox.mjs +1 -1
  13. package/src/parity.mjs +20 -1
  14. package/src/relay.mjs +1 -1
  15. package/src/render.mjs +12 -3
  16. package/src/runtime.mjs +23 -1
  17. package/src/sandbox.mjs +19 -3
  18. package/src/server.mjs +46 -7
  19. package/src/session.mjs +53 -10
  20. package/src/shared-data.mjs +1 -1
  21. package/src/tui/acp.mjs +121 -19
  22. package/src/tui/app.mjs +24 -5
  23. package/src/tui/chat.mjs +43 -16
  24. package/src/tui/cmd-config.mjs +3 -1
  25. package/src/tui/cmd-core.mjs +11 -7
  26. package/src/tui/cmd-extensibility.mjs +13 -2
  27. package/src/tui/commands.mjs +2 -1
  28. package/src/tui/extensions-local.mjs +2 -0
  29. package/src/tui/mcp-config.mjs +22 -4
  30. package/src/tui/review-cli.mjs +15 -4
  31. package/src/tui/snapshots.mjs +57 -16
  32. package/src/tui/statusline.mjs +2 -1
  33. package/src/two-words.mjs +14 -8
  34. package/src/view-sync.mjs +48 -0
  35. package/src/wakeups.mjs +2 -2
  36. package/src/winenv.mjs +1 -1
  37. package/src/workspace-api.mjs +21 -4
  38. package/vendor/client-sdk/index.cjs +5 -4
  39. package/vendor/cloud-cli/mlai.mjs +184 -28
  40. package/worker/architecture_worker.py +33 -4
  41. package/worker/browser_tool.py +19 -1
  42. package/worker/build_planner.py +75 -7
  43. package/worker/chat_fork.py +4 -1
  44. package/worker/checkpoints.py +71 -8
  45. package/worker/cloud_pty.py +91 -2
  46. package/worker/command_safety.py +80 -3
  47. package/worker/compaction_brief.py +186 -0
  48. package/worker/connected_turn.py +30 -0
  49. package/worker/context_manager.py +6 -0
  50. package/worker/conversation.py +139 -12
  51. package/worker/deck_render.js +11 -0
  52. package/worker/deck_tools.py +825 -0
  53. package/worker/git_sync.py +42 -17
  54. package/worker/goals.py +4 -0
  55. package/worker/image_generation.py +102 -23
  56. package/worker/kimi_bridge.py +580 -52
  57. package/worker/kimi_gates.py +74 -6
  58. package/worker/kimi_hook.py +213 -19
  59. package/worker/media_tools.py +182 -0
  60. package/worker/memory.py +2 -2
  61. package/worker/mlai_hooks.py +314 -5
  62. package/worker/mlai_mcp.py +49 -11
  63. package/worker/mlai_review/README.md +35 -0
  64. package/worker/mlai_review/__init__.py +1 -0
  65. package/worker/mlai_review/__main__.py +261 -0
  66. package/worker/mlai_review/_compat.py +29 -0
  67. package/worker/mlai_review/analysis/__init__.py +3 -0
  68. package/worker/mlai_review/analysis/detect.py +50 -0
  69. package/worker/mlai_review/analysis/main.py +128 -0
  70. package/worker/mlai_review/analysis/messages.py +8 -0
  71. package/worker/mlai_review/analysis/tools.py +194 -0
  72. package/worker/mlai_review/application/__init__.py +1 -0
  73. package/worker/mlai_review/application/asking.py +53 -0
  74. package/worker/mlai_review/application/challengers.py +142 -0
  75. package/worker/mlai_review/application/context_builder.py +194 -0
  76. package/worker/mlai_review/application/errors.py +17 -0
  77. package/worker/mlai_review/application/feedback.py +52 -0
  78. package/worker/mlai_review/application/matching.py +46 -0
  79. package/worker/mlai_review/application/messages.py +41 -0
  80. package/worker/mlai_review/application/ports.py +222 -0
  81. package/worker/mlai_review/application/prompts.py +342 -0
  82. package/worker/mlai_review/application/review_service.py +200 -0
  83. package/worker/mlai_review/application/reviewers.py +438 -0
  84. package/worker/mlai_review/application/streaming.py +91 -0
  85. package/worker/mlai_review/application/tools.py +138 -0
  86. package/worker/mlai_review/application/usage.py +145 -0
  87. package/worker/mlai_review/application/write_check.py +46 -0
  88. package/worker/mlai_review/contracts/__init__.py +5 -0
  89. package/worker/mlai_review/contracts/analysis.py +57 -0
  90. package/worker/mlai_review/contracts/review.py +42 -0
  91. package/worker/mlai_review/domain/__init__.py +4 -0
  92. package/worker/mlai_review/domain/changes.py +77 -0
  93. package/worker/mlai_review/domain/context.py +147 -0
  94. package/worker/mlai_review/domain/diagnostics.py +239 -0
  95. package/worker/mlai_review/domain/feedback.py +216 -0
  96. package/worker/mlai_review/domain/findings.py +505 -0
  97. package/worker/mlai_review/domain/lifecycle.py +85 -0
  98. package/worker/mlai_review/domain/partition.py +42 -0
  99. package/worker/mlai_review/domain/relations.py +166 -0
  100. package/worker/mlai_review/domain/relevance.py +202 -0
  101. package/worker/mlai_review/domain/report.py +99 -0
  102. package/worker/mlai_review/domain/structure.py +56 -0
  103. package/worker/mlai_review/domain/writes.py +86 -0
  104. package/worker/mlai_review/eval.py +221 -0
  105. package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
  106. package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
  107. package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
  108. package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
  109. package/worker/mlai_review/feedback_report.py +60 -0
  110. package/worker/mlai_review/infrastructure/__init__.py +1 -0
  111. package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
  112. package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
  113. package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
  114. package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
  115. package/worker/mlai_review/infrastructure/github/client.py +445 -0
  116. package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
  117. package/worker/mlai_review/infrastructure/github/http.py +31 -0
  118. package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
  119. package/worker/mlai_review/infrastructure/github/token.py +20 -0
  120. package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
  121. package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
  122. package/worker/mlai_review/infrastructure/local/events.py +100 -0
  123. package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
  124. package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
  125. package/worker/mlai_review/infrastructure/messages.py +54 -0
  126. package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
  127. package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
  128. package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
  129. package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
  130. package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
  131. package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
  132. package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
  133. package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
  134. package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
  135. package/worker/mlai_review/pipeline.py +89 -0
  136. package/worker/mlai_review/runtime.py +194 -0
  137. package/worker/mlai_review/settings.py +139 -0
  138. package/worker/model_stream.py +21 -7
  139. package/worker/offline_guard.py +1 -1
  140. package/worker/preview.py +62 -4
  141. package/worker/product_facts.py +20 -0
  142. package/worker/project_context.py +33 -7
  143. package/worker/reply_language.py +92 -10
  144. package/worker/repo_policy.py +5 -0
  145. package/worker/roles.json +199 -37
  146. package/worker/roles.py +271 -9
  147. package/worker/runner.py +366 -53
  148. package/worker/runtime_control.py +64 -3
  149. package/worker/secret_scan.py +3 -2
  150. package/worker/server_memory.py +122 -0
  151. package/worker/site_check.py +279 -0
  152. package/worker/skill_library.py +15 -3
  153. package/worker/skill_pack.py +170 -16
  154. package/worker/subagents.py +6 -2
  155. package/worker/tool_catalog.py +4 -3
  156. package/worker/verification.py +156 -19
  157. package/worker/wakeups.py +56 -4
@@ -68,17 +68,21 @@ def clean(text):
68
68
  def friendly(stderr, fallback):
69
69
  s = stderr.lower()
70
70
  if 'could not read username' in s or 'authentication failed' in s or 'invalid username or password' in s or 'terminal prompts disabled' in s:
71
- return 'GitHub did not accept the connection. Connect GitHub with a token that has Contents read and write access to this repository.'
71
+ return 'GitHub did not accept your key. It may have expired. Connect GitHub again with a new key, then retry.'
72
72
  if 'repository not found' in s or 'not found' in s and 'remote' in s:
73
- return 'GitHub could not find this repository, or the connected token cannot see it. Check the URL and the token\'s repository access.'
73
+ return 'GitHub could not find this repository with your key. Check that your GitHub account can open it, then connect GitHub again with a new key and retry.'
74
74
  if 'protected branch' in s or 'gh006' in s or 'gh013' in s:
75
- return 'This branch is protected on GitHub. Publish a draft PR instead.'
75
+ return 'GitHub does not allow pushing straight to this branch (it is protected). Choose Draft PR instead; it sends the same changes for review.'
76
76
  if 'permission to' in s and 'denied' in s or 'write access to repository not granted' in s or ' 403' in s:
77
- return 'The connected token can read this repository but cannot write to it. Give it Contents: Read and write.'
77
+ return 'Your GitHub key can read this repository but not save to it. Connect GitHub again with a new key (the Make a key button ticks the right boxes), then retry.'
78
78
  if 'non-fast-forward' in s or 'fetch first' in s or 'rejected' in s:
79
- return 'GitHub has newer commits on this branch. Sync first, then push.'
79
+ return 'Someone pushed to GitHub after you started. Press Get latest, then push again. Your work is kept.'
80
80
  if 'workflow' in s and 'scope' in s:
81
- return 'Changing files under .github/workflows needs a token with the Workflows permission.'
81
+ return 'Your changes touch .github/workflows, and your GitHub key is not allowed to change those. Connect GitHub again with a new key (the Make a key button ticks the right boxes), then retry.'
82
+ if 'large files' in s or 'exceeds github' in s or 'gh001' in s:
83
+ return 'A file is bigger than GitHub allows (100 MB). Remove it or add it to .gitignore, then push again.'
84
+ if 'could not resolve host' in s or 'timed out' in s or 'connection reset' in s:
85
+ return 'Could not reach GitHub just now. Check that GitHub is up and press retry.'
82
86
  tail = clean(stderr).strip().splitlines()[-3:]
83
87
  return fallback + (': ' + ' '.join(tail) if tail else '')
84
88
 
@@ -140,8 +144,8 @@ def snapshot(repo, baseline):
140
144
  fd, index = tempfile.mkstemp(prefix='mini-sync-index-')
141
145
  os.close(fd)
142
146
  os.unlink(index)
143
- env = git_env({'GIT_INDEX_FILE': index, 'GIT_AUTHOR_NAME': 'Mini Code', 'GIT_AUTHOR_EMAIL': 'workspace@mini-code.local',
144
- 'GIT_COMMITTER_NAME': 'Mini Code', 'GIT_COMMITTER_EMAIL': 'workspace@mini-code.local'})
147
+ env = git_env({'GIT_INDEX_FILE': index, 'GIT_AUTHOR_NAME': 'ML AI Code', 'GIT_AUTHOR_EMAIL': 'workspace@code.ml.ai',
148
+ 'GIT_COMMITTER_NAME': 'ML AI Code', 'GIT_COMMITTER_EMAIL': 'workspace@code.ml.ai'})
145
149
  try:
146
150
  git(['read-tree', baseline], repo, env=env)
147
151
  try:
@@ -191,8 +195,8 @@ def secrets_added(repo, baseline, work):
191
195
 
192
196
 
193
197
  def identity_env(a):
194
- name = (a.get('authorName') or '').strip()[:100] or 'Mini Code'
195
- email = (a.get('authorEmail') or '').strip()[:200] or 'workspace@mini-code.local'
198
+ name = (a.get('authorName') or '').strip()[:100] or 'ML AI Code'
199
+ email = (a.get('authorEmail') or '').strip()[:200] or 'workspace@code.ml.ai'
196
200
  return git_env({'GIT_AUTHOR_NAME': name, 'GIT_AUTHOR_EMAIL': email, 'GIT_COMMITTER_NAME': name, 'GIT_COMMITTER_EMAIL': email})
197
201
 
198
202
 
@@ -212,6 +216,25 @@ def act_clone(a):
212
216
  return {'branch': branch, 'sha': sha}
213
217
 
214
218
 
219
+ def act_link(repo, a):
220
+ """Point a blank chat at a repository that was just created on GitHub and return its first commit."""
221
+ if not (Path(repo) / '.git').exists():
222
+ git(['init', '-q'], repo)
223
+ ensure_remote(repo, a)
224
+ branch = valid_branch(a.get('branch'))
225
+ last = None
226
+ # A repository created a moment ago can take a few seconds before its first commit can be fetched.
227
+ for wait in (0, 1, 2, 3, 4):
228
+ if wait:
229
+ import time
230
+ time.sleep(wait)
231
+ try:
232
+ return {'remoteSha': fetch(repo, branch), 'branch': branch}
233
+ except SyncError as e:
234
+ last = e
235
+ raise last
236
+
237
+
215
238
  def act_status(repo, a):
216
239
  ensure_remote(repo, a)
217
240
  base, branch = a['baseline'], a['branch']
@@ -260,16 +283,16 @@ def act_push(repo, a):
260
283
  work = snapshot(repo, base)
261
284
  leaked = secrets_added(repo, base, work)
262
285
  if leaked and not a.get('allowSecrets'):
263
- raise SyncError('Refusing to push files that usually hold secrets: ' + ', '.join(leaked[:5]) + '. Remove them or add them to .gitignore.')
286
+ raise SyncError('Not pushed: ' + ', '.join(leaked[:5]) + ' usually hold passwords or keys. Add them to .gitignore (or ask the agent to), then push again.')
264
287
  env = identity_env(a)
265
288
  if mode == 'direct':
266
289
  if remote != a.get('remoteSha'):
267
- raise SyncError('GitHub has newer commits on ' + branch + '. Sync first, then push.')
290
+ raise SyncError('GitHub has newer commits on ' + branch + '. Press Get latest, then push again. Your work is kept.')
268
291
  tree, conflicts = replay(repo, base, work, remote)
269
292
  if conflicts:
270
- raise SyncError('Changes do not apply cleanly to ' + branch + ': ' + ', '.join(conflicts[:5]))
293
+ raise SyncError('Your changes and the newest GitHub commits edit the same lines in ' + ', '.join(conflicts[:5]) + '. Ask the agent to merge them, then push again.')
271
294
  if tree == out(['rev-parse', remote + '^{tree}'], repo):
272
- raise SyncError('No changes to push')
295
+ raise SyncError('Nothing new to push. GitHub already has all of your changes.')
273
296
  commit = out(['commit-tree', tree, '-p', remote, '-m', message], repo, env=env)
274
297
  git(['push', '--porcelain', origin_url(a), commit + ':refs/heads/' + branch], repo, timeout=600)
275
298
  git(['update-ref', 'refs/remotes/origin/' + branch, commit], repo)
@@ -283,7 +306,7 @@ def act_push(repo, a):
283
306
  ensure_commit(repo, onto)
284
307
  tree, conflicts = replay(repo, base, work, onto)
285
308
  if conflicts:
286
- raise SyncError('Changes do not apply cleanly: ' + ', '.join(conflicts[:5]) + '. Sync first.')
309
+ raise SyncError('Your changes and the newest GitHub commits edit the same lines in ' + ', '.join(conflicts[:5]) + '. Press Get latest, or ask the agent to merge them.')
287
310
  parent, force = onto, True
288
311
  previous = a.get('previousHead')
289
312
  if previous and a.get('previousBase') == onto:
@@ -295,7 +318,7 @@ def act_push(repo, a):
295
318
  if tree == out(['rev-parse', parent + '^{tree}'], repo):
296
319
  if parent != onto:
297
320
  return {'sha': parent, 'branch': target, 'unchanged': True}
298
- raise SyncError('No changes to publish')
321
+ raise SyncError('Nothing new to send. GitHub already has all of your changes.')
299
322
  commit = out(['commit-tree', tree, '-p', parent, '-m', message], repo, env=env)
300
323
  refspec = ('+' if force else '') + commit + ':refs/heads/' + target
301
324
  git(['push', '--porcelain', origin_url(a), refspec], repo, timeout=600)
@@ -309,6 +332,8 @@ def main(argv):
309
332
  try:
310
333
  if action == 'clone':
311
334
  result = act_clone(a)
335
+ elif action == 'link':
336
+ result = act_link(a.get('repo') or os.getcwd(), a)
312
337
  else:
313
338
  repo = a.get('repo') or os.getcwd()
314
339
  if not (Path(repo) / '.git').exists():
@@ -321,7 +346,7 @@ def main(argv):
321
346
  provider = os.environ.get('MINI_GIT_PROVIDER', '')
322
347
  message = clean(str(e))
323
348
  if provider and provider != 'GitHub':
324
- message = message.replace('GitHub', provider).replace('a draft PR', 'a merge request')
349
+ message = message.replace('GitHub', provider).replace('a draft PR', 'a merge request').replace('Draft PR', 'Merge request')
325
350
  print(json.dumps({'ok': False, 'error': message}))
326
351
  except KeyError:
327
352
  print(json.dumps({'ok': False, 'error': 'Unknown sync action'}))
package/worker/goals.py CHANGED
@@ -153,4 +153,8 @@ def wake_prompt(wake,goal):
153
153
  wake=wake if isinstance(wake,dict) else {}
154
154
  if wake.get('kind')=='goal' and goal and goal.get('status')=='working':
155
155
  return ('Resuming the goal you are working toward: %s\nLast verifier result: %s\nContinue until it holds.'%(goal['condition'],goal.get('reason') or 'not checked yet'))
156
+ if wake.get('kind')=='verify':
157
+ return ('Continue the previous task automatically. '+(str(wake.get('note') or ''))[:1000]+'\n'
158
+ 'Inspect the current files and the running preview, fix every open problem, rerun the relevant checks, and finish what the user asked for. '
159
+ 'Do not redo work that already passes. End with a complete, self-contained summary for the user: what is now working, how you checked it, and anything still open.')
156
160
  return 'Scheduled wakeup. You asked to be resumed now with this note: '+(str(wake.get('note') or 'check on the pending work'))[:1000]+'\nCheck on it and continue the work.'
@@ -3,23 +3,64 @@ A local gateway owns the provider key; child commands receive only a run-scoped
3
3
  Laptop runs have no provider key: with MINI_IMAGE_GATEWAY set, the same local gateway forwards to the app server's
4
4
  images endpoint with the run token (MODEL_API_KEY), which the server caps per run. Tools never see that token either.
5
5
  """
6
- import base64, hashlib, hmac, json, os, secrets, threading, urllib.request, urllib.error
6
+ import base64, hashlib, hmac, json, math, os, time, secrets, threading, urllib.request, urllib.error
7
7
  from pathlib import Path
8
8
  from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
9
9
 
10
10
  DEFAULT_MODEL = 'bytedance-seed/seedream-5-0-flash'
11
11
  ASPECTS = ('1:1', '16:9', '9:16', '4:3', '3:4', '3:2', '2:3')
12
12
  MAX_IMAGE_BYTES = 12_000_000
13
- TOOL = {'name': 'generate_image', 'description': 'Generate one original UI image (hero, illustration, background) and save it under public/generated/. Returns the actual file path and cost. Maximum four provider attempts per run; identical assets are reused. Use CSS/SVG for controls, charts and simple icons. Never send secrets or private code in prompts.',
13
+ # Per-run cap the person sets in Settings ("Images per task"); 6 at most.
14
+ IMAGE_LIMIT_MAX = 6
15
+ # Images are billed in credits at the provider's reported cost plus this margin (lib/plans.ts IMAGE_MARGIN).
16
+ IMAGE_MARGIN = 0.15
17
+ CREDIT_PRICE_USD = 0.125 # lib/plans.ts CREDIT_PRICE_USD; 1 credit = 1M billable tokens
18
+ FALLBACK_IMAGE_COST = 0.018 # the default model's list price, used when the response carries no cost
19
+ _BILLED = {'tokens': 0, 'images': 0}
20
+ _BILLED_LOCK = threading.Lock()
21
+
22
+ def image_tokens(cost):
23
+ """Billable tokens for one generated image: provider cost plus margin, in credits (1 credit = 1M tokens)."""
24
+ usd = cost if isinstance(cost, (int, float)) and not isinstance(cost, bool) and 0 <= cost < 5 else FALLBACK_IMAGE_COST
25
+ return int(math.ceil(round(usd * (1 + IMAGE_MARGIN) / CREDIT_PRICE_USD * 1_000_000, 3)))
26
+
27
+ def billed_tokens():
28
+ """Image tokens billed so far by this process (cloud runs; laptop images are metered by the server)."""
29
+ with _BILLED_LOCK: return _BILLED['tokens']
30
+
31
+ def reset_billed():
32
+ with _BILLED_LOCK: _BILLED['tokens'] = 0; _BILLED['images'] = 0
33
+
34
+ def image_limit(config):
35
+ value = (config or {}).get('imageLimit')
36
+ if isinstance(value, bool) or not isinstance(value, (int, float)): return IMAGE_LIMIT_MAX
37
+ return max(0, min(IMAGE_LIMIT_MAX, int(value)))
38
+ # The person pressed Reject on the approval card. Say that plainly so the agent does not blame the image service or retry (BUG-0103).
39
+ IMAGE_REJECTED = 'The user rejected this image on the approval card. Nothing was generated. Do not retry this image unless the user asks for it.'
40
+ TOOL = {'name': 'generate_image', 'description': 'Generate one original UI image (hero, illustration, background) and save it under public/generated/. Returns the actual file path and cost. Up to six images per run (the person may set fewer in Settings); each new image uses credits, identical assets are reused for free. Use CSS/SVG for controls, charts and simple icons. Never send secrets or private code in prompts.',
14
41
  'inputSchema': {'type': 'object', 'properties': {'prompt': {'type': 'string', 'maxLength': 4000}, 'filename': {'type': 'string', 'description': 'New two-word kebab filename, e.g. dashboard-hero.png. Longer names are shortened to two words; use the returned path. Never overwrite existing assets.'}, 'aspect_ratio': {'type': 'string', 'enum': list(ASPECTS)}}, 'required': ['prompt', 'filename'], 'additionalProperties': False}}
15
42
 
43
+ def plain_filename(filename):
44
+ """A plain image file name: 'Project Hero.PNG' becomes 'Project-Hero.png'. Paths are refused, never shortened, so the
45
+ agent learns to send a name; the error quotes what was sent so the next call can fix it instead of repeating it."""
46
+ import re
47
+ shown = repr(filename if isinstance(filename, str) else type(filename).__name__)[:90]
48
+ example = ' Example: project-hero.png'
49
+ if not isinstance(filename, str) or not filename.strip(): raise ValueError('Use a simple PNG, JPG or WebP filename (got %s).%s' % (shown, example))
50
+ name = filename.strip()
51
+ if '/' in name or '\\' in name: raise ValueError('Use a file name, not a path (got %s); images are always saved under public/generated/.%s' % (shown, example))
52
+ match = re.fullmatch(r'(.+)\.(png|jpg|jpeg|webp)', name, re.I)
53
+ if not match: raise ValueError('Use a simple PNG, JPG or WebP filename (got %s).%s' % (shown, example))
54
+ stem = re.sub(r'[^a-zA-Z0-9_-]+', '-', match.group(1)).strip('-_')
55
+ if not re.fullmatch(r'[a-zA-Z0-9][a-zA-Z0-9_-]{0,79}', stem): raise ValueError('Use a simple PNG, JPG or WebP filename: letters, digits and dashes, up to 80 characters (got %s).%s' % (shown, example))
56
+ return stem + '.' + match.group(2).lower()
57
+
16
58
  def validate(args):
17
59
  import re
18
60
  if not isinstance(args, dict) or set(args) - {'prompt', 'filename', 'aspect_ratio'}: raise ValueError('Use prompt, filename and aspect_ratio only')
19
61
  prompt = args.get('prompt', '')
20
62
  if not isinstance(prompt, str) or not 10 <= len(prompt.strip()) <= 4000: raise ValueError('Provide an image prompt of 10-4000 characters')
21
- filename = args.get('filename', '')
22
- if not isinstance(filename, str) or not re.fullmatch(r'[a-zA-Z0-9][a-zA-Z0-9_-]{0,79}\.(png|jpg|jpeg|webp)', filename): raise ValueError('Use a simple PNG, JPG or WebP filename')
63
+ filename = plain_filename(args.get('filename', ''))
23
64
  aspect = args.get('aspect_ratio', '1:1')
24
65
  if aspect not in ASPECTS: raise ValueError('Unsupported aspect ratio')
25
66
  stem, ext = filename.rsplit('.', 1)
@@ -46,7 +87,13 @@ def two_word_stem(stem, prompt=''):
46
87
 
47
88
  OPENROUTER_IMAGES = 'https://openrouter.ai/api/v1/images'
48
89
 
49
- def request_image(key, model, args, url=OPENROUTER_IMAGES):
90
+ # App gateway codes (lib/laptop-gateway.ts) returned before the paid image call: the metering service did not answer.
91
+ NOT_CHARGED = ('meter_unavailable',)
92
+ METER_RETRY_SECONDS = 2
93
+ class NotCharged(ValueError):
94
+ """The request was refused before the image service was called; it does not use the per-task image allowance."""
95
+
96
+ def request_image(key, model, args, url=OPENROUTER_IMAGES, _retried=False):
50
97
  payload = {'model': model, 'prompt': args['prompt'], 'aspect_ratio': args['aspect_ratio'], 'resolution': '1K', 'n': 1}
51
98
  req = urllib.request.Request(url, data=json.dumps(payload).encode(), headers={'Authorization': 'Bearer '+key, 'Content-Type': 'application/json'})
52
99
  try:
@@ -56,11 +103,19 @@ def request_image(key, model, args, url=OPENROUTER_IMAGES):
56
103
  data = json.loads(raw)
57
104
  except urllib.error.HTTPError as exc:
58
105
  # The app gateway returns a stable, key-free message (limit reached, run ended); OpenRouter bodies are never shown.
59
- message = None
106
+ message = code = None
60
107
  if url != OPENROUTER_IMAGES:
61
- try: message = str((json.loads(exc.read(20000)).get('error') or {}).get('message') or '')[:300] or None
62
- except (ValueError, AttributeError, OSError): message = None
108
+ try:
109
+ error = json.loads(exc.read(20000)).get('error') or {}
110
+ message, code = str(error.get('message') or '')[:300] or None, error.get('code')
111
+ except (ValueError, AttributeError, OSError): message = code = None
63
112
  exc.close()
113
+ if code in NOT_CHARGED:
114
+ # The app gateway refused before calling the image service, so nothing was charged: one short retry is safe.
115
+ if not _retried:
116
+ time.sleep(METER_RETRY_SECONDS)
117
+ return request_image(key, model, args, url=url, _retried=True)
118
+ raise NotCharged(message or 'Usage service is unavailable. Retry shortly.') from None
64
119
  raise ValueError(message or 'Image provider unavailable or timed out. No automatic retry was made; check usage before retrying.') from None
65
120
  except (urllib.error.URLError, TimeoutError) as exc:
66
121
  # Never expose provider bodies, authorization headers, or implicit retries of paid calls.
@@ -78,11 +133,12 @@ def request_image(key, model, args, url=OPENROUTER_IMAGES):
78
133
  return image, ext, cost if isinstance(cost, (float, int)) and cost >= 0 else None
79
134
 
80
135
  class Generator:
81
- def __init__(self, root, repo, key, emit=lambda *a, **kw: None, provider=request_image):
136
+ def __init__(self, root, repo, key, emit=lambda *a, **kw: None, provider=request_image, limit=IMAGE_LIMIT_MAX, bill=False):
82
137
  self.root, self.repo = Path(root), Path(repo).resolve()
83
138
  self.key, self.emit, self.provider = key, emit, provider
139
+ self.limit, self.bill = max(0, min(IMAGE_LIMIT_MAX, int(limit))), bill
84
140
  self.model = os.getenv('OPENROUTER_IMAGE_MODEL') or DEFAULT_MODEL
85
- self.attempts = 0
141
+ self.attempts = self.refunds = 0
86
142
  self.lock = threading.Lock()
87
143
  def generate(self, args):
88
144
  args = validate(args)
@@ -105,13 +161,16 @@ class Generator:
105
161
  # Reserve the complete stem so an unexpected response format cannot overwrite another asset.
106
162
  stem = Path(args['filename']).stem
107
163
  if any(directory.glob(stem+'.*')): raise ValueError('Asset filename already exists. Choose a new name.')
108
- if self.attempts >= 4: raise ValueError('Image generation limit reached (4 provider attempts per run). Reuse existing assets.')
164
+ if self.limit == 0: raise ValueError('Image generation is turned off for this task (Settings > Images per task). Use existing images, stock photos or CSS/SVG.')
165
+ if self.attempts >= self.limit: raise ValueError('Image limit reached (%d per task, set in Settings). Reuse existing assets.' % self.limit)
109
166
  self.attempts += 1
110
167
  self.emit('image_generation', state='running', content='Generating '+args['filename'])
111
168
  try: image, ext, cost = self.provider(self.key, self.model, args)
112
- except Exception:
169
+ except Exception as exc:
113
170
  # Close the gallery tile: without this the app would show the image as still being created.
114
171
  self.emit('image_generation', state='failed', content='Could not create '+args['filename'])
172
+ # Nothing was charged: give the attempt back (bounded, so an outage cannot loop for ever).
173
+ if isinstance(exc, NotCharged) and self.refunds < self.limit: self.attempts -= 1; self.refunds += 1
115
174
  raise
116
175
  if (self.root/'cancel').exists(): raise ValueError('Task cancelled after generation; the provider may have charged this request')
117
176
  # Recheck paths after the network wait before writing.
@@ -119,35 +178,55 @@ class Generator:
119
178
  path = directory/(stem+ext)
120
179
  with path.open('xb') as f: f.write(image)
121
180
  record = {'path': str(path.relative_to(self.repo)), 'url': '/generated/'+path.name, 'model': self.model, 'cost': cost, 'digest': digest, 'sha256': hashlib.sha256(image).hexdigest(), 'aspect_ratio': args['aspect_ratio']}
181
+ if self.bill:
182
+ tokens = image_tokens(cost)
183
+ with _BILLED_LOCK: _BILLED['tokens'] += tokens; _BILLED['images'] += 1
184
+ record['credits'] = round(tokens / 1_000_000, 3)
122
185
  records.append(record)
123
186
  temp = manifest_path.with_suffix('.tmp');temp.write_text(json.dumps(records[-200:]));temp.replace(manifest_path)
124
187
  self.emit('image_generation', state='completed', content='Saved '+record['path'], asset=record)
125
188
  return {**record, 'cached': False}
126
189
 
127
190
  def start_gateway(root, repo, config, emit):
128
- """Call before spawning tools; remove the paid provider credential from inherited environments."""
191
+ """Call before spawning tools; remove the paid provider credentials from inherited environments.
192
+ One loopback server answers /generate (images), /stock (Pixabay photos) and /brand (Brandfetch)."""
193
+ from media_tools import Media, NOT_CONFIGURED
194
+ reset_billed() # one gateway per run: never carry an earlier run's image charges into this one
129
195
  key = os.environ.pop('OPENROUTER_API_KEY', '')
196
+ stock_key, brand_key = os.environ.pop('PIXABAY_API_KEY', ''), os.environ.pop('BRANDFETCH_API_KEY', '')
130
197
  if config.get('subagent') or config.get('mode') == 'plan':
131
198
  os.environ.pop('MINI_IMAGE_URL', None);os.environ.pop('MINI_IMAGE_TOKEN', None)
132
199
  return None
133
- gateway = os.getenv('MINI_IMAGE_GATEWAY', '')
134
- if not key and gateway.startswith(('https://', 'http://127.0.0.1', 'http://localhost')) and os.getenv('MODEL_API_KEY'):
135
- # Laptop: the run token stays in this process; the server holds the provider key and counts attempts.
136
- generator = Generator(root, repo, os.environ['MODEL_API_KEY'], emit, provider=lambda k, m, a: request_image(k, m, a, url=gateway))
137
- elif key: generator = Generator(root, repo, key, emit)
138
- else: return None
200
+ gateway, media_gateway = os.getenv('MINI_IMAGE_GATEWAY', ''), os.getenv('MINI_MEDIA_GATEWAY', '')
201
+ remote_ok = lambda url: url.startswith(('https://', 'http://127.0.0.1', 'http://localhost')) and bool(os.getenv('MODEL_API_KEY'))
202
+ generator = media = None
203
+ if key: generator = Generator(root, repo, key, emit, limit=image_limit(config), bill=True)
204
+ elif remote_ok(gateway):
205
+ # Laptop: the run token stays in this process; the server holds the provider keys and counts attempts.
206
+ # The server meters these images on the account (laptop-gateway.ts), so the worker does not bill them again.
207
+ generator = Generator(root, repo, os.environ['MODEL_API_KEY'], emit, provider=lambda k, m, a: request_image(k, m, a, url=gateway), limit=image_limit(config))
208
+ if stock_key or brand_key: media = Media(root, repo, stock_key, brand_key, emit=emit)
209
+ elif remote_ok(media_gateway): media = Media(root, repo, remote=(media_gateway, os.environ['MODEL_API_KEY']), emit=emit)
210
+ if not generator and not media: return None
139
211
  token = secrets.token_urlsafe(32)
140
212
  class Handler(BaseHTTPRequestHandler):
141
213
  def log_message(self, *args): pass
142
214
  def do_POST(self):
143
- if self.path != '/generate' or not hmac.compare_digest(self.headers.get('Authorization', ''), 'Bearer '+token):
215
+ if self.path not in ('/generate', '/stock', '/brand') or not hmac.compare_digest(self.headers.get('Authorization', ''), 'Bearer '+token):
144
216
  self.send_error(403);return
145
217
  try:
146
218
  size = int(self.headers.get('Content-Length', '0'))
147
- if not 0 < size <= 20000: raise ValueError('Invalid image request size')
148
- value = generator.generate(json.loads(self.rfile.read(size)))
219
+ if not 0 < size <= 20000: raise ValueError('Invalid request size')
220
+ args = json.loads(self.rfile.read(size))
221
+ if not isinstance(args, dict): raise ValueError('Invalid request')
222
+ if self.path == '/generate':
223
+ if not generator: raise ValueError('Image generation is not configured. Set OPENROUTER_API_KEY on the application server.')
224
+ value = generator.generate(args)
225
+ else:
226
+ if not media: raise ValueError(NOT_CONFIGURED)
227
+ value = media.stock(args) if self.path == '/stock' else media.brand(args)
149
228
  body, status = {'result': value}, 200
150
- except (ValueError, OSError, TypeError) as exc:
229
+ except (ValueError, OSError, TypeError, KeyError, urllib.error.URLError) as exc:
151
230
  body, status = {'error': str(exc)[:300]}, 400
152
231
  encoded = json.dumps(body).encode();self.send_response(status);self.send_header('Content-Type','application/json');self.end_headers();self.wfile.write(encoded)
153
232
  server = ThreadingHTTPServer(('127.0.0.1', 0), Handler)