mlai-code 0.0.0-stage → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -2
- package/bin/mlai.mjs +3 -0
- package/package.json +18 -3
- package/src/browser-setup.mjs +77 -0
- package/src/cli-ext.mjs +146 -0
- package/src/cli.mjs +534 -0
- package/src/cloud.mjs +29 -0
- package/src/completion.mjs +159 -0
- package/src/credentials.mjs +54 -0
- package/src/deploy.mjs +134 -0
- package/src/distribution.mjs +373 -0
- package/src/engine-key.mjs +4 -0
- package/src/engine.mjs +167 -0
- package/src/headless.mjs +196 -0
- package/src/home.mjs +22 -0
- package/src/host.mjs +123 -0
- package/src/local-models.mjs +64 -0
- package/src/modes.mjs +38 -0
- package/src/netbox.mjs +97 -0
- package/src/netjail.py +59 -0
- package/src/offline.mjs +98 -0
- package/src/parity.mjs +86 -0
- package/src/project.mjs +68 -0
- package/src/relay.mjs +140 -0
- package/src/render.mjs +73 -0
- package/src/restricted.mjs +47 -0
- package/src/review.mjs +70 -0
- package/src/runner.mjs +127 -0
- package/src/runtime.mjs +75 -0
- package/src/sandbox.mjs +135 -0
- package/src/sdk.mjs +11 -0
- package/src/server.mjs +110 -0
- package/src/session.mjs +290 -0
- package/src/settings.mjs +135 -0
- package/src/shared-data.mjs +3 -0
- package/src/tui/acp.mjs +145 -0
- package/src/tui/agents-cli.mjs +114 -0
- package/src/tui/agents-local.mjs +65 -0
- package/src/tui/ansi.mjs +123 -0
- package/src/tui/app.mjs +710 -0
- package/src/tui/approval.mjs +88 -0
- package/src/tui/bg.mjs +172 -0
- package/src/tui/bgterm.mjs +79 -0
- package/src/tui/chat.mjs +230 -0
- package/src/tui/clipboard.mjs +74 -0
- package/src/tui/cloud-cli.mjs +57 -0
- package/src/tui/cloud-run.mjs +123 -0
- package/src/tui/cmd-agents.mjs +130 -0
- package/src/tui/cmd-auto-review.mjs +94 -0
- package/src/tui/cmd-cloud.mjs +17 -0
- package/src/tui/cmd-config.mjs +104 -0
- package/src/tui/cmd-core.mjs +160 -0
- package/src/tui/cmd-distribution.mjs +15 -0
- package/src/tui/cmd-ecosystem.mjs +169 -0
- package/src/tui/cmd-extensibility.mjs +333 -0
- package/src/tui/cmd-remote.mjs +18 -0
- package/src/tui/cmd-review.mjs +58 -0
- package/src/tui/cmd-session-extra.mjs +152 -0
- package/src/tui/cmd-tui.mjs +200 -0
- package/src/tui/commands.mjs +160 -0
- package/src/tui/custom-commands.mjs +98 -0
- package/src/tui/diff.mjs +60 -0
- package/src/tui/editor.mjs +190 -0
- package/src/tui/ext-state.mjs +16 -0
- package/src/tui/extensions-local.mjs +140 -0
- package/src/tui/files.mjs +72 -0
- package/src/tui/handoff.mjs +110 -0
- package/src/tui/import-setups.mjs +151 -0
- package/src/tui/init.mjs +104 -0
- package/src/tui/keymap.mjs +70 -0
- package/src/tui/keys.mjs +101 -0
- package/src/tui/loop.mjs +75 -0
- package/src/tui/markdown.mjs +118 -0
- package/src/tui/mcp-config.mjs +225 -0
- package/src/tui/mcp-oauth.mjs +162 -0
- package/src/tui/permissions.mjs +189 -0
- package/src/tui/plain.mjs +32 -0
- package/src/tui/plugins-local.mjs +103 -0
- package/src/tui/remote.mjs +125 -0
- package/src/tui/review-cli.mjs +332 -0
- package/src/tui/review-github.mjs +59 -0
- package/src/tui/screen.mjs +60 -0
- package/src/tui/session-ops.mjs +104 -0
- package/src/tui/skills-local.mjs +92 -0
- package/src/tui/snapshots.mjs +87 -0
- package/src/tui/statusline.mjs +98 -0
- package/src/tui/transcript.mjs +116 -0
- package/src/two-words.mjs +36 -0
- package/src/wakeups.mjs +53 -0
- package/src/winenv.mjs +94 -0
- package/src/winsandbox.py +351 -0
- package/src/workspace-api.mjs +621 -0
- package/src/worktree.mjs +128 -0
- package/vendor/client-sdk/controller.cjs +115 -0
- package/vendor/client-sdk/files.cjs +38 -0
- package/vendor/client-sdk/index.cjs +133 -0
- package/vendor/client-sdk/login.cjs +52 -0
- package/vendor/client-sdk/updates.cjs +41 -0
- package/vendor/cloud-cli/mlai.mjs +394 -0
- package/worker/architecture_parse.cjs +23 -0
- package/worker/architecture_worker.py +394 -0
- package/worker/ask_first.py +96 -0
- package/worker/attachment_reader.py +66 -0
- package/worker/background_jobs.py +111 -0
- package/worker/brief_questions.py +56 -0
- package/worker/browser_local.py +49 -0
- package/worker/browser_pane.py +89 -0
- package/worker/browser_tool.py +101 -0
- package/worker/build_planner.py +187 -0
- package/worker/chat_fork.py +277 -0
- package/worker/checkpoints.py +133 -0
- package/worker/cloud_pty.py +351 -0
- package/worker/command_policy.py +39 -0
- package/worker/command_safety.py +717 -0
- package/worker/computer_card.sh +23 -0
- package/worker/computer_setup.sh +86 -0
- package/worker/computer_use.py +221 -0
- package/worker/connector_search.py +43 -0
- package/worker/context_manager.py +60 -0
- package/worker/conversation.py +144 -0
- package/worker/deploy_pack.py +116 -0
- package/worker/design_director.py +240 -0
- package/worker/dev_jobs.py +166 -0
- package/worker/edit_guard.py +99 -0
- package/worker/env_setup.py +129 -0
- package/worker/evidence.py +109 -0
- package/worker/fast_search.py +24 -0
- package/worker/git_sync.py +333 -0
- package/worker/goals.py +156 -0
- package/worker/handover.py +98 -0
- package/worker/harness_memory.py +194 -0
- package/worker/image_generation.py +173 -0
- package/worker/intelligence.py +149 -0
- package/worker/intent_model.py +78 -0
- package/worker/intent_weights.json +1 -0
- package/worker/ios_simulator.py +87 -0
- package/worker/kimi_bridge.py +997 -0
- package/worker/kimi_compact.py +93 -0
- package/worker/kimi_gates.py +304 -0
- package/worker/kimi_hook.py +617 -0
- package/worker/lifecycle.py +95 -0
- package/worker/lsp.py +77 -0
- package/worker/mcp_local.py +293 -0
- package/worker/memory.py +99 -0
- package/worker/mlai_auto_review.py +177 -0
- package/worker/mlai_hooks.py +966 -0
- package/worker/mlai_mcp.py +441 -0
- package/worker/mlai_permissions.py +283 -0
- package/worker/model_stream.py +75 -0
- package/worker/nested_repos.py +76 -0
- package/worker/offline_guard.py +17 -0
- package/worker/preview.py +806 -0
- package/worker/proc_watch.py +124 -0
- package/worker/project_context.py +72 -0
- package/worker/project_rules.py +99 -0
- package/worker/quality.py +180 -0
- package/worker/reply_language.py +56 -0
- package/worker/repo_hygiene.py +132 -0
- package/worker/repo_memory.py +101 -0
- package/worker/repo_policy.py +269 -0
- package/worker/roles.json +3173 -0
- package/worker/roles.py +303 -0
- package/worker/runner.py +1795 -0
- package/worker/runtime_control.py +205 -0
- package/worker/secret_scan.py +168 -0
- package/worker/session_hub.py +80 -0
- package/worker/skill_library.py +129 -0
- package/worker/skill_pack.py +533 -0
- package/worker/subagents.py +91 -0
- package/worker/task_controller.py +139 -0
- package/worker/task_skills.py +159 -0
- package/worker/team_checkpoint.py +33 -0
- package/worker/token_count.py +32 -0
- package/worker/tool_catalog.py +38 -0
- package/worker/upload_storage.py +531 -0
- package/worker/verification.py +725 -0
- package/worker/vision.py +84 -0
- package/worker/wakeups.py +63 -0
- package/worker/web_tools.py +203 -0
- package/worker/winproc.py +135 -0
- package/worker/work_ledger.py +117 -0
- package/worker/worktrees.py +79 -0
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# mlai-code 0.3.1
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
ML AI Code in your terminal. The agent works in your project on this machine; models run in ML AI Code's cloud.
|
|
4
|
+
|
|
5
|
+
npm install -g mlai-code
|
|
6
|
+
mlai login
|
|
7
|
+
mlai # interactive session in the current folder
|
|
8
|
+
mlai -p "task" # one task without prompts (scripts, CI)
|
|
9
|
+
mlai cloud --help # cloud workspace commands
|
|
10
|
+
|
|
11
|
+
Update with `npm install -g mlai-code@latest` (`mlai update` tells you when one is available).
|
|
12
|
+
Shell completion: `mlai completion bash|zsh|fish|powershell`. Requires Node.js 20 or newer.
|
|
13
|
+
Docs: https://mlai-mini-code.vercel.app/docs
|
package/bin/mlai.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mlai-code",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "ML AI Code in your terminal: an agent that reads, edits and runs code in your project, on your machine.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"mlai": "bin/mlai.mjs"
|
|
8
|
+
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=20"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"bin",
|
|
14
|
+
"src",
|
|
15
|
+
"worker",
|
|
16
|
+
"vendor",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://mlai-mini-code.vercel.app/downloads",
|
|
20
|
+
"license": "UNLICENSED"
|
|
6
21
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {spawn} from 'node:child_process';
|
|
2
|
+
import {existsSync} from 'node:fs';
|
|
3
|
+
import {readFile,writeFile,rm,mkdir} from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import {sub} from './home.mjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The test browser for local runs (worker/browser_tool.py). Nothing is downloaded until the person agrees: either the
|
|
9
|
+
* agent's one-time approval card ({kind:'browser', install:true}) or Install browser in Tools. Chromium lives in
|
|
10
|
+
* ~/.mlai/runtime/pw-browsers, shared by every session; the sandbox gets read access to that folder only.
|
|
11
|
+
*/
|
|
12
|
+
export const PLAYWRIGHT='playwright==1.64.1';
|
|
13
|
+
export const BROWSER_VERSION='playwright 1.64.1';
|
|
14
|
+
export function browsersPath(){return path.join(sub('runtime'),'pw-browsers')}
|
|
15
|
+
const marker=()=>path.join(browsersPath(),'.mlai-ready');
|
|
16
|
+
/** Folders the sandbox must let the agent read for the browser to launch. */
|
|
17
|
+
export function browserReadPaths(){return [browsersPath()]}
|
|
18
|
+
|
|
19
|
+
export async function browserReady(){
|
|
20
|
+
try{return (await readFile(marker(),'utf8')).trim()===BROWSER_VERSION}catch{return false}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function step(cmd,args,env,label,{onProgress=()=>{},spawnImpl=spawn}={}){
|
|
24
|
+
onProgress(label);
|
|
25
|
+
return new Promise((resolve,reject)=>{
|
|
26
|
+
const c=spawnImpl(cmd,args,{stdio:['ignore','pipe','pipe'],env:{...process.env,...env,PIP_DISABLE_PIP_VERSION_CHECK:'1'}});
|
|
27
|
+
let err='';c.stderr?.on('data',d=>{err=(err+d).slice(-3000)});c.stdout?.on('data',()=>{});
|
|
28
|
+
c.on('error',reject);c.on('exit',code=>code===0?resolve():reject(new Error(label+' failed: '+err.trim().split('\n').slice(-3).join(' '))));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
const SMOKE='from playwright.sync_api import sync_playwright\nwith sync_playwright() as p:\n b=p.chromium.launch(headless=True,args=["--no-sandbox"]); b.close()';
|
|
32
|
+
|
|
33
|
+
let inflight=null;
|
|
34
|
+
/**
|
|
35
|
+
* Installs the Playwright package into the engine's Python and Chromium into ~/.mlai/runtime/pw-browsers, then checks
|
|
36
|
+
* that it launches. Runs outside the sandbox (it is the app doing the download the person agreed to). Single flight.
|
|
37
|
+
*/
|
|
38
|
+
export function installBrowser({python,uv=process.env.MLAI_UV,onProgress,spawnImpl,force=false}={}){
|
|
39
|
+
if(!python)return Promise.reject(new Error('The coding engine is not ready yet. Choose Prepare now first.'));
|
|
40
|
+
if(!inflight)inflight=(async()=>{
|
|
41
|
+
if(!force&&await browserReady())return {ready:true,installed:false};
|
|
42
|
+
const dir=browsersPath();await mkdir(dir,{recursive:true,mode:0o700});await rm(marker(),{force:true});
|
|
43
|
+
const env={PLAYWRIGHT_BROWSERS_PATH:dir,UV_SYSTEM_CERTS:'1',UV_NATIVE_TLS:'1'};const o={onProgress,spawnImpl};
|
|
44
|
+
if(uv&&existsSync(uv))await step(uv,['pip','install','--quiet','--python',python,PLAYWRIGHT],env,'Installing the test browser',o);
|
|
45
|
+
else await step(python,['-m','pip','install','--quiet',PLAYWRIGHT],env,'Installing the test browser',o);
|
|
46
|
+
await step(python,['-m','playwright','install','chromium'],env,'Downloading Chromium',o);
|
|
47
|
+
await step(python,['-c',SMOKE],env,'Checking the browser starts',o);
|
|
48
|
+
await writeFile(marker(),BROWSER_VERSION);
|
|
49
|
+
return {ready:true,installed:true};
|
|
50
|
+
})().finally(()=>{inflight=null});
|
|
51
|
+
return inflight;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Marks one session as browser-ready (the harness checks <state>/browser-ready) after the install. */
|
|
55
|
+
export async function installBrowserFor(dir,opts={}){
|
|
56
|
+
const r=await installBrowser(opts);
|
|
57
|
+
await writeFile(path.join(dir,'browser-ready'),BROWSER_VERSION);
|
|
58
|
+
return r;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Environment for a local run: where Chromium is, and the session's browser-ready marker when it is installed.
|
|
63
|
+
* Without an install the marker is removed, so the harness asks for consent ({kind:'browser', install:true}).
|
|
64
|
+
*/
|
|
65
|
+
export async function browserEnv(dir){
|
|
66
|
+
const ready=await browserReady();
|
|
67
|
+
if(ready)await writeFile(path.join(dir,'browser-ready'),BROWSER_VERSION);else await rm(path.join(dir,'browser-ready'),{force:true});
|
|
68
|
+
const extra={};
|
|
69
|
+
for(const fn of runEnv){try{const e=await fn(dir);if(e&&typeof e==='object')for(const [k,v] of Object.entries(e))if(/^MINI_(PANE|SIM)_[A-Z]+$/.test(k)&&typeof v==='string')extra[k]=v}catch{}}
|
|
70
|
+
return {PLAYWRIGHT_BROWSERS_PATH:browsersPath(),MINI_BROWSER_LOCAL:'1',...extra};
|
|
71
|
+
}
|
|
72
|
+
/* The Mac app's browser pane (apps/desktop/src/browser-pane.cjs) gives each local run its address. Only MINI_PANE_* and MINI_SIM_* names. */
|
|
73
|
+
const runEnv=new Set();
|
|
74
|
+
export function addRunEnv(fn){if(typeof fn==='function')runEnv.add(fn);return ()=>runEnv.delete(fn)}
|
|
75
|
+
|
|
76
|
+
/** What the install approval card says. */
|
|
77
|
+
export const INSTALL_NOTE='Install the test browser on this Mac (one-time download of about 150 MB into ~/.mlai/runtime). It opens only your local preview.';
|
package/src/cli-ext.mjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command-line parts of CLI wave 2, section 3 (extensibility). cli.mjs (the lead's) adds EXT_FLAGS to its --help
|
|
3
|
+
* list, asks extFlag() about any option it does not know, and dispatches EXT_COMMANDS before its own parsing.
|
|
4
|
+
* --mcp-config <file|json> MCP servers for this run (repeatable) --strict-mcp-config only those servers
|
|
5
|
+
* --plugin-dir <folder> load a plugin folder for this run --plugin-url <git url> fetch and load one
|
|
6
|
+
* --agents <json|file> custom subagents for this run --preset <name> start with a saved preset
|
|
7
|
+
* mlai mcp list|login|logout|add|remove|tools|enable|disable|trust mlai unarchive [id]
|
|
8
|
+
* Parsed values go to tui/ext-state.mjs, which the per-turn extension builder reads.
|
|
9
|
+
*/
|
|
10
|
+
import {readFileSync,existsSync} from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import {spawn,execFileSync} from 'node:child_process';
|
|
13
|
+
import {loadSettings} from './settings.mjs';
|
|
14
|
+
import {createInterface} from 'node:readline';
|
|
15
|
+
import {extState} from './tui/ext-state.mjs';
|
|
16
|
+
import {readPlugin,parsePluginUrl,loadPlugins} from './tui/plugins-local.mjs';
|
|
17
|
+
import {parseAgentsJson} from './tui/agents-local.mjs';
|
|
18
|
+
import {NAME} from './tui/mcp-config.mjs';
|
|
19
|
+
|
|
20
|
+
export const EXT_FLAGS=[
|
|
21
|
+
{flag:'--mcp-config',value:'<file|json>',summary:'Add MCP servers for this run from a file or a JSON string ({"mcpServers": {...}});\nrepeatable'},
|
|
22
|
+
{flag:'--strict-mcp-config',summary:'Use only the --mcp-config servers, ignoring every other MCP setting'},
|
|
23
|
+
{flag:'--plugin-dir',value:'<folder>',summary:'Load a plugin folder (skills, agents, commands, hooks, MCP servers) for this\nrun; repeatable'},
|
|
24
|
+
{flag:'--plugin-url',value:'<git url>',summary:'Fetch a plugin repository (https://host/owner/repo[#folder]) and load it for\nthis run; repeatable'},
|
|
25
|
+
{flag:'--agents',value:'<json|file>',summary:'Custom subagents for this run: {"name": {"description", "prompt", "tools"}}'},
|
|
26
|
+
{flag:'--preset',value:'<name>',summary:'Start with a saved preset of mode, model and effort (/presets)'},
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
/** {name: spec} from --mcp-config text: a path to a JSON file, or JSON itself. Throws a plain-English error. */
|
|
30
|
+
export function parseMcpConfig(text){
|
|
31
|
+
let raw=String(text||'').trim(),where='--mcp-config';
|
|
32
|
+
if(!raw.startsWith('{')){const f=path.resolve(raw);if(!existsSync(f))throw new Error('--mcp-config: no file '+f);raw=readFileSync(f,'utf8');where=f}
|
|
33
|
+
let v;try{v=JSON.parse(raw)}catch{throw new Error(where+' is not valid JSON.')}
|
|
34
|
+
const servers=v&&typeof v==='object'&&!Array.isArray(v)?(v.mcpServers&&typeof v.mcpServers==='object'?v.mcpServers:v.servers&&typeof v.servers==='object'?v.servers:v):null;
|
|
35
|
+
if(!servers)throw new Error(where+' must hold {"mcpServers": {name: {command, args} | {url}}}.');
|
|
36
|
+
for(const n of Object.keys(servers))if(!NAME.test(n)||n==='mlai')throw new Error(where+': server name '+n+' must use letters, digits, - and _ (mlai is reserved).');
|
|
37
|
+
return {id:'--mcp-config'+(where==='--mcp-config'?'':' '+path.basename(where)),servers};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Settings presets ({name: {mode, model, effort}}) applied to the parsed options when they are not set already. */
|
|
41
|
+
export function applyPreset(o,name,settings){
|
|
42
|
+
const p=settings?.presets?.[name];
|
|
43
|
+
if(!p||typeof p!=='object')throw new Error('No preset '+name+'. /presets save <name> in a session saves one.');
|
|
44
|
+
if(p.mode&&!o.mode)o.mode=p.mode;if(p.model&&!o.model)o.model=p.model;if(p.effort&&!o.effort)o.effort=p.effort;
|
|
45
|
+
extState.preset=name;return o;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const repoRoot=(cwd=process.cwd())=>{try{return execFileSync('git',['rev-parse','--show-toplevel'],{cwd,encoding:'utf8',stdio:['ignore','pipe','ignore']}).trim()||cwd}catch{return cwd}};
|
|
49
|
+
let pluginLoad=null;
|
|
50
|
+
/** Fetches --plugin-url repositories once; the first turn waits for it (tui/extensions-local.mjs). */
|
|
51
|
+
export function pluginsReady(){return pluginLoad||Promise.resolve(extState.plugins)}
|
|
52
|
+
extState.ready=pluginsReady;
|
|
53
|
+
|
|
54
|
+
/** Handles one of the extension options; returns false when `a` is not one of them. */
|
|
55
|
+
export function extFlag(o,a,next){
|
|
56
|
+
if(a==='--mcp-config'){extState.mcpConfigs.push(parseMcpConfig(next()));return true}
|
|
57
|
+
if(a==='--strict-mcp-config'){extState.strictMcp=true;return true}
|
|
58
|
+
if(a==='--plugin-dir'){const p=readPlugin(next());if(!extState.pluginDirs.includes(p.dir))extState.pluginDirs.push(p.dir);schedulePlugins();return true}
|
|
59
|
+
if(a==='--plugin-url'){const v=next();parsePluginUrl(v);extState.pluginUrls.push(v);schedulePlugins();return true}
|
|
60
|
+
if(a==='--agents'){extState.agents=[...(extState.agents||[]),...parseAgentsJson(next())];return true}
|
|
61
|
+
if(a==='--preset'){const name=next();applyPreset(o,name,loadSettings({root:repoRoot()}));o.preset=name;return true}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
function schedulePlugins(){
|
|
65
|
+
// Runs after parsing finishes (a microtask later), so every --plugin-* flag is seen.
|
|
66
|
+
pluginLoad=Promise.resolve().then(()=>loadPlugins({warn:m=>process.stderr.write(m+'\n')})).then(async r=>{
|
|
67
|
+
try{(await import('./tui/cmd-extensibility.mjs')).reloadCustomCommands()}catch{}
|
|
68
|
+
return r;
|
|
69
|
+
});
|
|
70
|
+
pluginLoad.catch(e=>process.stderr.write('Plugin not loaded: '+(e?.message||e)+'\n'));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ------------------------------------------------------------------ mlai mcp ..., mlai unarchive
|
|
74
|
+
const ask=q=>new Promise(r=>{const rl=createInterface({input:process.stdin,output:process.stderr});rl.question(q,a=>{rl.close();r(a)});rl.once('close',()=>r(null))});
|
|
75
|
+
function openBrowser(url){
|
|
76
|
+
if(process.env.MLAI_NO_BROWSER)return;
|
|
77
|
+
const cmd=process.platform==='darwin'?'open':process.platform==='win32'?'cmd':'xdg-open';
|
|
78
|
+
const args=process.platform==='win32'?['/c','start','',url]:[url];
|
|
79
|
+
try{const p=spawn(cmd,args,{stdio:'ignore',detached:true});p.on('error',()=>{});p.unref()}catch{}
|
|
80
|
+
}
|
|
81
|
+
function words(argv,valued){
|
|
82
|
+
const o={_:[]};
|
|
83
|
+
for(let i=0;i<argv.length;i++){const a=argv[i];const m=/^--([a-z-]+)(?:=(.*))?$/.exec(a);
|
|
84
|
+
if(m&&valued.includes(m[1])){o[m[1]]=m[2]??argv[++i];if(o[m[1]]==null)throw new Error('--'+m[1]+' needs a value.')}
|
|
85
|
+
else if(m)o[m[1]]=true;else o._.push(a)}
|
|
86
|
+
return o;
|
|
87
|
+
}
|
|
88
|
+
const MCP_HELP=`Usage: mlai mcp <command>
|
|
89
|
+
list MCP servers for this folder, their transport and sign-in state
|
|
90
|
+
login <name> sign in to a remote server that uses OAuth (--no-browser prints the link;
|
|
91
|
+
--client-id <id> and --scope <scopes> when the server needs them)
|
|
92
|
+
logout <name> forget the sign-in
|
|
93
|
+
add, remove, tools, enable, disable, trust the same as /mcp in a session`;
|
|
94
|
+
|
|
95
|
+
export async function mcpMain(argv,{out=t=>process.stdout.write(t),err=t=>process.stderr.write(t),cwd=process.cwd(),fetcher,open=openBrowser,prompt=ask}={}){
|
|
96
|
+
const {gitRoot}=await import('./tui/cmd-extensibility.mjs');
|
|
97
|
+
const {listServers,engineServers}=await import('./tui/mcp-config.mjs');
|
|
98
|
+
const oauth=await import('./tui/mcp-oauth.mjs');
|
|
99
|
+
const root=gitRoot(cwd);const [sub,...rest]=argv;
|
|
100
|
+
try{
|
|
101
|
+
if(!sub||sub==='help'||sub==='--help'||sub==='-h'){out(MCP_HELP+'\n');return sub?0:2}
|
|
102
|
+
if(sub==='login'){
|
|
103
|
+
const f=words(rest,['client-id','scope']);const name=f._[0];
|
|
104
|
+
if(!name)throw new Error('Usage: mlai mcp login <name> [--no-browser] [--client-id <id>] [--scope <scopes>]');
|
|
105
|
+
const s=listServers(root).find(x=>x.name===name);if(!s)throw new Error('No MCP server '+name+'. mlai mcp list shows them.');
|
|
106
|
+
if(!s.trusted)throw new Error(name+' comes from the repository and is not trusted yet; run /mcp trust in a session first.');
|
|
107
|
+
const e=engineServers(root);const url=e.mcpServers[name]?.url||s.spec.url;
|
|
108
|
+
if(!url)throw new Error(name+' is not a remote (URL) server; only those use sign-in.');
|
|
109
|
+
const rec=await oauth.login(name,{...s.spec,url},{...(fetcher?{fetcher}:{}),open,prompt,print:t=>err(t),noBrowser:!!f['no-browser'],clientId:f['client-id']||null,scope:f.scope||null});
|
|
110
|
+
out('Signed in to '+name+(rec.expires_at?' (renews automatically; the current sign-in lasts until '+new Date(rec.expires_at).toLocaleString()+')':'')+'. Sign-ins are kept in ~/.mlai/mcp-auth.json, readable only by you.\n');
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
if(sub==='logout'){
|
|
114
|
+
const name=rest[0];if(!name)throw new Error('Usage: mlai mcp logout <name>');
|
|
115
|
+
out((await oauth.logout(name,fetcher?{fetcher}:{}))?'Signed out of '+name+'.\n':'Not signed in to '+name+'.\n');return 0;
|
|
116
|
+
}
|
|
117
|
+
if(sub==='list'||sub==='ls'){
|
|
118
|
+
const all=listServers(root);
|
|
119
|
+
if(!all.length){out('No MCP servers. mlai mcp add <name> <command> (or --url <url>) adds one.\n');return 0}
|
|
120
|
+
for(const s of all)out(' '+s.name.padEnd(20)+' '+s.transport.padEnd(6)+' '+(s.problem?'problem: '+s.problem:!s.enabled?'disabled':!s.trusted?'not trusted (/mcp trust)':s.auth||(s.transport==='stdio'?'':'no sign-in'))+' '+s.source+'\n');
|
|
121
|
+
return 0;
|
|
122
|
+
}
|
|
123
|
+
// The rest is the session command, run with a plain terminal UI.
|
|
124
|
+
const {loadCommands,find}=await import('./tui/commands.mjs');const {plainUi}=await import('./tui/plain.mjs');const {loadSettings}=await import('./settings.mjs');
|
|
125
|
+
await loadCommands();const cmd=find('mcp');
|
|
126
|
+
const ui=plainUi({write:out,writeErr:err,ask:prompt});
|
|
127
|
+
const ctx={session:null,settings:loadSettings({root}),ui,out:t=>ui.print(t),server:null,exec:null};
|
|
128
|
+
await cmd.run(ctx,[sub,...rest].map(w=>/\s/.test(w)?JSON.stringify(w):w).join(' '));
|
|
129
|
+
return 0;
|
|
130
|
+
}catch(e){err('error: '+(e?.message||e)+'\n');return /^Usage/.test(e?.message||'')?2:1}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function unarchiveMain(argv,{out=t=>process.stdout.write(t),err=t=>process.stderr.write(t)}={}){
|
|
134
|
+
const ops=await import('./tui/session-ops.mjs');
|
|
135
|
+
try{
|
|
136
|
+
if(!argv[0]||argv[0]==='--list'||argv[0]==='list'){
|
|
137
|
+
const all=await ops.listArchived();
|
|
138
|
+
out(all.length?'Archived sessions (mlai unarchive <id> brings one back):\n'+all.map(m=>' '+m.id.slice(0,8)+' '+String(m.title||'Untitled').slice(0,40).padEnd(40)+' '+(m.folder||'')+' '+String(m.archived||'').slice(0,10)).join('\n')+'\n':'No archived sessions.\n');
|
|
139
|
+
return 0;
|
|
140
|
+
}
|
|
141
|
+
const m=await ops.unarchiveSession(argv[0]);
|
|
142
|
+
out('Restored '+(m?.title||'the session')+' ('+String(m?.id||argv[0]).slice(0,8)+'). Resume it with mlai -r '+String(m?.id||argv[0]).slice(0,8)+'.\n');return 0;
|
|
143
|
+
}catch(e){err('error: '+(e?.message||e)+'\n');return 1}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export const EXT_COMMANDS={mcp:mcpMain,unarchive:unarchiveMain};
|