mlai-code 0.0.0-stage → 0.3.0
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
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// Core session commands (lead). Other areas add their own cmd-<area>.mjs; see commands.mjs.
|
|
2
|
+
import {execFileSync,spawn} from 'node:child_process';
|
|
3
|
+
import {writeFile} from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import {helpText,CommandError} from './commands.mjs';
|
|
6
|
+
import {MODES,MODE_NAMES,modeName} from '../modes.mjs';
|
|
7
|
+
import {Session} from '../session.mjs';
|
|
8
|
+
import {VERSION,home} from '../home.mjs';
|
|
9
|
+
import {creditsFor,formatCredits,answerOf} from './chat.mjs';
|
|
10
|
+
|
|
11
|
+
const need=ctx=>{if(!ctx.chat)throw new CommandError('This command needs an interactive session.');return ctx.chat};
|
|
12
|
+
const short=(t,n=60)=>{const s=String(t||'').replace(/\s+/g,' ').trim();return s.length>n?s.slice(0,n-1)+'…':s};
|
|
13
|
+
const ago=iso=>{const m=Math.round((Date.now()-Date.parse(iso))/60000);return !Number.isFinite(m)?'':m<1?'just now':m<60?m+'m ago':m<1440?Math.round(m/60)+'h ago':Math.round(m/1440)+'d ago'};
|
|
14
|
+
const git=(cwd,args)=>{try{return execFileSync('git',args,{cwd,encoding:'utf8',stdio:['ignore','pipe','ignore']}).trim()}catch{return ''}};
|
|
15
|
+
|
|
16
|
+
/** Sessions matching an id prefix or a name; the newest first. */
|
|
17
|
+
export async function findSessions(q,{root=null}={}){
|
|
18
|
+
const all=await Session.list(),s=String(q||'').trim().toLowerCase();
|
|
19
|
+
if(!s)return root?all.filter(m=>m.root===root||m.parent===root):all;
|
|
20
|
+
const byId=all.filter(m=>m.id.startsWith(s));if(byId.length)return byId;
|
|
21
|
+
const exact=all.filter(m=>String(m.title||'').toLowerCase()===s);if(exact.length)return exact;
|
|
22
|
+
return all.filter(m=>String(m.title||'').toLowerCase().includes(s));
|
|
23
|
+
}
|
|
24
|
+
export const sessionRow=m=>({label:short(m.title||'Untitled',40).padEnd(40)+' '+m.id.slice(0,8),detail:m.folder+' · '+m.turns.length+' turn'+(m.turns.length===1?'':'s')+' · '+ago(m.updated||m.created),value:m.id});
|
|
25
|
+
|
|
26
|
+
/** Copies text to the system clipboard; falls back to the terminal's OSC 52 sequence (works over SSH in most terminals). */
|
|
27
|
+
export async function copyText(text,{write=t=>process.stdout.write(t)}={}){
|
|
28
|
+
const tries=process.platform==='darwin'?[['pbcopy',[]]]:process.platform==='win32'?[['clip',[]]]:[['wl-copy',[]],['xclip',['-selection','clipboard']],['xsel',['--clipboard','--input']]];
|
|
29
|
+
for(const [cmd,args] of tries){
|
|
30
|
+
const ok=await new Promise(r=>{try{const p=spawn(cmd,args,{stdio:['pipe','ignore','ignore']});p.on('error',()=>r(false));p.on('exit',c=>r(c===0));p.stdin.end(text)}catch{r(false)}});
|
|
31
|
+
if(ok)return cmd;
|
|
32
|
+
}
|
|
33
|
+
write('\x1b]52;c;'+Buffer.from(text).toString('base64')+'\x07');return 'terminal';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const EFFORT_NAMES=['low','medium','high','max'];
|
|
37
|
+
const EFFORT_HINT={low:'Fastest, fewest credits',medium:'Deeper on plans and after failures (default)',high:'Thinks hard on every step',max:'Most thorough, slowest'};
|
|
38
|
+
export function install(register){
|
|
39
|
+
register({name:'help',summary:'List commands and keys',run:async ctx=>{ctx.out(helpText(ctx));ctx.out('\nKeys: Shift+Tab switches mode, Esc stops the agent, Ctrl-C twice exits. Start a line with ! to run a shell command.')}});
|
|
40
|
+
register({name:'quit',aliases:['exit','q'],summary:'Leave the session',run:async()=>({exit:true})});
|
|
41
|
+
|
|
42
|
+
register({name:'model',args:'[filter] [low|medium|high|max]',summary:'Choose the model and its reasoning effort for the next turns',complete:async ctx=>(await need(ctx).loadModels()).filter(m=>m.available!==false).map(m=>m.id),run:async(ctx,a)=>{
|
|
43
|
+
const chat=need(ctx),list=await chat.loadModels();
|
|
44
|
+
let effort=null;const w=String(a||'').trim().split(/\s+/);if(w.length>1&&EFFORT_NAMES.includes(w.at(-1).toLowerCase())){effort=w.pop().toLowerCase();a=w.join(' ')}
|
|
45
|
+
const exact=a&&list.find(m=>m.id===a||m.label.toLowerCase()===a.toLowerCase());
|
|
46
|
+
const done=async m=>{
|
|
47
|
+
// Effort only where the model takes it (the server's efforts list); a picker when chosen from the list.
|
|
48
|
+
const levels=Array.isArray(m.efforts)?m.efforts:[];
|
|
49
|
+
if(effort){if(levels.length&&!levels.includes(effort))throw new CommandError(m.label+' takes '+levels.join(', ')+' effort');await chat.setEffort(effort).catch(e=>{throw new CommandError(e.message)})}
|
|
50
|
+
else if(!exact&&levels.length>1&&ctx.ui.pick){
|
|
51
|
+
const pick=await ctx.ui.pick(levels.map(l=>({label:(l===chat.effort?'* ':' ')+l,detail:EFFORT_HINT[l]||'',value:l})),{title:'Reasoning effort for '+m.label+' (now '+chat.effort+')'});
|
|
52
|
+
if(pick)await chat.setEffort(pick.value).catch(e=>{throw new CommandError(e.message)});
|
|
53
|
+
}
|
|
54
|
+
ctx.out('Model: '+m.label+' · '+m.creditLabel+(levels.length?' · '+chat.effort+' effort':''));return {model:m.id,effort:chat.effort};
|
|
55
|
+
};
|
|
56
|
+
if(exact){const m=await chat.setModel(exact.id).catch(e=>{throw new CommandError(e.message)});return done({...exact,...m})}
|
|
57
|
+
const rows=list.map(m=>({label:(m.id===chat.info?.id?'* ':' ')+m.label,detail:m.available===false?'Not available yet':m.creditLabel+(m.vision===false?' · text only':'')+(m.efforts?.length?' · effort '+m.efforts.join('/'):''),value:m.id,disabled:m.available===false}));
|
|
58
|
+
const pick=await ctx.ui.pick(rows.filter(r=>!r.disabled).concat(rows.filter(r=>r.disabled)),{title:'Model (now '+chat.modelLabel+')',filter:a});
|
|
59
|
+
if(!pick)return;
|
|
60
|
+
if(pick.disabled){ctx.ui.error(pick.label.trim()+' is not available yet.');return}
|
|
61
|
+
const m=await chat.setModel(pick.value);return done({...list.find(x=>x.id===m.id),...m});
|
|
62
|
+
}});
|
|
63
|
+
register({name:'mode',args:'[agent|plan|ask|auto]',summary:'Show or switch the mode',complete:async()=>MODE_NAMES,run:async(ctx,a)=>{
|
|
64
|
+
const chat=need(ctx);
|
|
65
|
+
if(!a){ctx.out(MODE_NAMES.map(m=>(m===chat.mode?'* ':' ')+m.padEnd(6)+' '+MODES[m].summary).join('\n'));return}
|
|
66
|
+
const m=modeName(a);if(!m)throw new CommandError('Modes: '+MODE_NAMES.join(', '));
|
|
67
|
+
await ctx.ui.setMode(m);ctx.out('Mode: '+m+' · '+MODES[m].summary);return {mode:m};
|
|
68
|
+
}});
|
|
69
|
+
register({name:'plan',args:'[task]',summary:'Switch to plan mode, or plan one task',run:async(ctx,a)=>{await ctx.ui.setMode('plan');ctx.out('Mode: plan · '+MODES.plan.summary);if(a)return {send:a}}});
|
|
70
|
+
register({name:'ask',args:'[question]',summary:'Switch to ask mode, or ask one question',run:async(ctx,a)=>{await ctx.ui.setMode('ask');ctx.out('Mode: ask · '+MODES.ask.summary);if(a)return {send:a}}});
|
|
71
|
+
register({name:'effort',args:'[low|medium|high|max]',summary:'Show or set reasoning effort',complete:async ctx=>need(ctx).efforts,run:async(ctx,a)=>{
|
|
72
|
+
const chat=need(ctx);const levels=chat.efforts;
|
|
73
|
+
if(!a){ctx.out('Effort: '+chat.effort+(levels.length?' ('+chat.modelLabel+' takes '+levels.join(', ')+')':' ('+chat.modelLabel+' does not use it)'));return}
|
|
74
|
+
await chat.setEffort(a.toLowerCase()).catch(e=>{throw new CommandError(e.message)});ctx.out('Effort: '+chat.effort);
|
|
75
|
+
}});
|
|
76
|
+
|
|
77
|
+
register({name:'status',summary:'Session, model, mode, account and sandbox',run:async ctx=>{
|
|
78
|
+
const chat=need(ctx),m=ctx.session.meta,branch=chat.branch;
|
|
79
|
+
const rows=[['Session',(m.title||'Untitled')+' · '+m.id],['Folder',m.root+(branch?' · '+branch:'')+(m.worktree?' (worktree)':'')],
|
|
80
|
+
['Model',chat.modelLabel+' · '+chat.credits],['Mode',chat.mode+' · '+MODES[chat.mode].summary],['Effort',chat.effort],['Max steps',String(chat.steps)],
|
|
81
|
+
['Turns',m.turns.length+' · '+formatCredits(creditsFor(chat.usage.tokens))+' credits used here'],
|
|
82
|
+
...(chat.addDirs.length?[['Extra folders',chat.addDirs.join(', ')]]:[]),
|
|
83
|
+
['Server',ctx.server?.server||'signed out'],
|
|
84
|
+
['Settings',Object.entries(ctx.settings?.paths||{}).map(([k,v])=>k+' '+v).join('; ')||'defaults'],
|
|
85
|
+
...((ctx.settings?.warnings||[]).length?[['Warnings',ctx.settings.warnings.join('; ')]]:[])];
|
|
86
|
+
try{const {sandboxKind}=await import('../session.mjs');rows.push(['Sandbox',sandboxKind()])}catch{}
|
|
87
|
+
ctx.out(rows.map(([k,v])=>k.padEnd(14)+v).join('\n'));
|
|
88
|
+
}});
|
|
89
|
+
register({name:'about',summary:'Version, coding engine and where data lives',run:async ctx=>{
|
|
90
|
+
let engine='bundled';try{const {activeEngine}=await import('../engine.mjs');const h=await activeEngine();engine=h.source+' '+String(h.version).slice(0,8)}catch{}
|
|
91
|
+
ctx.out(['ML AI Code '+VERSION,'Coding engine: '+engine,'Node '+process.versions.node+' · '+process.platform+' '+process.arch,'Local data: '+home(),'Server: '+(ctx.server?.server||'not signed in'),'Docs: '+(ctx.server?.server||'https://mlai-mini-code.vercel.app')+'/docs'].join('\n'));
|
|
92
|
+
}});
|
|
93
|
+
register({name:'rename',args:'<name>',summary:'Name this session (mlai -r <name> resumes it)',run:async(ctx,a)=>{const t=await need(ctx).rename(a).catch(e=>{throw new CommandError(e.message)});ctx.out('Renamed to '+t)}});
|
|
94
|
+
|
|
95
|
+
register({name:'clear',aliases:['new'],summary:'Start a fresh session in this folder',run:async ctx=>{
|
|
96
|
+
const chat=need(ctx),m=ctx.session.meta;
|
|
97
|
+
const next=m.worktree?await Session.create({mode:m.mode,worktree:{path:m.worktree.path,parent:m.parent,branch:m.worktree.branch,base:m.worktree.base,baseBranch:m.worktree.baseBranch,copied:[],named:true}}):await Session.create({cwd:m.root,mode:m.mode});
|
|
98
|
+
if(chat.model)next.meta.model=chat.model;next.meta.cliMode=chat.mode;await next.save();
|
|
99
|
+
await ctx.ui.setSession(next);ctx.out('New session '+next.meta.id.slice(0,8)+'. Earlier sessions: /resume');return {session:next.meta.id};
|
|
100
|
+
}});
|
|
101
|
+
register({name:'resume',args:'[id|name]',summary:'Switch to an earlier session',run:async(ctx,a)=>{
|
|
102
|
+
const root=ctx.session?.meta.parent||ctx.session?.meta.root;
|
|
103
|
+
let found=(await findSessions(a,{root:a?null:root})).filter(m=>m.id!==ctx.session?.meta.id);
|
|
104
|
+
if(!a&&!found.length)found=(await findSessions('')).filter(m=>m.id!==ctx.session?.meta.id);
|
|
105
|
+
if(!found.length)throw new CommandError(a?'No session matches '+a+'.':'No other sessions yet.');
|
|
106
|
+
const id=found.length===1&&a?found[0].id:(await ctx.ui.pick(found.slice(0,40).map(sessionRow),{title:'Resume a session'}))?.value;
|
|
107
|
+
if(!id)return;
|
|
108
|
+
const s=await Session.open(id);await ctx.ui.setSession(s);
|
|
109
|
+
const t=await s.transcript(),last=t.at(-1);
|
|
110
|
+
ctx.out('Resumed '+(s.meta.title||'session')+' · '+s.meta.turns.length+' turns'+(last?'\nLast: '+short(last.task,70)+'\n'+short(answerOf(last),300):''));
|
|
111
|
+
return {session:id};
|
|
112
|
+
}});
|
|
113
|
+
register({name:'compact',aliases:['summarize'],args:'[focus]',summary:'Summarize the conversation to free context',run:async(ctx,a)=>{
|
|
114
|
+
const chat=need(ctx);if(!ctx.session.meta.turns.length){ctx.out('Nothing to compact yet.');return}
|
|
115
|
+
ctx.out('Summarizing the conversation...');
|
|
116
|
+
const r=await chat.compact(a);
|
|
117
|
+
if(!r.ok)throw new CommandError('Could not summarize: '+(r.result?.error||r.result?.state));
|
|
118
|
+
ctx.out('Compacted. The next turns continue from this summary:\n'+short(r.summary,1200));
|
|
119
|
+
}});
|
|
120
|
+
register({name:'rewind',args:'[turn]',summary:'Restore files and conversation to an earlier turn',run:async(ctx,a)=>{
|
|
121
|
+
const chat=need(ctx),points=chat.rewindPoints();
|
|
122
|
+
if(!points.length){ctx.out('Nothing to rewind yet.');return}
|
|
123
|
+
let turn=a?Number(a)-1:null;
|
|
124
|
+
if(turn==null){const p=await ctx.ui.pick(points.slice().reverse().map(p=>({label:'Turn '+(p.turn+1)+': '+short(p.task,60),detail:ago(p.when),value:p.turn})),{title:'Rewind to before which turn?'});if(!p)return;turn=p.value}
|
|
125
|
+
if(!points.some(p=>p.turn===turn))throw new CommandError('Pick a turn from '+(points[0].turn+1)+' to '+(points.at(-1).turn+1)+'.');
|
|
126
|
+
const what=(await ctx.ui.pick([{label:'Files and conversation',value:'both'},{label:'Conversation only',value:'chat'},{label:'Files only',value:'files'}],{title:'Restore'}))?.value;
|
|
127
|
+
if(!what)return;
|
|
128
|
+
const r=await chat.rewind(turn,what).catch(e=>{throw new CommandError(e.message)});
|
|
129
|
+
ctx.out('Rewound to before turn '+(turn+1)+(r.files?' · '+r.files.restored+' file'+(r.files.restored===1?'':'s')+' restored, '+r.files.removed+' removed':'')+'.');
|
|
130
|
+
return {rewound:turn};
|
|
131
|
+
}});
|
|
132
|
+
register({name:'usage',aliases:['cost'],summary:'Credits and tokens used, and what is left',run:async ctx=>{
|
|
133
|
+
const chat=need(ctx),u=chat.usage;
|
|
134
|
+
ctx.out(['This session: '+formatCredits(creditsFor(u.tokens))+' credits · '+u.tokens.toLocaleString('en-US')+' tokens · '+u.calls+' model call'+(u.calls===1?'':'s')+' · '+ctx.session.meta.turns.length+' turn'+(ctx.session.meta.turns.length===1?'':'s'),
|
|
135
|
+
'Model: '+chat.modelLabel+' · '+chat.credits,
|
|
136
|
+
chat.tokensLeft!=null?'Plan: about '+formatCredits(creditsFor(chat.tokensLeft))+' credits left':'Plan: credits left show after the first turn',
|
|
137
|
+
'Plan and billing: the account menu in the app'].join('\n'));
|
|
138
|
+
}});
|
|
139
|
+
register({name:'export',args:'[file]',summary:'Save the conversation as Markdown',run:async(ctx,a)=>{
|
|
140
|
+
const chat=need(ctx),md=await chat.markdown();
|
|
141
|
+
const name=a||('mlai-'+String(ctx.session.meta.title||'session').toLowerCase().replace(/[^a-z0-9]+/g,'-').replace(/^-|-$/g,'')+'-'+new Date().toISOString().slice(0,10)+'.md');
|
|
142
|
+
const file=path.resolve(ctx.session.meta.root,name);
|
|
143
|
+
await writeFile(file,md);ctx.out('Saved '+file);return {file};
|
|
144
|
+
}});
|
|
145
|
+
register({name:'copy',summary:'Copy the last answer to the clipboard',run:async ctx=>{
|
|
146
|
+
const text=await need(ctx).lastAnswer();if(!text){ctx.out('No answer to copy yet.');return}
|
|
147
|
+
const how=await copyText(text);ctx.out('Copied the last answer'+(how==='terminal'?' (through the terminal)':'')+'.');
|
|
148
|
+
}});
|
|
149
|
+
register({name:'logout',summary:'Sign out on this device',run:async ctx=>{
|
|
150
|
+
if(!await ctx.ui.confirm('Sign out on this device?',{default:false}))return;
|
|
151
|
+
const {loadLogin,clearLogin}=await import('../credentials.mjs');const l=await loadLogin().catch(()=>null);
|
|
152
|
+
if(l?.token&&l.store!=='env')try{await ctx.server?.device?.revoke?.()}catch{}
|
|
153
|
+
await clearLogin();ctx.out('Signed out on this device.');return {exit:true};
|
|
154
|
+
}});
|
|
155
|
+
register({name:'diff',summary:'Show uncommitted changes and new files (stat)',run:async ctx=>{
|
|
156
|
+
const root=ctx.session.meta.root;if(!git(root,['rev-parse','--git-dir'])){ctx.out('Not a git repository.');return}
|
|
157
|
+
const stat=git(root,['diff','HEAD','--stat']),fresh=git(root,['ls-files','--others','--exclude-standard']).split('\n').filter(Boolean); // untracked files count as changes
|
|
158
|
+
ctx.out([stat,fresh.length?fresh.length+' untracked file'+(fresh.length===1?'':'s')+':\n'+fresh.slice(0,50).map(f=>' '+f+' (new)').join('\n')+(fresh.length>50?'\n ...and '+(fresh.length-50)+' more':''):''].filter(Boolean).join('\n')||'No uncommitted changes.');
|
|
159
|
+
}});
|
|
160
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Distribution commands (owner 6): /update checks for a new CLI build and, for managed installs, installs it.
|
|
2
|
+
import {update,installKind} from '../distribution.mjs';
|
|
3
|
+
|
|
4
|
+
export function install(register){
|
|
5
|
+
register({name:'update',args:'[--check]',summary:'Update the ML AI Code CLI',complete:async()=>['--check'],run:async(ctx,a)=>{
|
|
6
|
+
const check=/(^|\s)--check(\s|$)/.test(String(a||''));
|
|
7
|
+
const kind=installKind();
|
|
8
|
+
ctx.ui?.status?.(check?'Checking for updates':'Checking for updates');
|
|
9
|
+
try{
|
|
10
|
+
const r=await update({check:check||kind!=='managed',server:process.env.MLAI_URL||undefined});
|
|
11
|
+
ctx.out(r.message);
|
|
12
|
+
return {update:r.status};
|
|
13
|
+
}finally{ctx.ui?.status?.(null)}
|
|
14
|
+
}});
|
|
15
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// Ecosystem commands (CLI wave 2, section 3): /import, /skills, /subagents, /memories, /presets.
|
|
2
|
+
import {existsSync} from 'node:fs';
|
|
3
|
+
import {mkdir,writeFile,appendFile,readFile,rm} from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import {CommandError} from './commands.mjs';
|
|
6
|
+
import {planImport,describePlan,applyPlan} from './import-setups.mjs';
|
|
7
|
+
import {discoverSkills,skillId} from './skills-local.mjs';
|
|
8
|
+
import {discoverAgents} from './agents-local.mjs';
|
|
9
|
+
import {memoryFiles} from './extensions-local.mjs';
|
|
10
|
+
import {extState} from './ext-state.mjs';
|
|
11
|
+
import {home} from '../home.mjs';
|
|
12
|
+
import {modeName} from '../modes.mjs';
|
|
13
|
+
|
|
14
|
+
const isObj=v=>v!==null&&typeof v==='object'&&!Array.isArray(v);
|
|
15
|
+
const rootOf=ctx=>ctx.session?.meta?.root||ctx.settings?.root||null;
|
|
16
|
+
const words=a=>String(a||'').trim().split(/\s+/).filter(Boolean);
|
|
17
|
+
const shown=(root,f)=>{const h=path.dirname(home());return root&&f.startsWith(root+path.sep)?path.relative(root,f):f.startsWith(h+path.sep)?'~/'+path.relative(h,f):f};
|
|
18
|
+
async function save(ctx,scope,key,value){
|
|
19
|
+
if(!ctx.settings?.save)throw new CommandError('Settings cannot be saved here.');
|
|
20
|
+
if(scope!=='user'&&!ctx.settings.root)scope='user';
|
|
21
|
+
ctx.settings=await ctx.settings.save(scope,key,value);return scope;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ------------------------------------------------------------------ /import
|
|
25
|
+
async function importCmd(ctx,args){
|
|
26
|
+
const w=words(args);const which=w.find(x=>['claude','cursor','all'].includes(x))||'all';
|
|
27
|
+
const dry=w.includes('--dry-run')||w.includes('preview'),yes=w.includes('--yes')||w.includes('-y');
|
|
28
|
+
const bad=w.find(x=>!['claude','cursor','all','--dry-run','preview','--yes','-y'].includes(x));
|
|
29
|
+
if(bad)throw new CommandError('Usage: /import [claude|cursor|all] [--dry-run] [--yes]');
|
|
30
|
+
const root=rootOf(ctx);const plan=planImport(root,{which});
|
|
31
|
+
ctx.out(describePlan(plan));
|
|
32
|
+
if(dry&&plan.items.length)ctx.out('Dry run: nothing changed. /import without --dry-run applies this.');
|
|
33
|
+
if(!plan.items.length||dry)return {imported:0,planned:plan.items.length};
|
|
34
|
+
if(!yes&&!await ctx.ui.confirm('Import these into your ML AI Code settings?',{default:true})){ctx.out('Nothing imported.');return {imported:0}}
|
|
35
|
+
const n=await applyPlan(plan);
|
|
36
|
+
try{const {loadSettings}=await import('../settings.mjs');ctx.settings=loadSettings({root})}catch{}
|
|
37
|
+
ctx.out('Imported '+n+' item'+(n===1?'':'s')+'. They apply from the next turn; /permissions, /hooks and /mcp show them.');
|
|
38
|
+
return {imported:n,items:plan.items.map(i=>({kind:i.kind,scope:i.scope,from:i.from}))};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ------------------------------------------------------------------ /skills
|
|
42
|
+
const disabledOf=ctx=>Array.isArray(ctx.settings?.skills?.disabled)?ctx.settings.skills.disabled:[];
|
|
43
|
+
const SKILL_TEMPLATE=(id,desc)=>`---
|
|
44
|
+
name: ${id}
|
|
45
|
+
description: ${desc||'What this skill does and when to use it.'}
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
# ${id}
|
|
49
|
+
|
|
50
|
+
Steps, conventions and examples the agent should follow when this skill applies.
|
|
51
|
+
Put longer reference material in other files in this folder; the agent can open them when needed.
|
|
52
|
+
`;
|
|
53
|
+
async function skillsCmd(ctx,args){
|
|
54
|
+
const root=rootOf(ctx);const [sub,name,...rest]=words(args);
|
|
55
|
+
const all=()=>discoverSkills(root,{pluginDirs:extState.pluginDirs,disabled:disabledOf(ctx)});
|
|
56
|
+
if(!sub||sub==='list'){
|
|
57
|
+
const s=all();
|
|
58
|
+
ctx.out(s.length?'Skills (the agent loads one when the task fits, or when you type /<id> in a task):\n'+s.map(x=>' '+(x.enabled?' ':'- ')+x.id.padEnd(24)+' '+x.scope.padEnd(8)+' '+(x.description||x.name).slice(0,70)).join('\n')+(s.some(x=>!x.enabled)?'\n (- marks a disabled skill; /skills enable <id>)':'')
|
|
59
|
+
:'No skills. /skills new <name> creates .mlai/skills/<name>/SKILL.md; folders in .claude/skills and ~/.mlai/skills work too.');
|
|
60
|
+
return {skills:s.map(x=>({id:x.id,enabled:x.enabled,scope:x.scope}))};
|
|
61
|
+
}
|
|
62
|
+
if(sub==='show'){const s=all().find(x=>x.id===name);if(!s)throw new CommandError('No skill '+name+'.');ctx.out(shown(root,s.file)+'\n\n'+s.content.slice(0,4000)+(Object.keys(s.files).length?'\n\nFiles: '+Object.keys(s.files).join(', '):''));return {id:s.id}}
|
|
63
|
+
if(sub==='enable'||sub==='disable'){
|
|
64
|
+
if(!name)throw new CommandError('Usage: /skills '+sub+' <id>');
|
|
65
|
+
if(!all().some(x=>x.id===name))throw new CommandError('No skill '+name+'.');
|
|
66
|
+
const cur=new Set(disabledOf(ctx));sub==='disable'?cur.add(name):cur.delete(name);
|
|
67
|
+
const scope=await save(ctx,'local','skills',{...(isObj(ctx.settings?.skills)?ctx.settings.skills:{}),disabled:[...cur]});
|
|
68
|
+
ctx.out('Skill '+name+' '+sub+'d ('+scope+' settings).');return {id:name,enabled:sub==='enable'};
|
|
69
|
+
}
|
|
70
|
+
if(sub==='new'||sub==='create'){
|
|
71
|
+
const id=skillId(name);if(!id)throw new CommandError('Usage: /skills new <name> [description]');
|
|
72
|
+
const user=rest.includes('--user');const desc=rest.filter(x=>x!=='--user').join(' ');
|
|
73
|
+
const dir=user||!root?path.join(home(),'skills',id):path.join(root,'.mlai','skills',id);
|
|
74
|
+
if(existsSync(path.join(dir,'SKILL.md')))throw new CommandError(shown(root,path.join(dir,'SKILL.md'))+' already exists.');
|
|
75
|
+
await mkdir(dir,{recursive:true});await writeFile(path.join(dir,'SKILL.md'),SKILL_TEMPLATE(id,desc));
|
|
76
|
+
ctx.out('Created '+shown(root,path.join(dir,'SKILL.md'))+'. Edit it; the next turn picks it up.');return {id,file:path.join(dir,'SKILL.md')};
|
|
77
|
+
}
|
|
78
|
+
throw new CommandError('Usage: /skills [list | show <id> | enable <id> | disable <id> | new <name> [description] [--user]]');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ------------------------------------------------------------------ /subagents
|
|
82
|
+
async function subagentsCmd(ctx,args){
|
|
83
|
+
const root=rootOf(ctx);const [sub,name]=words(args);
|
|
84
|
+
const all=discoverAgents(root,{flagAgents:extState.agents||[],pluginDirs:extState.pluginDirs});
|
|
85
|
+
if(sub==='show'){const a=all.find(x=>x.name===name);if(!a)throw new CommandError('No subagent '+name+'.');ctx.out(a.name+' ('+a.source+')'+(a.tools.length?'\nTools: '+a.tools.join(', '):'')+'\n\n'+a.prompt.slice(0,4000));return {name:a.name}}
|
|
86
|
+
if(sub==='new'||sub==='create'){
|
|
87
|
+
const n=String(name||'').toLowerCase().replace(/[^a-z0-9]+/g,'-').replace(/^-+|-+$/g,'');if(!n)throw new CommandError('Usage: /subagents new <name>');
|
|
88
|
+
const dir=root?path.join(root,'.mlai','agents'):path.join(home(),'agents'),f=path.join(dir,n+'.md');
|
|
89
|
+
if(existsSync(f))throw new CommandError(shown(root,f)+' already exists.');
|
|
90
|
+
await mkdir(dir,{recursive:true});
|
|
91
|
+
await writeFile(f,'---\nname: '+n+'\ndescription: When the main agent should hand work to this helper.\ntools: Read, Grep, Glob\n---\n\nYou are '+n+'. Describe how this helper works, what it checks and what its report should contain.\n');
|
|
92
|
+
ctx.out('Created '+shown(root,f)+'. Edit it; the agent can use it from the next turn.');return {name:n,file:f};
|
|
93
|
+
}
|
|
94
|
+
if(sub&&sub!=='list')throw new CommandError('Usage: /subagents [list | show <name> | new <name>]');
|
|
95
|
+
ctx.out(all.length?'Custom subagents (the agent hands them self-contained tasks; they work read-only and report back):\n'+all.map(a=>' '+a.name.padEnd(22)+' '+a.scope.padEnd(8)+' '+(a.description||'').slice(0,70)).join('\n')
|
|
96
|
+
:'No custom subagents. /subagents new <name> creates .mlai/agents/<name>.md; .claude/agents and --agents work too.');
|
|
97
|
+
return {agents:all.map(a=>a.name)};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ------------------------------------------------------------------ /memories
|
|
101
|
+
async function memoriesCmd(ctx,args){
|
|
102
|
+
const root=rootOf(ctx);const t=String(args||'').trim();const [sub]=words(t);
|
|
103
|
+
const on=ctx.settings?.memories===true;const [project,user]=[memoryFiles(root).find(m=>m.scope==='project'),memoryFiles(root).find(m=>m.scope==='user')];
|
|
104
|
+
const target=t.includes('--user')||!project?user:project;
|
|
105
|
+
if(!sub||sub==='show'||sub==='list'){
|
|
106
|
+
const L=['Memories are '+(on?'on':'off')+(on?'':' (opt-in; /memories on sends the memory files with each turn)')+'.'];
|
|
107
|
+
for(const m of memoryFiles(root)){let txt='';try{txt=(await readFile(m.file,'utf8')).trim()}catch{}L.push('',m.label+(txt?'':' (empty)'));if(txt)L.push(...txt.split('\n').slice(0,30).map(l=>' '+l))}
|
|
108
|
+
L.push('','/memories add <note> saves a note; the agent also adds one when you ask it to remember something.');
|
|
109
|
+
ctx.out(L.join('\n'));return {enabled:on};
|
|
110
|
+
}
|
|
111
|
+
if(sub==='on'||sub==='off'){
|
|
112
|
+
const scope=await save(ctx,t.includes('--user')?'user':'local','memories',sub==='on');
|
|
113
|
+
ctx.out('Memories '+sub+' ('+scope+' settings).'+(sub==='on'?' '+target.label+' goes with each turn from now on.':''));return {enabled:sub==='on'};
|
|
114
|
+
}
|
|
115
|
+
if(sub==='add'||sub==='remember'){
|
|
116
|
+
const note=t.replace(/^\S+\s*/,'').replace(/\s*--user\b/,'').trim();if(!note)throw new CommandError('Usage: /memories add <note> [--user]');
|
|
117
|
+
await mkdir(path.dirname(target.file),{recursive:true});
|
|
118
|
+
const cur=await readFile(target.file,'utf8').catch(()=>'');
|
|
119
|
+
await appendFile(target.file,(cur?(cur.endsWith('\n')?'':'\n'):'# Project memory\n\n')+'- '+note.replace(/\s+/g,' ')+'\n');
|
|
120
|
+
ctx.out('Saved to '+target.label+'.'+(on?'':' Memories are off; /memories on sends them to the agent.'));return {file:target.file};
|
|
121
|
+
}
|
|
122
|
+
if(sub==='clear'){
|
|
123
|
+
if(!existsSync(target.file)){ctx.out(target.label+' is already empty.');return}
|
|
124
|
+
if(!await ctx.ui.confirm('Delete '+target.label+'?',{default:false})){ctx.out('Kept.');return}
|
|
125
|
+
await rm(target.file,{force:true});ctx.out('Deleted '+target.label+'.');return {cleared:target.file};
|
|
126
|
+
}
|
|
127
|
+
if(sub==='path'){ctx.out(memoryFiles(root).map(m=>m.label+' '+m.file).join('\n'));return}
|
|
128
|
+
throw new CommandError('Usage: /memories [show | on | off | add <note> | clear | path] [--user]');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ------------------------------------------------------------------ /presets
|
|
132
|
+
async function presetsCmd(ctx,args){
|
|
133
|
+
const [sub,name]=words(args);const presets=isObj(ctx.settings?.presets)?ctx.settings.presets:{};
|
|
134
|
+
const chat=ctx.chat;
|
|
135
|
+
const use=async n=>{
|
|
136
|
+
const p=presets[n];if(!isObj(p))throw new CommandError('No preset '+n+'. /presets lists them.');
|
|
137
|
+
if(!chat)throw new CommandError('This command needs an interactive session.');
|
|
138
|
+
if(p.mode&&modeName(p.mode))await ctx.ui.setMode(modeName(p.mode));
|
|
139
|
+
if(p.model)await chat.setModel(p.model).catch(e=>ctx.ui.error(e.message));
|
|
140
|
+
if(p.effort)await chat.setEffort(p.effort).catch(e=>ctx.ui.error(e.message));
|
|
141
|
+
ctx.out('Preset '+n+': '+describe(p));return {preset:n};
|
|
142
|
+
};
|
|
143
|
+
if(!sub||sub==='list'){
|
|
144
|
+
const names=Object.keys(presets);
|
|
145
|
+
ctx.out(names.length?'Presets (/presets <name> applies one; mlai --preset <name> starts with it):\n'+names.map(n=>' '+n.padEnd(18)+' '+describe(presets[n])).join('\n'):'No presets. /presets save <name> saves the current mode, model and effort.');
|
|
146
|
+
return {presets:names};
|
|
147
|
+
}
|
|
148
|
+
if(sub==='save'){
|
|
149
|
+
if(!/^[\w-]{1,32}$/.test(name||''))throw new CommandError('Usage: /presets save <name> (letters, digits, - and _)');
|
|
150
|
+
if(!chat)throw new CommandError('This command needs an interactive session.');
|
|
151
|
+
const p={mode:chat.mode,...(chat.model?{model:chat.model}:{}),effort:chat.effort};
|
|
152
|
+
await save(ctx,'user','presets',{...presets,[name]:p});ctx.out('Saved preset '+name+': '+describe(p)+'.');return {preset:name,value:p};
|
|
153
|
+
}
|
|
154
|
+
if(sub==='delete'||sub==='remove'){
|
|
155
|
+
if(!presets[name])throw new CommandError('No preset '+name+'.');
|
|
156
|
+
const {[name]:_,...rest}=presets;await save(ctx,'user','presets',rest);ctx.out('Deleted preset '+name+'.');return {deleted:name};
|
|
157
|
+
}
|
|
158
|
+
if(sub==='use')return use(name);
|
|
159
|
+
return use(sub);
|
|
160
|
+
}
|
|
161
|
+
const describe=p=>[p.mode&&'mode '+p.mode,p.model&&'model '+p.model,p.effort&&'effort '+p.effort].filter(Boolean).join(', ')||'(empty)';
|
|
162
|
+
|
|
163
|
+
export function install(register){
|
|
164
|
+
register({name:'import',args:'[claude|cursor|all] [--dry-run]',summary:'Import rules, MCP servers and hooks from Claude Code or Cursor',complete:async()=>['claude','cursor','all','--dry-run'],run:importCmd});
|
|
165
|
+
register({name:'skills',args:'[list|show|enable|disable|new]',summary:'List, turn on or off, or create agent skills',complete:async()=>['list','show','enable','disable','new'],run:skillsCmd});
|
|
166
|
+
register({name:'subagents',args:'[list|show|new]',summary:'List or create custom subagents',complete:async()=>['list','show','new'],run:subagentsCmd});
|
|
167
|
+
register({name:'memories',aliases:['memory'],args:'[on|off|add <note>|clear]',summary:'Project memory the agent keeps across sessions (opt-in)',complete:async()=>['show','on','off','add','clear','path'],run:memoriesCmd});
|
|
168
|
+
register({name:'presets',aliases:['preset'],args:'[save|use|delete <name>]',summary:'Save and switch bundles of mode, model and effort',complete:async ctx=>['save','delete',...Object.keys(isObj(ctx?.settings?.presets)?ctx.settings.presets:{})],run:presetsCmd});
|
|
169
|
+
}
|