mlai-code 0.3.2 → 0.3.3
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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/cli.mjs +27 -7
- package/src/engine.mjs +1 -1
- package/src/home.mjs +1 -1
- package/src/parity.mjs +3 -1
- package/src/render.mjs +3 -1
- package/src/runtime.mjs +21 -1
- package/src/sandbox.mjs +19 -3
- package/src/session.mjs +22 -2
- package/src/shared-data.mjs +1 -1
- package/src/tui/app.mjs +22 -4
- package/src/tui/chat.mjs +8 -6
- package/src/tui/cmd-core.mjs +2 -2
- package/src/tui/cmd-extensibility.mjs +8 -2
- package/src/tui/commands.mjs +2 -1
- package/src/tui/statusline.mjs +2 -1
- package/src/wakeups.mjs +2 -2
- package/vendor/client-sdk/index.cjs +3 -3
- package/vendor/cloud-cli/mlai.mjs +169 -25
- package/worker/build_planner.py +75 -7
- package/worker/cloud_pty.py +91 -2
- package/worker/command_safety.py +25 -1
- package/worker/compaction_brief.py +186 -0
- package/worker/context_manager.py +6 -0
- package/worker/conversation.py +58 -1
- package/worker/deck_render.js +11 -0
- package/worker/deck_tools.py +825 -0
- package/worker/git_sync.py +38 -13
- package/worker/goals.py +4 -0
- package/worker/image_generation.py +102 -23
- package/worker/kimi_bridge.py +340 -32
- package/worker/kimi_gates.py +49 -0
- package/worker/kimi_hook.py +65 -12
- package/worker/media_tools.py +179 -0
- package/worker/memory.py +2 -2
- package/worker/mlai_hooks.py +93 -3
- package/worker/mlai_mcp.py +23 -5
- package/worker/model_stream.py +21 -7
- package/worker/offline_guard.py +1 -1
- package/worker/preview.py +47 -4
- package/worker/product_facts.py +18 -0
- package/worker/project_context.py +33 -7
- package/worker/reply_language.py +92 -10
- package/worker/roles.json +199 -37
- package/worker/roles.py +243 -8
- package/worker/runner.py +241 -36
- package/worker/site_check.py +279 -0
- package/worker/skill_library.py +15 -3
- package/worker/skill_pack.py +170 -16
- package/worker/tool_catalog.py +3 -2
- package/worker/verification.py +43 -11
- package/worker/wakeups.py +56 -4
package/worker/git_sync.py
CHANGED
|
@@ -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
|
|
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
|
|
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 '
|
|
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 '
|
|
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 '
|
|
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 '
|
|
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
|
|
|
@@ -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('
|
|
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 + '.
|
|
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('
|
|
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('
|
|
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('
|
|
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('
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
62
|
-
|
|
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.
|
|
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
|
|
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
|
|
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
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
elif
|
|
138
|
-
|
|
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
|
|
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
|
|
148
|
-
|
|
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('Stock photos and brand lookup are not configured on this server.')
|
|
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)
|