mlai-code 0.3.1 → 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 +2 -2
- package/src/cli.mjs +27 -7
- package/src/distribution.mjs +5 -5
- package/src/engine-key.mjs +18 -4
- package/src/engine.mjs +4 -9
- 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 +9 -3
- 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 +406 -40
- package/worker/kimi_gates.py +84 -1
- package/worker/kimi_hook.py +113 -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/obs_mask.py +97 -0
- 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 +253 -42
- 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/kimi_gates.py
CHANGED
|
@@ -14,7 +14,7 @@ import json, os, re, subprocess, sys, time
|
|
|
14
14
|
from pathlib import Path
|
|
15
15
|
|
|
16
16
|
ACCEPTANCE_DIR = 'acceptance'
|
|
17
|
-
TRIM_LIMIT = int(os.getenv('MINI_KIMI_TRIM_CHARS', '
|
|
17
|
+
TRIM_LIMIT = int(os.getenv('MINI_KIMI_TRIM_CHARS', '12000'))
|
|
18
18
|
ERROR_LINE = re.compile(r'error|fail|exception|traceback|assert|panic|cannot|denied|not found|undefined|expected', re.I)
|
|
19
19
|
|
|
20
20
|
# ---------------------------------------------------------------- output trimming
|
|
@@ -115,6 +115,13 @@ def tester_invalid(language, output):
|
|
|
115
115
|
if re.search(r'AssertionError|ERR_ASSERTION', output): return False
|
|
116
116
|
return bool(re.search(r'ERR_MODULE_NOT_FOUND|SyntaxError|Cannot find module|ReferenceError|does not provide an export', output)) or '# fail 0' in output
|
|
117
117
|
|
|
118
|
+
TESTER_RETRY = '''
|
|
119
|
+
|
|
120
|
+
This is a second try. Your previous test could not run; the error is in `error`. Fix the test itself (imports, paths,
|
|
121
|
+
module format, a missing loader) and return the same JSON shape. Test through files and commands that run in plain
|
|
122
|
+
Node or Python here: for a React or other bundled UI, test the server, data and pure modules, or read the built output,
|
|
123
|
+
instead of importing JSX. If no test can run in this repository, return {"applicable": false, "reason": "..."}.'''
|
|
124
|
+
|
|
118
125
|
REVIEW_SYSTEM = '''You check failing acceptance tests before they are reported to a builder. For each failing test, decide whether the test itself is right: does the task text really require this behaviour, and is the expected value correct? Recompute every expected number step by step from the task. A test is wrong if it asserts something the task does not require, or if its expected value is miscalculated.
|
|
119
126
|
|
|
120
127
|
Reply with JSON only: {"genuine": ["test_name", ...], "wrong": [{"test": "test_name", "why": "..."}]}'''
|
|
@@ -172,6 +179,24 @@ def acceptance(config, st, model, root, repo, changed, event):
|
|
|
172
179
|
event('acceptance', status='skipped', content='Acceptance check not applicable: ' + str(reason)[:300])
|
|
173
180
|
return {'status': 'skipped', 'reason': str(reason)[:300]}
|
|
174
181
|
code, output = run_acceptance(root, repo, spec['language'], spec['code'])
|
|
182
|
+
if code not in (0, 124) and tester_invalid(spec['language'], output):
|
|
183
|
+
# Web bug list Bug-017 and Bug-021: a test that cannot even load (JSX imported into plain Node, a wrong path)
|
|
184
|
+
# says nothing about the build. Give the tester its own error once and run the corrected test.
|
|
185
|
+
retry = json.dumps({'task': str(config.get('task', ''))[:12000], 'languages_changed': sorted(langs), 'repository_files': files,
|
|
186
|
+
'previous_test': spec['code'][:20000], 'error': output[-3000:]})
|
|
187
|
+
try:
|
|
188
|
+
fixed_text, _ = model.request([{'role': 'system', 'content': TESTER_SYSTEM + TESTER_RETRY},
|
|
189
|
+
{'role': 'user', 'content': retry}], max_tokens=12000, allow_tools=False)
|
|
190
|
+
fixed = _parse(fixed_text)
|
|
191
|
+
except Exception:
|
|
192
|
+
fixed = None
|
|
193
|
+
if fixed and fixed.get('applicable') and fixed.get('language') == spec['language'] and isinstance(fixed.get('code'), str) and len(fixed['code']) <= 40000:
|
|
194
|
+
spec = {**spec, **fixed}
|
|
195
|
+
code, output = run_acceptance(root, repo, spec['language'], spec['code'])
|
|
196
|
+
elif isinstance(fixed, dict) and fixed.get('applicable') is False:
|
|
197
|
+
reason = str(fixed.get('reason') or 'the tester could not write a test that runs here')[:300]
|
|
198
|
+
event('acceptance', status='skipped', content='Acceptance check not applicable: ' + reason)
|
|
199
|
+
return {'status': 'skipped', 'reason': reason}
|
|
175
200
|
covers = [str(c)[:200] for c in spec.get('covers') or []][:8]
|
|
176
201
|
if code == 0: status = 'passed'
|
|
177
202
|
elif code != 124 and tester_invalid(spec['language'], output): status = 'invalid'
|
|
@@ -215,6 +240,35 @@ def pre_existing_failure(st, checks, repo, done, event):
|
|
|
215
240
|
return True
|
|
216
241
|
return False
|
|
217
242
|
|
|
243
|
+
# Off by default: every code change gets the independent acceptance check. MINI_SMALL_CHANGE_LINES=N skips it for
|
|
244
|
+
# one-file edits of at most N lines that already have a passing check after the last edit.
|
|
245
|
+
SMALL_CHANGE_LINES = int(os.getenv('MINI_SMALL_CHANGE_LINES', '0') or 0)
|
|
246
|
+
|
|
247
|
+
def changed_lines(repo, base, paths):
|
|
248
|
+
"""Added + removed lines in tracked files plus the length of new untracked files."""
|
|
249
|
+
total = 0
|
|
250
|
+
try:
|
|
251
|
+
out = subprocess.run(['git', 'diff', '--numstat', base or 'HEAD', '--', *paths], cwd=repo, capture_output=True, text=True, timeout=15)
|
|
252
|
+
seen = set()
|
|
253
|
+
for line in out.stdout.splitlines():
|
|
254
|
+
parts = line.split('\t')
|
|
255
|
+
if len(parts) == 3:
|
|
256
|
+
seen.add(parts[2]); total += sum(int(x) if x.isdigit() else 200 for x in parts[:2])
|
|
257
|
+
for p in paths:
|
|
258
|
+
if p not in seen:
|
|
259
|
+
try: total += len((Path(repo) / p).read_text(errors='replace').splitlines())
|
|
260
|
+
except OSError: pass
|
|
261
|
+
except (OSError, subprocess.SubprocessError, ValueError): return 10 ** 6
|
|
262
|
+
return total
|
|
263
|
+
|
|
264
|
+
def small_change(repo, base, code_changed, checks, last_edit, last_check):
|
|
265
|
+
"""One code file, at most SMALL_CHANGE_LINES changed lines, and a passing check that ran after the last edit."""
|
|
266
|
+
if SMALL_CHANGE_LINES <= 0 or len(code_changed) != 1 or not last_edit or last_check < last_edit: return False
|
|
267
|
+
try: from runtime_control import check_status
|
|
268
|
+
except Exception: return False
|
|
269
|
+
if not any(isinstance(c, dict) and check_status(c) == 'passed' for c in checks or []): return False
|
|
270
|
+
return changed_lines(repo, base, code_changed) <= SMALL_CHANGE_LINES
|
|
271
|
+
|
|
218
272
|
def final_issues(config, st, answer, done, model, root, repo, event, allow_acceptance=True):
|
|
219
273
|
"""Blockers persist until repaired; expensive new acceptance tests are written once."""
|
|
220
274
|
import roles as role_engine
|
|
@@ -262,6 +316,30 @@ def final_issues(config, st, answer, done, model, root, repo, event, allow_accep
|
|
|
262
316
|
except Exception as exc:
|
|
263
317
|
issue = 'Verification report could not complete: ' + str(exc)[:200]
|
|
264
318
|
event('note', content=issue); issues.append(issue)
|
|
319
|
+
if allow_acceptance and changed and not st.get('inspect'):
|
|
320
|
+
# The running preview is the user's view of the work: a page that answers 500, or a linked page that 404s, is not
|
|
321
|
+
# done, whatever the final message says. Nothing listening on the preview port means there is nothing to judge.
|
|
322
|
+
try:
|
|
323
|
+
import site_check
|
|
324
|
+
routes = sorted(site_check.internal_links(repo, changed))
|
|
325
|
+
bad = site_check.live_routes(routes)
|
|
326
|
+
found = site_check.live_findings(bad)
|
|
327
|
+
if found:
|
|
328
|
+
event('verification_report', status='failed', reasons=[found[0]['message'].split('.')[0]], findings=found, checks=0, passed=0)
|
|
329
|
+
issues += [f['message'] for f in found if f['message'] not in issues]
|
|
330
|
+
except Exception as exc:
|
|
331
|
+
event('note', content='Could not check the preview routes: ' + str(exc)[:200])
|
|
332
|
+
if allow_acceptance and config.get('conversation_route') in (None, 'build'):
|
|
333
|
+
# A run must not end on "I'll now make the slides", and a presentation or PDF request must leave a file behind.
|
|
334
|
+
# Each note is sent once; if the agent still cannot do it, its own explanation stands.
|
|
335
|
+
try:
|
|
336
|
+
from conversation import deliverable_issue, stalled_promise, STALLED_NOTE
|
|
337
|
+
if 'stalled' not in done and not changed and stalled_promise(answer):
|
|
338
|
+
done.add('stalled'); issues.append(STALLED_NOTE)
|
|
339
|
+
elif 'deliverable' not in done:
|
|
340
|
+
note = deliverable_issue(config.get('task', ''), changed, repo)
|
|
341
|
+
if note: done.add('deliverable'); issues.append(note)
|
|
342
|
+
except Exception: pass # these hints never block a run
|
|
265
343
|
if config.get('selected_design'):
|
|
266
344
|
from design_director import browser_review
|
|
267
345
|
from quality import fingerprint
|
|
@@ -277,6 +355,11 @@ def final_issues(config, st, answer, done, model, root, repo, event, allow_accep
|
|
|
277
355
|
if research_required(config) or ledger.load()['sources']:
|
|
278
356
|
issue = ledger.completion_issue(answer or '')
|
|
279
357
|
if issue: issues.append(issue.replace('evidence {claims', 'mcp__mlai__evidence {claims'))
|
|
358
|
+
if allow_acceptance and code_changed and not issues and 'acceptance' not in done and small_change(repo, st.get('base'), code_changed, checks, last_edit, last_check):
|
|
359
|
+
# A small edit with a passing check after it: writing and running a hidden test costs a model call and a test run
|
|
360
|
+
# for little extra signal. Larger changes still get the independent check.
|
|
361
|
+
done.add('acceptance'); st['_acceptance_skipped'] = True
|
|
362
|
+
event('acceptance', status='skipped', content='Independent acceptance check skipped: small change with a passing check after the last edit')
|
|
280
363
|
if allow_acceptance and code_changed and not issues:
|
|
281
364
|
previous = st.get('_acceptance')
|
|
282
365
|
if 'acceptance' not in done:
|
package/worker/kimi_hook.py
CHANGED
|
@@ -30,7 +30,7 @@ def save(name, data):
|
|
|
30
30
|
def event(event_type, **data):
|
|
31
31
|
with (ROOT / 'events.jsonl').open('a') as f: f.write(json.dumps({'type': event_type, 'time': time.time(), **data}) + '\n')
|
|
32
32
|
|
|
33
|
-
HOOK_KEYS = ('recent', 'landed', 'stuck_warned', 'gates_done', 'planned', 'plan_reviewed', 'plan_dismissed', 'brief_ok', 'brief_rounds', 'brief_nudged', 'mode', 'nudged', 'diagnosed_for', 'scope', 'scope_ok', 'approved_kinds', 'calls', 'agents', 'budget_warned', 'touched', 'policy_noted', 'policy_ok_paths', 'token_stage', 'account_stop', 'account_stop_at')
|
|
33
|
+
HOOK_KEYS = ('recent', 'landed', 'stuck_warned', 'gates_done', 'planned', 'plan_reviewed', 'plan_dismissed', 'brief_ok', 'brief_rounds', 'brief_nudged', 'mode', 'nudged', 'diagnosed_for', 'scope', 'scope_ok', 'approved_kinds', 'calls', 'agents', 'budget_warned', 'touched', 'policy_noted', 'policy_ok_paths', 'token_stage', 'account_stop', 'account_stop_at', 'helpers', 'budget_next', 'budget_declined', 'plan_items', 'brief_q_nudged', 'plan_nudged')
|
|
34
34
|
def save_state(st):
|
|
35
35
|
"""The hook owns only these keys; the bridge owns kimi-state.json (static) and kimi-progress.json."""
|
|
36
36
|
save('kimi-hook.json', {k: st[k] for k in HOOK_KEYS if k in st})
|
|
@@ -38,6 +38,22 @@ def save_state(st):
|
|
|
38
38
|
def state():
|
|
39
39
|
return {**load('kimi-state.json', {}), **load('kimi-progress.json', {}), **load('kimi-hook.json', {})}
|
|
40
40
|
|
|
41
|
+
# Tools that change the project, run code or reach outside it. Before a Brief task list is approved, and after the user
|
|
42
|
+
# dismissed a plan (Not now), none of them run: the agent reads, answers and waits (BUG-0102).
|
|
43
|
+
WORK_TOOLS = frozenset(('Write', 'Edit', 'Bash', 'Agent', 'AgentSwarm'))
|
|
44
|
+
WORK_MCP = frozenset(('scope', 'preview', 'browser', 'job', 'deploy', 'integration_call', 'network', 'schedule_wakeup', 'send_message', 'ios_simulator', 'generate_image', 'stock_photo', 'brand_lookup', 'deck_check', 'deck_import', 'find_image', 'find_icon', 'image_prep'))
|
|
45
|
+
DISMISSED_BLOCK = ('The user dismissed the plan (Not now). Do not start the work in this task: no edits, commands, images, scope '
|
|
46
|
+
'requests or helper agents, and do not look for another way to start. In one or two sentences, say the plan is on hold and ask '
|
|
47
|
+
'what they would like changed, then finish. The user starts the work again from the chat.')
|
|
48
|
+
|
|
49
|
+
def is_work_tool(name):
|
|
50
|
+
name = str(name or '')
|
|
51
|
+
if name in WORK_TOOLS: return True
|
|
52
|
+
m = re.match(r'^mcp__(\w+?)__(\w+)$', name)
|
|
53
|
+
if not m: return False
|
|
54
|
+
if m.group(1) != 'mlai': return True # connected-app and other MCP tools can change outside data
|
|
55
|
+
return m.group(2) in WORK_MCP
|
|
56
|
+
|
|
41
57
|
def block(reason):
|
|
42
58
|
sys.stderr.write(reason); sys.stderr.flush(); sys.exit(2)
|
|
43
59
|
|
|
@@ -45,13 +61,16 @@ def steering():
|
|
|
45
61
|
"""Consume queued user instructions (oldest first)."""
|
|
46
62
|
d = ROOT / 'steering'
|
|
47
63
|
if not d.is_dir(): return []
|
|
48
|
-
out = []
|
|
64
|
+
out, notes = [], []
|
|
49
65
|
for f in sorted(d.glob('*.json')):
|
|
50
|
-
try:
|
|
51
|
-
except (OSError, ValueError):
|
|
66
|
+
try: note = json.loads(f.read_text())
|
|
67
|
+
except (OSError, ValueError): note = {}
|
|
68
|
+
msg = note.get('message', '') if isinstance(note, dict) else ''
|
|
52
69
|
f.unlink(missing_ok=True)
|
|
53
|
-
if isinstance(msg, str) and msg.strip():
|
|
54
|
-
|
|
70
|
+
if isinstance(msg, str) and msg.strip():
|
|
71
|
+
out.append(msg.strip()[:5000]); notes.append({'note': f.stem, **({'source': str(note['source'])} if note.get('source') else {})})
|
|
72
|
+
# The note id lets the app match a delivered instruction to the message it sent.
|
|
73
|
+
for m, n in zip(out, notes): event('steering', content=m, **n)
|
|
55
74
|
return out
|
|
56
75
|
|
|
57
76
|
def platform_notes():
|
|
@@ -132,6 +151,17 @@ def brief_unlock(st):
|
|
|
132
151
|
st['brief_ok'] = True; st['mode'] = back; st['scope_ok'] = True; save_state(st)
|
|
133
152
|
event('mode', mode=back, content='Plan agreed. Building the approved list in %s mode.' % ('Auto' if back == 'auto' else 'Guided'))
|
|
134
153
|
|
|
154
|
+
def plan_key(text):
|
|
155
|
+
return ' '.join(str(text).lower().split())[:300]
|
|
156
|
+
|
|
157
|
+
def plan_added(items, st):
|
|
158
|
+
"""Items in this task list that the user has not approved yet. Status updates of the same list add nothing."""
|
|
159
|
+
known = set(st.get('plan_items') or [])
|
|
160
|
+
return [i for i in items if plan_key(i) not in known]
|
|
161
|
+
|
|
162
|
+
def remember_plan(items, st):
|
|
163
|
+
st['plan_items'] = sorted(set(st.get('plan_items') or []) | {plan_key(i) for i in items})[:200]
|
|
164
|
+
|
|
135
165
|
def plan_gate(args, st):
|
|
136
166
|
"""First task list of a run: optional user review (Guided mode by default, or always when the user asked).
|
|
137
167
|
Brief mode: every task list is reviewed until the user approves; replies come back to the agent as feedback."""
|
|
@@ -144,7 +174,18 @@ def plan_gate(args, st):
|
|
|
144
174
|
wants = brief or policy == 'always' or (policy == 'guided' and st.get('mode') == 'approve')
|
|
145
175
|
if brief:
|
|
146
176
|
if len(items) < 2: block('Brief mode: the task list needs at least two concrete items before the user can review it. Call TodoList again with the full plan.')
|
|
147
|
-
|
|
177
|
+
if st.get('mode') == 'brief' and not int(st.get('brief_rounds') or 0) and not st.get('brief_q_nudged') and not load('kimi-brief-questions.json', []):
|
|
178
|
+
# BUG-0124: Brief mode promises to clarify requirements before the task list. Ask once for the open questions.
|
|
179
|
+
st['brief_q_nudged'] = True; st['planned'] = False; save_state(st)
|
|
180
|
+
event('note', content='Brief mode: asked for clarifying questions before the task list')
|
|
181
|
+
block('Brief mode clarifies requirements first. Before this task list goes to the user, record one to three short questions about '
|
|
182
|
+
'what is still open in the request (for example style, content, images or scope) with mcp__mlai__brief_questions '
|
|
183
|
+
'{"questions": [{"question": "...", "options": [{"label": "...", "detail": "..."}], "recommended": 0}]}, each with your '
|
|
184
|
+
'recommended answer. Then call TodoList again with the same list. If the request truly leaves nothing open, call TodoList again unchanged.')
|
|
185
|
+
elif st.get('readonly') or len(items) < 2 or not wants or (ROOT / 'kimi-approve-all').exists(): return
|
|
186
|
+
elif not first or st.get('plan_reviewed'):
|
|
187
|
+
# Guided: an approved list is not asked again, but a later list that adds work asks again (BUG-0039).
|
|
188
|
+
if not st.get('plan_reviewed') or not plan_added(items, st): return
|
|
148
189
|
st['plan_reviewed'] = True
|
|
149
190
|
rounds = int(st.get('brief_rounds') or 0) + 1
|
|
150
191
|
if brief: st['brief_rounds'] = rounds
|
|
@@ -173,17 +214,19 @@ def plan_gate(args, st):
|
|
|
173
214
|
block('The user replied to your plan: "' + reply + '"\nAnswer briefly if they asked something, then call TodoList again with the revised task list (3 to 10 items) and, if anything is still open, record your questions with mcp__mlai__brief_questions {"questions": [...]} before the TodoList. Nothing is built until they approve.')
|
|
174
215
|
if verdict in ('approve', 'approve_all'):
|
|
175
216
|
if brief: brief_unlock(st)
|
|
217
|
+
remember_plan(items, st)
|
|
176
218
|
st['scope_ok'] = True; save_state(st); event('plan_review', content='Plan approved'); return
|
|
177
219
|
if verdict.startswith('approve:'):
|
|
178
220
|
keep = {int(x) for x in verdict[8:].split(',') if x.strip().isdigit()}
|
|
179
221
|
kept = [t for i, t in enumerate(items) if i in keep]; removed = [t for i, t in enumerate(items) if i not in keep]
|
|
180
222
|
if kept:
|
|
181
223
|
if brief: brief_unlock(st)
|
|
224
|
+
remember_plan(kept, st); save_state(st)
|
|
182
225
|
event('plan_review', content='%d of %d tasks approved' % (len(kept), len(items)))
|
|
183
226
|
block('The user approved only part of the task list.\nApproved: ' + '; '.join(kept) + '\nRemoved (keep these out of scope; do not do them): ' + '; '.join(removed) + '\nCall TodoList again with only the approved items, then build.')
|
|
184
227
|
event('plan_review', content='Plan dismissed')
|
|
185
228
|
st['plan_dismissed'] = True; save_state(st)
|
|
186
|
-
block(
|
|
229
|
+
block(DISMISSED_BLOCK)
|
|
187
230
|
|
|
188
231
|
def resolve(path):
|
|
189
232
|
p = Path(path)
|
|
@@ -214,6 +257,52 @@ def account_gate(st, name):
|
|
|
214
257
|
block('Your account has used all its credits. Do not start new work or call more tools. Write the final summary now in plain text: what changed, '
|
|
215
258
|
'what was verified, what is left. The user can continue after picking a plan or buying a top-up. This call was not run.')
|
|
216
259
|
|
|
260
|
+
# Helper agents re-read the repository from scratch: on small tasks they multiplied tokens (one trivial test started
|
|
261
|
+
# four) without finishing sooner. Unless the user asked for them, small tasks get none and large tasks at most three.
|
|
262
|
+
HELPER_ASK = re.compile(r'\b(sub-?agents?|helpers?|in parallel|parallel agents?|delegat\w*|swarm|agents)\b', re.I)
|
|
263
|
+
HELPER_CAP_LARGE = int(os.getenv('MINI_HELPER_CAP', '3') or 0)
|
|
264
|
+
|
|
265
|
+
def helper_gate(name, args, st):
|
|
266
|
+
if name not in ('Agent', 'AgentSwarm') or os.getenv('MINI_HELPER_GATE', '1') == '0': return
|
|
267
|
+
if args.get('resume') or HELPER_ASK.search(st.get('task') or ''): return
|
|
268
|
+
used = int(st.get('helpers') or 0)
|
|
269
|
+
cap = HELPER_CAP_LARGE if st.get('large') else 0
|
|
270
|
+
if used >= cap:
|
|
271
|
+
event('note', content='Helper agent not started: %s' % ('small task, done directly' if not cap else 'limit of %d helpers reached' % cap))
|
|
272
|
+
block(('This task is small enough to do directly. ' if not cap else 'You already started %d helper agents for this task. ' % used) +
|
|
273
|
+
'Do the work yourself with Read, Grep, Edit and Bash (several reads can go in one response). Helpers start cold and cost far more tokens. This call was not run.')
|
|
274
|
+
st['helpers'] = used + 1; save_state(st)
|
|
275
|
+
|
|
276
|
+
# Optional soft per-run budget, off by default (users are not asked about tokens). With MINI_RUN_SOFT_BUDGET set, past
|
|
277
|
+
# that many raw tokens the user is asked once whether to continue; each approval allows another budget's worth.
|
|
278
|
+
def soft_budget():
|
|
279
|
+
try: return max(0, int(os.getenv('MINI_RUN_SOFT_BUDGET', '0') or 0))
|
|
280
|
+
except ValueError: return 0
|
|
281
|
+
|
|
282
|
+
def budget_gate(st, name):
|
|
283
|
+
budget = soft_budget()
|
|
284
|
+
if not budget or name in ('TodoList', 'WaitFor', 'TaskOutput', 'TaskList'): return
|
|
285
|
+
if st.get('budget_declined'):
|
|
286
|
+
if name in ('Write', 'Edit', 'Bash', 'Agent', 'AgentSwarm'):
|
|
287
|
+
block('The user chose to stop this task at its token budget. Do not start new work. Write the final summary now: what changed, what was verified, what is left and the exact next step.')
|
|
288
|
+
return
|
|
289
|
+
u = load('usage.json', {}); used = u.get('raw') or u.get('tokens') or 0
|
|
290
|
+
threshold = st.get('budget_next') or budget
|
|
291
|
+
if used < threshold: return
|
|
292
|
+
request = {'id': os.urandom(8).hex(), 'kind': 'budget', 'command': 'This task has used %s tokens (budget %s). Continue working?' % (format(int(used), ','), format(int(threshold), ',')),
|
|
293
|
+
'title': 'Token budget reached', 'used': int(used), 'budget': int(threshold)}
|
|
294
|
+
policy_event('soft_budget', 'ask', 'Asked the user whether to continue past %s tokens' % format(int(threshold), ','))
|
|
295
|
+
try:
|
|
296
|
+
wait_decision(request, seconds=540, strict=True)
|
|
297
|
+
except SystemExit:
|
|
298
|
+
st['budget_declined'] = True; save_state(st)
|
|
299
|
+
try: (ROOT / 'budget-declined').write_text('1')
|
|
300
|
+
except OSError: pass
|
|
301
|
+
policy_event('soft_budget', 'stop', 'Stopped at the token budget')
|
|
302
|
+
block('The user chose to stop this task at its token budget (or did not answer). Do not start new work. Write the final summary now: what changed, what was verified, what is left and the exact next step, so the task can be resumed.')
|
|
303
|
+
st['budget_next'] = int(used) + budget; save_state(st)
|
|
304
|
+
policy_event('soft_budget', 'continue', 'User approved another %s tokens' % format(budget, ','))
|
|
305
|
+
|
|
217
306
|
def token_gate(pol, st, name):
|
|
218
307
|
"""Per-task token budget from policy: at 80% wrap up (Plan-style, no new work), at 100% stop and summarize."""
|
|
219
308
|
account_gate(st, name)
|
|
@@ -283,6 +372,9 @@ def pre(payload, st):
|
|
|
283
372
|
args = payload.get('tool_input') or {}
|
|
284
373
|
if not isinstance(args, dict): args = {}
|
|
285
374
|
if (ROOT / 'cancel').exists(): block('Task stopped by user.')
|
|
375
|
+
if st.get('plan_dismissed') and is_work_tool(name): # before any approval card: Not now means nothing else asks or runs
|
|
376
|
+
event('plan_review', content='Plan on hold: ' + re.sub(r'^mcp__\w+?__', '', name) + ' was not run')
|
|
377
|
+
block(DISMISSED_BLOCK)
|
|
286
378
|
hooked = user_hooks('kimi_pre', name, args, st) # hard-deny, credential block, before_tool / before_command hooks
|
|
287
379
|
if hooked: block(hooked)
|
|
288
380
|
queued = steering()
|
|
@@ -305,15 +397,19 @@ def pre(payload, st):
|
|
|
305
397
|
save_state(st)
|
|
306
398
|
if ACCEPTANCE in json.dumps(args) or HIDDEN.search(json.dumps(args)): block('That path holds the independent acceptance check, which is hidden from the builder by design. Work from the task and the failure output instead.')
|
|
307
399
|
if name == 'TodoList': return plan_gate(args, st)
|
|
308
|
-
|
|
309
|
-
if writes and st.get('plan_dismissed'): block('The user dismissed the task list, so no work may start. Ask what they want changed and finish.')
|
|
310
|
-
if briefing(st) and name in ('Write', 'Edit', 'Agent', 'AgentSwarm'):
|
|
400
|
+
if briefing(st) and (name in ('Write', 'Edit', 'Agent', 'AgentSwarm') or (name.startswith('mcp__') and is_work_tool(name))):
|
|
311
401
|
block('Brief mode: nothing is built until the user approves your task list. Read the repository, then call TodoList with the proposed items and state your open questions. This call was not run.')
|
|
312
402
|
if st.get('inspect') and name in ('Write', 'Edit', 'Agent', 'AgentSwarm'):
|
|
313
403
|
event('safety', effect='write', decision='deny', command=name + ' ' + str(args.get('path', ''))[:300], content='Refused: the user asked for no changes in this turn')
|
|
314
404
|
block(INSPECT_BLOCK)
|
|
315
405
|
if name in ('Write', 'Edit') and not st.get('readonly'):
|
|
316
406
|
nudged = set(st.get('nudged') or [])
|
|
407
|
+
if (st.get('plan_first') and st.get('mode', 'approve') == 'approve' and not st.get('planned') and not st.get('plan_reviewed')
|
|
408
|
+
and 'guided_plan' not in nudged and not (ROOT / 'kimi-approve-all').exists()):
|
|
409
|
+
nudged.add('guided_plan'); st['nudged'] = sorted(nudged); save_state(st)
|
|
410
|
+
event('note', content='Guided mode: asked for the plan before the first edit')
|
|
411
|
+
block('Guided mode: the user approves your plan before any file changes. Call TodoList with 2 to 6 short, concrete items '
|
|
412
|
+
'(what you will change and how you will check it), then wait for the review. This edit was not applied; re-issue it after the plan is approved.')
|
|
317
413
|
if st.get('large') and not st.get('planned') and 'plan' not in nudged:
|
|
318
414
|
nudged.add('plan'); st['nudged'] = sorted(nudged); save_state(st)
|
|
319
415
|
event('note', content='Large task: asked for a plan before editing')
|
|
@@ -336,6 +432,8 @@ def pre(payload, st):
|
|
|
336
432
|
auto = mode == 'auto' or (ROOT / 'kimi-approve-all').exists()
|
|
337
433
|
runaway(name, args, st)
|
|
338
434
|
token_gate(pol, st, name)
|
|
435
|
+
budget_gate(st, name)
|
|
436
|
+
helper_gate(name, args, st)
|
|
339
437
|
if name == 'Bash':
|
|
340
438
|
command = str(args.get('command', ''))
|
|
341
439
|
try:
|
|
@@ -463,8 +561,10 @@ def command_gate(command, st, mode, auto):
|
|
|
463
561
|
if effect not in ('read', 'check'): event('safety', effect=effect, decision='allow', command=command[:300], content=describe(decision))
|
|
464
562
|
return
|
|
465
563
|
request = {'id': os.urandom(8).hex(), 'kind': 'command', 'effect': effect, 'reason': decision.get('reason') or '', 'command': command[:20000]}
|
|
564
|
+
# Deletes and sudo ask every time, even after Approve all (BUG-0101). The card says so and offers no Approve all for them.
|
|
565
|
+
if effect in ('destructive', 'privilege'): request['strict'] = True
|
|
466
566
|
event('safety', effect=effect, decision='ask', command=command[:300], content=describe(decision))
|
|
467
|
-
wait_decision(request)
|
|
567
|
+
wait_decision(request, strict=effect in ('destructive', 'privilege')) # "Approve all" never covers deletes or sudo (BUG-0101)
|
|
468
568
|
if effect in ('install', 'service'):
|
|
469
569
|
kinds = sorted(set(st.get('approved_kinds') or []) | {effect}); st['approved_kinds'] = kinds; save_state(st)
|
|
470
570
|
elif effect in ('write', 'exec') and not st.get('readonly'):
|
|
@@ -532,6 +632,7 @@ def stop(payload, st):
|
|
|
532
632
|
queued = steering()
|
|
533
633
|
if queued: steer_block(queued, stopping=True)
|
|
534
634
|
if st.get('readonly') or st.get('inspect'): return readonly_stop(st)
|
|
635
|
+
if st.get('plan_dismissed'): return # Not now: the run ends here; no done gate may send the agent back to work (BUG-0102)
|
|
535
636
|
if briefing(st) and not st.get('planned') and not st.get('brief_nudged'):
|
|
536
637
|
st['brief_nudged'] = True; save_state(st)
|
|
537
638
|
event('done_gate', content='Brief mode: asked for the task list before finishing')
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""Stock photos (Pixabay) and brand lookup (Brandfetch) for decks and sites.
|
|
2
|
+
The loopback gateway in image_generation.py owns the provider keys (popped from the environment before any tool
|
|
3
|
+
starts); tools call it with the run-scoped gateway token. Laptop runs have no keys: the gateway forwards to the app
|
|
4
|
+
server's /api/model/v1/media endpoint with the run token, which holds the keys and caps use per run.
|
|
5
|
+
Pixabay terms: search results are cached for 24 hours and chosen images are downloaded into the project (no
|
|
6
|
+
permanent hotlinking); the photographer credit is recorded next to the files.
|
|
7
|
+
"""
|
|
8
|
+
import hashlib, json, re, threading, time, urllib.parse, urllib.request, urllib.error
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
PIXABAY = 'https://pixabay.com/api/'
|
|
12
|
+
BRANDFETCH = 'https://api.brandfetch.io/v2/brands/'
|
|
13
|
+
CACHE_SECONDS = 24 * 3600
|
|
14
|
+
MAX_PHOTO_BYTES = 12_000_000
|
|
15
|
+
MAX_LOGO_BYTES = 3_000_000
|
|
16
|
+
PHOTO_HOSTS = ('pixabay.com', 'cdn.pixabay.com')
|
|
17
|
+
LOGO_HOSTS = ('asset.brandfetch.io', 'cdn.brandfetch.io', 'asset.brandfetch.com')
|
|
18
|
+
CAPS = {'stock': 40, 'brand': 6}
|
|
19
|
+
|
|
20
|
+
STOCK_TOOL = {'name': 'stock_photo', 'description': 'Find a real photograph on Pixabay and save it under public/stock/ (free, no credits). Use for people, places, products, teams and textures; use generate_image for bespoke art. Returns the saved path, size, photographer credit and other matches; call again with pick=<id> to use a different match.',
|
|
21
|
+
'inputSchema': {'type': 'object', 'properties': {'query': {'type': 'string', 'maxLength': 100, 'description': 'A few concrete English words, e.g. "team meeting laptop"'},
|
|
22
|
+
'filename': {'type': 'string', 'description': 'New two-word kebab filename, e.g. team-meeting'},
|
|
23
|
+
'orientation': {'type': 'string', 'enum': ['horizontal', 'vertical', 'all']}, 'pick': {'type': 'integer', 'description': 'Pixabay id from an earlier result'}},
|
|
24
|
+
'required': ['query', 'filename'], 'additionalProperties': False}}
|
|
25
|
+
BRAND_TOOL = {'name': 'brand_lookup', 'description': "Look up a company's brand by domain (e.g. stripe.com): name, colours, fonts and logos saved under public/brand/. Use before designing a deck or site for a named company, so it carries their real logo and palette.",
|
|
26
|
+
'inputSchema': {'type': 'object', 'properties': {'domain': {'type': 'string', 'maxLength': 120}}, 'required': ['domain'], 'additionalProperties': False}}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _stem(name):
|
|
30
|
+
stem = re.sub(r'[^a-z0-9-]+', '-', str(name or '').lower().rsplit('.', 1)[0]).strip('-')[:48]
|
|
31
|
+
if not stem: raise ValueError('filename is required, e.g. team-meeting')
|
|
32
|
+
return stem
|
|
33
|
+
|
|
34
|
+
def clean_domain(value):
|
|
35
|
+
raw = str(value or '').strip().lower()
|
|
36
|
+
if '://' in raw: raw = urllib.parse.urlparse(raw).hostname or ''
|
|
37
|
+
raw = raw.split('/')[0].split('?')[0].removeprefix('www.')
|
|
38
|
+
if not re.fullmatch(r'(?=.{3,120}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,24}', raw): raise ValueError('Give a company domain such as stripe.com')
|
|
39
|
+
return raw
|
|
40
|
+
|
|
41
|
+
def _get(url, headers=None, limit=3_000_000, timeout=30):
|
|
42
|
+
req = urllib.request.Request(url, headers={'User-Agent': 'ml-ai-code/1', **(headers or {})})
|
|
43
|
+
with urllib.request.build_opener(urllib.request.ProxyHandler({})).open(req, timeout=timeout) as r:
|
|
44
|
+
data = r.read(limit + 1)
|
|
45
|
+
if len(data) > limit: raise ValueError('Response too large')
|
|
46
|
+
return data
|
|
47
|
+
|
|
48
|
+
def _host_ok(url, hosts):
|
|
49
|
+
p = urllib.parse.urlparse(str(url or ''))
|
|
50
|
+
return p.scheme == 'https' and (p.hostname or '') in hosts
|
|
51
|
+
|
|
52
|
+
def trim_hits(data):
|
|
53
|
+
"""The fields a deck needs from a Pixabay search; nothing else leaves the provider response."""
|
|
54
|
+
out = []
|
|
55
|
+
for h in (data or {}).get('hits', [])[:12]:
|
|
56
|
+
url = h.get('largeImageURL') or h.get('webformatURL')
|
|
57
|
+
if not _host_ok(url, PHOTO_HOSTS): continue
|
|
58
|
+
out.append({'id': int(h.get('id') or 0), 'tags': str(h.get('tags') or '')[:120], 'url': url, 'width': int(h.get('imageWidth') or 0), 'height': int(h.get('imageHeight') or 0),
|
|
59
|
+
'user': str(h.get('user') or '')[:60], 'page': str(h.get('pageURL') or '')[:300]})
|
|
60
|
+
return out
|
|
61
|
+
|
|
62
|
+
def trim_brand(data):
|
|
63
|
+
"""Name, colours, fonts and logo URLs from a Brandfetch brand; logos only from Brandfetch's asset hosts."""
|
|
64
|
+
d = data or {}
|
|
65
|
+
colors = [{'hex': c.get('hex'), 'type': c.get('type'), 'brightness': c.get('brightness')} for c in d.get('colors', [])[:8] if re.fullmatch(r'#[0-9a-fA-F]{6}', str(c.get('hex') or ''))]
|
|
66
|
+
fonts = [{'name': str(f.get('name') or '')[:60], 'type': f.get('type'), 'origin': f.get('origin')} for f in d.get('fonts', [])[:4] if f.get('name')]
|
|
67
|
+
logos = []
|
|
68
|
+
for l in d.get('logos', [])[:8]:
|
|
69
|
+
for f in (l.get('formats') or [])[:6]:
|
|
70
|
+
if _host_ok(f.get('src'), LOGO_HOSTS) and f.get('format') in ('svg', 'png', 'jpeg', 'webp'):
|
|
71
|
+
logos.append({'type': l.get('type'), 'theme': l.get('theme'), 'format': f.get('format'), 'src': f.get('src'), 'width': f.get('width'), 'height': f.get('height')})
|
|
72
|
+
return {'name': str(d.get('name') or '')[:120], 'domain': str(d.get('domain') or '')[:120], 'description': str(d.get('description') or '')[:400], 'colors': colors, 'fonts': fonts, 'logos': logos}
|
|
73
|
+
|
|
74
|
+
def search_pixabay(key, query, orientation='horizontal', pick=None):
|
|
75
|
+
params = {'key': key, 'image_type': 'photo', 'safesearch': 'true', 'per_page': 12, 'order': 'popular'}
|
|
76
|
+
if pick: params['id'] = int(pick)
|
|
77
|
+
else: params.update(q=' '.join(str(query).split())[:100], orientation=orientation if orientation in ('horizontal', 'vertical') else 'all')
|
|
78
|
+
return trim_hits(json.loads(_get(PIXABAY + '?' + urllib.parse.urlencode(params))))
|
|
79
|
+
|
|
80
|
+
def fetch_brand(key, domain):
|
|
81
|
+
return trim_brand(json.loads(_get(BRANDFETCH + urllib.parse.quote(domain), {'Authorization': 'Bearer ' + key})))
|
|
82
|
+
|
|
83
|
+
class Media:
|
|
84
|
+
"""Runs inside the gateway process. `remote` is (url, token) for laptop runs; otherwise the keys are used directly."""
|
|
85
|
+
def __init__(self, root, repo, stock_key='', brand_key='', remote=None, emit=lambda *a, **kw: None):
|
|
86
|
+
self.root, self.repo = Path(root), Path(repo).resolve()
|
|
87
|
+
self.stock_key, self.brand_key, self.remote, self.emit = stock_key, brand_key, remote, emit
|
|
88
|
+
self.used = {'stock': 0, 'brand': 0}; self.lock = threading.Lock()
|
|
89
|
+
@property
|
|
90
|
+
def available(self): return bool(self.remote or self.stock_key or self.brand_key)
|
|
91
|
+
def _cache(self, kind, key, fetch):
|
|
92
|
+
path = self.root / 'media-cache.json'
|
|
93
|
+
try: cache = json.loads(path.read_text())
|
|
94
|
+
except (FileNotFoundError, ValueError): cache = {}
|
|
95
|
+
ident = kind + ':' + hashlib.sha256(json.dumps(key, sort_keys=True).encode()).hexdigest()[:24]
|
|
96
|
+
hit = cache.get(ident)
|
|
97
|
+
if hit and time.time() - hit.get('at', 0) < CACHE_SECONDS: return hit['value']
|
|
98
|
+
with self.lock:
|
|
99
|
+
if self.used[kind] >= CAPS[kind]: raise ValueError('%s limit reached (%d per task). Reuse what you found.' % ('Stock photo search' if kind == 'stock' else 'Brand lookup', CAPS[kind]))
|
|
100
|
+
self.used[kind] += 1
|
|
101
|
+
value = fetch()
|
|
102
|
+
cache = {k: v for k, v in cache.items() if time.time() - v.get('at', 0) < CACHE_SECONDS}
|
|
103
|
+
cache[ident] = {'at': time.time(), 'value': value}
|
|
104
|
+
tmp = path.with_suffix('.tmp'); tmp.write_text(json.dumps(cache)[:4_000_000]); tmp.replace(path)
|
|
105
|
+
return value
|
|
106
|
+
def _call(self, op, body, direct):
|
|
107
|
+
if self.remote:
|
|
108
|
+
url, token = self.remote
|
|
109
|
+
req = urllib.request.Request(url, data=json.dumps({'op': op, **body}).encode(), headers={'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'})
|
|
110
|
+
try:
|
|
111
|
+
with urllib.request.urlopen(req, timeout=40) as r: return json.load(r)['result']
|
|
112
|
+
except urllib.error.HTTPError as exc:
|
|
113
|
+
try: message = json.load(exc).get('error', {}).get('message')
|
|
114
|
+
except Exception: message = None
|
|
115
|
+
raise ValueError(message or 'The media service had an error') from None
|
|
116
|
+
return direct()
|
|
117
|
+
def _dir(self, name):
|
|
118
|
+
public = self.repo / 'public'; d = public / name
|
|
119
|
+
for p in (public, d):
|
|
120
|
+
if p.is_symlink(): raise ValueError('Asset directory cannot be a symlink')
|
|
121
|
+
d.mkdir(parents=True, exist_ok=True); return d
|
|
122
|
+
def stock(self, args):
|
|
123
|
+
query = ' '.join(str(args.get('query') or '').split())[:100]
|
|
124
|
+
if not query: raise ValueError('query is required')
|
|
125
|
+
stem = _stem(args.get('filename')); orientation = args.get('orientation') or 'horizontal'; pick = args.get('pick')
|
|
126
|
+
if pick is not None and (isinstance(pick, bool) or not isinstance(pick, int) or pick <= 0): raise ValueError('pick is a Pixabay id from an earlier result')
|
|
127
|
+
if not (self.remote or self.stock_key): raise ValueError('Stock photos are not configured on this server (PIXABAY_API_KEY).')
|
|
128
|
+
hits = self._cache('stock', [query, orientation, pick], lambda: self._call('stock', {'query': query, 'orientation': orientation, **({'pick': pick} if pick else {})},
|
|
129
|
+
lambda: search_pixabay(self.stock_key, query, orientation, pick)))
|
|
130
|
+
if not hits: return {'saved': None, 'note': 'No photos matched. Try simpler, concrete words.', 'matches': []}
|
|
131
|
+
chosen = next((h for h in hits if pick and h['id'] == pick), hits[0])
|
|
132
|
+
if not _host_ok(chosen['url'], PHOTO_HOSTS): raise ValueError('Unexpected image host')
|
|
133
|
+
d = self._dir('stock'); ext = '.png' if chosen['url'].lower().endswith('.png') else '.jpg'; path = d / (stem + ext)
|
|
134
|
+
if any(d.glob(stem + '.*')): raise ValueError('Asset filename already exists. Choose a new name.')
|
|
135
|
+
data = _get(chosen['url'], limit=MAX_PHOTO_BYTES, timeout=60)
|
|
136
|
+
if not (data[:3] == b'\xff\xd8\xff' or data[:8] == b'\x89PNG\r\n\x1a\n'): raise ValueError('The photo download was not an image')
|
|
137
|
+
with path.open('xb') as f: f.write(data)
|
|
138
|
+
credit = 'Photo by %s on Pixabay' % (chosen['user'] or 'a Pixabay contributor')
|
|
139
|
+
with (d / 'CREDITS.md').open('a') as f: f.write('- %s: %s (%s)\n' % (path.name, credit, chosen['page']))
|
|
140
|
+
rel = str(path.relative_to(self.repo))
|
|
141
|
+
self.emit('stock_photo', content='Saved ' + rel, path=rel, credit=credit)
|
|
142
|
+
return {'saved': {'path': rel, 'url': '/stock/' + path.name, 'width': chosen['width'], 'height': chosen['height'], 'credit': credit, 'tags': chosen['tags']},
|
|
143
|
+
'matches': [{k: h[k] for k in ('id', 'tags', 'width', 'height')} for h in hits if h is not chosen][:8]}
|
|
144
|
+
def brand(self, args):
|
|
145
|
+
domain = clean_domain(args.get('domain'))
|
|
146
|
+
if not (self.remote or self.brand_key): raise ValueError('Brand lookup is not configured on this server (BRANDFETCH_API_KEY).')
|
|
147
|
+
info = self._cache('brand', [domain], lambda: self._call('brand', {'domain': domain}, lambda: fetch_brand(self.brand_key, domain)))
|
|
148
|
+
d = self._dir('brand'); slug = re.sub(r'[^a-z0-9]+', '-', domain).strip('-'); saved = []
|
|
149
|
+
# One logo per background, SVG first; then an icon as a fallback mark. Brandfetch's theme names the logo's own
|
|
150
|
+
# tone: a "light" logo is white (for dark slides), a "dark" logo is coloured or black (for light slides).
|
|
151
|
+
order = sorted(info.get('logos', []), key=lambda l: (l['type'] != 'logo', l['format'] != 'svg'))
|
|
152
|
+
seen = set()
|
|
153
|
+
for l in order:
|
|
154
|
+
if l['type'] not in ('logo', 'icon', 'symbol'): continue
|
|
155
|
+
tag = l['type'] + {'light': '-for-dark-bg', 'dark': '-for-light-bg'}.get(l.get('theme'), '')
|
|
156
|
+
if tag in seen or len(saved) >= 4: continue
|
|
157
|
+
try: data = _get(l['src'], limit=MAX_LOGO_BYTES)
|
|
158
|
+
except Exception: continue
|
|
159
|
+
ext = {'svg': '.svg', 'png': '.png', 'jpeg': '.jpg', 'webp': '.webp'}[l['format']]
|
|
160
|
+
if ext == '.svg' and b'<svg' not in data[:2000].lower(): continue
|
|
161
|
+
path = d / ('%s-%s%s' % (slug, tag, ext)); path.write_bytes(data); seen.add(tag)
|
|
162
|
+
saved.append({'path': str(path.relative_to(self.repo)), 'type': l['type'], 'use_on': {'light': 'dark backgrounds', 'dark': 'light backgrounds'}.get(l.get('theme'), 'any background'), 'format': l['format']})
|
|
163
|
+
self.emit('brand_lookup', content='Brand for ' + domain + (': ' + info.get('name') if info.get('name') else ''))
|
|
164
|
+
return {'name': info.get('name'), 'domain': domain, 'description': info.get('description'), 'colors': info.get('colors'), 'fonts': info.get('fonts'), 'logos': saved,
|
|
165
|
+
'note': 'Pick the logo whose use_on matches the deck background (theme.logo). Use the brand colours as accent/accent2 only if text stays readable (4.5:1).'}
|
|
166
|
+
|
|
167
|
+
def call(name, args):
|
|
168
|
+
"""Tool side (runner or the Kimi MCP server): forward to the loopback gateway, which holds the keys."""
|
|
169
|
+
import os
|
|
170
|
+
base, token = os.getenv('MINI_IMAGE_URL', ''), os.getenv('MINI_IMAGE_TOKEN', '')
|
|
171
|
+
if not base or not token: raise ValueError('Stock photos and brand lookup are not configured on this server.')
|
|
172
|
+
url = base.rsplit('/', 1)[0] + ('/stock' if name == 'stock_photo' else '/brand')
|
|
173
|
+
req = urllib.request.Request(url, data=json.dumps(args).encode(), headers={'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'})
|
|
174
|
+
try:
|
|
175
|
+
with urllib.request.build_opener(urllib.request.ProxyHandler({})).open(req, timeout=120) as r: return json.load(r)['result']
|
|
176
|
+
except urllib.error.HTTPError as exc:
|
|
177
|
+
try: message = json.load(exc).get('error', 'Request failed')
|
|
178
|
+
except ValueError: message = 'Request failed'
|
|
179
|
+
raise ValueError(message) from None
|
package/worker/memory.py
CHANGED
|
@@ -81,10 +81,10 @@ class MemoryStore:
|
|
|
81
81
|
def index_text(self, limit=6000):
|
|
82
82
|
facts = self.list()
|
|
83
83
|
if not facts:
|
|
84
|
-
return ('
|
|
84
|
+
return ('Computer memory (this chat\'s computer only; the project Memory card is separate): empty. This workspace keeps durable facts across tasks in mcp__mlai__memory. When you learn something that will matter next time '
|
|
85
85
|
'(how to run or test this project, a convention, a preference the user stated, a correction they gave, where something lives), save it with '
|
|
86
86
|
'mcp__mlai__memory {"action":"save","name":"short-name","type":"project|user|feedback|reference","description":"one line","content":"the fact"}. Never store secrets.')
|
|
87
|
-
text = '
|
|
87
|
+
text = 'Computer memory (this chat\'s computer only; the project Memory card is separate; facts saved in earlier tasks; read one with mcp__mlai__memory {"action":"read","name":"<name>"}; save new durable facts with action "save", correct wrong ones by saving the same name, drop them with "forget"):\n'
|
|
88
88
|
body = self.index.read_text() if self.index.is_file() else ''
|
|
89
89
|
body = '\n'.join(l for l in body.splitlines() if l.startswith('- ') or l.startswith('## '))
|
|
90
90
|
return (text + body)[:limit]
|