mlai-code 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/checkpoints.mjs +217 -0
- package/src/cli.mjs +178 -31
- package/src/cloud.mjs +2 -0
- package/src/engine.mjs +1 -1
- package/src/headless.mjs +25 -9
- package/src/home.mjs +3 -2
- package/src/host.mjs +141 -13
- package/src/ide-quick.mjs +147 -0
- package/src/ide.mjs +182 -0
- package/src/netbox.mjs +1 -1
- package/src/parity.mjs +20 -1
- package/src/relay.mjs +1 -1
- package/src/render.mjs +12 -3
- package/src/runtime.mjs +23 -1
- package/src/sandbox.mjs +19 -3
- package/src/server.mjs +46 -7
- package/src/session.mjs +53 -10
- package/src/shared-data.mjs +1 -1
- package/src/tui/acp.mjs +121 -19
- package/src/tui/app.mjs +24 -5
- package/src/tui/chat.mjs +43 -16
- package/src/tui/cmd-config.mjs +3 -1
- package/src/tui/cmd-core.mjs +11 -7
- package/src/tui/cmd-extensibility.mjs +13 -2
- package/src/tui/commands.mjs +2 -1
- package/src/tui/extensions-local.mjs +2 -0
- package/src/tui/mcp-config.mjs +22 -4
- package/src/tui/review-cli.mjs +15 -4
- package/src/tui/snapshots.mjs +57 -16
- package/src/tui/statusline.mjs +2 -1
- package/src/two-words.mjs +14 -8
- package/src/view-sync.mjs +48 -0
- package/src/wakeups.mjs +2 -2
- package/src/winenv.mjs +1 -1
- package/src/workspace-api.mjs +21 -4
- package/vendor/client-sdk/index.cjs +5 -4
- package/vendor/cloud-cli/mlai.mjs +184 -28
- package/worker/architecture_worker.py +33 -4
- package/worker/browser_tool.py +19 -1
- package/worker/build_planner.py +75 -7
- package/worker/chat_fork.py +4 -1
- package/worker/checkpoints.py +71 -8
- package/worker/cloud_pty.py +91 -2
- package/worker/command_safety.py +80 -3
- package/worker/compaction_brief.py +186 -0
- package/worker/connected_turn.py +30 -0
- package/worker/context_manager.py +6 -0
- package/worker/conversation.py +139 -12
- package/worker/deck_render.js +11 -0
- package/worker/deck_tools.py +825 -0
- package/worker/git_sync.py +42 -17
- package/worker/goals.py +4 -0
- package/worker/image_generation.py +102 -23
- package/worker/kimi_bridge.py +580 -52
- package/worker/kimi_gates.py +74 -6
- package/worker/kimi_hook.py +213 -19
- package/worker/media_tools.py +182 -0
- package/worker/memory.py +2 -2
- package/worker/mlai_hooks.py +314 -5
- package/worker/mlai_mcp.py +49 -11
- package/worker/mlai_review/README.md +35 -0
- package/worker/mlai_review/__init__.py +1 -0
- package/worker/mlai_review/__main__.py +261 -0
- package/worker/mlai_review/_compat.py +29 -0
- package/worker/mlai_review/analysis/__init__.py +3 -0
- package/worker/mlai_review/analysis/detect.py +50 -0
- package/worker/mlai_review/analysis/main.py +128 -0
- package/worker/mlai_review/analysis/messages.py +8 -0
- package/worker/mlai_review/analysis/tools.py +194 -0
- package/worker/mlai_review/application/__init__.py +1 -0
- package/worker/mlai_review/application/asking.py +53 -0
- package/worker/mlai_review/application/challengers.py +142 -0
- package/worker/mlai_review/application/context_builder.py +194 -0
- package/worker/mlai_review/application/errors.py +17 -0
- package/worker/mlai_review/application/feedback.py +52 -0
- package/worker/mlai_review/application/matching.py +46 -0
- package/worker/mlai_review/application/messages.py +41 -0
- package/worker/mlai_review/application/ports.py +222 -0
- package/worker/mlai_review/application/prompts.py +342 -0
- package/worker/mlai_review/application/review_service.py +200 -0
- package/worker/mlai_review/application/reviewers.py +438 -0
- package/worker/mlai_review/application/streaming.py +91 -0
- package/worker/mlai_review/application/tools.py +138 -0
- package/worker/mlai_review/application/usage.py +145 -0
- package/worker/mlai_review/application/write_check.py +46 -0
- package/worker/mlai_review/contracts/__init__.py +5 -0
- package/worker/mlai_review/contracts/analysis.py +57 -0
- package/worker/mlai_review/contracts/review.py +42 -0
- package/worker/mlai_review/domain/__init__.py +4 -0
- package/worker/mlai_review/domain/changes.py +77 -0
- package/worker/mlai_review/domain/context.py +147 -0
- package/worker/mlai_review/domain/diagnostics.py +239 -0
- package/worker/mlai_review/domain/feedback.py +216 -0
- package/worker/mlai_review/domain/findings.py +505 -0
- package/worker/mlai_review/domain/lifecycle.py +85 -0
- package/worker/mlai_review/domain/partition.py +42 -0
- package/worker/mlai_review/domain/relations.py +166 -0
- package/worker/mlai_review/domain/relevance.py +202 -0
- package/worker/mlai_review/domain/report.py +99 -0
- package/worker/mlai_review/domain/structure.py +56 -0
- package/worker/mlai_review/domain/writes.py +86 -0
- package/worker/mlai_review/eval.py +221 -0
- package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
- package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
- package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
- package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
- package/worker/mlai_review/feedback_report.py +60 -0
- package/worker/mlai_review/infrastructure/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
- package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
- package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
- package/worker/mlai_review/infrastructure/github/client.py +445 -0
- package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
- package/worker/mlai_review/infrastructure/github/http.py +31 -0
- package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
- package/worker/mlai_review/infrastructure/github/token.py +20 -0
- package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
- package/worker/mlai_review/infrastructure/local/events.py +100 -0
- package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
- package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
- package/worker/mlai_review/infrastructure/messages.py +54 -0
- package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
- package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
- package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
- package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
- package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
- package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
- package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
- package/worker/mlai_review/pipeline.py +89 -0
- package/worker/mlai_review/runtime.py +194 -0
- package/worker/mlai_review/settings.py +139 -0
- package/worker/model_stream.py +21 -7
- package/worker/offline_guard.py +1 -1
- package/worker/preview.py +62 -4
- package/worker/product_facts.py +20 -0
- package/worker/project_context.py +33 -7
- package/worker/reply_language.py +92 -10
- package/worker/repo_policy.py +5 -0
- package/worker/roles.json +199 -37
- package/worker/roles.py +271 -9
- package/worker/runner.py +366 -53
- package/worker/runtime_control.py +64 -3
- package/worker/secret_scan.py +3 -2
- package/worker/server_memory.py +122 -0
- package/worker/site_check.py +279 -0
- package/worker/skill_library.py +15 -3
- package/worker/skill_pack.py +170 -16
- package/worker/subagents.py +6 -2
- package/worker/tool_catalog.py +4 -3
- package/worker/verification.py +156 -19
- package/worker/wakeups.py +56 -4
package/src/host.mjs
CHANGED
|
@@ -9,9 +9,18 @@ import {runtimeInfo,ensureRuntime} from './runtime.mjs';
|
|
|
9
9
|
import {VERSION} from './home.mjs';
|
|
10
10
|
import {projectRoot} from './project.mjs';
|
|
11
11
|
import * as wt from './worktree.mjs';
|
|
12
|
+
import {Checkpoints,CheckpointError} from './checkpoints.mjs';
|
|
13
|
+
import {IdeError,validClient,validModel,validMcpServers,mcpSummary,renderContext,pickerModels,handoff,scanText,folderKey} from './ide.mjs';
|
|
14
|
+
import {modeName,applyMode,MODES as CLI_MODES} from './modes.mjs';
|
|
15
|
+
import {loadSettings} from './settings.mjs';
|
|
16
|
+
import {prepareEngine} from './engine.mjs';
|
|
12
17
|
|
|
13
18
|
export class HostError extends Error{constructor(message,status=400,code){super(message);this.status=status;this.code=code}}
|
|
14
19
|
const ID=/^[0-9a-f-]{36}$/;
|
|
20
|
+
/** Session mode names the API takes: the engine's (guided, auto, plan) and, for editor sessions, the app's (agent, ask). */
|
|
21
|
+
const modeOf=m=>MODES[m]?modeName(m):['agent','ask'].includes(m)?m:null;
|
|
22
|
+
/** Errors from the editor modules keep their status and code. */
|
|
23
|
+
const hostError=err=>err instanceof HostError?err:(err instanceof IdeError||err instanceof CheckpointError)?new HostError(err.message,err.status,err.code):err;
|
|
15
24
|
|
|
16
25
|
/**
|
|
17
26
|
* cloud: async () => Cloud for the signed-in account. Throw when signed out.
|
|
@@ -26,7 +35,9 @@ export class EngineHost extends EventEmitter{
|
|
|
26
35
|
s.on('event',e=>{entry.events.push(e);if(entry.events.length>5000)entry.events.splice(0,1000);out('harness',e)});
|
|
27
36
|
s.on('started',d=>{entry.events=[];out('started',d)});
|
|
28
37
|
s.on('done',r=>{entry.result=r;out('done',r)});
|
|
29
|
-
entry.out=out;this.live.set(id,entry);
|
|
38
|
+
entry.out=out;this.live.set(id,entry);
|
|
39
|
+
if(s.meta.client)this.#watch(entry);
|
|
40
|
+
return entry;
|
|
30
41
|
}
|
|
31
42
|
async entry(id){
|
|
32
43
|
if(typeof id!=='string'||!ID.test(id))throw new HostError('Unknown session',404);
|
|
@@ -38,11 +49,15 @@ export class EngineHost extends EventEmitter{
|
|
|
38
49
|
prepare(onProgress){return ensureRuntime({onProgress})}
|
|
39
50
|
/** Sessions for a project, including the ones working in their own worktree. Archived ones only when asked. */
|
|
40
51
|
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}));
|
|
52
|
+
const all=await Session.list(),want=root?folderKey(root):null;
|
|
53
|
+
return all.filter(m=>(!root||m.root===root||m.parent===root||(m.root&&folderKey(m.root)===want)||(m.parent&&folderKey(m.parent)===want))&&(archived||!m.archived)).map(m=>({...m,running:this.live.get(m.id)?.running||false}));
|
|
43
54
|
}
|
|
44
|
-
async create({cwd,mode='guided',worktree=false,name}){
|
|
45
|
-
|
|
55
|
+
async create({cwd,mode='guided',worktree=false,name,model,client,mcpServers}){
|
|
56
|
+
const asked=mode,ide={client:undefined,model:undefined,mcp:undefined};
|
|
57
|
+
try{ide.client=validClient(client);ide.model=validModel(model);ide.mcp=validMcpServers(mcpServers)}catch(err){throw hostError(err)}
|
|
58
|
+
const cli=modeOf(mode);
|
|
59
|
+
if(!cli)throw new HostError('Mode must be agent, plan, ask or auto');
|
|
60
|
+
if(!MODES[mode])mode=CLI_MODES[cli].engine;
|
|
46
61
|
if(typeof cwd!=='string'||!path.isAbsolute(cwd))throw new HostError('Choose an existing folder');
|
|
47
62
|
try{if(!(await stat(cwd)).isDirectory())throw 0}catch{throw new HostError('Choose an existing folder')}
|
|
48
63
|
let tree=null;
|
|
@@ -51,7 +66,10 @@ export class EngineHost extends EventEmitter{
|
|
|
51
66
|
if(!git)throw new HostError('Worktrees need a git repository. Turn off "Own worktree" to work in this folder directly.',400,'no_worktree');
|
|
52
67
|
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
68
|
}
|
|
54
|
-
const s=await Session.create({cwd,mode,worktree:tree});
|
|
69
|
+
const s=await Session.create({cwd,mode,worktree:tree});
|
|
70
|
+
if(ide.client||!MODES[asked]){s.meta.cliMode=cli;if(ide.client)s.meta.client=ide.client;if(ide.model)s.meta.model=ide.model;await s.save()}
|
|
71
|
+
const e=this.attach(s);if(ide.mcp)e.mcp=ide.mcp;
|
|
72
|
+
return ide.client?{...s.meta,mcpServers:mcpSummary(e.mcp)}:s.meta;
|
|
55
73
|
}
|
|
56
74
|
/** Rename, pin or archive a session. */
|
|
57
75
|
async annotate(id,{title,pinned,archived}={}){
|
|
@@ -75,20 +93,31 @@ export class EngineHost extends EventEmitter{
|
|
|
75
93
|
}catch(err){if(err instanceof HostError)throw err;throw new HostError(err.message,409,err.code)}
|
|
76
94
|
throw new HostError('Unknown worktree action');
|
|
77
95
|
}
|
|
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}}
|
|
96
|
+
async get(id){const e=await this.entry(id);return {...e.session.meta,running:e.running,pending:await e.session.pending(),result:e.result,...(e.session.meta.client?{mcpServers:mcpSummary(e.mcp)}:{})}}
|
|
79
97
|
async transcript(id){return (await this.entry(id)).session.transcript()}
|
|
80
98
|
events(id,after=0){const e=this.live.get(id);return e?e.events.slice(Math.max(0,after)):[]}
|
|
81
99
|
/** 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}={}){
|
|
100
|
+
async start(id,{task,steps,allowUnsandboxed=false,images,context,model,mcpServers}={}){
|
|
83
101
|
const e=await this.entry(id),s=e.session;
|
|
84
102
|
if(e.running)throw new HostError('A task is already running in this session',409,'session_busy');
|
|
85
103
|
if(typeof task!=='string'||!task.trim()||task.length>30000)throw new HostError('Enter a task under 30,000 characters');
|
|
104
|
+
let prefix='';
|
|
105
|
+
try{prefix=renderContext(context);if(s.meta.client){const m=validModel(model);if(m&&m!==s.meta.model){s.meta.model=m;await s.save()}const mcp=validMcpServers(mcpServers);if(mcp)e.mcp=mcp}}catch(err){throw hostError(err)}
|
|
106
|
+
if(prefix)task=prefix+'My request:\n'+task;
|
|
107
|
+
// Editors may send pasted images inline as data URLs; they are saved like uploads and passed by name.
|
|
108
|
+
if(Array.isArray(images)&&images.some(x=>typeof x==='string'&&x.startsWith('data:'))){
|
|
109
|
+
if(images.length>4)throw new HostError('Attach up to 4 images.');
|
|
110
|
+
const names=[];
|
|
111
|
+
for(const x of images){if(typeof x==='string'&&x.startsWith('data:')){const m=/^data:image\/(png|jpeg|jpg|webp|gif);base64,([A-Za-z0-9+/=\s]+)$/.exec(x);if(!m)throw new HostError('Use a PNG, JPEG, WebP or GIF image.');names.push((await this.addImage(id,Buffer.from(m[2],'base64'))).name)}else names.push(x)}
|
|
112
|
+
images=names;
|
|
113
|
+
}
|
|
86
114
|
let c;try{c=await this.cloud()}catch(err){throw new HostError(friendly(err),401,'not_signed_in')}
|
|
87
115
|
if(s.meta.worktree?.removed)throw new HostError('This session\u2019s worktree was removed. Start a new session.',409,'worktree_removed');
|
|
88
116
|
e.running=true;e.result=null;
|
|
89
117
|
let started=null;
|
|
90
118
|
const begun=new Promise(r=>s.once('started',x=>{started=x;r({started:x})}));
|
|
91
|
-
const
|
|
119
|
+
const run=s.meta.client?this.#ideTurn(e,task,{cloud:c,steps,allowUnsandboxed,images}):s.send(task,{cloud:c,steps:Number(steps)||120,allowUnsandboxed:allowUnsandboxed===true,images,onProgress:m=>e.out('progress',{message:m})});
|
|
120
|
+
const done=run
|
|
92
121
|
.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
122
|
.finally(()=>{e.running=false});
|
|
94
123
|
const first=await Promise.race([begun,done.then(r=>({result:r}))]);
|
|
@@ -101,8 +130,11 @@ export class EngineHost extends EventEmitter{
|
|
|
101
130
|
/** Saves an image for this session's next message and returns its name. */
|
|
102
131
|
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
132
|
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}={}){
|
|
133
|
+
async decide(id,{id:approval,verdict,revise,items}={}){
|
|
105
134
|
const e=await this.entry(id);
|
|
135
|
+
// Plan review: {revise:'text'} replies to the plan; {verdict:'approve', items:[0,2]} keeps only some tasks.
|
|
136
|
+
if(typeof revise==='string'&&revise.trim())verdict='revise:'+revise;
|
|
137
|
+
else if(verdict==='approve'&&Array.isArray(items)&&items.length&&items.every(n=>Number.isInteger(n)&&n>=0&&n<40))verdict='approve:'+items.join(',');
|
|
106
138
|
try{await e.session.decide(approval,verdict)}catch(err){throw new HostError(err.message)}
|
|
107
139
|
return {ok:true};
|
|
108
140
|
}
|
|
@@ -114,10 +146,106 @@ export class EngineHost extends EventEmitter{
|
|
|
114
146
|
}
|
|
115
147
|
async cancel(id){const e=await this.entry(id);await e.session.cancel();return {ok:true}}
|
|
116
148
|
async mode(id,{mode}={}){
|
|
117
|
-
const e=await this.entry(id);
|
|
118
|
-
if(!
|
|
149
|
+
const e=await this.entry(id),ide=Boolean(e.session.meta.client);
|
|
150
|
+
if(!modeOf(mode))throw new HostError('Mode must be agent, plan, ask or auto');
|
|
119
151
|
if(e.running)throw new HostError('Change the mode between tasks',409);
|
|
120
|
-
|
|
152
|
+
if(ide||!MODES[mode])await applyMode(e.session,modeOf(mode));
|
|
153
|
+
else{e.session.meta.mode=mode;await e.session.save()}
|
|
154
|
+
return e.session.meta;
|
|
155
|
+
}
|
|
156
|
+
// ---------- editor sessions (VS Code, JetBrains, ACP): checkpoints, models, fork, handoff ----------
|
|
157
|
+
cps(e){return e.cps||(e.cps=new Checkpoints(e.session))}
|
|
158
|
+
/** Checkpoint after every agent step that changed files; each one is announced with checkpoint and file_changed events. */
|
|
159
|
+
#watch(e){
|
|
160
|
+
const s=e.session;e.step=0;
|
|
161
|
+
const capture=label=>this.cps(e).capture({turn:Math.max(0,s.meta.turns.length-1),step:++e.step,label}).then(cp=>this.#announce(e,cp)).catch(()=>{});
|
|
162
|
+
s.on('event',ev=>{
|
|
163
|
+
if(!e.running||!ev||typeof ev!=='object')return;
|
|
164
|
+
if(ev.type==='output')capture(ev.output?String(ev.output).split('\n')[0].slice(0,120):'Step '+(e.step+1));
|
|
165
|
+
else if(ev.type==='finished')capture('End of turn');
|
|
166
|
+
});
|
|
167
|
+
s.on('done',()=>{e.final=capture('End of turn')});
|
|
168
|
+
}
|
|
169
|
+
#announce(e,cp){
|
|
170
|
+
if(!cp)return null;
|
|
171
|
+
const {tree,changes,...pub}=cp;
|
|
172
|
+
e.out('checkpoint',pub);
|
|
173
|
+
for(const f of changes||[])e.out('file_changed',{path:f.path,status:f.status,checkpoint:cp.id});
|
|
174
|
+
return pub;
|
|
175
|
+
}
|
|
176
|
+
/** One editor turn: a turn-start checkpoint, then the same Chat path as the terminal (modes, model, local extensions). */
|
|
177
|
+
async #ideTurn(e,task,{cloud,steps,allowUnsandboxed,images}){
|
|
178
|
+
const s=e.session;
|
|
179
|
+
const turn=s.meta.turns.length;e.step=0;
|
|
180
|
+
const cp=await this.cps(e).capture({turn,step:0,label:'Before turn '+(turn+1),force:true});
|
|
181
|
+
if(cp){const {tree,changes,...pub}=cp;e.out('checkpoint',pub)}
|
|
182
|
+
const {Chat}=await import('./tui/chat.mjs');
|
|
183
|
+
let settings={};try{settings=loadSettings({root:s.meta.parent||s.meta.root})}catch{}
|
|
184
|
+
const chat=new Chat({session:s,settings,cloud,flags:{model:s.meta.model||undefined,maxSteps:Number(steps)||undefined,...(allowUnsandboxed===true?{sandbox:false}:{})}});
|
|
185
|
+
chat.sessionMcp=e.mcp||null;
|
|
186
|
+
try{return await chat.send(task,{images,onProgress:m=>e.out('progress',{message:m})})}
|
|
187
|
+
finally{s.off('started',chat.onStarted)}
|
|
188
|
+
}
|
|
189
|
+
async checkpoints(id){const e=await this.entry(id);return this.cps(e).public()}
|
|
190
|
+
async changes(id,{since}={}){
|
|
191
|
+
const e=await this.entry(id);
|
|
192
|
+
try{return (await this.cps(e).changes(since||'turn-start')).files}catch(err){throw hostError(err)}
|
|
193
|
+
}
|
|
194
|
+
async revert(id,{path:file,hunks,since}={}){
|
|
195
|
+
const e=await this.entry(id);
|
|
196
|
+
if(e.running)throw new HostError('Wait for the task to finish, or stop it, before reverting',409,'session_busy');
|
|
197
|
+
if(hunks!=null&&(!Array.isArray(hunks)||hunks.length>500))throw new HostError('hunks must be a list');
|
|
198
|
+
try{const r=await this.cps(e).revert(file,{hunks,since:since||'turn-start'});e.out('file_changed',{path:r.path,status:r.status==='removed'?'deleted':'modified',checkpoint:r.checkpoint,reverted:true});return r}catch(err){throw hostError(err)}
|
|
199
|
+
}
|
|
200
|
+
/** Restores files, the conversation, or both to a checkpoint. A chat restore keeps the turns up to the checkpoint. */
|
|
201
|
+
async restore(id,{checkpoint,scope='both'}={}){
|
|
202
|
+
const e=await this.entry(id),s=e.session;
|
|
203
|
+
if(!['files','chat','both'].includes(scope))throw new HostError('scope must be files, chat or both');
|
|
204
|
+
if(e.running)throw new HostError('Stop the task before restoring a checkpoint',409,'session_busy');
|
|
205
|
+
let cp;try{cp=await this.cps(e).find(checkpoint)}catch(err){throw hostError(err)}
|
|
206
|
+
const out={checkpoint:cp.id,scope};
|
|
207
|
+
if(scope!=='chat'){out.files=await this.cps(e).restoreFiles(cp.id);for(const f of out.files.files)e.out('file_changed',{path:f,status:'modified',checkpoint:cp.id,restored:true})}
|
|
208
|
+
if(scope!=='files'){
|
|
209
|
+
// A turn-start checkpoint removes that turn too; a later step keeps its turn (the conversation cannot split a turn).
|
|
210
|
+
const keep=cp.step===0?cp.turn:cp.turn+1;
|
|
211
|
+
if(keep<s.meta.turns.length){
|
|
212
|
+
const {Chat}=await import('./tui/chat.mjs');const chat=new Chat({session:s});
|
|
213
|
+
await chat.rewind(keep,'chat');e.events=[];e.result=null;
|
|
214
|
+
await this.cps(e).forgetAfter(cp.step===0?cp.turn-1:cp.turn,{keep:cp.id});
|
|
215
|
+
}
|
|
216
|
+
out.turns=s.meta.turns.length;
|
|
217
|
+
}
|
|
218
|
+
e.out('restored',out);
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
/** A copy of the session (conversation, settings and checkpoints) under a new id. */
|
|
222
|
+
async fork(id,{title}={}){
|
|
223
|
+
const e=await this.entry(id);
|
|
224
|
+
const {Chat}=await import('./tui/chat.mjs');
|
|
225
|
+
const s=await Chat.fork(e.session.meta.id);
|
|
226
|
+
if(typeof title==='string'&&title.trim()){s.meta.title=title.replace(/[\0-\x1f]/g,' ').trim().slice(0,120);s.meta.renamed=true;await s.save()}
|
|
227
|
+
const n=this.attach(s);if(e.mcp)n.mcp=e.mcp;
|
|
228
|
+
return {...s.meta,...(s.meta.client?{mcpServers:mcpSummary(n.mcp)}:{})};
|
|
229
|
+
}
|
|
230
|
+
/** Sends the session to a cloud chat. {dryRun:true} (the default) only returns what would be sent. */
|
|
231
|
+
async handoff(id,{dryRun=true,note=''}={}){
|
|
232
|
+
const e=await this.entry(id),s=e.session;
|
|
233
|
+
if(typeof note!=='string'||note.length>5000)throw new HostError('Keep the note under 5,000 characters');
|
|
234
|
+
let python=null,worker=null;
|
|
235
|
+
try{python=await (await import('./runtime.mjs')).ensureRuntime({});worker=(await prepareEngine({cloud:null})).dir}catch{}
|
|
236
|
+
const scan=text=>scanText(text,{python,worker});
|
|
237
|
+
if(dryRun!==false){try{return await handoff(s,{note,scan})}catch(err){throw hostError(err)}}
|
|
238
|
+
if(e.running)throw new HostError('Wait for the task to finish, or stop it, before sending the session to the cloud.',409,'session_busy');
|
|
239
|
+
let c;try{c=await this.cloud()}catch(err){throw new HostError(friendly(err),401,'not_signed_in')}
|
|
240
|
+
try{return await handoff(s,{client:c.device,server:c.server,note,send:true,scan,commitAll:s.meta.worktree?()=>wt.commitAll({...s.meta.worktree,parent:s.meta.parent},s.meta.title||'Work from ML AI Code'):undefined})}
|
|
241
|
+
catch(err){const h=hostError(err);if(h instanceof HostError)throw h;throw new HostError(friendly(err),409,err.code)}
|
|
242
|
+
}
|
|
243
|
+
models(){return pickerModels({cloud:this.cloud})}
|
|
244
|
+
/** Replaces a session's editor MCP servers (an editor reconnecting to a session it did not create in this engine). */
|
|
245
|
+
async setMcp(id,{mcpServers}={}){
|
|
246
|
+
const e=await this.entry(id);
|
|
247
|
+
try{e.mcp=validMcpServers(mcpServers||[])}catch(err){throw hostError(err)}
|
|
248
|
+
return {mcpServers:mcpSummary(e.mcp)};
|
|
121
249
|
}
|
|
122
250
|
async close(){for(const e of this.live.values())if(e.running)await e.session.cancel().catch(()=>{})}
|
|
123
251
|
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// Editor quick calls on the `mlai serve` socket: tab completions (with next-edit suggestions), inline edit and commit
|
|
2
|
+
// messages. Each one is proxied to ML AI Code's cloud (/api/ide/*) with this computer's sign-in; the model is picked
|
|
3
|
+
// there. When the cloud cannot be reached and an on-device model is configured (MLAI_COMPLETIONS_MODEL or the request's
|
|
4
|
+
// `model`, an `offline:<server>:<model>` id from /v1/models), that model answers instead, on loopback only.
|
|
5
|
+
//
|
|
6
|
+
// POST /v1/complete {path,language,prefix,suffix,cursor:{line,character},recentEdits?,snippets?,model?}
|
|
7
|
+
// -> 200 {items:[{text,kind:'inline'|'next-edit',range?,targetLine?}]}
|
|
8
|
+
// (a request replaced by a newer one for the same file: 200 {items:[],superseded:true})
|
|
9
|
+
// POST /v1/edit {path,language,selection:{text,startLine,endLine},before,after,instruction,model?}
|
|
10
|
+
// -> 200 text/event-stream: `delta` {text} ..., `done` {text} (or `error` {message,code})
|
|
11
|
+
// POST /v1/commit-message {diff,model?} -> 200 {message}
|
|
12
|
+
// Errors before a reply starts: JSON {error,code} with the cloud's status (401 sign-in, 402 credits, 413, 429 ...).
|
|
13
|
+
//
|
|
14
|
+
// Completions: the latest request per file wins. A new one aborts the previous one (its cloud call too), and requests
|
|
15
|
+
// that arrive within the debounce window (MLAI_COMPLETE_DEBOUNCE_MS, default 30 ms) never reach the cloud. Every call
|
|
16
|
+
// is aborted when the editor closes the connection.
|
|
17
|
+
import {VERSION} from './home.mjs';
|
|
18
|
+
import {parseOfflineId,detectLocalModels,loopbackEndpoint} from './local-models.mjs';
|
|
19
|
+
import {networkError} from './offline.mjs';
|
|
20
|
+
|
|
21
|
+
export const QUICK_ROUTES=['complete','edit','commit-message'];
|
|
22
|
+
const CLOUD_PATH={complete:'/api/ide/complete',edit:'/api/ide/edit','commit-message':'/api/ide/commit-message'};
|
|
23
|
+
const MAX_BODY=96_000;
|
|
24
|
+
const TIMEOUT={complete:8_000,edit:120_000,'commit-message':30_000};
|
|
25
|
+
|
|
26
|
+
class QuickError extends Error{constructor(message,status=400,code='invalid_request'){super(message);this.status=status;this.code=code}}
|
|
27
|
+
const sendJson=(res,status,data)=>{if(res.headersSent||res.destroyed)return;res.writeHead(status,{'Content-Type':'application/json','Cache-Control':'no-store'});res.end(JSON.stringify(data))};
|
|
28
|
+
const sseLine=(type,data)=>'event: '+type+'\ndata: '+JSON.stringify(data)+'\n\n';
|
|
29
|
+
async function readBody(req){
|
|
30
|
+
// Drain an oversize body instead of breaking off (that would close the socket before the 413 is sent).
|
|
31
|
+
let raw='',size=0;for await(const c of req){size+=c.length;if(size<=MAX_BODY)raw+=c;else if(size>8*MAX_BODY)break}
|
|
32
|
+
if(size>MAX_BODY)throw new QuickError('Request is too large.',413,'request_too_large');
|
|
33
|
+
if(!raw)return {};
|
|
34
|
+
try{const v=JSON.parse(raw);if(!v||typeof v!=='object'||Array.isArray(v))throw 0;return v}catch{throw new QuickError('Invalid JSON body.')}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The same required fields the cloud checks, so a bad request is a 400 here, without a network call. */
|
|
38
|
+
export function validateQuick(name,b){
|
|
39
|
+
const str=(v,n,{required=false}={})=>{if(v===undefined||v===null){if(required)throw new QuickError(n+' is required.');return}if(typeof v!=='string')throw new QuickError(n+' must be text.')};
|
|
40
|
+
if(name==='complete'){str(b.prefix,'prefix',{required:true});str(b.suffix,'suffix');str(b.path,'path');str(b.language,'language');
|
|
41
|
+
if(b.cursor!==undefined&&(typeof b.cursor!=='object'||b.cursor===null))throw new QuickError('cursor must be {line, character}.');
|
|
42
|
+
if(b.recentEdits!==undefined&&!Array.isArray(b.recentEdits))throw new QuickError('recentEdits must be a list.');return}
|
|
43
|
+
if(name==='edit'){if(!b.selection||typeof b.selection!=='object')throw new QuickError('selection is required.');str(b.selection.text,'selection.text',{required:true});
|
|
44
|
+
str(b.instruction,'instruction',{required:true});if(!b.instruction.trim())throw new QuickError('Describe the change.');str(b.before,'before');str(b.after,'after');return}
|
|
45
|
+
str(b.diff,'diff',{required:true});if(!b.diff.trim())throw new QuickError('There are no changes to describe.');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/* ---------------- on-device fallback ---------------- */
|
|
49
|
+
/** Small prompts for a local model: the same reply formats as the cloud route, without next-edit suggestions. */
|
|
50
|
+
export function localMessages(name,b){
|
|
51
|
+
if(name==='complete')return [{role:'system',content:'You are a code completion engine. Reply with only <insert>...</insert> holding the text to insert at <CURSOR>: the rest of the line or statement, at most a few lines. Do not repeat text around the cursor. No code fences, no explanation.'},
|
|
52
|
+
{role:'user',content:'File: '+String(b.path||'untitled')+'\n\n'+String(b.prefix||'').slice(-6000)+'<CURSOR>'+String(b.suffix||'').slice(0,2000)}];
|
|
53
|
+
if(name==='edit')return [{role:'system',content:'Rewrite the selected code following the instruction. Reply with only the replacement code. No code fences, no explanation.'},
|
|
54
|
+
{role:'user',content:'Before:\n'+String(b.before||'').slice(-3000)+'\n\nSelection:\n'+String(b.selection?.text||'')+'\n\nAfter:\n'+String(b.after||'').slice(0,3000)+'\n\nInstruction: '+String(b.instruction||'')}];
|
|
55
|
+
return [{role:'system',content:'Write a git commit message: an imperative subject under 72 characters, optionally a blank line and a short body. Reply with only the message.'},{role:'user',content:String(b.diff||'').slice(0,24000)}];
|
|
56
|
+
}
|
|
57
|
+
const unfence=t=>String(t).replace(/^\s*```[\w#+.-]*[ \t]*\n/,'').replace(/\n?```\s*$/,'');
|
|
58
|
+
export function localCompletion(raw,b){
|
|
59
|
+
const m=/<insert>([\s\S]*?)(?:<\/insert>|$)/.exec(raw);let text=unfence(m?m[1]:raw).replace(/<CURSOR>/g,'');
|
|
60
|
+
const line=String(b.prefix||'').split('\n').pop()||'';if(line.trim()&&text.startsWith(line))text=text.slice(line.length);
|
|
61
|
+
text=text.split('\n').slice(0,16).join('\n').replace(/\s+$/,'');
|
|
62
|
+
return text.trim()?[{text,kind:'inline'}]:[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function createQuick({cloud,fetchImpl=globalThis.fetch,env=process.env,detect=detectLocalModels,debounceMs=Number(env.MLAI_COMPLETE_DEBOUNCE_MS??30)}={}){
|
|
66
|
+
const latest=new Map(); // completion key -> {seq, ctl}
|
|
67
|
+
const open=new Set();let seq=0;
|
|
68
|
+
const offlineModel=b=>parseOfflineId(typeof b.model==='string'&&b.model?b.model:env.MLAI_COMPLETIONS_MODEL)?(typeof b.model==='string'&&b.model?b.model:env.MLAI_COMPLETIONS_MODEL):null;
|
|
69
|
+
|
|
70
|
+
let cached=null;
|
|
71
|
+
const signedIn=async()=>{if(cached&&Date.now()-cached.at<60_000)return cached.c;const c=await cloud();cached={at:Date.now(),c};return c};
|
|
72
|
+
async function callCloud(name,b,signal){
|
|
73
|
+
let c;try{c=await signedIn()}catch(e){throw new QuickError(e?.message||'Not signed in. Run `mlai login`.',401,'sign_in_required')}
|
|
74
|
+
const token=await c.device?.tokenProvider?.();
|
|
75
|
+
if(!token)throw new QuickError('Not signed in. Run `mlai login`.',401,'sign_in_required');
|
|
76
|
+
const {model,...body}=b;
|
|
77
|
+
return (c.fetchImpl||fetchImpl)(c.server.replace(/\/$/,'')+CLOUD_PATH[name],{method:'POST',redirect:'error',signal,
|
|
78
|
+
headers:{'Content-Type':'application/json',Accept:name==='edit'?'text/event-stream':'application/json',Authorization:'Bearer '+token,'X-MLAI-Client':'laptop','X-MLAI-Version':VERSION},body:JSON.stringify(body)});
|
|
79
|
+
}
|
|
80
|
+
async function callLocal(name,b,id,signal){
|
|
81
|
+
const local=parseOfflineId(id);const found=(await detect({fetchImpl})).find(m=>m.id===id);
|
|
82
|
+
if(!local||!found||!loopbackEndpoint(found.endpoint))throw new QuickError('The offline model is not running. Start it on this computer, then try again.',503,'offline_model_missing');
|
|
83
|
+
const r=await fetchImpl(found.endpoint.replace(/\/$/,'')+'/chat/completions',{method:'POST',signal,headers:{'Content-Type':'application/json'},
|
|
84
|
+
body:JSON.stringify({model:local.model,messages:localMessages(name,b),temperature:0,max_tokens:name==='edit'?2048:name==='complete'?128:200,stream:false})});
|
|
85
|
+
if(!r.ok){await r.body?.cancel().catch(()=>{});throw new QuickError('The offline model had an error.',502,'offline_model_error')}
|
|
86
|
+
const d=await r.json().catch(()=>null);return String(d?.choices?.[0]?.message?.content||'');
|
|
87
|
+
}
|
|
88
|
+
/** Cloud first; on a network failure (or no sign-in) with an on-device model configured, that model. */
|
|
89
|
+
async function route(name,b,signal){
|
|
90
|
+
const id=offlineModel(b);
|
|
91
|
+
try{return {cloud:await callCloud(name,b,signal)}}
|
|
92
|
+
catch(e){
|
|
93
|
+
if(signal.aborted)throw e;
|
|
94
|
+
if(id&&(networkError(e)||e?.code==='sign_in_required'))return {local:await callLocal(name,b,id,signal)};
|
|
95
|
+
if(networkError(e))throw new QuickError('ML AI Code cannot be reached. Check the connection, or choose an offline model.',503,'offline');
|
|
96
|
+
throw e;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function handle(name,req,res){
|
|
101
|
+
const ctl=new AbortController();open.add(ctl);
|
|
102
|
+
const timer=setTimeout(()=>ctl.abort(),TIMEOUT[name]||30_000);
|
|
103
|
+
const hangup=()=>{if(!res.writableEnded)ctl.abort()};
|
|
104
|
+
req.on('aborted',hangup);res.on('close',hangup);
|
|
105
|
+
let mine=null,key=null;
|
|
106
|
+
try{
|
|
107
|
+
const b=await readBody(req);validateQuick(name,b);
|
|
108
|
+
if(name==='complete'){
|
|
109
|
+
key=String(b.path||'')+'\0'+String(req.headers['x-mlai-editor']||'');
|
|
110
|
+
latest.get(key)?.ctl.abort();
|
|
111
|
+
mine={seq:++seq,ctl};latest.set(key,mine);
|
|
112
|
+
if(debounceMs>0)await new Promise(r=>setTimeout(r,debounceMs));
|
|
113
|
+
if(ctl.signal.aborted)return sendJson(res,200,{items:[],superseded:true});
|
|
114
|
+
}
|
|
115
|
+
const out=await route(name,b,ctl.signal);
|
|
116
|
+
if(out.local!==undefined){
|
|
117
|
+
if(name==='complete')return sendJson(res,200,{items:localCompletion(out.local,b),offline:true});
|
|
118
|
+
if(name==='commit-message')return sendJson(res,200,{message:unfence(out.local).trim(),offline:true});
|
|
119
|
+
const text=unfence(out.local);res.writeHead(200,{'Content-Type':'text/event-stream','Cache-Control':'no-store'});
|
|
120
|
+
res.write(sseLine('delta',{text}));res.end(sseLine('done',{text}));return;
|
|
121
|
+
}
|
|
122
|
+
const r=out.cloud;
|
|
123
|
+
if(name==='edit'&&r.ok&&(r.headers.get('content-type')||'').includes('text/event-stream')){
|
|
124
|
+
res.writeHead(200,{'Content-Type':'text/event-stream','Cache-Control':'no-store',Connection:'keep-alive'});
|
|
125
|
+
const reader=r.body.getReader();
|
|
126
|
+
try{for(;;){const {done,value}=await reader.read();if(done)break;if(res.destroyed){await reader.cancel().catch(()=>{});break}res.write(Buffer.from(value))}}
|
|
127
|
+
catch{if(!ctl.signal.aborted)res.write(sseLine('error',{message:'The edit stopped early. Retry.',code:'interrupted'}))}
|
|
128
|
+
return res.end();
|
|
129
|
+
}
|
|
130
|
+
// An older server without these routes: say so plainly instead of passing a 404 through.
|
|
131
|
+
if(r.status===404||r.status===405){await r.body?.cancel().catch(()=>{});return sendJson(res,501,{error:'This ML AI Code server does not offer editor suggestions yet.',code:'not_available'})}
|
|
132
|
+
const text=await r.text();let data;try{data=JSON.parse(text)}catch{data=null}
|
|
133
|
+
if(!data||typeof data!=='object')return sendJson(res,502,{error:'The server returned an unreadable response.',code:'invalid_response'});
|
|
134
|
+
if(!r.ok)return sendJson(res,r.status,{error:String(data.error||'Request failed'),code:typeof data.code==='string'?data.code:'http_error'});
|
|
135
|
+
if(name==='complete'&&mine&&latest.get(key)!==mine)return sendJson(res,200,{items:[],superseded:true});
|
|
136
|
+
return sendJson(res,200,data);
|
|
137
|
+
}catch(e){
|
|
138
|
+
if(ctl.signal.aborted&&!(e instanceof QuickError&&e.status===413)){if(name==='complete')return sendJson(res,200,{items:[],superseded:true});return sendJson(res,499,{error:'Cancelled.',code:'cancelled'})}
|
|
139
|
+
const status=e instanceof QuickError?e.status:e?.status||502;
|
|
140
|
+
sendJson(res,status,{error:e instanceof QuickError||e?.status?e.message:'Suggestions are not available right now.',code:e?.code||'quick_failed'});
|
|
141
|
+
}finally{
|
|
142
|
+
clearTimeout(timer);open.delete(ctl);req.off('aborted',hangup);res.off('close',hangup);
|
|
143
|
+
if(key&&latest.get(key)===mine)latest.delete(key);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return {handle,features:[...QUICK_ROUTES],close(){for(const c of open)c.abort();open.clear();latest.clear()}};
|
|
147
|
+
}
|
package/src/ide.mjs
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editor plugin support for the engine host (VS Code, JetBrains, ACP clients): request validation for the extra
|
|
3
|
+
* session fields, editor context rendered into the task, the model picker list, and cloud handoff with a preview.
|
|
4
|
+
* Everything here is used only by sessions an editor creates; terminal and desktop sessions are unchanged.
|
|
5
|
+
*/
|
|
6
|
+
import {execFile} from 'node:child_process';
|
|
7
|
+
import {readFile,stat} from 'node:fs/promises';
|
|
8
|
+
import {realpathSync} from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import {FALLBACK_MODELS} from './tui/chat.mjs';
|
|
11
|
+
import {localModelsForPicker} from './local-models.mjs';
|
|
12
|
+
import {githubRepo,changedFiles,recap,sendToCloud,chatUrl} from './tui/handoff.mjs';
|
|
13
|
+
|
|
14
|
+
export class IdeError extends Error{constructor(message,status=400,code){super(message);this.status=status;this.code=code}}
|
|
15
|
+
/**
|
|
16
|
+
* One spelling per folder: real path, resolved, and on Windows case-insensitive with backslashes. Editors send the
|
|
17
|
+
* folder as they see it (Windows drive case and slashes, 8.3 names; macOS /var for /private/var); session roots
|
|
18
|
+
* come from git.
|
|
19
|
+
*/
|
|
20
|
+
export function folderKey(p){
|
|
21
|
+
let r=path.resolve(String(p));try{r=realpathSync.native(r)}catch{}
|
|
22
|
+
return process.platform==='win32'?r.replace(/\//g,'\\').toLowerCase():r;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const CLIENT_KINDS=['vscode','jetbrains','acp'];
|
|
26
|
+
const NAME=/^[A-Za-z0-9_-]{1,64}$/;
|
|
27
|
+
const MODEL_ID=/^[\w.:@/-]{1,160}$/;
|
|
28
|
+
const isObj=v=>v&&typeof v==='object'&&!Array.isArray(v);
|
|
29
|
+
const str=(v,n)=>typeof v==='string'?v.slice(0,n):'';
|
|
30
|
+
|
|
31
|
+
export function validClient(c){
|
|
32
|
+
if(c==null)return null;
|
|
33
|
+
if(!isObj(c)||!CLIENT_KINDS.includes(c.kind))throw new IdeError('client.kind must be vscode, jetbrains or acp');
|
|
34
|
+
return {kind:c.kind,version:str(c.version,40).replace(/[^\w.+-]/g,'')||null};
|
|
35
|
+
}
|
|
36
|
+
export function validModel(m){
|
|
37
|
+
if(m==null||m==='')return null;
|
|
38
|
+
if(typeof m!=='string'||!MODEL_ID.test(m))throw new IdeError('Choose a model from the list');
|
|
39
|
+
return m;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Editor MCP servers ([{name, url, headers?} | {name, command, args?, env?}]) as the engine's extension shape:
|
|
43
|
+
* {mcpServers:{name:{url,headers:{}}|{command,args,env:{}}}, mcpEnv:{name:{headers}|{VAR:value}}}. Header and env
|
|
44
|
+
* values travel in mcpEnv, like the CLI's ${VAR} secrets, so a bridge's bearer token never reaches request.json.
|
|
45
|
+
*/
|
|
46
|
+
export function validMcpServers(list){
|
|
47
|
+
if(list==null)return null;
|
|
48
|
+
if(!Array.isArray(list)||list.length>10)throw new IdeError('mcpServers must be a list of up to 10 servers');
|
|
49
|
+
const mcpServers={},mcpEnv={};
|
|
50
|
+
for(const s of list){
|
|
51
|
+
if(!isObj(s)||!NAME.test(String(s.name||''))||s.name==='mlai')throw new IdeError('Each MCP server needs a name of letters, digits, - and _');
|
|
52
|
+
if(mcpServers[s.name])throw new IdeError('MCP server names must be different');
|
|
53
|
+
if(typeof s.url==='string'){
|
|
54
|
+
let u;try{u=new URL(s.url)}catch{throw new IdeError('MCP server '+s.name+' has an invalid url')}
|
|
55
|
+
if(!['http:','https:'].includes(u.protocol))throw new IdeError('MCP server '+s.name+' needs an http or https url');
|
|
56
|
+
const headers={};
|
|
57
|
+
for(const [k,v] of Object.entries(isObj(s.headers)?s.headers:{})){if(!/^[A-Za-z0-9-]{1,64}$/.test(k)||typeof v!=='string'||v.length>4000)throw new IdeError('MCP server '+s.name+' has an invalid header');headers[k]=v}
|
|
58
|
+
mcpServers[s.name]={url:s.url,headers:{},...(s.transport==='sse'?{transport:'sse'}:{}),...(s.readOnly===true?{readOnly:true}:{})};
|
|
59
|
+
if(Object.keys(headers).length)mcpEnv[s.name]={headers};
|
|
60
|
+
}else if(typeof s.command==='string'&&s.command.trim()){
|
|
61
|
+
if(s.args!=null&&(!Array.isArray(s.args)||s.args.length>50||s.args.some(a=>typeof a!=='string'||a.length>4000)))throw new IdeError('MCP server '+s.name+' has invalid args');
|
|
62
|
+
const env={};
|
|
63
|
+
for(const [k,v] of Object.entries(isObj(s.env)?s.env:{})){if(!/^[A-Za-z_][A-Za-z0-9_]{0,99}$/.test(k)||typeof v!=='string'||v.length>8000)throw new IdeError('MCP server '+s.name+' has an invalid env entry');env[k]=v}
|
|
64
|
+
mcpServers[s.name]={command:s.command,args:s.args||[],env:{},...(typeof s.cwd==='string'&&path.isAbsolute(s.cwd)?{cwd:s.cwd}:{})};
|
|
65
|
+
if(Object.keys(env).length)mcpEnv[s.name]=env;
|
|
66
|
+
}else throw new IdeError('MCP server '+s.name+' needs a url or a command');
|
|
67
|
+
}
|
|
68
|
+
return {mcpServers,mcpEnv};
|
|
69
|
+
}
|
|
70
|
+
/** The names of a session's editor MCP servers (no urls, headers or env), for showing in session info. */
|
|
71
|
+
export const mcpSummary=m=>m?Object.entries(m.mcpServers).map(([name,s])=>({name,transport:s.url?'http':'stdio'})):[];
|
|
72
|
+
|
|
73
|
+
export const CONTEXT_KINDS=['file','selection','folder','symbol','problems','diff','terminal','url'];
|
|
74
|
+
const CONTEXT_TEXT=20000,CONTEXT_TOTAL=60000;
|
|
75
|
+
/**
|
|
76
|
+
* Editor context attached to a message ([{kind, path?, startLine?, endLine?, text?}]) as a block in front of the
|
|
77
|
+
* task. The agent can open files itself, so a file without text is a reference, not an upload.
|
|
78
|
+
*/
|
|
79
|
+
export function renderContext(list){
|
|
80
|
+
if(list==null)return '';
|
|
81
|
+
if(!Array.isArray(list)||list.length>30)throw new IdeError('Attach up to 30 context items');
|
|
82
|
+
const parts=[];let total=0;
|
|
83
|
+
for(const c of list){
|
|
84
|
+
if(!isObj(c)||!CONTEXT_KINDS.includes(c.kind))throw new IdeError('Context kind must be one of '+CONTEXT_KINDS.join(', '));
|
|
85
|
+
const p=str(c.path,1000).replace(/[\0-\x1f]/g,'');
|
|
86
|
+
const lines=Number.isInteger(c.startLine)?(Number.isInteger(c.endLine)&&c.endLine!==c.startLine?' lines '+c.startLine+'-'+c.endLine:' line '+c.startLine):'';
|
|
87
|
+
let text=typeof c.text==='string'?c.text.slice(0,CONTEXT_TEXT):'';
|
|
88
|
+
if(total+text.length>CONTEXT_TOTAL)text=text.slice(0,Math.max(0,CONTEXT_TOTAL-total));
|
|
89
|
+
total+=text.length;
|
|
90
|
+
const head={file:'File',selection:'Selection',folder:'Folder',symbol:'Symbol',problems:'Problems',diff:'Git diff',terminal:'Terminal output',url:'Link'}[c.kind]+(p?' '+p:'')+lines;
|
|
91
|
+
const fence=text.includes('```')?'~~~~':'```';
|
|
92
|
+
parts.push(text?head+':\n'+fence+'\n'+text+(text.endsWith('\n')?'':'\n')+fence:head);
|
|
93
|
+
}
|
|
94
|
+
return parts.length?'Context from my editor:\n\n'+parts.join('\n\n')+'\n\n':'';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The model picker for editors: only our models from the server's own list. On-device models appear only when this
|
|
99
|
+
* computer cannot reach ML AI Code, as one entry labelled "Offline model". No credits, prices or vendor names.
|
|
100
|
+
*/
|
|
101
|
+
export async function pickerModels({cloud,local=localModelsForPicker}={}){
|
|
102
|
+
let models=null,def=null,signedIn=true;
|
|
103
|
+
try{const c=await cloud();const caps=await c.device.capabilities();if(Array.isArray(caps?.models)&&caps.models.length){models=caps.models;def=caps.defaultModel||null}}catch{signedIn=false}
|
|
104
|
+
const list=(models||FALLBACK_MODELS).filter(m=>m&&typeof m.id==='string'&&m.id&&!m.id.startsWith('offline:'));
|
|
105
|
+
const d=def&&list.some(m=>m.id===def)?def:list.find(m=>m.available!==false)?.id;
|
|
106
|
+
const out=list.map(m=>({id:m.id,label:String(m.label||m.id).slice(0,80),...(typeof m.description==='string'&&m.description?{description:m.description.slice(0,200)}:{}),offline:false,available:m.available!==false,...(m.vision===false?{vision:false}:{}),...(m.id===d?{default:true}:{})}));
|
|
107
|
+
const lm=await local().catch(()=>({online:true,models:[]}));
|
|
108
|
+
if(lm&&lm.online===false&&lm.models.length)out.push({id:lm.models[0].id,label:'Offline model',description:'Runs on this computer',offline:true,available:true});
|
|
109
|
+
return {models:out,online:lm?.online!==false,signedIn};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function git(cwd,args,{input,timeout=60000}={}){
|
|
113
|
+
return new Promise(resolve=>{const p=execFile('git',args,{cwd,timeout,maxBuffer:32_000_000,windowsHide:true},(err,stdout,stderr)=>resolve({code:err?(typeof err.code==='number'?err.code:1):0,stdout:String(stdout||''),stderr:String(stderr||'')}));if(input!=null)p.stdin.end(input)});
|
|
114
|
+
}
|
|
115
|
+
const PATCH_FILES=200,PATCH_BYTES=3_000_000,FILE_BYTES=500_000;
|
|
116
|
+
/** Text files to upload for a folder without a GitHub remote: the non-ignored files of the folder. */
|
|
117
|
+
async function folderFiles(root){
|
|
118
|
+
const r=await git(root,['ls-files','-co','--exclude-standard','-z']);
|
|
119
|
+
const names=r.code?[]:r.stdout.split('\0').filter(Boolean);
|
|
120
|
+
const out=[];let bytes=0,skipped=0;
|
|
121
|
+
for(const n of names){
|
|
122
|
+
if(n.split('/').some(p=>p==='.git'||p==='node_modules'))continue;
|
|
123
|
+
let st;try{st=await stat(path.join(root,n))}catch{continue}
|
|
124
|
+
if(!st.isFile())continue;
|
|
125
|
+
if(st.size>FILE_BYTES||out.length>=PATCH_FILES||bytes+st.size>PATCH_BYTES){skipped++;continue}
|
|
126
|
+
const b=await readFile(path.join(root,n));if(b.includes(0)){skipped++;continue}
|
|
127
|
+
out.push({path:n,content:b.toString('utf8')});bytes+=b.length;
|
|
128
|
+
}
|
|
129
|
+
return {files:out,bytes,skipped,total:names.length};
|
|
130
|
+
}
|
|
131
|
+
/** Secret scan (worker/secret_scan.py, the same patterns as the server's push gate) over what would leave the machine. */
|
|
132
|
+
export async function scanText(text,{python,worker}){
|
|
133
|
+
if(!python||!worker||!text)return [];
|
|
134
|
+
return new Promise(resolve=>{
|
|
135
|
+
const p=execFile(python,[path.join(worker,'secret_scan.py'),'text'],{timeout:30000,maxBuffer:8_000_000,windowsHide:true},(err,stdout)=>{
|
|
136
|
+
try{const v=JSON.parse(String(stdout||'{}'));resolve(Array.isArray(v.findings)?v.findings.slice(0,20).map(f=>({file:f.file||null,line:f.line||null,kind:f.kind||null,name:f.name||null,preview:f.preview||null})):[])}catch{resolve(err?[{kind:'scan_failed',name:'The secret scan did not run'}]:[])}
|
|
137
|
+
});
|
|
138
|
+
p.stdin.end(text);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Cloud handoff. Without `send`, returns what would be sent: {mode:'branch'|'files', repo, files, task, findings}.
|
|
144
|
+
* With a GitHub origin the work goes as a branch (a snapshot commit on a new mlai/ branch; the person's own branch,
|
|
145
|
+
* index and files never change). Without one the folder's files are uploaded to a new cloud chat.
|
|
146
|
+
*/
|
|
147
|
+
export async function handoff(session,{client,server,note='',send=false,scan=async()=>[],commitAll}={}){
|
|
148
|
+
const m=session.meta,root=m.root;
|
|
149
|
+
const origin=m.git?(await git(root,['config','--get','remote.origin.url'])).stdout.trim():'';
|
|
150
|
+
const repo=githubRepo(origin);
|
|
151
|
+
const transcript=await session.transcript().catch(()=>null);
|
|
152
|
+
if(repo){
|
|
153
|
+
const files=await changedFiles(root);
|
|
154
|
+
const diff=(await git(root,['diff','HEAD'])).stdout;
|
|
155
|
+
const untracked=(await git(root,['ls-files','--others','--exclude-standard','-z'])).stdout.split('\0').filter(Boolean).slice(0,200);
|
|
156
|
+
let extra='';for(const f of untracked){try{const b=await readFile(path.join(root,f));if(b.length<FILE_BYTES&&!b.includes(0))extra+='\n--- '+f+'\n'+b.toString('utf8')}catch{}}
|
|
157
|
+
const findings=await scan(diff+extra);
|
|
158
|
+
const task=recap({title:m.title,transcript,branch:'mlai/<new branch>',files,note});
|
|
159
|
+
const preview={mode:'branch',repo:'https://github.com/'+repo,files,diffBytes:Buffer.byteLength(diff),task,findings,transcriptTurns:Array.isArray(transcript)?transcript.length:0};
|
|
160
|
+
if(!send)return {dryRun:true,...preview};
|
|
161
|
+
if(findings.length)throw new IdeError('Possible secrets found in the changes. Remove them before sending to the cloud.',409,'secrets_found');
|
|
162
|
+
const r=await sendToCloud(session,{client,server,note,commitAll});
|
|
163
|
+
return {url:r.url,chatId:r.name,mode:'branch',repo:'https://github.com/'+repo,branch:r.branch,commit:r.commit};
|
|
164
|
+
}
|
|
165
|
+
const {files,bytes,skipped,total}=await folderFiles(root);
|
|
166
|
+
const findings=await scan(files.map(f=>'--- '+f.path+'\n'+f.content).join('\n'));
|
|
167
|
+
const names=files.map(f=>f.path);
|
|
168
|
+
const task=recap({title:m.title,transcript,branch:'(none: the files were uploaded)',files:names.slice(0,40),note}).replace(/The work so far is on the branch [^\n]*?, which is checked out here\./,'The files from my computer were uploaded to this computer\u2019s project folder.');
|
|
169
|
+
const preview={mode:'files',repo:null,files:names,bytes,skipped,totalFiles:total,task,findings,transcriptTurns:Array.isArray(transcript)?transcript.length:0};
|
|
170
|
+
if(!send)return {dryRun:true,...preview};
|
|
171
|
+
if(findings.length)throw new IdeError('Possible secrets found in the files. Remove them before sending to the cloud.',409,'secrets_found');
|
|
172
|
+
if(!files.length)throw new IdeError('There are no files to send.',400,'nothing_to_send');
|
|
173
|
+
if(!client)throw new IdeError('Sign in first.',401,'not_signed_in');
|
|
174
|
+
if(session.child)throw new IdeError('Wait for the task to finish before sending the session to the cloud.',409,'session_busy');
|
|
175
|
+
const ws=await client.request('/api/workspace',{action:'create',repo:'',profile:'small',fit:true});
|
|
176
|
+
if(!ws?.token||!ws?.name)throw new IdeError('The cloud chat did not start. Try again.',502);
|
|
177
|
+
for(const f of files)await client.request('/api/workspace',{action:'write',token:ws.token,path:f.path,content:f.content});
|
|
178
|
+
await client.start(ws.token,task,{mode:'approve'});
|
|
179
|
+
m.cloudHandoffs=[...(Array.isArray(m.cloudHandoffs)?m.cloudHandoffs:[]),{name:ws.name,mode:'files',at:new Date().toISOString()}].slice(-10);
|
|
180
|
+
await session.save?.().catch(()=>{});
|
|
181
|
+
return {url:chatUrl(server,ws.name),chatId:ws.name,mode:'files',files:names.length};
|
|
182
|
+
}
|
package/src/netbox.mjs
CHANGED
|
@@ -90,7 +90,7 @@ export async function startRelay({targets,deny=[],kind,tmp=os.tmpdir()}){
|
|
|
90
90
|
*/
|
|
91
91
|
export function offlineEnv(env,relay){
|
|
92
92
|
const out={...env};
|
|
93
|
-
if(typeof out
|
|
93
|
+
for(const k of ['MINI_IMAGE_GATEWAY','MINI_MEDIA_GATEWAY'])if(typeof out[k]==='string')out[k]=relay.rewrite(out[k]);
|
|
94
94
|
const np=[out.NO_PROXY||out.no_proxy,'127.0.0.1','localhost'].filter(Boolean).join(',');
|
|
95
95
|
out.NO_PROXY=np;out.no_proxy=np;
|
|
96
96
|
return out;
|
package/src/parity.mjs
CHANGED
|
@@ -37,6 +37,8 @@ export function importSession(source){
|
|
|
37
37
|
|
|
38
38
|
/** lib/checkpoints.ts */
|
|
39
39
|
export const STEP_MARK='mini-step: ';
|
|
40
|
+
/** The pre-task checkpoint: a snapshot like a step's (parent = HEAD, never on a branch). Older ones were commits on the branch. */
|
|
41
|
+
export const TASK_MARK='mini-task: ';
|
|
40
42
|
export function checkpointRef(value){if(!value||value==='HEAD')return 'HEAD';if(typeof value!=='string'||!/^mini-checkpoints\/\d{13}$/.test(value))throw new Error('Invalid checkpoint');return 'refs/'+value}
|
|
41
43
|
export function parseCheckpoints(output,limit=40){
|
|
42
44
|
return output.trim().split('\n').filter(Boolean).slice(0,limit).map(line=>{
|
|
@@ -45,12 +47,29 @@ export function parseCheckpoints(output,limit=40){
|
|
|
45
47
|
return {id,sha,step,label:step?subject.slice(STEP_MARK.length):'Before task',...(Number.isFinite(at)?{at}:{})};
|
|
46
48
|
});
|
|
47
49
|
}
|
|
50
|
+
/* BUG-0601: rescue stashes (same as lib/checkpoints.ts). */
|
|
51
|
+
const RESCUE_RE=/rescue before checkpoint restore/;
|
|
52
|
+
export const RESCUE_LIST_ARGS=['stash','list','--format=%H%x09%ct%x09%gs'];
|
|
53
|
+
export function parseRescues(output,limit=20){
|
|
54
|
+
return output.split('\n').map(l=>l.split('\t')).filter(p=>p.length>=3&&/^[0-9a-f]{40}$/.test(p[0])&&RESCUE_RE.test(p.slice(2).join('\t')))
|
|
55
|
+
.slice(0,limit).map(([sha,ct])=>({sha,at:Number(ct)*1000||0,label:'Rescue '+sha.slice(0,12)}));
|
|
56
|
+
}
|
|
57
|
+
export async function restoreRescue(sha,git){
|
|
58
|
+
if(typeof sha!=='string'||!/^[0-9a-f]{40}$/.test(sha))throw new Error('Invalid rescue stash');
|
|
59
|
+
const index=async()=>{const list=(await git(['stash','list','--format=%H'])).trim().split('\n');const i=list.indexOf(sha);if(i<0)throw new Error('This rescue stash is no longer there.');return 'stash@{'+i+'}'};
|
|
60
|
+
await index();
|
|
61
|
+
const dirty=(await git(['status','--porcelain'])).trim();let rescue;
|
|
62
|
+
if(dirty){await git(['stash','push','--include-untracked','-m','Mini Code rescue before checkpoint restore']);rescue=(await git(['rev-parse','refs/stash'])).trim();}
|
|
63
|
+
await git(['stash','apply',await index()]);
|
|
64
|
+
await git(['stash','drop',await index()]);
|
|
65
|
+
return {ok:true,...(rescue?{rescue}:{})};
|
|
66
|
+
}
|
|
48
67
|
export async function rewind(value,git){
|
|
49
68
|
const ref=checkpointRef(value);if(ref!=='HEAD')await git(['show-ref','--verify',ref]);
|
|
50
69
|
const dirty=(await git(['status','--porcelain'])).trim();let rescue;
|
|
51
70
|
if(dirty){await git(['stash','push','--include-untracked','-m','Mini Code rescue before checkpoint restore']);rescue=(await git(['rev-parse','refs/stash'])).trim();}
|
|
52
71
|
const subject=ref==='HEAD'?'':(await git(['log','-1','--format=%s',ref])).trim();
|
|
53
|
-
if(subject.startsWith(STEP_MARK)){
|
|
72
|
+
if(subject.startsWith(STEP_MARK)||subject.startsWith(TASK_MARK)){
|
|
54
73
|
const parents=(await git(['rev-list','--parents','-n','1',ref])).trim().split(' ').slice(1);
|
|
55
74
|
if(parents[0])await git(['reset','--hard',parents[0]]);
|
|
56
75
|
await git(['clean','-fd']);
|
package/src/relay.mjs
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
/** What another device may ask for. The server checks the same list; manual terminal commands and deletes stay here. */
|
|
8
8
|
export const RELAY_OPS=new Set(['run','resume','approve','steer','stop','history','read','write','read_raw','diff','git_status','git_pull','git_push',
|
|
9
|
-
'checkpoints','checkpoint_compare','restore','memory_read','memory_save','memory_proposal','memory_apply','export','session_export','screenshot',
|
|
9
|
+
'checkpoints','checkpoint_compare','restore','rescues','rescue_restore','memory_read','memory_save','memory_proposal','memory_apply','export','session_export','screenshot',
|
|
10
10
|
'capabilities','secret_scan','skill_history','skill_save','policy_get','policy_validate','policy_save','pr_plan','pr_publish']);
|
|
11
11
|
export const RELAY_LIMITS={pollMs:20000,watchMs:120000,watchedDebounceMs:700,idleDebounceMs:1500,diffEveryMs:15000,resultChars:4_000_000,maxBackoffMs:60000,olderServerMs:300000,signedOutMs:60000,retryMs:3000,events:300,eventText:6000};
|
|
12
12
|
const NAME=/^mini-[a-f0-9-]{36}$/;
|