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,114 @@
|
|
|
1
|
+
// `mlai agents` and `mlai attach`: the terminal side of background agents (src/tui/bg.mjs).
|
|
2
|
+
// mlai agents live list (in a terminal): arrows select, Enter attaches, s stops, l shows logs, q quits
|
|
3
|
+
// mlai agents --json one JSON array, then exit (also `mlai agents list`)
|
|
4
|
+
// mlai agents attach <id> same as mlai attach <id>
|
|
5
|
+
// mlai agents stop <id> stop a background agent
|
|
6
|
+
// mlai agents logs <id> [-f] its log, rendered; -f keeps following
|
|
7
|
+
import readline from 'node:readline';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import {listAgents,agentLine,attachSession,stopAgent,renderLog,readFrom,stillRunning,agentInfo} from './bg.mjs';
|
|
10
|
+
|
|
11
|
+
const USAGE=`Usage
|
|
12
|
+
mlai agents Live list of background agents and sessions running in other terminals
|
|
13
|
+
mlai agents --json The list as JSON (also: mlai agents list)
|
|
14
|
+
mlai attach <id> Follow one, answer its approvals, steer it; Ctrl-C detaches (it keeps working)
|
|
15
|
+
mlai agents stop <id> Stop one
|
|
16
|
+
mlai agents logs <id> [-f] Show its log; -f keeps following
|
|
17
|
+
Start one with: mlai --bg "task". Continue a finished one with: mlai -r <id>.`;
|
|
18
|
+
|
|
19
|
+
export function parseAgentsArgs(argv){
|
|
20
|
+
const o={sub:null,id:null,json:false,follow:false,verbose:false,server:null,help:false};
|
|
21
|
+
const rest=[];
|
|
22
|
+
for(let i=0;i<argv.length;i++){const a=argv[i];
|
|
23
|
+
if(a==='--json')o.json=true;else if(a==='-f'||a==='--follow')o.follow=true;else if(a==='-v'||a==='--verbose')o.verbose=true;
|
|
24
|
+
else if(a==='-h'||a==='--help')o.help=true;else if(a==='--server')o.server=argv[++i];else if(a.startsWith('--server='))o.server=a.slice(9);
|
|
25
|
+
else if(a.startsWith('-'))throw Object.assign(new Error('Unknown option '+a+'. Run mlai agents --help.'),{usage:true});else rest.push(a)}
|
|
26
|
+
o.sub=rest[0]||null;o.id=rest[1]||null;
|
|
27
|
+
if(o.sub&&!['list','attach','stop','logs'].includes(o.sub)){o.id=o.sub;o.sub='attach'}
|
|
28
|
+
return o;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** deps: {Session, sessionsDir, findSessions, render:{renderEvent,approvalView,summaryLine}, c, out, err, stdin} */
|
|
32
|
+
export async function resolveAgent(ref,{Session,findSessions,sessionsDir}){
|
|
33
|
+
if(!ref)throw Object.assign(new Error('Which one? mlai agents lists them.'),{usage:true});
|
|
34
|
+
let found=await findSessions(ref);
|
|
35
|
+
if(found.length>1){const live=[];for(const m of found)if(await agentInfo(m,path.join(sessionsDir,m.id)))live.push(m);if(live.length)found=live}
|
|
36
|
+
if(!found.length)throw Object.assign(new Error('No session '+ref+'. mlai agents lists them.'),{usage:true});
|
|
37
|
+
if(found.length>1)throw Object.assign(new Error('More than one session matches '+ref+': '+found.slice(0,5).map(m=>m.id.slice(0,8)).join(', ')+'. Use more of the id.'),{usage:true});
|
|
38
|
+
return Session.open(found[0].id);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function agentsMain(argv,deps){
|
|
42
|
+
const o=parseAgentsArgs(argv);const {out,c}=deps;
|
|
43
|
+
if(o.help){out(USAGE);return 0}
|
|
44
|
+
if(o.sub==='attach')return attachMain([o.id],deps);
|
|
45
|
+
if(o.sub==='stop'){const s=await resolveAgent(o.id,deps);await stopAgent(s);out('Stopped '+s.meta.id.slice(0,8)+'.');return 0}
|
|
46
|
+
if(o.sub==='logs')return logs(await resolveAgent(o.id,deps),o,deps);
|
|
47
|
+
const list=()=>listAgents({Session:deps.Session,sessionsDir:deps.sessionsDir});
|
|
48
|
+
if(o.json||o.sub==='list'||!deps.stdin?.isTTY||!deps.stdout?.isTTY){
|
|
49
|
+
const all=await list();
|
|
50
|
+
if(o.json){out(JSON.stringify(all));return 0}
|
|
51
|
+
out(all.length?all.map(a=>agentLine(a,c)).join('\n'):'No background agents. Start one with mlai --bg "task".');return 0;
|
|
52
|
+
}
|
|
53
|
+
return live(list,deps);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function logs(s,o,{out,c,render,sleep=ms=>new Promise(r=>setTimeout(r,ms)),signal}){
|
|
57
|
+
const file=path.join(s.dir,'bg.log');let {text,offset}=await readFrom(file,0);
|
|
58
|
+
if(!text&&!o.follow){out('No background log for '+s.meta.id.slice(0,8)+'.');return 0}
|
|
59
|
+
const show=t=>{for(const l of renderLog(t,render.renderEvent,{c,verbose:o.verbose}))out(l)};show(text);
|
|
60
|
+
if(!o.follow)return 0;
|
|
61
|
+
let stop=false;const onInt=()=>{stop=true};process.once('SIGINT',onInt);
|
|
62
|
+
try{while(!stop&&!signal?.aborted){await sleep(500);const r=await readFrom(file,offset);offset=r.offset;if(r.text)show(r.text);if(!r.text&&!await stillRunning(s.dir))break}}
|
|
63
|
+
finally{process.off('SIGINT',onInt)}
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function live(list,{out,c,stdin,stdout,Session,sessionsDir,findSessions,render}){
|
|
68
|
+
let rows=await list(),sel=0,closed=false,timer=null,message='';
|
|
69
|
+
const draw=()=>{
|
|
70
|
+
const w=Math.max(40,stdout.columns||100);
|
|
71
|
+
const lines=[c.bold('Agents')+c.dim(' \u2191\u2193 select \u00b7 Enter attach \u00b7 s stop \u00b7 l logs \u00b7 q quit'),''];
|
|
72
|
+
if(!rows.length)lines.push(c.dim('No background agents. Start one with mlai --bg "task".'));
|
|
73
|
+
rows.forEach((a,i)=>{lines.push((i===sel?c.cyan('\u203a '):' ')+agentLine(a,c));if(a.waiting)lines.push(' '+c.yellow('waiting: '+a.waiting.command))});
|
|
74
|
+
if(message)lines.push('',c.dim(message));
|
|
75
|
+
stdout.write('\x1b[H\x1b[2J'+lines.map(l=>l.slice(0,w*4)).join('\n')+'\n');
|
|
76
|
+
};
|
|
77
|
+
return await new Promise(resolve=>{
|
|
78
|
+
readline.emitKeypressEvents(stdin);const raw=stdin.isRaw;stdin.setRawMode?.(true);stdin.resume();
|
|
79
|
+
const finish=async(next)=>{if(closed)return;closed=true;clearInterval(timer);stdin.off('keypress',onKey);stdin.setRawMode?.(raw);stdin.pause();stdout.write('\x1b[H\x1b[2J');resolve(next?await next():0)};
|
|
80
|
+
const onKey=async(_s,k)=>{
|
|
81
|
+
if(!k)return;const a=rows[sel];
|
|
82
|
+
if(k.name==='q'||k.name==='escape'||(k.ctrl&&k.name==='c'))return finish();
|
|
83
|
+
if(k.name==='up'){sel=Math.max(0,sel-1);draw()}else if(k.name==='down'){sel=Math.min(rows.length-1,sel+1);draw()}
|
|
84
|
+
else if(k.name==='return'&&a)return finish(()=>attachMain([a.id],{out,c,stdin,stdout,Session,sessionsDir,findSessions,render}));
|
|
85
|
+
else if(k.name==='l'&&a)return finish(async()=>logs(await Session.open(a.id),{follow:false},{out,c,render}));
|
|
86
|
+
else if(k.name==='s'&&a&&(a.state==='running'||a.state==='waiting')){message='Stopping '+a.id.slice(0,8)+'...';draw();await stopAgent(await Session.open(a.id)).catch(e=>{message=e.message});message='Stopped '+a.id.slice(0,8)+'.';rows=await list();draw()}
|
|
87
|
+
};
|
|
88
|
+
stdin.on('keypress',onKey);draw();
|
|
89
|
+
timer=setInterval(async()=>{rows=await list().catch(()=>rows);sel=Math.min(sel,Math.max(0,rows.length-1));if(!closed)draw()},1000);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** `mlai attach <id>`: follow a running session (background or another terminal). Exit code from its result. */
|
|
94
|
+
export async function attachMain(argv,{out,c,stdin=process.stdin,Session,sessionsDir,findSessions,render}){
|
|
95
|
+
const ref=argv.find(a=>a&&!a.startsWith('-'));
|
|
96
|
+
const s=await resolveAgent(ref,{Session,findSessions,sessionsDir});
|
|
97
|
+
const running=await stillRunning(s.dir);
|
|
98
|
+
out(c.bold(s.meta.title||'Session')+c.dim(' \u00b7 '+s.meta.id.slice(0,8)+' \u00b7 '+s.meta.folder+(running?' \u00b7 working':' \u00b7 finished')));
|
|
99
|
+
out(c.dim(running?'Type to steer. y / a / n answer approvals. /stop stops it. Ctrl-C detaches; it keeps working.':'Continue it with: mlai -r '+s.meta.id.slice(0,8)));
|
|
100
|
+
const a=attachSession(s,{write:out,approvalView:render.approvalView,renderEvent:render.renderEvent,c,isRunning:()=>stillRunning(s.dir)});
|
|
101
|
+
let rl=null;
|
|
102
|
+
if(stdin?.isTTY&&running){
|
|
103
|
+
rl=readline.createInterface({input:stdin,output:process.stdout,terminal:true});
|
|
104
|
+
rl.on('line',l=>{a.handleLine(l).catch(e=>out(c.red(e.message)))});
|
|
105
|
+
rl.on('SIGINT',()=>a.detach());rl.on('close',()=>a.detach());
|
|
106
|
+
}
|
|
107
|
+
const r=await a.done;rl?.close();
|
|
108
|
+
if(r.detached){out(c.dim('Detached. It keeps working: mlai attach '+s.meta.id.slice(0,8)));return 0}
|
|
109
|
+
const status=await import('node:fs/promises').then(f=>f.readFile(path.join(s.dir,'status.json'),'utf8')).then(JSON.parse).catch(()=>({state:r.state}));
|
|
110
|
+
out(render.summaryLine?.(status,c)||('Finished: '+r.state));
|
|
111
|
+
out(c.dim('Continue it with: mlai -r '+s.meta.id.slice(0,8)));
|
|
112
|
+
return render.exitCode?render.exitCode(status):(r.state==='completed'?0:1);
|
|
113
|
+
}
|
|
114
|
+
export {USAGE};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom subagents: named helpers with their own instructions that the main agent can hand a self-contained task to.
|
|
3
|
+
* --agents '{"reviewer": {"description": "...", "prompt": "...", "tools": ["Read", "Grep"]}}' (this run only)
|
|
4
|
+
* <repo>/.mlai/agents/<name>.md (also .claude/agents) ~/.mlai/agents/<name>.md (also ~/.claude/agents)
|
|
5
|
+
* <plugin>/agents/<name>.md (--plugin-dir, --plugin-url)
|
|
6
|
+
* A Markdown agent has frontmatter name, description and tools (a comma list); the body is its instructions.
|
|
7
|
+
* Earlier sources win by name: --agents, then project, plugins, user. They travel as request.extensions.agents and the
|
|
8
|
+
* engine runs them as bounded read-only helpers (worker/subagents.py custom_agent_task); a helper that lists edit or
|
|
9
|
+
* shell tools still runs read-only and reports what to change, which the main agent then applies.
|
|
10
|
+
*/
|
|
11
|
+
import {readdirSync,readFileSync,lstatSync} from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import {home} from '../home.mjs';
|
|
14
|
+
import {parseFrontmatter} from './skills-local.mjs';
|
|
15
|
+
|
|
16
|
+
const MAX_AGENTS=20,MAX_PROMPT=20000;
|
|
17
|
+
export const agentName=s=>String(s||'').toLowerCase().replace(/[^a-z0-9]+/g,'-').replace(/^-+|-+$/g,'').slice(0,40);
|
|
18
|
+
const tools=v=>(Array.isArray(v)?v:typeof v==='string'?v.split(','):[]).map(x=>String(x).trim()).filter(x=>/^[\w*()_:.-]{1,60}$/.test(x)).slice(0,30);
|
|
19
|
+
const WRITE=/^(edit|write|multiedit|notebookedit|bash|shell)$/i;
|
|
20
|
+
|
|
21
|
+
export function normalizeAgent(name,raw,source){
|
|
22
|
+
if(!raw||typeof raw!=='object')return null;
|
|
23
|
+
const n=agentName(raw.name||name);const prompt=String(raw.prompt||raw.instructions||raw.systemPrompt||'').trim();
|
|
24
|
+
if(!n||!prompt)return null;
|
|
25
|
+
const t=tools(raw.tools);
|
|
26
|
+
return {name:n,description:String(raw.description||'').replace(/\s+/g,' ').trim().slice(0,300),prompt:prompt.slice(0,MAX_PROMPT),tools:t,
|
|
27
|
+
readOnly:!t.length||!t.some(x=>WRITE.test(x.replace(/\(.*$/,''))),source};
|
|
28
|
+
}
|
|
29
|
+
/** --agents value: JSON text or a path to a JSON file. Throws a plain-English error. */
|
|
30
|
+
export function parseAgentsJson(text){
|
|
31
|
+
let raw=String(text||'').trim();
|
|
32
|
+
if(!raw.startsWith('{')&&!raw.startsWith('[')){try{raw=readFileSync(path.resolve(raw),'utf8')}catch{throw new Error('--agents takes JSON like {"name": {"description": "...", "prompt": "..."}} or a path to such a file.')}}
|
|
33
|
+
let v;try{v=JSON.parse(raw)}catch{throw new Error('--agents is not valid JSON.')}
|
|
34
|
+
const entries=Array.isArray(v)?v.map(a=>[a?.name,a]):v&&typeof v==='object'?Object.entries(v):[];
|
|
35
|
+
const out=[];for(const [k,a] of entries){const x=normalizeAgent(k,a,'--agents');if(!x)throw new Error('--agents: '+(k||'an agent')+' needs a name and a prompt.');out.push(x)}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
export function readAgentFile(file,source){
|
|
39
|
+
try{const st=lstatSync(file);if(!st.isFile()||st.isSymbolicLink()||st.size>MAX_PROMPT*2)return null}catch{return null}
|
|
40
|
+
const {meta,body}=parseFrontmatter(readFileSync(file,'utf8'));
|
|
41
|
+
return normalizeAgent(meta.name||path.basename(file,'.md'),{...meta,prompt:body},source);
|
|
42
|
+
}
|
|
43
|
+
export function agentDirs(root,{pluginDirs=[],homeDir=null}={}){
|
|
44
|
+
const h=homeDir||path.dirname(home());
|
|
45
|
+
return [
|
|
46
|
+
...(root?[{dir:path.join(root,'.mlai','agents'),scope:'project'},{dir:path.join(root,'.claude','agents'),scope:'project'}]:[]),
|
|
47
|
+
...pluginDirs.map(d=>({dir:path.join(d,'agents'),scope:'plugin'})),
|
|
48
|
+
{dir:path.join(home(),'agents'),scope:'user'},{dir:path.join(h,'.claude','agents'),scope:'user'},
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
/** [{name, description, prompt, tools, readOnly, source, scope}] */
|
|
52
|
+
export function discoverAgents(root,{flagAgents=[],pluginDirs=[],homeDir=null}={}){
|
|
53
|
+
const seen=new Map();
|
|
54
|
+
for(const a of flagAgents||[])if(!seen.has(a.name))seen.set(a.name,{...a,scope:'flag'});
|
|
55
|
+
for(const {dir,scope} of agentDirs(root,{pluginDirs,homeDir})){
|
|
56
|
+
let names=[];try{names=readdirSync(dir).sort()}catch{continue}
|
|
57
|
+
for(const n of names){
|
|
58
|
+
if(!n.endsWith('.md')||n.startsWith('.'))continue;
|
|
59
|
+
const a=readAgentFile(path.join(dir,n),path.relative(root||dir,path.join(dir,n))||n);
|
|
60
|
+
if(a&&!seen.has(a.name))seen.set(a.name,{...a,scope});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return [...seen.values()].slice(0,MAX_AGENTS);
|
|
64
|
+
}
|
|
65
|
+
export const engineAgents=agents=>agents.map(({name,description,prompt,tools,readOnly})=>({name,description,prompt,tools,readOnly}));
|
package/src/tui/ansi.mjs
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal primitives for the TUI: themes, display width, wrapping and truncation. Dependency-free.
|
|
3
|
+
* Colors use the basic 16-color and 256-color sets so they follow the terminal's own palette. NO_COLOR
|
|
4
|
+
* (https://no-color.org) and a non-color terminal keep layout and drop every color and style.
|
|
5
|
+
*/
|
|
6
|
+
export const CSI='\x1b[';
|
|
7
|
+
const SGR=/\x1b\[[0-9;:?]*[ -\/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b[@-Z\\-_]/g;
|
|
8
|
+
export const strip=s=>String(s??'').replace(SGR,'');
|
|
9
|
+
|
|
10
|
+
/** Zero-width code points: combining marks, variation selectors, zero-width joiners. */
|
|
11
|
+
const ZERO=/[\u0300-\u036f\u0483-\u0489\u0591-\u05bd\u0610-\u061a\u064b-\u065f\u0e31\u0e34-\u0e3a\u1ab0-\u1aff\u1dc0-\u1dff\u200b-\u200f\u20d0-\u20ff\ufe00-\ufe0f\ufe20-\ufe2f]/u;
|
|
12
|
+
function wide(cp){
|
|
13
|
+
return cp>=0x1100&&(cp<=0x115f||cp===0x2329||cp===0x232a||(cp>=0x2e80&&cp<=0x303e)||(cp>=0x3041&&cp<=0x33ff)||(cp>=0x3400&&cp<=0x4dbf)||(cp>=0x4e00&&cp<=0x9fff)||(cp>=0xa000&&cp<=0xa4cf)||(cp>=0xac00&&cp<=0xd7a3)||(cp>=0xf900&&cp<=0xfaff)||(cp>=0xfe30&&cp<=0xfe4f)||(cp>=0xff00&&cp<=0xff60)||(cp>=0xffe0&&cp<=0xffe6)||(cp>=0x1f300&&cp<=0x1f64f)||(cp>=0x1f900&&cp<=0x1f9ff)||(cp>=0x20000&&cp<=0x3fffd));
|
|
14
|
+
}
|
|
15
|
+
/** Columns one character takes. */
|
|
16
|
+
export function charWidth(ch){
|
|
17
|
+
const cp=ch.codePointAt(0);
|
|
18
|
+
if(cp===undefined||cp<32||(cp>=0x7f&&cp<0xa0))return 0;
|
|
19
|
+
if(ZERO.test(ch)||cp===0x200d)return 0;
|
|
20
|
+
return wide(cp)?2:1;
|
|
21
|
+
}
|
|
22
|
+
/** Columns a string takes on screen (escape codes ignored). */
|
|
23
|
+
export function width(s){let w=0;for(const ch of strip(s))w+=charWidth(ch);return w}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Splits a styled string into chunks of at most `cols` columns, keeping escape codes with their text and
|
|
27
|
+
* re-opening the active style on the next line. Breaks at spaces when it can.
|
|
28
|
+
*/
|
|
29
|
+
export function wrap(text,cols){
|
|
30
|
+
cols=Math.max(4,cols|0);
|
|
31
|
+
const out=[];
|
|
32
|
+
for(const line of String(text??'').split('\n')){
|
|
33
|
+
if(width(line)<=cols){out.push(line);continue}
|
|
34
|
+
let cur='',w=0,active='',lastSpace=-1,lastSpaceW=0;
|
|
35
|
+
const tokens=line.match(new RegExp(SGR.source+'|[\\s\\S]','gu'))||[];
|
|
36
|
+
for(const t of tokens){
|
|
37
|
+
if(t.startsWith('\x1b')){cur+=t;active=/\x1b\[0?m/.test(t)?'':active+t;continue}
|
|
38
|
+
const cw=charWidth(t);
|
|
39
|
+
if(w+cw>cols){
|
|
40
|
+
if(/\s/.test(t)){out.push(cur+(active?'\x1b[0m':''));cur=active;w=0;lastSpace=-1;continue}
|
|
41
|
+
if(lastSpace>0&&lastSpaceW>cols/3){out.push(cur.slice(0,lastSpace)+(active?'\x1b[0m':''));cur=active+cur.slice(lastSpace+1);w=width(cur)}
|
|
42
|
+
else{out.push(cur+(active?'\x1b[0m':''));cur=active;w=0}
|
|
43
|
+
lastSpace=-1;
|
|
44
|
+
}
|
|
45
|
+
if(t===' '){lastSpace=cur.length;lastSpaceW=w}
|
|
46
|
+
cur+=t;w+=cw;
|
|
47
|
+
}
|
|
48
|
+
out.push(cur);
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
/** Cuts a styled string to `cols` columns, adding an ellipsis when it was longer. */
|
|
53
|
+
export function truncate(s,cols,ellipsis='…'){
|
|
54
|
+
s=String(s??'');if(width(s)<=cols)return s;
|
|
55
|
+
let out='',w=0;const target=Math.max(0,cols-width(ellipsis));
|
|
56
|
+
for(const t of s.match(new RegExp(SGR.source+'|[\\s\\S]','gu'))||[]){
|
|
57
|
+
if(t.startsWith('\x1b')){out+=t;continue}
|
|
58
|
+
const cw=charWidth(t);if(w+cw>target)break;out+=t;w+=cw;
|
|
59
|
+
}
|
|
60
|
+
return out+ellipsis+(/\x1b\[/.test(out)?'\x1b[0m':'');
|
|
61
|
+
}
|
|
62
|
+
export const pad=(s,cols)=>s+' '.repeat(Math.max(0,cols-width(s)));
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Themes: role -> SGR parameters. Dark and light differ in the dim, accent and diff colors so both stay readable.
|
|
66
|
+
* A theme with color:false returns text unchanged for every role (NO_COLOR, TERM=dumb, pipes).
|
|
67
|
+
*/
|
|
68
|
+
const THEMES={
|
|
69
|
+
dark:{text:'39',dim:'90',faint:'2',bold:'1',italic:'3',under:'4',accent:'36',accent2:'35',ok:'32',warn:'33',bad:'31',
|
|
70
|
+
add:'32',del:'31',addBg:'48;5;22',delBg:'48;5;52',hunk:'36',border:'90',borderFocus:'36',borderShell:'35',borderPlan:'34',
|
|
71
|
+
code:'38;5;180',keyword:'38;5;176',string:'38;5;114',number:'38;5;209',comment:'38;5;245',fn:'38;5;117',head:'1;36',quote:'90',sel:'7',tool:'38;5;110'},
|
|
72
|
+
light:{text:'39',dim:'38;5;242',faint:'38;5;246',bold:'1',italic:'3',under:'4',accent:'38;5;25',accent2:'38;5;91',ok:'38;5;28',warn:'38;5;130',bad:'38;5;160',
|
|
73
|
+
add:'38;5;28',del:'38;5;160',addBg:'48;5;194',delBg:'48;5;224',hunk:'38;5;25',border:'38;5;248',borderFocus:'38;5;25',borderShell:'38;5;91',borderPlan:'38;5;26',
|
|
74
|
+
code:'38;5;94',keyword:'38;5;90',string:'38;5;28',number:'38;5;166',comment:'38;5;244',fn:'38;5;25',head:'1;38;5;25',quote:'38;5;242',sel:'7',tool:'38;5;24'},
|
|
75
|
+
};
|
|
76
|
+
export const THEME_NAMES=['dark','light'];
|
|
77
|
+
/**
|
|
78
|
+
* Syntax themes recolor fenced code only (code, keyword, string, number, comment, function names). 'default' keeps
|
|
79
|
+
* the interface theme's own colors; 'mono' turns highlighting off but keeps code distinct. Per background so each
|
|
80
|
+
* stays readable on dark and light terminals.
|
|
81
|
+
*/
|
|
82
|
+
export const SYNTAX={
|
|
83
|
+
github:{dark:{code:'38;5;252',keyword:'38;5;204',string:'38;5;153',number:'38;5;117',comment:'38;5;244',fn:'38;5;183'},
|
|
84
|
+
light:{code:'38;5;235',keyword:'38;5;161',string:'38;5;24',number:'38;5;25',comment:'38;5;245',fn:'38;5;92'}},
|
|
85
|
+
monokai:{dark:{code:'38;5;231',keyword:'38;5;197',string:'38;5;186',number:'38;5;141',comment:'38;5;242',fn:'38;5;148'},
|
|
86
|
+
light:{code:'38;5;236',keyword:'38;5;161',string:'38;5;136',number:'38;5;98',comment:'38;5;246',fn:'38;5;64'}},
|
|
87
|
+
solarized:{dark:{code:'38;5;246',keyword:'38;5;64',string:'38;5;37',number:'38;5;125',comment:'38;5;240',fn:'38;5;33'},
|
|
88
|
+
light:{code:'38;5;241',keyword:'38;5;64',string:'38;5;37',number:'38;5;125',comment:'38;5;245',fn:'38;5;33'}},
|
|
89
|
+
nord:{dark:{code:'38;5;253',keyword:'38;5;110',string:'38;5;150',number:'38;5;139',comment:'38;5;60',fn:'38;5;116'},
|
|
90
|
+
light:{code:'38;5;238',keyword:'38;5;67',string:'38;5;65',number:'38;5;96',comment:'38;5;246',fn:'38;5;30'}},
|
|
91
|
+
mono:{dark:{code:'38;5;250',keyword:'1',string:'38;5;250',number:'38;5;250',comment:'2',fn:'38;5;250'},
|
|
92
|
+
light:{code:'38;5;238',keyword:'1',string:'38;5;238',number:'38;5;238',comment:'2',fn:'38;5;238'}},
|
|
93
|
+
};
|
|
94
|
+
export const SYNTAX_NAMES=['default',...Object.keys(SYNTAX)];
|
|
95
|
+
/** The theme for a settings value: 'auto' reads COLORFGBG (set by many terminals) and falls back to dark. */
|
|
96
|
+
export function resolveTheme(name,env=process.env){
|
|
97
|
+
if(name==='dark'||name==='light')return name;
|
|
98
|
+
const bg=Number(String(env.COLORFGBG||'').split(';').pop());
|
|
99
|
+
return Number.isFinite(bg)&&(bg===7||bg===15||(bg>=9&&bg!==8&&bg<16&&bg!==0))?'light':'dark';
|
|
100
|
+
}
|
|
101
|
+
export function theme(name='dark',{color=true,syntax='default'}={}){
|
|
102
|
+
const base=THEMES[name]?name:'dark';
|
|
103
|
+
const roles={...THEMES[base],...(SYNTAX[syntax]?.[base]||{})};
|
|
104
|
+
const t={name:base,color,syntax:SYNTAX[syntax]?syntax:'default'};
|
|
105
|
+
// Nested styles: an inner reset re-opens the outer style, so bold inside a colored row keeps the row's color.
|
|
106
|
+
for(const [k,v] of Object.entries(roles)){const open=CSI+v+'m';t[k]=color?s=>open+String(s).split(CSI+'0m').join(CSI+'0m'+open)+CSI+'0m':s=>String(s)}
|
|
107
|
+
t.sgr=k=>color&&roles[k]?CSI+roles[k]+'m':'';
|
|
108
|
+
t.reset=color?CSI+'0m':'';
|
|
109
|
+
return t;
|
|
110
|
+
}
|
|
111
|
+
/** Whether colors are allowed for this stream and environment. */
|
|
112
|
+
export function colorAllowed(stream=process.stdout,env=process.env){
|
|
113
|
+
if('NO_COLOR' in env&&env.NO_COLOR!=='')return false;
|
|
114
|
+
if(env.FORCE_COLOR&&env.FORCE_COLOR!=='0')return true;
|
|
115
|
+
if(env.TERM==='dumb')return false;
|
|
116
|
+
return Boolean(stream?.isTTY);
|
|
117
|
+
}
|
|
118
|
+
/** Box characters; legacy Windows consoles without a modern terminal get ASCII. */
|
|
119
|
+
export function boxChars(env=process.env,platform=process.platform){
|
|
120
|
+
const ascii=env.MLAI_ASCII==='1'||(platform==='win32'&&!env.WT_SESSION&&!env.TERM_PROGRAM&&!env.ConEmuANSI&&env.TERM!=='xterm-256color');
|
|
121
|
+
return ascii?{tl:'+',tr:'+',bl:'+',br:'+',h:'-',v:'|',dot:'*',bar:'|',arrow:'>',ell:'...',check:'x',ascii:true}
|
|
122
|
+
:{tl:'╭',tr:'╮',bl:'╰',br:'╯',h:'─',v:'│',dot:'●',bar:'▌',arrow:'›',ell:'…',check:'✓',ascii:false};
|
|
123
|
+
}
|