document360-writer 0.4.22 → 0.4.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -1,79 +1,79 @@
1
1
  #!/usr/bin/env node
2
- var ps=Object.defineProperty;var fn=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var ms=(e,t)=>{for(var o in t)ps(e,o,{get:t[o],enumerable:!0})};import ve from"picocolors";var H,fs,gs,hs,gn,ot,u,v,L,E,y,Y,se,B=fn(()=>{"use strict";H="#7f56d9",[fs,gs,hs]=[127,86,217],gn=e=>ve.isColorSupported?`\x1B[38;2;${fs};${gs};${hs}m${e}\x1B[39m`:e,ot=e=>ve.bold(gn(e)),u=e=>ve.dim(e),v=e=>ve.red(e),L=e=>ve.yellow(e),E=e=>ve.green(e),y=gn,Y=e=>ve.gray(e),se=e=>ve.bold(e)});var fr={};ms(fr,{doctorCommand:()=>ut,renderDoctorChecks:()=>mr,runDoctorChecks:()=>pr});import{existsSync as Gi}from"node:fs";import{d360GetAll as Xi,getAccessToken as Vi,isExpired as Oo,loadProfileMap as Yi,loadTokens as Ki,packageSkillsDir as dr,projectConfigPath as Ji,readProjectConfig as Qi,resolveActiveProfile as Zi,resolveAuth as el,resolveModelSetting as tl}from"document360-engine";async function pr(e){let t=[],o=Number(process.versions.node.split(".")[0]);t.push(o>=20?{level:"ok",label:`Node ${process.versions.node}`}:{level:"fail",label:`Node ${process.versions.node} \u2014 20+ required`,fix:"Install Node 20 or later (nodejs.org)"});let n=el("auto");t.push(n.kind==="none"?{level:"fail",label:"Claude auth: not configured",fix:"Set ANTHROPIC_API_KEY, or sign in to Claude Code once (subscription reuse)"}:{level:"ok",label:`Claude auth: ${n.kind==="api"?"API key":"subscription"}`});let r=tl(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=Qi(e);if(!s)return t.push({level:"fail",label:`No ${Ji(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/, markdown, capture specs, and d360 config`});let p=s.authoritativeSourceFiles??[];t.push(p.length>0?{level:"ok",label:`Docs scope: ${p.length} folder(s) (${p.slice(0,3).join(", ")}${p.length>3?", \u2026":""})`}:{level:"warn",label:"Docs scope not set (authoritativeSourceFiles empty)",fix:"Run: /scope to choose which folders back the docs"});let h;try{h=Zi(e),t.push({level:"ok",label:`Profile: ${h.name} (${h.connection.name})${h.production?" \u26A0 PRODUCTION":""}`})}catch(R){return t.push({level:"fail",label:`Profile config: ${R.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let f=Ki(h.name);f?Oo(f)&&!f.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Oo(f)?t.push({level:"warn",label:"Document360: token expired \u2014 will auto-refresh on next call"}):t.push({level:"ok",label:`Document360: logged in (until ${new Date(f.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${h.name})`}),t.push(h.project.workspaceId?{level:"ok",label:`Workspace: ${h.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let x=Yi(e,h.name);if(x?x.projectId&&h.project.projectId&&x.projectId!==h.project.projectId?t.push({level:"fail",label:`Category map projectId (${x.projectId}) \u2260 profile projectId (${h.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys(x.articles).length} articles, ${Object.keys(x.categories).length} categories`}):t.push({level:"warn",label:`No d360-category-map.json section for "${h.name}" (created on first /publish)`}),t.push(Gi(dr())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${dr()}`,fix:"Reinstall document360-writer (broken install)"}),f&&(!Oo(f)||f.refreshToken)){let R={profile:h.name,connection:h.connection};try{await Vi(R);let $=await Xi(R,"/v3/projects");t.push({level:"ok",label:`API reachable (${h.connection.apiUrl}) \u2014 ${$.length} project(s) visible`})}catch($){t.push({level:"fail",label:`API call failed: ${$.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function mr(e){let t=[""];for(let r of e){let s=r.level==="ok"?E("\u2713"):r.level==="warn"?L("\u26A0"):v("\u2717");t.push(` ${s} ${r.label}${r.detail?u(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${u("fix:")} ${y(r.fix)}`)}let o=e.filter(r=>r.level==="fail").length,n=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(o===0?E(`\u2713 ${n===0?"All checks passed":`Healthy (${n} warning${n===1?"":"s"})`}`):v(`\u2717 ${o} problem${o===1?"":"s"} found`)),t.push(""),t}async function ut(e,t){console.log(u("Running checks\u2026"));for(let o of mr(await pr(t.cwd)))console.log(o);return{kind:"continue"}}var Bt=fn(()=>{"use strict";B()});import{Command as Ta}from"commander";import{createRequire as Ra}from"node:module";import{AUTH_MODES as ja}from"document360-engine";import{input as Ss}from"@inquirer/prompts";import{loginPkce as Ts,refreshTokens as Rs,toStoredTokens as yn,clearTokens as js,decodeJwtClaims as wn,isExpired as As,loadTokens as Es,saveTokens as xn,resolveActiveProfile as Rt,setProfileProject as Ds,readProjectConfig as Is}from"document360-engine";B();import{select as ks}from"@inquirer/prompts";import{resolveActiveProfile as ws,setProfileProject as ys,resolveProjectId as xs,listWorkspaces as $s}from"document360-engine";async function St(e,t){let o=ws(e,t),n={profile:o.name,connection:o.connection},r=o.project.projectId??xs(n);return{workspaces:await $s(n,r),projectId:r,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var hn=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function fo(e,t){let o=t.toLowerCase();return e.find(n=>(n.name??"").toLowerCase()===o)??e.find(n=>(n.name??"").toLowerCase().startsWith(o))??e.find(n=>n.id.startsWith(t))}function Tt(e,t,o,n){ys(e,t,{projectId:o,workspaceId:n})}async function kn(e,t,o){let n;try{n=await St(e,o)}catch(s){return console.log(v(`Could not list workspaces: ${s.message}`)),1}let r=fo(n.workspaces,t);return r?(Tt(e,n.profile,n.projectId,r.id),console.log(E(`\u2713 Workspace set to "${r.name??r.id}" for profile "${n.profile}".`)),0):(console.log(v(`No workspace matches "${t}". Available: ${n.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function Ge(e,t){let o;try{o=await St(e,t)}catch(f){console.log(v(`Could not list workspaces: ${f.message}`));return}let{workspaces:n,projectId:r,profile:s,current:c}=o;if(n.length===0){console.log(u("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let f of n)console.log(` ${f.id===c?y("\u25CF"):" "} ${hn(f)} ${u(f.id)}`);console.log(u("Run: d360-writer workspace use <name>"));return}let p=await ks({message:"Select the Document360 workspace for this repo:",choices:n.map(f=>({name:`${hn(f)}${f.id===c?" (current)":""}`,value:f.id}))});Tt(e,s,r,p);let h=n.find(f=>f.id===p);console.log(E(`\u2713 Workspace set to "${h?.name??p}" for profile "${s}".`))}B();import bs from"picocolors";function vs(e=process.env){return e.FORCE_HYPERLINK==="0"||!bs.isColorSupported?!1:e.FORCE_HYPERLINK?!0:!!(e.WT_SESSION||e.TERM_PROGRAM==="vscode"||e.TERM_PROGRAM==="iTerm.app"||e.TERM_PROGRAM==="WezTerm"||e.TERM_PROGRAM==="ghostty"||e.VTE_VERSION||e.KONSOLE_VERSION)}function Cs(e,t=e,o){return vs(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Ps=/https?:\/\/[^\s\x1b]+/g;function nt(e,t){return e.replace(Ps,o=>Cs(o,o,t))}function $n(e){return{...wn(e.idToken)??{},...wn(e.accessToken)??{}}}function jt(e){let t=$n(e),o=t.email??t.preferred_username??t.sub??"unknown",n=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${o}${n}`}async function At(e){let t=Rt(process.cwd(),e.profile),o=t.connection;console.log(u(`Profile "${t.name}" \u2192 ${o.name} (${o.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let n=await Ts(o,{manual:e.manual,promptForRedirect:s=>Ss({message:s})},s=>console.log(nt(s))),r=yn(t.name,n);if(xn(r),go(r,t.name,s=>console.log(u(s))),console.log(""),console.log(E(`\u2713 Logged in to "${t.name}" as ${jt(r)}`)),console.log(u(` access token expires: ${r.expiresAt}`)),console.log(u(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{Is(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Ge(process.cwd(),t.name))}catch{}}function go(e,t,o){let r=$n(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(Rt(process.cwd(),t).project.projectId)return;Ds(process.cwd(),t,{projectId:r}),o(` Project ${r} written to profile "${t}".`)}catch{}}async function bn(e){let t=Rt(process.cwd(),e.profile),o=t.connection,n=Es(t.name);if(!n){console.log(v(`Not logged in to Document360 (profile "${t.name}").`)),console.log(u(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${y(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${jt(n)}`),As(n))if(n.refreshToken)try{let r=yn(t.name,await Rs(o,n.refreshToken));xn(r),console.log(E(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(L(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(u(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(L("Session expired (no refresh token).")),console.log(u(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(u(` expires: ${n.expiresAt}`))}async function vn(e){let t=Rt(process.cwd(),e.profile);js(t.name)?console.log(E(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(u(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}B();import{readProjectConfig as Cn,writeProjectConfig as Ms,resolveActiveProfile as _s,loadTokens as Ns,isExpired as Ls}from"document360-engine";function Et(e){let t=Cn(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(v("No profiles in .d360-writer.json. Run: d360-writer init")),process.exitCode=1;return}console.log("");for(let[o,n]of Object.entries(t.profiles)){let r=o===t.defaultProfile?y("\u25CF "):" ",s=n.production?L(" \u26A0 PRODUCTION"):"",c=n.connection.environment??"(inline)",p=Ns(o),h=p?Ls(p)&&!p.refreshToken?L("expired"):u("logged in"):u("not logged in");console.log(`${r}${y(o)} \u2192 ${c}${s} [${h}]`)}console.log(""),console.log(u("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Dt(e,t){let o=Cn(e);if(!o?.profiles?.[t]){let r=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log(v(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}o.defaultProfile=t,Ms(o,e);let n=o.profiles[t].production?L(" \u26A0 PRODUCTION"):"";console.log(E(`\u2713 Default profile is now "${t}"${n}`))}function It(e,t){try{let o=_s(e,t);console.log(""),console.log(`Profile ${y(o.name)}${o.production?L(" \u26A0 PRODUCTION"):""}`),console.log(u(` api: ${o.connection.apiUrl}`)),console.log(u(` identity: ${o.connection.authorizationUrl}`)),console.log(u(` clientId: ${o.connection.clientId}`)),console.log(u(` scopes: ${o.connection.scopes.join(" ")}`)),console.log(u(` project: ${o.project.projectId??"(set at login)"}`)),console.log(u(` workspace:${o.project.workspaceId?" "+o.project.workspaceId:" (none)"}`)),console.log("")}catch(o){console.log(v(o.message)),process.exitCode=1}}B();import{existsSync as Os,readdirSync as Us,statSync as Ws}from"node:fs";import{join as Fs}from"node:path";import{apiLogDir as Bs}from"document360-engine";function Pn(){let e=Bs();if(console.log(""),console.log(`Document360 API logs: ${y(e)}`),!Os(e)){console.log(u(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=Us(e).filter(o=>o.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(u(" No logs yet \u2014 they appear after the first Document360 API call."));for(let o of t.slice(0,14)){let n=(Ws(Fs(e,o)).size/1024).toFixed(1);console.log(` ${o} ${u(`${n} KB`)}`)}console.log(""),console.log(u("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(u("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}B();import{createSession as Hs,resolveAuth as qs,findByName as zs,slugify as Gs,touchSession as Xs,upsertSession as Vs,resolveActiveProfile as Ys}from"document360-engine";async function Sn(e,t,o,n,r,s){let c=qs(o);c.kind==="none"&&(console.error(""),console.error(v("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${y("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${y("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(u("Using your Claude subscription (no API key set)."));let p=null;try{p=Ys(e,r)}catch($){console.error(v(`Document360 profile error: ${$.message}`)),process.exit(2)}p.production&&(console.error(L(`\u26A0 Profile "${p.name}" is PRODUCTION.`)),s||(console.error(v("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(u(" --yes given \u2014 proceeding against production.")));let h;if(n){let $=zs(e,n);$||(console.error(v(`No saved session matches "${n}" in this repo.`)),console.error(u("List sessions inside the REPL with /resume.")),process.exit(2)),h=$.uuid,console.error(u(`Resuming "${$.name}"`))}let f=Hs({cwd:e,resume:h,profileName:r,allowProdWrites:s===!0}),x=h??null,R=1;for await(let $ of f.send(t))switch($.type){case"session":if(!x){x=$.sessionId;let j=new Date().toISOString();Vs({uuid:x,name:Gs(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:j,updatedAt:j})}break;case"text":process.stdout.write($.delta);break;case"tool":console.error(Y(` \u2699 ${$.name}`));break;case"result":R=$.ok?0:1,console.error(u(`(${$.inputTokens}\u2192${$.outputTokens} tokens`+($.costUsd>0?`, $${$.costUsd<.01?$.costUsd.toFixed(4):$.costUsd.toFixed(2)}`:"")+")")),$.ok||console.error(v("agent finished with an error result"));break;case"error":console.error(""),console.error(v(`agent error: ${$.message}`)),process.exit(1)}x&&Xs(x),process.stdout.write(`
3
- `),process.exit(R)}import{createInterface as kl}from"node:readline/promises";import{createSession as qo,resolveAuth as wl,getSession as yl,setTitle as xl,slugify as $l,touchSession as Rr,upsertSession as bl,generateTitle as vl,resolveActiveProfile as jr,resolveModelSetting as Cl,readProjectConfig as Pl,decodeJwtClaims as Sr,isExpired as Sl,loadTokens as Tl}from"document360-engine";B();async function ho(){let e=["",se("document360-writer \u2014 slash commands"),"",` ${y("/help")} ${u("Show this help")}`,` ${y("/init")} ${u("Pick a Document360 environment & scaffold .d360-writer.json")}`,` ${y("/mcp add <name> <type> <ref>")} ${u("Register an MCP server (stdio|http|sse; -H key:value for auth)")}`,` ${y("/mcp list")} ${u("Show registered MCP servers")}`,` ${y("/mcp remove <name>")} ${u("Unregister an MCP server")}`,` ${y("/publish [path|--all]")} ${u("Publish to Document360 (no arg: pick; --all: every candidate)")}`,` ${y("/sync")} ${u("Drift report: local docs vs Document360 (no tokens)")}`,` ${y("/sync pull <path>|--all")} ${u("Pull portal edits into local markdown (diff + confirm)")}`,` ${y("/convert [--scope <f>] [--run]")} ${u("Convert tracked articles to DFM across parallel agents (--scope to limit; --run to start)")}`,` ${y("/scope")} ${u("Choose which repo folders back the docs (analyses + recommends)")}`,` ${y("/audit")} ${u("Gap analysis: code vs docs vs Document360 (incremental)")}`,` ${y("/screenshot [--list|id|--all|path]")} ${u("--list: show all placeholders + status; else author specs (id/--all/path)")}`,` ${y("/capture-setup")} ${u('Generate the "stage this data" checklist for screenshots')}`,` ${y("/resume [name]")} ${u("Resume a session (no arg: searchable picker)")}`,` ${y("/rename <name>")} ${u("Name the current session")}`,` ${y("/login")} ${u("Sign in to Document360 (browser) without leaving the session")}`,` ${y("/profile [name|add <name> [env]]")} ${u("Switch connection profile, or create one from a preset")}`,` ${y("/model [name|default]")} ${u("Show or set the Claude model (personal setting)")}`,` ${y("/doctor")} ${u("Health-check: auth, profile, workspace, map, API")}`,` ${y("/workspace [name]")} ${u("List or switch the Document360 workspace")}`,` ${y("/allow-prod")} ${u("Authorize writes to a production profile (this session)")}`,` ${y("/clear")} ${u("Reset conversation")}`,` ${y("/exit")} ${u("Quit")}`,"",u("Anything not starting with / is sent to the agent."),u("Reporting a problem? Run `d360-writer logs` from your shell for the API log files."),""];for(let t of e)console.log(t);return{kind:"continue"}}B();import{getSession as Ks}from"document360-engine";async function Tn(e,t){let o=t.currentUuid(),n=o?Ks(o):void 0;return console.log(n?u(`
2
+ var ms=Object.defineProperty;var gn=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var fs=(e,t)=>{for(var o in t)ms(e,o,{get:t[o],enumerable:!0})};import ve from"picocolors";var B,gs,hs,ks,hn,ot,p,b,L,E,P,Y,he,H=gn(()=>{"use strict";B="#7f56d9",[gs,hs,ks]=[127,86,217],hn=e=>ve.isColorSupported?`\x1B[38;2;${gs};${hs};${ks}m${e}\x1B[39m`:e,ot=e=>ve.bold(hn(e)),p=e=>ve.dim(e),b=e=>ve.red(e),L=e=>ve.yellow(e),E=e=>ve.green(e),P=hn,Y=e=>ve.gray(e),he=e=>ve.bold(e)});var kr={};fs(kr,{doctorCommand:()=>dt,renderDoctorChecks:()=>hr,runDoctorChecks:()=>gr});import{existsSync as Vi}from"node:fs";import{d360GetAll as Yi,getAccessToken as Ki,isExpired as Uo,loadProfileMap as Ji,loadTokens as Qi,packageSkillsDir as fr,projectConfigPath as Zi,readProjectConfig as el,resolveActiveProfile as tl,resolveAuth as ol,resolveModelSetting as nl}from"document360-engine";async function gr(e){let t=[],o=Number(process.versions.node.split(".")[0]);t.push(o>=20?{level:"ok",label:`Node ${process.versions.node}`}:{level:"fail",label:`Node ${process.versions.node} \u2014 20+ required`,fix:"Install Node 20 or later (nodejs.org)"});let n=ol("auto");t.push(n.kind==="none"?{level:"fail",label:"Claude auth: not configured",fix:"Set ANTHROPIC_API_KEY, or sign in to Claude Code once (subscription reuse)"}:{level:"ok",label:`Claude auth: ${n.kind==="api"?"API key":"subscription"}`});let r=nl(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=el(e);if(!s)return t.push({level:"fail",label:`No ${Zi(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/, markdown, capture specs, and d360 config`});let d=s.authoritativeSourceFiles??[];t.push(d.length>0?{level:"ok",label:`Docs scope: ${d.length} folder(s) (${d.slice(0,3).join(", ")}${d.length>3?", \u2026":""})`}:{level:"warn",label:"Docs scope not set (authoritativeSourceFiles empty)",fix:"Run: /scope to choose which folders back the docs"});let h;try{h=tl(e),t.push({level:"ok",label:`Profile: ${h.name} (${h.connection.name})${h.production?" \u26A0 PRODUCTION":""}`})}catch(R){return t.push({level:"fail",label:`Profile config: ${R.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let f=Qi(h.name);f?Uo(f)&&!f.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Uo(f)?t.push({level:"warn",label:"Document360: token expired \u2014 will auto-refresh on next call"}):t.push({level:"ok",label:`Document360: logged in (until ${new Date(f.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${h.name})`}),t.push(h.project.workspaceId?{level:"ok",label:`Workspace: ${h.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let w=Ji(e,h.name);if(w?w.projectId&&h.project.projectId&&w.projectId!==h.project.projectId?t.push({level:"fail",label:`Category map projectId (${w.projectId}) \u2260 profile projectId (${h.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys(w.articles).length} articles, ${Object.keys(w.categories).length} categories`}):t.push({level:"warn",label:`No d360-category-map.json section for "${h.name}" (created on first /publish)`}),t.push(Vi(fr())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${fr()}`,fix:"Reinstall document360-writer (broken install)"}),f&&(!Uo(f)||f.refreshToken)){let R={profile:h.name,connection:h.connection};try{await Ki(R);let x=await Yi(R,"/v3/projects");t.push({level:"ok",label:`API reachable (${h.connection.apiUrl}) \u2014 ${x.length} project(s) visible`})}catch(x){t.push({level:"fail",label:`API call failed: ${x.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function hr(e){let t=[""];for(let r of e){let s=r.level==="ok"?E("\u2713"):r.level==="warn"?L("\u26A0"):b("\u2717");t.push(` ${s} ${r.label}${r.detail?p(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${p("fix:")} ${P(r.fix)}`)}let o=e.filter(r=>r.level==="fail").length,n=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(o===0?E(`\u2713 ${n===0?"All checks passed":`Healthy (${n} warning${n===1?"":"s"})`}`):b(`\u2717 ${o} problem${o===1?"":"s"} found`)),t.push(""),t}async function dt(e,t){console.log(p("Running checks\u2026"));for(let o of hr(await gr(t.cwd)))console.log(o);return{kind:"continue"}}var qt=gn(()=>{"use strict";H()});import{Command as ja}from"commander";import{createRequire as Aa}from"node:module";import{AUTH_MODES as Ea}from"document360-engine";import{input as Ts}from"@inquirer/prompts";import{loginPkce as Rs,refreshTokens as js,toStoredTokens as xn,clearTokens as As,decodeJwtClaims as wn,isExpired as Es,loadTokens as Ds,saveTokens as $n,resolveActiveProfile as jt,setProfileProject as Is,readProjectConfig as _s}from"document360-engine";H();import{select as ys}from"@inquirer/prompts";import{resolveActiveProfile as ws,setProfileProject as xs,resolveProjectId as $s,listWorkspaces as bs}from"document360-engine";async function Tt(e,t){let o=ws(e,t),n={profile:o.name,connection:o.connection},r=o.project.projectId??$s(n);return{workspaces:await bs(n,r),projectId:r,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var kn=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function go(e,t){let o=t.toLowerCase();return e.find(n=>(n.name??"").toLowerCase()===o)??e.find(n=>(n.name??"").toLowerCase().startsWith(o))??e.find(n=>n.id.startsWith(t))}function Rt(e,t,o,n){xs(e,t,{projectId:o,workspaceId:n})}async function yn(e,t,o){let n;try{n=await Tt(e,o)}catch(s){return console.log(b(`Could not list workspaces: ${s.message}`)),1}let r=go(n.workspaces,t);return r?(Rt(e,n.profile,n.projectId,r.id),console.log(E(`\u2713 Workspace set to "${r.name??r.id}" for profile "${n.profile}".`)),0):(console.log(b(`No workspace matches "${t}". Available: ${n.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function Ge(e,t){let o;try{o=await Tt(e,t)}catch(f){console.log(b(`Could not list workspaces: ${f.message}`));return}let{workspaces:n,projectId:r,profile:s,current:c}=o;if(n.length===0){console.log(p("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let f of n)console.log(` ${f.id===c?P("\u25CF"):" "} ${kn(f)} ${p(f.id)}`);console.log(p("Run: d360-writer workspace use <name>"));return}let d=await ys({message:"Select the Document360 workspace for this repo:",choices:n.map(f=>({name:`${kn(f)}${f.id===c?" (current)":""}`,value:f.id}))});Rt(e,s,r,d);let h=n.find(f=>f.id===d);console.log(E(`\u2713 Workspace set to "${h?.name??d}" for profile "${s}".`))}H();import vs from"picocolors";function Cs(e=process.env){return e.FORCE_HYPERLINK==="0"||!vs.isColorSupported?!1:e.FORCE_HYPERLINK?!0:!!(e.WT_SESSION||e.TERM_PROGRAM==="vscode"||e.TERM_PROGRAM==="iTerm.app"||e.TERM_PROGRAM==="WezTerm"||e.TERM_PROGRAM==="ghostty"||e.VTE_VERSION||e.KONSOLE_VERSION)}function Ps(e,t=e,o){return Cs(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Ss=/https?:\/\/[^\s\x1b]+/g;function nt(e,t){return e.replace(Ss,o=>Ps(o,o,t))}function bn(e){return{...wn(e.idToken)??{},...wn(e.accessToken)??{}}}function At(e){let t=bn(e),o=t.email??t.preferred_username??t.sub??"unknown",n=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${o}${n}`}async function Et(e){let t=jt(process.cwd(),e.profile),o=t.connection;console.log(p(`Profile "${t.name}" \u2192 ${o.name} (${o.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let n=await Rs(o,{manual:e.manual,promptForRedirect:s=>Ts({message:s})},s=>console.log(nt(s))),r=xn(t.name,n);if($n(r),ho(r,t.name,s=>console.log(p(s))),console.log(""),console.log(E(`\u2713 Logged in to "${t.name}" as ${At(r)}`)),console.log(p(` access token expires: ${r.expiresAt}`)),console.log(p(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{_s(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Ge(process.cwd(),t.name))}catch{}}function ho(e,t,o){let r=bn(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(jt(process.cwd(),t).project.projectId)return;Is(process.cwd(),t,{projectId:r}),o(` Project ${r} written to profile "${t}".`)}catch{}}async function vn(e){let t=jt(process.cwd(),e.profile),o=t.connection,n=Ds(t.name);if(!n){console.log(b(`Not logged in to Document360 (profile "${t.name}").`)),console.log(p(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${P(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${At(n)}`),Es(n))if(n.refreshToken)try{let r=xn(t.name,await js(o,n.refreshToken));$n(r),console.log(E(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(L(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(p(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(L("Session expired (no refresh token).")),console.log(p(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(p(` expires: ${n.expiresAt}`))}async function Cn(e){let t=jt(process.cwd(),e.profile);As(t.name)?console.log(E(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(p(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}H();import{readProjectConfig as Pn,writeProjectConfig as Ms,resolveActiveProfile as Ns,loadTokens as Ls,isExpired as Os}from"document360-engine";function Dt(e){let t=Pn(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(b("No profiles in .d360-writer.json. Run: d360-writer init")),process.exitCode=1;return}console.log("");for(let[o,n]of Object.entries(t.profiles)){let r=o===t.defaultProfile?P("\u25CF "):" ",s=n.production?L(" \u26A0 PRODUCTION"):"",c=n.connection.environment??"(inline)",d=Ls(o),h=d?Os(d)&&!d.refreshToken?L("expired"):p("logged in"):p("not logged in");console.log(`${r}${P(o)} \u2192 ${c}${s} [${h}]`)}console.log(""),console.log(p("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function It(e,t){let o=Pn(e);if(!o?.profiles?.[t]){let r=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log(b(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}o.defaultProfile=t,Ms(o,e);let n=o.profiles[t].production?L(" \u26A0 PRODUCTION"):"";console.log(E(`\u2713 Default profile is now "${t}"${n}`))}function _t(e,t){try{let o=Ns(e,t);console.log(""),console.log(`Profile ${P(o.name)}${o.production?L(" \u26A0 PRODUCTION"):""}`),console.log(p(` api: ${o.connection.apiUrl}`)),console.log(p(` identity: ${o.connection.authorizationUrl}`)),console.log(p(` clientId: ${o.connection.clientId}`)),console.log(p(` scopes: ${o.connection.scopes.join(" ")}`)),console.log(p(` project: ${o.project.projectId??"(set at login)"}`)),console.log(p(` workspace:${o.project.workspaceId?" "+o.project.workspaceId:" (none)"}`)),console.log("")}catch(o){console.log(b(o.message)),process.exitCode=1}}H();import{existsSync as Us,readdirSync as Ws,statSync as Fs}from"node:fs";import{join as Bs}from"node:path";import{apiLogDir as Hs}from"document360-engine";function Sn(){let e=Hs();if(console.log(""),console.log(`Document360 API logs: ${P(e)}`),!Us(e)){console.log(p(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=Ws(e).filter(o=>o.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(p(" No logs yet \u2014 they appear after the first Document360 API call."));for(let o of t.slice(0,14)){let n=(Fs(Bs(e,o)).size/1024).toFixed(1);console.log(` ${o} ${p(`${n} KB`)}`)}console.log(""),console.log(p("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(p("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}H();import{createSession as qs,resolveAuth as zs,findByName as Gs,slugify as Xs,touchSession as Vs,upsertSession as Ys,resolveActiveProfile as Ks}from"document360-engine";async function Tn(e,t,o,n,r,s){let c=zs(o);c.kind==="none"&&(console.error(""),console.error(b("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${P("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${P("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(p("Using your Claude subscription (no API key set)."));let d=null;try{d=Ks(e,r)}catch(x){console.error(b(`Document360 profile error: ${x.message}`)),process.exit(2)}d.production&&(console.error(L(`\u26A0 Profile "${d.name}" is PRODUCTION.`)),s||(console.error(b("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(p(" --yes given \u2014 proceeding against production.")));let h;if(n){let x=Gs(e,n);x||(console.error(b(`No saved session matches "${n}" in this repo.`)),console.error(p("List sessions inside the REPL with /resume.")),process.exit(2)),h=x.uuid,console.error(p(`Resuming "${x.name}"`))}let f=qs({cwd:e,resume:h,profileName:r,allowProdWrites:s===!0}),w=h??null,R=1;for await(let x of f.send(t))switch(x.type){case"session":if(!w){w=x.sessionId;let j=new Date().toISOString();Ys({uuid:w,name:Xs(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:j,updatedAt:j})}break;case"text":process.stdout.write(x.delta);break;case"tool":console.error(Y(` \u2699 ${x.name}`));break;case"result":R=x.ok?0:1,console.error(p(`(${x.inputTokens}\u2192${x.outputTokens} tokens`+(x.costUsd>0?`, $${x.costUsd<.01?x.costUsd.toFixed(4):x.costUsd.toFixed(2)}`:"")+")")),x.ok||console.error(b("agent finished with an error result"));break;case"error":console.error(""),console.error(b(`agent error: ${x.message}`)),process.exit(1)}w&&Vs(w),process.stdout.write(`
3
+ `),process.exit(R)}import{createInterface as wl}from"node:readline/promises";import{createSession as zo,resolveAuth as xl,getSession as $l,setTitle as bl,slugify as vl,touchSession as Er,upsertSession as Cl,generateTitle as Pl,resolveActiveProfile as Dr,resolveModelSetting as Sl,readProjectConfig as Tl,decodeJwtClaims as jr,isExpired as Rl,loadTokens as jl}from"document360-engine";var rt=[{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json",group:"start"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser)",group:"start"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: placeholders + status; else author capture specs",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Write the data-staging checklist + the dev test-id requests",group:"docs"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)",group:"docs"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)",group:"publish"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (what changed)",group:"publish"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits",group:"publish"},{name:"convert",usage:"/convert [--scope <folder>] [--run]",desc:"Convert tracked articles to DFM (one-off legacy migration)",group:"publish"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch/create a Document360 connection (picker; s = session)",group:"setup"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)",group:"setup"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer",group:"setup"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile",group:"setup"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API",group:"setup"},{name:"resume",usage:"/resume [name]",desc:"Resume a session (no arg lists them)",group:"session"},{name:"rename",usage:"/rename [name]",desc:"Name the session (no arg: suggest one)",group:"session"},{name:"clear",usage:"/clear",desc:"Reset the conversation (resumable)",group:"session"},{name:"help",usage:"/help",desc:"Show this help",group:"session"},{name:"exit",usage:"/exit",desc:"Quit",group:"session"}],Js=[{key:"start",label:"Start here"},{key:"docs",label:"Write & screenshots"},{key:"publish",label:"Publish & keep in sync"},{key:"setup",label:"Setup & health"},{key:"session",label:"Session"}];function Mt(){let e=Math.max(...rt.map(o=>o.usage.length))+2,t=["document360-writer \u2014 commands","",'New here? /init \u2192 "write the docs for this repo" \u2192 /screenshot --all \u2192 /capture-setup \u2192 /publish'];for(let{key:o,label:n}of Js){t.push("",n);for(let r of rt.filter(s=>s.group===o))t.push(` ${r.usage.padEnd(e)}${r.desc}`)}return t.push("","Tip: anything not starting with / is sent to the agent."),t}function Rn(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return rt.filter(o=>o.name.startsWith(t))}function jn(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function ko(){console.log("");for(let e of Mt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}H();import{getSession as Qs}from"document360-engine";async function An(e,t){let o=t.currentUuid(),n=o?Qs(o):void 0;return console.log(n?p(`
4
4
  (conversation reset \u2014 "${n.name}" is still available via /resume)
5
- `):u(`
5
+ `):p(`
6
6
  (conversation reset \u2014 agent will start fresh on the next prompt)
7
- `)),{kind:"clear"}}async function ko(){return{kind:"exit"}}B();import{input as rt,confirm as Js}from"@inquirer/prompts";import{basename as Rn,join as wo}from"node:path";import{existsSync as st,readFileSync as Qs,readdirSync as Zs}from"node:fs";import{writeProjectConfig as jn,readProjectConfig as ei,projectConfigPath as An}from"document360-engine";function En(e,t="berlin"){let o=An(e);if(st(o))return{created:!1,path:o,profileName:""};let n={projectId:In(e)??Rn(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:Mn(e)};return jn(n,e),{created:!0,path:o,profileName:t}}async function Dn(){let e=process.cwd(),t=An(e);if(st(t)&&!await Js({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(u("init cancelled.")),{kind:"continue"};let o=In(e)??Rn(e),n=await rt({message:"Project ID (used to scope sessions, screenshots, etc.):",default:o}),r=await rt({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await rt({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await rt({message:"Default connection profile name:",default:"berlin"}),p=await rt({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),h={projectId:n,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:p},production:!1}},defaultProfile:c,authoritativeSourceFiles:Mn(e)},f=ei(e);return f?.terminologyGlossary&&(h.terminologyGlossary=f.terminologyGlossary),jn(h,e),console.log(""),console.log(E(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${y(`d360-writer login --profile ${c}`)} ${u("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function In(e){let t=wo(e,"package.json");if(!st(t))return null;try{return JSON.parse(Qs(t,"utf8")).name??null}catch{return null}}function Mn(e){let t=[];for(let o of["README.md","ARCHITECTURE.md","CLAUDE.md"])st(wo(e,o))&&t.push(o);for(let o of["src","api","docs"]){let n=wo(e,o);st(n)&&!ti(n)&&t.push(o)}return t}function ti(e){try{return Zs(e,{withFileTypes:!0}).filter(o=>o.isDirectory()&&!o.name.startsWith(".")).length>6}catch{return!1}}B();import{readMcpConfig as yo,writeMcpConfig as _n}from"document360-engine";async function Mt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(oi(),{kind:"continue"}):t==="add"?(ni(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(ri(e.slice(1)),{kind:"continue"}):(console.log(v(`Unknown /mcp subcommand: ${t}`)),console.log(u("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function oi(){let e=yo(),t=Object.keys(e.servers);if(t.length===0){console.log(u(`
7
+ `)),{kind:"clear"}}async function yo(){return{kind:"exit"}}H();import{input as st,confirm as Zs}from"@inquirer/prompts";import{basename as En,join as wo}from"node:path";import{existsSync as it,readFileSync as ei,readdirSync as ti}from"node:fs";import{writeProjectConfig as Dn,readProjectConfig as oi,projectConfigPath as In}from"document360-engine";function _n(e,t="berlin"){let o=In(e);if(it(o))return{created:!1,path:o,profileName:""};let n={projectId:Nn(e)??En(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:Ln(e)};return Dn(n,e),{created:!0,path:o,profileName:t}}async function Mn(){let e=process.cwd(),t=In(e);if(it(t)&&!await Zs({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(p("init cancelled.")),{kind:"continue"};let o=Nn(e)??En(e),n=await st({message:"Project ID (used to scope sessions, screenshots, etc.):",default:o}),r=await st({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await st({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await st({message:"Default connection profile name:",default:"berlin"}),d=await st({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),h={projectId:n,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:d},production:!1}},defaultProfile:c,authoritativeSourceFiles:Ln(e)},f=oi(e);return f?.terminologyGlossary&&(h.terminologyGlossary=f.terminologyGlossary),Dn(h,e),console.log(""),console.log(E(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${P(`d360-writer login --profile ${c}`)} ${p("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function Nn(e){let t=wo(e,"package.json");if(!it(t))return null;try{return JSON.parse(ei(t,"utf8")).name??null}catch{return null}}function Ln(e){let t=[];for(let o of["README.md","ARCHITECTURE.md","CLAUDE.md"])it(wo(e,o))&&t.push(o);for(let o of["src","api","docs"]){let n=wo(e,o);it(n)&&!ni(n)&&t.push(o)}return t}function ni(e){try{return ti(e,{withFileTypes:!0}).filter(o=>o.isDirectory()&&!o.name.startsWith(".")).length>6}catch{return!1}}H();import{readMcpConfig as xo,writeMcpConfig as On}from"document360-engine";async function Nt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(ri(),{kind:"continue"}):t==="add"?(si(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(ii(e.slice(1)),{kind:"continue"}):(console.log(b(`Unknown /mcp subcommand: ${t}`)),console.log(p("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function ri(){let e=xo(),t=Object.keys(e.servers);if(t.length===0){console.log(p(`
8
8
  No MCP servers registered. Add one with /mcp add <name> <type> <ref>
9
- `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let o of t){let n=e.servers[o];if(n.type==="stdio")console.log(` ${y(o)} ${u("(stdio)")} ${n.command} ${(n.args??[]).join(" ")}`);else{let r=Object.keys(n.headers??{}),s=r.length>0?u(` [headers: ${r.join(", ")}]`):"";console.log(` ${y(o)} ${u(`(${n.type})`)} ${n.url}${s}`)}}console.log("")}function ni(e){let[t,o,...n]=e;if(!t||!o||n.length===0){console.log(v("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(o!=="stdio"&&o!=="http"&&o!=="sse"){console.log(v(`Unknown transport: ${o}. Use stdio, http, or sse.`));return}let r=yo(),s;if(o==="stdio")s={type:"stdio",command:n[0],args:n.slice(1)};else{let c={};for(let p=1;p<n.length;p++)if(n[p]==="-H"||n[p]==="--header"){let h=n[++p],f=h?.match(/^([^:=]+)[:=](.+)$/);if(!f){console.log(v(`-H expects key:value (no spaces). Got: ${h??"(nothing)"}`));return}c[f[1].trim()]=f[2].trim()}else{console.log(v(`Unexpected argument: ${n[p]}. After the URL, only -H key:value is allowed.`));return}s={type:o,url:n[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,_n(r),console.log(""),console.log(E(`\u2713 Registered "${t}" (${o})`)),console.log(L(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(u(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function ri(e){let t=e[0];if(!t){console.log(v("Usage: /mcp remove <name>"));return}let o=yo();if(!o.servers[t]){console.log(v(`No server named "${t}".`));return}delete o.servers[t],_n(o),console.log(E(`\u2713 Removed "${t}".`)),console.log(u(" Run /clear to drop it from the current session immediately."))}B();import{select as si}from"@inquirer/prompts";import{computeSyncStatus as Nn}from"document360-engine";function Nt(e){return[`Run the publish-to-d360 skill for the article at: ${e}`,"","Steps you must follow:","1. Read the article markdown \u2014 use the repo-relative path exactly as given above (it resolves against the working directory; do not reconstruct an absolute path).","2. Compute the D360 publish form (strip the YAML frontmatter and the H1 title, strip every <!-- SCREENSHOT ... --> block while keeping the visible [Screenshot: ...] line, normalize cross-article links).","3. Look up the article ID in <repo>/d360-category-map.json. If present, call the document360 MCP update-article tool. If absent, call create-article with local_path set to the repo-relative .md path (the tool records the new ID + sync base in the category map itself \u2014 do not edit the articles map by hand).","4. Report what changed and remind me to publish the draft manually in the Document360 portal."].join(`
10
- `)}var Ln={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function _t(e){return e.filter(t=>t.path!==null&&t.status in Ln).map(t=>({path:t.path,label:Ln[t.status]}))}function it(e){return[`Run the publish-to-d360 skill for ALL of these ${e.length} articles, one by one:`,"",...e.map(t=>`- ${t}`),"","Use the repo-relative paths exactly as listed (they resolve against the working directory; do not reconstruct absolute paths). Apply the normal per-article publish flow (frontmatter/H1/screenshot-comment strips, link normalization; update when mapped, create with local_path when not). If one article fails, note it and continue. Finish with a summary table (article | created/updated | failures) and remind me drafts need review in the portal."].join(`
11
- `)}async function On(e,t){let o=e[0];if(o==="--all"){console.log(u("Checking what needs publishing\u2026"));try{let n=_t((await Nn({cwd:t?.cwd??process.cwd()})).entries);return n.length===0?(console.log(E("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:it(n.map(r=>r.path)),display:"/publish --all"}}catch(n){return console.log(v(`Could not compute sync status: ${n.message}`)),{kind:"continue"}}}if(!o){console.log(u("Checking what needs publishing\u2026"));let n;try{n=_t((await Nn({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(v(`Could not compute sync status: ${r.message}`)),console.log(u("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(n.length===0)return console.log(E("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(u(" (Articles without a sync base are unverified \u2014 publish those by path if needed.)")),{kind:"continue"};if(!process.stdin.isTTY||!t){for(let r of n)console.log(` ${r.path} ${u(`(${r.label})`)}`);return console.log(u("Run: /publish <article-path>")),{kind:"continue"}}try{o=await t.withPausedInput(()=>si({message:"Publish which article?",choices:[...n.length>1?[{name:`All ${n.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...n.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(u("Cancelled.")),{kind:"continue"}}if(o==="--all")return{kind:"forward-to-agent",prompt:it(n.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:Nt(o),display:`/publish ${o}`}}async function Lt(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","3. Map changed files to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
12
- `),display:"/audit"}}B();import{checkbox as ii}from"@inquirer/prompts";import{inventoryRepo as li,readProjectConfig as Un,writeProjectConfig as ai}from"document360-engine";function xo(e,t){let o=Un(e);o&&(o.authoritativeSourceFiles=t,ai(o,e))}function Ot(e){let t=[`${e.fileCount}${e.fileCount>=5e3?"+":""} files`];return e.stacks.length&&t.push(e.stacks.join("+")),t.push(e.reason),t.join(" \xB7 ")}async function Wn(e,t){let o=t?.cwd??process.cwd();if(!Un(o))return console.log(v("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let n=li(o);if(n.length===0)return console.log(u('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(y("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of n)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${u(Ot(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>ii({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:n.map(s=>({name:`${s.path} (${Ot(s)})`,value:s.path,checked:s.recommended})),pageSize:20}))}catch{return console.log(u("Cancelled.")),{kind:"continue"}}if(r.length===0)return console.log(u("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};xo(o,r),console.log(E(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let s of r)console.log(` ${s}`);return console.log(""),{kind:"continue"}}B();import{confirm as mi}from"@inquirer/prompts";import{applyPull as zn,computeSyncStatus as Gn,planPull as fi,D360AuthError as gi}from"document360-engine";B();var Fn=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:v,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:L,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:y,mark:"M"},{status:"deleted-local",header:"Deleted locally \u2014 still on Document360 (pull to restore, or remove from the map):",paint:v,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:v,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:Y,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:u,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:u,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:Y,mark:"\xB7"}];function ci(e){return e.path?e.path:`${e.title??"(untitled)"} ${Y(`[${e.articleId}]`)}`}function Ut(e){let t=[];for(let c of Fn){let p=e.entries.filter(h=>h.status===c.status);if(p.length!==0){t.push(""),t.push(se(c.header));for(let h of p)t.push(` ${c.paint(c.mark)} ${c.paint(ci(h))}${h.detail?Y(` (${h.detail})`):""}`)}}let o=e.counts["in-sync"]??0,n=e.entries.length;t.push("");let r={"in-sync":"in sync","local-ahead":"local ahead","remote-ahead":"remote ahead",conflict:"conflicts","untracked-local":"untracked local","untracked-remote":"untracked remote","deleted-local":"deleted locally","deleted-remote":"deleted on D360",orphaned:"orphaned","unknown-base":"no base yet"},s=n===o?E("\u2713 everything in sync"):[o>0?E(`\u2713 ${o} in sync`):null,...Fn.map(c=>{let p=e.counts[c.status]??0;return p>0?`${p} ${r[c.status]}`:null})].filter(Boolean).join(Y(" \xB7 "));return t.push(s+Y(` \xB7 ${n} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as ui}from"diff";import Bn from"picocolors";var Hn=80,di="\x1B[48;2;74;28;28m",pi="\x1B[48;2;24;66;24m",qn="\x1B[49m",$o=e=>String(e).padStart(5);function _e(e,t,o){let n=$=>{let j=$.replace(/\r\n/g,`
9
+ `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let o of t){let n=e.servers[o];if(n.type==="stdio")console.log(` ${P(o)} ${p("(stdio)")} ${n.command} ${(n.args??[]).join(" ")}`);else{let r=Object.keys(n.headers??{}),s=r.length>0?p(` [headers: ${r.join(", ")}]`):"";console.log(` ${P(o)} ${p(`(${n.type})`)} ${n.url}${s}`)}}console.log("")}function si(e){let[t,o,...n]=e;if(!t||!o||n.length===0){console.log(b("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(o!=="stdio"&&o!=="http"&&o!=="sse"){console.log(b(`Unknown transport: ${o}. Use stdio, http, or sse.`));return}let r=xo(),s;if(o==="stdio")s={type:"stdio",command:n[0],args:n.slice(1)};else{let c={};for(let d=1;d<n.length;d++)if(n[d]==="-H"||n[d]==="--header"){let h=n[++d],f=h?.match(/^([^:=]+)[:=](.+)$/);if(!f){console.log(b(`-H expects key:value (no spaces). Got: ${h??"(nothing)"}`));return}c[f[1].trim()]=f[2].trim()}else{console.log(b(`Unexpected argument: ${n[d]}. After the URL, only -H key:value is allowed.`));return}s={type:o,url:n[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,On(r),console.log(""),console.log(E(`\u2713 Registered "${t}" (${o})`)),console.log(L(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(p(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function ii(e){let t=e[0];if(!t){console.log(b("Usage: /mcp remove <name>"));return}let o=xo();if(!o.servers[t]){console.log(b(`No server named "${t}".`));return}delete o.servers[t],On(o),console.log(E(`\u2713 Removed "${t}".`)),console.log(p(" Run /clear to drop it from the current session immediately."))}H();import{select as li}from"@inquirer/prompts";import{computeSyncStatus as Un}from"document360-engine";function Ot(e){return[`Run the publish-to-d360 skill for the article at: ${e}`,"","Steps you must follow:","1. Read the article markdown \u2014 use the repo-relative path exactly as given above (it resolves against the working directory; do not reconstruct an absolute path).","2. Compute the D360 publish form (strip the YAML frontmatter and the H1 title, strip every <!-- SCREENSHOT ... --> block while keeping the visible [Screenshot: ...] line, normalize cross-article links).","3. Look up the article ID in <repo>/d360-category-map.json. If present, call the document360 MCP update-article tool. If absent, call create-article with local_path set to the repo-relative .md path (the tool records the new ID + sync base in the category map itself \u2014 do not edit the articles map by hand).","4. Report what changed and remind me to publish the draft manually in the Document360 portal."].join(`
10
+ `)}var Wn={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Lt(e){return e.filter(t=>t.path!==null&&t.status in Wn).map(t=>({path:t.path,label:Wn[t.status]}))}function lt(e){return[`Run the publish-to-d360 skill for ALL of these ${e.length} articles, one by one:`,"",...e.map(t=>`- ${t}`),"","Use the repo-relative paths exactly as listed (they resolve against the working directory; do not reconstruct absolute paths). Apply the normal per-article publish flow (frontmatter/H1/screenshot-comment strips, link normalization; update when mapped, create with local_path when not). If one article fails, note it and continue. Finish with a summary table (article | created/updated | failures) and remind me drafts need review in the portal."].join(`
11
+ `)}async function Fn(e,t){let o=e[0];if(o==="--all"){console.log(p("Checking what needs publishing\u2026"));try{let n=Lt((await Un({cwd:t?.cwd??process.cwd()})).entries);return n.length===0?(console.log(E("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:lt(n.map(r=>r.path)),display:"/publish --all"}}catch(n){return console.log(b(`Could not compute sync status: ${n.message}`)),{kind:"continue"}}}if(!o){console.log(p("Checking what needs publishing\u2026"));let n;try{n=Lt((await Un({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(b(`Could not compute sync status: ${r.message}`)),console.log(p("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(n.length===0)return console.log(E("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(p(" (Articles without a sync base are unverified \u2014 publish those by path if needed.)")),{kind:"continue"};if(!process.stdin.isTTY||!t){for(let r of n)console.log(` ${r.path} ${p(`(${r.label})`)}`);return console.log(p("Run: /publish <article-path>")),{kind:"continue"}}try{o=await t.withPausedInput(()=>li({message:"Publish which article?",choices:[...n.length>1?[{name:`All ${n.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...n.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(p("Cancelled.")),{kind:"continue"}}if(o==="--all")return{kind:"forward-to-agent",prompt:lt(n.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:Ot(o),display:`/publish ${o}`}}async function Ut(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","3. Map changed files to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
12
+ `),display:"/audit"}}H();import{checkbox as ai}from"@inquirer/prompts";import{inventoryRepo as ci,readProjectConfig as Bn,writeProjectConfig as ui}from"document360-engine";function $o(e,t){let o=Bn(e);o&&(o.authoritativeSourceFiles=t,ui(o,e))}function Wt(e){let t=[`${e.fileCount}${e.fileCount>=5e3?"+":""} files`];return e.stacks.length&&t.push(e.stacks.join("+")),t.push(e.reason),t.join(" \xB7 ")}async function Hn(e,t){let o=t?.cwd??process.cwd();if(!Bn(o))return console.log(b("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let n=ci(o);if(n.length===0)return console.log(p('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(P("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of n)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${p(Wt(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>ai({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:n.map(s=>({name:`${s.path} (${Wt(s)})`,value:s.path,checked:s.recommended})),pageSize:20}))}catch{return console.log(p("Cancelled.")),{kind:"continue"}}if(r.length===0)return console.log(p("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};$o(o,r),console.log(E(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let s of r)console.log(` ${s}`);return console.log(""),{kind:"continue"}}H();import{confirm as gi}from"@inquirer/prompts";import{applyPull as Vn,computeSyncStatus as Yn,planPull as hi,D360AuthError as ki}from"document360-engine";H();var qn=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:b,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:L,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:P,mark:"M"},{status:"deleted-local",header:"Deleted locally \u2014 still on Document360 (pull to restore, or remove from the map):",paint:b,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:b,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:Y,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:p,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:p,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:Y,mark:"\xB7"}];function di(e){return e.path?e.path:`${e.title??"(untitled)"} ${Y(`[${e.articleId}]`)}`}function Ft(e){let t=[];for(let c of qn){let d=e.entries.filter(h=>h.status===c.status);if(d.length!==0){t.push(""),t.push(he(c.header));for(let h of d)t.push(` ${c.paint(c.mark)} ${c.paint(di(h))}${h.detail?Y(` (${h.detail})`):""}`)}}let o=e.counts["in-sync"]??0,n=e.entries.length;t.push("");let r={"in-sync":"in sync","local-ahead":"local ahead","remote-ahead":"remote ahead",conflict:"conflicts","untracked-local":"untracked local","untracked-remote":"untracked remote","deleted-local":"deleted locally","deleted-remote":"deleted on D360",orphaned:"orphaned","unknown-base":"no base yet"},s=n===o?E("\u2713 everything in sync"):[o>0?E(`\u2713 ${o} in sync`):null,...qn.map(c=>{let d=e.counts[c.status]??0;return d>0?`${d} ${r[c.status]}`:null})].filter(Boolean).join(Y(" \xB7 "));return t.push(s+Y(` \xB7 ${n} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as pi}from"diff";import zn from"picocolors";var Gn=80,mi="\x1B[48;2;74;28;28m",fi="\x1B[48;2;24;66;24m",Xn="\x1B[49m",bo=e=>String(e).padStart(5);function Me(e,t,o){let n=x=>{let j=x.replace(/\r\n/g,`
13
13
  `);return j.endsWith(`
14
14
  `)||j===""?j:j+`
15
- `},r=n(e),s=n(t);if(r===s)return null;let c=Math.max(20,o-10),p=ui("a","b",r,s,"","",{context:3}),h=0,f=0,x=[];p.hunks.forEach(($,j)=>{j>0&&x.push(Bn.gray(" \u2026"));let T=$.oldStart,M=$.newStart;for(let q of $.lines){let z=q[0],ee=q.slice(1).slice(0,c);z==="-"?(f++,x.push(`${di}${$o(T++)} - ${ee}${qn}`)):z==="+"?(h++,x.push(`${pi}${$o(M++)} + ${ee}${qn}`)):(x.push(Bn.gray($o(M))+" "+ee),T++,M++)}});let R=x.slice(0,Hn);return{added:h,removed:f,lines:R,hidden:Math.max(0,x.length-Hn)}}async function Xn(e,t){let o=(e[0]??"status").toLowerCase();try{if(o==="status")return await hi(t.cwd),{kind:"continue"};if(o==="pull")return await ki(t,e.slice(1)),{kind:"continue"};console.log(v(`Unknown subcommand: /sync ${o}`)),console.log(u("Usage: /sync drift report (local vs Document360)")),console.log(u(" /sync pull <path> pull portal edits into the local file")),console.log(u(" /sync pull --all pull every remote-ahead article"))}catch(n){n instanceof gi?console.log(v(n.message)):console.log(v(`Sync failed: ${n.message}`))}return{kind:"continue"}}async function hi(e){console.log(u("Checking Document360 for drift\u2026"));let t=await Gn({cwd:e});for(let o of Ut(t))console.log(o);console.log("")}async function ki(e,t){let o=t[0];if(!o){console.log(v("Usage: /sync pull <article-path> | --all"));return}let n;if(o==="--all"){if(console.log(u("Checking Document360 for drift\u2026")),n=(await Gn({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),n.length===0){console.log(E("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(u(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${se(String(n.length))} article(s) are remote-ahead.`)}else n=[o.replace(/\\/g,"/")];for(let r of n){let s=await fi({cwd:e.cwd,relPath:r});console.log(""),console.log(`${y("\u25CF")} ${se(s.title)} ${Y(`(${s.path})`)}`);for(let f of s.notes)console.log(L(` \u26A0 ${f}`));s.overwritesLocalChanges&&console.log(L(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=_e(s.oldContent,s.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!c){console.log(u(" Local file already matches the remote content \u2014 advancing the sync base only.")),zn({cwd:e.cwd},s);continue}let p=f=>f===1?"":"s";console.log(Y(` \u23BF Added ${c.added} line${p(c.added)}, removed ${c.removed} line${p(c.removed)}`));for(let f of c.lines)console.log(f);if(c.hidden>0&&console.log(u(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>mi({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(u(" Skipped."));continue}zn({cwd:e.cwd},s),console.log(E(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}B();import{statSync as Ri}from"node:fs";import{resolve as ji}from"node:path";import{estimateBulkCost as Ai,planPartitions as Ei,readProjectConfig as Di,resolveModelForOperation as Ii,runPartitioned as Mi,trackedArticlePaths as _i}from"document360-engine";import W from"picocolors";import Zn from"wrap-ansi";import To from"string-width";B();import Ce from"picocolors";import bo from"wrap-ansi";import vo from"string-width";var Vn=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Yn=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Kn=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function wi(e){let t=e.replace(/\r/g,"").split(`
16
- `),o=[],n=0;for(;n<t.length;){let r=t[n];if(/^\s*```/.test(r)){let p=[];for(n++;n<t.length&&!/^\s*```/.test(t[n]);)p.push(t[n++]);n++,o.push({kind:"code",lines:p});continue}if(Vn(r)){o.push({kind:"hr"}),n++;continue}if(r.includes("|")&&n+1<t.length&&Yn(t[n+1])){let p=Kn(r);n+=2;let h=[];for(;n<t.length&&t[n].includes("|")&&t[n].trim()!=="";)h.push(Kn(t[n++]));o.push({kind:"table",header:p,rows:h});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){o.push({kind:"heading",level:s[1].length,text:s[2]}),n++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let p=[];for(;n<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[n]);)p.push(t[n++].replace(/^\s*([-*]|\d+\.)\s+/,""));o.push({kind:"list",items:p});continue}if(r.trim()===""){n++;continue}let c=[];for(;n<t.length&&t[n].trim()!==""&&!/^\s*```/.test(t[n])&&!/^(#{1,6})\s/.test(t[n])&&!/^\s*([-*]|\d+\.)\s+/.test(t[n])&&!Vn(t[n])&&!(t[n].includes("|")&&n+1<t.length&&Yn(t[n+1]));)c.push(t[n++]);o.push({kind:"para",text:c.join(" ")})}return o}function Co(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Ce.bold(t.slice(2,-2)):t.startsWith("`")?y(t.slice(1,-1)):Ce.italic(t.slice(1,-1)))}var yi=(e,t)=>e+" ".repeat(Math.max(0,t-vo(e)));function xi(e,t,o){let n=e.length,r=e.map((T,M)=>Math.max(vo(T),...t.map(q=>vo(q[M]??"")))),s=Math.max(24,o),c=3*n+1,p=[...r],h=()=>p.reduce((T,M)=>T+M,0)+c,f=0;for(;h()>s&&f++<1e4;){let T=-1,M=6;for(let q=0;q<n;q++)p[q]>M&&(M=p[q],T=q);if(T===-1)break;p[T]-=1}let x=(T,M,q)=>Ce.gray(T+p.map(z=>"\u2500".repeat(z+2)).join(M)+q),R=Ce.gray("\u2502"),$=(T,M)=>{let q=p.map((le,we)=>{let ae=T[we]??"",ce=M?Ce.bold(ae):Co(ae);return bo(ce,le,{hard:!0}).split(`
17
- `)}),z=Math.max(...q.map(le=>le.length)),ee=[];for(let le=0;le<z;le++)ee.push(p.map((we,ae)=>`${R} ${yi(q[ae][le]??"",we)} `).join("")+R);return ee.join(`
18
- `)},j=[x("\u250C","\u252C","\u2510"),$(e,!0)];return j.push(t.length===0?x("\u2514","\u2534","\u2518"):x("\u251C","\u253C","\u2524")),t.forEach((T,M)=>{j.push($(T,!1)),j.push(M===t.length-1?x("\u2514","\u2534","\u2518"):x("\u251C","\u253C","\u2524"))}),j.join(`
19
- `)}function $i(e,t){switch(e.kind){case"heading":return Ce.bold(e.text);case"hr":return Ce.gray("\u2500".repeat(t));case"para":return bo(Co(e.text),t);case"list":return e.items.map(o=>{let[n="",...r]=bo(Co(o),Math.max(10,t-4)).split(`
15
+ `},r=n(e),s=n(t);if(r===s)return null;let c=Math.max(20,o-10),d=pi("a","b",r,s,"","",{context:3}),h=0,f=0,w=[];d.hunks.forEach((x,j)=>{j>0&&w.push(zn.gray(" \u2026"));let T=x.oldStart,_=x.newStart;for(let q of x.lines){let z=q[0],ee=q.slice(1).slice(0,c);z==="-"?(f++,w.push(`${mi}${bo(T++)} - ${ee}${Xn}`)):z==="+"?(h++,w.push(`${fi}${bo(_++)} + ${ee}${Xn}`)):(w.push(zn.gray(bo(_))+" "+ee),T++,_++)}});let R=w.slice(0,Gn);return{added:h,removed:f,lines:R,hidden:Math.max(0,w.length-Gn)}}async function Kn(e,t){let o=(e[0]??"status").toLowerCase();try{if(o==="status")return await yi(t.cwd),{kind:"continue"};if(o==="pull")return await wi(t,e.slice(1)),{kind:"continue"};console.log(b(`Unknown subcommand: /sync ${o}`)),console.log(p("Usage: /sync drift report (local vs Document360)")),console.log(p(" /sync pull <path> pull portal edits into the local file")),console.log(p(" /sync pull --all pull every remote-ahead article"))}catch(n){n instanceof ki?console.log(b(n.message)):console.log(b(`Sync failed: ${n.message}`))}return{kind:"continue"}}async function yi(e){console.log(p("Checking Document360 for drift\u2026"));let t=await Yn({cwd:e});for(let o of Ft(t))console.log(o);console.log("")}async function wi(e,t){let o=t[0];if(!o){console.log(b("Usage: /sync pull <article-path> | --all"));return}let n;if(o==="--all"){if(console.log(p("Checking Document360 for drift\u2026")),n=(await Yn({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),n.length===0){console.log(E("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(p(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${he(String(n.length))} article(s) are remote-ahead.`)}else n=[o.replace(/\\/g,"/")];for(let r of n){let s=await hi({cwd:e.cwd,relPath:r});console.log(""),console.log(`${P("\u25CF")} ${he(s.title)} ${Y(`(${s.path})`)}`);for(let f of s.notes)console.log(L(` \u26A0 ${f}`));s.overwritesLocalChanges&&console.log(L(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=Me(s.oldContent,s.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!c){console.log(p(" Local file already matches the remote content \u2014 advancing the sync base only.")),Vn({cwd:e.cwd},s);continue}let d=f=>f===1?"":"s";console.log(Y(` \u23BF Added ${c.added} line${d(c.added)}, removed ${c.removed} line${d(c.removed)}`));for(let f of c.lines)console.log(f);if(c.hidden>0&&console.log(p(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>gi({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(p(" Skipped."));continue}Vn({cwd:e.cwd},s),console.log(E(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}H();import{statSync as Ai}from"node:fs";import{resolve as Ei}from"node:path";import{estimateBulkCost as Di,planPartitions as Ii,readProjectConfig as _i,resolveModelForOperation as Mi,runPartitioned as Ni,trackedArticlePaths as Li}from"document360-engine";import W from"picocolors";import or from"wrap-ansi";import Ro from"string-width";H();import Ce from"picocolors";import vo from"wrap-ansi";import Co from"string-width";var Jn=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Qn=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Zn=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function xi(e){let t=e.replace(/\r/g,"").split(`
16
+ `),o=[],n=0;for(;n<t.length;){let r=t[n];if(/^\s*```/.test(r)){let d=[];for(n++;n<t.length&&!/^\s*```/.test(t[n]);)d.push(t[n++]);n++,o.push({kind:"code",lines:d});continue}if(Jn(r)){o.push({kind:"hr"}),n++;continue}if(r.includes("|")&&n+1<t.length&&Qn(t[n+1])){let d=Zn(r);n+=2;let h=[];for(;n<t.length&&t[n].includes("|")&&t[n].trim()!=="";)h.push(Zn(t[n++]));o.push({kind:"table",header:d,rows:h});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){o.push({kind:"heading",level:s[1].length,text:s[2]}),n++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let d=[];for(;n<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[n]);)d.push(t[n++].replace(/^\s*([-*]|\d+\.)\s+/,""));o.push({kind:"list",items:d});continue}if(r.trim()===""){n++;continue}let c=[];for(;n<t.length&&t[n].trim()!==""&&!/^\s*```/.test(t[n])&&!/^(#{1,6})\s/.test(t[n])&&!/^\s*([-*]|\d+\.)\s+/.test(t[n])&&!Jn(t[n])&&!(t[n].includes("|")&&n+1<t.length&&Qn(t[n+1]));)c.push(t[n++]);o.push({kind:"para",text:c.join(" ")})}return o}function Po(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Ce.bold(t.slice(2,-2)):t.startsWith("`")?P(t.slice(1,-1)):Ce.italic(t.slice(1,-1)))}var $i=(e,t)=>e+" ".repeat(Math.max(0,t-Co(e)));function bi(e,t,o){let n=e.length,r=e.map((T,_)=>Math.max(Co(T),...t.map(q=>Co(q[_]??"")))),s=Math.max(24,o),c=3*n+1,d=[...r],h=()=>d.reduce((T,_)=>T+_,0)+c,f=0;for(;h()>s&&f++<1e4;){let T=-1,_=6;for(let q=0;q<n;q++)d[q]>_&&(_=d[q],T=q);if(T===-1)break;d[T]-=1}let w=(T,_,q)=>Ce.gray(T+d.map(z=>"\u2500".repeat(z+2)).join(_)+q),R=Ce.gray("\u2502"),x=(T,_)=>{let q=d.map((ie,ye)=>{let le=T[ye]??"",ae=_?Ce.bold(le):Po(le);return vo(ae,ie,{hard:!0}).split(`
17
+ `)}),z=Math.max(...q.map(ie=>ie.length)),ee=[];for(let ie=0;ie<z;ie++)ee.push(d.map((ye,le)=>`${R} ${$i(q[le][ie]??"",ye)} `).join("")+R);return ee.join(`
18
+ `)},j=[w("\u250C","\u252C","\u2510"),x(e,!0)];return j.push(t.length===0?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524")),t.forEach((T,_)=>{j.push(x(T,!1)),j.push(_===t.length-1?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524"))}),j.join(`
19
+ `)}function vi(e,t){switch(e.kind){case"heading":return Ce.bold(e.text);case"hr":return Ce.gray("\u2500".repeat(t));case"para":return vo(Po(e.text),t);case"list":return e.items.map(o=>{let[n="",...r]=vo(Po(o),Math.max(10,t-4)).split(`
20
20
  `);return" \u2022 "+n+r.map(s=>`
21
21
  `+s).join("")}).join(`
22
22
  `);case"code":return e.lines.map(o=>Ce.gray(" "+o)).join(`
23
- `);case"table":return xi(e.header,e.rows,t)}}function Po(e,t){let o=Math.max(20,t);return wi(e).map(n=>$i(n,o)).join(`
23
+ `);case"table":return bi(e.header,e.rows,t)}}function So(e,t){let o=Math.max(20,t);return xi(e).map(n=>vi(n,o)).join(`
24
24
 
25
- `)}B();import bi from"picocolors";var vi=[127,86,217],Ci=[22,38,43],Jn={T:vi,E:Ci},lt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],So=([e,t,o])=>`\x1B[38;2;${e};${t};${o}m`,Pi=([e,t,o])=>`\x1B[48;2;${e};${t};${o}m`;function Qn(){if(!bi.isColorSupported)return[];let e=lt[0].length,t=[];for(let o=0;o<lt.length;o+=2){let n="";for(let r=0;r<e;r++){let s=Jn[lt[o][r]],c=o+1<lt.length?Jn[lt[o+1][r]]:void 0;s&&c?n+=`${So(s)}${Pi(c)}\u2580\x1B[49m\x1B[39m`:s?n+=`${So(s)}\u2580\x1B[39m`:c?n+=`${So(c)}\u2584\x1B[39m`:n+=" "}t.push(n)}return t}function at(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function Ne(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Wt(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var Xe=(e,t)=>` ${e.padEnd(13)}${t}`;function Si(e,t){let o=[ot("\u270E document360-writer")+W.gray(` v${e.version}`),W.gray(" Reads your code, writes your docs."),"",Xe("Claude:",`${e.claude}${W.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),Xe("Document360:",e.configured?e.loggedOut?W.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?W.gray(` (${e.sessionHint})`):""}`:W.yellow("not set up \u2014 run /init")),Xe("Profile:",e.configured?`${e.profile}${W.gray(` (${e.apiUrl})`)}${e.prod?W.bold(W.yellow(" \u26A0 PRODUCTION")):""}`:W.gray("\u2014 (run /init)")),Xe("Project:",e.project),Xe("Mode:",W.gray(e.mode)),Xe("cwd:",W.gray(e.cwd))],n=Qn();if(n.length===0)return o.join(`
26
- `);let r=2,s=3,c=To(n[0]);if(!(t>=r+c+s+Math.max(...o.map(x=>To(x)))))return[...n.map(x=>" "+x),...o].join(`
27
- `);let h=Math.max(0,Math.floor((o.length-n.length)/2)),f=[];for(let x=0;x<Math.max(n.length+h,o.length);x++){let R=n[x-h]??" ".repeat(c);f.push((" ".repeat(r)+R+" ".repeat(s)+(o[x]??"")).trimEnd())}return f.join(`
28
- `)}var Ti={error:W.red,warn:W.yellow,ok:W.green,info:W.gray};function Ro(e,t){let o=Math.max(20,t);switch(e.kind){case"banner":return Si(e.info,o);case"user":{let n="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,o-4),c=50,p=e.text.split(`
29
- `).flatMap(f=>Zn(f,s,{hard:!0}).split(`
30
- `)),h=Math.max(0,p.length-c);return h>0&&(p=[...p.slice(0,c),W.dim(`\u2026 +${h} more lines`)]),`
31
- `+p.map((f,x)=>n+(x===0?y(" \u276F "):" ")+f+" ".repeat(Math.max(0,s-To(f))+1)+r).join(`
25
+ `)}H();import Ci from"picocolors";var Pi=[127,86,217],Si=[22,38,43],er={T:Pi,E:Si},at=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],To=([e,t,o])=>`\x1B[38;2;${e};${t};${o}m`,Ti=([e,t,o])=>`\x1B[48;2;${e};${t};${o}m`;function tr(){if(!Ci.isColorSupported)return[];let e=at[0].length,t=[];for(let o=0;o<at.length;o+=2){let n="";for(let r=0;r<e;r++){let s=er[at[o][r]],c=o+1<at.length?er[at[o+1][r]]:void 0;s&&c?n+=`${To(s)}${Ti(c)}\u2580\x1B[49m\x1B[39m`:s?n+=`${To(s)}\u2580\x1B[39m`:c?n+=`${To(c)}\u2584\x1B[39m`:n+=" "}t.push(n)}return t}function ct(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function Ne(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Bt(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var Xe=(e,t)=>` ${e.padEnd(13)}${t}`;function Ri(e,t){let o=[ot("\u270E document360-writer")+W.gray(` v${e.version}`),W.gray(" Reads your code, writes your docs."),"",Xe("Claude:",`${e.claude}${W.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),Xe("Document360:",e.configured?e.loggedOut?W.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?W.gray(` (${e.sessionHint})`):""}`:W.yellow("not set up \u2014 run /init")),Xe("Profile:",e.configured?`${e.profile}${W.gray(` (${e.apiUrl})`)}${e.prod?W.bold(W.yellow(" \u26A0 PRODUCTION")):""}`:W.gray("\u2014 (run /init)")),Xe("Project:",e.project),Xe("Mode:",W.gray(e.mode)),Xe("cwd:",W.gray(e.cwd))],n=tr();if(n.length===0)return o.join(`
26
+ `);let r=2,s=3,c=Ro(n[0]);if(!(t>=r+c+s+Math.max(...o.map(w=>Ro(w)))))return[...n.map(w=>" "+w),...o].join(`
27
+ `);let h=Math.max(0,Math.floor((o.length-n.length)/2)),f=[];for(let w=0;w<Math.max(n.length+h,o.length);w++){let R=n[w-h]??" ".repeat(c);f.push((" ".repeat(r)+R+" ".repeat(s)+(o[w]??"")).trimEnd())}return f.join(`
28
+ `)}var ji={error:W.red,warn:W.yellow,ok:W.green,info:W.gray};function jo(e,t){let o=Math.max(20,t);switch(e.kind){case"banner":return Ri(e.info,o);case"user":{let n="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,o-4),c=50,d=e.text.split(`
29
+ `).flatMap(f=>or(f,s,{hard:!0}).split(`
30
+ `)),h=Math.max(0,d.length-c);return h>0&&(d=[...d.slice(0,c),W.dim(`\u2026 +${h} more lines`)]),`
31
+ `+d.map((f,w)=>n+(w===0?P(" \u276F "):" ")+f+" ".repeat(Math.max(0,s-Ro(f))+1)+r).join(`
32
32
  `)}case"assistant":return`
33
- `+Po(e.text,o);case"tool":{let n=e.arg!==null?W.gray(`${e.sep}(${e.arg})`):"";return`
34
- `+Zn(W.green("\u25CF ")+W.bold(e.title)+n,o)}case"tool-result":{let n=e.isError?W.red:W.gray,r=e.lines.map((s,c)=>n((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(W.dim(` \u2026 +${e.hidden} lines`)),r.join(`
33
+ `+So(e.text,o);case"tool":{let n=e.arg!==null?W.gray(`${e.sep}(${e.arg})`):"";return`
34
+ `+or(W.green("\u25CF ")+W.bold(e.title)+n,o)}case"tool-result":{let n=e.isError?W.red:W.gray,r=e.lines.map((s,c)=>n((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(W.dim(` \u2026 +${e.hidden} lines`)),r.join(`
35
35
  `)}case"diff":{let n=c=>c===1?"":"s",s=[W.gray(` \u23BF Added ${e.added} line${n(e.added)}, removed ${e.removed} line${n(e.removed)}`),...e.lines];return e.hidden>0&&s.push(W.dim(` \u2026 +${e.hidden} more diff lines`)),s.join(`
36
- `)}case"link":return e.lines.map(n=>y(nt(` \u2B95 ${n}`))).join(`
36
+ `)}case"link":return e.lines.map(n=>P(nt(` \u2B95 ${n}`))).join(`
37
37
  `);case"preview":return`
38
38
  `+ot(`\u25A3 Preview \u2014 ${e.name}`)+`
39
39
 
40
- `+Po(e.text,o);case"note":return`
41
- `+Ti[e.tone](nt(e.text));case"done":return`
42
- `+(e.ok?W.magenta("\u2736 "):W.red("\u2736 "))+W.gray(`Cooked for ${at(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${Ne(e.costUsd)}`:""))}}function er(e,t){return e.map(o=>Ro(o,t)).join(`
43
- `)}var jo=3;function Ao(e){let t,o=!1;for(let r=0;r<e.length;r++){let s=e[r];s==="--run"||s==="--yes"?o=!0:s==="--scope"?t=e[++r]:s?.startsWith("--scope=")&&(t=s.slice(8))}return{scope:t?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0,run:o}}function Eo(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function Do(e,t){return t.map(o=>{let n=0;try{n=Ri(ji(e,o)).size}catch{n=0}return{path:o,bytes:n}})}function Io(e){return`Convert each of these articles to canonical Document360 Flavored Markdown (DFM) and re-publish each as a DRAFT. This is a mechanical syntax conversion, not a rewrite \u2014 be efficient:
40
+ `+So(e.text,o);case"note":return`
41
+ `+ji[e.tone](nt(e.text));case"done":return`
42
+ `+(e.ok?W.magenta("\u2736 "):W.red("\u2736 "))+W.gray(`Cooked for ${ct(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${Ne(e.costUsd)}`:""))}}function nr(e,t){return e.map(o=>jo(o,t)).join(`
43
+ `)}var Ao=3;function Eo(e){let t,o=!1;for(let r=0;r<e.length;r++){let s=e[r];s==="--run"||s==="--yes"?o=!0:s==="--scope"?t=e[++r]:s?.startsWith("--scope=")&&(t=s.slice(8))}return{scope:t?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0,run:o}}function Do(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function Io(e,t){return t.map(o=>{let n=0;try{n=Ai(Ei(e,o)).size}catch{n=0}return{path:o,bytes:n}})}function _o(e){return`Convert each of these articles to canonical Document360 Flavored Markdown (DFM) and re-publish each as a DRAFT. This is a mechanical syntax conversion, not a rewrite \u2014 be efficient:
44
44
  - Read ONLY the article files listed below. Do NOT read source code, other articles, or fetch the live portal \u2014 you already have everything you need.
45
45
  - Convert in place: wrap callouts, FAQs, tabs, accordions, and media embeds per the d360-markdown skill. Preserve all wording, headings, and structure; change only GFM\u2192DFM syntax.
46
46
  - Publish each as a draft with a single update call. Do NOT re-read, re-verify, or polish after converting, and skip articles already in DFM.
47
47
  - Work only on these files:
48
48
  `+e.paths.map(t=>`- ${t}`).join(`
49
- `)}function Mo(e,t,o){let n=e.reduce((p,h)=>p+h.paths.length,0),[r,s]=t.usd;return[`Convert ${n} article${n===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${o} agents at once):`,...e.map(p=>` \u2022 ${p.label} \u2014 ${p.paths.length} article${p.paths.length===1?"":"s"}`),"",`Estimated cost: ${Ne(r)}\u2013${Ne(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function _o(e,t,o){let n=T=>t[T.index]?.paths.length??0,r=e.filter(T=>T.ok),s=e.filter(T=>!T.ok),c=e.reduce((T,M)=>T+n(M),0),p=r.reduce((T,M)=>T+n(M),0),h=e.reduce((T,M)=>T+M.costUsd,0),f=e.reduce((T,M)=>T+M.outputTokens,0),x=o==="api"?`${Ne(h)} total`:Wt(f),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,$=T=>`${T} article${T===1?"":"s"}`,j=[];if(s.length===0)j.push(`Converted ${$(c)} (${R}) successfully \xB7 ${x}.`);else{j.push(`Converted ${p}/${$(c)} (${R}) \xB7 ${x}.`),j.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let T of s)j.push(` \u2717 ${T.label}${T.error?` \u2014 ${T.error}`:""}`)}return j}async function tr(e,t){if(!Di(t.cwd))return console.log(v("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let{scope:o,run:n}=Ao(e),r=_i(t.cwd,t.profileName);if(r.length===0)return console.log(v("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=Eo(r,o);if(s.length===0)return console.log(v(`No tracked articles under "${o}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=Ei(s),p=`/convert${o?` --scope ${o}`:""} --run`,{model:h,forced:f}=Ii(t.cwd,"light");if(!n){let x=Ai({files:Do(t.cwd,s),op:"convert",model:h});o&&console.log(u(`Scope: ${o} (${s.length} of ${r.length} tracked articles).`));for(let R of Mo(c,x,jo))console.log(R);return console.log(u(`Model: ${h}${f?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(u(`Run ${p} to start.`)),console.log(""),{kind:"continue"}}console.log(u(`Converting ${s.length} articles across ${c.length} partitions (\u2264${jo} agents at once) on ${h}\u2026`)),console.log(u(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let x of Mi({cwd:t.cwd,partitions:c,promptFor:Io,concurrency:jo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:h}))if(x.type==="partition_status")x.status==="running"?console.log(u(` \u25B8 ${x.label} \u2014 converting\u2026`)):x.status==="done"?console.log(E(` \u2713 ${x.label}`)):console.log(v(` \u2717 ${x.label}`));else if(x.type==="run_done"){console.log("");let R=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let $ of _o(x.results,c,R))console.log(x.ok?E($):L($))}}catch(x){console.log(v(`Convert run failed: ${x.message}`))}return console.log(""),{kind:"continue"}}B();import{search as Ni}from"@inquirer/prompts";import{findByName as Li,getSession as Oi,listSessions as Ui,relativeTime as nr}from"document360-engine";async function rr(e,t){let o=Ui(t.cwd).filter(n=>n.uuid!==t.currentUuid());if(o.length===0)return console.log(u("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let n=e.join(" "),r=Li(t.cwd,n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(v(`No session matches "${n}".`)),or(o),{kind:"continue"})}if(!process.stdin.isTTY)return or(o),console.log(u("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>Ni({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return o.filter(p=>!c||p.name.toLowerCase().includes(c)||p.firstPrompt.toLowerCase().includes(c)).map(p=>({name:`${p.name} ${nr(p.updatedAt)}`,value:p.uuid,description:p.firstPrompt.slice(0,100)}))}})),r=Oi(n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function or(e){console.log("");for(let t of e.slice(0,15))console.log(` ${y(t.name)} ${u(nr(t.updatedAt))}`),console.log(` ${u(t.firstPrompt.slice(0,80))}`);console.log("")}B();import{renameSession as Wi}from"document360-engine";async function sr(e,t){let o=e.join(" ").trim();if(!o)return console.log(v("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(Wi(n,o)?console.log(E(`\u2713 Session renamed to "${o}"`)):console.log(v("Could not find the current session record.")),{kind:"continue"}):(console.log(v("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as ir,readProjectConfig as Fi,writeProjectConfig as Bi}from"document360-engine";B();function No(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=Fi(e);if(!n)return"No .d360-writer.json \u2014 run /init first.";if(n.profiles?.[t])return`Profile "${t}" already exists.`;let r=o??t;return ir().includes(r)?(n.profiles={...n.profiles,[t]:{connection:{environment:r},production:!1}},Bi(n,e),null):`Unknown environment "${r}". Known: ${ir().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function lr(e,t){let o=e[0];if(!o)return Et(t.cwd),{kind:"continue"};if(o==="add"){let n=No(t.cwd,e[1],e[2]);return n?(console.log(v(n)),{kind:"continue"}):(console.log(E(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${y(`/profile ${e[1]}`)} then ${y("/login")}`),{kind:"continue"})}return Dt(t.cwd,o),It(t.cwd,o),console.log(u(" Restarting agent for the new profile\u2026")),{kind:"clear"}}B();import{select as Hi}from"@inquirer/prompts";import{readProjectConfig as qi,readUserConfig as ar,resolveModelSetting as Lo,writeUserConfig as cr}from"document360-engine";var pe=[{value:null,label:"Auto",desc:"Engine right-sizes per task \u2014 Sonnet for routine work, Opus for analysis (recommended)"},{value:"claude-fable-5",label:"Fable",desc:"Fable 5 \xB7 most capable, for the hardest and longest-running tasks"},{value:"opus",label:"Opus",desc:"Opus 4.8 \xB7 best for everyday complex tasks"},{value:"sonnet",label:"Sonnet",desc:"Sonnet 4.6 \xB7 efficient for routine tasks"},{value:"haiku",label:"Haiku",desc:"Haiku 4.5 \xB7 fastest for quick answers"}];function Ft(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),o=pe.findIndex(n=>n.value!==null&&(n.value===t||n.label.toLowerCase()===t||t.includes(n.label.toLowerCase())));return o>=0?o:0}function zi(e){switch(e.source){case"project":return".d360-writer.json defaultModel (team setting)";case"user":return"~/.document360-writer/config.json (your /model setting)";case"env":return"ANTHROPIC_MODEL environment variable";case"claude-settings":return"Claude Code's own settings (~/.claude/settings.json)";case"claude-default":return"Claude Code default (no override configured)"}}function ct(e,t){let o=()=>{let s=Lo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=ar();return s.defaultModel?(delete s.defaultModel,cr(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Lo(e).model??"Claude Code default"} (applies from your next message)`],changed:!0,effective:o()}):{lines:["No personal model override set \u2014 nothing to clear."],changed:!1,effective:void 0}}cr({...ar(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=qi(e)?.defaultModel;return r&&n.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:n,changed:!0,effective:o()}}async function ur(e,t){let o=e[0]?.trim();if(!o){let c=Lo(t.cwd);if(!process.stdin.isTTY)return console.log(`${se("Model:")} ${y(c.model??"Claude Code default")}`),console.log(u(` source: ${zi(c)}`)),console.log(u(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let p=Ft(c),h;try{h=await t.withPausedInput(()=>Hi({message:`Select model (current: ${c.model??"Claude Code default"})`,default:pe[p].value,choices:pe.map(($,j)=>({name:`${$.label}${j===p?" \u2714":""}`,value:$.value,description:$.desc}))}))}catch{return console.log(u("Cancelled.")),{kind:"continue"}}let{lines:f,changed:x,effective:R}=ct(t.cwd,h??"default");for(let $ of f)console.log($.startsWith("\u26A0")?L($):$.startsWith("\u2713")?E($):u($));return x&&await t.setModel(R),{kind:"continue"}}let{lines:n,changed:r,effective:s}=ct(t.cwd,o);for(let c of n)console.log(c.startsWith("\u26A0")?L(c):c.startsWith("\u2713")?E(c):u(c));return r&&await t.setModel(s),{kind:"continue"}}Bt();async function gr(e,t){return await t.withPausedInput(()=>Ge(t.cwd)),{kind:"clear"}}B();import{resolveActiveProfile as ol}from"document360-engine";async function hr(e,t){let o=!1;try{o=ol(t.cwd).production}catch{}return o?(console.log(L("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(u("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}B();var kr=async(e,t)=>{try{await t.withPausedInput(()=>At({}))}catch(o){console.log(v(`Login failed: ${o.message}`))}return{kind:"continue"}};import{existsSync as wr}from"node:fs";import{isAbsolute as nl,join as rl,resolve as sl}from"node:path";import{readProjectConfig as il,screenshotPlaceholderIds as ll}from"document360-engine";var yr=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Uo(e){if(e[0]==="--list")return{mode:"list",scope:e[1]?yr(e[1]):void 0};let t=e[0];return!t||t==="--all"?{mode:"all"}:/[\\/]/.test(t)||t.endsWith(".md")?{mode:"scope",scope:yr(t)}:{mode:"single",id:t}}function Wo(e,t){let o=il(e),n=o?.captureDir??"user-docs/_capture",r=o?.outputDir??"user-docs/_screenshots",s=(c,p)=>nl(c)?rl(c,p):sl(e,c,p);return ll(e,{scope:t}).map(({id:c,file:p})=>{let h=wr(s(r,`${c}.png`))?"captured":wr(s(n,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:p,state:h}})}var al={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},cl={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Fo(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let o=[...new Set(e.map(s=>s.file))].sort(),n=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${o.length} article${o.length===1?"":"s"} \xB7 ${n} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of o){r.push(s);for(let c of e.filter(p=>p.file===s))r.push(` ${al[c.state]} ${c.id.padEnd(34)} ${cl[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function xr(e){return["Run the emit-screenshot-spec skill to author the document360-capture spec for EACH of these","SCREENSHOT placeholder ids:",e.paths.map(t=>`- ${t}`).join(`
49
+ `)}function Mo(e,t,o){let n=e.reduce((d,h)=>d+h.paths.length,0),[r,s]=t.usd;return[`Convert ${n} article${n===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${o} agents at once):`,...e.map(d=>` \u2022 ${d.label} \u2014 ${d.paths.length} article${d.paths.length===1?"":"s"}`),"",`Estimated cost: ${Ne(r)}\u2013${Ne(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function No(e,t,o){let n=T=>t[T.index]?.paths.length??0,r=e.filter(T=>T.ok),s=e.filter(T=>!T.ok),c=e.reduce((T,_)=>T+n(_),0),d=r.reduce((T,_)=>T+n(_),0),h=e.reduce((T,_)=>T+_.costUsd,0),f=e.reduce((T,_)=>T+_.outputTokens,0),w=o==="api"?`${Ne(h)} total`:Bt(f),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,x=T=>`${T} article${T===1?"":"s"}`,j=[];if(s.length===0)j.push(`Converted ${x(c)} (${R}) successfully \xB7 ${w}.`);else{j.push(`Converted ${d}/${x(c)} (${R}) \xB7 ${w}.`),j.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let T of s)j.push(` \u2717 ${T.label}${T.error?` \u2014 ${T.error}`:""}`)}return j}async function rr(e,t){if(!_i(t.cwd))return console.log(b("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let{scope:o,run:n}=Eo(e),r=Li(t.cwd,t.profileName);if(r.length===0)return console.log(b("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=Do(r,o);if(s.length===0)return console.log(b(`No tracked articles under "${o}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=Ii(s),d=`/convert${o?` --scope ${o}`:""} --run`,{model:h,forced:f}=Mi(t.cwd,"light");if(!n){let w=Di({files:Io(t.cwd,s),op:"convert",model:h});o&&console.log(p(`Scope: ${o} (${s.length} of ${r.length} tracked articles).`));for(let R of Mo(c,w,Ao))console.log(R);return console.log(p(`Model: ${h}${f?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(p(`Run ${d} to start.`)),console.log(""),{kind:"continue"}}console.log(p(`Converting ${s.length} articles across ${c.length} partitions (\u2264${Ao} agents at once) on ${h}\u2026`)),console.log(p(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let w of Ni({cwd:t.cwd,partitions:c,promptFor:_o,concurrency:Ao,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:h}))if(w.type==="partition_status")w.status==="running"?console.log(p(` \u25B8 ${w.label} \u2014 converting\u2026`)):w.status==="done"?console.log(E(` \u2713 ${w.label}`)):console.log(b(` \u2717 ${w.label}`));else if(w.type==="run_done"){console.log("");let R=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let x of No(w.results,c,R))console.log(w.ok?E(x):L(x))}}catch(w){console.log(b(`Convert run failed: ${w.message}`))}return console.log(""),{kind:"continue"}}H();import{search as Oi}from"@inquirer/prompts";import{findByName as Ui,getSession as Wi,listSessions as Fi,relativeTime as ir}from"document360-engine";async function lr(e,t){let o=Fi(t.cwd).filter(n=>n.uuid!==t.currentUuid());if(o.length===0)return console.log(p("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let n=e.join(" "),r=Ui(t.cwd,n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(b(`No session matches "${n}".`)),sr(o),{kind:"continue"})}if(!process.stdin.isTTY)return sr(o),console.log(p("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>Oi({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return o.filter(d=>!c||d.name.toLowerCase().includes(c)||d.firstPrompt.toLowerCase().includes(c)).map(d=>({name:`${d.name} ${ir(d.updatedAt)}`,value:d.uuid,description:d.firstPrompt.slice(0,100)}))}})),r=Wi(n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function sr(e){console.log("");for(let t of e.slice(0,15))console.log(` ${P(t.name)} ${p(ir(t.updatedAt))}`),console.log(` ${p(t.firstPrompt.slice(0,80))}`);console.log("")}H();import{renameSession as Bi}from"document360-engine";async function ar(e,t){let o=e.join(" ").trim();if(!o)return console.log(b("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(Bi(n,o)?console.log(E(`\u2713 Session renamed to "${o}"`)):console.log(b("Could not find the current session record.")),{kind:"continue"}):(console.log(b("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as cr,readProjectConfig as Hi,writeProjectConfig as qi}from"document360-engine";H();function Lo(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=Hi(e);if(!n)return"No .d360-writer.json \u2014 run /init first.";if(n.profiles?.[t])return`Profile "${t}" already exists.`;let r=o??t;return cr().includes(r)?(n.profiles={...n.profiles,[t]:{connection:{environment:r},production:!1}},qi(n,e),null):`Unknown environment "${r}". Known: ${cr().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function ur(e,t){let o=e[0];if(!o)return Dt(t.cwd),{kind:"continue"};if(o==="add"){let n=Lo(t.cwd,e[1],e[2]);return n?(console.log(b(n)),{kind:"continue"}):(console.log(E(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${P(`/profile ${e[1]}`)} then ${P("/login")}`),{kind:"continue"})}return It(t.cwd,o),_t(t.cwd,o),console.log(p(" Restarting agent for the new profile\u2026")),{kind:"clear"}}H();import{select as zi}from"@inquirer/prompts";import{readProjectConfig as Gi,readUserConfig as dr,resolveModelSetting as Oo,writeUserConfig as pr}from"document360-engine";var de=[{value:null,label:"Auto",desc:"Engine right-sizes per task \u2014 Sonnet for routine work, Opus for analysis (recommended)"},{value:"claude-fable-5",label:"Fable",desc:"Fable 5 \xB7 most capable, for the hardest and longest-running tasks"},{value:"opus",label:"Opus",desc:"Opus 4.8 \xB7 best for everyday complex tasks"},{value:"sonnet",label:"Sonnet",desc:"Sonnet 4.6 \xB7 efficient for routine tasks"},{value:"haiku",label:"Haiku",desc:"Haiku 4.5 \xB7 fastest for quick answers"}];function Ht(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),o=de.findIndex(n=>n.value!==null&&(n.value===t||n.label.toLowerCase()===t||t.includes(n.label.toLowerCase())));return o>=0?o:0}function Xi(e){switch(e.source){case"project":return".d360-writer.json defaultModel (team setting)";case"user":return"~/.document360-writer/config.json (your /model setting)";case"env":return"ANTHROPIC_MODEL environment variable";case"claude-settings":return"Claude Code's own settings (~/.claude/settings.json)";case"claude-default":return"Claude Code default (no override configured)"}}function ut(e,t){let o=()=>{let s=Oo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=dr();return s.defaultModel?(delete s.defaultModel,pr(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Oo(e).model??"Claude Code default"} (applies from your next message)`],changed:!0,effective:o()}):{lines:["No personal model override set \u2014 nothing to clear."],changed:!1,effective:void 0}}pr({...dr(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=Gi(e)?.defaultModel;return r&&n.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:n,changed:!0,effective:o()}}async function mr(e,t){let o=e[0]?.trim();if(!o){let c=Oo(t.cwd);if(!process.stdin.isTTY)return console.log(`${he("Model:")} ${P(c.model??"Claude Code default")}`),console.log(p(` source: ${Xi(c)}`)),console.log(p(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let d=Ht(c),h;try{h=await t.withPausedInput(()=>zi({message:`Select model (current: ${c.model??"Claude Code default"})`,default:de[d].value,choices:de.map((x,j)=>({name:`${x.label}${j===d?" \u2714":""}`,value:x.value,description:x.desc}))}))}catch{return console.log(p("Cancelled.")),{kind:"continue"}}let{lines:f,changed:w,effective:R}=ut(t.cwd,h??"default");for(let x of f)console.log(x.startsWith("\u26A0")?L(x):x.startsWith("\u2713")?E(x):p(x));return w&&await t.setModel(R),{kind:"continue"}}let{lines:n,changed:r,effective:s}=ut(t.cwd,o);for(let c of n)console.log(c.startsWith("\u26A0")?L(c):c.startsWith("\u2713")?E(c):p(c));return r&&await t.setModel(s),{kind:"continue"}}qt();async function yr(e,t){return await t.withPausedInput(()=>Ge(t.cwd)),{kind:"clear"}}H();import{resolveActiveProfile as rl}from"document360-engine";async function wr(e,t){let o=!1;try{o=rl(t.cwd).production}catch{}return o?(console.log(L("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(p("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}H();var xr=async(e,t)=>{try{await t.withPausedInput(()=>Et({}))}catch(o){console.log(b(`Login failed: ${o.message}`))}return{kind:"continue"}};import{existsSync as $r}from"node:fs";import{isAbsolute as sl,join as il,resolve as ll}from"node:path";import{readProjectConfig as al,screenshotPlaceholderIds as cl}from"document360-engine";var br=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Wo(e){if(e[0]==="--list")return{mode:"list",scope:e[1]?br(e[1]):void 0};let t=e[0];return!t||t==="--all"?{mode:"all"}:/[\\/]/.test(t)||t.endsWith(".md")?{mode:"scope",scope:br(t)}:{mode:"single",id:t}}function Fo(e,t){let o=al(e),n=o?.captureDir??"user-docs/_capture",r=o?.outputDir??"user-docs/_screenshots",s=(c,d)=>sl(c)?il(c,d):ll(e,c,d);return cl(e,{scope:t}).map(({id:c,file:d})=>{let h=$r(s(r,`${c}.png`))?"captured":$r(s(n,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:d,state:h}})}var ul={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},dl={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Bo(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let o=[...new Set(e.map(s=>s.file))].sort(),n=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${o.length} article${o.length===1?"":"s"} \xB7 ${n} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of o){r.push(s);for(let c of e.filter(d=>d.file===s))r.push(` ${ul[c.state]} ${c.id.padEnd(34)} ${dl[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function vr(e){return["Run the emit-screenshot-spec skill to author the document360-capture spec for EACH of these","SCREENSHOT placeholder ids:",e.paths.map(t=>`- ${t}`).join(`
50
50
  `),"","For each: locate its <!-- SCREENSHOT --> block in user-docs/**/*.md; read the product source for","EXACT routes + stable data-testid selectors (never guess); enter the prepared context via","captureScope(); guard data prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts","(skip-with-TODO if a stable selector is missing). Do NOT run other skills. Report the specs you","wrote and any TODO data-testids."].join(`
51
- `)}async function Ht(e,t){let o=Uo(e);if(o.mode==="list"){let c=t?.cwd??process.cwd();for(let p of Fo(Wo(c,o.scope),o.scope))console.log(p);return{kind:"continue"}}if(o.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${o.id}\`.`,"Locate its <!-- SCREENSHOT --> block in user-docs/**/*.md, then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${o.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
51
+ `)}async function zt(e,t){let o=Wo(e);if(o.mode==="list"){let c=t?.cwd??process.cwd();for(let d of Bo(Fo(c,o.scope),o.scope))console.log(d);return{kind:"continue"}}if(o.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${o.id}\`.`,"Locate its <!-- SCREENSHOT --> block in user-docs/**/*.md, then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${o.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
52
52
  `),display:`/screenshot ${o.id}`};let r=[`Run the emit-screenshot-spec skill to (re)generate the document360-capture spec for ${o.mode==="scope"?`every <!-- SCREENSHOT --> placeholder in articles under ${o.scope}`:"every <!-- SCREENSHOT --> placeholder across user-docs/**/*.md"}.`,"Follow that skill exactly for each: read the SCREENSHOT block; read the product source for EXACT routes","and stable data-testid selectors (never guess); enter the prepared context via captureScope(); guard data","prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts. When done, run capture-setup-checklist","to refresh CAPTURE-SETUP.md. Report specs written, TODO data-testids, and data to stage."].join(`
53
- `),s=o.mode==="scope"?`/screenshot ${o.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}async function qt(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across user-docs/**/*.md, read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
54
- `),display:"/capture-setup"}}var $r={help:ho,"?":ho,clear:Tn,exit:ko,quit:ko,init:Dn,mcp:Mt,publish:On,audit:Lt,scope:Wn,sync:Xn,convert:tr,resume:rr,rename:sr,profile:lr,model:ur,doctor:ut,workspace:gr,"allow-prod":hr,login:kr,screenshot:Ht,"capture-setup":qt};function br(e){let t=e.trim();if(!t.startsWith("/"))return null;let o=t.slice(1).split(/\s+/),n=(o[0]??"").toLowerCase();return n?{name:n,args:o.slice(1)}:null}B();var ul={Bash:"command",PowerShell:"command",Read:"file_path",Write:"file_path",Edit:"file_path",NotebookEdit:"notebook_path",Glob:"pattern",Grep:"pattern",WebFetch:"url",WebSearch:"query",Agent:"description",Task:"description",Skill:"skill"},Cr=160,Ho=200,Pr=40;function Z(e,t){let o=e.replace(/\s+/g," ").trim();return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function vr(e){let t=Object.entries(e).filter(([,n])=>n!=null&&n!=="");if(t.length===0)return null;let o=t.slice(0,4).map(([n,r])=>`${n}: ${Z(typeof r=="string"?r:JSON.stringify(r),Pr)}`);return t.length>4&&o.push("\u2026"),Z(o.join(", "),Cr)}var Pe=e=>typeof e=="string"&&e?e:null,Bo=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function dl(e){let t=Pe(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);if(o.length<2)return null;let n=o[o.length-2],r=n.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?n:r.map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}function pl(e,t){let o=(n,r)=>({title:`Document360: ${n}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let n=Pe(t.title);if(!n)return null;let r=dl(t.local_path);return o("Create article",`"${Z(n,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let n=Pe(t.title),r=Bo(t.article_id);return o("Update article",n?`"${Z(n,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return o("Fork article (new draft)",Bo(t.article_id)?`id ${Bo(t.article_id)}\u2026`:null);case"d360_publish_article":{let n=t.version_number;return o("Publish article LIVE",typeof n=="number"?`v${n}`:null)}case"d360_unpublish_article":return o("Unpublish article",null);case"d360_create_category":return o("Create category",Pe(t.name)?`"${Z(Pe(t.name),60)}"`:null);case"d360_upload_drive_file":{let n=Pe(t.file_path);return o("Upload image",n?Z(n.replace(/\\/g,"/").split("/").pop()??n,60):null)}case"d360_sync_status":return o("Check sync status",null);default:return null}}function zt(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let f=pl(c,t);if(f)return f}let p=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${p}`,sep:" ",arg:vr(t)}}let o=ul[e],n=o?t[o]:void 0;return typeof n=="string"&&n?{title:e,sep:"",arg:Z(n,Cr)}:{title:e,sep:"",arg:vr(t)}}function ml(e){if(e===null||typeof e!="object")return typeof e=="string"?e:null;let t=e;for(let o of["name","title","slug","id"])if(typeof t[o]=="string"&&t[o])return t[o];return null}function fl(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let o=t.map(ml).filter(r=>r!==null),n=`${t.length} item${t.length===1?"":"s"}`;return o.length===0?[n]:[n,...o.map(r=>Z(r,Ho))]}if(t!==null&&typeof t=="object"){let o=Object.entries(t).filter(([,n])=>n!==null&&(typeof n=="string"||typeof n=="number"||typeof n=="boolean")).slice(0,6).map(([n,r])=>`${n}: ${Z(String(r),Pr)}`);return o.length>0?[Z(o.join(" \xB7 "),Ho)]:null}return null}function gl(e){let t=Pe(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);return o.length>1?o.slice(1).join("/"):t}function hl(e,t,o){switch(e){case"d360_create_article":{let n=gl(t?.local_path);return[`\u2713 draft created${n?` \xB7 ${n}`:""}`]}case"d360_update_article":return["\u2713 draft updated"];case"d360_fork_article":return["\u2713 forked to a new draft"];case"d360_publish_article":return["\u2713 PUBLISHED LIVE \u2014 visible to readers"];case"d360_unpublish_article":return["\u2713 reverted to draft (removed from readers)"];case"d360_create_category":{let n=Pe(t?.name);return[`\u2713 category created${n?` \xB7 "${n}"`:""}`]}case"d360_upload_drive_file":{let n=o.match(/https?:\/\/\S+/);return[`\u2713 uploaded${n?` \xB7 ${Z(n[0],120)}`:""}`]}default:return null}}function Gt(e,t=4,o,n){let r=e.replace(/\r\n/g,`
55
- `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(o?.startsWith("mcp__document360__")){let c=hl(o.slice(18),n,r);if(c)return{lines:c,hidden:0}}let s=fl(r)??r.split(`
56
- `);return{lines:s.slice(0,t).map(c=>Z(c,Ho)),hidden:Math.max(0,s.length-t)}}function Xt(e,t,o,n="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${n}/${o}`}function Vt(e,t){if(typeof e.article_id=="string"&&e.article_id)return e.article_id;try{let o=JSON.parse(t),r=(Array.isArray(o)?o[0]:o)?.id;return typeof r=="string"&&r?r:null}catch{return null}}function Yt(e){try{let t=JSON.parse(e),n=(Array.isArray(t)?t[0]:t)?.url;return typeof n=="string"&&/^https?:\/\//.test(n)?n:null}catch{return null}}var Rl=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function jl(e,t,o,n){if(Rl.test(e))try{let r=jr(n),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=Vt(t,o),p=Yt(o);e.endsWith("publish_article")&&p&&console.log(y(` \u2B95 Live: ${p}`)),c&&s&&console.log(y(` \u2B95 Preview: ${Xt(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function Ar(e=process.cwd(),t="auto",o){let n=wl(t);n.kind==="none"&&(console.error(""),console.error(v("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${y("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${y('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${y("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${y("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),Al(e,o),n.kind==="subscription"&&(n.stored?console.log(u(" Using your Claude subscription (no API key set).")):console.log(u(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let r=!1,s=qo({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},p=kl({input:process.stdin,output:process.stdout}),h=[],f=null,x=!1;p.on("line",j=>{if(f){let T=f;f=null,T(j)}else h.push(j)}),p.on("close",()=>{if(x=!0,f){let j=f;f=null,j(null)}});function R(){return h.length>0?Promise.resolve(h.shift()):x?Promise.resolve(null):(process.stdout.write(y("> ")),new Promise(j=>{f=j}))}let $={cwd:e,profileName:o,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=qo({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async j=>s.setModel(j),withPausedInput:async j=>{p.pause();try{return await j()}finally{p.resume()}}};try{for(;;){let j=await R();if(j===null)break;let T=j.trim();if(T){if(T.startsWith("/")){let M=br(T);if(!M)continue;let q=$r[M.name];if(!q){console.log(v(`Unknown command: /${M.name}`)),console.log(u("Type /help for the list."));continue}let z=await q(M.args,$);if(z.kind==="exit")break;if(z.kind==="clear"){$.restartAgent();continue}if(z.kind==="allow-prod"){r=!0,$.restartAgent(),console.log(E("\u2713 Production writes authorized for this session.")),console.log("");continue}if(z.kind==="resume"){s.close(),s=qo({cwd:e,resume:z.uuid,profileName:o,allowProdWrites:r});let ee=yl(z.uuid);c={uuid:z.uuid,firstPrompt:ee?.firstPrompt??null,titleFired:!0},Rr(z.uuid),console.log(E(`\u2713 Resumed "${z.name}"`)),console.log("");continue}z.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=z.display??z.prompt),await Tr(s,z.prompt,n,c,e));continue}c.firstPrompt||(c.firstPrompt=T),await Tr(s,T,n,c,e)}}}finally{s.close(),p.close()}}function Al(e,t){console.log(""),console.log(ot("document360-writer")),console.log(u(` cwd: ${e}`));let o=Cl(e);console.log(u(` model: ${o.model??"auto (engine right-sizes per task)"}${o.model?` (${o.source})`:""}`)),console.log(El(e,t)),Pl(e)||console.log(L(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(u(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function El(e,t){try{let o=jr(e,t),n=o.production?L(" \u26A0 PRODUCTION"):"",r=Tl(o.name);if(!r)return u(` Document360: profile "${o.name}"${n} \u2014 not logged in (d360-writer login)`);let s={...Sr(r.idToken)??{},...Sr(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return Sl(r)&&!r.refreshToken?L(` Document360: profile "${o.name}"${n} \u2014 session expired (d360-writer login)`):u(` Document360: ${c} \xB7 profile "${o.name}"${n}`)}catch(o){return u(` Document360: ${o.message.split(".")[0]}`)}}function Dl(){console.error(""),console.error(`Sign in with your Claude subscription: run ${y("claude")} once, then retry.`),console.error(` (No Claude Code? ${y("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${y("https://console.anthropic.com/settings/keys")}`)}function Il(e,t,o){e.uuid=t;let n=new Date().toISOString();bl({uuid:t,name:$l(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:o,firstPrompt:e.firstPrompt??"",createdAt:n,updatedAt:n})}function Ml(e,t){e.titleFired=!0;let o=e.uuid,n=e.firstPrompt;!o||!n||vl(n,t).then(r=>{r&&xl(o,r)}).catch(()=>{})}async function Tr(e,t,o,n,r){let s=new Map;for await(let c of e.send(t))_l(c,n,r,o,s)}function _l(e,t,o,n,r){switch(e.type){case"session":t.uuid||Il(t,e.sessionId,o);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=zt(e.name,e.input);s&&(process.stdout.write(`
53
+ `),s=o.mode==="scope"?`/screenshot ${o.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}async function Gt(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across user-docs/**/*.md, read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
54
+ `),display:"/capture-setup"}}var Cr={help:ko,"?":ko,clear:An,exit:yo,quit:yo,init:Mn,mcp:Nt,publish:Fn,audit:Ut,scope:Hn,sync:Kn,convert:rr,resume:lr,rename:ar,profile:ur,model:mr,doctor:dt,workspace:yr,"allow-prod":wr,login:xr,screenshot:zt,"capture-setup":Gt};function Pr(e){let t=e.trim();if(!t.startsWith("/"))return null;let o=t.slice(1).split(/\s+/),n=(o[0]??"").toLowerCase();return n?{name:n,args:o.slice(1)}:null}H();var pl={Bash:"command",PowerShell:"command",Read:"file_path",Write:"file_path",Edit:"file_path",NotebookEdit:"notebook_path",Glob:"pattern",Grep:"pattern",WebFetch:"url",WebSearch:"query",Agent:"description",Task:"description",Skill:"skill"},Tr=160,qo=200,Rr=40;function Z(e,t){let o=e.replace(/\s+/g," ").trim();return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function Sr(e){let t=Object.entries(e).filter(([,n])=>n!=null&&n!=="");if(t.length===0)return null;let o=t.slice(0,4).map(([n,r])=>`${n}: ${Z(typeof r=="string"?r:JSON.stringify(r),Rr)}`);return t.length>4&&o.push("\u2026"),Z(o.join(", "),Tr)}var Pe=e=>typeof e=="string"&&e?e:null,Ho=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function ml(e){let t=Pe(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);if(o.length<2)return null;let n=o[o.length-2],r=n.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?n:r.map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}function fl(e,t){let o=(n,r)=>({title:`Document360: ${n}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let n=Pe(t.title);if(!n)return null;let r=ml(t.local_path);return o("Create article",`"${Z(n,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let n=Pe(t.title),r=Ho(t.article_id);return o("Update article",n?`"${Z(n,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return o("Fork article (new draft)",Ho(t.article_id)?`id ${Ho(t.article_id)}\u2026`:null);case"d360_publish_article":{let n=t.version_number;return o("Publish article LIVE",typeof n=="number"?`v${n}`:null)}case"d360_unpublish_article":return o("Unpublish article",null);case"d360_create_category":return o("Create category",Pe(t.name)?`"${Z(Pe(t.name),60)}"`:null);case"d360_upload_drive_file":{let n=Pe(t.file_path);return o("Upload image",n?Z(n.replace(/\\/g,"/").split("/").pop()??n,60):null)}case"d360_sync_status":return o("Check sync status",null);default:return null}}function Xt(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let f=fl(c,t);if(f)return f}let d=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${d}`,sep:" ",arg:Sr(t)}}let o=pl[e],n=o?t[o]:void 0;return typeof n=="string"&&n?{title:e,sep:"",arg:Z(n,Tr)}:{title:e,sep:"",arg:Sr(t)}}function gl(e){if(e===null||typeof e!="object")return typeof e=="string"?e:null;let t=e;for(let o of["name","title","slug","id"])if(typeof t[o]=="string"&&t[o])return t[o];return null}function hl(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let o=t.map(gl).filter(r=>r!==null),n=`${t.length} item${t.length===1?"":"s"}`;return o.length===0?[n]:[n,...o.map(r=>Z(r,qo))]}if(t!==null&&typeof t=="object"){let o=Object.entries(t).filter(([,n])=>n!==null&&(typeof n=="string"||typeof n=="number"||typeof n=="boolean")).slice(0,6).map(([n,r])=>`${n}: ${Z(String(r),Rr)}`);return o.length>0?[Z(o.join(" \xB7 "),qo)]:null}return null}function kl(e){let t=Pe(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);return o.length>1?o.slice(1).join("/"):t}function yl(e,t,o){switch(e){case"d360_create_article":{let n=kl(t?.local_path);return[`\u2713 draft created${n?` \xB7 ${n}`:""}`]}case"d360_update_article":return["\u2713 draft updated"];case"d360_fork_article":return["\u2713 forked to a new draft"];case"d360_publish_article":return["\u2713 PUBLISHED LIVE \u2014 visible to readers"];case"d360_unpublish_article":return["\u2713 reverted to draft (removed from readers)"];case"d360_create_category":{let n=Pe(t?.name);return[`\u2713 category created${n?` \xB7 "${n}"`:""}`]}case"d360_upload_drive_file":{let n=o.match(/https?:\/\/\S+/);return[`\u2713 uploaded${n?` \xB7 ${Z(n[0],120)}`:""}`]}default:return null}}function Vt(e,t=4,o,n){let r=e.replace(/\r\n/g,`
55
+ `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(o?.startsWith("mcp__document360__")){let c=yl(o.slice(18),n,r);if(c)return{lines:c,hidden:0}}let s=hl(r)??r.split(`
56
+ `);return{lines:s.slice(0,t).map(c=>Z(c,qo)),hidden:Math.max(0,s.length-t)}}function Yt(e,t,o,n="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${n}/${o}`}function Kt(e,t){if(typeof e.article_id=="string"&&e.article_id)return e.article_id;try{let o=JSON.parse(t),r=(Array.isArray(o)?o[0]:o)?.id;return typeof r=="string"&&r?r:null}catch{return null}}function Jt(e){try{let t=JSON.parse(e),n=(Array.isArray(t)?t[0]:t)?.url;return typeof n=="string"&&/^https?:\/\//.test(n)?n:null}catch{return null}}var Al=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function El(e,t,o,n){if(Al.test(e))try{let r=Dr(n),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=Kt(t,o),d=Jt(o);e.endsWith("publish_article")&&d&&console.log(P(` \u2B95 Live: ${d}`)),c&&s&&console.log(P(` \u2B95 Preview: ${Yt(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function Ir(e=process.cwd(),t="auto",o){let n=xl(t);n.kind==="none"&&(console.error(""),console.error(b("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${P("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${P('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${P("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${P("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),Dl(e,o),n.kind==="subscription"&&(n.stored?console.log(p(" Using your Claude subscription (no API key set).")):console.log(p(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let r=!1,s=zo({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},d=wl({input:process.stdin,output:process.stdout}),h=[],f=null,w=!1;d.on("line",j=>{if(f){let T=f;f=null,T(j)}else h.push(j)}),d.on("close",()=>{if(w=!0,f){let j=f;f=null,j(null)}});function R(){return h.length>0?Promise.resolve(h.shift()):w?Promise.resolve(null):(process.stdout.write(P("> ")),new Promise(j=>{f=j}))}let x={cwd:e,profileName:o,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=zo({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async j=>s.setModel(j),withPausedInput:async j=>{d.pause();try{return await j()}finally{d.resume()}}};try{for(;;){let j=await R();if(j===null)break;let T=j.trim();if(T){if(T.startsWith("/")){let _=Pr(T);if(!_)continue;let q=Cr[_.name];if(!q){console.log(b(`Unknown command: /${_.name}`)),console.log(p("Type /help for the list."));continue}let z=await q(_.args,x);if(z.kind==="exit")break;if(z.kind==="clear"){x.restartAgent();continue}if(z.kind==="allow-prod"){r=!0,x.restartAgent(),console.log(E("\u2713 Production writes authorized for this session.")),console.log("");continue}if(z.kind==="resume"){s.close(),s=zo({cwd:e,resume:z.uuid,profileName:o,allowProdWrites:r});let ee=$l(z.uuid);c={uuid:z.uuid,firstPrompt:ee?.firstPrompt??null,titleFired:!0},Er(z.uuid),console.log(E(`\u2713 Resumed "${z.name}"`)),console.log("");continue}z.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=z.display??z.prompt),await Ar(s,z.prompt,n,c,e));continue}c.firstPrompt||(c.firstPrompt=T),await Ar(s,T,n,c,e)}}}finally{s.close(),d.close()}}function Dl(e,t){console.log(""),console.log(ot("document360-writer")),console.log(p(` cwd: ${e}`));let o=Sl(e);console.log(p(` model: ${o.model??"auto (engine right-sizes per task)"}${o.model?` (${o.source})`:""}`)),console.log(Il(e,t)),Tl(e)||console.log(L(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(p(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function Il(e,t){try{let o=Dr(e,t),n=o.production?L(" \u26A0 PRODUCTION"):"",r=jl(o.name);if(!r)return p(` Document360: profile "${o.name}"${n} \u2014 not logged in (d360-writer login)`);let s={...jr(r.idToken)??{},...jr(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return Rl(r)&&!r.refreshToken?L(` Document360: profile "${o.name}"${n} \u2014 session expired (d360-writer login)`):p(` Document360: ${c} \xB7 profile "${o.name}"${n}`)}catch(o){return p(` Document360: ${o.message.split(".")[0]}`)}}function _l(){console.error(""),console.error(`Sign in with your Claude subscription: run ${P("claude")} once, then retry.`),console.error(` (No Claude Code? ${P("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${P("https://console.anthropic.com/settings/keys")}`)}function Ml(e,t,o){e.uuid=t;let n=new Date().toISOString();Cl({uuid:t,name:vl(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:o,firstPrompt:e.firstPrompt??"",createdAt:n,updatedAt:n})}function Nl(e,t){e.titleFired=!0;let o=e.uuid,n=e.firstPrompt;!o||!n||Pl(n,t).then(r=>{r&&bl(o,r)}).catch(()=>{})}async function Ar(e,t,o,n,r){let s=new Map;for await(let c of e.send(t))Ll(c,n,r,o,s)}function Ll(e,t,o,n,r){switch(e.type){case"session":t.uuid||Ml(t,e.sessionId,o);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=Xt(e.name,e.input);s&&(process.stdout.write(`
57
57
 
58
- `),console.log(`${E("\u25CF")} ${se(s.title)}${s.arg!==null?Y(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=_e(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=p=>p===1?"":"s";console.log(Y(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let p of s.lines)console.log(p);s.hidden>0&&console.log(u(` \u2026 +${s.hidden} more diff lines`));break}case"tool_result":{let s=r.get(e.id);if(!s)break;r.delete(e.id);let c=Gt(e.output,4,e.isError?void 0:s.name,s.input),p=e.isError?v:Y;c.lines.forEach((h,f)=>console.log(p((f===0?" \u23BF ":" ")+h))),c.hidden>0&&console.log(u(` \u2026 +${c.hidden} lines`)),e.isError||jl(s.name,s.input,e.output,o);break}case"result":process.stdout.write(`
59
- `),console.log(u(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(Rr(t.uuid),t.titleFired||Ml(t,o));break;case"error":console.error(""),console.error(v(`agent error: ${e.message}`)),n.kind==="subscription"&&e.kind==="auth"&&Dl();break}}import{render as Ca}from"ink";import{resolveAuth as Pa}from"document360-engine";import{useCallback as G,useEffect as Ve,useMemo as Zt,useRef as V,useState as U}from"react";import{Box as X,Text as C,useApp as Ol,useInput as Ul,useStdout as Wl}from"ink";import{existsSync as Fl,readFileSync as Hr,readdirSync as Bl}from"node:fs";import{basename as qr,isAbsolute as Hl,join as to}from"node:path";import{createSession as zr,loginPkce as ql,toStoredTokens as zl,saveTokens as Gl,getAccessToken as Xl,resolveActiveProfile as me,resolveProjectId as Vl,getArticle as Yl,decodeJwtClaims as Ye,isExpired as Se,loadTokens as Le,setTitle as Kl,slugify as Jl,touchSession as zo,upsertSession as Ql,generateTitle as Gr,findByName as Zl,listSessions as ea,renameSession as ta,suggestNextAction as oa,readProjectConfig as ie,writeProjectConfig as na,resolveModelSetting as Vo,loadProfileMap as Xr,applyPull as ra,computeSyncStatus as Go,planPull as sa,inventoryRepo as ia,knownEnvironments as la,resolveEnvironment as aa,planPartitions as ca,partitionEvenly as ua,screenshotPlaceholderIds as da,trackedArticlePaths as pa,runPartitioned as Vr,estimateBulkCost as ma,resolveModelForOperation as Yr}from"document360-engine";Bt();var Kt=[{name:"help",usage:"/help",desc:"Show available commands"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser) without leaving the session"},{name:"resume",usage:"/resume [name]",desc:"Resume a session (no arg lists them)"},{name:"rename",usage:"/rename [name]",desc:"Name the session (no arg: suggest one)"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch connection (picker; s = session only)"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API"},{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Manage MCP servers"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits"},{name:"convert",usage:"/convert [--scope <folder>] [--run]",desc:"Convert tracked articles to DFM across parallel agents (preview; --scope to limit; --run to start)"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (analyses + recommends)"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (incremental)"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: placeholders + status; else author specs (id/--all/path)"},{name:"capture-setup",usage:"/capture-setup",desc:'Generate the "stage this data" checklist for screenshots'},{name:"clear",usage:"/clear",desc:"Reset the conversation (resumable)"},{name:"exit",usage:"/exit",desc:"Quit"}];function Er(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return Kt.filter(o=>o.name.startsWith(t))}function Dr(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}var Nl=/^(?:\d+[.)]|[-*•])\s+/;function Ir(e,t,o=4){let n=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
60
- `)){let c=s.trim().replace(Nl,""),p=c.match(/^`([^`]+)`$/);p&&(c=p[1].trim());let h=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!h||!n.has(h[1].toLowerCase()))continue;let f=`/${h[1].toLowerCase()}${h[2]?` ${h[2]}`:""}`;if(r.includes(f)||r.push(f),r.length>=o)break}return r}B();var Ll=/\[Pasted text #\d+ \+\d+ lines?\]/g;function Mr(e){return e.replace(/\r\n?/g,`
61
- `)}function _r(e){return e.includes(`
62
- `)||e.length>200}function Nr(e,t){let o=t.split(`
63
- `).length;return`[Pasted text #${e} +${o} line${o===1?"":"s"}]`}function Lr(e,t){return e.replace(Ll,o=>t.get(o)??o)}function Or(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function Jt(e,t){let o=Math.max(1,t),n=[],r=0;for(let s of e.split(`
64
- `)){if(s.length===0)n.push({start:r,end:r});else for(let c=0;c<s.length;c+=o)n.push({start:r+c,end:r+Math.min(c+o,s.length)});r+=s.length+1}return n.length>0?n:[{start:0,end:0}]}function Qt(e,t){let o=0;for(let n=0;n<e.length&&e[n].start<=t;n++)o=n;return o}function Ur(e,t,o){let n=Qt(e,t),r=n+o;if(r<0||r>=e.length)return t;let s=Math.min(t,e[n].end)-e[n].start;return Math.min(e[r].start+s,e[r].end)}function Wr(e,t,o){let n=e[Qt(e,t)];return o==="start"?n.start:n.end}function Fr(e,t,o){let n=Math.max(1,o),r=e.split(`
65
- `),s=0;for(let c=r.length-1;c>=0;c--){let p=r[c];if(s+=Math.max(1,Math.ceil(p.length/n)),s>=t){let h=s-t;return{text:[h>0?p.slice(h*n):p,...r.slice(c+1)].join(`
66
- `),truncated:c>0||h>0}}}return{text:e,truncated:!1}}function Br(e){let t=!1,o=0,n=0;for(let r of e.split(`
67
- `)){let s=n+r.length;/^\s*```/.test(r)?t=!t:!t&&r.trim()===""&&s<e.length&&(o=s+1),n=s+1}return o}import{Fragment as Xo,jsx as P,jsxs as O}from"react/jsx-runtime";var fa={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function ga(e,t,o,n){let r=o.kind==="api"?"API key":o.kind==="subscription"?"subscription":"not configured",s=Vo(e),c=ie(e),p=(c?.docsDir??"user-docs").replace(/\/+$/,""),h=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${p}/ + config`,f={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:fa[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:h};if(c===null)return f;try{let x=me(e,n);f.profile=x.name,f.apiUrl=x.connection.apiUrl,f.prod=x.production,f.project=x.project.projectId??"(chosen at login)";let R=Le(x.name);if(R){let $={...Ye(R.idToken)??{},...Ye(R.accessToken)??{}},j=$.email??$.preferred_username??"signed in";Se(R)?R.refreshToken&&(f.who=j,f.loggedOut=!1,f.sessionHint="session expired \u2014 refreshing\u2026"):(f.who=j,f.loggedOut=!1,f.sessionHint=`session valid until ${new Date(R.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return f}function ha(e,t){try{let o=me(e,t),n=Le(o.name);if(!n)return{text:`profile "${o.name}" \u2014 not logged in (/login)`,prod:o.production};let r={...Ye(n.idToken)??{},...Ye(n.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return Se(n)&&!n.refreshToken?{text:`profile "${o.name}" \u2014 session expired (/login)`,prod:o.production}:{text:`${s} \xB7 profile "${o.name}"`,prod:o.production}}catch(o){return{text:o.message.split(".")[0],prod:!1}}}var Kr=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],oo=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],ka="Ask me to write or update an article\u2026";function eo({ch:e,dim:t}){let[o,n]=U(!0);return Ve(()=>{let r=setInterval(()=>n(s=>!s),530);return()=>clearInterval(r)},[]),P(C,{inverse:o,color:t&&!o?"gray":void 0,children:e})}var wa=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Jr(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var ya=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Qr(e){try{return Bl(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function xa(e,t){let o=t??[];return o.length===0?["src","api","services","packages","modules"].some(n=>Qr(to(e,n))):o.some(n=>!n.includes("/")&&!n.endsWith(".md")&&Qr(to(e,n)))}function $a({startTime:e,chars:t}){let[o,n]=U(0);Ve(()=>{let h=setInterval(()=>n(f=>f+1),120);return()=>clearInterval(h)},[]);let r=oo[o%oo.length],s=Kr[Math.floor(o/16)%Kr.length],c=Math.floor((Date.now()-e)/1e3),p=Math.round(t/4);return O(X,{children:[P(C,{color:H,children:` ${r} ${s}\u2026 `}),P(C,{color:"gray",children:`(${at(c)} \xB7 ~${p} tokens \xB7 esc to interrupt)`})]})}var ba=12e4;function va({p:e}){let[t,o]=U(0);Ve(()=>{let h=setInterval(()=>o(f=>f+1),150);return()=>clearInterval(h)},[]);let n=oo[t%oo.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),p=Date.now()-e.lastAt>=ba;return O(X,{flexDirection:"column",children:[O(X,{children:[P(C,{color:H,children:` ${n} ${e.verb??"Converting"} `}),P(C,{color:"gray",children:`${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${at(r)} \xB7 esc to stop`})]}),e.active.length>0&&P(C,{dimColor:!0,children:` \u25B8 ${e.active.join(", ")} \xB7 last activity ${s}s ago`}),p&&P(C,{color:"yellow",children:` \u26A0 no activity for ${at(s)} \u2014 press esc to stop if it's wedged`})]})}function Zr({cwd:e,auth:t,profileName:o,version:n}){let{exit:r}=Ol(),[s,c]=U(o),[p,h]=U(null),[f,x]=U({text:"",pos:0}),R=f.text,$=G(a=>{x(l=>{let i=typeof a=="function"?a(l.text):a;return{text:i,pos:i.length}})},[]),j=G(a=>{x(l=>({text:l.text.slice(0,l.pos)+a+l.text.slice(l.pos),pos:l.pos+a.length}))},[]),T=G(()=>{x(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),i=Or(l)??l.slice(0,-1);return{text:i+a.text.slice(a.pos),pos:i.length}})},[]),[M,q]=U(!1),[z,ee]=U(!1),[le,we]=U(0),[ae,ce]=U(null),[Ke,K]=U([]),dt=V(0),[ns,no]=U(!1),[rs,pt]=U(0),Je=V(0),[Qe,Oe]=U(0),[Ue,mt]=U(null),Jo=V(new Map),ss=V(0),ro=V([]),te=V(null);te.current===null&&(te.current=zr({cwd:e,profileName:s,allowProdWrites:!1}));let Te=V({uuid:null,firstPrompt:null,titleFired:!1}),ft=V(new Map),gt=V(!1),so=V([]),ye=V([]),[Re,je]=U(null),[fe,We]=U(null),[xe,Fe]=U(null),[J,Be]=U(null),[ge,He]=U(null),[oe,Ae]=U(null),[Q,qe]=U(null),[Qo,io]=U([]),Ee=V([]),ht=V(!1),De=V(null),[Zo,ue]=U(null),kt=V(!1),wt=V(null),[ne,Ie]=U(null),[en,yt]=U(0),[tn,xt]=U(0),[is,on]=U(0),lo=Zt(()=>{try{return Vo(e).model??"auto"}catch{return null}},[e,is]),{stdout:de}=Wl(),[,ls]=U(0),nn=V(`${de.columns??80}x${de.rows??24}`),$e=G(()=>Math.max(20,(de.columns??80)-1),[de]),d=G(a=>{ro.current.push(a),console.log(Ro(a,$e()))},[$e]);Ve(()=>{if(d({kind:"banner",info:ga(e,n,t,s)}),!ie(e)){d({kind:"note",tone:"info",text:`Welcome! This repo isn't set up for d360-writer yet \u2014 three steps and you're writing docs:
58
+ `),console.log(`${E("\u25CF")} ${he(s.title)}${s.arg!==null?Y(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=Me(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=d=>d===1?"":"s";console.log(Y(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let d of s.lines)console.log(d);s.hidden>0&&console.log(p(` \u2026 +${s.hidden} more diff lines`));break}case"tool_result":{let s=r.get(e.id);if(!s)break;r.delete(e.id);let c=Vt(e.output,4,e.isError?void 0:s.name,s.input),d=e.isError?b:Y;c.lines.forEach((h,f)=>console.log(d((f===0?" \u23BF ":" ")+h))),c.hidden>0&&console.log(p(` \u2026 +${c.hidden} lines`)),e.isError||El(s.name,s.input,e.output,o);break}case"result":process.stdout.write(`
59
+ `),console.log(p(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(Er(t.uuid),t.titleFired||Nl(t,o));break;case"error":console.error(""),console.error(b(`agent error: ${e.message}`)),n.kind==="subscription"&&e.kind==="auth"&&_l();break}}import{render as Sa}from"ink";import{resolveAuth as Ta}from"document360-engine";import{useCallback as G,useEffect as Ve,useMemo as eo,useRef as V,useState as U}from"react";import{Box as X,Text as v,useApp as Wl,useInput as Fl,useStdout as Bl}from"ink";import{existsSync as Hl,readFileSync as qr,readdirSync as ql}from"node:fs";import{basename as zr,isAbsolute as zl,join as oo}from"node:path";import{createSession as Gr,loginPkce as Gl,toStoredTokens as Xl,saveTokens as Vl,getAccessToken as Yl,resolveActiveProfile as pe,resolveProjectId as Kl,getArticle as Jl,decodeJwtClaims as Ye,isExpired as Se,loadTokens as Le,setTitle as Ql,slugify as Zl,touchSession as Go,upsertSession as ea,generateTitle as Xr,findByName as ta,listSessions as oa,renameSession as na,suggestNextAction as ra,readProjectConfig as se,writeProjectConfig as sa,resolveModelSetting as Yo,loadProfileMap as Vr,applyPull as ia,computeSyncStatus as Xo,planPull as la,inventoryRepo as aa,knownEnvironments as ca,resolveEnvironment as ua,planPartitions as da,partitionEvenly as pa,screenshotPlaceholderIds as ma,trackedArticlePaths as fa,runPartitioned as Yr,estimateBulkCost as ga,resolveModelForOperation as Kr}from"document360-engine";qt();var Ol=/^(?:\d+[.)]|[-*•])\s+/;function _r(e,t,o=4){let n=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
60
+ `)){let c=s.trim().replace(Ol,""),d=c.match(/^`([^`]+)`$/);d&&(c=d[1].trim());let h=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!h||!n.has(h[1].toLowerCase()))continue;let f=`/${h[1].toLowerCase()}${h[2]?` ${h[2]}`:""}`;if(r.includes(f)||r.push(f),r.length>=o)break}return r}H();var Ul=/\[Pasted text #\d+ \+\d+ lines?\]/g;function Mr(e){return e.replace(/\r\n?/g,`
61
+ `)}function Nr(e){return e.includes(`
62
+ `)||e.length>200}function Lr(e,t){let o=t.split(`
63
+ `).length;return`[Pasted text #${e} +${o} line${o===1?"":"s"}]`}function Or(e,t){return e.replace(Ul,o=>t.get(o)??o)}function Ur(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function Qt(e,t){let o=Math.max(1,t),n=[],r=0;for(let s of e.split(`
64
+ `)){if(s.length===0)n.push({start:r,end:r});else for(let c=0;c<s.length;c+=o)n.push({start:r+c,end:r+Math.min(c+o,s.length)});r+=s.length+1}return n.length>0?n:[{start:0,end:0}]}function Zt(e,t){let o=0;for(let n=0;n<e.length&&e[n].start<=t;n++)o=n;return o}function Wr(e,t,o){let n=Zt(e,t),r=n+o;if(r<0||r>=e.length)return t;let s=Math.min(t,e[n].end)-e[n].start;return Math.min(e[r].start+s,e[r].end)}function Fr(e,t,o){let n=e[Zt(e,t)];return o==="start"?n.start:n.end}function Br(e,t,o){let n=Math.max(1,o),r=e.split(`
65
+ `),s=0;for(let c=r.length-1;c>=0;c--){let d=r[c];if(s+=Math.max(1,Math.ceil(d.length/n)),s>=t){let h=s-t;return{text:[h>0?d.slice(h*n):d,...r.slice(c+1)].join(`
66
+ `),truncated:c>0||h>0}}}return{text:e,truncated:!1}}function Hr(e){let t=!1,o=0,n=0;for(let r of e.split(`
67
+ `)){let s=n+r.length;/^\s*```/.test(r)?t=!t:!t&&r.trim()===""&&s<e.length&&(o=s+1),n=s+1}return o}import{Fragment as Vo,jsx as C,jsxs as O}from"react/jsx-runtime";var ha={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function ka(e,t,o,n){let r=o.kind==="api"?"API key":o.kind==="subscription"?"subscription":"not configured",s=Yo(e),c=se(e),d=(c?.docsDir??"user-docs").replace(/\/+$/,""),h=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${d}/ + config`,f={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:ha[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:h};if(c===null)return f;try{let w=pe(e,n);f.profile=w.name,f.apiUrl=w.connection.apiUrl,f.prod=w.production,f.project=w.project.projectId??"(chosen at login)";let R=Le(w.name);if(R){let x={...Ye(R.idToken)??{},...Ye(R.accessToken)??{}},j=x.email??x.preferred_username??"signed in";Se(R)?R.refreshToken&&(f.who=j,f.loggedOut=!1,f.sessionHint="session expired \u2014 refreshing\u2026"):(f.who=j,f.loggedOut=!1,f.sessionHint=`session valid until ${new Date(R.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return f}function ya(e,t){try{let o=pe(e,t),n=Le(o.name);if(!n)return{text:`profile "${o.name}" \u2014 not logged in (/login)`,prod:o.production};let r={...Ye(n.idToken)??{},...Ye(n.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return Se(n)&&!n.refreshToken?{text:`profile "${o.name}" \u2014 session expired (/login)`,prod:o.production}:{text:`${s} \xB7 profile "${o.name}"`,prod:o.production}}catch(o){return{text:o.message.split(".")[0],prod:!1}}}var Jr=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],no=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],wa="Ask me to write or update an article\u2026";function to({ch:e,dim:t}){let[o,n]=U(!0);return Ve(()=>{let r=setInterval(()=>n(s=>!s),530);return()=>clearInterval(r)},[]),C(v,{inverse:o,color:t&&!o?"gray":void 0,children:e})}var xa=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Qr(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var $a=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Zr(e){try{return ql(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function ba(e,t){let o=t??[];return o.length===0?["src","api","services","packages","modules"].some(n=>Zr(oo(e,n))):o.some(n=>!n.includes("/")&&!n.endsWith(".md")&&Zr(oo(e,n)))}function va({startTime:e,chars:t}){let[o,n]=U(0);Ve(()=>{let h=setInterval(()=>n(f=>f+1),120);return()=>clearInterval(h)},[]);let r=no[o%no.length],s=Jr[Math.floor(o/16)%Jr.length],c=Math.floor((Date.now()-e)/1e3),d=Math.round(t/4);return O(X,{children:[C(v,{color:B,children:` ${r} ${s}\u2026 `}),C(v,{color:"gray",children:`(${ct(c)} \xB7 ~${d} tokens \xB7 esc to interrupt)`})]})}var Ca=12e4;function Pa({p:e}){let[t,o]=U(0);Ve(()=>{let h=setInterval(()=>o(f=>f+1),150);return()=>clearInterval(h)},[]);let n=no[t%no.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),d=Date.now()-e.lastAt>=Ca;return O(X,{flexDirection:"column",children:[O(X,{children:[C(v,{color:B,children:` ${n} ${e.verb??"Converting"} `}),C(v,{color:"gray",children:`${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${ct(r)} \xB7 esc to stop`})]}),e.active.length>0&&C(v,{dimColor:!0,children:` \u25B8 ${e.active.join(", ")} \xB7 last activity ${s}s ago`}),d&&C(v,{color:"yellow",children:` \u26A0 no activity for ${ct(s)} \u2014 press esc to stop if it's wedged`})]})}function es({cwd:e,auth:t,profileName:o,version:n}){let{exit:r}=Wl(),[s,c]=U(o),[d,h]=U(null),[f,w]=U({text:"",pos:0}),R=f.text,x=G(a=>{w(l=>{let i=typeof a=="function"?a(l.text):a;return{text:i,pos:i.length}})},[]),j=G(a=>{w(l=>({text:l.text.slice(0,l.pos)+a+l.text.slice(l.pos),pos:l.pos+a.length}))},[]),T=G(()=>{w(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),i=Ur(l)??l.slice(0,-1);return{text:i+a.text.slice(a.pos),pos:i.length}})},[]),[_,q]=U(!1),[z,ee]=U(!1),[ie,ye]=U(0),[le,ae]=U(null),[Ke,K]=U([]),pt=V(0),[rs,ro]=U(!1),[ss,mt]=U(0),Je=V(0),[Qe,Oe]=U(0),[Ue,ft]=U(null),Qo=V(new Map),is=V(0),so=V([]),te=V(null);te.current===null&&(te.current=Gr({cwd:e,profileName:s,allowProdWrites:!1}));let Te=V({uuid:null,firstPrompt:null,titleFired:!1}),gt=V(new Map),ht=V(!1),io=V([]),we=V([]),[Re,je]=U(null),[me,We]=U(null),[xe,Fe]=U(null),[J,Be]=U(null),[fe,He]=U(null),[oe,Ae]=U(null),[Q,qe]=U(null),[Zo,lo]=U([]),Ee=V([]),kt=V(!1),De=V(null),[en,ce]=U(null),yt=V(!1),wt=V(null),[ne,Ie]=U(null),[tn,xt]=U(0),[on,$t]=U(0),[ls,nn]=U(0),ao=eo(()=>{try{return Yo(e).model??"auto"}catch{return null}},[e,ls]),{stdout:ue}=Bl(),[,as]=U(0),rn=V(`${ue.columns??80}x${ue.rows??24}`),$e=G(()=>Math.max(20,(ue.columns??80)-1),[ue]),u=G(a=>{so.current.push(a),console.log(jo(a,$e()))},[$e]);Ve(()=>{if(u({kind:"banner",info:ka(e,n,t,s)}),!se(e)){u({kind:"note",tone:"info",text:`Welcome! This repo isn't set up for d360-writer yet \u2014 three steps and you're writing docs:
68
68
  1. /init \u2014 pick your Document360 environment & scaffold config
69
69
  2. /login signs in to that environment
70
70
  3. /workspace picks where articles go
71
- Press 1 to start.`}),K(["/init"]);return}try{let a=me(e,s),l=Le(a.name);l&&Se(l)&&l.refreshToken?Xl({profile:a.name,connection:a.connection}).then(()=>{d({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),we(i=>i+1)}).catch(()=>{d({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),K(["/login"])}):(!l||Se(l))&&(d({kind:"note",tone:"warn",text:`Profile "${a.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),K(["/login"]))}catch{}try{let a=ie(e),l=me(e,s),i=Le(l.name);a&&i&&!Se(i)&&xa(e,a.authoritativeSourceFiles)&&(d({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),K(["/scope"]))}catch{}},[]),Ve(()=>{let a=null,l=null,i=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let k=`${de.columns??80}x${de.rows??24}`;k!==nn.current&&(nn.current=k,ls(m=>m+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+er(ro.current,$e())+"\x1B[?2026l")},80))},400)};return de.on("resize",i),()=>{a&&clearTimeout(a),l&&clearTimeout(l),de.off("resize",i)}},[de,$e]);let Me=Math.max(20,(de.columns??80)-1),rn=Zt(()=>ha(e,s),[e,s,le]),ao=Zt(()=>{if(!ie(e))return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};try{let a=me(e,s),l=Le(a.name);if(!(!!l&&!(Se(l)&&!l.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${a.name}")\u2026`,isSetup:!0};if(!a.project.workspaceId)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}return{text:ka,isSetup:!1}},[e,s,le]),Ze=Er(R),sn=Ze.length>0&&!M,co=p!==null?Fr(p,8,Me):null,$t=oe?oe.paths.filter(a=>!oe.query||a.toLowerCase().includes(oe.query.toLowerCase())).slice(0,8):[],bt=ne?ne.sessions.filter(a=>{let l=ne.query.toLowerCase();return!l||a.name.toLowerCase().includes(l)||a.firstPrompt.toLowerCase().includes(l)}).slice(0,8):[],he=G((a,l)=>{te.current?.close(),te.current=zr({cwd:e,resume:a,profileName:l??s,allowProdWrites:gt.current}),a||(Te.current={uuid:null,firstPrompt:null,titleFired:!1}),yt(0),xt(0)},[e,s]),ln=G((a,l,i)=>{if(wa.test(a))try{let k=me(e,s),m=typeof l.project_id=="string"&&l.project_id||k.project.projectId,g=Vt(l,i),w=[],A=Yt(i);a.endsWith("publish_article")&&A&&w.push(`Live: ${A}`),g&&m&&w.push(`Preview: ${Xt(k.connection.portalUrl,m,g,k.project.languageCode??"en")}`),w.length>0&&d({kind:"link",lines:w})}catch{}},[e,s,d]),be=G(async(a,l)=>{no(!0),ce(null),K([]);let i=++dt.current;d({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let k=Te.current;k.firstPrompt||(k.firstPrompt=l?.display??a),Je.current=Date.now(),pt(0),q(!0),ft.current.clear();let m="",g="",w=null,A=()=>{w||(w=setTimeout(()=>{w=null,h(g.length>0?g:null)},60))},I=()=>{w&&clearTimeout(w),w=null,h(null)},N=()=>{if(g.trim()){let S=g.trimEnd();d({kind:"assistant",text:S})}g="",I()};try{for await(let S of te.current.send(a))if(S.type==="session"){if(!k.uuid){k.uuid=S.sessionId;let b=new Date().toISOString();Ql({uuid:S.sessionId,name:Jl(k.firstPrompt??"session"),renamed:!1,titled:!1,cwd:e,firstPrompt:k.firstPrompt??"",createdAt:b,updatedAt:b})}}else if(S.type==="text"){g+=S.delta,m+=S.delta;let b=Br(g);if(b>0){let D=g.slice(0,b).trimEnd();D&&d({kind:"assistant",text:D}),g=g.slice(b)}A(),pt(D=>D+S.delta.length)}else if(S.type==="tool"){let b=zt(S.name,S.input);b&&(N(),d({kind:"tool",title:b.title,sep:b.sep,arg:b.arg}),ft.current.set(S.id,{name:S.name,input:S.input}))}else if(S.type==="article_diff"){let b=_e(S.oldContent,S.newContent,$e());b&&(N(),d({kind:"diff",added:b.added,removed:b.removed,lines:b.lines,hidden:b.hidden}))}else if(S.type==="tool_result"){S.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(S.output)&&(kt.current=!0);let b=ft.current.get(S.id);if(b){ft.current.delete(S.id),N();let D=Gt(S.output,4,S.isError?void 0:b.name,b.input);d({kind:"tool-result",lines:D.lines,hidden:D.hidden,isError:S.isError}),S.isError||ln(b.name,b.input,S.output)}}else if(S.type==="result"){N(),yt(D=>D+S.outputTokens),xt(D=>D+S.costUsd),d({kind:"done",seconds:Math.round((Date.now()-Je.current)/1e3),tokens:S.outputTokens,costUsd:S.costUsd,ok:S.ok});let b=S.ok?Ir(m,Kt.map(D=>D.name)):[];if(b.length>0?K(b):S.ok&&m.trim()&&oa(a,m,e).then(D=>{D&&dt.current===i&&ce(D)}).catch(()=>{}),k.uuid&&(zo(k.uuid),!k.titleFired)){k.titleFired=!0;let D=k.uuid,_=k.firstPrompt;_&&Gr(_,e).then(F=>F&&Kl(D,F)).catch(()=>{})}}else S.type==="error"&&(N(),S.kind==="auth"&&(kt.current=!0),d({kind:"note",text:`agent error: ${S.message}`,tone:"error"}))}finally{q(!1),I(),kt.current&&(kt.current=!1,wt.current=l?.display??a,K(["/login"])),ht.current&&(ht.current=!1,Ee.current.length>0&&(d({kind:"note",tone:"info",text:`(${Ee.current.length} queued message(s) discarded)`}),Ee.current=[],io([])),d({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,d,ln,$e]),uo=G(a=>{let l=Le(a);if(!l||Se(l)&&!l.refreshToken)return null;let i={...Ye(l.idToken)??{},...Ye(l.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),po=G((a,l)=>{if(l){let i=ie(e);i&&(i.defaultProfile=a,na(i,e))}c(a),he(void 0,a),d({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${a}"${l?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),uo(a)||(d({kind:"note",tone:"warn",text:`Profile "${a}" is not signed in \u2014 do you want to log in now? (press 1)`}),K(["/login"])),we(i=>i+1)},[e,d,he,uo]),an=G((a,l,i)=>{Tt(e,a,l,i.id),d({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),he(),Xr(e,a)||(d({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),ce("analyze this repo and propose a docs structure"))},[e,d,he]),vt=G(()=>{let a=ye.current[0];if(!a)return;d({kind:"note",tone:"info",text:`\u25CF ${a.title} (${a.path})`});for(let i of a.notes)d({kind:"note",tone:"warn",text:`\u26A0 ${i}`});a.overwritesLocalChanges&&d({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let l=_e(a.oldContent,a.newContent,$e());d(l?{kind:"diff",added:l.added,removed:l.removed,lines:l.lines,hidden:l.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),d({kind:"note",tone:"info",text:`Write ${a.path}? (y/n \u2014 anything else cancels)`})},[d,$e]),cn=G(a=>{if(ye.current.length===0)return!1;let l=a.trim().toLowerCase();if(l==="y"||l==="yes"){let i=ye.current.shift();try{ra({cwd:e,profileName:s},i),d({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),ye.current.length===0&&K(k=>k.length>0?k:["/sync"])}catch(k){d({kind:"note",tone:"error",text:`Pull failed: ${k.message}`})}}else if(l==="n"||l==="no"){let i=ye.current.shift();d({kind:"note",tone:"info",text:`Skipped ${i.path}.`})}else{let i=ye.current.length;return ye.current=[],d({kind:"note",tone:"info",text:`Pull cancelled (${i} article(s) left untouched).`}),!0}return vt(),!0},[e,s,vt,d]),un=G(async a=>{let l=a.slice(1).trim().split(/\s+/),i=(l[0]??"").toLowerCase(),k=l.slice(1);switch(no(!0),i){case"help":d({kind:"note",tone:"info",text:Kt.map(m=>` ${m.usage.padEnd(22)} ${m.desc}`).join(`
72
- `)});return;case"exit":case"quit":te.current?.close(),r();return;case"clear":he(),ro.current=[],no(!1),ce(null),dt.current++,d({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let m;try{m=me(e,s)}catch(g){d({kind:"note",tone:"error",text:g.message});return}d({kind:"note",tone:"info",text:`Profile "${m.name}" \u2192 ${m.connection.name} (${m.connection.apiUrl})${m.production?" \u26A0 PRODUCTION":""}`}),ee(!0);try{let g=await ql(m.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},A=>d({kind:"note",tone:"info",text:A})),w=zl(m.name,g);Gl(w),go(w,m.name,A=>d({kind:"note",tone:"info",text:A})),d({kind:"note",tone:"ok",text:`\u2713 Logged in to "${m.name}" as ${jt(w)}`}),m.project.workspaceId||(d({kind:"note",tone:"info",text:"Next: pick the workspace your articles publish to."}),K(["/workspace"])),wt.current&&(ce(wt.current),wt.current=null,d({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(g){d({kind:"note",tone:"error",text:`Login failed: ${g.message}`})}finally{ee(!1),we(g=>g+1)}return}case"allow-prod":{let m=!1;try{m=me(e,s).production}catch{}if(!m){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}gt.current=!0,he(),d({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let m=k.join(" ").trim(),g=Te.current.uuid;if(!g){d({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!m){d({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let w=Te.current.firstPrompt??"";Gr(w,e).then(A=>{A?(ce(`/rename ${A}`),d({kind:"note",tone:"info",text:`Suggestion: "${A}" \u2014 press tab to accept, or type /rename <your name>.`})):d({kind:"note",tone:"info",text:"Usage: /rename <name>"})}).catch(()=>d({kind:"note",tone:"info",text:"Usage: /rename <name>"}));return}ta(g,m),d({kind:"note",tone:"ok",text:`Session renamed to "${m}".`});return}case"profile":{let m=k[0],g=ie(e);if(!m){let w=Object.entries(g?.profiles??{});if(w.length===0){d({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let A=w.map(([S,b])=>({name:S,env:b.connection?.environment??"custom",prod:b.production===!0,who:uo(S)})),I=s??g?.defaultProfile,N=Math.max(0,A.findIndex(S=>S.name===I));We({cursor:N,current:N,rows:A});return}if(m==="add"){let w=No(e,k[1],k[2]);if(w){d({kind:"note",tone:"error",text:w});return}d({kind:"note",tone:"ok",text:`\u2713 Profile "${k[1]}" created (environment: ${k[2]??k[1]}).`}),po(k[1],!1);return}if(!g?.profiles?.[m]){d({kind:"note",tone:"error",text:`Unknown profile "${m}". Create it: /profile add ${m} <environment>`});return}po(m,!0);return}case"doctor":await ut(k,{cwd:e});return;case"mcp":await Mt(k);return;case"init":{if(ie(e)){d({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let m=la().map(g=>({name:g,apiUrl:aa(g).apiUrl}));Fe({cursor:0,rows:m});return}case"resume":{let m=k.join(" ").trim(),g=ea(e).filter(A=>A.uuid!==Te.current.uuid);if(!m){if(!g.length){d({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Ie({query:"",cursor:0,sessions:g});return}let w=Zl(e,m);if(!w){d({kind:"note",tone:"error",text:`No session matches "${m}".`});return}he(w.uuid),Te.current={uuid:w.uuid,firstPrompt:w.firstPrompt,titleFired:!0},zo(w.uuid),d({kind:"note",tone:"ok",text:`Resumed "${w.name}".`});return}case"workspace":{let m=k.join(" ").trim(),g;try{g=await St(e,s)}catch(A){d({kind:"note",tone:"error",text:`Could not list workspaces: ${A.message}`});return}if(!m){let A=g.workspaces.map(N=>({id:N.id,name:N.name??N.id,type:N.workspace_type}));if(A.length===0){d({kind:"note",tone:"info",text:"No workspaces in this project."});return}let I=Math.max(0,A.findIndex(N=>N.id===g.current));Be({cursor:I,current:I,rows:A,profile:g.profile,projectId:g.projectId,environment:g.environment});return}let w=fo(g.workspaces,m);if(!w){d({kind:"note",tone:"error",text:`No workspace matches "${m}".`});return}an(g.profile,g.projectId,w);return}case"publish":{if(k[0]&&k[0]!=="--all"){await be(Nt(k[0]),{display:`/publish ${k[0]}`,echoDisplay:!0});return}let m=k[0]==="--all";d({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let g=await Go({cwd:e,profileName:s}),w=_t(g.entries);if(w.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let I=g.counts["unknown-base"]??0;I>0&&d({kind:"note",tone:"info",text:`(${I} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(m){await be(it(w.map(I=>I.path)),{display:"/publish --all",echoDisplay:!0});return}let A=w.length>1?[{path:"--all",label:`publish all ${w.length} candidates in one run`},...w]:w;He({cursor:0,rows:A})}catch(g){d({kind:"note",tone:"error",text:`Could not compute sync status: ${g.message}`}),d({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let m=k.join(" ").trim();if(!m){let w=[];try{w=Object.keys(Xr(e,me(e,s).name)?.articles??{})}catch{}if(w.length===0){d({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}Ae({query:"",cursor:0,paths:w});return}let g=Hl(m)?m:to(e,m);if(Fl(g)){try{d({kind:"preview",name:qr(g),text:Jr(Hr(g,"utf8"))})}catch(w){d({kind:"note",tone:"error",text:`Could not read ${g}: ${w.message}`})}return}if(ya.test(m)){try{let w=me(e,s),A={profile:w.name,connection:w.connection},I=w.project.projectId??Vl(A),N=await Yl(A,I,m);d({kind:"preview",name:N.title??m,text:N.content??"*(article has no content)*"})}catch(w){d({kind:"note",tone:"error",text:`Could not fetch article: ${w.message}`})}return}d({kind:"note",tone:"error",text:`"${m}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let m=k[0]?.trim();if(!m){let I=Ft(Vo(e));je({cursor:I,current:I});return}let{lines:g,changed:w,effective:A}=ct(e,m);for(let I of g)d({kind:"note",tone:I.startsWith("\u26A0")?"warn":I.startsWith("\u2713")?"ok":"info",text:I});w&&(on(I=>I+1),te.current?.setModel(A));return}case"convert":{if(!ie(e)){d({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}let{scope:m,run:g}=Ao(k),w=pa(e,s);if(w.length===0){d({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let A=Eo(w,m);if(A.length===0){d({kind:"note",tone:"error",text:`No tracked articles under "${m}". (${w.length} are tracked overall.)`});return}let I=ca(A),N=3,S=`/convert${m?` --scope ${m}`:""} --run`,b=Yr(e,"light");if(!g){let _=ma({files:Do(e,A),op:"convert",model:b.model}),F=m?`Scope: ${m} (${A.length} of ${w.length} tracked)
71
+ Press 1 to start.`}),K(["/init"]);return}try{let a=pe(e,s),l=Le(a.name);l&&Se(l)&&l.refreshToken?Yl({profile:a.name,connection:a.connection}).then(()=>{u({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),ye(i=>i+1)}).catch(()=>{u({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),K(["/login"])}):(!l||Se(l))&&(u({kind:"note",tone:"warn",text:`Profile "${a.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),K(["/login"]))}catch{}try{let a=se(e),l=pe(e,s),i=Le(l.name);a&&i&&!Se(i)&&ba(e,a.authoritativeSourceFiles)&&(u({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),K(["/scope"]))}catch{}},[]),Ve(()=>{let a=null,l=null,i=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let k=`${ue.columns??80}x${ue.rows??24}`;k!==rn.current&&(rn.current=k,as(m=>m+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+nr(so.current,$e())+"\x1B[?2026l")},80))},400)};return ue.on("resize",i),()=>{a&&clearTimeout(a),l&&clearTimeout(l),ue.off("resize",i)}},[ue,$e]);let _e=Math.max(20,(ue.columns??80)-1),sn=eo(()=>ya(e,s),[e,s,ie]),co=eo(()=>{if(!se(e))return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};try{let a=pe(e,s),l=Le(a.name);if(!(!!l&&!(Se(l)&&!l.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${a.name}")\u2026`,isSetup:!0};if(!a.project.workspaceId)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}return{text:wa,isSetup:!1}},[e,s,ie]),Ze=Rn(R),ln=Ze.length>0&&!_,uo=d!==null?Br(d,8,_e):null,bt=oe?oe.paths.filter(a=>!oe.query||a.toLowerCase().includes(oe.query.toLowerCase())).slice(0,8):[],vt=ne?ne.sessions.filter(a=>{let l=ne.query.toLowerCase();return!l||a.name.toLowerCase().includes(l)||a.firstPrompt.toLowerCase().includes(l)}).slice(0,8):[],ge=G((a,l)=>{te.current?.close(),te.current=Gr({cwd:e,resume:a,profileName:l??s,allowProdWrites:ht.current}),a||(Te.current={uuid:null,firstPrompt:null,titleFired:!1}),xt(0),$t(0)},[e,s]),an=G((a,l,i)=>{if(xa.test(a))try{let k=pe(e,s),m=typeof l.project_id=="string"&&l.project_id||k.project.projectId,g=Kt(l,i),y=[],A=Jt(i);a.endsWith("publish_article")&&A&&y.push(`Live: ${A}`),g&&m&&y.push(`Preview: ${Yt(k.connection.portalUrl,m,g,k.project.languageCode??"en")}`),y.length>0&&u({kind:"link",lines:y})}catch{}},[e,s,u]),be=G(async(a,l)=>{ro(!0),ae(null),K([]);let i=++pt.current;u({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let k=Te.current;k.firstPrompt||(k.firstPrompt=l?.display??a),Je.current=Date.now(),mt(0),q(!0),gt.current.clear();let m="",g="",y=null,A=()=>{y||(y=setTimeout(()=>{y=null,h(g.length>0?g:null)},60))},I=()=>{y&&clearTimeout(y),y=null,h(null)},N=()=>{if(g.trim()){let S=g.trimEnd();u({kind:"assistant",text:S})}g="",I()};try{for await(let S of te.current.send(a))if(S.type==="session"){if(!k.uuid){k.uuid=S.sessionId;let $=new Date().toISOString();ea({uuid:S.sessionId,name:Zl(k.firstPrompt??"session"),renamed:!1,titled:!1,cwd:e,firstPrompt:k.firstPrompt??"",createdAt:$,updatedAt:$})}}else if(S.type==="text"){g+=S.delta,m+=S.delta;let $=Hr(g);if($>0){let D=g.slice(0,$).trimEnd();D&&u({kind:"assistant",text:D}),g=g.slice($)}A(),mt(D=>D+S.delta.length)}else if(S.type==="tool"){let $=Xt(S.name,S.input);$&&(N(),u({kind:"tool",title:$.title,sep:$.sep,arg:$.arg}),gt.current.set(S.id,{name:S.name,input:S.input}))}else if(S.type==="article_diff"){let $=Me(S.oldContent,S.newContent,$e());$&&(N(),u({kind:"diff",added:$.added,removed:$.removed,lines:$.lines,hidden:$.hidden}))}else if(S.type==="tool_result"){S.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(S.output)&&(yt.current=!0);let $=gt.current.get(S.id);if($){gt.current.delete(S.id),N();let D=Vt(S.output,4,S.isError?void 0:$.name,$.input);u({kind:"tool-result",lines:D.lines,hidden:D.hidden,isError:S.isError}),S.isError||an($.name,$.input,S.output)}}else if(S.type==="result"){N(),xt(D=>D+S.outputTokens),$t(D=>D+S.costUsd),u({kind:"done",seconds:Math.round((Date.now()-Je.current)/1e3),tokens:S.outputTokens,costUsd:S.costUsd,ok:S.ok});let $=S.ok?_r(m,rt.map(D=>D.name)):[];if($.length>0?K($):S.ok&&m.trim()&&ra(a,m,e).then(D=>{D&&pt.current===i&&ae(D)}).catch(()=>{}),k.uuid&&(Go(k.uuid),!k.titleFired)){k.titleFired=!0;let D=k.uuid,M=k.firstPrompt;M&&Xr(M,e).then(F=>F&&Ql(D,F)).catch(()=>{})}}else S.type==="error"&&(N(),S.kind==="auth"&&(yt.current=!0),u({kind:"note",text:`agent error: ${S.message}`,tone:"error"}))}finally{q(!1),I(),yt.current&&(yt.current=!1,wt.current=l?.display??a,K(["/login"])),kt.current&&(kt.current=!1,Ee.current.length>0&&(u({kind:"note",tone:"info",text:`(${Ee.current.length} queued message(s) discarded)`}),Ee.current=[],lo([])),u({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,u,an,$e]),po=G(a=>{let l=Le(a);if(!l||Se(l)&&!l.refreshToken)return null;let i={...Ye(l.idToken)??{},...Ye(l.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),mo=G((a,l)=>{if(l){let i=se(e);i&&(i.defaultProfile=a,sa(i,e))}c(a),ge(void 0,a),u({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${a}"${l?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),po(a)||(u({kind:"note",tone:"warn",text:`Profile "${a}" is not signed in \u2014 do you want to log in now? (press 1)`}),K(["/login"])),ye(i=>i+1)},[e,u,ge,po]),cn=G((a,l,i)=>{Rt(e,a,l,i.id),u({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),ge(),Vr(e,a)||(u({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),ae("analyze this repo and propose a docs structure"))},[e,u,ge]),Ct=G(()=>{let a=we.current[0];if(!a)return;u({kind:"note",tone:"info",text:`\u25CF ${a.title} (${a.path})`});for(let i of a.notes)u({kind:"note",tone:"warn",text:`\u26A0 ${i}`});a.overwritesLocalChanges&&u({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let l=Me(a.oldContent,a.newContent,$e());u(l?{kind:"diff",added:l.added,removed:l.removed,lines:l.lines,hidden:l.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),u({kind:"note",tone:"info",text:`Write ${a.path}? (y/n \u2014 anything else cancels)`})},[u,$e]),un=G(a=>{if(we.current.length===0)return!1;let l=a.trim().toLowerCase();if(l==="y"||l==="yes"){let i=we.current.shift();try{ia({cwd:e,profileName:s},i),u({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),we.current.length===0&&K(k=>k.length>0?k:["/sync"])}catch(k){u({kind:"note",tone:"error",text:`Pull failed: ${k.message}`})}}else if(l==="n"||l==="no"){let i=we.current.shift();u({kind:"note",tone:"info",text:`Skipped ${i.path}.`})}else{let i=we.current.length;return we.current=[],u({kind:"note",tone:"info",text:`Pull cancelled (${i} article(s) left untouched).`}),!0}return Ct(),!0},[e,s,Ct,u]),dn=G(async a=>{let l=a.slice(1).trim().split(/\s+/),i=(l[0]??"").toLowerCase(),k=l.slice(1);switch(ro(!0),i){case"help":u({kind:"note",tone:"info",text:Mt().join(`
72
+ `)});return;case"exit":case"quit":te.current?.close(),r();return;case"clear":ge(),so.current=[],ro(!1),ae(null),pt.current++,u({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let m;try{m=pe(e,s)}catch(g){u({kind:"note",tone:"error",text:g.message});return}u({kind:"note",tone:"info",text:`Profile "${m.name}" \u2192 ${m.connection.name} (${m.connection.apiUrl})${m.production?" \u26A0 PRODUCTION":""}`}),ee(!0);try{let g=await Gl(m.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},A=>u({kind:"note",tone:"info",text:A})),y=Xl(m.name,g);Vl(y),ho(y,m.name,A=>u({kind:"note",tone:"info",text:A})),u({kind:"note",tone:"ok",text:`\u2713 Logged in to "${m.name}" as ${At(y)}`}),m.project.workspaceId||(u({kind:"note",tone:"info",text:"Next: pick the workspace your articles publish to."}),K(["/workspace"])),wt.current&&(ae(wt.current),wt.current=null,u({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(g){u({kind:"note",tone:"error",text:`Login failed: ${g.message}`})}finally{ee(!1),ye(g=>g+1)}return}case"allow-prod":{let m=!1;try{m=pe(e,s).production}catch{}if(!m){u({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}ht.current=!0,ge(),u({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let m=k.join(" ").trim(),g=Te.current.uuid;if(!g){u({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!m){u({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let y=Te.current.firstPrompt??"";Xr(y,e).then(A=>{A?(ae(`/rename ${A}`),u({kind:"note",tone:"info",text:`Suggestion: "${A}" \u2014 press tab to accept, or type /rename <your name>.`})):u({kind:"note",tone:"info",text:"Usage: /rename <name>"})}).catch(()=>u({kind:"note",tone:"info",text:"Usage: /rename <name>"}));return}na(g,m),u({kind:"note",tone:"ok",text:`Session renamed to "${m}".`});return}case"profile":{let m=k[0],g=se(e);if(!m){let y=Object.entries(g?.profiles??{});if(y.length===0){u({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let A=y.map(([S,$])=>({name:S,env:$.connection?.environment??"custom",prod:$.production===!0,who:po(S)})),I=s??g?.defaultProfile,N=Math.max(0,A.findIndex(S=>S.name===I));We({cursor:N,current:N,rows:A});return}if(m==="add"){let y=Lo(e,k[1],k[2]);if(y){u({kind:"note",tone:"error",text:y});return}u({kind:"note",tone:"ok",text:`\u2713 Profile "${k[1]}" created (environment: ${k[2]??k[1]}).`}),mo(k[1],!1);return}if(!g?.profiles?.[m]){u({kind:"note",tone:"error",text:`Unknown profile "${m}". Create it: /profile add ${m} <environment>`});return}mo(m,!0);return}case"doctor":await dt(k,{cwd:e});return;case"mcp":await Nt(k);return;case"init":{if(se(e)){u({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let m=ca().map(g=>({name:g,apiUrl:ua(g).apiUrl}));Fe({cursor:0,rows:m});return}case"resume":{let m=k.join(" ").trim(),g=oa(e).filter(A=>A.uuid!==Te.current.uuid);if(!m){if(!g.length){u({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Ie({query:"",cursor:0,sessions:g});return}let y=ta(e,m);if(!y){u({kind:"note",tone:"error",text:`No session matches "${m}".`});return}ge(y.uuid),Te.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},Go(y.uuid),u({kind:"note",tone:"ok",text:`Resumed "${y.name}".`});return}case"workspace":{let m=k.join(" ").trim(),g;try{g=await Tt(e,s)}catch(A){u({kind:"note",tone:"error",text:`Could not list workspaces: ${A.message}`});return}if(!m){let A=g.workspaces.map(N=>({id:N.id,name:N.name??N.id,type:N.workspace_type}));if(A.length===0){u({kind:"note",tone:"info",text:"No workspaces in this project."});return}let I=Math.max(0,A.findIndex(N=>N.id===g.current));Be({cursor:I,current:I,rows:A,profile:g.profile,projectId:g.projectId,environment:g.environment});return}let y=go(g.workspaces,m);if(!y){u({kind:"note",tone:"error",text:`No workspace matches "${m}".`});return}cn(g.profile,g.projectId,y);return}case"publish":{if(k[0]&&k[0]!=="--all"){await be(Ot(k[0]),{display:`/publish ${k[0]}`,echoDisplay:!0});return}let m=k[0]==="--all";u({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let g=await Xo({cwd:e,profileName:s}),y=Lt(g.entries);if(y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let I=g.counts["unknown-base"]??0;I>0&&u({kind:"note",tone:"info",text:`(${I} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(m){await be(lt(y.map(I=>I.path)),{display:"/publish --all",echoDisplay:!0});return}let A=y.length>1?[{path:"--all",label:`publish all ${y.length} candidates in one run`},...y]:y;He({cursor:0,rows:A})}catch(g){u({kind:"note",tone:"error",text:`Could not compute sync status: ${g.message}`}),u({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let m=k.join(" ").trim();if(!m){let y=[];try{y=Object.keys(Vr(e,pe(e,s).name)?.articles??{})}catch{}if(y.length===0){u({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}Ae({query:"",cursor:0,paths:y});return}let g=zl(m)?m:oo(e,m);if(Hl(g)){try{u({kind:"preview",name:zr(g),text:Qr(qr(g,"utf8"))})}catch(y){u({kind:"note",tone:"error",text:`Could not read ${g}: ${y.message}`})}return}if($a.test(m)){try{let y=pe(e,s),A={profile:y.name,connection:y.connection},I=y.project.projectId??Kl(A),N=await Jl(A,I,m);u({kind:"preview",name:N.title??m,text:N.content??"*(article has no content)*"})}catch(y){u({kind:"note",tone:"error",text:`Could not fetch article: ${y.message}`})}return}u({kind:"note",tone:"error",text:`"${m}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let m=k[0]?.trim();if(!m){let I=Ht(Yo(e));je({cursor:I,current:I});return}let{lines:g,changed:y,effective:A}=ut(e,m);for(let I of g)u({kind:"note",tone:I.startsWith("\u26A0")?"warn":I.startsWith("\u2713")?"ok":"info",text:I});y&&(nn(I=>I+1),te.current?.setModel(A));return}case"convert":{if(!se(e)){u({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}let{scope:m,run:g}=Eo(k),y=fa(e,s);if(y.length===0){u({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let A=Do(y,m);if(A.length===0){u({kind:"note",tone:"error",text:`No tracked articles under "${m}". (${y.length} are tracked overall.)`});return}let I=da(A),N=3,S=`/convert${m?` --scope ${m}`:""} --run`,$=Kr(e,"light");if(!g){let M=ga({files:Io(e,A),op:"convert",model:$.model}),F=m?`Scope: ${m} (${A.length} of ${y.length} tracked)
73
73
  `:"",re=`
74
- Model: ${b.model}${b.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;d({kind:"note",tone:"info",text:F+Mo(I,_,N).join(`
75
- `)+re}),K([S]);return}Je.current=Date.now(),pt(0),q(!0);let D=new AbortController;De.current=D,ue({total:I.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Converting ${A.length} articles across ${I.length} partitions (\u2264${N} agents at once) on ${b.model}\u2026 (esc to stop)`});try{for await(let _ of Vr({cwd:e,partitions:I,promptFor:Io,concurrency:N,profileName:s,allowProdWrites:gt.current,model:b.model,signal:D.signal}))if(_.type==="partition_status")_.status==="running"?(ue(F=>F&&{...F,active:[...F.active,_.label],lastAt:Date.now()}),d({kind:"note",tone:"info",text:` \u25B8 ${_.label} \u2014 converting\u2026`})):(ue(F=>F&&{...F,active:F.active.filter(re=>re!==_.label),done:F.done+1,lastAt:Date.now()}),d({kind:"note",tone:_.status==="done"?"ok":"error",text:` ${_.status==="done"?"\u2713":"\u2717"} ${_.label}`}));else if(_.type==="partition_event")ue(F=>{if(!F)return F;let re={...F,lastAt:Date.now()};return _.event.type==="tool"?re.tools=F.tools+1:_.event.type==="text"&&(re.chars=F.chars+_.event.delta.length),re});else if(_.type==="run_done"){xt(tt=>tt+_.totalCostUsd),yt(tt=>tt+_.results.reduce((us,ds)=>us+ds.outputTokens,0));let F=_.aborted?"Stopped. ":"",re=t.kind==="api"?"api":"subscription";d({kind:"note",tone:_.aborted?"warn":_.ok?"ok":"warn",text:F+_o(_.results,I,re).join(`
76
- `)})}}catch(_){d({kind:"note",tone:"error",text:`Convert run failed: ${_.message}`})}finally{De.current=null,ue(null),q(!1)}return}case"sync":{let m=(k[0]??"status").toLowerCase();try{if(m==="status"){d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let g=await Go({cwd:e,profileName:s});d({kind:"note",tone:"info",text:Ut(g).join(`
77
- `)});return}if(m==="pull"){let g=k[1];if(!g){d({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let w;if(g==="--all"){if(d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),w=(await Go({cwd:e,profileName:s})).entries.filter(N=>N.status==="remote-ahead"&&N.path).map(N=>N.path),w.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is remote-ahead \u2014 no pulls needed. (Conflicts are never bulk-pulled; pull them one by one.)"});return}}else w=[g.replace(/\\/g,"/")];let A=[];for(let I of w)A.push(await sa({cwd:e,profileName:s,relPath:I}));ye.current=A,vt();return}d({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${m} \u2014 use /sync or /sync pull <path>|--all.`})}catch(g){d({kind:"note",tone:"error",text:`Sync failed: ${g.message}`})}return}case"scope":{if(!ie(e)){d({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}let m=ia(e);if(m.length===0){d({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}qe({cursor:0,rows:m.map(g=>({...g,checked:g.recommended}))});return}case"audit":case"capture-setup":{let g=await(i==="audit"?Lt:qt)(k,void 0);g.kind==="forward-to-agent"&&g.prompt&&await be(g.prompt,{display:g.display,echoDisplay:!0});return}case"screenshot":{let m=Uo(k);if(m.mode==="list"){if(!ie(e)){d({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}d({kind:"note",tone:"info",text:Fo(Wo(e,m.scope),m.scope).join(`
78
- `)});return}if(m.mode==="single"){let b=await Ht(k);b.kind==="forward-to-agent"&&b.prompt&&await be(b.prompt,{display:b.display,echoDisplay:!0});return}if(!ie(e)){d({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}let g=m.mode==="scope"?m.scope:void 0,w=da(e,{scope:g}).map(b=>b.id);if(w.length===0){d({kind:"note",tone:"info",text:g?`No screenshot placeholders under ${g}.`:"No screenshot placeholders found in the docs."});return}let A=6,I=ua(w,3),N=Yr(e,"standard");Je.current=Date.now(),pt(0),q(!0);let S=new AbortController;De.current=S,ue({verb:"Authoring",total:I.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Authoring ${w.length} screenshot spec${w.length===1?"":"s"} across ${I.length} partition${I.length===1?"":"s"} (\u2264${A} agents at once) on ${N.model}${N.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let b of Vr({cwd:e,partitions:I,promptFor:xr,concurrency:A,profileName:s,allowProdWrites:gt.current,model:N.model,signal:S.signal}))if(b.type==="partition_status")b.status==="running"?(ue(D=>D&&{...D,active:[...D.active,b.label],lastAt:Date.now()}),d({kind:"note",tone:"info",text:` \u25B8 ${b.label} \u2014 authoring\u2026`})):(ue(D=>D&&{...D,active:D.active.filter(_=>_!==b.label),done:D.done+1,lastAt:Date.now()}),d({kind:"note",tone:b.status==="done"?"ok":"error",text:` ${b.status==="done"?"\u2713":"\u2717"} ${b.label}`}));else if(b.type==="partition_event")ue(D=>{if(!D)return D;let _={...D,lastAt:Date.now()};return b.event.type==="tool"?_.tools=D.tools+1:b.event.type==="text"&&(_.chars=D.chars+b.event.delta.length),_});else if(b.type==="run_done"){xt(F=>F+b.totalCostUsd),yt(F=>F+b.results.reduce((re,tt)=>re+tt.outputTokens,0));let D=b.results.filter(F=>F.ok).length,_=b.aborted?"Stopped. ":"";d({kind:"note",tone:b.aborted?"warn":b.ok?"ok":"warn",text:`${_}Authored specs \u2014 ${D}/${b.results.length} batches ok. Next: /capture-setup, then d360-capture capture.`})}}catch(b){d({kind:"note",tone:"error",text:`Spec authoring failed: ${b.message}`})}finally{De.current=null,ue(null),q(!1)}return}default:d({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,vt,d,he,be]),Ct=G(a=>{let l=(a??R).trim();if($(""),mt(null),Oe(0),l.startsWith("/")){let k=g=>g.trim().split(/\s+/).slice(1).join(" "),m=k(l);K(g=>g.filter(w=>w.trim()!==l&&!(m&&k(w)===m)))}if(!l||cn(l))return;so.current.push(l);let i=Lr(l,Jo.current);l.startsWith("/")?un(i):be(i,{display:l})},[R,cn,un,be]),dn=G(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=Mr(a);if(_r(l)){let i=Nr(++ss.current,l);Jo.current.set(i,l),j(i)}else j(l);return}j(a)},[j]),ze=Math.max(10,Me-6),pn=Zt(()=>Jt(f.text,ze),[f.text,ze]),mo=G(a=>x(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),Pt=G(a=>x(l=>({...l,pos:Ur(Jt(l.text,ze),l.pos,a)})),[ze]),et=G(a=>x(l=>({...l,pos:Wr(Jt(l.text,ze),l.pos,a)})),[ze]),as=["\x1B[H","\x1B[1~","\x1BOH"],cs=["\x1B[F","\x1B[4~","\x1BOF"],mn=G((a,l)=>l.leftArrow?(mo(-1),!0):l.rightArrow?(mo(1),!0):a&&as.includes(a)?(et("start"),!0):a&&cs.includes(a)?(et("end"),!0):l.ctrl&&a==="a"?(et("start"),!0):l.ctrl&&a==="e"?(et("end"),!0):!1,[mo,et]);return Ul((a,l)=>{if(l.ctrl&&a==="c"){te.current?.close(),r();return}if(!z){if(M){if(l.escape){if(De.current){De.current.signal.aborted||(d({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),De.current.abort());return}ht.current||(ht.current=!0,d({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),te.current?.interrupt());return}if(l.return){let i=R.trim();if(!i)return;Ee.current.push(i),io([...Ee.current]),$("");return}if(mn(a,l))return;if(l.upArrow){Pt(-1);return}if(l.downArrow){Pt(1);return}if(l.backspace||l.delete){T();return}a&&!l.ctrl&&!l.meta&&dn(a);return}if(Re){if(l.upArrow){je(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){je(i=>i&&{...i,cursor:Math.min(pe.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=pe.length){je(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=pe[Re.cursor];je(null);let{lines:k,changed:m,effective:g}=ct(e,i.value??"default");for(let w of k)d({kind:"note",tone:w.startsWith("\u26A0")?"warn":w.startsWith("\u2713")?"ok":"info",text:w});m&&(on(w=>w+1),te.current?.setModel(g));return}if(a==="s"){let i=pe[Re.cursor];je(null),te.current?.setModel(i.value??void 0),d({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(l.escape){je(null);return}return}if(fe){if(l.upArrow){We(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){We(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=fe.rows.length){We(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=fe.rows[fe.cursor];We(null),po(i.name,l.return===!0);return}if(l.escape){We(null);return}return}if(xe){if(l.upArrow){Fe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Fe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=xe.rows.length){Fe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=xe.rows[xe.cursor].name;if(Fe(null),!En(e,i).created){d({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}d({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let m=!1;try{let g=Le(i);m=!!g&&!(Se(g)&&!g.refreshToken)}catch{}m?(d({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),K(["/workspace"])):(d({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),K(["/login"])),we(g=>g+1);return}if(l.escape){Fe(null);return}return}if(J){if(l.upArrow){Be(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Be(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=J.rows.length){Be(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=J.rows[J.cursor],{profile:k,projectId:m}=J;Be(null),an(k,m,i);return}if(l.escape){Be(null);return}return}if(Q){if(l.upArrow){qe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){qe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){qe(i=>i&&{...i,rows:i.rows.map((k,m)=>m===i.cursor?{...k,checked:!k.checked}:k)});return}if(l.return){let i=Q.rows.filter(k=>k.checked).map(k=>k.path);if(qe(null),i.length===0){d({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}xo(e,i),d({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let k of i)d({kind:"note",tone:"info",text:` ${k}`});d({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(l.escape){qe(null);return}return}if(ge){if(l.upArrow){He(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){He(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ge.rows.length){He(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ge.rows[ge.cursor],k=ge.rows.filter(m=>m.path!=="--all").map(m=>m.path);He(null),be(i.path==="--all"?it(k):Nt(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){He(null);return}return}if(oe){if(l.escape){Ae(null);return}if(l.upArrow){Ae(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ae(i=>i&&{...i,cursor:Math.min(Math.max(0,$t.length-1),i.cursor+1)});return}if(l.return){let i=$t[oe.cursor];if(i){Ae(null);try{d({kind:"preview",name:qr(i),text:Jr(Hr(to(e,i),"utf8"))})}catch(k){d({kind:"note",tone:"error",text:`Could not read ${i}: ${k.message}`})}}return}if(l.backspace||l.delete){Ae(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Ae(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(ne){if(l.escape){Ie(null);return}if(l.upArrow){Ie(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ie(i=>i&&{...i,cursor:Math.min(Math.max(0,bt.length-1),i.cursor+1)});return}if(l.return){let i=bt[ne.cursor];i&&(Ie(null),he(i.uuid),Te.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},zo(i.uuid),d({kind:"note",tone:"ok",text:`Resumed "${i.name}".`}));return}if(l.backspace||l.delete){Ie(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Ie(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!R&&ae){$(ae),ce(null),dt.current++;return}if(!R&&Ke.length>0&&a&&/^[1-9]$/.test(a)){let i=Ke[Number(a)-1];if(i){$(i);return}}if(!mn(a,l)){if(sn){if(l.upArrow){Oe(i=>Math.max(0,i-1));return}if(l.downArrow){Oe(i=>Math.min(Ze.length-1,i+1));return}if(l.tab){$("/"+(Ze[Qe]?.name??"")+" "),Oe(0);return}if(l.return){let i=Ze[Qe];if(i){let k=R.trim().slice(1).split(/\s+/).slice(1).join(" ");if(Dr(i.usage)&&!k){$("/"+i.name+" "),Oe(0);return}Ct("/"+i.name+(k?" "+k:""));return}}}else{if(l.upArrow){if(R!==""&&Ue===null){Pt(-1);return}let i=so.current;if(!i.length)return;let k=Ue===null?i.length-1:Math.max(0,Ue-1);mt(k),$(i[k]??"");return}if(l.downArrow){if(R!==""&&Ue===null){Pt(1);return}let i=so.current;if(Ue===null)return;let k=Ue+1;k>=i.length?(mt(null),$("")):(mt(k),$(i[k]??""));return}}if(l.return){Ct();return}if(l.backspace||l.delete){T();return}if(l.escape){$(""),Oe(0),ce(null),K([]);return}a&&!l.ctrl&&!l.meta&&dn(a)}}}),Ve(()=>{if(M||z)return;let a=Ee.current.shift();a!==void 0&&(io([...Ee.current]),Ct(a))},[M,z,Ct]),O(X,{flexDirection:"column",width:Me,children:[co!==null&&O(X,{marginTop:1,flexDirection:"column",children:[co.truncated&&P(C,{dimColor:!0,children:"\u2026"}),P(C,{children:co.text})]}),M&&(Zo?P(va,{p:Zo}):P($a,{startTime:Je.current,chars:rs})),P(X,{borderStyle:"round",borderColor:rn.prod?"yellow":H,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:R?pn.map((a,l)=>{let i=f.text.slice(a.start,a.end),k=l===Qt(pn,f.pos),m=Math.min(f.pos,a.end)-a.start;return O(C,{children:[P(C,{color:H,children:l===0?"> ":" "}),k?O(Xo,{children:[i.slice(0,m),P(eo,{ch:i[m]??" "}),i.slice(m+1)]}):i||" "]},`${l}-${a.start}`)}):O(C,{children:[P(C,{color:H,children:"> "}),ae&&!M?O(Xo,{children:[P(eo,{ch:ae[0],dim:!0}),P(C,{color:"gray",children:ae.slice(1)}),P(C,{dimColor:!0,children:" (tab)"})]}):ao.isSetup||!ns?O(Xo,{children:[P(eo,{ch:ao.text[0],dim:!0}),P(C,{color:"gray",children:ao.text.slice(1)})]}):P(eo,{ch:" "})]})}),Qo.length>0&&P(X,{flexDirection:"column",paddingX:1,children:Qo.map((a,l)=>P(C,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),Re?O(X,{flexDirection:"column",paddingX:1,children:[P(C,{color:H,bold:!0,children:"Select model"}),P(C,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),pe.map((a,l)=>O(C,{color:l===Re.cursor?H:void 0,children:[l===Re.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===Re.current?" \u2714":""}`.padEnd(16),P(C,{color:"gray",children:a.desc})]},a.label)),P(C,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):fe?O(X,{flexDirection:"column",paddingX:1,children:[P(C,{color:H,bold:!0,children:"Switch connection profile"}),fe.rows.map((a,l)=>O(C,{color:l===fe.cursor?H:void 0,children:[l===fe.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===fe.current?" \u2714":""}`.padEnd(20),P(C,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?P(C,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),P(C,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):xe?O(X,{flexDirection:"column",paddingX:1,children:[P(C,{color:H,bold:!0,children:"Pick your Document360 environment"}),xe.rows.map((a,l)=>O(C,{color:l===xe.cursor?H:void 0,children:[l===xe.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),P(C,{color:"gray",children:a.apiUrl})]},a.name)),P(C,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):J?O(X,{flexDirection:"column",paddingX:1,children:[P(C,{color:H,bold:!0,children:"Switch workspace"}),P(C,{dimColor:!0,children:`environment ${J.environment} \xB7 project ${J.projectId.slice(0,8)}\u2026`}),J.rows.map((a,l)=>O(C,{color:l===J.cursor?H:void 0,children:[l===J.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===J.current?" \u2714":""}`.padEnd(30),P(C,{color:"gray",children:a.type??""})]},a.id)),P(C,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):Q?(()=>{let l=Math.min(Math.max(0,Q.cursor-Math.floor(7)),Math.max(0,Q.rows.length-14)),i=Q.rows.slice(l,l+14),k=Q.rows.filter(m=>m.checked).length;return O(X,{flexDirection:"column",paddingX:1,children:[P(C,{color:H,bold:!0,children:`Which folders back the user docs? (${k} selected of ${Q.rows.length})`}),l>0?P(C,{dimColor:!0,children:` \u2191 ${l} more`}):null,i.map((m,g)=>{let w=l+g;return O(C,{color:w===Q.cursor?H:void 0,children:[w===Q.cursor?"\u276F ":" ",m.checked?"\u25C9 ":"\u25CB ",m.path.padEnd(Math.min(48,Me-34)),P(C,{color:"gray",children:Ot(m)})]},m.path)}),l+14<Q.rows.length?P(C,{dimColor:!0,children:` \u2193 ${Q.rows.length-l-14} more`}):null,P(C,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():ge?O(X,{flexDirection:"column",paddingX:1,children:[P(C,{color:H,bold:!0,children:"Publish which article?"}),ge.rows.map((a,l)=>O(C,{color:l===ge.cursor?H:void 0,children:[l===ge.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,Me-30)),P(C,{color:"gray",children:a.label})]},a.path)),P(C,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):oe?O(X,{flexDirection:"column",paddingX:1,children:[P(C,{color:H,bold:!0,children:`Preview article${oe.query?` \u2014 filter: ${oe.query}`:" (type to filter)"}`}),$t.length===0?P(C,{color:"gray",children:"no articles match"}):$t.map((a,l)=>O(C,{color:l===oe.cursor?H:void 0,children:[l===oe.cursor?"\u276F ":" ",a]},a)),P(C,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ne?O(X,{flexDirection:"column",paddingX:1,children:[P(C,{color:H,bold:!0,children:`Resume session${ne.query?` \u2014 filter: ${ne.query}`:" (type to filter)"}`}),bt.length===0?P(C,{color:"gray",children:"no sessions match"}):bt.map((a,l)=>O(C,{color:l===ne.cursor?H:void 0,children:[l===ne.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),P(C,{color:l===ne.cursor?H:"gray",children:a.firstPrompt.slice(0,Math.max(10,Me-40))})]},a.uuid)),P(C,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):sn?P(X,{flexDirection:"column",children:Ze.map((a,l)=>O(C,{color:l===Qe?H:void 0,children:[l===Qe?"\u276F ":" ",a.usage.padEnd(22)," ",P(C,{color:l===Qe?H:"gray",children:a.name==="model"&&lo?`${a.desc} (currently ${lo})`:a.desc})]},a.name))}):!R&&Ke.length>0?O(X,{flexDirection:"column",paddingX:1,children:[Ke.map((a,l)=>O(C,{children:[P(C,{color:H,children:l+1})," ",a.slice(0,Math.max(20,Me-5))]},a)),P(C,{dimColor:!0,children:`press 1-${Ke.length} to fill the command \xB7 esc dismiss`})]}):P(X,{paddingX:1,children:O(C,{color:"gray",children:[rn.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${lo??"model n/a"}${t.kind==="api"?tn>0?` \xB7 ${Ne(tn)}`:"":en>0?` \xB7 ${Wt(en)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}B();import{jsx as Sa}from"react/jsx-runtime";async function es(e=process.cwd(),t="auto",o,n="0.0.0"){let r=Pa(t);r.kind==="none"&&(console.error(""),console.error(v("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${y("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${y("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=Ca(Sa(Zr,{cwd:e,auth:r,profileName:o,version:n}));await s(),process.stdout.write(`
79
- `),process.exit(0)}var Aa=Ra(import.meta.url),ts=Aa("../package.json"),ke=new Ta;function Yo(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}ke.command("login").description("Sign in to Document360 (browser OAuth; project chosen during login)").option("--profile <name>","Connection profile (defaults to the repo's defaultProfile)").option("--env <name>",!1).option("--manual","No local listener \u2014 paste the redirect URL instead (SSH/locked-down setups)").action(async e=>{Yo(e),await At({profile:e.profile,manual:e.manual})});ke.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Yo(e),await vn({profile:e.profile})});ke.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Yo(e),await bn({profile:e.profile})});var Ko=ke.command("profile").description("Manage connection profiles for the current repo");Ko.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Et(process.cwd()));Ko.command("use <name>").description("Set the default profile for this repo").action(e=>Dt(process.cwd(),e));Ko.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>It(process.cwd(),e));var os=ke.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");os.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>Ge(process.cwd(),e.profile));os.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await kn(process.cwd(),e,t.profile)});ke.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Pn());ke.command("doctor").description("Health-check: node, Claude auth, Document360 login, profile/workspace, category map, API reachability").action(async()=>{let{renderDoctorChecks:e,runDoctorChecks:t}=await Promise.resolve().then(()=>(Bt(),fr)),o=await t(process.cwd());for(let n of e(o))console.log(n);process.exitCode=o.some(n=>n.level==="fail")?1:0});ke.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(ts.version,"-v, --version").option("-p, --prompt <text>","One-shot prompt mode (non-interactive)").option("-r, --resume <name>","Resume a saved session (with --prompt; use /resume inside the REPL)").option("-C, --cwd <dir>","Working directory",process.cwd()).option("--auth <mode>","Auth mode: auto | api | subscription","auto").option("--profile <name>","Connection profile (defaults to the repo's defaultProfile)").option("--yes","Skip the production write confirmation (for one-shot/CI)").option("--classic","Use the plain readline REPL instead of the Ink TUI").action(async e=>{if(ja.includes(e.auth)||(console.error(`\u2717 Invalid --auth mode: ${e.auth}`),console.error("Run: d360-writer --auth auto | api | subscription"),process.exit(2)),e.prompt){await Sn(e.cwd,e.prompt,e.auth,e.resume,e.profile,e.yes);return}e.resume&&(console.error("\u2717 --resume requires --prompt. In the interactive REPL, use /resume instead."),process.exit(2)),e.classic||!process.stdin.isTTY?await Ar(e.cwd,e.auth,e.profile):await es(e.cwd,e.auth,e.profile,ts.version)});ke.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
74
+ Model: ${$.model}${$.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;u({kind:"note",tone:"info",text:F+Mo(I,M,N).join(`
75
+ `)+re}),K([S]);return}Je.current=Date.now(),mt(0),q(!0);let D=new AbortController;De.current=D,ce({total:I.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Converting ${A.length} articles across ${I.length} partitions (\u2264${N} agents at once) on ${$.model}\u2026 (esc to stop)`});try{for await(let M of Yr({cwd:e,partitions:I,promptFor:_o,concurrency:N,profileName:s,allowProdWrites:ht.current,model:$.model,signal:D.signal}))if(M.type==="partition_status")M.status==="running"?(ce(F=>F&&{...F,active:[...F.active,M.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${M.label} \u2014 converting\u2026`})):(ce(F=>F&&{...F,active:F.active.filter(re=>re!==M.label),done:F.done+1,lastAt:Date.now()}),u({kind:"note",tone:M.status==="done"?"ok":"error",text:` ${M.status==="done"?"\u2713":"\u2717"} ${M.label}`}));else if(M.type==="partition_event")ce(F=>{if(!F)return F;let re={...F,lastAt:Date.now()};return M.event.type==="tool"?re.tools=F.tools+1:M.event.type==="text"&&(re.chars=F.chars+M.event.delta.length),re});else if(M.type==="run_done"){$t(tt=>tt+M.totalCostUsd),xt(tt=>tt+M.results.reduce((ds,ps)=>ds+ps.outputTokens,0));let F=M.aborted?"Stopped. ":"",re=t.kind==="api"?"api":"subscription";u({kind:"note",tone:M.aborted?"warn":M.ok?"ok":"warn",text:F+No(M.results,I,re).join(`
76
+ `)})}}catch(M){u({kind:"note",tone:"error",text:`Convert run failed: ${M.message}`})}finally{De.current=null,ce(null),q(!1)}return}case"sync":{let m=(k[0]??"status").toLowerCase();try{if(m==="status"){u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let g=await Xo({cwd:e,profileName:s});u({kind:"note",tone:"info",text:Ft(g).join(`
77
+ `)});return}if(m==="pull"){let g=k[1];if(!g){u({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let y;if(g==="--all"){if(u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),y=(await Xo({cwd:e,profileName:s})).entries.filter(N=>N.status==="remote-ahead"&&N.path).map(N=>N.path),y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is remote-ahead \u2014 no pulls needed. (Conflicts are never bulk-pulled; pull them one by one.)"});return}}else y=[g.replace(/\\/g,"/")];let A=[];for(let I of y)A.push(await la({cwd:e,profileName:s,relPath:I}));we.current=A,Ct();return}u({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${m} \u2014 use /sync or /sync pull <path>|--all.`})}catch(g){u({kind:"note",tone:"error",text:`Sync failed: ${g.message}`})}return}case"scope":{if(!se(e)){u({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}let m=aa(e);if(m.length===0){u({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}qe({cursor:0,rows:m.map(g=>({...g,checked:g.recommended}))});return}case"audit":case"capture-setup":{let g=await(i==="audit"?Ut:Gt)(k,void 0);g.kind==="forward-to-agent"&&g.prompt&&await be(g.prompt,{display:g.display,echoDisplay:!0});return}case"screenshot":{let m=Wo(k);if(m.mode==="list"){if(!se(e)){u({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}u({kind:"note",tone:"info",text:Bo(Fo(e,m.scope),m.scope).join(`
78
+ `)});return}if(m.mode==="single"){let $=await zt(k);$.kind==="forward-to-agent"&&$.prompt&&await be($.prompt,{display:$.display,echoDisplay:!0});return}if(!se(e)){u({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}let g=m.mode==="scope"?m.scope:void 0,y=ma(e,{scope:g}).map($=>$.id);if(y.length===0){u({kind:"note",tone:"info",text:g?`No screenshot placeholders under ${g}.`:"No screenshot placeholders found in the docs."});return}let A=6,I=pa(y,3),N=Kr(e,"standard");Je.current=Date.now(),mt(0),q(!0);let S=new AbortController;De.current=S,ce({verb:"Authoring",total:I.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Authoring ${y.length} screenshot spec${y.length===1?"":"s"} across ${I.length} partition${I.length===1?"":"s"} (\u2264${A} agents at once) on ${N.model}${N.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let $ of Yr({cwd:e,partitions:I,promptFor:vr,concurrency:A,profileName:s,allowProdWrites:ht.current,model:N.model,signal:S.signal}))if($.type==="partition_status")$.status==="running"?(ce(D=>D&&{...D,active:[...D.active,$.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${$.label} \u2014 authoring\u2026`})):(ce(D=>D&&{...D,active:D.active.filter(M=>M!==$.label),done:D.done+1,lastAt:Date.now()}),u({kind:"note",tone:$.status==="done"?"ok":"error",text:` ${$.status==="done"?"\u2713":"\u2717"} ${$.label}`}));else if($.type==="partition_event")ce(D=>{if(!D)return D;let M={...D,lastAt:Date.now()};return $.event.type==="tool"?M.tools=D.tools+1:$.event.type==="text"&&(M.chars=D.chars+$.event.delta.length),M});else if($.type==="run_done"){$t(F=>F+$.totalCostUsd),xt(F=>F+$.results.reduce((re,tt)=>re+tt.outputTokens,0));let D=$.results.filter(F=>F.ok).length,M=$.aborted?"Stopped. ":"";u({kind:"note",tone:$.aborted?"warn":$.ok?"ok":"warn",text:`${M}Authored specs \u2014 ${D}/${$.results.length} batches ok. Next: /capture-setup, then d360-capture capture.`})}}catch($){u({kind:"note",tone:"error",text:`Spec authoring failed: ${$.message}`})}finally{De.current=null,ce(null),q(!1)}return}default:u({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,Ct,u,ge,be]),Pt=G(a=>{let l=(a??R).trim();if(x(""),ft(null),Oe(0),l.startsWith("/")){let k=g=>g.trim().split(/\s+/).slice(1).join(" "),m=k(l);K(g=>g.filter(y=>y.trim()!==l&&!(m&&k(y)===m)))}if(!l||un(l))return;io.current.push(l);let i=Or(l,Qo.current);l.startsWith("/")?dn(i):be(i,{display:l})},[R,un,dn,be]),pn=G(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=Mr(a);if(Nr(l)){let i=Lr(++is.current,l);Qo.current.set(i,l),j(i)}else j(l);return}j(a)},[j]),ze=Math.max(10,_e-6),mn=eo(()=>Qt(f.text,ze),[f.text,ze]),fo=G(a=>w(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),St=G(a=>w(l=>({...l,pos:Wr(Qt(l.text,ze),l.pos,a)})),[ze]),et=G(a=>w(l=>({...l,pos:Fr(Qt(l.text,ze),l.pos,a)})),[ze]),cs=["\x1B[H","\x1B[1~","\x1BOH"],us=["\x1B[F","\x1B[4~","\x1BOF"],fn=G((a,l)=>l.leftArrow?(fo(-1),!0):l.rightArrow?(fo(1),!0):a&&cs.includes(a)?(et("start"),!0):a&&us.includes(a)?(et("end"),!0):l.ctrl&&a==="a"?(et("start"),!0):l.ctrl&&a==="e"?(et("end"),!0):!1,[fo,et]);return Fl((a,l)=>{if(l.ctrl&&a==="c"){te.current?.close(),r();return}if(!z){if(_){if(l.escape){if(De.current){De.current.signal.aborted||(u({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),De.current.abort());return}kt.current||(kt.current=!0,u({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),te.current?.interrupt());return}if(l.return){let i=R.trim();if(!i)return;Ee.current.push(i),lo([...Ee.current]),x("");return}if(fn(a,l))return;if(l.upArrow){St(-1);return}if(l.downArrow){St(1);return}if(l.backspace||l.delete){T();return}a&&!l.ctrl&&!l.meta&&pn(a);return}if(Re){if(l.upArrow){je(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){je(i=>i&&{...i,cursor:Math.min(de.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=de.length){je(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=de[Re.cursor];je(null);let{lines:k,changed:m,effective:g}=ut(e,i.value??"default");for(let y of k)u({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});m&&(nn(y=>y+1),te.current?.setModel(g));return}if(a==="s"){let i=de[Re.cursor];je(null),te.current?.setModel(i.value??void 0),u({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(l.escape){je(null);return}return}if(me){if(l.upArrow){We(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){We(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=me.rows.length){We(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=me.rows[me.cursor];We(null),mo(i.name,l.return===!0);return}if(l.escape){We(null);return}return}if(xe){if(l.upArrow){Fe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Fe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=xe.rows.length){Fe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=xe.rows[xe.cursor].name;if(Fe(null),!_n(e,i).created){u({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}u({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let m=!1;try{let g=Le(i);m=!!g&&!(Se(g)&&!g.refreshToken)}catch{}m?(u({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),K(["/workspace"])):(u({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),K(["/login"])),ye(g=>g+1);return}if(l.escape){Fe(null);return}return}if(J){if(l.upArrow){Be(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Be(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=J.rows.length){Be(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=J.rows[J.cursor],{profile:k,projectId:m}=J;Be(null),cn(k,m,i);return}if(l.escape){Be(null);return}return}if(Q){if(l.upArrow){qe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){qe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){qe(i=>i&&{...i,rows:i.rows.map((k,m)=>m===i.cursor?{...k,checked:!k.checked}:k)});return}if(l.return){let i=Q.rows.filter(k=>k.checked).map(k=>k.path);if(qe(null),i.length===0){u({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}$o(e,i),u({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let k of i)u({kind:"note",tone:"info",text:` ${k}`});u({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(l.escape){qe(null);return}return}if(fe){if(l.upArrow){He(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){He(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=fe.rows.length){He(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=fe.rows[fe.cursor],k=fe.rows.filter(m=>m.path!=="--all").map(m=>m.path);He(null),be(i.path==="--all"?lt(k):Ot(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){He(null);return}return}if(oe){if(l.escape){Ae(null);return}if(l.upArrow){Ae(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ae(i=>i&&{...i,cursor:Math.min(Math.max(0,bt.length-1),i.cursor+1)});return}if(l.return){let i=bt[oe.cursor];if(i){Ae(null);try{u({kind:"preview",name:zr(i),text:Qr(qr(oo(e,i),"utf8"))})}catch(k){u({kind:"note",tone:"error",text:`Could not read ${i}: ${k.message}`})}}return}if(l.backspace||l.delete){Ae(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Ae(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(ne){if(l.escape){Ie(null);return}if(l.upArrow){Ie(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ie(i=>i&&{...i,cursor:Math.min(Math.max(0,vt.length-1),i.cursor+1)});return}if(l.return){let i=vt[ne.cursor];i&&(Ie(null),ge(i.uuid),Te.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},Go(i.uuid),u({kind:"note",tone:"ok",text:`Resumed "${i.name}".`}));return}if(l.backspace||l.delete){Ie(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Ie(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!R&&le){x(le),ae(null),pt.current++;return}if(!R&&Ke.length>0&&a&&/^[1-9]$/.test(a)){let i=Ke[Number(a)-1];if(i){x(i);return}}if(!fn(a,l)){if(ln){if(l.upArrow){Oe(i=>Math.max(0,i-1));return}if(l.downArrow){Oe(i=>Math.min(Ze.length-1,i+1));return}if(l.tab){x("/"+(Ze[Qe]?.name??"")+" "),Oe(0);return}if(l.return){let i=Ze[Qe];if(i){let k=R.trim().slice(1).split(/\s+/).slice(1).join(" ");if(jn(i.usage)&&!k){x("/"+i.name+" "),Oe(0);return}Pt("/"+i.name+(k?" "+k:""));return}}}else{if(l.upArrow){if(R!==""&&Ue===null){St(-1);return}let i=io.current;if(!i.length)return;let k=Ue===null?i.length-1:Math.max(0,Ue-1);ft(k),x(i[k]??"");return}if(l.downArrow){if(R!==""&&Ue===null){St(1);return}let i=io.current;if(Ue===null)return;let k=Ue+1;k>=i.length?(ft(null),x("")):(ft(k),x(i[k]??""));return}}if(l.return){Pt();return}if(l.backspace||l.delete){T();return}if(l.escape){x(""),Oe(0),ae(null),K([]);return}a&&!l.ctrl&&!l.meta&&pn(a)}}}),Ve(()=>{if(_||z)return;let a=Ee.current.shift();a!==void 0&&(lo([...Ee.current]),Pt(a))},[_,z,Pt]),O(X,{flexDirection:"column",width:_e,children:[uo!==null&&O(X,{marginTop:1,flexDirection:"column",children:[uo.truncated&&C(v,{dimColor:!0,children:"\u2026"}),C(v,{children:uo.text})]}),_&&(en?C(Pa,{p:en}):C(va,{startTime:Je.current,chars:ss})),C(X,{borderStyle:"round",borderColor:sn.prod?"yellow":B,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:R?mn.map((a,l)=>{let i=f.text.slice(a.start,a.end),k=l===Zt(mn,f.pos),m=Math.min(f.pos,a.end)-a.start;return O(v,{children:[C(v,{color:B,children:l===0?"> ":" "}),k?O(Vo,{children:[i.slice(0,m),C(to,{ch:i[m]??" "}),i.slice(m+1)]}):i||" "]},`${l}-${a.start}`)}):O(v,{children:[C(v,{color:B,children:"> "}),le&&!_?O(Vo,{children:[C(to,{ch:le[0],dim:!0}),C(v,{color:"gray",children:le.slice(1)}),C(v,{dimColor:!0,children:" (tab)"})]}):co.isSetup||!rs?O(Vo,{children:[C(to,{ch:co.text[0],dim:!0}),C(v,{color:"gray",children:co.text.slice(1)})]}):C(to,{ch:" "})]})}),Zo.length>0&&C(X,{flexDirection:"column",paddingX:1,children:Zo.map((a,l)=>C(v,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),Re?O(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Select model"}),C(v,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),de.map((a,l)=>O(v,{color:l===Re.cursor?B:void 0,children:[l===Re.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===Re.current?" \u2714":""}`.padEnd(16),C(v,{color:"gray",children:a.desc})]},a.label)),C(v,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):me?O(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Switch connection profile"}),me.rows.map((a,l)=>O(v,{color:l===me.cursor?B:void 0,children:[l===me.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===me.current?" \u2714":""}`.padEnd(20),C(v,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?C(v,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),C(v,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):xe?O(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Pick your Document360 environment"}),xe.rows.map((a,l)=>O(v,{color:l===xe.cursor?B:void 0,children:[l===xe.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),C(v,{color:"gray",children:a.apiUrl})]},a.name)),C(v,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):J?O(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Switch workspace"}),C(v,{dimColor:!0,children:`environment ${J.environment} \xB7 project ${J.projectId.slice(0,8)}\u2026`}),J.rows.map((a,l)=>O(v,{color:l===J.cursor?B:void 0,children:[l===J.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===J.current?" \u2714":""}`.padEnd(30),C(v,{color:"gray",children:a.type??""})]},a.id)),C(v,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):Q?(()=>{let l=Math.min(Math.max(0,Q.cursor-Math.floor(7)),Math.max(0,Q.rows.length-14)),i=Q.rows.slice(l,l+14),k=Q.rows.filter(m=>m.checked).length;return O(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:`Which folders back the user docs? (${k} selected of ${Q.rows.length})`}),l>0?C(v,{dimColor:!0,children:` \u2191 ${l} more`}):null,i.map((m,g)=>{let y=l+g;return O(v,{color:y===Q.cursor?B:void 0,children:[y===Q.cursor?"\u276F ":" ",m.checked?"\u25C9 ":"\u25CB ",m.path.padEnd(Math.min(48,_e-34)),C(v,{color:"gray",children:Wt(m)})]},m.path)}),l+14<Q.rows.length?C(v,{dimColor:!0,children:` \u2193 ${Q.rows.length-l-14} more`}):null,C(v,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():fe?O(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Publish which article?"}),fe.rows.map((a,l)=>O(v,{color:l===fe.cursor?B:void 0,children:[l===fe.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,_e-30)),C(v,{color:"gray",children:a.label})]},a.path)),C(v,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):oe?O(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:`Preview article${oe.query?` \u2014 filter: ${oe.query}`:" (type to filter)"}`}),bt.length===0?C(v,{color:"gray",children:"no articles match"}):bt.map((a,l)=>O(v,{color:l===oe.cursor?B:void 0,children:[l===oe.cursor?"\u276F ":" ",a]},a)),C(v,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ne?O(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:`Resume session${ne.query?` \u2014 filter: ${ne.query}`:" (type to filter)"}`}),vt.length===0?C(v,{color:"gray",children:"no sessions match"}):vt.map((a,l)=>O(v,{color:l===ne.cursor?B:void 0,children:[l===ne.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),C(v,{color:l===ne.cursor?B:"gray",children:a.firstPrompt.slice(0,Math.max(10,_e-40))})]},a.uuid)),C(v,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):ln?C(X,{flexDirection:"column",children:Ze.map((a,l)=>O(v,{color:l===Qe?B:void 0,children:[l===Qe?"\u276F ":" ",a.usage.padEnd(22)," ",C(v,{color:l===Qe?B:"gray",children:a.name==="model"&&ao?`${a.desc} (currently ${ao})`:a.desc})]},a.name))}):!R&&Ke.length>0?O(X,{flexDirection:"column",paddingX:1,children:[Ke.map((a,l)=>O(v,{children:[C(v,{color:B,children:l+1})," ",a.slice(0,Math.max(20,_e-5))]},a)),C(v,{dimColor:!0,children:`press 1-${Ke.length} to fill the command \xB7 esc dismiss`})]}):C(X,{paddingX:1,children:O(v,{color:"gray",children:[sn.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${ao??"model n/a"}${t.kind==="api"?on>0?` \xB7 ${Ne(on)}`:"":tn>0?` \xB7 ${Bt(tn)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}H();import{jsx as Ra}from"react/jsx-runtime";async function ts(e=process.cwd(),t="auto",o,n="0.0.0"){let r=Ta(t);r.kind==="none"&&(console.error(""),console.error(b("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${P("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${P("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=Sa(Ra(es,{cwd:e,auth:r,profileName:o,version:n}));await s(),process.stdout.write(`
79
+ `),process.exit(0)}var Da=Aa(import.meta.url),os=Da("../package.json"),ke=new ja;function Ko(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}ke.command("login").description("Sign in to Document360 (browser OAuth; project chosen during login)").option("--profile <name>","Connection profile (defaults to the repo's defaultProfile)").option("--env <name>",!1).option("--manual","No local listener \u2014 paste the redirect URL instead (SSH/locked-down setups)").action(async e=>{Ko(e),await Et({profile:e.profile,manual:e.manual})});ke.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Ko(e),await Cn({profile:e.profile})});ke.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Ko(e),await vn({profile:e.profile})});var Jo=ke.command("profile").description("Manage connection profiles for the current repo");Jo.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Dt(process.cwd()));Jo.command("use <name>").description("Set the default profile for this repo").action(e=>It(process.cwd(),e));Jo.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>_t(process.cwd(),e));var ns=ke.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");ns.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>Ge(process.cwd(),e.profile));ns.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await yn(process.cwd(),e,t.profile)});ke.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Sn());ke.command("doctor").description("Health-check: node, Claude auth, Document360 login, profile/workspace, category map, API reachability").action(async()=>{let{renderDoctorChecks:e,runDoctorChecks:t}=await Promise.resolve().then(()=>(qt(),kr)),o=await t(process.cwd());for(let n of e(o))console.log(n);process.exitCode=o.some(n=>n.level==="fail")?1:0});ke.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(os.version,"-v, --version").option("-p, --prompt <text>","One-shot prompt mode (non-interactive)").option("-r, --resume <name>","Resume a saved session (with --prompt; use /resume inside the REPL)").option("-C, --cwd <dir>","Working directory",process.cwd()).option("--auth <mode>","Auth mode: auto | api | subscription","auto").option("--profile <name>","Connection profile (defaults to the repo's defaultProfile)").option("--yes","Skip the production write confirmation (for one-shot/CI)").option("--classic","Use the plain readline REPL instead of the Ink TUI").action(async e=>{if(Ea.includes(e.auth)||(console.error(`\u2717 Invalid --auth mode: ${e.auth}`),console.error("Run: d360-writer --auth auto | api | subscription"),process.exit(2)),e.prompt){await Tn(e.cwd,e.prompt,e.auth,e.resume,e.profile,e.yes);return}e.resume&&(console.error("\u2717 --resume requires --prompt. In the interactive REPL, use /resume instead."),process.exit(2)),e.classic||!process.stdin.isTTY?await Ir(e.cwd,e.auth,e.profile):await ts(e.cwd,e.auth,e.profile,os.version)});ke.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
@@ -1,9 +1,14 @@
1
+ export type CommandGroup = 'start' | 'docs' | 'publish' | 'setup' | 'session';
1
2
  export type CommandSpec = {
2
3
  name: string;
3
4
  usage: string;
4
5
  desc: string;
6
+ group: CommandGroup;
5
7
  };
6
8
  export declare const COMMANDS: CommandSpec[];
9
+ /** The grouped /help text — one source of truth for both the TUI and the classic REPL. Plain
10
+ strings (no ANSI), so the TUI can push it as a note without leaking escape codes. */
11
+ export declare function renderGroupedHelp(): string[];
7
12
  /** Commands whose body filters as you type `/<prefix>`. */
8
13
  export declare function filterCommands(input: string): CommandSpec[];
9
14
  /** True when the usage has a REQUIRED argument (`<...>` outside optional `[...]`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document360-writer",
3
- "version": "0.4.22",
3
+ "version": "0.4.23",
4
4
  "description": "Standalone documentation agent CLI. Reads your code, writes your docs. Specialized for Document360 publishing.",
5
5
  "type": "module",
6
6
  "bin": {