@gencode/cli 0.8.16 → 0.9.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.
@@ -0,0 +1,40 @@
1
+ import{createRequire as e}from"node:module";import{Command as t}from"commander";import{GoalConflictError as n,applyCliGoalBeforeRun as r,bootstrapMountLayout as i,buildAimaxGoalCliHelpExtra as a,buildResumeNarration as o,clearPendingUiTool as s,createBuiltinMemoryProvider as c,deleteGoal as l,ensureBootstrapMountLayout as u,exportSession as d,formatApprovalResolution as f,formatClarifyResolution as p,formatReviewResolution as m,hasBootstrapSentinel as h,initializePluginSystem as g,inspectSession as _,listAvailableSlashCommands as v,listSessionSummaries as ee,loadPendingHitl as te,loadPendingUiTool as ne,loadSessionExportSnapshots as re,loadSkillsWithPluginDirs as ie,loadTranscript as ae,normalizePluginsConfig as oe,normalizeSessionStoreName as y,readGoal as b,resolveGoalObjective as se,resolveMemoryProvider as ce,resolvePendingHitl as le,resolvePendingUiTool as ue,rewriteTranscript as de,runAgent as fe,sessionsDir as pe,transcriptPath as me,updateGoal as he,writeGoal as ge}from"@gencode/agents";import{formatTaskForDisplay as _e,isAgentDiagnosticEvent as ve,isHitlTool as ye,parseMatchedTextToResolution as be,parseTextToResolution as xe}from"@gencode/shared";import x,{existsSync as S,mkdirSync as Se,readFileSync as Ce,statSync as we,writeFileSync as Te}from"node:fs";import C,{basename as Ee,dirname as w,join as T,resolve as E}from"node:path";import De from"log4js";import D,{glob as Oe,readdir as ke}from"node:fs/promises";import Ae from"node:os";import{execFile as je,execFileSync as Me}from"node:child_process";import Ne from"gensign-node";import{fileURLToPath as Pe}from"node:url";import{addAgent as Fe,addBinding as Ie,getAgentConfig as O,listAgents as Le,listBindings as Re,loadAgentsConfig as k,normalizeAgentId as ze,removeAgent as Be,removeBindings as Ve,resolveAgentDir as He,resolveDefaultAgentId as Ue,updateAgentIdentity as We}from"@gencode/agents/config";import{createHash as Ge,randomUUID as Ke}from"node:crypto";function qe(e){let t=new Intl.DateTimeFormat(`en-CA`,{timeZone:`Asia/Shanghai`,year:`numeric`,month:`2-digit`,day:`2-digit`,hour:`2-digit`,minute:`2-digit`,second:`2-digit`,hour12:!1}).formatToParts(e);return Object.fromEntries(t.map(e=>[e.type,e.value]))}function Je(e=new Date){let t=qe(e),n=String(e.getMilliseconds()).padStart(3,`0`);return`${t.year}-${t.month}-${t.day} ${t.hour}:${t.minute}:${t.second}.${n}`}let A=function(e){return e.INFO=`INFO`,e.ERROR=`ERROR`,e.WARN=`WARN`,e}({}),j=null,M=[];function Ye(){return Je()}function Xe(e){if(!e)return``;let t=Object.entries(e).filter(([,e])=>e!==void 0).map(([e,t])=>`${e}=${Ze(t)}`);return t.length>0?` ${t.join(` `)}`:``}function Ze(e){return JSON.stringify(Qe(e,new WeakSet))??`undefined`}function Qe(e,t){return typeof e==`bigint`?e.toString():e==null||typeof e==`string`||typeof e==`number`||typeof e==`boolean`?e:e instanceof Error?{name:e.name,message:e.message,stack:e.stack}:typeof e!=`object`&&typeof e!=`function`?String(e):t.has(e)?`[Circular]`:(t.add(e),Array.isArray(e)?e.map(e=>Qe(e,t)):Object.fromEntries(Object.entries(e).filter(([,e])=>e!==void 0).map(([e,n])=>[e,Qe(n,t)])))}function $e(e){return C.join(e,`.aimax`)}function et(e){let t=e?.trim();return t&&t.replace(/[^A-Za-z0-9_.-]/g,`_`)||`no-message-${process.pid}`}function tt(e,t){let n=et(t?.messageId),r=t?.sessionStoreName?.trim()||`sessions`;return t?.sessionId?{runLogId:n,logDir:C.join($e(e),r,t.sessionId,`logs`,n)}:{runLogId:n,logDir:C.join($e(e),`logs`,n)}}function nt(e){return{appLogPath:C.join(e,`app.log`),errorLogPath:C.join(e,`errors.log`)}}function rt(e){let{appLogPath:t,errorLogPath:n}=nt(e);De.configure({appenders:{appFile:{type:`file`,filename:t,maxLogSize:5*1024*1024,backups:3,compress:!0,keepFileExt:!0,layout:{type:`messagePassThrough`}},errorFile:{type:`file`,filename:n,maxLogSize:5*1024*1024,backups:3,compress:!0,keepFileExt:!0,layout:{type:`messagePassThrough`}},errorsOnly:{type:`logLevelFilter`,appender:`errorFile`,level:`error`,maxLevel:`fatal`}},categories:{default:{appenders:[`appFile`,`errorsOnly`],level:`info`}}})}function it(e,t,n){return`[${Ye()}] [${e}] ${t}${Xe(n)}`}function at(e,t,n){try{process.stderr.write(`${it(e,t,n)}\n`)}catch{}}function ot(e,t,n){if(!j){M.push({level:e,message:t,context:n}),M.length>200&&(M=M.slice(-200)),at(e,t,n);return}let r=it(e,t,n),i=De.getLogger();if(e===A.ERROR){i.error(r);return}if(e===A.WARN){i.warn(r);return}i.info(r)}function N(e,t){let{logDir:n,runLogId:r}=tt(e,t);if(j?.dataDir===e&&j.runLogId===r&&j.logDir===n)return;x.mkdirSync(n,{recursive:!0}),rt(n),j={dataDir:e,logDir:n,runLogId:r};let i=M;M=[];for(let e of i)ot(e.level,e.message,e.context)}function st(){return j?{...j}:null}async function ct(){await De.shutdown()}const P={info:(e,t)=>ot(A.INFO,e,t),warn:(e,t)=>ot(A.WARN,e,t),error:(e,t)=>ot(A.ERROR,e,t)};var lt=class{startTime;name;constructor(e){this.name=e,this.startTime=Date.now()}end(){let e=Date.now()-this.startTime;return P.info(`${this.name} completed in ${e}ms`),e}};function ut(e){if(typeof e==`string`)return e;let t=JSON.stringify(e);return t===void 0?String(e):t}const dt=new Set;function ft(e){return e instanceof Error?{name:e.name,message:e.message,code:e.code}:{message:String(e)}}function pt(e,t){try{P.warn(`CLI ${e} write failed; suppressing further ${e} writes`,{stream:e,...ft(t)})}catch{}}function mt(e,t){if(dt.has(e))return;let n=e===`stdout`?process.stdout:process.stderr;try{n.write(t)}catch(t){dt.add(e),pt(e,t)}}function ht(e){mt(`stdout`,e)}function F(e){mt(`stdout`,e+`
2
+ `)}function I(e){mt(`stderr`,e+`
3
+ `)}function L(e){switch(e.type){case`start`:F(`\n[start] ${e.message}`);break;case`text`:ht(e.text);break;case`stream_text_delta`:ht(e.text);break;case`bootstrap`:F(`\n[bootstrap:${e.phase}] ${e.dataDir}`);break;case`session_reset`:F(`\n[session:${e.action}] ${e.message}`);break;case`tool_start`:F(`\n[tool:${e.name}] ${JSON.stringify(e.input??{})}`);break;case`tool_end`:{let t=ut(e.output);F(`[tool:${e.name}] ${e.isError?`ERROR`:`OK`} ${t.slice(0,200)}`);break}case`compaction`:F(`\n[compaction${e.layer?`:${e.layer}`:``}] ${e.reason}${e.strategy?` (${e.strategy})`:``}`);break;case`skill_used`:F(`\n[skill] ${e.skillName} agent=${e.agent} task=${e.taskId}`);break;case`custom`:F(`\n[plugin:${e.pluginId}] ${e.name}${e.label?` ${e.label}`:``}${e.data?` ${JSON.stringify(e.data)}`:``}`);break;case`error`:I(`\n[error] ${e.message}`);break;case`diagnostic`:break;case`subagent_spawn`:F(`\n[subagent:spawn]${e.label?` "${e.label}"`:``} ${_e(e.task)}`);break;case`subagent_complete`:F(`[subagent:${e.status}] ${_e(e.task)}`);break;case`hitl_requested`:if(F(`\n[hitl:${e.request.kind}] ${e.request.title}`),F(` prompt: ${e.request.prompt}`),F(` requestId: ${e.request.requestId}`),e.request.input.mode===`choice`)for(let t of e.request.input.choices)F(` - [${t.id}] ${t.label}`);break;case`hitl_resumed`:F(`\n[hitl:resumed] requestId=${e.requestId} action=${e.resolution.action}`);break;case`hitl_expired`:F(`\n[hitl:expired] requestId=${e.requestId} reason=${e.reason}`);break;case`hitl_cancelled`:F(`\n[hitl:cancelled] requestId=${e.requestId}${e.reason?` reason=${e.reason}`:``}`);break;case`ui_tool_request`:F(`\n[ui-tool:${e.request.toolName}] ${e.request.outputSchema.title}`),F(` requestId: ${e.request.requestId}`),F(` toolCallId: ${e.request.toolCallId}`);break;case`ui_tool_result`:F(`\n[ui-tool:resumed] requestId=${e.requestId} tool=${e.toolName}`);break}}function gt(e,t){if(t===`json`){e&&F(JSON.stringify(e,null,2));return}F(`
4
+ `),F(`session: ${e.sessionId}`),F(`duration: ${e.durationMs}ms`),F(`tokens: input=${e.usage.input} output=${e.usage.output} total=${e.usage.total}`),e.context&&(F(`context: ${e.context.snapshotPath}`),F(`tool-results: ${e.context.toolResultsDir}`)),e.error&&I(`error: ${e.error}`)}function _t(e,t){if(t===`json`){F(JSON.stringify(e,null,2));return}if(e.length===0){F(`No sessions found.`);return}for(let t of e){let e=t.createdAt?new Date(t.createdAt).toLocaleString():`unknown`,n=t.goalStatus?` [GOAL:${t.goalStatus}]`:` [GOAL:—]`;F(`${t.id} ${e} [${t.channel}]${n} ${t.title}`)}}function vt(e,t){if(t===`json`){F(JSON.stringify(e,null,2));return}F(`session: ${e.id}`),e.metadata&&(F(`title: ${e.metadata.title}`),F(`channel: ${e.metadata.channel}`),F(`created: ${e.metadata.createdAt}`),F(`updated: ${e.metadata.updatedAt}`)),F(`transcript: ${e.transcriptPath}`),F(`entries: ${e.transcriptEntryCount}`),F(`context: ${e.contextSnapshotPath}`),F(`session-memory: ${e.sessionMemoryPath}`),F(`collapse-log: ${e.collapseLogPath}`),F(`read-states: ${e.readStateCount}`),F(`tool-results: ${e.toolResultRefCount}`),F(`tool-results-dir: ${e.toolResultsDir}`)}function yt(e,t){if(t===`json`){F(JSON.stringify(e,null,2));return}F(`session: ${e.id}`),F(`transcript: ${e.paths.transcriptPath}`),F(`context: ${e.paths.contextSnapshotPath}`),F(`session-memory: ${e.paths.sessionMemoryPath}`),F(`collapse-log: ${e.paths.collapseLogPath}`),F(`tool-results-dir: ${e.paths.toolResultsDir}`),F(`transcript-entries: ${e.transcript.length}`),F(`read-states: ${e.context.readStates.length}`),F(`tool-results: ${e.context.toolResults.length}`),F(`collapse-spans: ${e.context.compaction.collapseSpans.length}`)}function bt(e,t){if(t===`json`){F(JSON.stringify(e,null,2));return}F(`Bootstrap completed.`),F(`dataDir: ${e.dataDir}`),F(`created dirs: ${e.createdDirs.length}`),F(`created files: ${e.createdFiles.length}`),F(`skipped dirs: ${e.skippedDirs.length}`),F(`skipped files: ${e.skippedFiles.length}`)}const xt=`.aimax`,St=`${xt}.pre-gocryptfs.`,Ct=[`app.log`,`errors.log`],wt=[`logs`],Tt=[`fusermount3`,`fusermount`,`umount`],Et=[{command:`fusermount3`,args:[`-u`],label:`fusermount3 -u`},{command:`fusermount3`,args:[`-uz`],label:`fusermount3 -uz`},{command:`fusermount`,args:[`-u`],label:`fusermount -u`},{command:`fusermount`,args:[`-uz`],label:`fusermount -uz`},{command:`umount`,args:[],label:`umount`},{command:`umount`,args:[`-l`],label:`umount -l`}];function Dt(e){return C.join(e,`.aimax.enc`)}function Ot(e){return C.join(e,xt)}function kt(e){return C.join(e,`${St}${process.pid}.${Date.now()}`)}function At(e){return C.join(e,`${xt}.plaintext-restore.${process.pid}.${Date.now()}`)}function jt(e){return e.replace(/\\([0-7]{3})/g,(e,t)=>String.fromCharCode(Number.parseInt(t,8)))}async function R(e){try{let t=await Pt.read(),n=C.resolve(e);for(let e of t.split(`
5
+ `)){if(!e.trim())continue;let t=e.split(` `);if(t.length>=5&&C.resolve(jt(t[4]??``))===n)return!0}return!1}catch{return!1}}function Mt(e){try{let t=x.readFileSync(`/proc/self/mountinfo`,`utf-8`),n=C.resolve(e);for(let e of t.split(`
6
+ `)){if(!e.trim())continue;let t=e.split(` `);if(t.length>=5&&C.resolve(jt(t[4]??``))===n)return!0}return!1}catch{return!1}}async function Nt(){return await D.readFile(`/proc/self/mountinfo`,`utf-8`)}const Pt={read:Nt};async function z(e){try{return await D.stat(e),!0}catch(e){if(e.code===`ENOENT`)return!1;throw e}}async function B(e){await D.mkdir(e,{recursive:!0})}async function Ft(e){if(await B(e),(await D.readdir(e)).length>0)throw Error(`Refusing to mount encrypted .aimax over non-empty directory: ${e}. Clear stale plaintext files or disable --encrypt-sessions for this run.`)}async function It(e){return await z(e)?(await D.readdir(e)).length===0:!0}async function V(e){if(await z(e))return(await D.readdir(e)).length}async function Lt(e){if(!await z(e))return!1;let t=await D.readdir(e,{withFileTypes:!0});for(let e of t)if(!(e.isDirectory()&&wt.includes(e.name))&&!(e.isFile()&&Ct.some(t=>e.name.startsWith(t))))return!0;return!1}async function Rt(e){let t=await D.readdir(e,{withFileTypes:!0});for(let n of t){if(n.isDirectory()&&wt.includes(n.name)){await D.rm(C.join(e,n.name),{recursive:!0,force:!0}).catch(()=>{});continue}n.isFile()&&Ct.some(e=>n.name.startsWith(e))&&await D.rm(C.join(e,n.name),{force:!0}).catch(()=>{})}}async function zt(e){if(!await z(C.join(e,`gocryptfs.conf`)))throw Error(`Encrypted .aimax directory exists but is not initialized: ${e}. Remove the incomplete .aimax.enc directory or restore it from backup before retrying --encrypt-sessions.`)}async function Bt(e){return(await D.readdir(e,{withFileTypes:!0})).filter(e=>e.isDirectory()&&e.name.startsWith(St)).map(t=>C.join(e,t.name)).sort()}async function Vt(e,t){if(!await z(t))return null;let n=kt(e);return P.info(`--encrypt-sessions moving plaintext .aimax aside before migration`,{plainDir:t,backupDir:n}),await D.rename(t,n),n}async function H(e,t,n){P.info(`--encrypt-sessions copying plaintext backup into encrypted mount`,{backupDir:e,plainDir:t,force:n}),await D.cp(e,t,{recursive:!0,force:n})}async function Ht(e,t){if(e.length===0)return[];await B(t);let n=[];for(let r of e)await z(r)&&(await H(r,t,!0),await D.rm(r,{recursive:!0,force:!0}),P.info(`--encrypt-sessions restored interrupted plaintext backup after failed migration`,{backupDir:r,plainDir:t}),n.push(r));return n}async function Ut(e){let t=Dt(e),n=Ot(e),r=await z(t);if(!r)return;let i=r&&await z(C.join(t,`gocryptfs.conf`)),a=await z(n),o=a?await R(n):!1,s=await It(n);if(P.info(`plaintext .aimax restore check for disabled --encrypt-sessions`,{dataDir:e,encryptedDir:t,plainDir:n,encryptedDirExists:r,encryptedDirInitialized:i,plainDirExists:a,plainDirMountPoint:o,plainDirEntryCount:await V(n)}),!i){P.warn(`plaintext .aimax restore skipped because .aimax.enc is not initialized`,{encryptedDir:t,plainDir:n}),await B(n);return}if(o){P.warn(`plaintext .aimax restore skipped because .aimax is already a mount point`,{plainDir:n,encryptedDir:t});return}if(await Lt(n)){P.info(`plaintext .aimax restore skipped because plaintext .aimax already contains data`,{plainDir:n,encryptedDir:t,plainDirEntryCount:await V(n)});return}s||(P.info(`plaintext .aimax clearing ephemeral files before restore`,{plainDir:n,encryptedDir:t,plainDirEntryCount:await V(n)}),await Rt(n));try{await Kt()}catch(e){P.warn(`plaintext .aimax restore unavailable; continuing with plaintext .aimax`,{error:e instanceof Error?e.message:String(e),plainDir:n,encryptedDir:t}),await B(n);return}let c=At(e),l=!1;try{await Ft(n),await qt(t,n),l=!0,await D.rm(c,{recursive:!0,force:!0}),await B(c),await D.cp(n,c,{recursive:!0,force:!0}),P.info(`plaintext .aimax restore copied encrypted history to staging directory`,{plainDir:n,encryptedDir:t,restoreDir:c,restoredEntryCount:await V(c)}),await Yt(n),l=!1,await D.rm(n,{recursive:!0,force:!0}),await D.rename(c,n),P.info(`plaintext .aimax restore completed for disabled --encrypt-sessions`,{plainDir:n,encryptedDir:t})}catch(e){l&&await Yt(n).catch(e=>{P.warn(`plaintext .aimax restore failed to unmount during rollback`,{error:e instanceof Error?e.message:String(e),plainDir:n,encryptedDir:t})}),await D.rm(c,{recursive:!0,force:!0}).catch(()=>{}),await B(n).catch(()=>{}),P.warn(`plaintext .aimax restore failed; continuing with existing plaintext .aimax`,{error:e instanceof Error?e.message:String(e),plainDir:n,encryptedDir:t})}}async function Wt(e){let t=C.join(Ae.tmpdir(),`aimax-gocryptfs-pass.${process.pid}.${Date.now()}`);await D.writeFile(t,`wizard_aimax@2026
7
+ `,{mode:384});try{return await e(t)}finally{await D.rm(t,{force:!0})}}async function Gt(e){let t=e.includes(`-init`)?`init`:`mount`;P.info(`--encrypt-sessions starting gocryptfs ${t}`);try{await $t(`gocryptfs`,e),P.info(`--encrypt-sessions completed gocryptfs ${t}`)}catch(n){let r=n instanceof Error?n.message:String(n);throw P.warn(`--encrypt-sessions failed gocryptfs ${t}`,{error:r}),Error(`gocryptfs ${e.join(` `)} failed: ${r}`)}}async function Kt(){P.info(`--encrypt-sessions checking encryption dependencies`),await tn(`gocryptfs`,`required for --encrypt-sessions`),P.info(`--encrypt-sessions dependency available`,{command:`gocryptfs`});for(let e of Tt)if(await en(e)){P.info(`--encrypt-sessions unmount dependency available`,{command:e});return}throw Error(`--encrypt-sessions requires one of ${Tt.join(`, `)} to be installed for secure unmount cleanup.`)}async function qt(e,t){P.info(`--encrypt-sessions mounting encrypted .aimax`,{encryptedDir:e,plainDir:t}),await Wt(async n=>{await Gt([`-q`,`-passfile`,n,e,t])})}async function Jt(e){P.info(`--encrypt-sessions initializing encrypted .aimax directory`,{encryptedDir:e}),await Wt(async t=>{await Gt([`-q`,`-init`,`-passfile`,t,e])})}async function Yt(e){let t=[];P.info(`--encrypt-sessions unmounting encrypted .aimax`,{plainDir:e});for(let n of Et)try{if(P.info(`--encrypt-sessions attempting encrypted .aimax unmount`,{command:n.label,plainDir:e}),await $t(n.command,[...n.args,e]),!await R(e)){P.info(`--encrypt-sessions unmounted encrypted .aimax`,{command:n.label,plainDir:e});return}let r=`${n.label} returned success but ${e} is still mounted`;t.push(r),P.warn(`--encrypt-sessions unmount command returned success but mountpoint remains`,{command:n.label,plainDir:e})}catch(r){let i=nn(n.label,r);t.push(i),P.warn(`--encrypt-sessions unmount attempt failed`,{command:n.label,plainDir:e,error:r instanceof Error?r.message:String(r)})}throw Error(`Failed to unmount ${e}: ${t.join(`; `)}`)}function Xt(e){for(let t of Et)try{if(Me(t.command,[...t.args,e],{stdio:`ignore`}),!Mt(e))return}catch{}}function Zt(e){let t=!1,n=()=>{t||(t=!0,Xt(e))},r=()=>n(),i=()=>{n(),process.exit(130)},a=()=>{n(),process.exit(143)};return process.once(`exit`,r),process.once(`SIGINT`,i),process.once(`SIGTERM`,a),{release(){process.removeListener(`exit`,r),process.removeListener(`SIGINT`,i),process.removeListener(`SIGTERM`,a)}}}async function Qt(e){let t=Dt(e),n=Ot(e),r=await z(n),i=await z(t),a=i&&await z(C.join(t,`gocryptfs.conf`)),o=r?await R(n):!1;P.info(`--encrypt-sessions preparing encrypted .aimax mount`,{dataDir:e,encryptedDir:t,plainDir:n,plainDirExists:r,encryptedDirExists:i,encryptedDirInitialized:a,plainDirMountPoint:o,plainDirEntryCount:await V(n),encryptedDirEntryCount:await V(t)});let s=()=>({mounted:!1,plainDir:n,encryptedDir:t,async cleanup(){}});try{await Kt()}catch(e){return P.warn(`--encrypt-sessions is unavailable; continuing with plaintext .aimax`,{error:e instanceof Error?e.message:String(e)}),await B(n),s()}if(await R(n))return P.warn(`--encrypt-sessions found existing .aimax mount; continuing without taking ownership`,{plainDir:n}),s();let c=null,l=[],u=null,d=null,f=!1;try{if(l=await Bt(e),l.length>0&&P.warn(`--encrypt-sessions found interrupted pre-gocryptfs migration backups`,{count:l.length}),!await z(t))if(P.info(`--encrypt-sessions encrypted .aimax directory does not exist; starting first-time migration`,{encryptedDir:t,plainDir:n,resumedBackupCount:l.length}),l.length>0?u=await Vt(e,n):c=await Vt(e,n),await B(t),await Jt(t),await B(n),await qt(t,n),f=!0,d=Zt(n),P.info(`--encrypt-sessions mounted encrypted .aimax`,{plainDir:n,encryptedDir:t}),l.length>0){for(let e of l)await H(e,n,!0);u&&=(await H(u,n,!1),await D.rm(u,{recursive:!0,force:!0}),P.info(`--encrypt-sessions removed residual plaintext backup after migration`,{backupDir:u}),null);for(let e of l)await D.rm(e,{recursive:!0,force:!0}),P.info(`--encrypt-sessions removed resumed plaintext migration backup`,{backupDir:e});l=[]}else c&&=(await H(c,n,!0),await D.rm(c,{recursive:!0,force:!0}),P.info(`--encrypt-sessions removed plaintext migration backup`,{backupDir:c}),null);else if(P.info(`--encrypt-sessions encrypted .aimax directory exists; mounting existing encrypted data`,{encryptedDir:t,plainDir:n,resumedBackupCount:l.length,plainDirEntryCount:await V(n)}),await zt(t),l.length>0?u=await Vt(e,n):await It(n)||(c=await Vt(e,n)),await Ft(n),await qt(t,n),f=!0,d=Zt(n),P.info(`--encrypt-sessions mounted encrypted .aimax`,{plainDir:n,encryptedDir:t}),l.length>0){for(let e of l)await H(e,n,!0);u&&=(await H(u,n,!1),await D.rm(u,{recursive:!0,force:!0}),P.info(`--encrypt-sessions removed residual plaintext backup after migration`,{backupDir:u}),null);for(let e of l)await D.rm(e,{recursive:!0,force:!0}),P.info(`--encrypt-sessions removed resumed plaintext migration backup`,{backupDir:e});l=[]}else c&&=(await H(c,n,!0),await D.rm(c,{recursive:!0,force:!0}),P.info(`--encrypt-sessions removed plaintext migration backup`,{backupDir:c}),null);return P.info(`--encrypt-sessions prepared encrypted .aimax mount result`,{mounted:f,plainDir:n,encryptedDir:t,plainDirMountPoint:await R(n),plainDirEntryCount:await V(n),encryptedDirEntryCount:await V(t)}),{mounted:f,plainDir:n,encryptedDir:t,async cleanup(){if(d?.release(),!f){P.info(`--encrypt-sessions cleanup skipped because encrypted .aimax is not mounted`,{plainDir:n,encryptedDir:t});return}f=!1,P.info(`--encrypt-sessions cleanup started`,{plainDir:n,encryptedDir:t});try{await Yt(n),P.info(`--encrypt-sessions cleanup completed`,{plainDir:n,encryptedDir:t})}catch(e){P.warn(`--encrypt-sessions failed to unmount encrypted .aimax; continuing shutdown`,{error:e instanceof Error?e.message:String(e)})}}}}catch(e){d?.release(),f&&await Yt(n).catch(e=>{P.warn(`--encrypt-sessions failed to unmount during mount rollback`,{error:e instanceof Error?e.message:String(e),plainDir:n,encryptedDir:t})}),c&&await z(c)&&await It(n)&&(await D.rm(n,{recursive:!0,force:!0}).catch(()=>{}),await D.rename(c,n).catch(()=>{}),P.info(`--encrypt-sessions restored plaintext backup after failed migration`,{backupDir:c,plainDir:n})),u&&await z(u)&&await It(n)&&(await D.rm(n,{recursive:!0,force:!0}).catch(()=>{}),await D.rename(u,n).catch(()=>{}),P.info(`--encrypt-sessions restored residual plaintext backup after failed migration`,{backupDir:u,plainDir:n}));let r=await Ht(l,n).catch(e=>(P.warn(`--encrypt-sessions failed to restore interrupted plaintext backups after failed migration`,{error:e instanceof Error?e.message:String(e),plainDir:n,backupCount:l.length}),[]));return l=l.filter(e=>!r.includes(e)),P.warn(`--encrypt-sessions failed; continuing with plaintext .aimax`,{error:e instanceof Error?e.message:String(e),mountedBeforeFailure:f,plainDir:n,encryptedDir:t,backupDir:c,residualPlainBackupDir:u,resumedBackupCount:l.length}),await B(n),s()}}async function U(e,t,n){if(!t.encryptSessions)return await Ut(e),n();P.info(`--encrypt-sessions wrapper starting encrypted .aimax preparation`,{dataDir:e});let r=await Qt(e);P.info(`--encrypt-sessions wrapper prepared encrypted .aimax state`,{dataDir:e,mounted:r.mounted,plainDir:r.plainDir,encryptedDir:r.encryptedDir});try{return P.info(`--encrypt-sessions wrapper entering protected operation`,{dataDir:e,mounted:r.mounted}),await n(r)}finally{P.info(`--encrypt-sessions wrapper starting cleanup`,{dataDir:e,mounted:r.mounted,plainDir:r.plainDir,encryptedDir:r.encryptedDir}),await r.cleanup(),P.info(`--encrypt-sessions wrapper finished cleanup`,{dataDir:e,mounted:r.mounted,plainDir:r.plainDir,encryptedDir:r.encryptedDir})}}async function $t(e,t){await new Promise((n,r)=>{je(e,t,{encoding:`utf-8`},e=>{if(e){r(e);return}n()})})}async function en(e){try{return await $t(`sh`,[`-c`,`command -v "${e}" >/dev/null 2>&1`]),!0}catch{return!1}}async function tn(e,t){if(!await en(e))throw Error(`--encrypt-sessions requires ${e}: ${t}.`)}function nn(e,t){return`${e} failed: ${t instanceof Error?t.message:String(t)}`}function rn(e){e.command(`bootstrap`).description(`Initialize the data directory with required directories and templates`).requiredOption(`-d, --data-dir <path>`,`Data directory path`).option(`--output <format>`,`Output format: text (default) or json`,`text`).option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`).action(async e=>{let t=e.output===`json`?`json`:`text`;try{bt(await U(e.dataDir,e,async()=>i(e.dataDir)),t)}catch(e){I(`Error bootstrapping data directory: ${e.message}`),process.exit(1)}})}function an(e){return Ne.sm4_encrypt_ecb(e)}function on(e){return an(e)}const sn=[`minimal`,`low`,`medium`,`high`,`xhigh`];function cn(e,t){if(e===void 0)return;let n=e.trim().toLowerCase();if(n){if(sn.includes(n))return n;throw Error(`Invalid ${t}: ${e}. Must be one of ${sn.join(`, `)}.`)}}function ln(e={}){let t=un(e.apiFormat??process.env.AIMAX_API_FORMAT),n=e.baseUrl??process.env.AIMAX_BASE_URL,r=process.env.AIMAX_AUTH_TOKEN,i=e.authToken?on(e.authToken):r?on(r):e.apiKey??process.env.AIMAX_API_KEY,a=e.model??process.env.AIMAX_MODEL,o=[];if(n||o.push(`--base-url / AIMAX_BASE_URL`),i||o.push(`--api-key / AIMAX_API_KEY`),a||o.push(`--model / AIMAX_MODEL`),o.length>0)throw Error(`Missing required LLM configuration: ${o.join(`, `)}`);return{apiFormat:t,baseUrl:n,apiKey:i,model:a,contextWindow:e.contextWindow??(process.env.AIMAX_CONTEXT_WINDOW?Number(process.env.AIMAX_CONTEXT_WINDOW):void 0),flashModel:e.flashModel??process.env.AIMAX_FLASH_MODEL,thinking:e.thinking===void 0?cn(process.env.AIMAX_THINKING,`AIMAX_THINKING`):cn(e.thinking,`--thinking`)}}function un(e){if(!e||e===`openai-completions`||e===`anthropic-messages`)return e;throw Error(`Unsupported LLM API format: ${e}`)}function dn(e,t){return t?C.resolve(t):C.join(e,`.aimax`,`plugins.json`)}async function W(e,t){let n=dn(e,t);try{let e=await D.readFile(n,`utf-8`);return JSON.parse(e)}catch(e){if(e.code===`ENOENT`)return;throw e}}async function G(e,t,n){let r=dn(e,n);await D.mkdir(C.dirname(r),{recursive:!0}),await D.writeFile(r,JSON.stringify(t,null,2),`utf-8`)}function K(e){return typeof e==`object`&&!!e}function fn(e){return K(e)&&e.type===`text`&&typeof e.text==`string`&&(e.textSignature===void 0||typeof e.textSignature==`string`)}function pn(e){return K(e)&&e.type===`image`&&typeof e.data==`string`&&typeof e.mimeType==`string`}function mn(e){return K(e)&&e.type===`thinking`&&typeof e.thinking==`string`&&(e.thinkingSignature===void 0||typeof e.thinkingSignature==`string`)&&(e.redacted===void 0||typeof e.redacted==`boolean`)}function hn(e){return K(e)&&e.type===`toolCall`&&typeof e.id==`string`&&typeof e.name==`string`&&K(e.arguments)&&(e.thoughtSignature===void 0||typeof e.thoughtSignature==`string`)}function gn(e){return typeof e==`string`||Array.isArray(e)&&e.every(e=>fn(e)||pn(e))}function _n(e){return Array.isArray(e)&&e.every(e=>fn(e)||mn(e)||hn(e))}function vn(e){return K(e)&&typeof e.input==`number`&&typeof e.output==`number`&&typeof e.cacheRead==`number`&&typeof e.cacheWrite==`number`&&typeof e.totalTokens==`number`&&K(e.cost)&&typeof e.cost.input==`number`&&typeof e.cost.output==`number`&&typeof e.cost.cacheRead==`number`&&typeof e.cost.cacheWrite==`number`&&typeof e.cost.total==`number`}function yn(e){return!K(e)||typeof e.role!=`string`?!1:e.role===`user`?gn(e.content):e.role===`assistant`?_n(e.content)&&typeof e.api==`string`&&typeof e.provider==`string`&&typeof e.model==`string`&&vn(e.usage)&&(e.stopReason===`stop`||e.stopReason===`length`||e.stopReason===`toolUse`||e.stopReason===`error`||e.stopReason===`aborted`)&&(e.errorMessage===void 0||typeof e.errorMessage==`string`):e.role===`toolResult`?typeof e.toolCallId==`string`&&typeof e.toolName==`string`&&Array.isArray(e.content)&&e.content.every(e=>fn(e)||pn(e))&&typeof e.isError==`boolean`:!1}async function bn(e){let t;try{t=await D.readFile(e,`utf-8`)}catch(e){throw Error(`Failed to read message file: ${e.message}`)}let n;try{n=JSON.parse(t)}catch(e){throw Error(`Invalid JSON in message file: ${e.message}`)}let r=Array.isArray(n)?n:[n];if(r.length===0||!r.every(e=>yn(e)))throw Error(`Message file must contain one Message object or an array of Message objects`);return r}async function xn(e){if(!!e.message==!!e.fromFile)throw Error(`Exactly one of --message or --from-file must be provided`);return e.fromFile?{kind:`messages`,messages:await bn(e.fromFile)}:{kind:`text`,message:e.message}}var Sn=class{constructor(e){this.sinks=e}async send(e){await Promise.allSettled(this.sinks.map(async t=>{await t.send(e)}))}async close(){await Promise.allSettled(this.sinks.map(async e=>{await e.close()}))}},Cn=class{constructor(e){this.url=e}async send(e){await this.post(this.toPayload(e))}async close(){}async post(e){let t=Date.now(),n=wn(e);P.info(`sending callback event`,{callbackUrl:this.url,...n});let r=new AbortController,i=!1,a=setTimeout(()=>{i=!0,r.abort()},5e3);try{let a=await fetch(this.url,{method:`POST`,headers:{"content-type":`application/json`},body:JSON.stringify(e),signal:r.signal}),o=Date.now()-t;if(!a.ok){P.error(`callback event failed`,{callbackUrl:this.url,httpStatus:a.status,durationMs:o,timedOut:i,...n});return}P.info(`callback event delivered`,{callbackUrl:this.url,httpStatus:a.status,durationMs:o,...n})}catch(e){let r=Date.now()-t;P.error(`callback event failed`,{callbackUrl:this.url,error:e instanceof Error?e.message:String(e),durationMs:r,timedOut:i,...n})}finally{clearTimeout(a)}}toPayload(e){switch(e.type){case`start`:return e;case`progress`:return e;case`done`:return e;case`error`:return e;case`session_reset`:return e;case`hitl`:return e;case`title_updated`:return e;default:{let t=e;throw Error(`Unsupported callback event: ${JSON.stringify(t)}`)}}}};function wn(e){return{eventType:e.type,sessionId:e.sessionId,messageId:e.messageId,channel:e.channel,user:e.user,progressType:e.type===`progress`?e.event.type:void 0}}function Tn(e,t){if(t.kind===`none`)return e;let n=new URL(e);return n.searchParams.set(`authToken`,t.token),n.toString()}const En=5e3,Dn=5e3;function On(){let e=globalThis.WebSocket;if(!e)throw Error(`WebSocket is not available in this runtime`);return e}function kn(e){return e?{kind:`query_token`,token:e}:{kind:`none`}}function An(e,t,n){if(typeof e.once==`function`){e.once(t,n);return}if(typeof e.addEventListener==`function`&&typeof e.removeEventListener==`function`){let r=(...i)=>{e.removeEventListener?.(t,r),n(...i)};e.addEventListener(t,r);return}throw Error(`WebSocket does not support one-time listener registration for "${t}"`)}function jn(e,t,n){if(typeof e.on==`function`){e.on(t,n);return}if(typeof e.addEventListener==`function`){e.addEventListener(t,n);return}throw Error(`WebSocket does not support listener registration for "${t}"`)}var Mn=class{socket=null;connectPromise=null;enabled=!0;textSequence=0;constructor(e,t,n){this.url=e,this.events=t,this.authToken=n}async send(e){let t=this.toEnvelope(e);if(!(!t||!this.enabled))try{await Nn(this.ensureConnected(),En,`WebSocket connection timed out after ${En}ms`);let e=On();if(!this.socket||this.socket.readyState!==e.OPEN)return;this.socket.send(JSON.stringify(t))}catch(e){P.warn(`websocket sink disabled after send failure`,{url:this.url,error:e instanceof Error?e.message:String(e)}),this.enabled=!1}}async sendTextDelta(e){if(!this.enabled||!this.events.has(`text`))return;this.textSequence+=1;let t={type:`text`,sessionId:e.sessionId,messageId:e.messageId,channel:e.channel,user:e.user,timestamp:new Date().toISOString(),sequence:this.textSequence,delta:e.text};try{await Nn(this.ensureConnected(),En,`WebSocket connection timed out after ${En}ms`);let e=On();if(!this.socket||this.socket.readyState!==e.OPEN)return;this.socket.send(JSON.stringify(t))}catch(e){P.warn(`websocket sink disabled after text delta send failure`,{url:this.url,error:e instanceof Error?e.message:String(e)}),this.enabled=!1}}async close(){this.enabled=!1;let e=this.socket;if(this.socket=null,this.connectPromise=null,!e)return;let t=On();if(e.readyState!==t.CLOSED)try{await Nn(new Promise(t=>{An(e,`close`,()=>t()),e.close()}),Dn,`WebSocket close timed out after ${Dn}ms`)}catch(e){P.warn(`websocket sink close timed out`,{url:this.url,error:e instanceof Error?e.message:String(e)})}}async ensureConnected(){if(!this.enabled)return;let e=On();if(this.socket&&this.socket.readyState===e.OPEN)return;if(this.connectPromise){await this.connectPromise;return}let t=new e(Tn(this.url,kn(this.authToken)));this.connectPromise=new Promise((e,n)=>{An(t,`open`,()=>{this.socket=t,jn(t,`close`,()=>{this.socket===t&&(this.socket=null)}),e()}),An(t,`error`,e=>{n(e)})});try{await this.connectPromise}finally{this.connectPromise=null}}toEnvelope(e){let t=new Date().toISOString();switch(e.type){case`start`:return this.events.has(`start`)?{...e,timestamp:t}:null;case`done`:return this.events.has(`done`)?{...e,timestamp:t}:null;case`error`:return this.events.has(`error`)?{...e,timestamp:t}:null;case`hitl`:return this.events.has(`hitl`)?{...e,timestamp:t}:null;case`title_updated`:return this.events.has(`title_updated`)?{type:`title_updated`,sessionId:e.sessionId,messageId:e.messageId,channel:e.channel,user:e.user,timestamp:t,title:e.title}:null;case`session_reset`:return null;case`progress`:return e.event.type===`text`?this.events.has(`text`)?(this.textSequence+=1,{type:`text`,sessionId:e.sessionId,messageId:e.messageId,channel:e.channel,user:e.user,timestamp:t,sequence:this.textSequence,delta:e.event.text}):null:e.event.type===`stream_text_delta`||!this.events.has(`progress`)?null:{type:`progress`,sessionId:e.sessionId,messageId:e.messageId,channel:e.channel,user:e.user,timestamp:t,event:e.event};default:{let t=e;throw Error(`Unsupported stream event: ${JSON.stringify(t)}`)}}}};function Nn(e,t,n){return new Promise((r,i)=>{let a=setTimeout(()=>i(Error(n)),t);e.then(e=>{clearTimeout(a),r(e)},e=>{clearTimeout(a),i(e)})})}function Pn(e){let t=e.trim();if(!t)throw Error(`Goal objective cannot be empty`);return t}function Fn(e,t){if(t.action===`unchanged`)return null;if(t.action===`created`||t.action===`replaced`)return t.action;if(e?.status!==t.goal.status)switch(t.goal.status){case`active`:return e?.status===`paused`?`resumed`:`updated`;case`paused`:return`paused`;case`complete`:return`completed`;case`budget_limited`:return`budget_limited`;default:return`updated`}return`updated`}function In(e){let t=Fn(e.before,e.result);return t?{type:`goal_updated`,sessionId:e.sessionId,action:t,goalId:e.result.goal.goalId,goalStatus:e.result.goal.status}:null}function Ln(e){let t=e?.packageRoot??Rn(),n=C.join(t,`models`),r=C.join(n,...`Xenova/bge-small-zh-v1.5`.split(`/`));if(x.existsSync(C.join(r,`config.json`)))return n}function Rn(){let e=C.dirname(Pe(import.meta.url));return C.basename(e)===`src`||C.basename(e)===`dist`?C.resolve(e,`..`):e}const zn=[`start`,`text`,`done`,`error`,`hitl`],Bn=[`off`,`gate`,`dry_run`,`write`];function Vn(e){if(!e?.trim())return new Set(zn);let t=e.split(`,`).map(e=>e.trim().toLowerCase()).filter(Boolean),n=new Set([`start`,`text`,`progress`,`done`,`error`,`hitl`,`title_updated`]),r=new Set;for(let e of t){if(!n.has(e))throw Error(`Invalid stream event: ${e}. Must be one of start,text,progress,done,error,hitl`);r.add(e)}return r.size>0?r:new Set(zn)}function Hn(e){if(e.disableTopicSegmentation===!0||process.env.AIMAX_DISABLE_TOPIC_SEGMENTATION===`true`)return{enabled:!1};let t=Ln();return t?{embeddingModelDir:t}:void 0}function Un(e){let t=e.dataDir??process.env.AIMAX_DATA_DIR;if(!t)throw Error(`Data directory must be specified via --data-dir or AIMAX_DATA_DIR`);return t}function Wn(e){return e?.trim()||void 0}function Gn(e){let t=(e.systemAgentsDir??process.env.AIMAX_SYSTEM_AGENTS_DIR)?.trim();if(t){if(!C.isAbsolute(t)){let n=e.systemAgentsDir===void 0?`AIMAX_SYSTEM_AGENTS_DIR`:`--system-agents-dir`;throw Error(`Invalid ${n}: ${t}. Path must be absolute.`)}return t}}function Kn(e){return e?y(e):void 0}function qn(e){if(!e?.trim())return[];let t=e.split(`,`).map(e=>e.trim()).filter(Boolean),n=new Set,r=[];for(let e of t){if(!C.isAbsolute(e))throw Error(`Invalid --skillsLoadPaths entry: ${e}. Each path must be absolute.`);n.has(e)||(n.add(e),r.push(e))}return r}function Jn(e,t){if(e===void 0)return;let n=e.trim().toLowerCase();if(n){if([`true`,`1`,`yes`,`on`].includes(n))return!0;if([`false`,`0`,`no`,`off`].includes(n))return!1;throw Error(`Invalid ${t}: ${e}. Must be true or false.`)}}function Yn(e,t){if(e===void 0)return;let n=e.trim().toLowerCase();if(n){if(Bn.includes(n))return n;throw Error(`Invalid ${t}: ${e}. Must be one of off,gate,dry_run,write.`)}}function Xn(e){let t=e.autoSkillsLoadEnabled===void 0?Jn(process.env.AIMAX_AUTO_SKILLS_LOAD_ENABLED,`AIMAX_AUTO_SKILLS_LOAD_ENABLED`):Jn(e.autoSkillsLoadEnabled,`--auto-skills-load-enabled`),n=e.autoSkillsReviewMode===void 0?Yn(process.env.AIMAX_AUTO_SKILLS_REVIEW_MODE,`AIMAX_AUTO_SKILLS_REVIEW_MODE`):Yn(e.autoSkillsReviewMode,`--auto-skills-review-mode`),r={};return t!==void 0&&(r.load={enabled:t}),n!==void 0&&(r.review={mode:n}),r.load||r.review?r:void 0}function Zn(e,t){if(e.inputText&&t){let n=xe(e.inputText,t);return n.submittedBy={userId:e.user,channel:e.channel??`IM`},n}if(!e.inputJson&&!e.inputText)throw Error(`Either --input-json or --input-text must be provided for the resume command`);try{let t=JSON.parse(e.inputJson);return{requestId:e.requestId,sessionId:e.sessionId,action:t.action??`submit`,values:t.values,submittedBy:t.submittedBy,idempotencyKey:t.idempotencyKey,submittedAt:t.submittedAt??new Date().toISOString()}}catch(e){throw Error(`Invalid --input-json: ${e.message}`)}}function Qn(e,t){if(!e.inputJson)throw Error(`UI tool resume requires --input-json`);try{let n=JSON.parse(e.inputJson),r=n&&typeof n==`object`&&`values`in n&&n.values&&typeof n.values==`object`&&!Array.isArray(n.values)?n.values:n;return{requestId:t.request.requestId,sessionId:t.sessionId,toolCallId:t.request.toolCallId,toolName:t.request.toolName,values:r,submittedAt:new Date().toISOString()}}catch(e){throw Error(`Invalid --input-json: ${e.message}`)}}async function $n(e){let t=await ae(e.dataDir,e.sessionId,{storeName:e.sessionStoreName,encryptSessions:e.encryptSessions});for(let n=t.length-1;n>=0;--n){let r=t[n];if(r?.role!==`assistant`||!Array.isArray(r.toolCalls))continue;let i=r.toolCalls.find(t=>t.id===e.toolCallId&&t.name===e.toolName);if(i)return i.arguments}}async function er(e){let t=Un(e),n=e.channel??`WEB`,r=ln({apiFormat:e.apiFormat,baseUrl:e.baseUrl,apiKey:e.apiKey,authToken:e.authToken,model:e.model,contextWindow:e.contextWindow?Number(e.contextWindow):void 0,flashModel:e.flashModel,thinking:e.thinking});return{dataDir:t,systemAgentsDir:Gn(e),channel:n,format:e.output===`json`?`json`:`text`,timeoutMs:e.timeout?Number(e.timeout):void 0,llm:r,skillsLoadPaths:qn(e.skillsLoadPaths),autoSkills:Xn(e)}}function tr(e){let t=[],n=null;return e.callbackUrl&&t.push(new Cn(e.callbackUrl)),e.streamUrl&&(n=new Mn(e.streamUrl,Vn(e.streamEvents),e.streamAuthToken),t.push(n)),{sink:new Sn(t),websocketSink:n}}async function nr(e){let{dataDir:t,sessionId:n,sessionStoreName:r,toolCallId:i,toolName:a,resolution:o,encryptSessions:s}=e;if(!i||!a||!ye(a))return;let c=(e,t)=>{switch(e){case`request_approval`:return f(t);case`clarify`:return p(t);case`request_review`:return m(t);default:return JSON.stringify({action:t.action,values:t.values})}};await de(t,n,e=>{let t=e.findIndex(e=>e.role===`tool_result`&&e.toolCallId===i&&e.toolName===a);if(t===-1)return e;let n=e.slice(0,t+1);return n[t]={...n[t],content:c(a,o),isError:!1},n},{storeName:r,encryptSessions:s})}async function rr(e){let{dataDir:t,sessionId:n,sessionStoreName:r,result:i,encryptSessions:a}=e;await de(t,n,e=>{let t=e.findIndex(e=>e.role===`assistant`&&Array.isArray(e.toolCalls)&&e.toolCalls.some(e=>e.id===i.toolCallId&&e.name===i.toolName));if(t===-1)return e;let n=e.slice(0,t+1);return n.push({role:`tool_result`,toolCallId:i.toolCallId,toolName:i.toolName,content:JSON.stringify({submitted:!0,values:i.values}),isError:!1,timestamp:i.submittedAt}),n},{storeName:r,encryptSessions:a})}async function ir(e){let t=Un(e);await U(t,e,async()=>{N(t,{messageId:e.messageId,sessionId:e.sessionId,sessionStoreName:Kn(e.sessionStore)});let i=new lt(`resume command`),a=null;try{let t=await er(e),o=Kn(e.sessionStore),c=null;if(e.goal||e.goalFile){let i;i=e.goalFile?await D.readFile(e.goalFile,`utf-8`):e.goal;let a=Pn(i),s=e.tokenBudget===void 0?void 0:e.tokenBudget===`0`||e.tokenBudget===``?null:Number(e.tokenBudget);s!=null&&(Number.isNaN(s)||s<=0)&&(I(`error: --token-budget must be a positive integer, or 0 for unlimited`),process.exit(1));try{let n=await b(t.dataDir,e.sessionId,{storeName:o}),i=await r({dataDir:t.dataDir,sessionId:e.sessionId,objective:a,tokenBudget:s,force:e.force,storeName:o});c=In({sessionId:e.sessionId,before:n,result:i})}catch(e){throw e instanceof n&&(I(e.message),I(`Existing: ${e.existingObjectivePreview}`),process.exit(2)),e}}P.info(`resume command started`,{sessionId:e.sessionId,requestId:e.requestId,channel:t.channel,dataDir:t.dataDir});let l=await te(t.dataDir,e.sessionId,{storeName:o,encryptSessions:e.encryptSessions}),u=l?null:await ne(t.dataDir,e.sessionId,{storeName:o,encryptSessions:e.encryptSessions});if(!l&&!u)throw Error(`No pending HITL or UI tool request found for session "${e.sessionId}"`);if(l){if(l.request.requestId!==e.requestId)throw Error(`Request ID mismatch: pending is "${l.request.requestId}", got "${e.requestId}"`);if(l.status===`expired`)throw Error(`HITL request has expired`);if(l.status===`cancelled`)throw Error(`HITL request has been cancelled`)}else if(u){if(u.request.requestId!==e.requestId)throw Error(`Request ID mismatch: pending is "${u.request.requestId}", got "${e.requestId}"`);if(u.status!==`pending`)throw Error(`UI tool request is already "${u.status}", cannot resume`)}if(l){let n=Zn(e,l.request),r=await le({dataDir:t.dataDir,sessionStoreName:o,sessionId:e.sessionId,requestId:e.requestId,resolution:n}),s=r.state;if(!s)throw Error(`Resolved HITL state was not persisted`);let u=ar(n,s.request.kind);await nr({dataDir:t.dataDir,sessionId:e.sessionId,sessionStoreName:o,toolCallId:s.toolContext?.toolCallId,toolName:s.toolContext?.toolName,resolution:n,encryptSessions:e.encryptSessions});let d=tr(e);a=d.sink;let f=e.sessionId,p=async n=>{if(f=n.sessionId??f,!ve(n)){if(n.type===`stream_text_delta`){t.format===`text`&&L(n),await d.websocketSink?.sendTextDelta({sessionId:f,channel:t.channel,messageId:e.messageId,user:e.user,text:n.text});return}t.format===`text`&&L(n),await a?.send({sessionId:f,channel:t.channel,messageId:e.messageId,user:e.user,type:`progress`,event:n})}};c&&await p(c),await p({type:`hitl_resumed`,requestId:e.requestId,resolution:n,sessionId:e.sessionId}),`idempotentReplay`in r&&r.idempotentReplay&&(P.info(`resume command treated as idempotent replay`,{sessionId:e.sessionId,requestId:e.requestId,idempotencyKey:n.idempotencyKey}),gt({sessionId:e.sessionId,text:`Resume request already processed.`,usage:{input:0,output:0,total:0},durationMs:0},t.format),i.end(),process.exit(0));let m=e.pluginsConfig??process.env.AIMAX_PLUGINS_CONFIG,h=await W(t.dataDir,m),g;try{g=await fe({dataDir:t.dataDir,projectDir:Wn(e.projectDir),systemAgentsDir:t.systemAgentsDir,sessionStoreName:o,sessionId:e.sessionId,messageId:e.messageId,channel:t.channel,llm:t.llm,skillsLoadPaths:t.skillsLoadPaths,autoSkills:t.autoSkills,timeoutMs:t.timeoutMs,message:u,encryptSessions:e.encryptSessions??!1,topicSegmentation:Hn(e),hitlResume:{request:s.request,resolution:n,checkpoint:s.checkpoint,toolContext:s.toolContext},plugins:h?{config:h,dataDir:t.dataDir,workspaceDir:C.join(t.dataDir,`workspace`),bundledDir:process.env.AIMAX_PLUGINS_BUNDLED_DIR,llmAllowlist:h.llmAllowlist}:void 0,onProgress:p})}finally{}g.error?await a?.send({sessionId:g.sessionId,channel:t.channel,messageId:e.messageId,user:e.user,type:`error`,message:g.error}):await a?.send({sessionId:g.sessionId,channel:t.channel,messageId:e.messageId,user:e.user,type:`done`,result:{text:g.text,usage:g.usage,durationMs:g.durationMs,error:g.error,paused:g.paused,uiToolPending:g.uiToolPending}}),gt(g,t.format),g.error?P.error(`resume command failed: ${g.error}`):P.info(`resume command succeeded`),i.end(),process.exit(g.error?1:0);return}let d=Qn(e,u);if(!await ue(t.dataDir,e.sessionId,e.requestId,d,{storeName:o,encryptSessions:e.encryptSessions}))throw Error(`UI tool resume validation failed`);await rr({dataDir:t.dataDir,sessionId:e.sessionId,sessionStoreName:o,result:d,encryptSessions:e.encryptSessions});let f=tr(e);a=f.sink;let p=e.sessionId,m=async n=>{if(p=n.sessionId??p,!ve(n)){if(n.type===`stream_text_delta`){t.format===`text`&&L(n),await f.websocketSink?.sendTextDelta({sessionId:p,channel:t.channel,messageId:e.messageId,user:e.user,text:n.text});return}if(t.format===`text`&&L(n),n.type===`start`){await a?.send({sessionId:p,channel:t.channel,messageId:e.messageId,user:e.user,type:`start`,message:n.message});return}await a?.send({sessionId:p,channel:t.channel,messageId:e.messageId,user:e.user,type:`progress`,event:n})}},h=await $n({dataDir:t.dataDir,sessionId:e.sessionId,sessionStoreName:o,toolCallId:d.toolCallId,toolName:d.toolName,encryptSessions:e.encryptSessions});c&&await m(c),await m({type:`tool_end`,sessionId:e.sessionId,toolCallId:d.toolCallId,name:d.toolName,input:h,output:JSON.stringify({submitted:!0,values:d.values}),isError:!1});let g=e.pluginsConfig??process.env.AIMAX_PLUGINS_CONFIG,_=await W(t.dataDir,g),v=await fe({dataDir:t.dataDir,projectDir:Wn(e.projectDir),systemAgentsDir:t.systemAgentsDir,sessionStoreName:o,sessionId:e.sessionId,messageId:e.messageId,channel:t.channel,llm:t.llm,skillsLoadPaths:t.skillsLoadPaths,autoSkills:t.autoSkills,timeoutMs:t.timeoutMs,message:`[UI_TOOL_RESUME]
8
+ The pending UI tool result has been written into the transcript. Continue from the latest tool result without repeating the same form request.`,encryptSessions:e.encryptSessions??!1,topicSegmentation:Hn(e),uiToolResume:d,plugins:_?{config:_,dataDir:t.dataDir,workspaceDir:C.join(t.dataDir,`workspace`),bundledDir:process.env.AIMAX_PLUGINS_BUNDLED_DIR,llmAllowlist:_.llmAllowlist}:void 0,onProgress:m});await s(t.dataDir,e.sessionId,{storeName:o,encryptSessions:e.encryptSessions}),v.error?await a?.send({sessionId:v.sessionId,channel:t.channel,messageId:e.messageId,user:e.user,type:`error`,message:v.error}):await a?.send({sessionId:v.sessionId,channel:t.channel,messageId:e.messageId,user:e.user,type:`done`,result:{text:v.text,usage:v.usage,durationMs:v.durationMs,error:v.error,paused:v.paused,uiToolPending:v.uiToolPending}}),gt(v,t.format),v.error?P.error(`resume command failed: ${v.error}`):P.info(`resume command succeeded`),i.end(),process.exit(v.error?1:0)}catch(t){let n=t;a&&(n.message.includes(`expired`)&&await a.send({sessionId:e.sessionId,channel:e.channel??`WEB`,messageId:e.messageId,user:e.user,type:`progress`,event:{type:`hitl_expired`,sessionId:e.sessionId,requestId:e.requestId,reason:n.message}}),n.message.includes(`cancelled`)&&await a.send({sessionId:e.sessionId,channel:e.channel??`WEB`,messageId:e.messageId,user:e.user,type:`progress`,event:{type:`hitl_cancelled`,sessionId:e.sessionId,requestId:e.requestId,reason:n.message}}),await a.send({sessionId:e.sessionId,channel:e.channel??`WEB`,messageId:e.messageId,user:e.user,type:`error`,message:`Resume failed: ${n.message}`})),P.error(`resume command error: ${n.message}`),I(`Fatal: ${n.message}`),i.end(),process.exit(1)}finally{await a?.close()}})}function ar(e,t){return o(e,t)}function or(e){e.command(`resume`).description(`Resume a paused HITL session with user input`).requiredOption(`-d, --data-dir <path>`,`Data directory path (overrides AIMAX_DATA_DIR)`).option(`--project-dir <path>`,`Current project directory used as the default cwd context`).option(`--system-agents-dir <path>`,`System agent definition directory (overrides AIMAX_SYSTEM_AGENTS_DIR, default: /aimax/agents)`).requiredOption(`-s, --session-id <id>`,`Session ID to resume`).option(`--session-store <name>`,`Session store directory under .aimax (default: sessions)`).requiredOption(`--request-id <id>`,`HITL request ID to resolve`).option(`--input-json <json>`,`JSON string with the resolution (action, values, etc.)`).option(`--input-text <text>`,`Plain text response for IM channels without rich UI (e.g. '同意', '拒绝')`).option(`--skillsLoadPaths <paths>`,`Comma-separated absolute skill load paths`).option(`--auto-skills-load-enabled <boolean>`,`Enable learned auto-skill loading for the main agent (overrides AIMAX_AUTO_SKILLS_LOAD_ENABLED)`).option(`--auto-skills-review-mode <mode>`,`Auto-skill review mode: off, gate, dry_run, or write (overrides AIMAX_AUTO_SKILLS_REVIEW_MODE)`).option(`--message-id <id>`,`Message ID for correlating events`).option(`--user <id>`,`User identifier propagated to external callback and stream payloads`).option(`-c, --channel <channel>`,`Channel name (default: WEB)`,`WEB`).option(`--base-url <url>`,`LLM API base URL (overrides AIMAX_BASE_URL)`).option(`--api-format <format>`,`LLM API format: openai-completions or anthropic-messages (overrides AIMAX_API_FORMAT)`).option(`--api-key <key>`,`LLM API key (overrides AIMAX_API_KEY)`).option(`--auth-token <token>`,`Auth token used to generate the API key`).option(`--model <name>`,`LLM model name (overrides AIMAX_MODEL)`).option(`--context-window <n>`,`LLM context window size`).option(`--thinking <level>`,`Explicit thinking level: minimal, low, medium, high, or xhigh (overrides AIMAX_THINKING)`).option(`--callback-url <url>`,`HTTP callback URL for progress events`).option(`--stream-url <url>`,`WebSocket URL for streaming text events`).option(`--stream-auth-token <token>`,`Auth token for WebSocket streaming`).option(`--stream-events <list>`,`Comma-separated stream events`).option(`--timeout <ms>`,`Execution timeout in milliseconds`).option(`--output <format>`,`Output format: text (default) or json`,`text`).option(`--plugins-config <path>`,`Plugins config file path`).option(`--goal <text>`,`Set or replace the thread goal objective before resuming`).option(`--goal-file <path>`,`Read goal objective from a file`).option(`--token-budget <n>`,`Token budget for the goal (positive integer, or 0 for unlimited)`).option(`--force`,`Replace existing goal with a different objective`).option(`--encrypt-sessions`,`Enable encryption for the .aimax data directory`).option(`--disable-topic-segmentation`,`Disable smart topic segmentation for this resume run`).action(async e=>{await ir(e)})}function sr(e,t){return{notify:(n,r=`info`)=>{t?.readyState===1?t.send(JSON.stringify({type:`diagnostic`,sessionId:e,level:r,message:n})):console.log(`[${r}] ${n}`)},confirm:async n=>!t||t.readyState!==1?!1:new Promise(r=>{let i=`confirm-${Date.now()}`;t.send(JSON.stringify({type:`hitl_confirm`,sessionId:e,id:i,message:n}));let a=e=>{try{let t=JSON.parse(String(e));r(t.id===i&&t.confirmed===!0)}catch{r(!1)}};typeof t.once==`function`?t.once(`message`,a):t.addEventListener(`message`,e=>a(e.data),{once:!0})}),select:async(n,r)=>{if(!(!t||t.readyState!==1))return new Promise(i=>{let a=`select-${Date.now()}`;t.send(JSON.stringify({type:`hitl_select`,sessionId:e,id:a,title:n,options:r}));let o=e=>{try{let t=JSON.parse(String(e));i(t.id===a?t.selected:void 0)}catch{i(void 0)}};typeof t.once==`function`?t.once(`message`,o):t.addEventListener(`message`,e=>o(e.data),{once:!0})})},setStatus:n=>{t?.readyState===1?t.send(JSON.stringify({type:`diagnostic`,sessionId:e,level:`info`,message:n})):console.log(`[status] ${n}`)}}}const cr=[`start`,`text`,`done`,`error`],lr=[`off`,`gate`,`dry_run`,`write`];function ur(e){if(!e?.trim())return new Set(cr);let t=e.split(`,`).map(e=>e.trim().toLowerCase()).filter(Boolean),n=new Set([`start`,`text`,`progress`,`done`,`error`,`hitl`,`title_updated`]),r=new Set;for(let e of t){if(!n.has(e))throw Error(`Invalid stream event: ${e}. Must be one of start,text,progress,done,error,hitl,title_updated`);r.add(e)}return r.size>0?r:new Set(cr)}function dr(e){if(e.disableTopicSegmentation===!0||process.env.AIMAX_DISABLE_TOPIC_SEGMENTATION===`true`)return{enabled:!1};let t=Ln();return t?{embeddingModelDir:t}:void 0}function fr(e){let t=e.dataDir??process.env.AIMAX_DATA_DIR;if(!t)throw Error(`Data directory must be specified via --data-dir option or AIMAX_DATA_DIR environment variable`);return t}function pr(e){return e?.trim()||void 0}function mr(e){let t=(e.systemAgentsDir??process.env.AIMAX_SYSTEM_AGENTS_DIR)?.trim();if(t){if(!C.isAbsolute(t)){let n=e.systemAgentsDir===void 0?`AIMAX_SYSTEM_AGENTS_DIR`:`--system-agents-dir`;throw Error(`Invalid ${n}: ${t}. Path must be absolute.`)}return t}}function hr(e){return e?.trim()||void 0}function gr(e){return e??`WEB`}function q(e){return e?y(e):void 0}function _r(e){if(!e?.trim())return[];let t=e.split(`,`).map(e=>e.trim()).filter(Boolean),n=new Set,r=[];for(let e of t){if(!C.isAbsolute(e))throw Error(`Invalid --skillsLoadPaths entry: ${e}. Each path must be absolute.`);n.has(e)||(n.add(e),r.push(e))}return r}function vr(e){let t=``;for(let n=0;n<e.length;n+=1){let r=e[n];if(r===`\\`&&n+1<e.length){let r=e[n+1];if(r===`,`||r===`\\`||r===`=`){t+=r,n+=1;continue}}t+=r}return t}function yr(e){let t=[],n=``;for(let r=0;r<e.length;r+=1){let i=e[r];if(i===`\\`&&r+1<e.length){let t=e[r+1];if(t===`,`||t===`\\`){n+=t,r+=1;continue}}if(i===`,`){t.push(n),n=``;continue}n+=i}return t.push(n),t}function br(e){if(!e?.trim())return{};let t=[],n=new Set;for(let r of yr(e)){let e=r.trim();if(!e)continue;let i=e.indexOf(`=`);if(i<=0)throw Error(`Invalid --env entry: ${e}. Expected KEY=VALUE (use \\= to embed an equals sign in the value).`);let a=e.slice(0,i).trim(),o=e.slice(i+1).trim();if(!a)throw Error(`Invalid --env entry: ${e}. Key must not be empty.`);if(n.has(a))throw Error(`Invalid --env entry: duplicate key ${a}.`);n.add(a),t.push([a,vr(o)])}return Object.fromEntries(t)}function xr(e,t){if(e===void 0)return;let n=e.trim().toLowerCase();if(n){if([`true`,`1`,`yes`,`on`].includes(n))return!0;if([`false`,`0`,`no`,`off`].includes(n))return!1;throw Error(`Invalid ${t}: ${e}. Must be true or false.`)}}function Sr(e,t){if(e===void 0)return;let n=e.trim().toLowerCase();if(n){if(lr.includes(n))return n;throw Error(`Invalid ${t}: ${e}. Must be one of off,gate,dry_run,write.`)}}function Cr(e){let t=e.autoSkillsLoadEnabled===void 0?xr(process.env.AIMAX_AUTO_SKILLS_LOAD_ENABLED,`AIMAX_AUTO_SKILLS_LOAD_ENABLED`):xr(e.autoSkillsLoadEnabled,`--auto-skills-load-enabled`),n=e.autoSkillsReviewMode===void 0?Sr(process.env.AIMAX_AUTO_SKILLS_REVIEW_MODE,`AIMAX_AUTO_SKILLS_REVIEW_MODE`):Sr(e.autoSkillsReviewMode,`--auto-skills-review-mode`),r={};return t!==void 0&&(r.load={enabled:t}),n!==void 0&&(r.review={mode:n}),r.load||r.review?r:void 0}function wr(e,t){return e.length<=t?e:t<=3?e.slice(0,t):`${e.slice(0,t-3)}...`}function Tr(e,t){if(t<=0)return[``];let n=[];for(let r=0;r<e.length;r+=t)n.push(e.slice(r,r+t));return n.length>0?n:[``]}function Er(e,t,n=78){let r=Math.max(0,n-4),i=e?e.length+2:0,a=Math.max(r,i,...t.map(e=>e.length)),o=a+2,s=e?` ${wr(e,Math.max(0,a-2))} `:``,c=e?`┌${s}${`─`.repeat(Math.max(0,o-s.length))}┐`:`┌${`─`.repeat(o)}┐`,l=`└${`─`.repeat(o)}┘`;return[c,...t.flatMap(e=>Tr(e,a).map(e=>`│ ${e.padEnd(a,` `)} │`)),l].join(`
9
+ `)}function Dr(e){return e.kind===`text`?`inline message (${e.message.length} chars)`:`message file (${e.messages.length} messages)`}function J(e,t=`-`){return e==null||e===``?t:String(e)}function Or(e){return e?`set`:`unset`}function kr(e){return e?`true`:`false`}function Ar(e,t){let n=q(e.sessionStore)??`sessions`,r=t.timeoutMs===void 0?`default`:`${t.timeoutMs}`,i=e.callbackUrl?`enabled`:`disabled`,a=e.streamUrl?`enabled`:`disabled`,o=Dr(t.input),s=t.input.kind===`text`?`inline`:e.fromFile??`file`,c=Je(),l=dr(e)?.enabled===!1?`disabled`:`enabled`,u=e.pluginsConfig??process.env.AIMAX_PLUGINS_CONFIG,d=[...ur(e.streamEvents)].join(`,`),f=t.autoSkills?.load?.enabled,p=t.autoSkills?.review?.mode,m=e.tokenBudget===`0`?`unlimited`:J(e.tokenBudget),h=e.env?Object.keys(br(e.env)):[],g=h.length===0?`-`:`${h.length} key${h.length===1?``:`s`}`;return{banner:Er(void 0,[`AIMax CLI`,`Stateless Agent Runner`]),contextBox:Er(`Run Context`,[`time : ${c}`,`dataDir : ${t.dataDir}`,`projectDir : ${pr(e.projectDir)??`-`}`,`systemAgents : ${t.systemAgentsDir??`/aimax/agents`}`,`agent : ${hr(e.agent)??`-`}`,`channel : ${t.channel}`,`user : ${e.user??`-`}`,`message : ${e.message===void 0?`-`:`inline (${e.message.length} chars)`}`,`fromFile : ${J(e.fromFile)}`,`input : ${o}`,`skillsPaths : ${t.skillsLoadPaths.length>0?t.skillsLoadPaths.join(`,`):`-`}`,`autoSkillLoad: ${f===void 0?`-`:String(f)}`,`autoSkillRev : ${p??`-`}`,`sessionId : ${e.sessionId??`new`}`,`sessionStore : ${n}`,`messageId : ${e.messageId??`-`}`,`workspace : ${C.join(t.dataDir,`workspace`)}`,`baseUrl : ${t.llm.baseUrl}`,`apiKey : ${Or(e.apiKey)}`,`authToken : ${Or(e.authToken)}`,`model : ${t.llm.model}`,`contextWindow: ${J(t.llm.contextWindow)}`,`flashModel : ${J(t.llm.flashModel)}`,`thinking : ${J(t.llm.thinking)}`,`callbackUrl : ${J(e.callbackUrl)}`,`streamUrl : ${J(e.streamUrl)}`,`streamAuth : ${Or(e.streamAuthToken)}`,`streamEvents : ${d}`,`output : ${t.format}`,`callback : ${i}`,`websocket : ${a}`,`timeoutMs : ${r}`,`pluginsConfig: ${J(u)}`,`env : ${g}`,`goal : ${e.goal===void 0?`-`:`inline (${e.goal.length} chars)`}`,`goalFile : ${J(e.goalFile)}`,`tokenBudget : ${m}`,`force : ${kr(e.force)}`,`resumeReqId : ${J(e.resumeRequestId)}`,`resumeInput : ${e.resumeInputJson===void 0?`-`:`inline (${e.resumeInputJson.length} chars)`}`,`resumeFile : ${J(e.resumeFromFile)}`,`encryptSess : ${kr(e.encryptSessions)}`,`topicSegment : ${l}`]),logContext:{time:c,channel:t.channel,user:e.user,dataDir:t.dataDir,projectDir:pr(e.projectDir),systemAgentsDir:t.systemAgentsDir,agent:hr(e.agent),workspaceDir:C.join(t.dataDir,`workspace`),sessionId:e.sessionId??`new`,sessionStore:n,messageId:e.messageId,hasMessage:e.message!==void 0,fromFile:e.fromFile,skillsLoadPaths:t.skillsLoadPaths.join(`,`)||void 0,autoSkillsLoadEnabled:f,autoSkillsReviewMode:p,baseUrl:t.llm.baseUrl,hasApiKey:!!e.apiKey,hasAuthToken:!!e.authToken,model:t.llm.model,contextWindow:t.llm.contextWindow,flashModel:t.llm.flashModel,thinking:t.llm.thinking,callbackUrl:e.callbackUrl,streamUrl:e.streamUrl,hasStreamAuthToken:!!e.streamAuthToken,streamEvents:d,output:t.format,hasCallback:!!e.callbackUrl,hasStream:!!e.streamUrl,timeoutMs:t.timeoutMs,pluginsConfig:u,envKeys:h.length>0?h.join(`,`):void 0,hasGoal:e.goal!==void 0,goalFile:e.goalFile,tokenBudget:m===`-`?void 0:m,force:!!e.force,resumeRequestId:e.resumeRequestId,hasResumeInputJson:e.resumeInputJson!==void 0,resumeFromFile:e.resumeFromFile,encryptSessions:!!e.encryptSessions,topicSegmentation:l,inputKind:t.input.kind,inputSource:s,inputSummary:o}}}async function jr(e,t){let n=fr(e),r=gr(e.channel),i=ln({apiFormat:e.apiFormat,baseUrl:e.baseUrl,apiKey:e.apiKey,authToken:e.authToken,model:e.model,contextWindow:e.contextWindow?Number(e.contextWindow):void 0,flashModel:e.flashModel,thinking:e.thinking});return{dataDir:n,systemAgentsDir:mr(e),channel:r,format:e.output===`json`?`json`:`text`,timeoutMs:e.timeout?Number(e.timeout):void 0,llm:i,input:t,skillsLoadPaths:_r(e.skillsLoadPaths),autoSkills:Cr(e)}}function Mr(e){if(e.kind===`text`)return e.message;if(e.messages.length!==1)return null;let[t]=e.messages;if(t.role!==`user`)return null;if(typeof t.content==`string`)return t.content;if(t.content.length===0)return null;let n=[];for(let e of t.content){if(e.type!==`text`)return null;n.push(e.text)}return n.join(`
10
+ `)}async function Nr(e,t){if(!e.sessionId)return null;let n=Mr(t);if(!n)return null;let r=await te(fr(e),e.sessionId,{storeName:q(e.sessionStore),encryptSessions:e.encryptSessions});return!r||r.status!==`pending`||!be(n,r.request)?null:{requestId:r.request.requestId,inputText:n}}function Pr(e){let t=[],n=null;return e.callbackUrl&&t.push(new Cn(e.callbackUrl)),e.streamUrl&&(n=new Mn(e.streamUrl,ur(e.streamEvents),e.streamAuthToken),t.push(n)),{sink:new Sn(t),websocketSink:n}}async function Y(e,t){P.info(`dispatching external event`,ti(t)),await e.sink.send(t)}function Fr(e){let t={sessionId:e.sessionId,messageId:e.messageId,parentSessionId:e.parentSessionId,depth:e.depth,scope:e.scope,phase:e.phase,...e.details};if(e.level===`error`){P.error(e.message,t);return}if(e.level===`warn`){P.warn(e.message,t);return}P.info(e.message,t)}function Ir(e,t,n,r,i){return{sink:e,websocketSink:t,channel:n,defaultMessageId:r,user:i}}function Lr(e){let t=new AbortController,n=n=>{P.warn(`run abort signal received`,{signal:n,uptimeMs:Math.round(process.uptime()*1e3),alreadyAborted:t.signal.aborted,...e?.()}),t.abort()},r=()=>n(`SIGTERM`),i=()=>n(`SIGINT`);return process.once(`SIGTERM`,r),process.once(`SIGINT`,i),{controller:t,cleanup:()=>{process.off(`SIGTERM`,r),process.off(`SIGINT`,i)}}}function Rr(e){return{activeSessionId:e??`pending`,finalResult:null,loggerSessionId:void 0}}function zr(e,t,n,r){!r||n.loggerSessionId===r||(N(e,{messageId:t.messageId,sessionId:r,sessionStoreName:q(t.sessionStore)}),n.loggerSessionId=r)}function Br(e,t){e.activeSessionId=t.sessionId??e.activeSessionId}function Vr(e,t){return e.messageId??t}async function Hr(e,t,n){if(!await h(e.dataDir)){if(await Ur(t,e.format,n.activeSessionId,e.dataDir,`checking`),(await u(e.dataDir)).performedBootstrap){await Ur(t,e.format,n.activeSessionId,e.dataDir,`initializing`),await Ur(t,e.format,n.activeSessionId,e.dataDir,`initialized`);return}await Ur(t,e.format,n.activeSessionId,e.dataDir,`ready`)}}async function Ur(e,t,n,r,i){let a={type:`bootstrap`,phase:i,dataDir:r};t===`text`&&L(a),await Y(e,{sessionId:n,channel:e.channel,messageId:e.defaultMessageId,user:e.user,type:`progress`,event:a})}function Wr(e,t,n,r){return async i=>{if(Br(n,i),zr(e.dataDir,r,n,i.sessionId),ve(i)){Fr(i);return}if(i.type===`stream_text_delta`){e.format===`text`&&L(i),await t.websocketSink?.sendTextDelta({sessionId:n.activeSessionId,channel:e.channel,messageId:Vr(i,t.defaultMessageId),user:t.user,text:i.text});return}e.format===`text`&&L(i);let a=Vr(i,t.defaultMessageId);if(i.type===`hitl_requested`){await Y(t,{sessionId:n.activeSessionId,channel:e.channel,messageId:a,user:t.user,type:`hitl`,request:i.request}),await Y(t,{sessionId:n.activeSessionId,channel:e.channel,messageId:a,user:t.user,type:`progress`,event:i});return}if(i.type===`start`){await Y(t,{sessionId:n.activeSessionId,channel:e.channel,messageId:a,user:t.user,type:`start`,message:i.message});return}if(i.type===`session_reset`){await Y(t,{sessionId:n.activeSessionId,channel:e.channel,messageId:a,user:t.user,type:`session_reset`,action:i.action,previousSessionId:i.previousSessionId,message:i.message});return}await Y(t,{sessionId:n.activeSessionId,channel:e.channel,messageId:a,user:t.user,type:`progress`,event:i})}}function Gr(e){let t=sr(e);return{confirm:(e,n)=>t.confirm(e),select:(e,n,r)=>t.select?t.select(e,n):Promise.resolve(void 0)}}function Kr(e,t,n,r,i,a){let o={dataDir:t.dataDir,projectDir:pr(e.projectDir),systemAgentsDir:t.systemAgentsDir,agentPolicy:hr(e.agent)?{requestedAgentName:hr(e.agent)}:void 0,sessionStoreName:q(e.sessionStore),sessionId:e.sessionId,messageId:e.messageId,channel:t.channel,llm:t.llm,skillsLoadPaths:t.skillsLoadPaths,autoSkills:t.autoSkills,timeoutMs:t.timeoutMs,abortSignal:n.signal,pendingGoal:a,encryptSessions:e.encryptSessions??!1,topicSegmentation:dr(e),env:br(e.env),plugins:i?{config:i,dataDir:t.dataDir,workspaceDir:C.join(t.dataDir,`workspace`),bundledDir:process.env.AIMAX_PLUGINS_BUNDLED_DIR,llmAllowlist:i.llmAllowlist}:void 0,onProgress:r,createPiExtensionDialogBridge:Gr};return t.input.kind===`messages`?{...o,messages:t.input.messages}:{...o,message:t.input.message}}async function qr(e,t,n,r){if(t.activeSessionId=r.sessionId,t.finalResult=r,r.error){await Y(e,{sessionId:r.sessionId,channel:e.channel,messageId:n,user:e.user,type:`error`,message:r.error});return}await Y(e,{sessionId:r.sessionId,channel:e.channel,messageId:n,user:e.user,type:`done`,result:{text:r.text,usage:r.usage,durationMs:r.durationMs,error:r.error,paused:r.paused}})}async function Jr(e,t,n,r){await Y(e,{sessionId:t.finalResult?.sessionId??t.activeSessionId,channel:e.channel,messageId:n,user:e.user,type:`error`,message:`Fatal: ${r.message}`})}function Yr(e,t){let n=Ar(e,t);P.info([`run command started`,n.banner,n.contextBox].join(`
11
+ `),n.logContext),t.format===`text`&&(F(n.banner),F(``),F(n.contextBox),F(``))}function Xr(e,t,n){return gt(t,n),t.error?P.error(`run command failed: ${t.error}`):P.info(`run command succeeded`),e.end(),t.error?1:0}function Zr(e){let t=st();if(!t)return;let n=`[${Je()}] [ERROR] ${e}\n`;try{x.appendFileSync(C.join(t.logDir,`app.log`),n),x.appendFileSync(C.join(t.logDir,`errors.log`),n)}catch{}}async function Qr(e,t){let n=`run command error: ${t.message}`;P.error(n),Zr(n),I(`Fatal: ${t.message}`),e.end(),await ct(),process.exit(1)}async function $r(e){if(e.resumeInputJson&&e.resumeFromFile)throw Error(`--resume-input-json and --resume-from-file are mutually exclusive`);if(!e.resumeFromFile)return e.resumeInputJson;try{return await D.readFile(e.resumeFromFile,`utf-8`)}catch(e){throw Error(`Failed to read resume input file: ${e.message}`)}}async function ei(e){let t=fr(e);await U(t,e,async i=>{let a=Rr(e.sessionId);zr(t,e,a,e.sessionId),e.encryptSessions&&P.info(`--encrypt-sessions run mount state after logger initialization`,{dataDir:t,mounted:i?.mounted??!1,plainDir:i?.plainDir,encryptedDir:i?.encryptedDir});let o=new lt(`run command`),s=await $r(e);if(e.resumeRequestId||s||e.resumeFromFile){if(!e.sessionId||!e.resumeRequestId||!s)throw Error(`--session-id, --resume-request-id, and exactly one of --resume-input-json or --resume-from-file must be provided together`);await ir({dataDir:e.dataDir,projectDir:e.projectDir,systemAgentsDir:e.systemAgentsDir,sessionId:e.sessionId,sessionStore:e.sessionStore,requestId:e.resumeRequestId,inputJson:s,channel:e.channel,baseUrl:e.baseUrl,apiKey:e.apiKey,authToken:e.authToken,model:e.model,contextWindow:e.contextWindow,flashModel:e.flashModel,thinking:e.thinking,callbackUrl:e.callbackUrl,streamUrl:e.streamUrl,streamAuthToken:e.streamAuthToken,streamEvents:e.streamEvents,timeout:e.timeout,output:e.output,pluginsConfig:e.pluginsConfig,skillsLoadPaths:e.skillsLoadPaths,autoSkillsLoadEnabled:e.autoSkillsLoadEnabled,autoSkillsReviewMode:e.autoSkillsReviewMode,messageId:e.messageId,user:e.user,goal:e.goal,goalFile:e.goalFile,tokenBudget:e.tokenBudget,force:e.force,encryptSessions:e.encryptSessions,disableTopicSegmentation:e.disableTopicSegmentation});return}let c;try{c=await xn({message:e.message,fromFile:e.fromFile})}catch(e){await Qr(o,e);return}let l=await Nr(e,c);if(l){await ir({dataDir:e.dataDir,projectDir:e.projectDir,systemAgentsDir:e.systemAgentsDir,sessionId:e.sessionId,sessionStore:e.sessionStore,requestId:l.requestId,inputText:l.inputText,channel:e.channel,baseUrl:e.baseUrl,apiKey:e.apiKey,authToken:e.authToken,model:e.model,contextWindow:e.contextWindow,flashModel:e.flashModel,thinking:e.thinking,callbackUrl:e.callbackUrl,streamUrl:e.streamUrl,streamAuthToken:e.streamAuthToken,streamEvents:e.streamEvents,timeout:e.timeout,output:e.output,pluginsConfig:e.pluginsConfig,skillsLoadPaths:e.skillsLoadPaths,autoSkillsLoadEnabled:e.autoSkillsLoadEnabled,autoSkillsReviewMode:e.autoSkillsReviewMode,messageId:e.messageId,user:e.user,goal:e.goal,goalFile:e.goalFile,tokenBudget:e.tokenBudget,force:e.force,encryptSessions:e.encryptSessions,disableTopicSegmentation:e.disableTopicSegmentation});return}let u=e.channel??`WEB`,{controller:d,cleanup:f}=Lr(()=>({activeSessionId:a.activeSessionId,finalSessionId:a.finalResult?.sessionId,messageId:e.messageId,channel:u,user:e.user})),p=null;try{let t=await jr(e,c);u=t.channel,Yr(e,t);let i=Pr(e);p=i.sink,P.info(`external sink configured`,{callbackUrl:e.callbackUrl,streamUrl:e.streamUrl,streamEvents:e.streamEvents});let s=Ir(p,i.websocketSink,t.channel,e.messageId,e.user);await Hr(t,s,a);let l,f=null;if(e.goal||e.goalFile){let i;i=e.goalFile?await D.readFile(e.goalFile,`utf-8`):e.goal;let a=Pn(i),o=q(e.sessionStore),s=e.tokenBudget===void 0?void 0:e.tokenBudget===`0`||e.tokenBudget===``?null:Number(e.tokenBudget);s!=null&&(Number.isNaN(s)||s<=0)&&(I(`error: --token-budget must be a positive integer, or 0 for unlimited`),process.exit(1));try{if(e.sessionId){let n=await b(t.dataDir,e.sessionId,{storeName:o}),i=await r({dataDir:t.dataDir,sessionId:e.sessionId,objective:a,tokenBudget:s,force:e.force,storeName:o});f=In({sessionId:e.sessionId,before:n,result:i})}else l={objective:a,tokenBudget:s??null}}catch(e){throw e instanceof n&&(I(e.message),I(`Existing: ${e.existingObjectivePreview}`),process.exit(2)),e}}let m=e.pluginsConfig??process.env.AIMAX_PLUGINS_CONFIG,h=await W(t.dataDir,m),g=Wr(t,s,a,e);f&&await g(f);let _=await fe(Kr(e,t,d,g,h,l));zr(t.dataDir,e,a,_.sessionId),await qr(s,a,e.messageId,_),process.exitCode=Xr(o,_,t.format)}catch(t){let n=t;p&&await Jr(Ir(p,null,u,e.messageId,e.user),a,e.messageId,n),await Qr(o,n)}finally{await p?.close(),f()}})}function ti(e){return{eventType:e.type,sessionId:e.sessionId,messageId:e.messageId,channel:e.channel,user:e.user,progressType:e.type===`progress`?e.event.type:void 0,textLength:e.type===`done`?e.result.text.length:void 0,hasError:e.type===`error`?!0:e.type===`done`?!!e.result.error:void 0}}function ni(e){e.command(`run`).description(`Run an agent task`).option(`-d, --data-dir <path>`,`Data directory path (overrides AIMAX_DATA_DIR)`).option(`--project-dir <path>`,`Current project directory used as the default cwd context`).option(`--system-agents-dir <path>`,`System agent definition directory (overrides AIMAX_SYSTEM_AGENTS_DIR, default: /aimax/agents)`).option(`--agent <name>`,`Custom agent name to run as the root agent`).option(`--user <id>`,`User identifier propagated to external callback and stream payloads`).option(`--message <text>`,`User message to send to the agent`).option(`--from-file <path>`,`Load structured Message JSON from a file`).option(`--skillsLoadPaths <paths>`,`Comma-separated absolute skill load paths`).option(`--auto-skills-load-enabled <boolean>`,`Enable learned auto-skill loading for the main agent (overrides AIMAX_AUTO_SKILLS_LOAD_ENABLED)`).option(`--auto-skills-review-mode <mode>`,`Auto-skill review mode: off, gate, dry_run, or write (overrides AIMAX_AUTO_SKILLS_REVIEW_MODE)`).option(`-s, --session-id <id>`,`Resume an existing session by ID`).option(`--session-store <name>`,`Session store directory under .aimax (default: sessions)`).option(`--message-id <id>`,`Message ID for correlating events`).option(`-c, --channel <channel>`,`Channel name (default: WEB)`,`WEB`).option(`--base-url <url>`,`LLM API base URL (overrides AIMAX_BASE_URL)`).option(`--api-format <format>`,`LLM API format: openai-completions or anthropic-messages (overrides AIMAX_API_FORMAT)`).option(`--api-key <key>`,`LLM API key (overrides AIMAX_API_KEY)`).option(`--auth-token <token>`,`Auth token used to generate the API key (highest priority)`).option(`--model <name>`,`LLM model name (overrides AIMAX_MODEL)`).option(`--context-window <n>`,`LLM context window size`).option(`--flash-model <name>`,`Flash model for lightweight tasks like title generation (overrides AIMAX_FLASH_MODEL)`).option(`--thinking <level>`,`Explicit thinking level: minimal, low, medium, high, or xhigh (overrides AIMAX_THINKING)`).option(`--callback-url <url>`,`HTTP callback URL for progress events`).option(`--stream-url <url>`,`WebSocket URL for streaming text events`).option(`--stream-auth-token <token>`,`Auth token for WebSocket streaming`).option(`--stream-events <list>`,`Comma-separated stream events: start,text,progress,done,error`).option(`--timeout <ms>`,`Execution timeout in milliseconds`).option(`--output <format>`,`Output format: text (default) or json`,`text`).option(`--plugins-config <path>`,`Plugins config file path`).option(`--env <kvlist>`,`Session-scoped env vars injected into the run. Comma-separated KEY=VALUE pairs. Backslash-escape "," and "=" inside values. Available to plugins via api.runtime.session.env and merged into child process environments spawned by the exec/process tools.`).option(`--goal <text>`,`Set or replace the thread goal objective before running`).option(`--goal-file <path>`,`Read goal objective from a file`).option(`--token-budget <n>`,`Token budget for the goal (positive integer, or 0 for unlimited)`).option(`--force`,`Replace existing goal with a different objective`).option(`--resume-request-id <id>`,`Resume a pending HITL request from aimax run`).option(`--resume-input-json <json>`,`Structured HITL resume payload used with --resume-request-id`).option(`--resume-from-file <path>`,`Load structured HITL resume payload from a file (alternative to --resume-input-json)`).option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`).option(`--disable-topic-segmentation`,`Disable smart topic segmentation for this run`).action(async e=>{await ei(e)})}function ri(e){e.command(`sessions`).description(`List or inspect sessions for a data directory`).argument(`[action]`,`Action: list (default) | inspect | export`).option(`-d, --data-dir <path>`,`Data directory path`).option(`--session-store <name>`,`Session store directory under .aimax (default: sessions)`).option(`-c, --channel <channel>`,`Filter by channel type: H5 | WEB | KLPA | TASK | CRON | EIP_ASSISTANT`).option(`-s, --session-id <id>`,`Session ID for inspect/export`).option(`--output <format>`,`Output format: text or json`).option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`).action(async(e,t,n)=>{let r=n.opts(),i=e??`list`,a=i===`export`?r.output===`text`?`text`:`json`:r.output===`json`?`json`:`text`;r.dataDir||(I(`error: required option '-d, --data-dir <path>' not specified`),process.exit(1)),r.channel&&r.channel!==`H5`&&r.channel!==`WEB`&&r.channel!==`KLPA`&&r.channel!==`TASK`&&r.channel!==`CRON`&&r.channel!==`EIP_ASSISTANT`&&(I(`Invalid channel: ${r.channel}. Must be 'H5', 'WEB', 'KLPA', 'TASK', 'CRON', or 'EIP_ASSISTANT'`),process.exit(1));try{await U(r.dataDir,r,async()=>{let e=r.sessionStore?y(r.sessionStore):void 0;if(i===`list`){_t(await ee(r.dataDir,r.channel,{storeName:e}),a);return}if(r.sessionId||(I(`error: required option '-s, --session-id <id>' not specified`),process.exit(1)),i===`inspect`){vt(await _(r.dataDir,r.sessionId,{storeName:e}),a);return}if(i===`export`){yt(await d(r.dataDir,r.sessionId,{storeName:e}),a);return}I(`Invalid sessions action: ${i}. Must be 'list', 'inspect', or 'export'`),process.exit(1)})}catch(e){I(`${i===`inspect`?`Error inspecting session`:i===`export`?`Error exporting session`:`Error listing sessions`}: ${e.message}`),process.exit(1)}})}function ii(e){return C.join(e,`workspace`)}function ai(){let e=process.env.AIMAX_PLUGINS_BUNDLED_DIR;return e&&e.trim()||void 0}function oi(e,t){let n=[];n.push(`backend: ${e.backend}`),n.push(`provider: ${e.provider}${e.model?` (${e.model})`:``}`);let r=typeof e.custom?.providerSource==`string`?e.custom.providerSource:void 0,i=typeof e.custom?.providerId==`string`?e.custom.providerId:void 0,a=typeof e.custom?.pluginId==`string`?e.custom.pluginId:void 0;r&&n.push(`provider_source: ${r}`),i&&n.push(`provider_id: ${i}`),a&&n.push(`plugin_id: ${a}`),typeof e.files==`number`&&n.push(`files: ${e.files}`),typeof e.chunks==`number`&&n.push(`chunks: ${e.chunks}`),typeof e.dirty==`boolean`&&n.push(`dirty: ${e.dirty}`),e.dbPath&&n.push(`db: ${e.dbPath}`),e.sources?.length&&n.push(`sources: ${e.sources.join(`, `)}`),t&&(e.fts&&(n.push(`fts: enabled=${e.fts.enabled} available=${e.fts.available}`),e.fts.error&&n.push(`fts_error: ${e.fts.error}`)),e.vector&&(n.push(`vector: enabled=${e.vector.enabled} available=${e.vector.available??`unknown`}`),e.vector.dims&&n.push(`vector_dims: ${e.vector.dims}`),e.vector.loadError&&n.push(`vector_error: ${e.vector.loadError}`)),e.cache&&n.push(`cache: enabled=${e.cache.enabled} entries=${e.cache.entries??0}`));let o=Array.isArray(e.custom?.cliWarnings)?e.custom.cliWarnings.filter(e=>typeof e==`string`&&e.length>0):[];for(let e of o)n.push(`warning: ${e}`);return n.join(`
12
+ `)}function si(e){let t=e.level===`error`?`plugin error`:`plugin warning`;return e.pluginId?`${t} (${e.pluginId}): ${e.message}`:`${t}: ${e.message}`}function ci(e){if(!e||e===`cli`)return`cli`;if(e===`cron`)return`cron`;throw Error(`Unsupported dream trigger: ${e}. Expected "cli" or "cron".`)}async function li(e){let t=[],n;try{n=await W(e.dataDir,e.pluginsConfig)}catch(e){t.push(`failed to load plugins config: ${e.message}`)}if(!e.shouldInitialize&&!n)return{warnings:t};try{let r=g({config:n,dataDir:e.dataDir,workspaceDir:ii(e.dataDir),bundledDir:ai()});return t.push(...r.diagnostics.map(si)),{pluginSystem:r,warnings:t}}catch(e){return t.push(`failed to initialize plugin system: ${e.message}`),{warnings:t}}}async function ui(e){let t=C.join(e.dataDir,`.aimax`),{pluginSystem:n,warnings:r}=await li({dataDir:e.dataDir,pluginsConfig:e.pluginsConfig,shouldInitialize:!!(e.requirePluginSystem||e.provider||e.providerPlugin)}),i=n?.normalizedConfig.slots.memory,a=e.providerPlugin?.trim()||(e.provider?void 0:i),o=ce({providerId:e.provider,pluginId:a,dataDir:e.dataDir,memoryDir:t});return o?{provider:o.provider,providerId:o.registration.id,pluginId:o.registration.pluginId,providerOrigin:`plugin`,warnings:r,pluginSystem:n}:((a||e.provider)&&r.push(`requested memory provider was not resolved; falling back to builtin provider`),{provider:c({dataDir:e.dataDir,memoryDir:t},{includeSessions:e.includeSessions}),providerId:`builtin`,providerOrigin:`builtin`,warnings:r,pluginSystem:n})}function di(e,t,n){let r={...e.custom??{},providerSource:t.providerOrigin,providerId:t.providerId,pluginId:t.pluginId,cliWarnings:t.warnings},i={...e,custom:r};return n&&t.providerOrigin===`plugin`?i.custom={...r,note:`deep probe details are provider-defined for plugin memory providers`}:n&&(i.custom={...r,note:`deep probe is only supported by builtin provider`}),i}function fi(e){let t=e.command(`memory`).description(`Manage semantic memory indexing and search`),n=e=>e.option(`--plugins-config <path>`,`Plugins config file path`);n(t.command(`status`)).description(`Show memory index status`).requiredOption(`-d, --data-dir <path>`,`Data directory path`).option(`--deep`,`Probe embedding/vector availability`).option(`--index`,`Run a refresh before reporting status`).option(`--include-sessions`,`Include session transcripts in indexing`).option(`--provider <id>`,`Memory provider id`).option(`--provider-plugin <id>`,`Memory provider plugin id`).option(`--verbose`,`Verbose output`).option(`--output <format>`,`Output format: text (default) or json`,`text`).option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`).action(async e=>{let t=e.output===`json`?`json`:`text`;try{await U(e.dataDir,e,async()=>{let n=await ui({dataDir:e.dataDir,provider:e.provider,providerPlugin:e.providerPlugin,pluginsConfig:e.pluginsConfig,includeSessions:e.includeSessions});e.index&&n.provider.sync&&await n.provider.sync(`cli-status`);let r=di(n.provider.status(),n,e.deep);F(t===`json`?JSON.stringify(r,null,2):oi(r,e.deep))})}catch(e){I(`Error getting memory status: ${e.message}`),process.exit(1)}}),n(t.command(`index`)).description(`Reindex memory files`).requiredOption(`-d, --data-dir <path>`,`Data directory path`).option(`--include-sessions`,`Include session transcripts in indexing`).option(`--provider <id>`,`Memory provider id`).option(`--provider-plugin <id>`,`Memory provider plugin id`).option(`--rebuild`,`Force full rebuild from Markdown source`).option(`--verbose`,`Verbose output`).option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`).action(async e=>{try{await U(e.dataDir,e,async()=>{let t=await ui({dataDir:e.dataDir,provider:e.provider,providerPlugin:e.providerPlugin,pluginsConfig:e.pluginsConfig,includeSessions:e.includeSessions}),n;if(t.provider.sync&&(await t.provider.sync(e.rebuild?`cli-rebuild`:`cli-index`),n=t.provider.status().custom?.lastRebuildSummary),t.warnings.length>0)for(let e of t.warnings)I(`Warning: ${e}`);e.verbose&&n&&F(JSON.stringify(n,null,2)),F(e.rebuild?`Memory index rebuilt.`:`Memory index refreshed.`)})}catch(e){I(`Error indexing memory: ${e.message}`),process.exit(1)}}),n(t.command(`search [query]`)).description(`Search semantic memory`).requiredOption(`-d, --data-dir <path>`,`Data directory path`).option(`--query <text>`,`Search query`).option(`--include-sessions`,`Include session transcripts in search`).option(`--provider <id>`,`Memory provider id`).option(`--provider-plugin <id>`,`Memory provider plugin id`).option(`--output <format>`,`Output format: text (default) or json`,`text`).option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`).action(async(e,t)=>{let n=t.output===`json`?`json`:`text`,r=t.query?.trim()||e?.trim();r||(I(`Query is required: provide [query] or --query <text>`),process.exit(1));try{await U(t.dataDir,t,async()=>{let e=await(await ui({dataDir:t.dataDir,provider:t.provider,providerPlugin:t.providerPlugin,pluginsConfig:t.pluginsConfig,includeSessions:t.includeSessions})).provider.search(r);if(n===`json`){F(JSON.stringify(e,null,2));return}if(e.length===0){F(`No results found for: ${r}`);return}F(e.map(e=>{let t=`${e.path}:${e.startLine}-${e.endLine} (${e.score.toFixed(4)}) ${e.snippet}`;return e.citation?`${t}\n${e.citation}`:t}).join(`
13
+ `))})}catch(e){I(`Error searching memory: ${e.message}`),process.exit(1)}}),n(t.command(`dream`)).description(`Trigger Dream Gate host hook for memory plugins`).requiredOption(`-d, --data-dir <path>`,`Data directory path`).option(`--provider <id>`,`Memory provider id`).option(`--provider-plugin <id>`,`Memory provider plugin id`).option(`--trigger <mode>`,`Dream Gate trigger mode: cli (default) or cron`,`cli`).option(`--dry-run`,`Return hook results without plugin-side mutation`).option(`--output <format>`,`Output format: text (default) or json`,`text`).option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`).action(async e=>{let t=e.output===`json`?`json`:`text`;try{await U(e.dataDir,e,async()=>{let n=ci(e.trigger),r=await ui({dataDir:e.dataDir,provider:e.provider,providerPlugin:e.providerPlugin,pluginsConfig:e.pluginsConfig,requirePluginSystem:!0}),i=r.pluginSystem?await r.pluginSystem.registry.hooks.dispatch(`dream_gate`,{dataDir:e.dataDir,memoryDir:C.join(e.dataDir,`.aimax`),providerId:r.providerId,pluginId:r.pluginId,trigger:n,dryRun:!!e.dryRun},{workspaceDir:ii(e.dataDir)}):[],a={ok:!0,trigger:n,providerSource:r.providerOrigin,providerId:r.providerId,pluginId:r.pluginId,resultCount:i.length,results:i,warnings:r.warnings};if(t===`json`){F(JSON.stringify(a,null,2));return}F(`trigger: ${a.trigger}`),F(`provider_source: ${a.providerSource}`),F(`provider_id: ${a.providerId}`),a.pluginId&&F(`plugin_id: ${a.pluginId}`),F(`result_count: ${a.resultCount}`),a.results.length===0?F(`note: no dream_gate hook registered`):F(`results: ${JSON.stringify(a.results)}`);for(let e of a.warnings)F(`warning: ${e}`)})}catch(e){I(`Error running dream gate: ${e.message}`),process.exit(1)}})}function pi(e){return`${e.match.accountId?`${e.match.channel}:${e.match.accountId}`:e.match.channel} -> ${e.agentId}`}function mi(e){return async t=>{let n=await k(t),r=Le(n),i=Re(n);if(Ue(n),e.json){console.log(JSON.stringify({agents:r,bindings:i},null,2));return}console.log(`Agents:`);for(let e of r){let n=e.default?` (default)`:``,r=e.name&&e.name!==e.id?`${e.id}${n} (${e.name})`:`${e.id}${n}`;if(console.log(` - ${r}`),e.identity?.emoji||e.identity?.name){let t=[e.identity.emoji,e.identity.name].filter(Boolean).join(` `);console.log(` Identity: ${t}`)}let a=He(t,e.id);if(console.log(` Agent dir: ${a}`),e.model){let t=typeof e.model==`string`?e.model:e.model.primary;console.log(` Model: ${t}`)}let o=i.filter(t=>t.agentId===e.id);if(o.length>0){console.log(` Routing rules:`);for(let e of o)console.log(` - ${pi(e)}`)}}}}function hi(e,t){return async n=>{O(await k(n),ze(e))&&(console.error(`Agent "${e}" already exists.`),process.exit(1)),await Fe(n,{id:e,name:t.name,model:t.model,default:t.default})||(console.error(`Agent "${e}" already exists.`),process.exit(1));let r=He(n,e);console.log(`Agent "${e}" added successfully.`),console.log(` Agent dir: ${r}`)}}function gi(e){return async t=>{let n=O(await k(t),ze(e));n||(console.error(`Agent "${e}" not found.`),process.exit(1)),n.default===!0&&(console.error(`Cannot delete default agent "${e}".`),process.exit(1)),await Be(t,e)||(console.error(`Failed to delete agent "${e}".`),process.exit(1)),console.log(`Agent "${e}" deleted.`)}}function _i(e){return async t=>{let n=await k(t),r=e.agent??Ue(n);O(n,r)||(console.error(`Agent "${r}" not found.`),process.exit(1)),(!e.bind||e.bind.length===0)&&(console.error(`Please specify --bind <channel>[:<account>]`),process.exit(1));for(let n of e.bind){let[e,...i]=n.split(`:`),a=i.join(`:`),o=[`H5`,`WEB`,`KLPA`,`TASK`,`CRON`,`EIP_ASSISTANT`];o.includes(e)||(console.error(`Invalid channel: ${e}`),console.error(`Valid channels: ${o.join(`, `)}`),process.exit(1)),await Ie(t,{agentId:r,match:{channel:e,accountId:a||void 0}}),console.log(`Binding added: ${n} -> ${r}`)}}}function vi(e){return async t=>{let n=await k(t),r=e.agent??Ue(n);if(O(n,r)||(console.error(`Agent "${r}" not found.`),process.exit(1)),e.all){let e=await Ve(t,r,`*`);console.log(`Removed ${e} binding(s) for agent "${r}".`)}else if(e.bind&&e.bind.length>0){let n=0;for(let i of e.bind){let[e,...a]=i.split(`:`),o=await Ve(t,r,e,a.join(`:`)||void 0);o>0?(console.log(`Binding removed: ${i} -> ${r}`),n+=o):console.log(`No binding found: ${i} -> ${r}`)}}else console.error(`Please specify --bind <channel>[:<account>] or --all`),process.exit(1)}}function yi(e){return async t=>{let n=Re(await k(t));if(e.agent&&(n=n.filter(t=>t.agentId===e.agent)),e.json){console.log(JSON.stringify(n,null,2));return}if(n.length===0){console.log(`No bindings configured.`);return}console.log(`Routing bindings:`);for(let e of n)console.log(` ${pi(e)}`)}}function bi(e){return async t=>{let n=await k(t),r=e.agent??Ue(n);O(n,r)||(console.error(`Agent "${r}" not found.`),process.exit(1)),await We(t,r,{name:e.name,emoji:e.emoji,avatar:e.avatar})||(console.error(`Failed to update identity for agent "${r}".`),process.exit(1)),console.log(`Identity updated for agent "${r}".`)}}function xi(e,t){let n=e.command(`agents`).description(`Manage agents`),r=e=>e.option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`);r(n.command(`list`).description(`List all configured agents`).option(`-j, --json`,`Output as JSON`).option(`-b, --bindings`,`Show detailed binding rules`)).action(async e=>{let n=t();await U(n,e,async()=>{await mi(e)(n)})}),r(n.command(`add <id>`).description(`Add a new agent`).option(`-n, --name <name>`,`Display name`).option(`-m, --model <model>`,`Model identifier`).option(`--default`,`Mark as default agent`)).action(async(e,n)=>{let r=t();await U(r,n,async()=>{await hi(e,n)(r)})}),r(n.command(`delete <id>`).description(`Delete an agent`)).action(async(e,n)=>{let r=t();await U(r,n,async()=>{await gi(e)(r)})}),r(n.command(`bind`).description(`Bind a channel to an agent`).option(`-a, --agent <id>`,`Target agent ID`).option(`-b, --bind <channel...>`,`Channel binding (e.g., WEB, KLPA:ops)`)).action(async e=>{let n=t();await U(n,e,async()=>{await _i(e)(n)})}),r(n.command(`unbind`).description(`Unbind a channel from an agent`).option(`-a, --agent <id>`,`Target agent ID`).option(`-b, --bind <channel...>`,`Channel binding to remove`).option(`--all`,`Remove all bindings for the agent`)).action(async e=>{let n=t();await U(n,e,async()=>{await vi(e)(n)})}),r(n.command(`bindings`).description(`List routing bindings`).option(`-a, --agent <id>`,`Filter by agent ID`).option(`-j, --json`,`Output as JSON`)).action(async e=>{let n=t();await U(n,e,async()=>{await yi(e)(n)})}),r(n.command(`set-identity`).description(`Set agent identity`).option(`-a, --agent <id>`,`Target agent ID`).option(`-n, --name <name>`,`Agent name`).option(`-e, --emoji <emoji>`,`Agent emoji`).option(`--avatar <path>`,`Avatar path`)).action(async e=>{let n=t();await U(n,e,async()=>{await bi(e)(n)})})}function X(e){return C.join(e,`workspace`)}function Z(){let e=process.env.AIMAX_PLUGINS_BUNDLED_DIR;return e&&e.trim()||void 0}function Si(e){if(e.length===0){F(`No plugins discovered.`);return}for(let t of e){let e=t.status.padEnd(8,` `);F(`${t.id} ${e} ${t.origin} ${t.source}`)}}function Ci(e){F(`id: ${e.id}`),F(`status: ${e.status}`),F(`origin: ${e.origin}`),F(`source: ${e.source}`),F(`enabled: ${e.enabled}`),e.error&&F(`error: ${e.error}`),F(`tools: ${e.toolCount}`),F(`hooks: ${e.hookCount}`),e.skills.length>0&&F(`skills: ${e.skills.join(`, `)}`)}function wi(e){return Array.from(new Set((e??[]).map(e=>e.trim()).filter(Boolean)))}function Ti(e){if(e.length===0){F(`LLM allowlist is empty.`);return}for(let t of e)F(t)}function Ei(e,t){let n=e.command(`plugins`).description(`Manage AIMax plugins`),r=e=>e.option(`--plugins-config <path>`,`Plugins config file path`),i=e=>e.option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`);i(r(n.command(`list`))).description(`List discovered plugins`).option(`-d, --data-dir <path>`,`Data directory path`).action(async e=>{let n=e.dataDir??t();await U(n,e,async()=>{Si(g({config:await W(n,e.pluginsConfig),dataDir:n,workspaceDir:X(n),bundledDir:Z()}).registry.plugins)})}),i(r(n.command(`info`))).description(`Show plugin details`).argument(`<id>`,`Plugin id`).option(`-d, --data-dir <path>`,`Data directory path`).action(async(e,n)=>{let r=n.dataDir??t();await U(r,n,async()=>{let t=g({config:await W(r,n.pluginsConfig),dataDir:r,workspaceDir:X(r),bundledDir:Z()}).registry.plugins.find(t=>t.id===e);t||(I(`Plugin not found: ${e}`),process.exit(1)),Ci(t)})}),i(r(n.command(`enable`))).description(`Enable a plugin`).argument(`<id>`,`Plugin id`).option(`-d, --data-dir <path>`,`Data directory path`).action(async(e,n)=>{let r=n.dataDir??t();await U(r,n,async()=>{let t=await W(r,n.pluginsConfig)??{},i=oe(t);g({config:t,dataDir:r,workspaceDir:X(r),bundledDir:Z()}).registry.plugins.some(t=>t.id===e)||(I(`Plugin not found: ${e}`),process.exit(1)),await G(r,{...t,entries:{...t.entries,[e]:{...t.entries?.[e]??{},enabled:!0}}},n.pluginsConfig),F(`Enabled ${e}`),i.allow.length>0&&!i.allow.includes(e)&&F(`Note: plugins.allow is set; add this plugin id to allowlist if needed.`)})}),i(r(n.command(`disable`))).description(`Disable a plugin`).argument(`<id>`,`Plugin id`).option(`-d, --data-dir <path>`,`Data directory path`).action(async(e,n)=>{let r=n.dataDir??t();await U(r,n,async()=>{let t=await W(r,n.pluginsConfig)??{};g({config:t,dataDir:r,workspaceDir:X(r),bundledDir:Z()}).registry.plugins.some(t=>t.id===e)||(I(`Plugin not found: ${e}`),process.exit(1)),await G(r,{...t,entries:{...t.entries,[e]:{...t.entries?.[e]??{},enabled:!1}}},n.pluginsConfig),F(`Disabled ${e}`)})}),i(r(n.command(`doctor`))).description(`Validate plugin configuration`).option(`-d, --data-dir <path>`,`Data directory path`).action(async e=>{let n=e.dataDir??t();await U(n,e,async()=>{let t=g({config:await W(n,e.pluginsConfig),dataDir:n,workspaceDir:X(n),bundledDir:Z()});if(t.diagnostics.length===0){F(`No plugin issues detected.`);return}for(let e of t.diagnostics)F(`${e.level===`error`?`ERROR`:`WARN`}${e.pluginId?` ${e.pluginId}`:``}: ${e.message}`);t.diagnostics.some(e=>e.level===`error`)&&process.exit(1)})});let a=n.command(`llm-allow`).description(`Manage plugin LLM allowlist`);i(r(a.command(`list`))).description(`List LLM allowlist entries`).option(`-d, --data-dir <path>`,`Data directory path`).action(async e=>{let n=e.dataDir??t();await U(n,e,async()=>{Ti(wi((await W(n,e.pluginsConfig))?.llmAllowlist))})}),i(r(a.command(`add`))).description(`Add entries to the LLM allowlist (plugin id or tool name)`).argument(`<entry...>`,`Plugin id or tool name`).option(`-d, --data-dir <path>`,`Data directory path`).action(async(e,n)=>{let r=n.dataDir??t();await U(r,n,async()=>{let t=await W(r,n.pluginsConfig)??{},i=wi([...t.llmAllowlist??[],...e]);await G(r,{...t,llmAllowlist:i},n.pluginsConfig),F(`LLM allowlist updated (${i.length} entries).`)})}),i(r(a.command(`remove`))).description(`Remove entries from the LLM allowlist`).argument(`<entry...>`,`Plugin id or tool name`).option(`-d, --data-dir <path>`,`Data directory path`).action(async(e,n)=>{let r=n.dataDir??t();await U(r,n,async()=>{let t=await W(r,n.pluginsConfig)??{},i=new Set(e.map(e=>e.trim()).filter(Boolean)),a=wi(t.llmAllowlist).filter(e=>!i.has(e));await G(r,{...t,llmAllowlist:a},n.pluginsConfig),F(`LLM allowlist updated (${a.length} entries).`)})}),i(r(a.command(`clear`))).description(`Clear the LLM allowlist`).option(`-d, --data-dir <path>`,`Data directory path`).action(async e=>{let n=e.dataDir??t();await U(n,e,async()=>{await G(n,{...await W(n,e.pluginsConfig)??{},llmAllowlist:[]},e.pluginsConfig),F(`LLM allowlist cleared.`)})})}function Di(e){e.command(`commands`).description(`List available slash commands for a data directory`).requiredOption(`-d, --data-dir <path>`,`Data directory path`).option(`--output <format>`,`Output format: text (default) or json`,`text`).option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`).action(async e=>{let t=e.output===`json`?`json`:`text`;try{await U(e.dataDir,e,async()=>{let n=v(await ie(e.dataDir,[]));if(t===`json`){F(JSON.stringify(n,null,2));return}F(`Commands`),F(` builtin: ${n.builtin.map(e=>e.name).join(` | `)}`),n.skillCommands.length>0?F(` skills: ${n.skillCommands.map(e=>e.name).join(` | `)}`):F(` skills: (none)`)})}catch(e){I(`Error listing commands: ${e.message}`),process.exit(1)}})}function Q(e){let t=e.dataDir??process.env.AIMAX_DATA_DIR;if(!t)throw Error(`Data directory must be specified via --data-dir option or AIMAX_DATA_DIR environment variable`);return t}function $(e){if(!e.sessionId)throw Error(`Session ID is required (--session-id or -s)`);return e.sessionId}function Oi(e){return e?y(e):void 0}async function ki(e){let t={storeName:e.sessionStoreName},n=e.objective.trim(),r=await b(e.dataDir,e.sessionId,t);if(!r||(await se(e.dataDir,e.sessionId,r,t)).trim()!==n)return ge(e.dataDir,e.sessionId,{goalId:Ke(),objective:n,status:`active`,tokenBudget:e.tokenBudget??null,tokensUsed:0,timeUsedSeconds:0},{...t,eventSource:`cli`});if(r.status===`complete`)return r;let i={};return r.status===`paused`&&(i.status=`active`),e.tokenBudget!==void 0&&e.tokenBudget!==r.tokenBudget&&(i.tokenBudget=e.tokenBudget),Object.keys(i).length===0?r:await he(e.dataDir,e.sessionId,i,{...t,eventSource:`cli`})}async function Ai(e,t){let n=Q(t),r=$(t),i=Oi(t.sessionStore),a=e.trim();a||(I(`error: objective cannot be empty`),process.exit(1));let o=t.tokenBudget===void 0||t.tokenBudget===`0`||t.tokenBudget===``?null:Number(t.tokenBudget);o!==null&&(Number.isNaN(o)||o<=0)&&(I(`error: --token-budget must be a positive integer, or 0 for unlimited`),process.exit(1));let s=await ki({dataDir:n,sessionId:r,sessionStoreName:i,objective:a,tokenBudget:o});F(JSON.stringify({goal:s},null,2))}async function ji(e){let t=Q(e),n=$(e),r=Oi(e.sessionStore),i=await b(t,n,{storeName:r});if(!i){F(JSON.stringify({goal:null},null,2));return}let a=await se(t,n,i,{storeName:r});F(JSON.stringify({goal:{...i,objective:a}},null,2))}async function Mi(e){let t=Q(e),n=$(e),r=Oi(e.sessionStore),i=await b(t,n,{storeName:r});i||(I(`error: no goal exists for this session`),process.exit(1)),i.status===`complete`&&(I(`error: cannot pause a completed goal`),process.exit(1));let a=await he(t,n,{status:`paused`},{storeName:r,eventSource:`cli`});F(JSON.stringify({goal:a},null,2))}async function Ni(e){let t=Q(e),n=$(e),r=Oi(e.sessionStore),i=await b(t,n,{storeName:r});if(i||(I(`error: no goal exists for this session`),process.exit(1)),i.status===`complete`&&(I(`error: cannot resume a completed goal`),process.exit(1)),i.status===`budget_limited`&&(I(`error: cannot resume a budget-limited goal; clear or replace it instead`),process.exit(1)),i.status===`active`){F(JSON.stringify({goal:i},null,2));return}let a=await he(t,n,{status:`active`},{storeName:r,eventSource:`cli`});F(JSON.stringify({goal:a},null,2))}async function Pi(e){let t=await l(Q(e),$(e),{storeName:Oi(e.sessionStore),eventSource:`cli`});F(JSON.stringify({cleared:t},null,2))}function Fi(e){let t=e.command(`goal`).description(`Manage thread goals for a session`).addHelpText(`after`,a());t.command(`set <objective>`).description(`Set or replace the thread goal (creates new goalId)`).option(`-d, --data-dir <path>`,`Data directory path (overrides AIMAX_DATA_DIR)`).requiredOption(`-s, --session-id <id>`,`Session ID`).option(`--session-store <name>`,`Session store directory under .aimax (default: sessions)`).option(`--token-budget <n>`,`Optional token budget (positive integer, or 0 for unlimited)`).action(async(e,t)=>{try{await Ai(e,t)}catch(e){I(`error: ${e.message}`),process.exit(1)}}),t.command(`get`).description(`Get the current goal as JSON`).option(`-d, --data-dir <path>`,`Data directory path (overrides AIMAX_DATA_DIR)`).requiredOption(`-s, --session-id <id>`,`Session ID`).option(`--session-store <name>`,`Session store directory under .aimax (default: sessions)`).action(async e=>{try{await ji(e)}catch(e){I(`error: ${e.message}`),process.exit(1)}}),t.command(`pause`).description(`Pause the current goal`).option(`-d, --data-dir <path>`,`Data directory path (overrides AIMAX_DATA_DIR)`).requiredOption(`-s, --session-id <id>`,`Session ID`).option(`--session-store <name>`,`Session store directory under .aimax (default: sessions)`).action(async e=>{try{await Mi(e)}catch(e){I(`error: ${e.message}`),process.exit(1)}}),t.command(`resume`).description(`Resume a paused goal`).option(`-d, --data-dir <path>`,`Data directory path (overrides AIMAX_DATA_DIR)`).requiredOption(`-s, --session-id <id>`,`Session ID`).option(`--session-store <name>`,`Session store directory under .aimax (default: sessions)`).action(async e=>{try{await Ni(e)}catch(e){I(`error: ${e.message}`),process.exit(1)}}),t.command(`clear`).description(`Clear the current goal`).option(`-d, --data-dir <path>`,`Data directory path (overrides AIMAX_DATA_DIR)`).requiredOption(`-s, --session-id <id>`,`Session ID`).option(`--session-store <name>`,`Session store directory under .aimax (default: sessions)`).action(async e=>{try{await Pi(e)}catch(e){I(`error: ${e.message}`),process.exit(1)}})}const Ii=[`progress`,`error`,`hitl`];function Li(e){let t=e.dataDir??process.env.AIMAX_DATA_DIR;if(!t)throw Error(`Data directory must be specified via --data-dir or AIMAX_DATA_DIR`);return t}function Ri(e){if(!e?.trim())return new Set(Ii);let t=e.split(`,`).map(e=>e.trim().toLowerCase()).filter(Boolean),n=new Set([`start`,`text`,`progress`,`done`,`error`,`hitl`,`title_updated`]),r=new Set;for(let e of t){if(!n.has(e))throw Error(`Invalid stream event: ${e}. Must be one of start,text,progress,done,error,hitl`);r.add(e)}return r.size>0?r:new Set(Ii)}function zi(e){return e?y(e):void 0}function Bi(e){let t=[];return e.callbackUrl&&t.push(new Cn(e.callbackUrl)),e.streamUrl&&t.push(new Mn(e.streamUrl,Ri(e.streamEvents),e.streamAuthToken)),new Sn(t)}async function Vi(e){let t=Li(e);await U(t,e,async()=>{N(t,{messageId:e.messageId});let n=new lt(`cancel command`),r=e.channel??`WEB`,i=e.output===`json`?`json`:`text`,a=Bi(e),o=zi(e.sessionStore);try{let s=await te(t,e.sessionId,{storeName:o});if(!s)throw Error(`No pending HITL request found for session "${e.sessionId}"`);let c=e.requestId??s.request.requestId;if(s.request.requestId!==c)throw Error(`Request ID mismatch: pending is "${s.request.requestId}", got "${c}"`);if(!(await le({dataDir:t,sessionStoreName:o,sessionId:e.sessionId,requestId:c,resolution:{requestId:c,sessionId:e.sessionId,action:`cancel`,submittedAt:new Date().toISOString(),submittedBy:{channel:r}}})).state)throw Error(`Failed to cancel HITL request`);let l={type:`hitl_cancelled`,sessionId:e.sessionId,requestId:c,reason:e.reason??`cancelled by operator`};i===`text`&&L(l),await a.send({type:`progress`,sessionId:e.sessionId,channel:r,messageId:e.messageId,event:l}),n.end(),process.exit(0)}catch(t){let i=t;await a.send({type:`error`,sessionId:e.sessionId,channel:r,messageId:e.messageId,message:i.message}),P.error(`cancel command error: ${i.message}`),I(`Fatal: ${i.message}`),n.end(),process.exit(1)}finally{await a.close()}})}function Hi(e){e.command(`cancel`).description(`Cancel a pending HITL request`).requiredOption(`-d, --data-dir <path>`,`Data directory path`).requiredOption(`-s, --session-id <id>`,`Session ID containing the pending HITL request`).option(`--session-store <name>`,`Session store directory under .aimax (default: sessions)`).option(`--request-id <id>`,`HITL request ID to cancel`).option(`--reason <text>`,`Human-readable cancel reason`).option(`-c, --channel <channel>`,`Channel type (default: WEB)`,`WEB`).option(`--callback-url <url>`,`HTTP callback URL for progress events`).option(`--stream-url <url>`,`WebSocket URL for streaming text events`).option(`--stream-auth-token <token>`,`Auth token for WebSocket streaming`).option(`--stream-events <list>`,`Comma-separated stream events`).option(`--message-id <id>`,`Message ID for correlating events`).option(`--output <format>`,`Output format: text (default) or json`,`text`).option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`).action(async e=>{await Vi(e)})}function Ui(e){let t=e.dataDir??process.env.AIMAX_DATA_DIR;if(!t)throw Error(`Data directory must be specified via --data-dir option or AIMAX_DATA_DIR environment variable`);return t}function Wi(e){return e.query?.trim()||void 0}function Gi(e){if(!e)return;let t=Number(e);if(!Number.isFinite(t)||t<=0)throw Error(`--timeout must be a positive number of milliseconds`);return t}function Ki(e){if(!e)return;let t=Number(e);if(!Number.isInteger(t)||t<=0)throw Error(`--max-tokens must be a positive integer`);return t}async function qi(e){if(e.filePath){if(!C.isAbsolute(e.filePath))throw Error(`--file-path must be an absolute path`);try{await D.access(e.filePath)}catch(e){throw Error(`Failed to access --file-path: ${e.message}`)}return{source:`file`,filePath:e.filePath}}let t=e.content;if(!t||!t.trim())throw Error(`Either --file-path or --content must be provided`);return{source:`content`,content:t}}async function Ji(e){let t=null;try{let n=Ui(e);N(n,{messageId:e.messageId}),t=new lt(`summarize command`);let r=Wi(e),i=await qi(e),a=ln({apiFormat:e.apiFormat,baseUrl:e.baseUrl,apiKey:e.apiKey,model:e.model,contextWindow:2e5}),o=Gi(e.timeout),s=Ki(e.maxTokens);P.info(`summarize command started`,{dataDir:n,source:i.source,filePath:i.source===`file`?i.filePath:void 0,queryLength:r?.length,contentLength:i.source===`content`?i.content.length:void 0,timeoutMs:o,maxTokens:s});let c=await fe({dataDir:n,channel:`WEB`,messageId:e.messageId,message:Yi(i,r),llm:{...a,maxTokens:s},timeoutMs:o});F(c.text.trim()),P.info(`summarize command succeeded`,{source:i.source,summaryLength:c.text.length,durationMs:c.durationMs}),t.end()}catch(e){P.error(`summarize command error: ${e.message}`),I(`Fatal: ${e.message}`),t?.end(),process.exit(1)}}function Yi(e,t){return e.source===`file`?[t?`Summarize exactly one file for the query below.`:`Summarize exactly one file.`,`Read only the absolute file path provided here. Do not search, browse, read other files, execute commands, modify files, or perform any other processing.`,`Return only the summary text.`,``,...t?[`Query:\n${t}`,``]:[],`Input source: file`,`File path: ${e.filePath}`].join(`
14
+ `):[t?`Summarize the provided content for the query below.`:`Summarize the provided content.`,t?`Use only the provided content and query. Do not search, browse, read files, execute commands, modify files, or perform any other processing.`:`Use only the provided content. Do not search, browse, read files, execute commands, modify files, or perform any other processing.`,`Return only the summary text.`,``,...t?[`Query:\n${t}`,``]:[],`Input source: content`,`Source content:`,`<source>`,e.content,`</source>`].join(`
15
+ `)}function Xi(e){e.command(`summarize`).description(`Summarize one absolute file path or inline content`).option(`--file-path <path>`,`Absolute file path to summarize`).option(`--content <text>`,`Inline text content to summarize when --file-path is not provided`).option(`--query <text>`,`Optional search target or current description to focus the summary`).option(`-d, --data-dir <path>`,`Data directory path (overrides AIMAX_DATA_DIR)`).option(`--base-url <url>`,`LLM API base URL (overrides AIMAX_BASE_URL)`).option(`--api-format <format>`,`LLM API format: openai-completions or anthropic-messages (overrides AIMAX_API_FORMAT)`).option(`--api-key <key>`,`LLM API key (overrides AIMAX_API_KEY)`).option(`--model <name>`,`LLM model name (overrides AIMAX_MODEL)`).option(`--timeout <ms>`,`Execution timeout in milliseconds`).option(`--max-tokens <number>`,`Maximum number of tokens for model output`).option(`--message-id <id>`,`Message ID for log correlation`).action(async e=>{await Ji(e)})}const Zi={accent:`#8abeb7`,border:`#5f87ff`,borderAccent:`#00d7ff`,borderMuted:`#505050`,success:`#b5bd68`,error:`#cc6666`,warning:`#ffff00`,muted:`#808080`,dim:`#666666`,text:`#d4d4d4`,thinkingText:`#808080`,selectedBg:`#3a3a4a`,userMessageBg:`#343541`,userMessageText:`#d4d4d4`,customMessageBg:`#2d2838`,customMessageText:`#d4d4d4`,customMessageLabel:`#9575cd`,toolPendingBg:`#282832`,toolSuccessBg:`#283228`,toolErrorBg:`#3c2828`,toolTitle:`#d4d4d4`,toolOutput:`#808080`,mdHeading:`#f0c674`,mdLink:`#81a2be`,mdLinkUrl:`#666666`,mdCode:`#8abeb7`,mdCodeBlock:`#b5bd68`,mdCodeBlockBorder:`#808080`,mdQuote:`#808080`,mdQuoteBorder:`#808080`,mdHr:`#808080`,mdListBullet:`#8abeb7`,toolDiffAdded:`#b5bd68`,toolDiffRemoved:`#cc6666`,toolDiffContext:`#808080`,syntaxComment:`#6A9955`,syntaxKeyword:`#569CD6`,syntaxFunction:`#DCDCAA`,syntaxVariable:`#9CDCFE`,syntaxString:`#CE9178`,syntaxNumber:`#B5CEA8`,syntaxType:`#4EC9B0`,syntaxOperator:`#D4D4D4`,syntaxPunctuation:`#D4D4D4`,thinkingOff:`#505050`,thinkingMinimal:`#6e6e6e`,thinkingLow:`#5f87af`,thinkingMedium:`#81a2be`,thinkingHigh:`#b294bb`,thinkingXhigh:`#d183e8`,bashMode:`#b5bd68`},Qi=`#18181e`,$i=`#1e1e24`,ea=`#3c3728`;function ta(e){if(!e||typeof e!=`object`)return!1;let t=e.role;return t===`user`||t===`assistant`||t===`tool_result`||t===`compaction`}function na(e){return!e||typeof e!=`object`?!1:typeof e.type==`string`}function ra(e){return{type:`text`,text:e}}function ia(e){return{type:`thinking`,thinking:e}}function aa(e,t){let n=e.timestamp,r=typeof n==`string`&&n?n:e.content;return`${e.role}\0${r}\0${t}`}function oa(e,t,n){let r=aa(e,t);for(let e=0;e<100;e++){let t=Ge(`sha256`).update(`${r}\0${e}`).digest(`hex`).slice(0,8);if(!n.has(t))return n.add(t),t}let i=Ge(`sha256`).update(`${r}\0fallback`).digest(`hex`);return n.add(i),i}function sa(e,t){let n=new Set,r=null,i=[],a={type:`session`,version:3,id:t?.sessionId??Ke(),timestamp:e.find(e=>e.timestamp)?.timestamp??new Date().toISOString(),cwd:t?.cwd??process.cwd()};for(let[t,a]of e.entries()){let e=oa(a,t,n),o={id:e,parentId:r,timestamp:a.timestamp};if(a.role===`user`)i.push({type:`message`,...o,message:{role:`user`,content:[ra(a.content)]}});else if(a.role===`assistant`){let e=[];a.thinking?.trim()&&e.push(ia(a.thinking.trim())),a.content.trim()&&e.push(ra(a.content));for(let t of a.toolCalls??[])e.push({type:`toolCall`,id:t.id,name:t.name,arguments:t.arguments});a.errorMessage&&e.push(ra(a.errorMessage)),i.push({type:`message`,...o,message:{role:`assistant`,content:e,...a.stopReason?{stopReason:a.stopReason}:{}}})}else if(a.role===`tool_result`){let e=a.content;a.toolResultRef?.preview&&!e.includes(a.toolResultRef.preview)&&(e=`${e}\n\nPreview:\n${a.toolResultRef.preview}`),a.toolResultRef?.storagePath&&(e=`${e}\n\n[Full output: ${a.toolResultRef.storagePath}]`),i.push({type:`message`,...o,message:{role:`toolResult`,toolCallId:a.toolCallId,toolName:a.toolName,content:[ra(e)],isError:a.isError}})}else a.role===`compaction`&&i.push({type:`compaction`,...o,summary:a.content,firstKeptEntryId:r??e,tokensBefore:0});r=e}return{header:a,entries:i,leafId:r}}function ca(e,t){let n=e.trim().split(`
16
+ `).filter(e=>e.trim()),r=[],i=0;for(let e of n)try{r.push(JSON.parse(e))}catch{i+=1}if(r.length===0)return{data:sa([],t),skippedLines:i};let a=r[0];if(na(a)&&a.type===`session`){let e=a,t=r.slice(1).filter(na);return{data:{header:e,entries:t,leafId:t.length>0?t[t.length-1].id:null},skippedLines:i}}if(r.every(na)){let e=r;return{data:{header:{type:`session`,version:3,id:t?.sessionId??Ke(),timestamp:e[0]?.timestamp??new Date().toISOString(),cwd:t?.cwd??process.cwd()},entries:e,leafId:e.length>0?e[e.length-1].id:null},skippedLines:i}}return{data:sa(r.filter(ta),t),skippedLines:i}}function la(e){let t=e;for(;t!==w(t);){if(S(T(t,`package.json`)))return t;t=w(t)}throw Error(`Could not find @gencode/cli package root`)}function ua(){let e=la(w(Pe(import.meta.url))),t=[T(e,`dist`,`export-html`),T(e,`src`,`export-html`)];for(let e of t)if(S(T(e,`template.html`)))return e;throw Error(`export-html template directory not found under ${e}`)}function da(e){let t=C.resolve(e),n=`${C.sep}.aimax${C.sep}`,r=t.indexOf(n);if(!(r<0))return t.slice(0,r)}function fa(e){let t=C.dirname(C.resolve(e));return C.basename(t)}async function pa(e){let t=e.dataDir??(e.transcriptPath?da(e.transcriptPath):void 0),n=e.sessionId??(e.transcriptPath?fa(e.transcriptPath):void 0);return!t||!n?{}:re(t,n,e.sessionPathOptions)}const ma=new Map;function ha(e){let t=ma.get(e);if(t!==void 0)return t;let n=Ce(T(ua(),e),`utf-8`);return ma.set(e,n),n}function ga(){let e=[];for(let[t,n]of Object.entries(Zi))e.push(`--${t}: ${n};`);return e.push(`--exportPageBg: ${Qi};`),e.push(`--exportCardBg: ${$i};`),e.push(`--exportInfoBg: ${ea};`),e.join(`
17
+ `)}function _a(e){return e.replace(/[&<>"']/g,e=>{switch(e){case`&`:return`&amp;`;case`<`:return`&lt;`;case`>`:return`&gt;`;case`"`:return`&quot;`;case`'`:return`&#39;`;default:return e}})}function va(e){let t=ha(`template.html`),n=ha(`template.css`),r=ha(`template.js`),i=Buffer.from(JSON.stringify(e)).toString(`base64`),a=n.replace(`{{THEME_VARS}}`,ga()).replace(`{{BODY_BG}}`,Qi).replace(`{{CONTAINER_BG}}`,$i).replace(`{{INFO_BG}}`,ea);return t.replace(`{{CSS}}`,a).replace(`{{JS}}`,r).replace(`{{SESSION_DATA}}`,i)}function ya(e,t){return t||`aimax-session-${Ee(e,`.jsonl`)}.html`}function ba(e,t){return`Skipped ${t} malformed JSONL line(s) while exporting ${e}`}function xa(e){for(let t of e)console.warn(t)}async function Sa(e,t){if(t.options?.includeSnapshots===!1)return e;let n=await pa({dataDir:t.options?.dataDir,sessionId:t.options?.sessionId??e.header.id,transcriptPath:t.inputLabel,sessionPathOptions:t.options?.sessionPathOptions});return!n.systemPrompt&&!n.tools?.length?e:{...e,...n.systemPrompt?{systemPrompt:n.systemPrompt}:{},...n.tools?.length?{tools:n.tools}:{}}}async function Ca(e,t,n){let r=ca(e,{sessionId:n?.sessionId,cwd:n?.cwd}),i=r.skippedLines>0?[ba(t,r.skippedLines)]:[],a=r.data;a=await Sa(a,{inputLabel:t,options:n});let o=va(a),s=ya(t,n?.outputPath);return Te(s,o,`utf8`),{outputPath:s,warnings:i}}async function wa(e,t){if(!S(e))throw Error(`File not found: ${e}`);return Ca(Ce(e,`utf-8`),e,t)}async function Ta(e,t){let n=await wa(e,t);return xa(n.warnings),n.outputPath}async function Ea(e,t,n){let r=me(e,t,n);if(!S(r))throw Error(`Transcript not found: ${r}`);if((await ae(e,t,n)).length===0&&!Ce(r,`utf-8`).trim())throw Error(`Nothing to export yet - transcript is empty`);let i=Ce(r,`utf-8`),{outputPath:a,sessionId:o,cwd:s,dataDir:c,includeSnapshots:l,...u}=n??{};return Ca(i,r,{outputPath:n?.outputPath,sessionId:t,cwd:n?.cwd,dataDir:e,sessionPathOptions:u,includeSnapshots:n?.includeSnapshots})}async function Da(e,t,n){let r=await Ea(e,t,n);return xa(r.warnings),r.outputPath}function Oa(e,t){let n=t.map(t=>{let n=S(T(e,t.fileName))?we(T(e,t.fileName)):void 0,r=n?`${Math.round(n.size/1024)} KB`:``,i=t.sourcePath?`<span class="muted">${_a(t.sourcePath)}</span>`:``;return`<tr><td><a href="${_a(t.fileName)}">${_a(t.sessionId)}</a></td><td>${r}</td><td>${i}</td></tr>`}).join(`
18
+ `),r=`<!DOCTYPE html>
19
+ <html lang="zh-CN">
20
+ <head>
21
+ <meta charset="UTF-8">
22
+ <title>AIMax Session Exports</title>
23
+ <style>
24
+ body { font-family: system-ui, sans-serif; margin: 2rem; background: #18181e; color: #d4d4d4; }
25
+ table { border-collapse: collapse; width: 100%; }
26
+ th, td { text-align: left; padding: 0.5rem 0.75rem; border-bottom: 1px solid #333; }
27
+ a { color: #81a2be; }
28
+ .muted { color: #808080; font-size: 0.85em; }
29
+ </style>
30
+ </head>
31
+ <body>
32
+ <h1>AIMax Session Exports (${t.length})</h1>
33
+ <table>
34
+ <thead><tr><th>Session</th><th>Size</th><th>Source</th></tr></thead>
35
+ <tbody>
36
+ ${n}
37
+ </tbody>
38
+ </table>
39
+ </body>
40
+ </html>`,i=T(e,`index.html`);return Te(i,r,`utf8`),i}async function ka(e,t,n){Se(t,{recursive:!0});let r=pe(e,n),i=[],a=[],o=[],s=[],c;try{c=await ke(r)}catch(e){throw e.code===`ENOENT`?Error(`Sessions directory not found: ${r}`):e}for(let r of c.sort()){let c=me(e,r,n);if(!S(c)){a.push({sessionId:r,reason:`no transcript.jsonl`});continue}try{let a=T(t,`${r}.html`),l=await Ea(e,r,{...n,outputPath:a});o.push(...l.warnings),i.push({sessionId:r,path:l.outputPath}),s.push({sessionId:r,fileName:`${r}.html`,sourcePath:c})}catch(e){a.push({sessionId:r,reason:e.message})}}let l;return n?.writeIndex!==!1&&s.length>0&&(l=Oa(t,s)),{exported:i,skipped:a,warnings:o,indexPath:l}}async function Aa(e,t,n){Se(t,{recursive:!0});let r=[],i=[],a=[],o=[],s=[];for await(let t of Oe(e,{cwd:process.cwd()}))s.push(t);if(s.sort(),s.length===0)throw Error(`No files matched glob: ${e}`);for(let e of s){let n=E(e),s=Ee(w(n));if(!S(n)){i.push({sessionId:s,reason:`file missing`});continue}try{let e=await wa(n,{outputPath:T(t,`${s}.html`),sessionId:s,dataDir:da(n)});a.push(...e.warnings),r.push({sessionId:s,path:e.outputPath}),o.push({sessionId:s,fileName:`${s}.html`,sourcePath:n})}catch(e){i.push({sessionId:s,reason:e.message})}}let c;return n?.writeIndex!==!1&&o.length>0&&(c=Oa(t,o)),{exported:r,skipped:i,warnings:a,indexPath:c}}function ja(e){e.command(`export-html`).description(`Export transcript.jsonl to standalone HTML files for browsing`).argument(`[transcript]`,`Path to transcript.jsonl (or use -d/-s, --all, or --glob)`).option(`-d, --data-dir <path>`,`Data directory containing .aimax/sessions`).option(`-s, --session-id <id>`,`Session ID under the data directory`).option(`--session-store <name>`,`Session store under .aimax (default: sessions)`).option(`-o, --output <path>`,`Output HTML path for a single export`).option(`--output-dir <path>`,`Output directory for batch export`).option(`--all`,`Export all sessions under --data-dir (requires --output-dir)`).option(`--glob <pattern>`,`Export transcripts matching a glob (requires --output-dir)`).option(`--no-index`,`Skip writing index.html in batch mode`).option(`--encrypt-sessions`,`Enable gocryptfs-backed encryption for the .aimax data directory`).action(async(e,t,n)=>{let r=n.opts();try{if(r.glob){r.outputDir||(I(`error: --glob requires --output-dir`),process.exit(1)),Na(await Aa(r.glob,E(r.outputDir),{writeIndex:!r.noIndex}));return}if(r.all){(!r.dataDir||!r.outputDir)&&(I(`error: --all requires --data-dir and --output-dir`),process.exit(1)),await U(r.dataDir,r,async()=>{let e=r.sessionStore?y(r.sessionStore):void 0;Na(await ka(r.dataDir,E(r.outputDir),{storeName:e,writeIndex:!r.noIndex}))});return}if(e){let t=E(e);S(t)||(I(`File not found: ${t}`),process.exit(1)),r.outputDir&&Se(E(r.outputDir),{recursive:!0});let n=await Ta(t,{outputPath:r.outputDir?T(E(r.outputDir),`${Ee(w(t))}.html`):r.output});console.log(n);return}(!r.dataDir||!r.sessionId)&&(I(`error: specify <transcript.jsonl>, --glob, (-d --all --output-dir), or (-d -s)`),process.exit(1)),Ma(r.sessionId),await U(r.dataDir,r,async()=>{let e=r.sessionStore?y(r.sessionStore):void 0;r.outputDir&&Se(E(r.outputDir),{recursive:!0});let t=r.outputDir?E(r.outputDir,`${r.sessionId}.html`):r.output,n=await Da(r.dataDir,r.sessionId,{outputPath:t,storeName:e});console.log(n)})}catch(e){I(`Error exporting HTML: ${e.message}`),process.exit(1)}})}function Ma(e){if(e.includes(`..`)||e.startsWith(`/`))throw Error(`Invalid session id: ${e}`)}function Na(e){for(let t of e.exported)console.log(t.path);for(let t of e.warnings??[])I(`warning: ${t}`);for(let t of e.skipped)I(`skipped ${t.sessionId}: ${t.reason}`);e.indexPath&&console.log(e.indexPath),console.log(`exported ${e.exported.length}, skipped ${e.skipped.length}`)}const Pa=e(import.meta.url)(`../package.json`);function Fa(){return process.env.AIMAX_DATA_DIR||process.cwd()}function Ia(){let e=new t;return e.name(`aimax`).description(`AIMax CLI — runs agent tasks in a containerized environment`).version(Pa.version),ni(e),or(e),Fi(e),Hi(e),ri(e),ja(e),Xi(e),Di(e),rn(e),fi(e),xi(e,Fa),Ei(e,Fa),e}export{ct as a,P as i,Fa as n,N as r,Ia as t};
package/dist/program.js CHANGED
@@ -1 +1 @@
1
- import{n as e,t}from"./program-BZGVIX65.js";export{t as createProgram,e as getDataDir};
1
+ import{n as e,t}from"./program-CShTao8s.js";export{t as createProgram,e as getDataDir};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gencode/cli",
3
- "version": "0.8.16",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "aimax": "./dist/bin.js"
@@ -14,6 +14,7 @@
14
14
  "files": [
15
15
  "dist",
16
16
  "scripts/download-embedding-model.mjs",
17
+ "scripts/copy-export-html-assets.mjs",
17
18
  "CHANGELOG.md"
18
19
  ],
19
20
  "publishConfig": {
@@ -23,8 +24,8 @@
23
24
  "commander": "^14.0.3",
24
25
  "gensign-node": "latest",
25
26
  "log4js": "^6.9.1",
26
- "@gencode/agents": "0.10.14",
27
- "@gencode/shared": "0.2.1"
27
+ "@gencode/agents": "0.11.0",
28
+ "@gencode/shared": "0.2.2"
28
29
  },
29
30
  "devDependencies": {
30
31
  "@types/node": "^22.0.0",
@@ -33,7 +34,7 @@
33
34
  "vitest": "^4.0.18"
34
35
  },
35
36
  "scripts": {
36
- "build": "tsdown",
37
+ "build": "tsdown && node scripts/copy-export-html-assets.mjs",
37
38
  "test": "vitest run",
38
39
  "test:coverage": "vitest run --coverage",
39
40
  "typecheck": "tsc --noEmit",
@@ -0,0 +1,21 @@
1
+ import { cpSync, existsSync, mkdirSync, rmSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ const packageRoot = join(dirname(fileURLToPath(import.meta.url)), "..");
6
+ const srcDir = join(packageRoot, "src", "export-html");
7
+ const distDir = join(packageRoot, "dist", "export-html");
8
+
9
+ if (!existsSync(join(packageRoot, "dist"))) {
10
+ console.log("skip copy-export-html: dist/ not found (run tsdown first)");
11
+ process.exit(0);
12
+ }
13
+
14
+ // marked / highlight.js are now loaded from CDN at view time, so only the
15
+ // template assets need to ship alongside the compiled CLI.
16
+ rmSync(distDir, { recursive: true, force: true });
17
+ mkdirSync(distDir, { recursive: true });
18
+ for (const name of ["template.html", "template.css", "template.js"]) {
19
+ cpSync(join(srcDir, name), join(distDir, name));
20
+ }
21
+ console.log("copied export-html assets to dist/export-html");