mlai-code 0.0.0-stage → 0.3.1
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 +12 -2
- package/bin/mlai.mjs +3 -0
- package/package.json +18 -3
- package/src/browser-setup.mjs +77 -0
- package/src/cli-ext.mjs +146 -0
- package/src/cli.mjs +534 -0
- package/src/cloud.mjs +29 -0
- package/src/completion.mjs +159 -0
- package/src/credentials.mjs +54 -0
- package/src/deploy.mjs +134 -0
- package/src/distribution.mjs +373 -0
- package/src/engine-key.mjs +4 -0
- package/src/engine.mjs +167 -0
- package/src/headless.mjs +196 -0
- package/src/home.mjs +22 -0
- package/src/host.mjs +123 -0
- package/src/local-models.mjs +64 -0
- package/src/modes.mjs +38 -0
- package/src/netbox.mjs +97 -0
- package/src/netjail.py +59 -0
- package/src/offline.mjs +98 -0
- package/src/parity.mjs +86 -0
- package/src/project.mjs +68 -0
- package/src/relay.mjs +140 -0
- package/src/render.mjs +73 -0
- package/src/restricted.mjs +47 -0
- package/src/review.mjs +70 -0
- package/src/runner.mjs +127 -0
- package/src/runtime.mjs +75 -0
- package/src/sandbox.mjs +135 -0
- package/src/sdk.mjs +11 -0
- package/src/server.mjs +110 -0
- package/src/session.mjs +290 -0
- package/src/settings.mjs +135 -0
- package/src/shared-data.mjs +3 -0
- package/src/tui/acp.mjs +145 -0
- package/src/tui/agents-cli.mjs +114 -0
- package/src/tui/agents-local.mjs +65 -0
- package/src/tui/ansi.mjs +123 -0
- package/src/tui/app.mjs +710 -0
- package/src/tui/approval.mjs +88 -0
- package/src/tui/bg.mjs +172 -0
- package/src/tui/bgterm.mjs +79 -0
- package/src/tui/chat.mjs +230 -0
- package/src/tui/clipboard.mjs +74 -0
- package/src/tui/cloud-cli.mjs +57 -0
- package/src/tui/cloud-run.mjs +123 -0
- package/src/tui/cmd-agents.mjs +130 -0
- package/src/tui/cmd-auto-review.mjs +94 -0
- package/src/tui/cmd-cloud.mjs +17 -0
- package/src/tui/cmd-config.mjs +104 -0
- package/src/tui/cmd-core.mjs +160 -0
- package/src/tui/cmd-distribution.mjs +15 -0
- package/src/tui/cmd-ecosystem.mjs +169 -0
- package/src/tui/cmd-extensibility.mjs +333 -0
- package/src/tui/cmd-remote.mjs +18 -0
- package/src/tui/cmd-review.mjs +58 -0
- package/src/tui/cmd-session-extra.mjs +152 -0
- package/src/tui/cmd-tui.mjs +200 -0
- package/src/tui/commands.mjs +160 -0
- package/src/tui/custom-commands.mjs +98 -0
- package/src/tui/diff.mjs +60 -0
- package/src/tui/editor.mjs +190 -0
- package/src/tui/ext-state.mjs +16 -0
- package/src/tui/extensions-local.mjs +140 -0
- package/src/tui/files.mjs +72 -0
- package/src/tui/handoff.mjs +110 -0
- package/src/tui/import-setups.mjs +151 -0
- package/src/tui/init.mjs +104 -0
- package/src/tui/keymap.mjs +70 -0
- package/src/tui/keys.mjs +101 -0
- package/src/tui/loop.mjs +75 -0
- package/src/tui/markdown.mjs +118 -0
- package/src/tui/mcp-config.mjs +225 -0
- package/src/tui/mcp-oauth.mjs +162 -0
- package/src/tui/permissions.mjs +189 -0
- package/src/tui/plain.mjs +32 -0
- package/src/tui/plugins-local.mjs +103 -0
- package/src/tui/remote.mjs +125 -0
- package/src/tui/review-cli.mjs +332 -0
- package/src/tui/review-github.mjs +59 -0
- package/src/tui/screen.mjs +60 -0
- package/src/tui/session-ops.mjs +104 -0
- package/src/tui/skills-local.mjs +92 -0
- package/src/tui/snapshots.mjs +87 -0
- package/src/tui/statusline.mjs +98 -0
- package/src/tui/transcript.mjs +116 -0
- package/src/two-words.mjs +36 -0
- package/src/wakeups.mjs +53 -0
- package/src/winenv.mjs +94 -0
- package/src/winsandbox.py +351 -0
- package/src/workspace-api.mjs +621 -0
- package/src/worktree.mjs +128 -0
- package/vendor/client-sdk/controller.cjs +115 -0
- package/vendor/client-sdk/files.cjs +38 -0
- package/vendor/client-sdk/index.cjs +133 -0
- package/vendor/client-sdk/login.cjs +52 -0
- package/vendor/client-sdk/updates.cjs +41 -0
- package/vendor/cloud-cli/mlai.mjs +394 -0
- package/worker/architecture_parse.cjs +23 -0
- package/worker/architecture_worker.py +394 -0
- package/worker/ask_first.py +96 -0
- package/worker/attachment_reader.py +66 -0
- package/worker/background_jobs.py +111 -0
- package/worker/brief_questions.py +56 -0
- package/worker/browser_local.py +49 -0
- package/worker/browser_pane.py +89 -0
- package/worker/browser_tool.py +101 -0
- package/worker/build_planner.py +187 -0
- package/worker/chat_fork.py +277 -0
- package/worker/checkpoints.py +133 -0
- package/worker/cloud_pty.py +351 -0
- package/worker/command_policy.py +39 -0
- package/worker/command_safety.py +717 -0
- package/worker/computer_card.sh +23 -0
- package/worker/computer_setup.sh +86 -0
- package/worker/computer_use.py +221 -0
- package/worker/connector_search.py +43 -0
- package/worker/context_manager.py +60 -0
- package/worker/conversation.py +144 -0
- package/worker/deploy_pack.py +116 -0
- package/worker/design_director.py +240 -0
- package/worker/dev_jobs.py +166 -0
- package/worker/edit_guard.py +99 -0
- package/worker/env_setup.py +129 -0
- package/worker/evidence.py +109 -0
- package/worker/fast_search.py +24 -0
- package/worker/git_sync.py +333 -0
- package/worker/goals.py +156 -0
- package/worker/handover.py +98 -0
- package/worker/harness_memory.py +194 -0
- package/worker/image_generation.py +173 -0
- package/worker/intelligence.py +149 -0
- package/worker/intent_model.py +78 -0
- package/worker/intent_weights.json +1 -0
- package/worker/ios_simulator.py +87 -0
- package/worker/kimi_bridge.py +997 -0
- package/worker/kimi_compact.py +93 -0
- package/worker/kimi_gates.py +304 -0
- package/worker/kimi_hook.py +617 -0
- package/worker/lifecycle.py +95 -0
- package/worker/lsp.py +77 -0
- package/worker/mcp_local.py +293 -0
- package/worker/memory.py +99 -0
- package/worker/mlai_auto_review.py +177 -0
- package/worker/mlai_hooks.py +966 -0
- package/worker/mlai_mcp.py +441 -0
- package/worker/mlai_permissions.py +283 -0
- package/worker/model_stream.py +75 -0
- package/worker/nested_repos.py +76 -0
- package/worker/offline_guard.py +17 -0
- package/worker/preview.py +806 -0
- package/worker/proc_watch.py +124 -0
- package/worker/project_context.py +72 -0
- package/worker/project_rules.py +99 -0
- package/worker/quality.py +180 -0
- package/worker/reply_language.py +56 -0
- package/worker/repo_hygiene.py +132 -0
- package/worker/repo_memory.py +101 -0
- package/worker/repo_policy.py +269 -0
- package/worker/roles.json +3173 -0
- package/worker/roles.py +303 -0
- package/worker/runner.py +1795 -0
- package/worker/runtime_control.py +205 -0
- package/worker/secret_scan.py +168 -0
- package/worker/session_hub.py +80 -0
- package/worker/skill_library.py +129 -0
- package/worker/skill_pack.py +533 -0
- package/worker/subagents.py +91 -0
- package/worker/task_controller.py +139 -0
- package/worker/task_skills.py +159 -0
- package/worker/team_checkpoint.py +33 -0
- package/worker/token_count.py +32 -0
- package/worker/tool_catalog.py +38 -0
- package/worker/upload_storage.py +531 -0
- package/worker/verification.py +725 -0
- package/worker/vision.py +84 -0
- package/worker/wakeups.py +63 -0
- package/worker/web_tools.py +203 -0
- package/worker/winproc.py +135 -0
- package/worker/work_ledger.py +117 -0
- package/worker/worktrees.py +79 -0
package/src/headless.mjs
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mlai -p: one task, or a stream of them (--input-format stream-json), with no prompts. Shared by cli.mjs.
|
|
3
|
+
* --output-format text | json | stream-json (stream-json: the session line, every agent event, then a result per turn)
|
|
4
|
+
* --json-schema the final answer must be JSON matching the schema; one correction turn, then error_schema
|
|
5
|
+
* --max-turns caps agent steps; a run stopped by it ends with subtype error_max_turns
|
|
6
|
+
* --max-budget-credits the server caps the run's tokens (run.maxTokens); the next turn never starts past it
|
|
7
|
+
* --fallback-model Chat.send switches model when the chosen one is overloaded or unavailable
|
|
8
|
+
* --allowedTools / --disallowedTools / --tools permission rules (worker/mlai_permissions.py syntax)
|
|
9
|
+
* Result subtypes: success, error_during_execution, error_max_turns, error_max_budget, error_schema, stopped.
|
|
10
|
+
*/
|
|
11
|
+
import {readFileSync,existsSync} from 'node:fs';
|
|
12
|
+
import {readFile,rm} from 'node:fs/promises';
|
|
13
|
+
import {normalizeRule} from './tui/permissions.mjs';
|
|
14
|
+
import {TOKENS_PER_CREDIT,creditsFor,answerOf} from './tui/chat.mjs';
|
|
15
|
+
import {renderEvent,summaryLine,exitCode,EXIT} from './render.mjs';
|
|
16
|
+
|
|
17
|
+
export const TOOL_NAMES=['Bash','Edit','Read','WebFetch','WebSearch','Agent'];
|
|
18
|
+
export class HeadlessError extends Error{}
|
|
19
|
+
|
|
20
|
+
/** 'Bash(git log:*) Edit,Read' -> ['Bash(git log:*)','Edit','Read']: commas or spaces outside parentheses separate. */
|
|
21
|
+
export function splitRules(text){
|
|
22
|
+
const out=[];let cur='',depth=0;
|
|
23
|
+
for(const ch of String(text)){
|
|
24
|
+
if(ch==='(')depth++;else if(ch===')')depth=Math.max(0,depth-1);
|
|
25
|
+
if(depth===0&&(ch===','||/\s/.test(ch))){if(cur.trim())out.push(cur.trim());cur='';continue}
|
|
26
|
+
cur+=ch;
|
|
27
|
+
}
|
|
28
|
+
if(cur.trim())out.push(cur.trim());
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
/** Validated, canonical rules; throws HeadlessError naming the first bad one. */
|
|
32
|
+
export function rulesFrom(list,flag){
|
|
33
|
+
return list.flatMap(splitRules).map(r=>{const n=normalizeRule(r);if(!n)throw new HeadlessError(flag+': "'+r+'" is not a rule. Use names like Bash, Bash(npm test:*), Edit(src/**), Read, WebFetch(domain:example.com), WebSearch, Agent or mcp__server.');return n});
|
|
34
|
+
}
|
|
35
|
+
/** --tools: only these built-in tools; the rest become deny rules. '' turns every built-in tool off. */
|
|
36
|
+
export function toolsOnly(text){
|
|
37
|
+
const keep=new Set(splitRules(text).map(r=>{const n=normalizeRule(r);if(!n||n.includes('(')||n.startsWith('mcp__'))throw new HeadlessError('--tools takes tool names: '+TOOL_NAMES.join(', ')+' (got "'+r+'").');return n}));
|
|
38
|
+
return TOOL_NAMES.filter(t=>!keep.has(t));
|
|
39
|
+
}
|
|
40
|
+
export function toolRules(o){
|
|
41
|
+
const allow=rulesFrom(o.allowedTools||[],'--allowedTools'),deny=[...new Set([...rulesFrom(o.disallowedTools||[],'--disallowedTools'),...(o.tools!=null?toolsOnly(o.tools):[])])];
|
|
42
|
+
return allow.length||deny.length?{allow,deny}:null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** --json-schema: inline JSON, or a file with it. */
|
|
46
|
+
export function loadSchema(v){
|
|
47
|
+
let text=String(v).trim();
|
|
48
|
+
if(!text.startsWith('{')){if(!existsSync(text))throw new HeadlessError('--json-schema: no file '+text+' (pass a file or inline JSON)');text=readFileSync(text,'utf8')}
|
|
49
|
+
let schema;try{schema=JSON.parse(text)}catch{throw new HeadlessError('--json-schema is not valid JSON')}
|
|
50
|
+
if(!schema||typeof schema!=='object'||Array.isArray(schema))throw new HeadlessError('--json-schema must be a JSON object');
|
|
51
|
+
if(JSON.stringify(schema).length>20000)throw new HeadlessError('--json-schema is too large (20 KB at most)');
|
|
52
|
+
return schema;
|
|
53
|
+
}
|
|
54
|
+
const typeOf=v=>v===null?'null':Array.isArray(v)?'array':Number.isInteger(v)?'integer':typeof v;
|
|
55
|
+
/** A dependency-free JSON Schema check (types, properties, required, additionalProperties, items, enum, const, ranges, anyOf, oneOf, allOf). Returns error strings. */
|
|
56
|
+
export function validate(v,schema,at='$'){
|
|
57
|
+
if(schema===true||schema==null)return [];
|
|
58
|
+
if(schema===false)return [at+' is not allowed'];
|
|
59
|
+
const errs=[];
|
|
60
|
+
if(schema.type){const types=[].concat(schema.type),t=typeOf(v);if(!types.some(x=>x===t||(x==='number'&&t==='integer')))return [at+' should be '+types.join(' or ')+', got '+t]}
|
|
61
|
+
if(schema.enum&&!schema.enum.some(e=>JSON.stringify(e)===JSON.stringify(v)))errs.push(at+' should be one of '+schema.enum.map(e=>JSON.stringify(e)).join(', '));
|
|
62
|
+
if('const' in schema&&JSON.stringify(schema.const)!==JSON.stringify(v))errs.push(at+' should be '+JSON.stringify(schema.const));
|
|
63
|
+
if(typeof v==='string'){if(schema.minLength!=null&&v.length<schema.minLength)errs.push(at+' is shorter than '+schema.minLength);if(schema.maxLength!=null&&v.length>schema.maxLength)errs.push(at+' is longer than '+schema.maxLength);if(schema.pattern){try{if(!new RegExp(schema.pattern,'u').test(v))errs.push(at+' does not match '+schema.pattern)}catch{}}}
|
|
64
|
+
if(typeof v==='number'){if(schema.minimum!=null&&v<schema.minimum)errs.push(at+' is below '+schema.minimum);if(schema.maximum!=null&&v>schema.maximum)errs.push(at+' is above '+schema.maximum)}
|
|
65
|
+
if(Array.isArray(v)){
|
|
66
|
+
if(schema.minItems!=null&&v.length<schema.minItems)errs.push(at+' has fewer than '+schema.minItems+' items');
|
|
67
|
+
if(schema.maxItems!=null&&v.length>schema.maxItems)errs.push(at+' has more than '+schema.maxItems+' items');
|
|
68
|
+
if(schema.items&&typeof schema.items==='object')v.forEach((x,i)=>errs.push(...validate(x,schema.items,at+'['+i+']')));
|
|
69
|
+
}
|
|
70
|
+
if(v&&typeof v==='object'&&!Array.isArray(v)){
|
|
71
|
+
for(const k of schema.required||[])if(!(k in v))errs.push(at+'.'+k+' is required');
|
|
72
|
+
const props=schema.properties||{};
|
|
73
|
+
for(const [k,x] of Object.entries(v)){
|
|
74
|
+
if(k in props)errs.push(...validate(x,props[k],at+'.'+k));
|
|
75
|
+
else if(schema.additionalProperties===false)errs.push(at+'.'+k+' is not allowed');
|
|
76
|
+
else if(schema.additionalProperties&&typeof schema.additionalProperties==='object')errs.push(...validate(x,schema.additionalProperties,at+'.'+k));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if(schema.allOf)for(const s of schema.allOf)errs.push(...validate(v,s,at));
|
|
80
|
+
if(schema.anyOf&&!schema.anyOf.some(s=>!validate(v,s,at).length))errs.push(at+' matches none of anyOf');
|
|
81
|
+
if(schema.oneOf&&schema.oneOf.filter(s=>!validate(v,s,at).length).length!==1)errs.push(at+' should match exactly one of oneOf');
|
|
82
|
+
return errs.slice(0,20);
|
|
83
|
+
}
|
|
84
|
+
/** The JSON value in an answer: the whole text, a ```json fence, or the outermost {...} / [...]. */
|
|
85
|
+
export function extractJson(text){
|
|
86
|
+
const t=String(text||'').trim();
|
|
87
|
+
const tries=[t,(t.match(/```(?:json)?\s*\n([\s\S]*?)\n```/)||[])[1]];
|
|
88
|
+
for(const [a,b] of [['{','}'],['[',']']]){const i=t.indexOf(a),j=t.lastIndexOf(b);if(i>=0&&j>i)tries.push(t.slice(i,j+1))}
|
|
89
|
+
for(const x of tries){if(x==null)continue;try{return {ok:true,value:JSON.parse(x)}}catch{}}
|
|
90
|
+
return {ok:false};
|
|
91
|
+
}
|
|
92
|
+
const schemaTask=schema=>'\n\nWhen you finish, reply with only a JSON value (no prose, no code fence) that matches this JSON Schema:\n'+JSON.stringify(schema);
|
|
93
|
+
|
|
94
|
+
/** Credits to the run's token cap, counted the way /usage counts them (TOKENS_PER_CREDIT). */
|
|
95
|
+
export const budgetTokens=credits=>Math.max(1,Math.floor(credits*TOKENS_PER_CREDIT));
|
|
96
|
+
|
|
97
|
+
/** stream-json input: one JSON message per line, {type:'user', message:{content: string | [{type:'text'|'image', ...}]}}. */
|
|
98
|
+
export async function* streamInputs(lines){
|
|
99
|
+
let n=0;
|
|
100
|
+
for await(const raw of lines){
|
|
101
|
+
n++;const line=String(raw).trim();if(!line)continue;
|
|
102
|
+
let m;try{m=JSON.parse(line)}catch{throw new HeadlessError('stdin line '+n+' is not JSON')}
|
|
103
|
+
if(m?.type!=='user')continue;// other message types (control, keep-alive) are ignored
|
|
104
|
+
const c=m.message?.content??m.content;
|
|
105
|
+
const parts=typeof c==='string'?[{type:'text',text:c}]:Array.isArray(c)?c:[];
|
|
106
|
+
const text=parts.filter(p=>p?.type==='text').map(p=>String(p.text||'')).join('\n').trim();
|
|
107
|
+
const images=parts.filter(p=>p?.type==='image'&&p.source?.type==='base64'&&typeof p.source.data==='string').map(p=>Buffer.from(p.source.data,'base64'));
|
|
108
|
+
if(!text)throw new HeadlessError('stdin line '+n+' has no text');
|
|
109
|
+
yield {text,images};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export async function* stdinLines(stdin=process.stdin){
|
|
113
|
+
let buf='';
|
|
114
|
+
for await(const chunk of stdin){buf+=chunk;let i;while((i=buf.indexOf('\n'))>=0){yield buf.slice(0,i);buf=buf.slice(i+1)}}
|
|
115
|
+
if(buf.trim())yield buf;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const finalText=(r,events)=>answerOf({events,status:{result:r.result,error:r.error}});
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Runs the print-mode turns. o: parsed flags; s: the Session; chat: its Chat (toolRules, appendPrompt, fallbacks set).
|
|
122
|
+
* inputs: an async iterable of {text, images} (one item without --input-format stream-json). Returns the exit code.
|
|
123
|
+
*/
|
|
124
|
+
export async function runHeadless(o,{s,chat,err,inputs,out=t=>process.stdout.write(t),log=t=>process.stderr.write(t)}){
|
|
125
|
+
const stream=o.outputFormat==='stream-json',json=o.outputFormat==='json';
|
|
126
|
+
const write=x=>out(JSON.stringify(x)+'\n');
|
|
127
|
+
const schema=o.jsonSchema?loadSchema(o.jsonSchema):null;
|
|
128
|
+
if(o.maxTurns)chat.steps=Math.min(chat.steps,o.maxTurns);
|
|
129
|
+
let events=[],refused=0,spent=0,turns=0,code=EXIT.completed,last=null;
|
|
130
|
+
const stop=()=>{s.cancel().catch(()=>{})};process.once('SIGINT',stop);process.once('SIGTERM',stop);
|
|
131
|
+
const onEvent=e=>{
|
|
132
|
+
events.push(e);
|
|
133
|
+
if(stream)write(e);
|
|
134
|
+
else if(!json){const line=renderEvent(e,{c:err,verbose:o.verbose});if(line&&e.type!=='answer')log(line+'\n')}
|
|
135
|
+
if(e.type==='approval'){
|
|
136
|
+
if(o.yes)s.decide(e.id,'approve_all').catch(()=>{});
|
|
137
|
+
else if(o.bgChild){} // a background run (mlai --bg) waits for mlai attach to answer
|
|
138
|
+
else{refused++;s.decide(e.id,'reject').catch(()=>{});if(!json&&!stream)log(err.yellow('Rejected (no one to approve): pass --yes, or use --mode auto in a sandbox.')+'\n')}
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const onStarted=e=>{if(stream)write({type:'session',id:s.meta.id,runId:e.runId,mode:chat.mode,model:chat.model||chat.info?.id||null,effort:chat.effort,sandbox:e.sandbox,root:s.meta.root})};
|
|
142
|
+
const onFallback=f=>{if(stream)write({type:'system',subtype:'fallback',from:f.from,to:f.to});else if(!json)log(err.yellow('The model is busy or unavailable; continuing with '+f.to+'.')+'\n')};
|
|
143
|
+
s.on('event',onEvent);s.on('started',onStarted);chat.on('fallback',onFallback);
|
|
144
|
+
const cap=o.maxBudgetCredits?budgetTokens(o.maxBudgetCredits):null;
|
|
145
|
+
const send=async(text,images)=>{
|
|
146
|
+
if(cap!=null)chat.maxTokens=Math.max(1,cap-spent);
|
|
147
|
+
const r=await chat.send(text,{images,onProgress:m=>{if(!json&&!stream)log(err.dim(m+'...')+'\n')}});
|
|
148
|
+
spent+=Number(r.usage?.tokens)||0;turns++;return r;
|
|
149
|
+
};
|
|
150
|
+
try{
|
|
151
|
+
for await(const input of inputs){
|
|
152
|
+
events=[];
|
|
153
|
+
const images=[];
|
|
154
|
+
for(const b of input.images||[])images.push(await s.addImage(b));
|
|
155
|
+
for(const f of input.files||[])images.push(await s.addImage(await readFile(f)));
|
|
156
|
+
let r,subtype,structured;
|
|
157
|
+
if(cap!=null&&spent>=cap){r={state:'limited',error:'The --max-budget-credits limit was reached before this turn.'};subtype='error_max_budget'}
|
|
158
|
+
else{
|
|
159
|
+
r=await send(input.text+(schema?schemaTask(schema):''),images.length?images:undefined);
|
|
160
|
+
const budgetHit=cap!=null&&(r.code==='billing_limit'||(r.state!=='completed'&&spent>=cap));
|
|
161
|
+
if(budgetHit){r={...r,code:'budget_limit',error:'Stopped at the --max-budget-credits limit ('+o.maxBudgetCredits+' credits). Changes so far are kept.'};subtype='error_max_budget'}
|
|
162
|
+
else if(r.state==='limited'&&o.maxTurns&&r.result?.limit!=='tokens')subtype='error_max_turns';
|
|
163
|
+
else if(r.state==='stopped')subtype='stopped';
|
|
164
|
+
else if(r.state!=='completed')subtype='error_during_execution';
|
|
165
|
+
else subtype='success';
|
|
166
|
+
if(schema&&subtype==='success'){
|
|
167
|
+
let got=extractJson(finalText(r,events)),problems=got.ok?validate(got.value,schema):['the answer is not JSON'];
|
|
168
|
+
if(problems.length&&!(cap!=null&&spent>=cap)){
|
|
169
|
+
events=[];
|
|
170
|
+
r=await send('Your last answer did not match the required JSON Schema: '+problems.slice(0,8).join('; ')+'. Reply again with only the corrected JSON value.'+schemaTask(schema));
|
|
171
|
+
got=extractJson(finalText(r,events));problems=r.state!=='completed'?['the correction turn did not finish']:got.ok?validate(got.value,schema):['the answer is not JSON'];
|
|
172
|
+
}
|
|
173
|
+
if(problems.length){subtype='error_schema';r={...r,error:'The answer did not match --json-schema: '+problems.slice(0,8).join('; ')}}
|
|
174
|
+
else structured=got.value;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const text=finalText(r,events);
|
|
178
|
+
const exit=subtype==='success'?EXIT.completed:subtype==='error_max_turns'||subtype==='error_max_budget'?EXIT.limited:subtype==='error_schema'?EXIT.error:exitCode(r);
|
|
179
|
+
if(exit!==EXIT.completed)code=exit;
|
|
180
|
+
last={type:'result',subtype,state:r.state,exitCode:exit,sessionId:s.meta.id,name:s.meta.title||null,mode:chat.mode,model:chat.model||chat.info?.id||null,effort:chat.effort,text,
|
|
181
|
+
...(schema?{structuredOutput:structured??null}:{}),error:subtype==='success'?null:(r.error||null),usage:r.usage||null,
|
|
182
|
+
credits:creditsFor(spent),turns,changedFiles:r.completion?.changedFiles||[],checksPassed:r.completion?.checksPassed||0,checksFailed:r.completion?.checksFailed||0,rejectedApprovals:refused,
|
|
183
|
+
persisted:!o.noSessionPersistence};
|
|
184
|
+
if(stream)write(last);
|
|
185
|
+
else if(!json){if(schema&&structured!==undefined)out(JSON.stringify(structured)+'\n');else if(text)out(text.trim()+'\n');log(summaryLine(r,err)+'\n')}
|
|
186
|
+
if(['error_max_budget','stopped'].includes(subtype))break;
|
|
187
|
+
}
|
|
188
|
+
if(json&&last)write(last);
|
|
189
|
+
if(!last)throw new HeadlessError('No task: pass one with -p "task" or send {"type":"user",...} lines on stdin.');
|
|
190
|
+
return code;
|
|
191
|
+
}finally{
|
|
192
|
+
process.off('SIGINT',stop);process.off('SIGTERM',stop);
|
|
193
|
+
s.off('event',onEvent);s.off('started',onStarted);chat.off('fallback',onFallback);
|
|
194
|
+
if(o.noSessionPersistence)await rm(s.dir,{recursive:true,force:true}).catch(()=>{});
|
|
195
|
+
}
|
|
196
|
+
}
|
package/src/home.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {mkdirSync,chmodSync,statSync} from 'node:fs';
|
|
4
|
+
import {fileURLToPath} from 'node:url';
|
|
5
|
+
|
|
6
|
+
export const VERSION='0.3.1';
|
|
7
|
+
export const DEFAULT_SERVER='https://mlai-mini-code.vercel.app';
|
|
8
|
+
/** All local state lives here; nothing is written into the user's repository except what the agent edits. */
|
|
9
|
+
export function home(){
|
|
10
|
+
const dir=process.env.MLAI_HOME||path.join(os.homedir(),'.mlai');
|
|
11
|
+
mkdirSync(dir,{recursive:true,mode:0o700});try{chmodSync(dir,0o700)}catch{}
|
|
12
|
+
return dir;
|
|
13
|
+
}
|
|
14
|
+
export function sub(...parts){const dir=path.join(home(),...parts);mkdirSync(dir,{recursive:true,mode:0o700});return dir}
|
|
15
|
+
const isFile=p=>{try{return statSync(p).isFile()}catch{return false}};
|
|
16
|
+
/** The harness ships next to the engine (app bundle or package); in the repository it is ../../worker. */
|
|
17
|
+
export function workerDir(){
|
|
18
|
+
if(process.env.MLAI_WORKER_DIR)return path.resolve(process.env.MLAI_WORKER_DIR);
|
|
19
|
+
const here=path.dirname(fileURLToPath(import.meta.url));
|
|
20
|
+
for(const c of [path.join(here,'..','worker'),path.join(here,'..','..','..','worker')]){if(isFile(path.join(c,'runner.py')))return c;}
|
|
21
|
+
throw new Error('The coding harness is missing from this install. Reinstall ML AI Code.');
|
|
22
|
+
}
|
package/src/host.mjs
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// The engine host: owns live sessions for one process and exposes the operations every front end needs.
|
|
2
|
+
// `mlai serve` puts it behind an owner-only socket; the Mac app calls it directly from its main process.
|
|
3
|
+
import {EventEmitter} from 'node:events';
|
|
4
|
+
import {stat} from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import {Session,MODES,sandboxKind} from './session.mjs';
|
|
7
|
+
import {friendly} from './cloud.mjs';
|
|
8
|
+
import {runtimeInfo,ensureRuntime} from './runtime.mjs';
|
|
9
|
+
import {VERSION} from './home.mjs';
|
|
10
|
+
import {projectRoot} from './project.mjs';
|
|
11
|
+
import * as wt from './worktree.mjs';
|
|
12
|
+
|
|
13
|
+
export class HostError extends Error{constructor(message,status=400,code){super(message);this.status=status;this.code=code}}
|
|
14
|
+
const ID=/^[0-9a-f-]{36}$/;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* cloud: async () => Cloud for the signed-in account. Throw when signed out.
|
|
18
|
+
* Emits 'session' events: {id, type: 'started'|'harness'|'progress'|'done', data}.
|
|
19
|
+
*/
|
|
20
|
+
export class EngineHost extends EventEmitter{
|
|
21
|
+
constructor({cloud}){super();this.cloud=cloud;this.live=new Map()}
|
|
22
|
+
attach(s){
|
|
23
|
+
if(this.live.has(s.meta.id))return this.live.get(s.meta.id);
|
|
24
|
+
const id=s.meta.id,entry={session:s,events:[],result:null,running:false};
|
|
25
|
+
const out=(type,data)=>this.emit('session',{id,type,data});
|
|
26
|
+
s.on('event',e=>{entry.events.push(e);if(entry.events.length>5000)entry.events.splice(0,1000);out('harness',e)});
|
|
27
|
+
s.on('started',d=>{entry.events=[];out('started',d)});
|
|
28
|
+
s.on('done',r=>{entry.result=r;out('done',r)});
|
|
29
|
+
entry.out=out;this.live.set(id,entry);return entry;
|
|
30
|
+
}
|
|
31
|
+
async entry(id){
|
|
32
|
+
if(typeof id!=='string'||!ID.test(id))throw new HostError('Unknown session',404);
|
|
33
|
+
if(this.live.has(id))return this.live.get(id);
|
|
34
|
+
try{return this.attach(await Session.open(id))}catch{throw new HostError('Unknown session',404)}
|
|
35
|
+
}
|
|
36
|
+
health(){return {ok:true,version:VERSION,sandbox:sandboxKind(),pid:process.pid}}
|
|
37
|
+
async doctor(){return {...this.health(),runtime:await runtimeInfo()}}
|
|
38
|
+
prepare(onProgress){return ensureRuntime({onProgress})}
|
|
39
|
+
/** Sessions for a project, including the ones working in their own worktree. Archived ones only when asked. */
|
|
40
|
+
async sessions({root,archived=false}={}){
|
|
41
|
+
const all=await Session.list();
|
|
42
|
+
return all.filter(m=>(!root||m.root===root||m.parent===root)&&(archived||!m.archived)).map(m=>({...m,running:this.live.get(m.id)?.running||false}));
|
|
43
|
+
}
|
|
44
|
+
async create({cwd,mode='guided',worktree=false,name}){
|
|
45
|
+
if(!MODES[mode])throw new HostError('Mode must be guided, auto or plan');
|
|
46
|
+
if(typeof cwd!=='string'||!path.isAbsolute(cwd))throw new HostError('Choose an existing folder');
|
|
47
|
+
try{if(!(await stat(cwd)).isDirectory())throw 0}catch{throw new HostError('Choose an existing folder')}
|
|
48
|
+
let tree=null;
|
|
49
|
+
if(worktree){
|
|
50
|
+
const {root,git}=await projectRoot(cwd);
|
|
51
|
+
if(!git)throw new HostError('Worktrees need a git repository. Turn off "Own worktree" to work in this folder directly.',400,'no_worktree');
|
|
52
|
+
try{tree=await wt.createWorktree(root,{name:typeof name==='string'&&name.trim()?name.slice(0,200):undefined})}catch(err){throw new HostError(err.message,400,err.code||'no_worktree')}
|
|
53
|
+
}
|
|
54
|
+
const s=await Session.create({cwd,mode,worktree:tree});this.attach(s);return s.meta;
|
|
55
|
+
}
|
|
56
|
+
/** Rename, pin or archive a session. */
|
|
57
|
+
async annotate(id,{title,pinned,archived}={}){
|
|
58
|
+
const e=await this.entry(id),m=e.session.meta;
|
|
59
|
+
if(title!==undefined){const t=String(title||'').replace(/[\0-\x1f]/g,' ').trim().slice(0,120);if(!t)throw new HostError('Enter a name');m.title=t;m.renamed=true}
|
|
60
|
+
if(pinned!==undefined)m.pinned=pinned===true;
|
|
61
|
+
if(archived!==undefined){if(archived===true&&e.running)throw new HostError('Stop the task before archiving',409);m.archived=archived===true}
|
|
62
|
+
await e.session.save();return m;
|
|
63
|
+
}
|
|
64
|
+
/** Worktree actions for a session: status, merge into the main folder's branch, commit, or remove the folder. */
|
|
65
|
+
async worktree(id,{action,deleteBranch=false}={}){
|
|
66
|
+
const e=await this.entry(id),m=e.session.meta;
|
|
67
|
+
if(!m.worktree)throw new HostError('This session works in the main folder');
|
|
68
|
+
const tree={...m.worktree,parent:m.parent};
|
|
69
|
+
if(action==='status')return {...m.worktree,parent:m.parent,removed:!!m.worktree.removed,...await wt.worktreeStatus(tree)};
|
|
70
|
+
if(e.running)throw new HostError('Wait for the task to finish',409,'session_busy');
|
|
71
|
+
try{
|
|
72
|
+
if(action==='commit')return await wt.commitAll(tree,m.title||'Work from ML AI Code');
|
|
73
|
+
if(action==='merge'){const r=await wt.mergeWorktree(tree,{message:m.title});if(r.merged){m.worktree.merged=new Date().toISOString();await e.session.save()}return r}
|
|
74
|
+
if(action==='remove'){const r=await wt.removeWorktree(tree,{deleteBranch:deleteBranch===true});m.worktree.removed=new Date().toISOString();if(deleteBranch===true)m.worktree.branchDeleted=true;await e.session.save();return r}
|
|
75
|
+
}catch(err){if(err instanceof HostError)throw err;throw new HostError(err.message,409,err.code)}
|
|
76
|
+
throw new HostError('Unknown worktree action');
|
|
77
|
+
}
|
|
78
|
+
async get(id){const e=await this.entry(id);return {...e.session.meta,running:e.running,pending:await e.session.pending(),result:e.result}}
|
|
79
|
+
async transcript(id){return (await this.entry(id)).session.transcript()}
|
|
80
|
+
events(id,after=0){const e=this.live.get(id);return e?e.events.slice(Math.max(0,after)):[]}
|
|
81
|
+
/** Starts a task and resolves once it has started, or rejects if it could not start. The run continues in the background. */
|
|
82
|
+
async start(id,{task,steps,allowUnsandboxed=false,images}={}){
|
|
83
|
+
const e=await this.entry(id),s=e.session;
|
|
84
|
+
if(e.running)throw new HostError('A task is already running in this session',409,'session_busy');
|
|
85
|
+
if(typeof task!=='string'||!task.trim()||task.length>30000)throw new HostError('Enter a task under 30,000 characters');
|
|
86
|
+
let c;try{c=await this.cloud()}catch(err){throw new HostError(friendly(err),401,'not_signed_in')}
|
|
87
|
+
if(s.meta.worktree?.removed)throw new HostError('This session\u2019s worktree was removed. Start a new session.',409,'worktree_removed');
|
|
88
|
+
e.running=true;e.result=null;
|
|
89
|
+
let started=null;
|
|
90
|
+
const begun=new Promise(r=>s.once('started',x=>{started=x;r({started:x})}));
|
|
91
|
+
const done=s.send(task,{cloud:c,steps:Number(steps)||120,allowUnsandboxed:allowUnsandboxed===true,images,onProgress:m=>e.out('progress',{message:m})})
|
|
92
|
+
.catch(err=>{const r={state:'error',error:friendly(err),code:err.code||err.error?.code||null};if(started){e.result=r;e.out('done',r)}return {...r,failedToStart:!started,status:err.status}})
|
|
93
|
+
.finally(()=>{e.running=false});
|
|
94
|
+
const first=await Promise.race([begun,done.then(r=>({result:r}))]);
|
|
95
|
+
if(first.result?.failedToStart){
|
|
96
|
+
const r=first.result,status=r.code==='billing_limit'?402:r.code==='session_busy'?409:r.status===401?401:400;
|
|
97
|
+
throw new HostError(r.error,status,r.code);
|
|
98
|
+
}
|
|
99
|
+
return {ok:true,...(first.started||{})};
|
|
100
|
+
}
|
|
101
|
+
/** Saves an image for this session's next message and returns its name. */
|
|
102
|
+
async addImage(id,bytes){const e=await this.entry(id);try{return {name:await e.session.addImage(bytes)}}catch(err){throw new HostError(err.message)}}
|
|
103
|
+
async image(id,name){const e=await this.entry(id);try{return {url:await e.session.image(name)}}catch(err){throw new HostError(err.message,404)}}
|
|
104
|
+
async decide(id,{id:approval,verdict}={}){
|
|
105
|
+
const e=await this.entry(id);
|
|
106
|
+
try{await e.session.decide(approval,verdict)}catch(err){throw new HostError(err.message)}
|
|
107
|
+
return {ok:true};
|
|
108
|
+
}
|
|
109
|
+
async steer(id,{message}={}){
|
|
110
|
+
const e=await this.entry(id);
|
|
111
|
+
if(!e.running)throw new HostError('Nothing is running',409);
|
|
112
|
+
if(typeof message!=='string'||!message.trim())throw new HostError('Enter a message');
|
|
113
|
+
await e.session.steer(message);return {ok:true};
|
|
114
|
+
}
|
|
115
|
+
async cancel(id){const e=await this.entry(id);await e.session.cancel();return {ok:true}}
|
|
116
|
+
async mode(id,{mode}={}){
|
|
117
|
+
const e=await this.entry(id);
|
|
118
|
+
if(!MODES[mode])throw new HostError('Mode must be guided, auto or plan');
|
|
119
|
+
if(e.running)throw new HostError('Change the mode between tasks',409);
|
|
120
|
+
e.session.meta.mode=mode;await e.session.save();return e.session.meta;
|
|
121
|
+
}
|
|
122
|
+
async close(){for(const e of this.live.values())if(e.running)await e.session.cancel().catch(()=>{})}
|
|
123
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* On-device models for offline work. Looks for a local OpenAI-compatible server on loopback only: Ollama
|
|
3
|
+
* (127.0.0.1:11434), LM Studio (127.0.0.1:1234) and an MLX server when MLAI_MLX_URL points at one. Nothing is
|
|
4
|
+
* downloaded or started here; the person runs their own server.
|
|
5
|
+
*
|
|
6
|
+
* Online, the picker shows only our models and runs never use these (see offline.mjs planRun).
|
|
7
|
+
*/
|
|
8
|
+
import {DEFAULT_SERVER} from './home.mjs';
|
|
9
|
+
|
|
10
|
+
export const SERVERS=[
|
|
11
|
+
{server:'ollama',endpoint:'http://127.0.0.1:11434/v1'},
|
|
12
|
+
{server:'lmstudio',endpoint:'http://127.0.0.1:1234/v1'},
|
|
13
|
+
];
|
|
14
|
+
const LOOPBACK=/^http:\/\/(127\.0\.0\.1|localhost|\[::1\])(:\d{1,5})?(\/[\w./-]*)?$/;
|
|
15
|
+
/** Only plain-http loopback endpoints: an on-device model must never become a way to send code elsewhere. */
|
|
16
|
+
export function loopbackEndpoint(u){return typeof u==='string'&&LOOPBACK.test(u)}
|
|
17
|
+
export function configuredServers(env=process.env){
|
|
18
|
+
const out=[...SERVERS];
|
|
19
|
+
const mlx=String(env.MLAI_MLX_URL||'').replace(/\/+$/,'');
|
|
20
|
+
if(mlx&&loopbackEndpoint(mlx))out.push({server:'mlx',endpoint:mlx.endsWith('/v1')?mlx:mlx+'/v1'});
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
const ID=/^[\w.:@/-]{1,160}$/;
|
|
24
|
+
/** Our id for a local model: offline:<server>:<model>. */
|
|
25
|
+
export const offlineId=(server,model)=>'offline:'+server+':'+model;
|
|
26
|
+
export function parseOfflineId(id){
|
|
27
|
+
const m=/^offline:(ollama|lmstudio|mlx):(.+)$/.exec(String(id||''));
|
|
28
|
+
return m&&ID.test(m[2])?{server:m[1],model:m[2]}:null;
|
|
29
|
+
}
|
|
30
|
+
export const isOfflineModel=id=>Boolean(parseOfflineId(id));
|
|
31
|
+
|
|
32
|
+
async function getJson(fetchImpl,url,timeoutMs){
|
|
33
|
+
const ctl=new AbortController();const t=setTimeout(()=>ctl.abort(),timeoutMs);
|
|
34
|
+
try{const r=await fetchImpl(url,{signal:ctl.signal,headers:{accept:'application/json'}});if(!r.ok)return null;return await r.json()}
|
|
35
|
+
catch{return null}finally{clearTimeout(t)}
|
|
36
|
+
}
|
|
37
|
+
/** Every model the local servers report. Unreachable servers are skipped quietly. */
|
|
38
|
+
export async function detectLocalModels({fetchImpl=globalThis.fetch,env=process.env,timeoutMs=700,servers=configuredServers(env)}={}){
|
|
39
|
+
const found=await Promise.all(servers.map(async s=>{
|
|
40
|
+
const d=await getJson(fetchImpl,s.endpoint+'/models',timeoutMs);
|
|
41
|
+
const list=Array.isArray(d?.data)?d.data:[];
|
|
42
|
+
return list.map(m=>String(m?.id||'')).filter(id=>ID.test(id)&&!/embed/i.test(id)).slice(0,50)
|
|
43
|
+
.map(model=>({id:offlineId(s.server,model),name:model,server:s.server,endpoint:s.endpoint}));
|
|
44
|
+
}));
|
|
45
|
+
return found.flat();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** True when ML AI Code's servers answer. A short probe so an offline start does not wait for the 45 s client timeout. */
|
|
49
|
+
export async function isOnline({server=process.env.MLAI_SERVER||DEFAULT_SERVER,fetchImpl=globalThis.fetch,timeoutMs=2500}={}){
|
|
50
|
+
const ctl=new AbortController();const t=setTimeout(()=>ctl.abort(),timeoutMs);
|
|
51
|
+
try{await fetchImpl(server.replace(/\/+$/,'')+'/api/health',{method:'GET',signal:ctl.signal,cache:'no-store'});return true}
|
|
52
|
+
catch{return false}finally{clearTimeout(t)}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* What the bridge's localModels() returns. Online: no local models at all, so the picker shows only our models.
|
|
57
|
+
* Offline: the detected models, which the shell lists under one "Offline model" entry.
|
|
58
|
+
*/
|
|
59
|
+
export async function localModelsForPicker(opts={}){
|
|
60
|
+
const online=await (opts.isOnlineImpl||isOnline)(opts);
|
|
61
|
+
if(online)return {online:true,models:[]};
|
|
62
|
+
const models=await detectLocalModels(opts);
|
|
63
|
+
return {online:false,models:models.map(({id,name,server})=>({id,name,server}))};
|
|
64
|
+
}
|
package/src/modes.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI modes (CLI v1 shared contract) and how each maps onto the engine's session modes (session.mjs MODES).
|
|
3
|
+
* agent asks before edits and commands unless a permission rule allows them -> engine 'guided'
|
|
4
|
+
* plan read-only; proposes a plan, then "Build it" switches to agent -> engine 'plan'
|
|
5
|
+
* ask read-only questions and answers, no plan -> engine 'plan' + an answer-only card
|
|
6
|
+
* auto works on its own inside the OS sandbox, no asks -> engine 'auto'
|
|
7
|
+
* Shift+Tab rotates agent -> plan -> ask -> auto (auto only when a sandbox is available) -> agent.
|
|
8
|
+
* The old names still parse: guided is agent.
|
|
9
|
+
*/
|
|
10
|
+
export const MODE_NAMES=['agent','plan','ask','auto'];
|
|
11
|
+
export const MODES=Object.freeze({
|
|
12
|
+
agent:{name:'agent',engine:'guided',readOnly:false,asks:true,label:'Agent',summary:'Asks before edits and commands unless allowed'},
|
|
13
|
+
plan:{name:'plan',engine:'plan',readOnly:true,asks:false,label:'Plan',summary:'Reads only and proposes a plan; Build it switches to agent'},
|
|
14
|
+
ask:{name:'ask',engine:'plan',readOnly:true,asks:false,label:'Ask',summary:'Reads only and answers questions, no plan'},
|
|
15
|
+
auto:{name:'auto',engine:'auto',readOnly:false,asks:false,label:'Auto',summary:'Works on its own inside the OS sandbox',needsSandbox:true},
|
|
16
|
+
});
|
|
17
|
+
const LEGACY={guided:'agent',approve:'agent'};
|
|
18
|
+
/** 'agent' | 'Plan' | 'guided' -> canonical name, or null. */
|
|
19
|
+
export function modeName(v){const s=String(v||'').trim().toLowerCase();return MODES[s]?s:LEGACY[s]||null}
|
|
20
|
+
/** Engine mode for a session (session.mjs accepts guided | auto | plan). */
|
|
21
|
+
export const engineMode=m=>MODES[modeName(m)||'agent'].engine;
|
|
22
|
+
/** CLI mode for a stored session: ask is kept in meta.cliMode because the engine sees it as plan. */
|
|
23
|
+
export function sessionMode(meta){const c=modeName(meta?.cliMode);if(c&&MODES[c].engine===meta?.mode)return c;return modeName(meta?.mode)||'agent'}
|
|
24
|
+
/** The next mode for Shift+Tab. */
|
|
25
|
+
export function nextMode(current,{sandbox=true}={}){
|
|
26
|
+
const order=MODE_NAMES.filter(m=>sandbox||!MODES[m].needsSandbox);
|
|
27
|
+
const i=order.indexOf(modeName(current)||'agent');
|
|
28
|
+
return order[(i+1)%order.length];
|
|
29
|
+
}
|
|
30
|
+
/** Text added in front of the task in ask mode, so the read-only run answers instead of planning. */
|
|
31
|
+
export const ASK_CARD='Answer the question from the code in this folder. Read files as needed; do not edit, do not run commands that change anything, and do not write a plan. Reply with a direct answer.';
|
|
32
|
+
/** The task as sent to the engine for a mode. */
|
|
33
|
+
export function taskFor(mode,task){return modeName(mode)==='ask'?ASK_CARD+'\n\nQuestion: '+task:task}
|
|
34
|
+
/** Applies a CLI mode to a Session: engine mode in meta.mode, CLI mode in meta.cliMode. */
|
|
35
|
+
export async function applyMode(session,mode){
|
|
36
|
+
const m=modeName(mode);if(!m)throw new Error('Mode must be agent, plan, ask or auto');
|
|
37
|
+
session.meta.mode=MODES[m].engine;session.meta.cliMode=m;await session.save();return m;
|
|
38
|
+
}
|
package/src/netbox.mjs
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OS-level network off (CLI /sandbox network off, settings sandbox.network false).
|
|
3
|
+
*
|
|
4
|
+
* The whole harness runs inside the OS sandbox, and it still has to reach the model gateway. So the network is cut
|
|
5
|
+
* for the harness and every command it starts, and one way out is left: a relay in this (unsandboxed) CLI process that
|
|
6
|
+
* forwards plain HTTP to the gateway's origin, for the gateway's own paths only. Nothing else gets through: no DNS,
|
|
7
|
+
* no other host, no other path on the same host.
|
|
8
|
+
*
|
|
9
|
+
* macOS Seatbelt (deny network*) with only localhost and Unix sockets allowed; the relay listens on 127.0.0.1.
|
|
10
|
+
* Linux bubblewrap --unshare-net (an empty network namespace with only loopback). The relay listens on a Unix
|
|
11
|
+
* socket bound into the sandbox; src/netjail.py forwards 127.0.0.1:<port> inside the namespace to it.
|
|
12
|
+
* Windows Not available without administrator rights (Windows Filtering Platform / firewall rules need elevation).
|
|
13
|
+
* Network off stays the engine's hook-level block (web tools, remote hosts, installs); `osNetwork` says so.
|
|
14
|
+
*/
|
|
15
|
+
import http from 'node:http';
|
|
16
|
+
import https from 'node:https';
|
|
17
|
+
import {mkdtempSync,rmSync} from 'node:fs';
|
|
18
|
+
import {randomInt} from 'node:crypto';
|
|
19
|
+
import {fileURLToPath} from 'node:url';
|
|
20
|
+
import os from 'node:os';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
|
|
23
|
+
/** Sandboxes that can switch the network off at the OS level. */
|
|
24
|
+
export const NET_KINDS=['seatbelt','bwrap'];
|
|
25
|
+
export const NET_LAUNCHER=path.join(path.dirname(fileURLToPath(import.meta.url)),'netjail.py');
|
|
26
|
+
export const OFFLINE_BODY='Network access is off for this session. Only the model gateway is reachable from the sandbox.';
|
|
27
|
+
|
|
28
|
+
/** How network off is enforced with this sandbox kind: 'os' (kernel/Seatbelt) or 'hooks' (the engine's check only). */
|
|
29
|
+
export function osNetwork(kind){return NET_KINDS.includes(kind)?'os':'hooks'}
|
|
30
|
+
|
|
31
|
+
const HOP=['connection','keep-alive','proxy-connection','proxy-authorization','upgrade','te','trailer'];
|
|
32
|
+
const strip=h=>{const o={...h};for(const k of HOP)delete o[k];return o};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Starts the relay for `targets` (the gateway URLs the harness uses; all must share the first URL's origin). Paths
|
|
36
|
+
* under a `deny` URL are refused.
|
|
37
|
+
* kind 'bwrap' listens on a Unix socket in a fresh private folder (bound into the sandbox); otherwise on 127.0.0.1.
|
|
38
|
+
* Returns {box, rewrite(url), close()}: box goes to sandbox.wrap({network}); rewrite turns a gateway URL into the
|
|
39
|
+
* address the sandboxed harness uses.
|
|
40
|
+
*/
|
|
41
|
+
export async function startRelay({targets,deny=[],kind,tmp=os.tmpdir()}){
|
|
42
|
+
const urls=targets.filter(Boolean).map(u=>new URL(u));
|
|
43
|
+
if(!urls.length)throw new Error('The relay needs the model gateway address.');
|
|
44
|
+
const origin=urls[0].origin;
|
|
45
|
+
const prefixes=[...new Set(urls.filter(u=>u.origin===origin).map(u=>u.pathname.replace(/\/+$/,'')||'/'))];
|
|
46
|
+
// Gateways that reach the open web (web research) are refused even when they sit under the model endpoint's path.
|
|
47
|
+
const blocked=deny.filter(Boolean).flatMap(u=>{try{const x=new URL(u);return x.origin===origin?[x.pathname.replace(/\/+$/,'')]:[]}catch{return []}});
|
|
48
|
+
const under=(p,pre)=>pre==='/'||p===pre||p.startsWith(pre+'/');
|
|
49
|
+
const allowed=p=>prefixes.some(pre=>under(p,pre))&&!blocked.some(pre=>under(p,pre));
|
|
50
|
+
const upstream=origin.startsWith('https:')?https:http;
|
|
51
|
+
const sockets=new Set();
|
|
52
|
+
const server=http.createServer((req,res)=>{
|
|
53
|
+
let target;try{target=new URL(req.url,origin)}catch{res.writeHead(400).end();return}
|
|
54
|
+
// Only origin-form requests for the gateway's own paths; a proxy-style absolute URL to another host is refused.
|
|
55
|
+
if(target.origin!==origin||!req.url.startsWith('/')||!allowed(target.pathname)){res.writeHead(403,{'content-type':'text/plain'});res.end(OFFLINE_BODY);req.resume();return}
|
|
56
|
+
const up=upstream.request(target,{method:req.method,headers:{...strip(req.headers),host:target.host}},r=>{res.writeHead(r.statusCode||502,strip(r.headers));r.pipe(res)});
|
|
57
|
+
up.on('error',()=>{if(!res.headersSent)res.writeHead(502,{'content-type':'text/plain'});res.end()});
|
|
58
|
+
res.on('close',()=>{if(!res.writableFinished)up.destroy()});
|
|
59
|
+
req.pipe(up);
|
|
60
|
+
});
|
|
61
|
+
server.on('connection',s=>{sockets.add(s);s.on('close',()=>sockets.delete(s))});
|
|
62
|
+
let dir=null,socket=null,port;
|
|
63
|
+
if(kind==='bwrap'){
|
|
64
|
+
// A short private folder (Unix socket paths are limited to about 100 bytes); bwrap binds it into the sandbox.
|
|
65
|
+
dir=mkdtempSync(path.join(tmp,'mlai-net-'));socket=path.join(dir,'relay.sock');
|
|
66
|
+
await new Promise((ok,fail)=>{server.once('error',fail);server.listen(socket,ok)});
|
|
67
|
+
port=randomInt(20000,60000); // inside the empty namespace nothing else listens, so any port is free
|
|
68
|
+
}else{
|
|
69
|
+
await new Promise((ok,fail)=>{server.once('error',fail);server.listen(0,'127.0.0.1',ok)});
|
|
70
|
+
port=server.address().port;
|
|
71
|
+
}
|
|
72
|
+
const rewrite=u=>{
|
|
73
|
+
if(!u)return u;let x;try{x=new URL(u)}catch{return u}
|
|
74
|
+
return x.origin===origin&&allowed(x.pathname)?'http://127.0.0.1:'+port+x.pathname.replace(/\/+$/,'')+x.search:u;
|
|
75
|
+
};
|
|
76
|
+
let closed=false;
|
|
77
|
+
const close=async()=>{
|
|
78
|
+
if(closed)return;closed=true;
|
|
79
|
+
for(const s of sockets)s.destroy();
|
|
80
|
+
await new Promise(r=>server.close(()=>r()));
|
|
81
|
+
if(dir)rmSync(dir,{recursive:true,force:true});
|
|
82
|
+
};
|
|
83
|
+
return {box:{port,socket,dir},rewrite,close,origin,prefixes};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Applies network off to a run: rewrites the request's model endpoint and the same-origin gateway variables in env to
|
|
88
|
+
* the relay, and keeps HTTP proxies away from loopback. Gateways on other hosts (web research) are left as they are,
|
|
89
|
+
* so they fail at the OS level like any other host.
|
|
90
|
+
*/
|
|
91
|
+
export function offlineEnv(env,relay){
|
|
92
|
+
const out={...env};
|
|
93
|
+
if(typeof out.MINI_IMAGE_GATEWAY==='string')out.MINI_IMAGE_GATEWAY=relay.rewrite(out.MINI_IMAGE_GATEWAY);
|
|
94
|
+
const np=[out.NO_PROXY||out.no_proxy,'127.0.0.1','localhost'].filter(Boolean).join(',');
|
|
95
|
+
out.NO_PROXY=np;out.no_proxy=np;
|
|
96
|
+
return out;
|
|
97
|
+
}
|
package/src/netjail.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Network-off launcher for Linux (src/netbox.mjs, src/sandbox.mjs).
|
|
2
|
+
|
|
3
|
+
bubblewrap --unshare-net gives the harness an empty network namespace: only loopback, no route out, no DNS. This
|
|
4
|
+
launcher runs inside it, listens on 127.0.0.1:<port> and forwards each connection to the CLI's relay on a Unix socket
|
|
5
|
+
(bound into the sandbox), then runs the harness and exits with its code. The relay reaches the model gateway only.
|
|
6
|
+
|
|
7
|
+
python netjail.py --port 41234 --unix /tmp/mlai-net-x/relay.sock -- python runner.py
|
|
8
|
+
"""
|
|
9
|
+
import os, signal, socket, subprocess, sys, threading
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def pipe(src, dst):
|
|
13
|
+
try:
|
|
14
|
+
while True:
|
|
15
|
+
data = src.recv(65536)
|
|
16
|
+
if not data: break
|
|
17
|
+
dst.sendall(data)
|
|
18
|
+
except OSError: pass
|
|
19
|
+
finally:
|
|
20
|
+
try: dst.shutdown(socket.SHUT_WR)
|
|
21
|
+
except OSError: pass
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def bridge(client, path):
|
|
25
|
+
try:
|
|
26
|
+
upstream = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM); upstream.connect(path)
|
|
27
|
+
except OSError:
|
|
28
|
+
client.close(); return
|
|
29
|
+
a = threading.Thread(target=pipe, args=(client, upstream), daemon=True)
|
|
30
|
+
b = threading.Thread(target=pipe, args=(upstream, client), daemon=True)
|
|
31
|
+
a.start(); b.start(); a.join(); b.join()
|
|
32
|
+
client.close(); upstream.close()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def serve(listener, path):
|
|
36
|
+
while True:
|
|
37
|
+
try: client, _ = listener.accept()
|
|
38
|
+
except OSError: return
|
|
39
|
+
threading.Thread(target=bridge, args=(client, path), daemon=True).start()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def main(argv):
|
|
43
|
+
if '--' not in argv: sys.exit('usage: netjail.py --port N --unix PATH -- command...')
|
|
44
|
+
opts, cmd = argv[:argv.index('--')], argv[argv.index('--') + 1:]
|
|
45
|
+
port = int(opts[opts.index('--port') + 1]); path = opts[opts.index('--unix') + 1]
|
|
46
|
+
if not cmd: sys.exit('netjail.py: no command')
|
|
47
|
+
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
48
|
+
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
49
|
+
listener.bind(('127.0.0.1', port)); listener.listen(64)
|
|
50
|
+
threading.Thread(target=serve, args=(listener, path), daemon=True).start()
|
|
51
|
+
child = subprocess.Popen(cmd)
|
|
52
|
+
for sig in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP):
|
|
53
|
+
signal.signal(sig, lambda s, _f: child.send_signal(s))
|
|
54
|
+
code = child.wait()
|
|
55
|
+
sys.exit(code if code >= 0 else 128 - code)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
if __name__ == '__main__':
|
|
59
|
+
main(sys.argv[1:])
|