mlai-code 0.3.3 → 0.3.5

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.
Files changed (136) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/checkpoints.mjs +217 -0
  4. package/src/cli.mjs +153 -26
  5. package/src/cloud.mjs +2 -0
  6. package/src/engine.mjs +1 -1
  7. package/src/headless.mjs +39 -11
  8. package/src/home.mjs +3 -2
  9. package/src/host.mjs +141 -13
  10. package/src/ide-quick.mjs +147 -0
  11. package/src/ide.mjs +182 -0
  12. package/src/netbox.mjs +1 -1
  13. package/src/parity.mjs +17 -0
  14. package/src/relay.mjs +1 -1
  15. package/src/render.mjs +16 -5
  16. package/src/runtime.mjs +2 -0
  17. package/src/server.mjs +46 -7
  18. package/src/session.mjs +31 -8
  19. package/src/tui/acp.mjs +121 -19
  20. package/src/tui/app.mjs +2 -1
  21. package/src/tui/chat.mjs +35 -10
  22. package/src/tui/cmd-config.mjs +3 -1
  23. package/src/tui/cmd-core.mjs +9 -5
  24. package/src/tui/cmd-extensibility.mjs +5 -0
  25. package/src/tui/extensions-local.mjs +2 -0
  26. package/src/tui/mcp-config.mjs +22 -4
  27. package/src/tui/review-cli.mjs +15 -4
  28. package/src/tui/snapshots.mjs +57 -16
  29. package/src/tui/transcript.mjs +13 -2
  30. package/src/two-words.mjs +14 -8
  31. package/src/view-sync.mjs +48 -0
  32. package/src/winenv.mjs +1 -1
  33. package/src/workspace-api.mjs +21 -4
  34. package/vendor/client-sdk/index.cjs +2 -1
  35. package/vendor/cloud-cli/mlai.mjs +23 -11
  36. package/worker/architecture_worker.py +33 -4
  37. package/worker/browser_tool.py +19 -1
  38. package/worker/chat_fork.py +4 -1
  39. package/worker/checkpoints.py +71 -8
  40. package/worker/command_safety.py +56 -3
  41. package/worker/connected_turn.py +30 -0
  42. package/worker/conversation.py +125 -13
  43. package/worker/git_sync.py +4 -4
  44. package/worker/image_generation.py +2 -2
  45. package/worker/kimi_bridge.py +251 -31
  46. package/worker/kimi_gates.py +25 -6
  47. package/worker/kimi_hook.py +154 -13
  48. package/worker/media_tools.py +4 -1
  49. package/worker/mlai_hooks.py +222 -3
  50. package/worker/mlai_mcp.py +26 -6
  51. package/worker/mlai_review/README.md +35 -0
  52. package/worker/mlai_review/__init__.py +1 -0
  53. package/worker/mlai_review/__main__.py +261 -0
  54. package/worker/mlai_review/_compat.py +29 -0
  55. package/worker/mlai_review/analysis/__init__.py +3 -0
  56. package/worker/mlai_review/analysis/detect.py +50 -0
  57. package/worker/mlai_review/analysis/main.py +128 -0
  58. package/worker/mlai_review/analysis/messages.py +8 -0
  59. package/worker/mlai_review/analysis/tools.py +194 -0
  60. package/worker/mlai_review/application/__init__.py +1 -0
  61. package/worker/mlai_review/application/asking.py +53 -0
  62. package/worker/mlai_review/application/challengers.py +142 -0
  63. package/worker/mlai_review/application/context_builder.py +194 -0
  64. package/worker/mlai_review/application/errors.py +17 -0
  65. package/worker/mlai_review/application/feedback.py +52 -0
  66. package/worker/mlai_review/application/matching.py +46 -0
  67. package/worker/mlai_review/application/messages.py +41 -0
  68. package/worker/mlai_review/application/ports.py +222 -0
  69. package/worker/mlai_review/application/prompts.py +342 -0
  70. package/worker/mlai_review/application/review_service.py +200 -0
  71. package/worker/mlai_review/application/reviewers.py +438 -0
  72. package/worker/mlai_review/application/streaming.py +91 -0
  73. package/worker/mlai_review/application/tools.py +138 -0
  74. package/worker/mlai_review/application/usage.py +145 -0
  75. package/worker/mlai_review/application/write_check.py +46 -0
  76. package/worker/mlai_review/contracts/__init__.py +5 -0
  77. package/worker/mlai_review/contracts/analysis.py +57 -0
  78. package/worker/mlai_review/contracts/review.py +42 -0
  79. package/worker/mlai_review/domain/__init__.py +4 -0
  80. package/worker/mlai_review/domain/changes.py +77 -0
  81. package/worker/mlai_review/domain/context.py +147 -0
  82. package/worker/mlai_review/domain/diagnostics.py +239 -0
  83. package/worker/mlai_review/domain/feedback.py +216 -0
  84. package/worker/mlai_review/domain/findings.py +505 -0
  85. package/worker/mlai_review/domain/lifecycle.py +85 -0
  86. package/worker/mlai_review/domain/partition.py +42 -0
  87. package/worker/mlai_review/domain/relations.py +166 -0
  88. package/worker/mlai_review/domain/relevance.py +202 -0
  89. package/worker/mlai_review/domain/report.py +99 -0
  90. package/worker/mlai_review/domain/structure.py +56 -0
  91. package/worker/mlai_review/domain/writes.py +86 -0
  92. package/worker/mlai_review/eval.py +221 -0
  93. package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
  94. package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
  95. package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
  96. package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
  97. package/worker/mlai_review/feedback_report.py +60 -0
  98. package/worker/mlai_review/infrastructure/__init__.py +1 -0
  99. package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
  100. package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
  101. package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
  102. package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
  103. package/worker/mlai_review/infrastructure/github/client.py +445 -0
  104. package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
  105. package/worker/mlai_review/infrastructure/github/http.py +31 -0
  106. package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
  107. package/worker/mlai_review/infrastructure/github/token.py +20 -0
  108. package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
  109. package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
  110. package/worker/mlai_review/infrastructure/local/events.py +100 -0
  111. package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
  112. package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
  113. package/worker/mlai_review/infrastructure/messages.py +54 -0
  114. package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
  115. package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
  116. package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
  117. package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
  118. package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
  119. package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
  120. package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
  121. package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
  122. package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
  123. package/worker/mlai_review/pipeline.py +89 -0
  124. package/worker/mlai_review/runtime.py +194 -0
  125. package/worker/mlai_review/settings.py +139 -0
  126. package/worker/preview.py +15 -0
  127. package/worker/product_facts.py +3 -1
  128. package/worker/repo_policy.py +5 -0
  129. package/worker/roles.py +29 -2
  130. package/worker/runner.py +164 -23
  131. package/worker/runtime_control.py +95 -9
  132. package/worker/secret_scan.py +3 -2
  133. package/worker/server_memory.py +122 -0
  134. package/worker/subagents.py +6 -2
  135. package/worker/tool_catalog.py +5 -2
  136. package/worker/verification.py +113 -8
package/src/cloud.mjs CHANGED
@@ -25,5 +25,7 @@ export function friendly(error){
25
25
  if(error.code==='billing_limit')return error.message+' Manage your plan at code.ml.ai.';
26
26
  if(error.status===429)return error.message;
27
27
  if(error.code==='not_configured')return 'Laptop runs are not enabled on this server yet.';
28
+ // BUG-0504: nothing was connected, so "Refresh status before retrying a write" only confused people.
29
+ if(error.code==='connection_interrupted')return 'Cannot reach the ML AI Code server'+(error.server?' at '+error.server:'')+'. Check your internet connection'+(error.server?' and the server address (--server or MLAI_SERVER)':'')+'.';
28
30
  return error.message;
29
31
  }
package/src/engine.mjs CHANGED
@@ -14,7 +14,7 @@ import {pinnedPublicKey,ed25519PublicKey} from './engine-key.mjs';
14
14
  export const ENGINE_PROTOCOL=1;
15
15
  export const CHECK_EVERY=10*60*1000,WAIT_MS=3000,DOWNLOAD_MS=60000;
16
16
  /** Copy of lib/laptop-engine.ts ENGINE_FILES (parity test): the bundled copy's version is hashed over these. */
17
- export const ENGINE_FILES=['ask_first.py','attachment_reader.py','background_jobs.py','brief_questions.py','browser_local.py','browser_pane.py','browser_tool.py','build_planner.py','checkpoints.py','command_policy.py','command_safety.py','compaction_brief.py','computer_card.sh','computer_setup.sh','computer_use.py','connector_search.py','context_manager.py','conversation.py','deck_render.js','deck_tools.py','deploy_pack.py','design_director.py','dev_jobs.py','edit_guard.py','env_setup.py','evidence.py','fast_search.py','goals.py','handover.py','harness_memory.py','image_generation.py','intelligence.py','intent_model.py','intent_weights.json','ios_simulator.py','kimi_bridge.py','kimi_compact.py','kimi_gates.py','kimi_hook.py','lifecycle.py','lsp.py','mcp_local.py','media_tools.py','memory.py','mlai_auto_review.py','mlai_hooks.py','mlai_mcp.py','mlai_permissions.py','model_stream.py','nested_repos.py','obs_mask.py','offline_guard.py','preview.py','proc_watch.py','product_facts.py','project_context.py','project_rules.py','quality.py','reply_language.py','repo_hygiene.py','repo_memory.py','repo_policy.py','roles.json','roles.py','runner.py','runtime_control.py','secret_scan.py','session_hub.py','site_check.py','skill_library.py','skill_pack.py','subagents.py','task_controller.py','task_skills.py','token_count.py','tool_catalog.py','verification.py','vision.py','wakeups.py','web_tools.py','winproc.py','work_ledger.py','worktrees.py'];
17
+ export const ENGINE_FILES=['ask_first.py','attachment_reader.py','background_jobs.py','brief_questions.py','browser_local.py','browser_pane.py','browser_tool.py','build_planner.py','checkpoints.py','command_policy.py','command_safety.py','compaction_brief.py','computer_card.sh','computer_setup.sh','computer_use.py','connected_turn.py','connector_search.py','context_manager.py','conversation.py','deck_render.js','deck_tools.py','deploy_pack.py','design_director.py','dev_jobs.py','edit_guard.py','env_setup.py','evidence.py','fast_search.py','goals.py','handover.py','harness_memory.py','image_generation.py','intelligence.py','intent_model.py','intent_weights.json','ios_simulator.py','kimi_bridge.py','kimi_compact.py','kimi_gates.py','kimi_hook.py','lifecycle.py','lsp.py','mcp_local.py','media_tools.py','memory.py','mlai_auto_review.py','mlai_hooks.py','mlai_mcp.py','mlai_permissions.py','model_stream.py','nested_repos.py','obs_mask.py','offline_guard.py','preview.py','proc_watch.py','product_facts.py','project_context.py','project_rules.py','quality.py','reply_language.py','repo_hygiene.py','repo_memory.py','repo_policy.py','roles.json','roles.py','runner.py','runtime_control.py','secret_scan.py','server_memory.py','session_hub.py','site_check.py','skill_library.py','skill_pack.py','subagents.py','task_controller.py','task_skills.py','token_count.py','tool_catalog.py','verification.py','vision.py','wakeups.py','web_tools.py','winproc.py','work_ledger.py','worktrees.py'];
18
18
  /** The entry scripts; a manifest without them is not a harness. */
19
19
  export const REQUIRED=['runner.py','kimi_bridge.py'];
20
20
  const NAME=/^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/,SHA=/^[a-f0-9]{64}$/,VERSION=/^[a-f0-9]{32}$/;
package/src/headless.mjs CHANGED
@@ -6,13 +6,14 @@
6
6
  * --max-budget-credits the server caps the run's tokens (run.maxTokens); the next turn never starts past it
7
7
  * --fallback-model Chat.send switches model when the chosen one is overloaded or unavailable
8
8
  * --allowedTools / --disallowedTools / --tools permission rules (worker/mlai_permissions.py syntax)
9
- * Result subtypes: success, error_during_execution, error_max_turns, error_max_budget, error_schema, stopped.
9
+ * Result subtypes: success, error_during_execution, error_max_turns, error_max_budget, error_schema, error_needs_approval,
10
+ * error_cannot_complete (exit 6: the agent reported the request cannot be done), stopped.
10
11
  */
11
12
  import {readFileSync,existsSync} from 'node:fs';
12
13
  import {readFile,rm} from 'node:fs/promises';
13
14
  import {normalizeRule} from './tui/permissions.mjs';
14
15
  import {TOKENS_PER_CREDIT,creditsFor,answerOf} from './tui/chat.mjs';
15
- import {renderEvent,summaryLine,exitCode,EXIT} from './render.mjs';
16
+ import {renderEvent,summaryLine,exitCode,EXIT,prose as proseOf} from './render.mjs';
16
17
 
17
18
  export const TOOL_NAMES=['Bash','Edit','Read','WebFetch','WebSearch','Agent'];
18
19
  export class HeadlessError extends Error{}
@@ -121,17 +122,36 @@ const finalText=(r,events)=>answerOf({events,status:{result:r.result,error:r.err
121
122
  * Runs the print-mode turns. o: parsed flags; s: the Session; chat: its Chat (toolRules, appendPrompt, fallbacks set).
122
123
  * inputs: an async iterable of {text, images} (one item without --input-format stream-json). Returns the exit code.
123
124
  */
124
- export async function runHeadless(o,{s,chat,err,inputs,out=t=>process.stdout.write(t),log=t=>process.stderr.write(t)}){
125
+ export async function runHeadless(o,{s,chat,err,inputs,out=t=>process.stdout.write(t),log=t=>process.stderr.write(t),tty=!!(process.stdout.isTTY&&process.stderr.isTTY)}){
125
126
  const stream=o.outputFormat==='stream-json',json=o.outputFormat==='json';
126
127
  const write=x=>out(JSON.stringify(x)+'\n');
127
128
  const schema=o.jsonSchema?loadSchema(o.jsonSchema):null;
128
- if(o.maxTurns)chat.steps=Math.min(chat.steps,o.maxTurns);
129
+ // BUG-0492: --max-turns is an exact cap for the whole run (hardSteps), not the engine's usual minimum of 10.
130
+ if(o.maxTurns){chat.steps=Math.min(chat.steps,o.maxTurns);chat.hardSteps=true}
131
+ chat.unattended=true; // BUG-0519: nobody can answer a -p run, so a sandbox-blocked write ends it instead of looping
129
132
  let events=[],refused=0,spent=0,turns=0,code=EXIT.completed,last=null;
133
+ // BUG-0557: the engine must not promise an automatic continue that a one-shot process will never run.
134
+ const oneShot=process.env.MINI_ONE_SHOT;process.env.MINI_ONE_SHOT='1';
135
+ // BUG-0561: the agent's last message is also the final answer on stdout; held back so a terminal shows it once.
136
+ // The run's closing events (the "N files changed" summary, notes, retries) arrive after the answer; they are printed
137
+ // without releasing the held prose, so the answer is not shown once as prose and again as the final text.
138
+ const CLOSING=new Set(['completion','note','finished','questions','retry','intent','mode','budget','run_metrics','done_gate','verification_report','policy']);
139
+ let prose=null,proseRaw=null,after=[];const shown=[];
140
+ const flushProse=()=>{if(prose!=null){log(prose+'\n');shown.push(proseOf(proseRaw));if(shown.length>8)shown.shift();prose=null}for(const l of after)log(l+'\n');after=[]};
141
+ const sameText=(a,b)=>{const x=String(a||'').replace(/\s+/g,' ').trim();return !!x&&String(b||'').replace(/\s+/g,' ').includes(x)};
142
+ // In a terminal (stdout and stderr both on screen) an answer the agent already wrote as prose is not printed again:
143
+ // only what the harness added to it (a verification note) is. Piped stdout always gets the full answer.
144
+ const unseen=text=>{
145
+ if(!tty||!text)return text;
146
+ for(const p of shown){const raw=String(p||'').trim();if(!raw)continue;
147
+ if(String(text).replace(/\s+/g,' ').trim()===raw.replace(/\s+/g,' ').trim())return '';
148
+ if(raw.length>40&&String(text).includes(raw))return String(text).replace(raw,'').trim()}
149
+ return text};
130
150
  const stop=()=>{s.cancel().catch(()=>{})};process.once('SIGINT',stop);process.once('SIGTERM',stop);
131
151
  const onEvent=e=>{
132
152
  events.push(e);
133
153
  if(stream)write(e);
134
- else if(!json){const line=renderEvent(e,{c:err,verbose:o.verbose});if(line&&e.type!=='answer')log(line+'\n')}
154
+ else if(!json){const line=renderEvent(e,{c:err,verbose:o.verbose});if(line&&e.type!=='answer'){if(prose!=null&&CLOSING.has(e.type))after.push(line);else{flushProse();if(e.type==='assistant'){prose=line;proseRaw=e.content}else log(line+'\n')}}}
135
155
  if(e.type==='approval'){
136
156
  if(o.yes)s.decide(e.id,'approve_all').catch(()=>{});
137
157
  else if(o.bgChild){} // a background run (mlai --bg) waits for mlai attach to answer
@@ -149,7 +169,7 @@ export async function runHeadless(o,{s,chat,err,inputs,out=t=>process.stdout.wri
149
169
  };
150
170
  try{
151
171
  for await(const input of inputs){
152
- events=[];
172
+ events=[];const turnRefused=refused;
153
173
  const images=[];
154
174
  for(const b of input.images||[])images.push(await s.addImage(b));
155
175
  for(const f of input.files||[])images.push(await s.addImage(await readFile(f)));
@@ -157,8 +177,9 @@ export async function runHeadless(o,{s,chat,err,inputs,out=t=>process.stdout.wri
157
177
  if(cap!=null&&spent>=cap){r={state:'limited',error:'The --max-budget-credits limit was reached before this turn.'};subtype='error_max_budget'}
158
178
  else{
159
179
  r=await send(input.text+(schema?schemaTask(schema):''),images.length?images:undefined);
160
- const budgetHit=cap!=null&&(r.code==='billing_limit'||(r.state!=='completed'&&spent>=cap));
161
- if(budgetHit){r={...r,code:'budget_limit',error:'Stopped at the --max-budget-credits limit ('+o.maxBudgetCredits+' credits). Changes so far are kept.'};subtype='error_max_budget'}
180
+ // BUG-0509: reaching the cap is never a success, even when the agent wrapped up on its own at the limit.
181
+ const budgetHit=cap!=null&&(r.code==='billing_limit'||spent>=cap);
182
+ if(budgetHit){r={...r,code:'budget_limit',error:'Stopped at the --max-budget-credits limit ('+o.maxBudgetCredits+' credits; this run used '+creditsFor(spent)+' because the model step in progress finishes). Changes so far are kept.'};subtype='error_max_budget'}
162
183
  else if(r.state==='limited'&&o.maxTurns&&r.result?.limit!=='tokens')subtype='error_max_turns';
163
184
  else if(r.state==='stopped')subtype='stopped';
164
185
  else if(r.state!=='completed')subtype='error_during_execution';
@@ -175,20 +196,27 @@ export async function runHeadless(o,{s,chat,err,inputs,out=t=>process.stdout.wri
175
196
  }
176
197
  }
177
198
  const text=finalText(r,events);
178
- const exit=subtype==='success'?EXIT.completed:subtype==='error_max_turns'||subtype==='error_max_budget'?EXIT.limited:subtype==='error_schema'?EXIT.error:exitCode(r);
199
+ // BUG-0544: approvals refused because nobody could answer them (-p without --yes): the run needs attention.
200
+ // BUG-0523: the agent decided the request cannot be done (no remote to push to, an unsupported feature): not a success.
201
+ if(subtype==='success'&&r.cannotComplete){subtype='error_cannot_complete';r={...r,error:r.cannotComplete.reason}}
202
+ if(subtype==='success'&&refused>turnRefused){subtype='error_needs_approval';r={...r,error:(refused-turnRefused)+' action'+(refused-turnRefused===1?'':'s')+' needed approval and did not run. Pass --yes (-y) or --allowedTools to allow them.'}}
203
+ const exit=subtype==='error_cannot_complete'?EXIT.cannotComplete:subtype==='error_needs_approval'?EXIT.limited:subtype==='success'?EXIT.completed:subtype==='error_max_turns'||subtype==='error_max_budget'?EXIT.limited:subtype==='error_schema'?EXIT.error:exitCode(r);
179
204
  if(exit!==EXIT.completed)code=exit;
180
205
  last={type:'result',subtype,state:r.state,exitCode:exit,sessionId:s.meta.id,name:s.meta.title||null,mode:chat.mode,model:chat.model||chat.info?.id||null,effort:chat.effort,text,
181
206
  ...(schema?{structuredOutput:structured??null}:{}),error:subtype==='success'?null:(r.error||null),usage:r.usage||null,
182
207
  credits:creditsFor(spent),turns,changedFiles:r.completion?.changedFiles||[],checksPassed:r.completion?.checksPassed||0,checksFailed:r.completion?.checksFailed||0,rejectedApprovals:refused,
183
- persisted:!o.noSessionPersistence};
208
+ persisted:!o.noSessionPersistence,
209
+ // BUG-0555: with -w the work is in its own worktree; say where, so scripts and people can find it.
210
+ ...(s.meta.worktree&&!s.meta.worktree.removed?{worktree:{path:s.meta.worktree.path,branch:s.meta.worktree.branch}}:{})};
184
211
  if(stream)write(last);
185
- else if(!json){if(schema&&structured!==undefined)out(JSON.stringify(structured)+'\n');else if(text)out(text.trim()+'\n');log(summaryLine(r,err)+'\n')}
212
+ else if(!json){if(prose!=null&&(sameText(proseRaw,text)||sameText(prose,text)))prose=null;flushProse();const fresh=unseen(text);if(schema&&structured!==undefined)out(JSON.stringify(structured)+'\n');else if(fresh)out(fresh.trim()+'\n');log(summaryLine(r,err)+'\n');if(last.worktree&&last.changedFiles.length)log(err.dim('The changes are in the worktree '+last.worktree.path+' on branch '+last.worktree.branch+', not in your folder. Look with: cd '+last.worktree.path+' · bring them back with: git merge '+last.worktree.branch+' (after committing them there)')+'\n')}
186
213
  if(['error_max_budget','stopped'].includes(subtype))break;
187
214
  }
188
215
  if(json&&last)write(last);
189
216
  if(!last)throw new HeadlessError('No task: pass one with -p "task" or send {"type":"user",...} lines on stdin.');
190
217
  return code;
191
218
  }finally{
219
+ if(oneShot===undefined)delete process.env.MINI_ONE_SHOT;else process.env.MINI_ONE_SHOT=oneShot;
192
220
  process.off('SIGINT',stop);process.off('SIGTERM',stop);
193
221
  s.off('event',onEvent);s.off('started',onStarted);chat.off('fallback',onFallback);
194
222
  if(o.noSessionPersistence)await rm(s.dir,{recursive:true,force:true}).catch(()=>{});
package/src/home.mjs CHANGED
@@ -3,8 +3,9 @@ import path from 'node:path';
3
3
  import {mkdirSync,chmodSync,statSync} from 'node:fs';
4
4
  import {fileURLToPath} from 'node:url';
5
5
 
6
- export const VERSION='0.3.3';
7
- export const DEFAULT_SERVER='https://mlai-mini-code.vercel.app';
6
+ export const VERSION='0.3.5';
7
+ // BUG-0469: the production CLI signs in and calls the production domain.
8
+ export const DEFAULT_SERVER='https://code.ml.ai';
8
9
  /** All local state lives here; nothing is written into the user's repository except what the agent edits. */
9
10
  export function home(){
10
11
  const dir=process.env.MLAI_HOME||path.join(os.homedir(),'.mlai');
package/src/host.mjs CHANGED
@@ -9,9 +9,18 @@ import {runtimeInfo,ensureRuntime} from './runtime.mjs';
9
9
  import {VERSION} from './home.mjs';
10
10
  import {projectRoot} from './project.mjs';
11
11
  import * as wt from './worktree.mjs';
12
+ import {Checkpoints,CheckpointError} from './checkpoints.mjs';
13
+ import {IdeError,validClient,validModel,validMcpServers,mcpSummary,renderContext,pickerModels,handoff,scanText,folderKey} from './ide.mjs';
14
+ import {modeName,applyMode,MODES as CLI_MODES} from './modes.mjs';
15
+ import {loadSettings} from './settings.mjs';
16
+ import {prepareEngine} from './engine.mjs';
12
17
 
13
18
  export class HostError extends Error{constructor(message,status=400,code){super(message);this.status=status;this.code=code}}
14
19
  const ID=/^[0-9a-f-]{36}$/;
20
+ /** Session mode names the API takes: the engine's (guided, auto, plan) and, for editor sessions, the app's (agent, ask). */
21
+ const modeOf=m=>MODES[m]?modeName(m):['agent','ask'].includes(m)?m:null;
22
+ /** Errors from the editor modules keep their status and code. */
23
+ const hostError=err=>err instanceof HostError?err:(err instanceof IdeError||err instanceof CheckpointError)?new HostError(err.message,err.status,err.code):err;
15
24
 
16
25
  /**
17
26
  * cloud: async () => Cloud for the signed-in account. Throw when signed out.
@@ -26,7 +35,9 @@ export class EngineHost extends EventEmitter{
26
35
  s.on('event',e=>{entry.events.push(e);if(entry.events.length>5000)entry.events.splice(0,1000);out('harness',e)});
27
36
  s.on('started',d=>{entry.events=[];out('started',d)});
28
37
  s.on('done',r=>{entry.result=r;out('done',r)});
29
- entry.out=out;this.live.set(id,entry);return entry;
38
+ entry.out=out;this.live.set(id,entry);
39
+ if(s.meta.client)this.#watch(entry);
40
+ return entry;
30
41
  }
31
42
  async entry(id){
32
43
  if(typeof id!=='string'||!ID.test(id))throw new HostError('Unknown session',404);
@@ -38,11 +49,15 @@ export class EngineHost extends EventEmitter{
38
49
  prepare(onProgress){return ensureRuntime({onProgress})}
39
50
  /** Sessions for a project, including the ones working in their own worktree. Archived ones only when asked. */
40
51
  async sessions({root,archived=false}={}){
41
- const all=await Session.list();
42
- return all.filter(m=>(!root||m.root===root||m.parent===root)&&(archived||!m.archived)).map(m=>({...m,running:this.live.get(m.id)?.running||false}));
52
+ const all=await Session.list(),want=root?folderKey(root):null;
53
+ return all.filter(m=>(!root||m.root===root||m.parent===root||(m.root&&folderKey(m.root)===want)||(m.parent&&folderKey(m.parent)===want))&&(archived||!m.archived)).map(m=>({...m,running:this.live.get(m.id)?.running||false}));
43
54
  }
44
- async create({cwd,mode='guided',worktree=false,name}){
45
- if(!MODES[mode])throw new HostError('Mode must be guided, auto or plan');
55
+ async create({cwd,mode='guided',worktree=false,name,model,client,mcpServers}){
56
+ const asked=mode,ide={client:undefined,model:undefined,mcp:undefined};
57
+ try{ide.client=validClient(client);ide.model=validModel(model);ide.mcp=validMcpServers(mcpServers)}catch(err){throw hostError(err)}
58
+ const cli=modeOf(mode);
59
+ if(!cli)throw new HostError('Mode must be agent, plan, ask or auto');
60
+ if(!MODES[mode])mode=CLI_MODES[cli].engine;
46
61
  if(typeof cwd!=='string'||!path.isAbsolute(cwd))throw new HostError('Choose an existing folder');
47
62
  try{if(!(await stat(cwd)).isDirectory())throw 0}catch{throw new HostError('Choose an existing folder')}
48
63
  let tree=null;
@@ -51,7 +66,10 @@ export class EngineHost extends EventEmitter{
51
66
  if(!git)throw new HostError('Worktrees need a git repository. Turn off "Own worktree" to work in this folder directly.',400,'no_worktree');
52
67
  try{tree=await wt.createWorktree(root,{name:typeof name==='string'&&name.trim()?name.slice(0,200):undefined})}catch(err){throw new HostError(err.message,400,err.code||'no_worktree')}
53
68
  }
54
- const s=await Session.create({cwd,mode,worktree:tree});this.attach(s);return s.meta;
69
+ const s=await Session.create({cwd,mode,worktree:tree});
70
+ if(ide.client||!MODES[asked]){s.meta.cliMode=cli;if(ide.client)s.meta.client=ide.client;if(ide.model)s.meta.model=ide.model;await s.save()}
71
+ const e=this.attach(s);if(ide.mcp)e.mcp=ide.mcp;
72
+ return ide.client?{...s.meta,mcpServers:mcpSummary(e.mcp)}:s.meta;
55
73
  }
56
74
  /** Rename, pin or archive a session. */
57
75
  async annotate(id,{title,pinned,archived}={}){
@@ -75,20 +93,31 @@ export class EngineHost extends EventEmitter{
75
93
  }catch(err){if(err instanceof HostError)throw err;throw new HostError(err.message,409,err.code)}
76
94
  throw new HostError('Unknown worktree action');
77
95
  }
78
- async get(id){const e=await this.entry(id);return {...e.session.meta,running:e.running,pending:await e.session.pending(),result:e.result}}
96
+ async get(id){const e=await this.entry(id);return {...e.session.meta,running:e.running,pending:await e.session.pending(),result:e.result,...(e.session.meta.client?{mcpServers:mcpSummary(e.mcp)}:{})}}
79
97
  async transcript(id){return (await this.entry(id)).session.transcript()}
80
98
  events(id,after=0){const e=this.live.get(id);return e?e.events.slice(Math.max(0,after)):[]}
81
99
  /** Starts a task and resolves once it has started, or rejects if it could not start. The run continues in the background. */
82
- async start(id,{task,steps,allowUnsandboxed=false,images}={}){
100
+ async start(id,{task,steps,allowUnsandboxed=false,images,context,model,mcpServers}={}){
83
101
  const e=await this.entry(id),s=e.session;
84
102
  if(e.running)throw new HostError('A task is already running in this session',409,'session_busy');
85
103
  if(typeof task!=='string'||!task.trim()||task.length>30000)throw new HostError('Enter a task under 30,000 characters');
104
+ let prefix='';
105
+ try{prefix=renderContext(context);if(s.meta.client){const m=validModel(model);if(m&&m!==s.meta.model){s.meta.model=m;await s.save()}const mcp=validMcpServers(mcpServers);if(mcp)e.mcp=mcp}}catch(err){throw hostError(err)}
106
+ if(prefix)task=prefix+'My request:\n'+task;
107
+ // Editors may send pasted images inline as data URLs; they are saved like uploads and passed by name.
108
+ if(Array.isArray(images)&&images.some(x=>typeof x==='string'&&x.startsWith('data:'))){
109
+ if(images.length>4)throw new HostError('Attach up to 4 images.');
110
+ const names=[];
111
+ for(const x of images){if(typeof x==='string'&&x.startsWith('data:')){const m=/^data:image\/(png|jpeg|jpg|webp|gif);base64,([A-Za-z0-9+/=\s]+)$/.exec(x);if(!m)throw new HostError('Use a PNG, JPEG, WebP or GIF image.');names.push((await this.addImage(id,Buffer.from(m[2],'base64'))).name)}else names.push(x)}
112
+ images=names;
113
+ }
86
114
  let c;try{c=await this.cloud()}catch(err){throw new HostError(friendly(err),401,'not_signed_in')}
87
115
  if(s.meta.worktree?.removed)throw new HostError('This session\u2019s worktree was removed. Start a new session.',409,'worktree_removed');
88
116
  e.running=true;e.result=null;
89
117
  let started=null;
90
118
  const begun=new Promise(r=>s.once('started',x=>{started=x;r({started:x})}));
91
- const done=s.send(task,{cloud:c,steps:Number(steps)||120,allowUnsandboxed:allowUnsandboxed===true,images,onProgress:m=>e.out('progress',{message:m})})
119
+ const run=s.meta.client?this.#ideTurn(e,task,{cloud:c,steps,allowUnsandboxed,images}):s.send(task,{cloud:c,steps:Number(steps)||120,allowUnsandboxed:allowUnsandboxed===true,images,onProgress:m=>e.out('progress',{message:m})});
120
+ const done=run
92
121
  .catch(err=>{const r={state:'error',error:friendly(err),code:err.code||err.error?.code||null};if(started){e.result=r;e.out('done',r)}return {...r,failedToStart:!started,status:err.status}})
93
122
  .finally(()=>{e.running=false});
94
123
  const first=await Promise.race([begun,done.then(r=>({result:r}))]);
@@ -101,8 +130,11 @@ export class EngineHost extends EventEmitter{
101
130
  /** Saves an image for this session's next message and returns its name. */
102
131
  async addImage(id,bytes){const e=await this.entry(id);try{return {name:await e.session.addImage(bytes)}}catch(err){throw new HostError(err.message)}}
103
132
  async image(id,name){const e=await this.entry(id);try{return {url:await e.session.image(name)}}catch(err){throw new HostError(err.message,404)}}
104
- async decide(id,{id:approval,verdict}={}){
133
+ async decide(id,{id:approval,verdict,revise,items}={}){
105
134
  const e=await this.entry(id);
135
+ // Plan review: {revise:'text'} replies to the plan; {verdict:'approve', items:[0,2]} keeps only some tasks.
136
+ if(typeof revise==='string'&&revise.trim())verdict='revise:'+revise;
137
+ else if(verdict==='approve'&&Array.isArray(items)&&items.length&&items.every(n=>Number.isInteger(n)&&n>=0&&n<40))verdict='approve:'+items.join(',');
106
138
  try{await e.session.decide(approval,verdict)}catch(err){throw new HostError(err.message)}
107
139
  return {ok:true};
108
140
  }
@@ -114,10 +146,106 @@ export class EngineHost extends EventEmitter{
114
146
  }
115
147
  async cancel(id){const e=await this.entry(id);await e.session.cancel();return {ok:true}}
116
148
  async mode(id,{mode}={}){
117
- const e=await this.entry(id);
118
- if(!MODES[mode])throw new HostError('Mode must be guided, auto or plan');
149
+ const e=await this.entry(id),ide=Boolean(e.session.meta.client);
150
+ if(!modeOf(mode))throw new HostError('Mode must be agent, plan, ask or auto');
119
151
  if(e.running)throw new HostError('Change the mode between tasks',409);
120
- e.session.meta.mode=mode;await e.session.save();return e.session.meta;
152
+ if(ide||!MODES[mode])await applyMode(e.session,modeOf(mode));
153
+ else{e.session.meta.mode=mode;await e.session.save()}
154
+ return e.session.meta;
155
+ }
156
+ // ---------- editor sessions (VS Code, JetBrains, ACP): checkpoints, models, fork, handoff ----------
157
+ cps(e){return e.cps||(e.cps=new Checkpoints(e.session))}
158
+ /** Checkpoint after every agent step that changed files; each one is announced with checkpoint and file_changed events. */
159
+ #watch(e){
160
+ const s=e.session;e.step=0;
161
+ const capture=label=>this.cps(e).capture({turn:Math.max(0,s.meta.turns.length-1),step:++e.step,label}).then(cp=>this.#announce(e,cp)).catch(()=>{});
162
+ s.on('event',ev=>{
163
+ if(!e.running||!ev||typeof ev!=='object')return;
164
+ if(ev.type==='output')capture(ev.output?String(ev.output).split('\n')[0].slice(0,120):'Step '+(e.step+1));
165
+ else if(ev.type==='finished')capture('End of turn');
166
+ });
167
+ s.on('done',()=>{e.final=capture('End of turn')});
168
+ }
169
+ #announce(e,cp){
170
+ if(!cp)return null;
171
+ const {tree,changes,...pub}=cp;
172
+ e.out('checkpoint',pub);
173
+ for(const f of changes||[])e.out('file_changed',{path:f.path,status:f.status,checkpoint:cp.id});
174
+ return pub;
175
+ }
176
+ /** One editor turn: a turn-start checkpoint, then the same Chat path as the terminal (modes, model, local extensions). */
177
+ async #ideTurn(e,task,{cloud,steps,allowUnsandboxed,images}){
178
+ const s=e.session;
179
+ const turn=s.meta.turns.length;e.step=0;
180
+ const cp=await this.cps(e).capture({turn,step:0,label:'Before turn '+(turn+1),force:true});
181
+ if(cp){const {tree,changes,...pub}=cp;e.out('checkpoint',pub)}
182
+ const {Chat}=await import('./tui/chat.mjs');
183
+ let settings={};try{settings=loadSettings({root:s.meta.parent||s.meta.root})}catch{}
184
+ const chat=new Chat({session:s,settings,cloud,flags:{model:s.meta.model||undefined,maxSteps:Number(steps)||undefined,...(allowUnsandboxed===true?{sandbox:false}:{})}});
185
+ chat.sessionMcp=e.mcp||null;
186
+ try{return await chat.send(task,{images,onProgress:m=>e.out('progress',{message:m})})}
187
+ finally{s.off('started',chat.onStarted)}
188
+ }
189
+ async checkpoints(id){const e=await this.entry(id);return this.cps(e).public()}
190
+ async changes(id,{since}={}){
191
+ const e=await this.entry(id);
192
+ try{return (await this.cps(e).changes(since||'turn-start')).files}catch(err){throw hostError(err)}
193
+ }
194
+ async revert(id,{path:file,hunks,since}={}){
195
+ const e=await this.entry(id);
196
+ if(e.running)throw new HostError('Wait for the task to finish, or stop it, before reverting',409,'session_busy');
197
+ if(hunks!=null&&(!Array.isArray(hunks)||hunks.length>500))throw new HostError('hunks must be a list');
198
+ try{const r=await this.cps(e).revert(file,{hunks,since:since||'turn-start'});e.out('file_changed',{path:r.path,status:r.status==='removed'?'deleted':'modified',checkpoint:r.checkpoint,reverted:true});return r}catch(err){throw hostError(err)}
199
+ }
200
+ /** Restores files, the conversation, or both to a checkpoint. A chat restore keeps the turns up to the checkpoint. */
201
+ async restore(id,{checkpoint,scope='both'}={}){
202
+ const e=await this.entry(id),s=e.session;
203
+ if(!['files','chat','both'].includes(scope))throw new HostError('scope must be files, chat or both');
204
+ if(e.running)throw new HostError('Stop the task before restoring a checkpoint',409,'session_busy');
205
+ let cp;try{cp=await this.cps(e).find(checkpoint)}catch(err){throw hostError(err)}
206
+ const out={checkpoint:cp.id,scope};
207
+ if(scope!=='chat'){out.files=await this.cps(e).restoreFiles(cp.id);for(const f of out.files.files)e.out('file_changed',{path:f,status:'modified',checkpoint:cp.id,restored:true})}
208
+ if(scope!=='files'){
209
+ // A turn-start checkpoint removes that turn too; a later step keeps its turn (the conversation cannot split a turn).
210
+ const keep=cp.step===0?cp.turn:cp.turn+1;
211
+ if(keep<s.meta.turns.length){
212
+ const {Chat}=await import('./tui/chat.mjs');const chat=new Chat({session:s});
213
+ await chat.rewind(keep,'chat');e.events=[];e.result=null;
214
+ await this.cps(e).forgetAfter(cp.step===0?cp.turn-1:cp.turn,{keep:cp.id});
215
+ }
216
+ out.turns=s.meta.turns.length;
217
+ }
218
+ e.out('restored',out);
219
+ return out;
220
+ }
221
+ /** A copy of the session (conversation, settings and checkpoints) under a new id. */
222
+ async fork(id,{title}={}){
223
+ const e=await this.entry(id);
224
+ const {Chat}=await import('./tui/chat.mjs');
225
+ const s=await Chat.fork(e.session.meta.id);
226
+ if(typeof title==='string'&&title.trim()){s.meta.title=title.replace(/[\0-\x1f]/g,' ').trim().slice(0,120);s.meta.renamed=true;await s.save()}
227
+ const n=this.attach(s);if(e.mcp)n.mcp=e.mcp;
228
+ return {...s.meta,...(s.meta.client?{mcpServers:mcpSummary(n.mcp)}:{})};
229
+ }
230
+ /** Sends the session to a cloud chat. {dryRun:true} (the default) only returns what would be sent. */
231
+ async handoff(id,{dryRun=true,note=''}={}){
232
+ const e=await this.entry(id),s=e.session;
233
+ if(typeof note!=='string'||note.length>5000)throw new HostError('Keep the note under 5,000 characters');
234
+ let python=null,worker=null;
235
+ try{python=await (await import('./runtime.mjs')).ensureRuntime({});worker=(await prepareEngine({cloud:null})).dir}catch{}
236
+ const scan=text=>scanText(text,{python,worker});
237
+ if(dryRun!==false){try{return await handoff(s,{note,scan})}catch(err){throw hostError(err)}}
238
+ if(e.running)throw new HostError('Wait for the task to finish, or stop it, before sending the session to the cloud.',409,'session_busy');
239
+ let c;try{c=await this.cloud()}catch(err){throw new HostError(friendly(err),401,'not_signed_in')}
240
+ try{return await handoff(s,{client:c.device,server:c.server,note,send:true,scan,commitAll:s.meta.worktree?()=>wt.commitAll({...s.meta.worktree,parent:s.meta.parent},s.meta.title||'Work from ML AI Code'):undefined})}
241
+ catch(err){const h=hostError(err);if(h instanceof HostError)throw h;throw new HostError(friendly(err),409,err.code)}
242
+ }
243
+ models(){return pickerModels({cloud:this.cloud})}
244
+ /** Replaces a session's editor MCP servers (an editor reconnecting to a session it did not create in this engine). */
245
+ async setMcp(id,{mcpServers}={}){
246
+ const e=await this.entry(id);
247
+ try{e.mcp=validMcpServers(mcpServers||[])}catch(err){throw hostError(err)}
248
+ return {mcpServers:mcpSummary(e.mcp)};
121
249
  }
122
250
  async close(){for(const e of this.live.values())if(e.running)await e.session.cancel().catch(()=>{})}
123
251
  }
@@ -0,0 +1,147 @@
1
+ // Editor quick calls on the `mlai serve` socket: tab completions (with next-edit suggestions), inline edit and commit
2
+ // messages. Each one is proxied to ML AI Code's cloud (/api/ide/*) with this computer's sign-in; the model is picked
3
+ // there. When the cloud cannot be reached and an on-device model is configured (MLAI_COMPLETIONS_MODEL or the request's
4
+ // `model`, an `offline:<server>:<model>` id from /v1/models), that model answers instead, on loopback only.
5
+ //
6
+ // POST /v1/complete {path,language,prefix,suffix,cursor:{line,character},recentEdits?,snippets?,model?}
7
+ // -> 200 {items:[{text,kind:'inline'|'next-edit',range?,targetLine?}]}
8
+ // (a request replaced by a newer one for the same file: 200 {items:[],superseded:true})
9
+ // POST /v1/edit {path,language,selection:{text,startLine,endLine},before,after,instruction,model?}
10
+ // -> 200 text/event-stream: `delta` {text} ..., `done` {text} (or `error` {message,code})
11
+ // POST /v1/commit-message {diff,model?} -> 200 {message}
12
+ // Errors before a reply starts: JSON {error,code} with the cloud's status (401 sign-in, 402 credits, 413, 429 ...).
13
+ //
14
+ // Completions: the latest request per file wins. A new one aborts the previous one (its cloud call too), and requests
15
+ // that arrive within the debounce window (MLAI_COMPLETE_DEBOUNCE_MS, default 30 ms) never reach the cloud. Every call
16
+ // is aborted when the editor closes the connection.
17
+ import {VERSION} from './home.mjs';
18
+ import {parseOfflineId,detectLocalModels,loopbackEndpoint} from './local-models.mjs';
19
+ import {networkError} from './offline.mjs';
20
+
21
+ export const QUICK_ROUTES=['complete','edit','commit-message'];
22
+ const CLOUD_PATH={complete:'/api/ide/complete',edit:'/api/ide/edit','commit-message':'/api/ide/commit-message'};
23
+ const MAX_BODY=96_000;
24
+ const TIMEOUT={complete:8_000,edit:120_000,'commit-message':30_000};
25
+
26
+ class QuickError extends Error{constructor(message,status=400,code='invalid_request'){super(message);this.status=status;this.code=code}}
27
+ const sendJson=(res,status,data)=>{if(res.headersSent||res.destroyed)return;res.writeHead(status,{'Content-Type':'application/json','Cache-Control':'no-store'});res.end(JSON.stringify(data))};
28
+ const sseLine=(type,data)=>'event: '+type+'\ndata: '+JSON.stringify(data)+'\n\n';
29
+ async function readBody(req){
30
+ // Drain an oversize body instead of breaking off (that would close the socket before the 413 is sent).
31
+ let raw='',size=0;for await(const c of req){size+=c.length;if(size<=MAX_BODY)raw+=c;else if(size>8*MAX_BODY)break}
32
+ if(size>MAX_BODY)throw new QuickError('Request is too large.',413,'request_too_large');
33
+ if(!raw)return {};
34
+ try{const v=JSON.parse(raw);if(!v||typeof v!=='object'||Array.isArray(v))throw 0;return v}catch{throw new QuickError('Invalid JSON body.')}
35
+ }
36
+
37
+ /** The same required fields the cloud checks, so a bad request is a 400 here, without a network call. */
38
+ export function validateQuick(name,b){
39
+ const str=(v,n,{required=false}={})=>{if(v===undefined||v===null){if(required)throw new QuickError(n+' is required.');return}if(typeof v!=='string')throw new QuickError(n+' must be text.')};
40
+ if(name==='complete'){str(b.prefix,'prefix',{required:true});str(b.suffix,'suffix');str(b.path,'path');str(b.language,'language');
41
+ if(b.cursor!==undefined&&(typeof b.cursor!=='object'||b.cursor===null))throw new QuickError('cursor must be {line, character}.');
42
+ if(b.recentEdits!==undefined&&!Array.isArray(b.recentEdits))throw new QuickError('recentEdits must be a list.');return}
43
+ if(name==='edit'){if(!b.selection||typeof b.selection!=='object')throw new QuickError('selection is required.');str(b.selection.text,'selection.text',{required:true});
44
+ str(b.instruction,'instruction',{required:true});if(!b.instruction.trim())throw new QuickError('Describe the change.');str(b.before,'before');str(b.after,'after');return}
45
+ str(b.diff,'diff',{required:true});if(!b.diff.trim())throw new QuickError('There are no changes to describe.');
46
+ }
47
+
48
+ /* ---------------- on-device fallback ---------------- */
49
+ /** Small prompts for a local model: the same reply formats as the cloud route, without next-edit suggestions. */
50
+ export function localMessages(name,b){
51
+ if(name==='complete')return [{role:'system',content:'You are a code completion engine. Reply with only <insert>...</insert> holding the text to insert at <CURSOR>: the rest of the line or statement, at most a few lines. Do not repeat text around the cursor. No code fences, no explanation.'},
52
+ {role:'user',content:'File: '+String(b.path||'untitled')+'\n\n'+String(b.prefix||'').slice(-6000)+'<CURSOR>'+String(b.suffix||'').slice(0,2000)}];
53
+ if(name==='edit')return [{role:'system',content:'Rewrite the selected code following the instruction. Reply with only the replacement code. No code fences, no explanation.'},
54
+ {role:'user',content:'Before:\n'+String(b.before||'').slice(-3000)+'\n\nSelection:\n'+String(b.selection?.text||'')+'\n\nAfter:\n'+String(b.after||'').slice(0,3000)+'\n\nInstruction: '+String(b.instruction||'')}];
55
+ return [{role:'system',content:'Write a git commit message: an imperative subject under 72 characters, optionally a blank line and a short body. Reply with only the message.'},{role:'user',content:String(b.diff||'').slice(0,24000)}];
56
+ }
57
+ const unfence=t=>String(t).replace(/^\s*```[\w#+.-]*[ \t]*\n/,'').replace(/\n?```\s*$/,'');
58
+ export function localCompletion(raw,b){
59
+ const m=/<insert>([\s\S]*?)(?:<\/insert>|$)/.exec(raw);let text=unfence(m?m[1]:raw).replace(/<CURSOR>/g,'');
60
+ const line=String(b.prefix||'').split('\n').pop()||'';if(line.trim()&&text.startsWith(line))text=text.slice(line.length);
61
+ text=text.split('\n').slice(0,16).join('\n').replace(/\s+$/,'');
62
+ return text.trim()?[{text,kind:'inline'}]:[];
63
+ }
64
+
65
+ export function createQuick({cloud,fetchImpl=globalThis.fetch,env=process.env,detect=detectLocalModels,debounceMs=Number(env.MLAI_COMPLETE_DEBOUNCE_MS??30)}={}){
66
+ const latest=new Map(); // completion key -> {seq, ctl}
67
+ const open=new Set();let seq=0;
68
+ const offlineModel=b=>parseOfflineId(typeof b.model==='string'&&b.model?b.model:env.MLAI_COMPLETIONS_MODEL)?(typeof b.model==='string'&&b.model?b.model:env.MLAI_COMPLETIONS_MODEL):null;
69
+
70
+ let cached=null;
71
+ const signedIn=async()=>{if(cached&&Date.now()-cached.at<60_000)return cached.c;const c=await cloud();cached={at:Date.now(),c};return c};
72
+ async function callCloud(name,b,signal){
73
+ let c;try{c=await signedIn()}catch(e){throw new QuickError(e?.message||'Not signed in. Run `mlai login`.',401,'sign_in_required')}
74
+ const token=await c.device?.tokenProvider?.();
75
+ if(!token)throw new QuickError('Not signed in. Run `mlai login`.',401,'sign_in_required');
76
+ const {model,...body}=b;
77
+ return (c.fetchImpl||fetchImpl)(c.server.replace(/\/$/,'')+CLOUD_PATH[name],{method:'POST',redirect:'error',signal,
78
+ headers:{'Content-Type':'application/json',Accept:name==='edit'?'text/event-stream':'application/json',Authorization:'Bearer '+token,'X-MLAI-Client':'laptop','X-MLAI-Version':VERSION},body:JSON.stringify(body)});
79
+ }
80
+ async function callLocal(name,b,id,signal){
81
+ const local=parseOfflineId(id);const found=(await detect({fetchImpl})).find(m=>m.id===id);
82
+ if(!local||!found||!loopbackEndpoint(found.endpoint))throw new QuickError('The offline model is not running. Start it on this computer, then try again.',503,'offline_model_missing');
83
+ const r=await fetchImpl(found.endpoint.replace(/\/$/,'')+'/chat/completions',{method:'POST',signal,headers:{'Content-Type':'application/json'},
84
+ body:JSON.stringify({model:local.model,messages:localMessages(name,b),temperature:0,max_tokens:name==='edit'?2048:name==='complete'?128:200,stream:false})});
85
+ if(!r.ok){await r.body?.cancel().catch(()=>{});throw new QuickError('The offline model had an error.',502,'offline_model_error')}
86
+ const d=await r.json().catch(()=>null);return String(d?.choices?.[0]?.message?.content||'');
87
+ }
88
+ /** Cloud first; on a network failure (or no sign-in) with an on-device model configured, that model. */
89
+ async function route(name,b,signal){
90
+ const id=offlineModel(b);
91
+ try{return {cloud:await callCloud(name,b,signal)}}
92
+ catch(e){
93
+ if(signal.aborted)throw e;
94
+ if(id&&(networkError(e)||e?.code==='sign_in_required'))return {local:await callLocal(name,b,id,signal)};
95
+ if(networkError(e))throw new QuickError('ML AI Code cannot be reached. Check the connection, or choose an offline model.',503,'offline');
96
+ throw e;
97
+ }
98
+ }
99
+
100
+ async function handle(name,req,res){
101
+ const ctl=new AbortController();open.add(ctl);
102
+ const timer=setTimeout(()=>ctl.abort(),TIMEOUT[name]||30_000);
103
+ const hangup=()=>{if(!res.writableEnded)ctl.abort()};
104
+ req.on('aborted',hangup);res.on('close',hangup);
105
+ let mine=null,key=null;
106
+ try{
107
+ const b=await readBody(req);validateQuick(name,b);
108
+ if(name==='complete'){
109
+ key=String(b.path||'')+'\0'+String(req.headers['x-mlai-editor']||'');
110
+ latest.get(key)?.ctl.abort();
111
+ mine={seq:++seq,ctl};latest.set(key,mine);
112
+ if(debounceMs>0)await new Promise(r=>setTimeout(r,debounceMs));
113
+ if(ctl.signal.aborted)return sendJson(res,200,{items:[],superseded:true});
114
+ }
115
+ const out=await route(name,b,ctl.signal);
116
+ if(out.local!==undefined){
117
+ if(name==='complete')return sendJson(res,200,{items:localCompletion(out.local,b),offline:true});
118
+ if(name==='commit-message')return sendJson(res,200,{message:unfence(out.local).trim(),offline:true});
119
+ const text=unfence(out.local);res.writeHead(200,{'Content-Type':'text/event-stream','Cache-Control':'no-store'});
120
+ res.write(sseLine('delta',{text}));res.end(sseLine('done',{text}));return;
121
+ }
122
+ const r=out.cloud;
123
+ if(name==='edit'&&r.ok&&(r.headers.get('content-type')||'').includes('text/event-stream')){
124
+ res.writeHead(200,{'Content-Type':'text/event-stream','Cache-Control':'no-store',Connection:'keep-alive'});
125
+ const reader=r.body.getReader();
126
+ try{for(;;){const {done,value}=await reader.read();if(done)break;if(res.destroyed){await reader.cancel().catch(()=>{});break}res.write(Buffer.from(value))}}
127
+ catch{if(!ctl.signal.aborted)res.write(sseLine('error',{message:'The edit stopped early. Retry.',code:'interrupted'}))}
128
+ return res.end();
129
+ }
130
+ // An older server without these routes: say so plainly instead of passing a 404 through.
131
+ if(r.status===404||r.status===405){await r.body?.cancel().catch(()=>{});return sendJson(res,501,{error:'This ML AI Code server does not offer editor suggestions yet.',code:'not_available'})}
132
+ const text=await r.text();let data;try{data=JSON.parse(text)}catch{data=null}
133
+ if(!data||typeof data!=='object')return sendJson(res,502,{error:'The server returned an unreadable response.',code:'invalid_response'});
134
+ if(!r.ok)return sendJson(res,r.status,{error:String(data.error||'Request failed'),code:typeof data.code==='string'?data.code:'http_error'});
135
+ if(name==='complete'&&mine&&latest.get(key)!==mine)return sendJson(res,200,{items:[],superseded:true});
136
+ return sendJson(res,200,data);
137
+ }catch(e){
138
+ if(ctl.signal.aborted&&!(e instanceof QuickError&&e.status===413)){if(name==='complete')return sendJson(res,200,{items:[],superseded:true});return sendJson(res,499,{error:'Cancelled.',code:'cancelled'})}
139
+ const status=e instanceof QuickError?e.status:e?.status||502;
140
+ sendJson(res,status,{error:e instanceof QuickError||e?.status?e.message:'Suggestions are not available right now.',code:e?.code||'quick_failed'});
141
+ }finally{
142
+ clearTimeout(timer);open.delete(ctl);req.off('aborted',hangup);res.off('close',hangup);
143
+ if(key&&latest.get(key)===mine)latest.delete(key);
144
+ }
145
+ }
146
+ return {handle,features:[...QUICK_ROUTES],close(){for(const c of open)c.abort();open.clear();latest.clear()}};
147
+ }