document360-writer 0.4.24 → 0.4.26

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,80 +1,80 @@
1
1
  #!/usr/bin/env node
2
- var $s=Object.defineProperty;var $n=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var bs=(e,t)=>{for(var o in t)$s(e,o,{get:t[o],enumerable:!0})};import ve from"picocolors";var H,vs,Cs,Ps,bn,ot,p,x,N,A,P,Y,he,B=$n(()=>{"use strict";H="#7f56d9",[vs,Cs,Ps]=[127,86,217],bn=e=>ve.isColorSupported?`\x1B[38;2;${vs};${Cs};${Ps}m${e}\x1B[39m`:e,ot=e=>ve.bold(bn(e)),p=e=>ve.dim(e),x=e=>ve.red(e),N=e=>ve.yellow(e),A=e=>ve.green(e),P=bn,Y=e=>ve.gray(e),he=e=>ve.bold(e)});var vr={};bs(vr,{doctorCommand:()=>dt,renderDoctorChecks:()=>br,runDoctorChecks:()=>$r});import{existsSync as ol}from"node:fs";import{d360GetAll as nl,getAccessToken as rl,isExpired as Wo,loadProfileMap as sl,loadTokens as il,packageSkillsDir as xr,projectConfigPath as ll,readProjectConfig as al,resolveActiveProfile as cl,resolveAuth as ul,resolveModelSetting as dl}from"document360-engine";async function $r(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=ul("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=dl(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=al(e);if(!s)return t.push({level:"fail",label:`No ${ll(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=cl(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=il(h.name);f?Wo(f)&&!f.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Wo(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=sl(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(ol(xr())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${xr()}`,fix:"Reinstall document360-writer (broken install)"}),f&&(!Wo(f)||f.refreshToken)){let R={profile:h.name,connection:h.connection};try{await rl(R);let $=await nl(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 br(e){let t=[""];for(let r of e){let s=r.level==="ok"?A("\u2713"):r.level==="warn"?N("\u26A0"):x("\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?A(`\u2713 ${n===0?"All checks passed":`Healthy (${n} warning${n===1?"":"s"})`}`):x(`\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 br(await $r(t.cwd)))console.log(o);return{kind:"continue"}}var qt=$n(()=>{"use strict";B()});import{Command as Ba}from"commander";import{createRequire as Ha}from"node:module";import{AUTH_MODES as qa}from"document360-engine";import{input as Ms}from"@inquirer/prompts";import{loginPkce as Ns,refreshTokens as Ls,toStoredTokens as Sn,clearTokens as Os,decodeJwtClaims as Pn,isExpired as Us,loadTokens as Ws,saveTokens as Tn,resolveActiveProfile as jt,setProfileProject as Fs,readProjectConfig as Bs}from"document360-engine";B();import{select as Ss}from"@inquirer/prompts";import{resolveActiveProfile as Ts,setProfileProject as Rs,resolveProjectId as js,listWorkspaces as As}from"document360-engine";async function Tt(e,t){let o=Ts(e,t),n={profile:o.name,connection:o.connection},r=o.project.projectId??js(n);return{workspaces:await As(n,r),projectId:r,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var vn=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function ho(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){Rs(e,t,{projectId:o,workspaceId:n})}async function Cn(e,t,o){let n;try{n=await Tt(e,o)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=ho(n.workspaces,t);return r?(Rt(e,n.profile,n.projectId,r.id),console.log(A(`\u2713 Workspace set to "${r.name??r.id}" for profile "${n.profile}".`)),0):(console.log(x(`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(x(`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"):" "} ${vn(f)} ${p(f.id)}`);console.log(p("Run: d360-writer workspace use <name>"));return}let d=await Ss({message:"Select the Document360 workspace for this repo:",choices:n.map(f=>({name:`${vn(f)}${f.id===c?" (current)":""}`,value:f.id}))});Rt(e,s,r,d);let h=n.find(f=>f.id===d);console.log(A(`\u2713 Workspace set to "${h?.name??d}" for profile "${s}".`))}B();import Es from"picocolors";function Ds(e=process.env){return e.FORCE_HYPERLINK==="0"||!Es.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 Is(e,t=e,o){return Ds(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var _s=/https?:\/\/[^\s\x1b]+/g;function nt(e,t){return e.replace(_s,o=>Is(o,o,t))}function Rn(e){return{...Pn(e.idToken)??{},...Pn(e.accessToken)??{}}}function At(e){let t=Rn(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 Ns(o,{manual:e.manual,promptForRedirect:s=>Ms({message:s})},s=>console.log(nt(s))),r=Sn(t.name,n);if(Tn(r),ko(r,t.name,s=>console.log(p(s))),console.log(""),console.log(A(`\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{Bs(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Ge(process.cwd(),t.name))}catch{}}function ko(e,t,o){let r=Rn(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(jt(process.cwd(),t).project.projectId)return;Fs(process.cwd(),t,{projectId:r}),o(` Project ${r} written to profile "${t}".`)}catch{}}async function jn(e){let t=jt(process.cwd(),e.profile),o=t.connection,n=Ws(t.name);if(!n){console.log(x(`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)}`),Us(n))if(n.refreshToken)try{let r=Sn(t.name,await Ls(o,n.refreshToken));Tn(r),console.log(A(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(N(`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(N("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 An(e){let t=jt(process.cwd(),e.profile);Os(t.name)?console.log(A(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(p(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}B();import{readProjectConfig as En,writeProjectConfig as Hs,resolveActiveProfile as qs,loadTokens as zs,isExpired as Gs}from"document360-engine";function Dt(e){let t=En(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(x("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?N(" \u26A0 PRODUCTION"):"",c=n.connection.environment??"(inline)",d=zs(o),h=d?Gs(d)&&!d.refreshToken?N("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=En(e);if(!o?.profiles?.[t]){let r=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log(x(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}o.defaultProfile=t,Hs(o,e);let n=o.profiles[t].production?N(" \u26A0 PRODUCTION"):"";console.log(A(`\u2713 Default profile is now "${t}"${n}`))}function _t(e,t){try{let o=qs(e,t);console.log(""),console.log(`Profile ${P(o.name)}${o.production?N(" \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(x(o.message)),process.exitCode=1}}B();import{existsSync as Xs,readdirSync as Vs,statSync as Ys}from"node:fs";import{join as Ks}from"node:path";import{apiLogDir as Js}from"document360-engine";function Dn(){let e=Js();if(console.log(""),console.log(`Document360 API logs: ${P(e)}`),!Xs(e)){console.log(p(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=Vs(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=(Ys(Ks(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("")}B();import{createSession as Qs,resolveAuth as Zs,findByName as ei,slugify as ti,touchSession as oi,upsertSession as ni,resolveActiveProfile as ri}from"document360-engine";async function In(e,t,o,n,r,s){let c=Zs(o);c.kind==="none"&&(console.error(""),console.error(x("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=ri(e,r)}catch($){console.error(x(`Document360 profile error: ${$.message}`)),process.exit(2)}d.production&&(console.error(N(`\u26A0 Profile "${d.name}" is PRODUCTION.`)),s||(console.error(x("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 $=ei(e,n);$||(console.error(x(`No saved session matches "${n}" in this repo.`)),console.error(p("List sessions inside the REPL with /resume.")),process.exit(2)),h=$.uuid,console.error(p(`Resuming "${$.name}"`))}let f=Qs({cwd:e,resume:h,profileName:r,allowProdWrites:s===!0}),w=h??null,R=1;for await(let $ of f.send(t))switch($.type){case"session":if(!w){w=$.sessionId;let j=new Date().toISOString();ni({uuid:w,name:ti(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(p(`(${$.inputTokens}\u2192${$.outputTokens} tokens`+($.costUsd>0?`, $${$.costUsd<.01?$.costUsd.toFixed(4):$.costUsd.toFixed(2)}`:"")+")")),$.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${$.message}`)),process.exit(1)}w&&oi(w),process.stdout.write(`
3
- `),process.exit(R)}import{createInterface as Dl}from"node:readline/promises";import{createSession as Yo,resolveAuth as Il,getSession as _l,setTitle as Ml,slugify as Nl,touchSession as Wr,upsertSession as Ll,generateTitle as Ol,resolveActiveProfile as Fr,resolveModelSetting as Ul,readProjectConfig as Wl,decodeJwtClaims as Or,isExpired as Fl,loadTokens as Bl}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:"reset",usage:"/reset",desc:"[DANGER] Delete all d360-writer files (docs, config, screenshots); types repo name to confirm",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"}],si=[{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 si){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 _n(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return rt.filter(o=>o.name.startsWith(t))}function Mn(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function yo(){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"}}B();import{getSession as ii}from"document360-engine";async function Nn(e,t){let o=t.currentUuid(),n=o?ii(o):void 0;return console.log(n?p(`
2
+ var Rs=Object.defineProperty;var Tn=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var js=(e,t)=>{for(var o in t)Rs(e,o,{get:t[o],enumerable:!0})};import ve from"picocolors";var H,As,Es,Ds,Rn,nt,p,x,N,A,P,V,he,B=Tn(()=>{"use strict";H="#7f56d9",[As,Es,Ds]=[127,86,217],Rn=e=>ve.isColorSupported?`\x1B[38;2;${As};${Es};${Ds}m${e}\x1B[39m`:e,nt=e=>ve.bold(Rn(e)),p=e=>ve.dim(e),x=e=>ve.red(e),N=e=>ve.yellow(e),A=e=>ve.green(e),P=Rn,V=e=>ve.gray(e),he=e=>ve.bold(e)});var jr={};js(jr,{doctorCommand:()=>pt,renderDoctorChecks:()=>Rr,runDoctorChecks:()=>Tr});import{existsSync as cl}from"node:fs";import{d360GetAll as ul,getAccessToken as dl,isExpired as Go,loadProfileMap as pl,loadTokens as ml,packageSkillsDir as Sr,projectConfigPath as fl,readProjectConfig as gl,resolveActiveProfile as hl,resolveAuth as kl,resolveModelSetting as yl}from"document360-engine";async function Tr(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=kl("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=yl(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=gl(e);if(!s)return t.push({level:"fail",label:`No ${fl(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=hl(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=ml(h.name);f?Go(f)&&!f.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Go(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=pl(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(cl(Sr())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Sr()}`,fix:"Reinstall document360-writer (broken install)"}),f&&(!Go(f)||f.refreshToken)){let R={profile:h.name,connection:h.connection};try{await dl(R);let $=await ul(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 Rr(e){let t=[""];for(let r of e){let s=r.level==="ok"?A("\u2713"):r.level==="warn"?N("\u26A0"):x("\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?A(`\u2713 ${n===0?"All checks passed":`Healthy (${n} warning${n===1?"":"s"})`}`):x(`\u2717 ${o} problem${o===1?"":"s"} found`)),t.push(""),t}async function pt(e,t){console.log(p("Running checks\u2026"));for(let o of Rr(await Tr(t.cwd)))console.log(o);return{kind:"continue"}}var zt=Tn(()=>{"use strict";B()});import{Command as Ja}from"commander";import{createRequire as Qa}from"node:module";import{AUTH_MODES as Za}from"document360-engine";import{input as Bs}from"@inquirer/prompts";import{loginPkce as Hs,refreshTokens as qs,toStoredTokens as Dn,clearTokens as zs,decodeJwtClaims as En,isExpired as Gs,loadTokens as Xs,saveTokens as In,resolveActiveProfile as At,setProfileProject as Ys,readProjectConfig as Vs}from"document360-engine";B();import{select as Is}from"@inquirer/prompts";import{resolveActiveProfile as _s,setProfileProject as Ms,resolveProjectId as Ns,listWorkspaces as Ls}from"document360-engine";async function Rt(e,t){let o=_s(e,t),n={profile:o.name,connection:o.connection},r=o.project.projectId??Ns(n);return{workspaces:await Ls(n,r),projectId:r,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var jn=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function $o(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 jt(e,t,o,n){Ms(e,t,{projectId:o,workspaceId:n})}async function An(e,t,o){let n;try{n=await Rt(e,o)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=$o(n.workspaces,t);return r?(jt(e,n.profile,n.projectId,r.id),console.log(A(`\u2713 Workspace set to "${r.name??r.id}" for profile "${n.profile}".`)),0):(console.log(x(`No workspace matches "${t}". Available: ${n.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function Ye(e,t){let o;try{o=await Rt(e,t)}catch(f){console.log(x(`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"):" "} ${jn(f)} ${p(f.id)}`);console.log(p("Run: d360-writer workspace use <name>"));return}let d=await Is({message:"Select the Document360 workspace for this repo:",choices:n.map(f=>({name:`${jn(f)}${f.id===c?" (current)":""}`,value:f.id}))});jt(e,s,r,d);let h=n.find(f=>f.id===d);console.log(A(`\u2713 Workspace set to "${h?.name??d}" for profile "${s}".`))}B();import Os from"picocolors";function Us(e=process.env){return e.FORCE_HYPERLINK==="0"||!Os.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 Ws(e,t=e,o){return Us(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Fs=/https?:\/\/[^\s\x1b]+/g;function rt(e,t){return e.replace(Fs,o=>Ws(o,o,t))}function _n(e){return{...En(e.idToken)??{},...En(e.accessToken)??{}}}function Et(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 Dt(e){let t=At(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 Hs(o,{manual:e.manual,promptForRedirect:s=>Bs({message:s})},s=>console.log(rt(s))),r=Dn(t.name,n);if(In(r),bo(r,t.name,s=>console.log(p(s))),console.log(""),console.log(A(`\u2713 Logged in to "${t.name}" as ${Et(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{Vs(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Ye(process.cwd(),t.name))}catch{}}function bo(e,t,o){let r=_n(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(At(process.cwd(),t).project.projectId)return;Ys(process.cwd(),t,{projectId:r}),o(` Project ${r} written to profile "${t}".`)}catch{}}async function Mn(e){let t=At(process.cwd(),e.profile),o=t.connection,n=Xs(t.name);if(!n){console.log(x(`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":""}: ${Et(n)}`),Gs(n))if(n.refreshToken)try{let r=Dn(t.name,await qs(o,n.refreshToken));In(r),console.log(A(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(N(`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(N("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 Nn(e){let t=At(process.cwd(),e.profile);zs(t.name)?console.log(A(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(p(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}B();import{readProjectConfig as Ln,writeProjectConfig as Ks,resolveActiveProfile as Js,loadTokens as Qs,isExpired as Zs}from"document360-engine";function It(e){let t=Ln(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(x("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?N(" \u26A0 PRODUCTION"):"",c=n.connection.environment??"(inline)",d=Qs(o),h=d?Zs(d)&&!d.refreshToken?N("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 _t(e,t){let o=Ln(e);if(!o?.profiles?.[t]){let r=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log(x(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}o.defaultProfile=t,Ks(o,e);let n=o.profiles[t].production?N(" \u26A0 PRODUCTION"):"";console.log(A(`\u2713 Default profile is now "${t}"${n}`))}function Mt(e,t){try{let o=Js(e,t);console.log(""),console.log(`Profile ${P(o.name)}${o.production?N(" \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(x(o.message)),process.exitCode=1}}B();import{existsSync as ei,readdirSync as ti,statSync as oi}from"node:fs";import{join as ni}from"node:path";import{apiLogDir as ri}from"document360-engine";function On(){let e=ri();if(console.log(""),console.log(`Document360 API logs: ${P(e)}`),!ei(e)){console.log(p(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=ti(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=(oi(ni(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("")}B();import{createSession as si,resolveAuth as ii,findByName as li,slugify as ai,touchSession as ci,upsertSession as ui,resolveActiveProfile as di}from"document360-engine";async function Un(e,t,o,n,r,s){let c=ii(o);c.kind==="none"&&(console.error(""),console.error(x("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=di(e,r)}catch($){console.error(x(`Document360 profile error: ${$.message}`)),process.exit(2)}d.production&&(console.error(N(`\u26A0 Profile "${d.name}" is PRODUCTION.`)),s||(console.error(x("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 $=li(e,n);$||(console.error(x(`No saved session matches "${n}" in this repo.`)),console.error(p("List sessions inside the REPL with /resume.")),process.exit(2)),h=$.uuid,console.error(p(`Resuming "${$.name}"`))}let f=si({cwd:e,resume:h,profileName:r,allowProdWrites:s===!0}),w=h??null,R=1;for await(let $ of f.send(t))switch($.type){case"session":if(!w){w=$.sessionId;let j=new Date().toISOString();ui({uuid:w,name:ai(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(V(` \u2699 ${$.name}`));break;case"result":R=$.ok?0:1,console.error(p(`(${$.inputTokens}\u2192${$.outputTokens} tokens`+($.costUsd>0?`, $${$.costUsd<.01?$.costUsd.toFixed(4):$.costUsd.toFixed(2)}`:"")+")")),$.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${$.message}`)),process.exit(1)}w&&ci(w),process.stdout.write(`
3
+ `),process.exit(R)}import{createInterface as Ul}from"node:readline/promises";import{createSession as tn,resolveAuth as Wl,getSession as Fl,setTitle as Bl,slugify as Hl,touchSession as Gr,upsertSession as ql,generateTitle as zl,resolveActiveProfile as Xr,resolveModelSetting as Gl,readProjectConfig as Xl,decodeJwtClaims as qr,isExpired as Yl,loadTokens as Vl}from"document360-engine";var st=[{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:"reset",usage:"/reset",desc:"[DANGER] Delete all d360-writer files (docs, config, screenshots); types repo name to confirm",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"}],pi=[{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 Nt(){let e=Math.max(...st.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 pi){t.push("",n);for(let r of st.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 Wn(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return st.filter(o=>o.name.startsWith(t))}function Fn(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function vo(){console.log("");for(let e of Nt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}B();import{getSession as mi}from"document360-engine";async function Bn(e,t){let o=t.currentUuid(),n=o?mi(o):void 0;return console.log(n?p(`
4
4
  (conversation reset \u2014 "${n.name}" is still available via /resume)
5
5
  `):p(`
6
6
  (conversation reset \u2014 agent will start fresh on the next prompt)
7
- `)),{kind:"clear"}}async function wo(){return{kind:"exit"}}B();import{input as st,confirm as li}from"@inquirer/prompts";import{basename as Ln,join as xo}from"node:path";import{existsSync as it,readFileSync as ai,readdirSync as ci}from"node:fs";import{writeProjectConfig as On,readProjectConfig as ui,projectConfigPath as Un}from"document360-engine";function Wn(e,t="berlin"){let o=Un(e);if(it(o))return{created:!1,path:o,profileName:""};let n={projectId:Bn(e)??Ln(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:Hn(e)};return On(n,e),{created:!0,path:o,profileName:t}}async function Fn(){let e=process.cwd(),t=Un(e);if(it(t)&&!await li({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(p("init cancelled.")),{kind:"continue"};let o=Bn(e)??Ln(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:Hn(e)},f=ui(e);return f?.terminologyGlossary&&(h.terminologyGlossary=f.terminologyGlossary),On(h,e),console.log(""),console.log(A(`\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 Bn(e){let t=xo(e,"package.json");if(!it(t))return null;try{return JSON.parse(ai(t,"utf8")).name??null}catch{return null}}function Hn(e){let t=[];for(let o of["README.md","ARCHITECTURE.md","CLAUDE.md"])it(xo(e,o))&&t.push(o);for(let o of["src","api","docs"]){let n=xo(e,o);it(n)&&!di(n)&&t.push(o)}return t}function di(e){try{return ci(e,{withFileTypes:!0}).filter(o=>o.isDirectory()&&!o.name.startsWith(".")).length>6}catch{return!1}}B();import{readMcpConfig as $o,writeMcpConfig as qn}from"document360-engine";async function Nt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(pi(),{kind:"continue"}):t==="add"?(mi(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(fi(e.slice(1)),{kind:"continue"}):(console.log(x(`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 pi(){let e=$o(),t=Object.keys(e.servers);if(t.length===0){console.log(p(`
7
+ `)),{kind:"clear"}}async function Co(){return{kind:"exit"}}B();import{input as it,confirm as fi}from"@inquirer/prompts";import{basename as Hn,join as Po}from"node:path";import{existsSync as lt,readFileSync as gi,readdirSync as hi}from"node:fs";import{writeProjectConfig as qn,readProjectConfig as ki,projectConfigPath as zn}from"document360-engine";function Gn(e,t="berlin"){let o=zn(e);if(lt(o))return{created:!1,path:o,profileName:""};let n={projectId:Yn(e)??Hn(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:Vn(e)};return qn(n,e),{created:!0,path:o,profileName:t}}async function Xn(){let e=process.cwd(),t=zn(e);if(lt(t)&&!await fi({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(p("init cancelled.")),{kind:"continue"};let o=Yn(e)??Hn(e),n=await it({message:"Project ID (used to scope sessions, screenshots, etc.):",default:o}),r=await it({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await it({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await it({message:"Default connection profile name:",default:"berlin"}),d=await it({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:Vn(e)},f=ki(e);return f?.terminologyGlossary&&(h.terminologyGlossary=f.terminologyGlossary),qn(h,e),console.log(""),console.log(A(`\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 Yn(e){let t=Po(e,"package.json");if(!lt(t))return null;try{return JSON.parse(gi(t,"utf8")).name??null}catch{return null}}function Vn(e){let t=[];for(let o of["README.md","ARCHITECTURE.md","CLAUDE.md"])lt(Po(e,o))&&t.push(o);for(let o of["src","api","docs"]){let n=Po(e,o);lt(n)&&!yi(n)&&t.push(o)}return t}function yi(e){try{return hi(e,{withFileTypes:!0}).filter(o=>o.isDirectory()&&!o.name.startsWith(".")).length>6}catch{return!1}}B();import{readMcpConfig as So,writeMcpConfig as Kn}from"document360-engine";async function Lt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(wi(),{kind:"continue"}):t==="add"?(xi(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?($i(e.slice(1)),{kind:"continue"}):(console.log(x(`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 wi(){let e=So(),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(` ${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 mi(e){let[t,o,...n]=e;if(!t||!o||n.length===0){console.log(x("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(o!=="stdio"&&o!=="http"&&o!=="sse"){console.log(x(`Unknown transport: ${o}. Use stdio, http, or sse.`));return}let r=$o(),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(x(`-H expects key:value (no spaces). Got: ${h??"(nothing)"}`));return}c[f[1].trim()]=f[2].trim()}else{console.log(x(`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,qn(r),console.log(""),console.log(A(`\u2713 Registered "${t}" (${o})`)),console.log(N(" 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 fi(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let o=$o();if(!o.servers[t]){console.log(x(`No server named "${t}".`));return}delete o.servers[t],qn(o),console.log(A(`\u2713 Removed "${t}".`)),console.log(p(" Run /clear to drop it from the current session immediately."))}B();import{select as gi}from"@inquirer/prompts";import{computeSyncStatus as zn}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 Gn={"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 Gn).map(t=>({path:t.path,label:Gn[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 Xn(e,t){let o=e[0];if(o==="--all"){console.log(p("Checking what needs publishing\u2026"));try{let n=Lt((await zn({cwd:t?.cwd??process.cwd()})).entries);return n.length===0?(console.log(A("\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(x(`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 zn({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(x(`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(A("\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(()=>gi({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"}}B();import{checkbox as hi}from"@inquirer/prompts";import{inventoryRepo as ki,readProjectConfig as Vn,writeProjectConfig as yi}from"document360-engine";function bo(e,t){let o=Vn(e);o&&(o.authoritativeSourceFiles=t,yi(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 Yn(e,t){let o=t?.cwd??process.cwd();if(!Vn(o))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let n=ki(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(()=>hi({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"};bo(o,r),console.log(A(`\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 vi}from"@inquirer/prompts";import{applyPull as er,computeSyncStatus as tr,planPull as Ci,D360AuthError as Pi}from"document360-engine";B();var Kn=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:x,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:N,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:x,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:x,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 wi(e){return e.path?e.path:`${e.title??"(untitled)"} ${Y(`[${e.articleId}]`)}`}function Ft(e){let t=[];for(let c of Kn){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(wi(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?A("\u2713 everything in sync"):[o>0?A(`\u2713 ${o} in sync`):null,...Kn.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 xi}from"diff";import Jn from"picocolors";var Qn=80,$i="\x1B[48;2;74;28;28m",bi="\x1B[48;2;24;66;24m",Zn="\x1B[49m",vo=e=>String(e).padStart(5);function Me(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 xi(e){let[t,o,...n]=e;if(!t||!o||n.length===0){console.log(x("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(o!=="stdio"&&o!=="http"&&o!=="sse"){console.log(x(`Unknown transport: ${o}. Use stdio, http, or sse.`));return}let r=So(),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(x(`-H expects key:value (no spaces). Got: ${h??"(nothing)"}`));return}c[f[1].trim()]=f[2].trim()}else{console.log(x(`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,Kn(r),console.log(""),console.log(A(`\u2713 Registered "${t}" (${o})`)),console.log(N(" 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 $i(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let o=So();if(!o.servers[t]){console.log(x(`No server named "${t}".`));return}delete o.servers[t],Kn(o),console.log(A(`\u2713 Removed "${t}".`)),console.log(p(" Run /clear to drop it from the current session immediately."))}B();import{select as bi}from"@inquirer/prompts";import{computeSyncStatus as Jn}from"document360-engine";function Ut(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 Qn={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Ot(e){return e.filter(t=>t.path!==null&&t.status in Qn).map(t=>({path:t.path,label:Qn[t.status]}))}function at(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 Zn(e,t){let o=e[0];if(o==="--all"){console.log(p("Checking what needs publishing\u2026"));try{let n=Ot((await Jn({cwd:t?.cwd??process.cwd()})).entries);return n.length===0?(console.log(A("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:at(n.map(r=>r.path)),display:"/publish --all"}}catch(n){return console.log(x(`Could not compute sync status: ${n.message}`)),{kind:"continue"}}}if(!o){console.log(p("Checking what needs publishing\u2026"));let n;try{n=Ot((await Jn({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(x(`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(A("\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(()=>bi({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:at(n.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:Ut(o),display:`/publish ${o}`}}async function Wt(){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 vi}from"@inquirer/prompts";import{inventoryRepo as Ci,readProjectConfig as er,writeProjectConfig as Pi}from"document360-engine";function To(e,t){let o=er(e);o&&(o.authoritativeSourceFiles=t,Pi(o,e))}function Ft(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 tr(e,t){let o=t?.cwd??process.cwd();if(!er(o))return console.log(x("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(Ft(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>vi({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:n.map(s=>({name:`${s.path} (${Ft(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"};To(o,r),console.log(A(`\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 Ai}from"@inquirer/prompts";import{applyPull as ir,computeSyncStatus as lr,planPull as Ei,D360AuthError as Di}from"document360-engine";B();var or=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:x,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:N,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:x,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:x,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:V,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:V,mark:"\xB7"}];function Si(e){return e.path?e.path:`${e.title??"(untitled)"} ${V(`[${e.articleId}]`)}`}function Bt(e){let t=[];for(let c of or){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(Si(h))}${h.detail?V(` (${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?A("\u2713 everything in sync"):[o>0?A(`\u2713 ${o} in sync`):null,...or.map(c=>{let d=e.counts[c.status]??0;return d>0?`${d} ${r[c.status]}`:null})].filter(Boolean).join(V(" \xB7 "));return t.push(s+V(` \xB7 ${n} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as Ti}from"diff";import nr from"picocolors";var rr=80,Ri="\x1B[48;2;74;28;28m",ji="\x1B[48;2;24;66;24m",sr="\x1B[49m",Ro=e=>String(e).padStart(5);function Ne(e,t,o){let n=$=>{let j=$.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),d=xi("a","b",r,s,"","",{context:3}),h=0,f=0,w=[];d.hunks.forEach(($,j)=>{j>0&&w.push(Jn.gray(" \u2026"));let T=$.oldStart,_=$.newStart;for(let q of $.lines){let z=q[0],ee=q.slice(1).slice(0,c);z==="-"?(f++,w.push(`${$i}${vo(T++)} - ${ee}${Zn}`)):z==="+"?(h++,w.push(`${bi}${vo(_++)} + ${ee}${Zn}`)):(w.push(Jn.gray(vo(_))+" "+ee),T++,_++)}});let R=w.slice(0,Qn);return{added:h,removed:f,lines:R,hidden:Math.max(0,w.length-Qn)}}async function or(e,t){let o=(e[0]??"status").toLowerCase();try{if(o==="status")return await Si(t.cwd),{kind:"continue"};if(o==="pull")return await Ti(t,e.slice(1)),{kind:"continue"};console.log(x(`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 Pi?console.log(x(n.message)):console.log(x(`Sync failed: ${n.message}`))}return{kind:"continue"}}async function Si(e){console.log(p("Checking Document360 for drift\u2026"));let t=await tr({cwd:e});for(let o of Ft(t))console.log(o);console.log("")}async function Ti(e,t){let o=t[0];if(!o){console.log(x("Usage: /sync pull <article-path> | --all"));return}let n;if(o==="--all"){if(console.log(p("Checking Document360 for drift\u2026")),n=(await tr({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),n.length===0){console.log(A("\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 Ci({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(N(` \u26A0 ${f}`));s.overwritesLocalChanges&&console.log(N(" \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.")),er({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(()=>vi({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(p(" Skipped."));continue}er({cwd:e.cwd},s),console.log(A(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}B();import{statSync as Oi}from"node:fs";import{resolve as Ui}from"node:path";import{estimateBulkCost as Wi,planPartitions as Fi,readProjectConfig as Bi,resolveModelForOperation as Hi,runPartitioned as qi,trackedArticlePaths as zi}from"document360-engine";import W from"picocolors";import ar from"wrap-ansi";import jo from"string-width";B();import Ce from"picocolors";import Co from"wrap-ansi";import Po from"string-width";var nr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),rr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),sr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function Ri(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(nr(r)){o.push({kind:"hr"}),n++;continue}if(r.includes("|")&&n+1<t.length&&rr(t[n+1])){let d=sr(r);n+=2;let h=[];for(;n<t.length&&t[n].includes("|")&&t[n].trim()!=="";)h.push(sr(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])&&!nr(t[n])&&!(t[n].includes("|")&&n+1<t.length&&rr(t[n+1]));)c.push(t[n++]);o.push({kind:"para",text:c.join(" ")})}return o}function So(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 ji=(e,t)=>e+" ".repeat(Math.max(0,t-Po(e)));function Ai(e,t,o){let n=e.length,r=e.map((T,_)=>Math.max(Po(T),...t.map(q=>Po(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"),$=(T,_)=>{let q=d.map((ie,ye)=>{let le=T[ye]??"",ae=_?Ce.bold(le):So(le);return Co(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} ${ji(q[le][ie]??"",ye)} `).join("")+R);return ee.join(`
18
- `)},j=[w("\u250C","\u252C","\u2510"),$(e,!0)];return j.push(t.length===0?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524")),t.forEach((T,_)=>{j.push($(T,!1)),j.push(_===t.length-1?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524"))}),j.join(`
19
- `)}function Ei(e,t){switch(e.kind){case"heading":return Ce.bold(e.text);case"hr":return Ce.gray("\u2500".repeat(t));case"para":return Co(So(e.text),t);case"list":return e.items.map(o=>{let[n="",...r]=Co(So(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=Ti("a","b",r,s,"","",{context:3}),h=0,f=0,w=[];d.hunks.forEach(($,j)=>{j>0&&w.push(nr.gray(" \u2026"));let T=$.oldStart,I=$.newStart;for(let q of $.lines){let z=q[0],ee=q.slice(1).slice(0,c);z==="-"?(f++,w.push(`${Ri}${Ro(T++)} - ${ee}${sr}`)):z==="+"?(h++,w.push(`${ji}${Ro(I++)} + ${ee}${sr}`)):(w.push(nr.gray(Ro(I))+" "+ee),T++,I++)}});let R=w.slice(0,rr);return{added:h,removed:f,lines:R,hidden:Math.max(0,w.length-rr)}}async function ar(e,t){let o=(e[0]??"status").toLowerCase();try{if(o==="status")return await Ii(t.cwd),{kind:"continue"};if(o==="pull")return await _i(t,e.slice(1)),{kind:"continue"};console.log(x(`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 Di?console.log(x(n.message)):console.log(x(`Sync failed: ${n.message}`))}return{kind:"continue"}}async function Ii(e){console.log(p("Checking Document360 for drift\u2026"));let t=await lr({cwd:e});for(let o of Bt(t))console.log(o);console.log("")}async function _i(e,t){let o=t[0];if(!o){console.log(x("Usage: /sync pull <article-path> | --all"));return}let n;if(o==="--all"){if(console.log(p("Checking Document360 for drift\u2026")),n=(await lr({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),n.length===0){console.log(A("\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 Ei({cwd:e.cwd,relPath:r});console.log(""),console.log(`${P("\u25CF")} ${he(s.title)} ${V(`(${s.path})`)}`);for(let f of s.notes)console.log(N(` \u26A0 ${f}`));s.overwritesLocalChanges&&console.log(N(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=Ne(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.")),ir({cwd:e.cwd},s);continue}let d=f=>f===1?"":"s";console.log(V(` \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(()=>Ai({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(p(" Skipped."));continue}ir({cwd:e.cwd},s),console.log(A(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}B();import{statSync as zi}from"node:fs";import{resolve as Gi}from"node:path";import{estimateBulkCost as Xi,planPartitions as Yi,readProjectConfig as Vi,resolveModelForOperation as Ki,runPartitioned as Ji,trackedArticlePaths as Qi}from"document360-engine";import F from"picocolors";import fr from"wrap-ansi";import _o from"string-width";B();import Ce from"picocolors";import jo from"wrap-ansi";import Ao from"string-width";var cr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),ur=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),dr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function Mi(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(cr(r)){o.push({kind:"hr"}),n++;continue}if(r.includes("|")&&n+1<t.length&&ur(t[n+1])){let d=dr(r);n+=2;let h=[];for(;n<t.length&&t[n].includes("|")&&t[n].trim()!=="";)h.push(dr(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])&&!cr(t[n])&&!(t[n].includes("|")&&n+1<t.length&&ur(t[n+1]));)c.push(t[n++]);o.push({kind:"para",text:c.join(" ")})}return o}function Eo(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 Ni=(e,t)=>e+" ".repeat(Math.max(0,t-Ao(e)));function Li(e,t,o){let n=e.length,r=e.map((T,I)=>Math.max(Ao(T),...t.map(q=>Ao(q[I]??"")))),s=Math.max(24,o),c=3*n+1,d=[...r],h=()=>d.reduce((T,I)=>T+I,0)+c,f=0;for(;h()>s&&f++<1e4;){let T=-1,I=6;for(let q=0;q<n;q++)d[q]>I&&(I=d[q],T=q);if(T===-1)break;d[T]-=1}let w=(T,I,q)=>Ce.gray(T+d.map(z=>"\u2500".repeat(z+2)).join(I)+q),R=Ce.gray("\u2502"),$=(T,I)=>{let q=d.map((ie,ye)=>{let le=T[ye]??"",ae=I?Ce.bold(le):Eo(le);return jo(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} ${Ni(q[le][ie]??"",ye)} `).join("")+R);return ee.join(`
18
+ `)},j=[w("\u250C","\u252C","\u2510"),$(e,!0)];return j.push(t.length===0?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524")),t.forEach((T,I)=>{j.push($(T,!1)),j.push(I===t.length-1?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524"))}),j.join(`
19
+ `)}function Oi(e,t){switch(e.kind){case"heading":return Ce.bold(e.text);case"hr":return Ce.gray("\u2500".repeat(t));case"para":return jo(Eo(e.text),t);case"list":return e.items.map(o=>{let[n="",...r]=jo(Eo(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 Ai(e.header,e.rows,t)}}function To(e,t){let o=Math.max(20,t);return Ri(e).map(n=>Ei(n,o)).join(`
23
+ `);case"table":return Li(e.header,e.rows,t)}}function Do(e,t){let o=Math.max(20,t);return Mi(e).map(n=>Oi(n,o)).join(`
24
24
 
25
- `)}B();import Di from"picocolors";var Ii=[127,86,217],_i=[22,38,43],ir={T:Ii,E:_i},at=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Ro=([e,t,o])=>`\x1B[38;2;${e};${t};${o}m`,Mi=([e,t,o])=>`\x1B[48;2;${e};${t};${o}m`;function lr(){if(!Di.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=ir[at[o][r]],c=o+1<at.length?ir[at[o+1][r]]:void 0;s&&c?n+=`${Ro(s)}${Mi(c)}\u2580\x1B[49m\x1B[39m`:s?n+=`${Ro(s)}\u2580\x1B[39m`:c?n+=`${Ro(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 Ni(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=lr();if(n.length===0)return o.join(`
26
- `);let r=2,s=3,c=jo(n[0]);if(!(t>=r+c+s+Math.max(...o.map(w=>jo(w)))))return[...n.map(w=>" "+w),...o].join(`
25
+ `)}B();import Ui from"picocolors";var Wi=[127,86,217],Fi=[22,38,43],pr={T:Wi,E:Fi},ct=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Io=([e,t,o])=>`\x1B[38;2;${e};${t};${o}m`,Bi=([e,t,o])=>`\x1B[48;2;${e};${t};${o}m`;function mr(){if(!Ui.isColorSupported)return[];let e=ct[0].length,t=[];for(let o=0;o<ct.length;o+=2){let n="";for(let r=0;r<e;r++){let s=pr[ct[o][r]],c=o+1<ct.length?pr[ct[o+1][r]]:void 0;s&&c?n+=`${Io(s)}${Bi(c)}\u2580\x1B[49m\x1B[39m`:s?n+=`${Io(s)}\u2580\x1B[39m`:c?n+=`${Io(c)}\u2584\x1B[39m`:n+=" "}t.push(n)}return t}function ut(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function Le(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Ht(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var Ve=(e,t)=>` ${e.padEnd(13)}${t}`;function Hi(e,t){let o=[nt("\u270E document360-writer")+F.gray(` v${e.version}`),F.gray(" Reads your code, writes your docs."),"",Ve("Claude:",`${e.claude}${F.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),Ve("Document360:",e.configured?e.loggedOut?F.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?F.gray(` (${e.sessionHint})`):""}`:F.yellow("not set up \u2014 run /init")),Ve("Profile:",e.configured?`${e.profile}${F.gray(` (${e.apiUrl})`)}${e.prod?F.bold(F.yellow(" \u26A0 PRODUCTION")):""}`:F.gray("\u2014 (run /init)")),Ve("Project:",e.project),Ve("Mode:",F.gray(e.mode)),Ve("cwd:",F.gray(e.cwd))],n=mr();if(n.length===0)return o.join(`
26
+ `);let r=2,s=3,c=_o(n[0]);if(!(t>=r+c+s+Math.max(...o.map(w=>_o(w)))))return[...n.map(w=>" "+w),...o].join(`
27
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 Li={error:W.red,warn:W.yellow,ok:W.green,info:W.gray};function Ao(e,t){let o=Math.max(20,t);switch(e.kind){case"banner":return Ni(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=>ar(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-jo(f))+1)+r).join(`
28
+ `)}var qi={error:F.red,warn:F.yellow,ok:F.green,info:F.gray};function Mo(e,t){let o=Math.max(20,t);switch(e.kind){case"banner":return Hi(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=>fr(f,s,{hard:!0}).split(`
30
+ `)),h=Math.max(0,d.length-c);return h>0&&(d=[...d.slice(0,c),F.dim(`\u2026 +${h} more lines`)]),`
31
+ `+d.map((f,w)=>n+(w===0?P(" \u276F "):" ")+f+" ".repeat(Math.max(0,s-_o(f))+1)+r).join(`
32
32
  `)}case"assistant":return`
33
- `+To(e.text,o);case"tool":{let n=e.arg!==null?W.gray(`${e.sep}(${e.arg})`):"";return`
34
- `+ar(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
- `)}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=>P(nt(` \u2B95 ${n}`))).join(`
33
+ `+Do(e.text,o);case"tool":{let n=e.arg!==null?F.gray(`${e.sep}(${e.arg})`):"";return`
34
+ `+fr(F.green("\u25CF ")+F.bold(e.title)+n,o)}case"tool-result":{let n=e.isError?F.red:F.gray,r=e.lines.map((s,c)=>n((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(F.dim(` \u2026 +${e.hidden} lines`)),r.join(`
35
+ `)}case"diff":{let n=c=>c===1?"":"s",s=[F.gray(` \u23BF Added ${e.added} line${n(e.added)}, removed ${e.removed} line${n(e.removed)}`),...e.lines];return e.hidden>0&&s.push(F.dim(` \u2026 +${e.hidden} more diff lines`)),s.join(`
36
+ `)}case"link":return e.lines.map(n=>P(rt(` \u2B95 ${n}`))).join(`
37
37
  `);case"preview":return`
38
- `+ot(`\u25A3 Preview \u2014 ${e.name}`)+`
38
+ `+nt(`\u25A3 Preview \u2014 ${e.name}`)+`
39
39
 
40
- `+To(e.text,o);case"note":return`
41
- `+Li[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 cr(e,t){return e.map(o=>Ao(o,t)).join(`
43
- `)}var Eo=3;function Do(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 Io(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function _o(e,t){return t.map(o=>{let n=0;try{n=Oi(Ui(e,o)).size}catch{n=0}return{path:o,bytes:n}})}function Mo(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
+ `+Do(e.text,o);case"note":return`
41
+ `+qi[e.tone](rt(e.text));case"done":return`
42
+ `+(e.ok?F.magenta("\u2736 "):F.red("\u2736 "))+F.gray(`Cooked for ${ut(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${Le(e.costUsd)}`:""))}}function gr(e,t){return e.map(o=>Mo(o,t)).join(`
43
+ `)}var No=3;function Lo(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 Oo(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function Uo(e,t){return t.map(o=>{let n=0;try{n=zi(Gi(e,o)).size}catch{n=0}return{path:o,bytes:n}})}function Wo(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 No(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 Lo(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`,$=T=>`${T} article${T===1?"":"s"}`,j=[];if(s.length===0)j.push(`Converted ${$(c)} (${R}) successfully \xB7 ${w}.`);else{j.push(`Converted ${d}/${$(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 ur(e,t){if(!Bi(t.cwd))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let{scope:o,run:n}=Do(e),r=zi(t.cwd,t.profileName);if(r.length===0)return console.log(x("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=Io(r,o);if(s.length===0)return console.log(x(`No tracked articles under "${o}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=Fi(s),d=`/convert${o?` --scope ${o}`:""} --run`,{model:h,forced:f}=Hi(t.cwd,"light");if(!n){let w=Wi({files:_o(t.cwd,s),op:"convert",model:h});o&&console.log(p(`Scope: ${o} (${s.length} of ${r.length} tracked articles).`));for(let R of No(c,w,Eo))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${Eo} 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 qi({cwd:t.cwd,partitions:c,promptFor:Mo,concurrency:Eo,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(A(` \u2713 ${w.label}`)):console.log(x(` \u2717 ${w.label}`));else if(w.type==="run_done"){console.log("");let R=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let $ of Lo(w.results,c,R))console.log(w.ok?A($):N($))}}catch(w){console.log(x(`Convert run failed: ${w.message}`))}return console.log(""),{kind:"continue"}}B();import{search as Gi}from"@inquirer/prompts";import{findByName as Xi,getSession as Vi,listSessions as Yi,relativeTime as pr}from"document360-engine";async function mr(e,t){let o=Yi(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=Xi(t.cwd,n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${n}".`)),dr(o),{kind:"continue"})}if(!process.stdin.isTTY)return dr(o),console.log(p("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>Gi({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} ${pr(d.updatedAt)}`,value:d.uuid,description:d.firstPrompt.slice(0,100)}))}})),r=Vi(n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function dr(e){console.log("");for(let t of e.slice(0,15))console.log(` ${P(t.name)} ${p(pr(t.updatedAt))}`),console.log(` ${p(t.firstPrompt.slice(0,80))}`);console.log("")}B();import{renameSession as Ki}from"document360-engine";async function fr(e,t){let o=e.join(" ").trim();if(!o)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(Ki(n,o)?console.log(A(`\u2713 Session renamed to "${o}"`)):console.log(x("Could not find the current session record.")),{kind:"continue"}):(console.log(x("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as gr,readProjectConfig as Ji,writeProjectConfig as Qi}from"document360-engine";B();function Oo(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=Ji(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 gr().includes(r)?(n.profiles={...n.profiles,[t]:{connection:{environment:r},production:!1}},Qi(n,e),null):`Unknown environment "${r}". Known: ${gr().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function hr(e,t){let o=e[0];if(!o)return Dt(t.cwd),{kind:"continue"};if(o==="add"){let n=Oo(t.cwd,e[1],e[2]);return n?(console.log(x(n)),{kind:"continue"}):(console.log(A(`\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"}}B();import{select as Zi}from"@inquirer/prompts";import{readProjectConfig as el,readUserConfig as kr,resolveModelSetting as Uo,writeUserConfig as yr}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 tl(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=Uo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=kr();return s.defaultModel?(delete s.defaultModel,yr(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Uo(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}}yr({...kr(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=el(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 wr(e,t){let o=e[0]?.trim();if(!o){let c=Uo(t.cwd);if(!process.stdin.isTTY)return console.log(`${he("Model:")} ${P(c.model??"Claude Code default")}`),console.log(p(` source: ${tl(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(($,j)=>({name:`${$.label}${j===d?" \u2714":""}`,value:$.value,description:$.desc}))}))}catch{return console.log(p("Cancelled.")),{kind:"continue"}}let{lines:f,changed:w,effective:R}=ut(t.cwd,h??"default");for(let $ of f)console.log($.startsWith("\u26A0")?N($):$.startsWith("\u2713")?A($):p($));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")?N(c):c.startsWith("\u2713")?A(c):p(c));return r&&await t.setModel(s),{kind:"continue"}}qt();async function Cr(e,t){return await t.withPausedInput(()=>Ge(t.cwd)),{kind:"clear"}}B();import{resolveActiveProfile as pl}from"document360-engine";async function Pr(e,t){let o=!1;try{o=pl(t.cwd).production}catch{}return o?(console.log(N("\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"})}B();var Sr=async(e,t)=>{try{await t.withPausedInput(()=>Et({}))}catch(o){console.log(x(`Login failed: ${o.message}`))}return{kind:"continue"}};import{existsSync as Tr}from"node:fs";import{isAbsolute as ml,join as fl,resolve as gl}from"node:path";import{readProjectConfig as hl,screenshotPlaceholderIds as kl}from"document360-engine";var Rr=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Fo(e){if(e[0]==="--list")return{mode:"list",scope:e[1]?Rr(e[1]):void 0};let t=e[0];return!t||t==="--all"?{mode:"all"}:/[\\/]/.test(t)||t.endsWith(".md")?{mode:"scope",scope:Rr(t)}:{mode:"single",id:t}}function Bo(e,t){let o=hl(e),n=o?.captureDir??"user-docs/_capture",r=o?.outputDir??"user-docs/_screenshots",s=(c,d)=>ml(c)?fl(c,d):gl(e,c,d);return kl(e,{scope:t}).map(({id:c,file:d})=>{let h=Tr(s(r,`${c}.png`))?"captured":Tr(s(n,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:d,state:h}})}var yl={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},wl={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Ho(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(` ${yl[c.state]} ${c.id.padEnd(34)} ${wl[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function jr(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 Fo(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: ${Le(r)}\u2013${Le(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function Bo(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,I)=>T+n(I),0),d=r.reduce((T,I)=>T+n(I),0),h=e.reduce((T,I)=>T+I.costUsd,0),f=e.reduce((T,I)=>T+I.outputTokens,0),w=o==="api"?`${Le(h)} total`:Ht(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 ${w}.`);else{j.push(`Converted ${d}/${$(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 hr(e,t){if(!Vi(t.cwd))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let{scope:o,run:n}=Lo(e),r=Qi(t.cwd,t.profileName);if(r.length===0)return console.log(x("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=Oo(r,o);if(s.length===0)return console.log(x(`No tracked articles under "${o}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=Yi(s),d=`/convert${o?` --scope ${o}`:""} --run`,{model:h,forced:f}=Ki(t.cwd,"light");if(!n){let w=Xi({files:Uo(t.cwd,s),op:"convert",model:h});o&&console.log(p(`Scope: ${o} (${s.length} of ${r.length} tracked articles).`));for(let R of Fo(c,w,No))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${No} 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 Ji({cwd:t.cwd,partitions:c,promptFor:Wo,concurrency:No,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(A(` \u2713 ${w.label}`)):console.log(x(` \u2717 ${w.label}`));else if(w.type==="run_done"){console.log("");let R=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let $ of Bo(w.results,c,R))console.log(w.ok?A($):N($))}}catch(w){console.log(x(`Convert run failed: ${w.message}`))}return console.log(""),{kind:"continue"}}B();import{search as Zi}from"@inquirer/prompts";import{findByName as el,getSession as tl,listSessions as ol,relativeTime as yr}from"document360-engine";async function wr(e,t){let o=ol(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=el(t.cwd,n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${n}".`)),kr(o),{kind:"continue"})}if(!process.stdin.isTTY)return kr(o),console.log(p("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>Zi({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} ${yr(d.updatedAt)}`,value:d.uuid,description:d.firstPrompt.slice(0,100)}))}})),r=tl(n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function kr(e){console.log("");for(let t of e.slice(0,15))console.log(` ${P(t.name)} ${p(yr(t.updatedAt))}`),console.log(` ${p(t.firstPrompt.slice(0,80))}`);console.log("")}B();import{renameSession as nl}from"document360-engine";function Ho(e){let t=e.trim();return t.length>=2&&(t[0]==='"'&&t.endsWith('"')||t[0]==="'"&&t.endsWith("'"))?t.slice(1,-1).trim():t}async function xr(e,t){let o=Ho(e.join(" "));if(!o)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(nl(n,o)?console.log(A(`\u2713 Session renamed to "${o}"`)):console.log(x("Could not find the current session record.")),{kind:"continue"}):(console.log(x("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as $r,readProjectConfig as rl,writeProjectConfig as sl}from"document360-engine";B();function qo(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=rl(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 $r().includes(r)?(n.profiles={...n.profiles,[t]:{connection:{environment:r},production:!1}},sl(n,e),null):`Unknown environment "${r}". Known: ${$r().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function br(e,t){let o=e[0];if(!o)return It(t.cwd),{kind:"continue"};if(o==="add"){let n=qo(t.cwd,e[1],e[2]);return n?(console.log(x(n)),{kind:"continue"}):(console.log(A(`\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 _t(t.cwd,o),Mt(t.cwd,o),console.log(p(" Restarting agent for the new profile\u2026")),{kind:"clear"}}B();import{select as il}from"@inquirer/prompts";import{readProjectConfig as ll,readUserConfig as vr,resolveModelSetting as zo,writeUserConfig as Cr}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 qt(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 al(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 dt(e,t){let o=()=>{let s=zo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=vr();return s.defaultModel?(delete s.defaultModel,Cr(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${zo(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({...vr(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=ll(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 Pr(e,t){let o=e[0]?.trim();if(!o){let c=zo(t.cwd);if(!process.stdin.isTTY)return console.log(`${he("Model:")} ${P(c.model??"Claude Code default")}`),console.log(p(` source: ${al(c)}`)),console.log(p(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let d=qt(c),h;try{h=await t.withPausedInput(()=>il({message:`Select model (current: ${c.model??"Claude Code default"})`,default:de[d].value,choices:de.map(($,j)=>({name:`${$.label}${j===d?" \u2714":""}`,value:$.value,description:$.desc}))}))}catch{return console.log(p("Cancelled.")),{kind:"continue"}}let{lines:f,changed:w,effective:R}=dt(t.cwd,h??"default");for(let $ of f)console.log($.startsWith("\u26A0")?N($):$.startsWith("\u2713")?A($):p($));return w&&await t.setModel(R),{kind:"continue"}}let{lines:n,changed:r,effective:s}=dt(t.cwd,o);for(let c of n)console.log(c.startsWith("\u26A0")?N(c):c.startsWith("\u2713")?A(c):p(c));return r&&await t.setModel(s),{kind:"continue"}}zt();async function Ar(e,t){return await t.withPausedInput(()=>Ye(t.cwd)),{kind:"clear"}}B();import{resolveActiveProfile as wl}from"document360-engine";async function Er(e,t){let o=!1;try{o=wl(t.cwd).production}catch{}return o?(console.log(N("\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"})}B();var Dr=async(e,t)=>{try{await t.withPausedInput(()=>Dt({}))}catch(o){console.log(x(`Login failed: ${o.message}`))}return{kind:"continue"}};import{existsSync as Ir}from"node:fs";import{isAbsolute as xl,join as $l,resolve as bl}from"node:path";import{readProjectConfig as vl,screenshotPlaceholderIds as Cl}from"document360-engine";var _r=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Xo(e){if(e[0]==="--list")return{mode:"list",scope:e[1]?_r(e[1]):void 0};let t=e[0];return!t||t==="--all"?{mode:"all"}:/[\\/]/.test(t)||t.endsWith(".md")?{mode:"scope",scope:_r(t)}:{mode:"single",id:t}}function Yo(e,t){let o=vl(e),n=o?.captureDir??"user-docs/_capture",r=o?.outputDir??"user-docs/_screenshots",s=(c,d)=>xl(c)?$l(c,d):bl(e,c,d);return Cl(e,{scope:t}).map(({id:c,file:d})=>{let h=Ir(s(r,`${c}.png`))?"captured":Ir(s(n,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:d,state:h}})}var Pl={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},Sl={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Vo(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(` ${Pl[c.state]} ${c.id.padEnd(34)} ${Sl[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function Mr(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 zt(e,t){let o=Fo(e);if(o.mode==="list"){let c=t?.cwd??process.cwd();for(let d of Ho(Bo(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(`
51
+ `)}async function Gt(e,t){let o=Xo(e);if(o.mode==="list"){let c=t?.cwd??process.cwd();for(let d of Vo(Yo(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 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"}}B();import{existsSync as xl,rmSync as $l}from"node:fs";import{basename as Ar,resolve as Er}from"node:path";import{input as bl}from"@inquirer/prompts";import{readProjectConfig as vl}from"document360-engine";var Cl=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function qo(e){let t=null;try{t=vl(e)}catch{t=null}let o=s=>s?.replace(/\\/g,"/").replace(/\/+$/,""),n=o(t?.docsDir)??"user-docs",r=new Set([n]);for(let s of[o(t?.captureDir),o(t?.outputDir)])s&&s!==n&&!s.startsWith(`${n}/`)&&r.add(s);return[...r,...Cl].filter(s=>xl(Er(e,s)))}function zo(e,t){let o=[],n=[];for(let r of t)try{$l(Er(e,r),{recursive:!0,force:!0}),o.push(r)}catch(s){n.push({path:r,error:s.message})}return{removed:o,failed:n}}function Go(e,t){return t.length===0?["Nothing to reset \u2014 no d360-writer files found in this repo."]:["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(o=>` \u2022 ${o}`),"","Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${Ar(e)}`]}async function Dr(e,t){let o=qo(t.cwd);for(let d of Go(t.cwd,o))console.log(o.length===0?p(d):d);if(o.length===0)return{kind:"continue"};let n=Ar(t.cwd);if((await t.withPausedInput(()=>bl({message:`Type "${n}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==n)return console.log(N("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=zo(t.cwd,o);console.log(A(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let d of c)console.log(x(` \u2717 ${d.path}: ${d.error}`));return{kind:"continue"}}var Ir={help:yo,"?":yo,clear:Nn,exit:wo,quit:wo,init:Fn,mcp:Nt,publish:Xn,audit:Ut,scope:Yn,sync:or,convert:ur,resume:mr,rename:fr,profile:hr,model:wr,doctor:dt,workspace:Cr,"allow-prod":Pr,login:Sr,screenshot:zt,"capture-setup":Gt,reset:Dr};function _r(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 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"},Nr=160,Vo=200,Lr=40;function Z(e,t){let o=e.replace(/\s+/g," ").trim();return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function Mr(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),Lr)}`);return t.length>4&&o.push("\u2026"),Z(o.join(", "),Nr)}var Pe=e=>typeof e=="string"&&e?e:null,Xo=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Sl(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 Tl(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=Sl(t.local_path);return o("Create article",`"${Z(n,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let n=Pe(t.title),r=Xo(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)",Xo(t.article_id)?`id ${Xo(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=Tl(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:Mr(t)}}let o=Pl[e],n=o?t[o]:void 0;return typeof n=="string"&&n?{title:e,sep:"",arg:Z(n,Nr)}:{title:e,sep:"",arg:Mr(t)}}function Rl(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 jl(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let o=t.map(Rl).filter(r=>r!==null),n=`${t.length} item${t.length===1?"":"s"}`;return o.length===0?[n]:[n,...o.map(r=>Z(r,Vo))]}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),Lr)}`);return o.length>0?[Z(o.join(" \xB7 "),Vo)]:null}return null}function Al(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 El(e,t,o){switch(e){case"d360_create_article":{let n=Al(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=El(o.slice(18),n,r);if(c)return{lines:c,hidden:0}}let s=jl(r)??r.split(`
56
- `);return{lines:s.slice(0,t).map(c=>Z(c,Vo)),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 Hl=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function ql(e,t,o,n){if(Hl.test(e))try{let r=Fr(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 Br(e=process.cwd(),t="auto",o){let n=Il(t);n.kind==="none"&&(console.error(""),console.error(x("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)),zl(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=Yo({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},d=Dl({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 $={cwd:e,profileName:o,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=Yo({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 _=_r(T);if(!_)continue;let q=Ir[_.name];if(!q){console.log(x(`Unknown command: /${_.name}`)),console.log(p("Type /help for the list."));continue}let z=await q(_.args,$);if(z.kind==="exit")break;if(z.kind==="clear"){$.restartAgent();continue}if(z.kind==="allow-prod"){r=!0,$.restartAgent(),console.log(A("\u2713 Production writes authorized for this session.")),console.log("");continue}if(z.kind==="resume"){s.close(),s=Yo({cwd:e,resume:z.uuid,profileName:o,allowProdWrites:r});let ee=_l(z.uuid);c={uuid:z.uuid,firstPrompt:ee?.firstPrompt??null,titleFired:!0},Wr(z.uuid),console.log(A(`\u2713 Resumed "${z.name}"`)),console.log("");continue}z.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=z.display??z.prompt),await Ur(s,z.prompt,n,c,e));continue}c.firstPrompt||(c.firstPrompt=T),await Ur(s,T,n,c,e)}}}finally{s.close(),d.close()}}function zl(e,t){console.log(""),console.log(ot("document360-writer")),console.log(p(` cwd: ${e}`));let o=Ul(e);console.log(p(` model: ${o.model??"auto (engine right-sizes per task)"}${o.model?` (${o.source})`:""}`)),console.log(Gl(e,t)),Wl(e)||console.log(N(" 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 Gl(e,t){try{let o=Fr(e,t),n=o.production?N(" \u26A0 PRODUCTION"):"",r=Bl(o.name);if(!r)return p(` Document360: profile "${o.name}"${n} \u2014 not logged in (d360-writer login)`);let s={...Or(r.idToken)??{},...Or(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return Fl(r)&&!r.refreshToken?N(` 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 Xl(){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 Vl(e,t,o){e.uuid=t;let n=new Date().toISOString();Ll({uuid:t,name:Nl(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:o,firstPrompt:e.firstPrompt??"",createdAt:n,updatedAt:n})}function Yl(e,t){e.titleFired=!0;let o=e.uuid,n=e.firstPrompt;!o||!n||Ol(n,t).then(r=>{r&&Ml(o,r)}).catch(()=>{})}async function Ur(e,t,o,n,r){let s=new Map;for await(let c of e.send(t))Kl(c,n,r,o,s)}function Kl(e,t,o,n,r){switch(e.type){case"session":t.uuid||Vl(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(`
53
+ `),s=o.mode==="scope"?`/screenshot ${o.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}async function Xt(){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"}}B();import{existsSync as Tl,rmSync as Rl}from"node:fs";import{basename as Nr,resolve as Lr}from"node:path";import{input as jl}from"@inquirer/prompts";import{readProjectConfig as Al}from"document360-engine";var El=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function Ko(e){let t=null;try{t=Al(e)}catch{t=null}let o=s=>s?.replace(/\\/g,"/").replace(/\/+$/,""),n=o(t?.docsDir)??"user-docs",r=new Set([n]);for(let s of[o(t?.captureDir),o(t?.outputDir)])s&&s!==n&&!s.startsWith(`${n}/`)&&r.add(s);return[...r,...El].filter(s=>Tl(Lr(e,s)))}function Jo(e,t){let o=[],n=[];for(let r of t)try{Rl(Lr(e,r),{recursive:!0,force:!0}),o.push(r)}catch(s){n.push({path:r,error:s.message})}return{removed:o,failed:n}}function Qo(e,t){return t.length===0?["Nothing to reset \u2014 no d360-writer files found in this repo."]:["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(o=>` \u2022 ${o}`),"","Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${Nr(e)}`]}async function Or(e,t){let o=Ko(t.cwd);for(let d of Qo(t.cwd,o))console.log(o.length===0?p(d):d);if(o.length===0)return{kind:"continue"};let n=Nr(t.cwd);if((await t.withPausedInput(()=>jl({message:`Type "${n}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==n)return console.log(N("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=Jo(t.cwd,o);console.log(A(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let d of c)console.log(x(` \u2717 ${d.path}: ${d.error}`));return{kind:"continue"}}var Ur={help:vo,"?":vo,clear:Bn,exit:Co,quit:Co,init:Xn,mcp:Lt,publish:Zn,audit:Wt,scope:tr,sync:ar,convert:hr,resume:wr,rename:xr,profile:br,model:Pr,doctor:pt,workspace:Ar,"allow-prod":Er,login:Dr,screenshot:Gt,"capture-setup":Xt,reset:Or};function Wr(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 Dl={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"},Br=160,en=200,Hr=40;function Z(e,t){let o=e.replace(/\s+/g," ").trim();return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function Fr(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),Hr)}`);return t.length>4&&o.push("\u2026"),Z(o.join(", "),Br)}var Pe=e=>typeof e=="string"&&e?e:null,Zo=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Il(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 _l(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=Il(t.local_path);return o("Create article",`"${Z(n,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let n=Pe(t.title),r=Zo(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)",Zo(t.article_id)?`id ${Zo(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 Yt(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let f=_l(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:Fr(t)}}let o=Dl[e],n=o?t[o]:void 0;return typeof n=="string"&&n?{title:e,sep:"",arg:Z(n,Br)}:{title:e,sep:"",arg:Fr(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 Nl(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,en))]}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),Hr)}`);return o.length>0?[Z(o.join(" \xB7 "),en)]:null}return null}function Ll(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 Ol(e,t,o){switch(e){case"d360_create_article":{let n=Ll(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=Ol(o.slice(18),n,r);if(c)return{lines:c,hidden:0}}let s=Nl(r)??r.split(`
56
+ `);return{lines:s.slice(0,t).map(c=>Z(c,en)),hidden:Math.max(0,s.length-t)}}function Kt(e,t,o,n="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${n}/${o}`}function Jt(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 Qt(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 Kl=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Jl(e,t,o,n){if(Kl.test(e))try{let r=Xr(n),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=Jt(t,o),d=Qt(o);e.endsWith("publish_article")&&d&&console.log(P(` \u2B95 Live: ${d}`)),c&&s&&console.log(P(` \u2B95 Preview: ${Kt(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function Yr(e=process.cwd(),t="auto",o){let n=Wl(t);n.kind==="none"&&(console.error(""),console.error(x("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)),Ql(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=tn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},d=Ul({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 $={cwd:e,profileName:o,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=tn({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 I=Wr(T);if(!I)continue;let q=Ur[I.name];if(!q){console.log(x(`Unknown command: /${I.name}`)),console.log(p("Type /help for the list."));continue}let z=await q(I.args,$);if(z.kind==="exit")break;if(z.kind==="clear"){$.restartAgent();continue}if(z.kind==="allow-prod"){r=!0,$.restartAgent(),console.log(A("\u2713 Production writes authorized for this session.")),console.log("");continue}if(z.kind==="resume"){s.close(),s=tn({cwd:e,resume:z.uuid,profileName:o,allowProdWrites:r});let ee=Fl(z.uuid);c={uuid:z.uuid,firstPrompt:ee?.firstPrompt??null,titleFired:!0},Gr(z.uuid),console.log(A(`\u2713 Resumed "${z.name}"`)),console.log("");continue}z.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=z.display??z.prompt),await zr(s,z.prompt,n,c,e));continue}c.firstPrompt||(c.firstPrompt=T),await zr(s,T,n,c,e)}}}finally{s.close(),d.close()}}function Ql(e,t){console.log(""),console.log(nt("document360-writer")),console.log(p(` cwd: ${e}`));let o=Gl(e);console.log(p(` model: ${o.model??"auto (engine right-sizes per task)"}${o.model?` (${o.source})`:""}`)),console.log(Zl(e,t)),Xl(e)||console.log(N(" 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 Zl(e,t){try{let o=Xr(e,t),n=o.production?N(" \u26A0 PRODUCTION"):"",r=Vl(o.name);if(!r)return p(` Document360: profile "${o.name}"${n} \u2014 not logged in (d360-writer login)`);let s={...qr(r.idToken)??{},...qr(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return Yl(r)&&!r.refreshToken?N(` 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 ea(){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 ta(e,t,o){e.uuid=t;let n=new Date().toISOString();ql({uuid:t,name:Hl(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:o,firstPrompt:e.firstPrompt??"",createdAt:n,updatedAt:n})}function oa(e,t){e.titleFired=!0;let o=e.uuid,n=e.firstPrompt;!o||!n||zl(n,t).then(r=>{r&&Bl(o,r)}).catch(()=>{})}async function zr(e,t,o,n,r){let s=new Map;for await(let c of e.send(t))na(c,n,r,o,s)}function na(e,t,o,n,r){switch(e.type){case"session":t.uuid||ta(t,e.sessionId,o);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=Yt(e.name,e.input);s&&(process.stdout.write(`
57
57
 
58
- `),console.log(`${A("\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?x: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||ql(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&&(Wr(t.uuid),t.titleFired||Yl(t,o));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),n.kind==="subscription"&&e.kind==="auth"&&Xl();break}}import{render as Ua}from"ink";import{resolveAuth as Wa}from"document360-engine";import{useCallback as G,useEffect as Ve,useMemo as eo,useRef as X,useState as U}from"react";import{Box as V,Text as v,useApp as Zl,useInput as ea,useStdout as ta}from"ink";import{existsSync as oa,readFileSync as Zr,readdirSync as na}from"node:fs";import{basename as Ko,isAbsolute as ra,join as oo}from"node:path";import{createSession as es,loginPkce as sa,toStoredTokens as ia,saveTokens as la,getAccessToken as aa,resolveActiveProfile as pe,resolveProjectId as ca,getArticle as ua,decodeJwtClaims as Ye,isExpired as Se,loadTokens as Le,setTitle as da,slugify as pa,touchSession as Jo,upsertSession as ma,generateTitle as ts,findByName as fa,listSessions as ga,renameSession as ha,suggestNextAction as ka,readProjectConfig as se,writeProjectConfig as ya,resolveModelSetting as en,loadProfileMap as os,applyPull as wa,computeSyncStatus as Qo,planPull as xa,inventoryRepo as $a,knownEnvironments as ba,resolveEnvironment as va,planPartitions as Ca,partitionEvenly as Pa,screenshotPlaceholderIds as Sa,trackedArticlePaths as Ta,runPartitioned as ns,estimateBulkCost as Ra,resolveModelForOperation as rs}from"document360-engine";qt();var Jl=/^(?:\d+[.)]|[-*•])\s+/;function Hr(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(Jl,""),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}B();var Ql=/\[Pasted text #\d+ \+\d+ lines?\]/g;function qr(e){return e.replace(/\r\n?/g,`
61
- `)}function zr(e){return e.includes(`
62
- `)||e.length>200}function Gr(e,t){let o=t.split(`
63
- `).length;return`[Pasted text #${e} +${o} line${o===1?"":"s"}]`}function Xr(e,t){return e.replace(Ql,o=>t.get(o)??o)}function Vr(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 Yr(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 Kr(e,t,o){let n=e[Zt(e,t)];return o==="start"?n.start:n.end}function Jr(e,t,o){let n=Math.max(1,o),r=e.split(`
58
+ `),console.log(`${A("\u25CF")} ${he(s.title)}${s.arg!==null?V(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=Ne(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=d=>d===1?"":"s";console.log(V(` \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?x:V;c.lines.forEach((h,f)=>console.log(d((f===0?" \u23BF ":" ")+h))),c.hidden>0&&console.log(p(` \u2026 +${c.hidden} lines`)),e.isError||Jl(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&&(Gr(t.uuid),t.titleFired||oa(t,o));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),n.kind==="subscription"&&e.kind==="auth"&&ea();break}}import{render as Ya}from"ink";import{resolveAuth as Va}from"document360-engine";import{useCallback as G,useEffect as Te,useMemo as ro,useRef as X,useState as O}from"react";import{Box as Y,Text as v,useApp as aa,useInput as ca,useStdout as ua}from"ink";import{existsSync as da,readFileSync as is,readdirSync as pa}from"node:fs";import{basename as so,isAbsolute as ma,join as lo}from"node:path";import{createSession as ls,loginPkce as fa,toStoredTokens as ga,saveTokens as ha,getAccessToken as ka,resolveActiveProfile as pe,resolveProjectId as ya,getArticle as wa,decodeJwtClaims as Ke,isExpired as Se,loadTokens as Oe,setTitle as xa,slugify as $a,touchSession as on,upsertSession as ba,generateTitle as as,findByName as va,listSessions as Ca,renameSession as Pa,suggestNextAction as Sa,readProjectConfig as se,writeProjectConfig as Ta,resolveModelSetting as sn,loadProfileMap as cs,applyPull as Ra,computeSyncStatus as nn,planPull as ja,inventoryRepo as Aa,knownEnvironments as Ea,resolveEnvironment as Da,planPartitions as Ia,partitionEvenly as _a,screenshotPlaceholderIds as Ma,trackedArticlePaths as Na,runPartitioned as us,estimateBulkCost as La,resolveModelForOperation as ds}from"document360-engine";var ra=e=>`\x1B]0;${e}\x07`,sa=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function Vr(e){process.stdout.isTTY&&process.stdout.write(e)}function Zt(e){Vr(ra(e))}function eo(e){Vr(sa(e))}var to=["\xB7 ","\xB7\xB7 ","\xB7\xB7\xB7"];zt();var ia=/^(?:\d+[.)]|[-*•])\s+/;function Kr(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(ia,""),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}B();var la=/\[Pasted text #\d+ \+\d+ lines?\]/g;function Jr(e){return e.replace(/\r\n?/g,`
61
+ `)}function Qr(e){return e.includes(`
62
+ `)||e.length>200}function Zr(e,t){let o=t.split(`
63
+ `).length;return`[Pasted text #${e} +${o} line${o===1?"":"s"}]`}function es(e,t){return e.replace(la,o=>t.get(o)??o)}function ts(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function oo(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 no(e,t){let o=0;for(let n=0;n<e.length&&e[n].start<=t;n++)o=n;return o}function os(e,t,o){let n=no(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 ns(e,t,o){let n=e[no(e,t)];return o==="start"?n.start:n.end}function rs(e,t,o){let n=Math.max(1,o),r=e.split(`
65
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 Qr(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 Zo,jsx as C,jsxs as O}from"react/jsx-runtime";var ja={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function Aa(e,t,o,n){let r=o.kind==="api"?"API key":o.kind==="subscription"?"subscription":"not configured",s=en(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:ja[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 $={...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 Ea(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 ss=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],no=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],Da="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 Ia=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function is(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 ls(e){try{return na(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function Ma(e,t){let o=t??[];return o.length===0?["src","api","services","packages","modules"].some(n=>ls(oo(e,n))):o.some(n=>!n.includes("/")&&!n.endsWith(".md")&&ls(oo(e,n)))}function Na({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=ss[Math.floor(o/16)%ss.length],c=Math.floor((Date.now()-e)/1e3),d=Math.round(t/4);return O(V,{children:[C(v,{color:H,children:` ${r} ${s}\u2026 `}),C(v,{color:"gray",children:`(${ct(c)} \xB7 ~${d} tokens \xB7 esc to interrupt)`})]})}var La=12e4;function Oa({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>=La;return O(V,{flexDirection:"column",children:[O(V,{children:[C(v,{color:H,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 as({cwd:e,auth:t,profileName:o,version:n}){let{exit:r}=Zl(),[s,c]=U(o),[d,h]=U(null),[f,w]=U({text:"",pos:0}),R=f.text,$=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=Vr(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=X(0),[ps,ro]=U(!1),[ms,mt]=U(0),Je=X(0),[Qe,Oe]=U(0),[Ue,ft]=U(null),nn=X(new Map),fs=X(0),so=X([]),te=X(null);te.current===null&&(te.current=es({cwd:e,profileName:s,allowProdWrites:!1}));let Te=X({uuid:null,firstPrompt:null,titleFired:!1}),gt=X(new Map),ht=X(!1),io=X([]),we=X([]),lo=X(null),[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),[rn,ao]=U([]),Ee=X([]),kt=X(!1),De=X(null),[sn,ce]=U(null),yt=X(!1),wt=X(null),[ne,Ie]=U(null),[ln,xt]=U(0),[an,$t]=U(0),[gs,cn]=U(0),co=eo(()=>{try{return en(e).model??"auto"}catch{return null}},[e,gs]),{stdout:ue}=ta(),[,hs]=U(0),un=X(`${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(Ao(a,$e()))},[$e]);Ve(()=>{if(u({kind:"banner",info:Aa(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:
66
+ `),truncated:c>0||h>0}}}return{text:e,truncated:!1}}function ss(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 rn,jsx as C,jsxs as U}from"react/jsx-runtime";var Oa={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function Ua(e,t,o,n){let r=o.kind==="api"?"API key":o.kind==="subscription"?"subscription":"not configured",s=sn(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:Oa[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=Oe(w.name);if(R){let $={...Ke(R.idToken)??{},...Ke(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 Wa(e,t){try{let o=pe(e,t),n=Oe(o.name);if(!n)return{text:`profile "${o.name}" \u2014 not logged in (/login)`,prod:o.production};let r={...Ke(n.idToken)??{},...Ke(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 ps=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],ao=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],Fa="Ask me to write or update an article\u2026";function io({ch:e,dim:t}){let[o,n]=O(!0);return Te(()=>{let r=setInterval(()=>n(s=>!s),530);return()=>clearInterval(r)},[]),C(v,{inverse:o,color:t&&!o?"gray":void 0,children:e})}var Ba=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function ms(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var Ha=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function fs(e){try{return pa(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function qa(e,t){let o=t??[];return o.length===0?["src","api","services","packages","modules"].some(n=>fs(lo(e,n))):o.some(n=>!n.includes("/")&&!n.endsWith(".md")&&fs(lo(e,n)))}function za({startTime:e,chars:t}){let[o,n]=O(0);Te(()=>{let h=setInterval(()=>n(f=>f+1),120);return()=>clearInterval(h)},[]);let r=ao[o%ao.length],s=ps[Math.floor(o/16)%ps.length],c=Math.floor((Date.now()-e)/1e3),d=Math.round(t/4);return U(Y,{children:[C(v,{color:H,children:` ${r} ${s}\u2026 `}),C(v,{color:"gray",children:`(${ut(c)} \xB7 ~${d} tokens \xB7 esc to interrupt)`})]})}var Ga=12e4;function Xa({p:e}){let[t,o]=O(0);Te(()=>{let h=setInterval(()=>o(f=>f+1),150);return()=>clearInterval(h)},[]);let n=ao[t%ao.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>=Ga;return U(Y,{flexDirection:"column",children:[U(Y,{children:[C(v,{color:H,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 ${ut(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 ${ut(s)} \u2014 press esc to stop if it's wedged`})]})}function gs({cwd:e,auth:t,profileName:o,version:n}){let{exit:r}=aa(),[s,c]=O(o),[d,h]=O(null),[f,w]=O({text:"",pos:0}),R=f.text,$=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=ts(l)??l.slice(0,-1);return{text:i+a.text.slice(a.pos),pos:i.length}})},[]),[I,q]=O(!1),[z,ee]=O(!1),[ie,ye]=O(0),[le,ae]=O(null),[Je,K]=O([]),mt=X(0),[ws,co]=O(!1),[xs,ft]=O(0),Qe=X(0),[Ze,Ue]=O(0),[We,gt]=O(null),cn=X(new Map),$s=X(0),uo=X([]),te=X(null);te.current===null&&(te.current=ls({cwd:e,profileName:s,allowProdWrites:!1}));let Re=X({uuid:null,firstPrompt:null,titleFired:!1}),ht=X(new Map),kt=X(!1),po=X([]),we=X([]),mo=X(null),[un,Fe]=O(null),[je,Ae]=O(null),[me,Be]=O(null),[xe,He]=O(null),[J,qe]=O(null),[fe,ze]=O(null),[oe,Ee]=O(null),[Q,Ge]=O(null),[dn,fo]=O([]),De=X([]),yt=X(!1),Ie=X(null),[pn,ce]=O(null),wt=X(!1),xt=X(null),[ne,_e]=O(null),[mn,$t]=O(0),[fn,bt]=O(0),[bs,gn]=O(0),go=ro(()=>{try{return sn(e).model??"auto"}catch{return null}},[e,bs]),{stdout:ue}=ua(),[,vs]=O(0),hn=X(`${ue.columns??80}x${ue.rows??24}`),$e=G(()=>Math.max(20,(ue.columns??80)-1),[ue]),u=G(a=>{uo.current.push(a),console.log(Mo(a,$e()))},[$e]);Te(()=>{let a=`d360-writer \xB7 ${un??so(e)}`;if(!I){eo(1),Zt(a);return}eo(3);let l=0;Zt(`${to[0]} ${a}`);let i=setInterval(()=>{l=(l+1)%to.length,Zt(`${to[l]} ${a}`)},450);return()=>clearInterval(i)},[I,e,un]),Te(()=>()=>eo(0),[]),Te(()=>{if(u({kind:"banner",info:Ua(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=pe(e,s),l=Le(a.name);l&&Se(l)&&l.refreshToken?aa({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)&&Ma(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!==un.current&&(un.current=k,hs(m=>m+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+cr(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),dn=eo(()=>Ea(e,s),[e,s,ie]),uo=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:Da,isSetup:!1}},[e,s,ie]),Ze=_n(R),pn=Ze.length>0&&!_,po=d!==null?Jr(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=es({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]),mn=G((a,l,i)=>{if(Ia.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=[],E=Jt(i);a.endsWith("publish_article")&&E&&y.push(`Live: ${E}`),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,E=()=>{y||(y=setTimeout(()=>{y=null,h(g.length>0?g:null)},60))},I=()=>{y&&clearTimeout(y),y=null,h(null)},L=()=>{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 b=new Date().toISOString();ma({uuid:S.sessionId,name:pa(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=Qr(g);if(b>0){let D=g.slice(0,b).trimEnd();D&&u({kind:"assistant",text:D}),g=g.slice(b)}E(),mt(D=>D+S.delta.length)}else if(S.type==="tool"){let b=Xt(S.name,S.input);b&&(L(),u({kind:"tool",title:b.title,sep:b.sep,arg:b.arg}),gt.current.set(S.id,{name:S.name,input:S.input}))}else if(S.type==="article_diff"){let b=Me(S.oldContent,S.newContent,$e());b&&(L(),u({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)&&(yt.current=!0);let b=gt.current.get(S.id);if(b){gt.current.delete(S.id),L();let D=Vt(S.output,4,S.isError?void 0:b.name,b.input);u({kind:"tool-result",lines:D.lines,hidden:D.hidden,isError:S.isError}),S.isError||mn(b.name,b.input,S.output)}}else if(S.type==="result"){L(),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 b=S.ok?Hr(m,rt.map(D=>D.name)):[];if(b.length>0?K(b):S.ok&&m.trim()&&ka(a,m,e).then(D=>{D&&pt.current===i&&ae(D)}).catch(()=>{}),k.uuid&&(Jo(k.uuid),!k.titleFired)){k.titleFired=!0;let D=k.uuid,M=k.firstPrompt;M&&ts(M,e).then(F=>F&&da(D,F)).catch(()=>{})}}else S.type==="error"&&(L(),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=[],ao([])),u({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,u,mn,$e]),mo=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"},[]),fo=G((a,l)=>{if(l){let i=se(e);i&&(i.defaultProfile=a,ya(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.`}),mo(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,mo]),fn=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(),os(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]),gn=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{wa({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]),hn=G(a=>{let l=lo.current;if(!l)return!1;if(lo.current=null,a.trim()!==l.repoName)return u({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:i,failed:k}=zo(e,l.targets);u({kind:"note",tone:k.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${i.length} item${i.length===1?"":"s"}. The repo is back to its original state.`});for(let m of k)u({kind:"note",tone:"error",text:` \u2717 ${m.path}: ${m.error}`});return!0},[e,u]),kn=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 sa(m.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},E=>u({kind:"note",tone:"info",text:E})),y=ia(m.name,g);la(y),ko(y,m.name,E=>u({kind:"note",tone:"info",text:E})),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??"";ts(y,e).then(E=>{E?(ae(`/rename ${E}`),u({kind:"note",tone:"info",text:`Suggestion: "${E}" \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}ha(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 E=y.map(([S,b])=>({name:S,env:b.connection?.environment??"custom",prod:b.production===!0,who:mo(S)})),I=s??g?.defaultProfile,L=Math.max(0,E.findIndex(S=>S.name===I));We({cursor:L,current:L,rows:E});return}if(m==="add"){let y=Oo(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]}).`}),fo(k[1],!1);return}if(!g?.profiles?.[m]){u({kind:"note",tone:"error",text:`Unknown profile "${m}". Create it: /profile add ${m} <environment>`});return}fo(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=ba().map(g=>({name:g,apiUrl:va(g).apiUrl}));Fe({cursor:0,rows:m});return}case"resume":{let m=k.join(" ").trim(),g=ga(e).filter(E=>E.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=fa(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},Jo(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(E){u({kind:"note",tone:"error",text:`Could not list workspaces: ${E.message}`});return}if(!m){let E=g.workspaces.map(L=>({id:L.id,name:L.name??L.id,type:L.workspace_type}));if(E.length===0){u({kind:"note",tone:"info",text:"No workspaces in this project."});return}let I=Math.max(0,E.findIndex(L=>L.id===g.current));Be({cursor:I,current:I,rows:E,profile:g.profile,projectId:g.projectId,environment:g.environment});return}let y=ho(g.workspaces,m);if(!y){u({kind:"note",tone:"error",text:`No workspace matches "${m}".`});return}fn(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 Qo({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 E=y.length>1?[{path:"--all",label:`publish all ${y.length} candidates in one run`},...y]:y;He({cursor:0,rows:E})}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(os(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=ra(m)?m:oo(e,m);if(oa(g)){try{u({kind:"preview",name:Ko(g),text:is(Zr(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),E={profile:y.name,connection:y.connection},I=y.project.projectId??ca(E),L=await ua(E,I,m);u({kind:"preview",name:L.title??m,text:L.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(en(e));je({cursor:I,current:I});return}let{lines:g,changed:y,effective:E}=ut(e,m);for(let I of g)u({kind:"note",tone:I.startsWith("\u26A0")?"warn":I.startsWith("\u2713")?"ok":"info",text:I});y&&(cn(I=>I+1),te.current?.setModel(E));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}=Do(k),y=Ta(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 E=Io(y,m);if(E.length===0){u({kind:"note",tone:"error",text:`No tracked articles under "${m}". (${y.length} are tracked overall.)`});return}let I=Ca(E),L=3,S=`/convert${m?` --scope ${m}`:""} --run`,b=rs(e,"light");if(!g){let M=Ra({files:_o(e,E),op:"convert",model:b.model}),F=m?`Scope: ${m} (${E.length} of ${y.length} tracked)
71
+ Press 1 to start.`}),K(["/init"]);return}try{let a=pe(e,s),l=Oe(a.name);l&&Se(l)&&l.refreshToken?ka({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=Oe(l.name);a&&i&&!Se(i)&&qa(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{}},[]),Te(()=>{let a=null,l=null,i=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let k=`${ue.columns??80}x${ue.rows??24}`;k!==hn.current&&(hn.current=k,vs(m=>m+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+gr(uo.current,$e())+"\x1B[?2026l")},80))},400)};return ue.on("resize",i),()=>{a&&clearTimeout(a),l&&clearTimeout(l),ue.off("resize",i)}},[ue,$e]);let Me=Math.max(20,(ue.columns??80)-1),kn=ro(()=>Wa(e,s),[e,s,ie]),ho=ro(()=>{if(!se(e))return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};try{let a=pe(e,s),l=Oe(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:Fa,isSetup:!1}},[e,s,ie]),et=Wn(R),yn=et.length>0&&!I,ko=d!==null?rs(d,8,Me):null,vt=oe?oe.paths.filter(a=>!oe.query||a.toLowerCase().includes(oe.query.toLowerCase())).slice(0,8):[],Ct=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=ls({cwd:e,resume:a,profileName:l??s,allowProdWrites:kt.current}),a||(Re.current={uuid:null,firstPrompt:null,titleFired:!1},Fe(null)),$t(0),bt(0)},[e,s]),wn=G((a,l,i)=>{if(Ba.test(a))try{let k=pe(e,s),m=typeof l.project_id=="string"&&l.project_id||k.project.projectId,g=Jt(l,i),y=[],E=Qt(i);a.endsWith("publish_article")&&E&&y.push(`Live: ${E}`),g&&m&&y.push(`Preview: ${Kt(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)=>{co(!0),ae(null),K([]);let i=++mt.current;u({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let k=Re.current;k.firstPrompt||(k.firstPrompt=l?.display??a),Qe.current=Date.now(),ft(0),q(!0),ht.current.clear();let m="",g="",y=null,E=()=>{y||(y=setTimeout(()=>{y=null,h(g.length>0?g:null)},60))},_=()=>{y&&clearTimeout(y),y=null,h(null)},L=()=>{if(g.trim()){let S=g.trimEnd();u({kind:"assistant",text:S})}g="",_()};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(),D=$a(k.firstPrompt??"session");ba({uuid:S.sessionId,name:D,renamed:!1,titled:!1,cwd:e,firstPrompt:k.firstPrompt??"",createdAt:b,updatedAt:b}),Fe(D)}}else if(S.type==="text"){g+=S.delta,m+=S.delta;let b=ss(g);if(b>0){let D=g.slice(0,b).trimEnd();D&&u({kind:"assistant",text:D}),g=g.slice(b)}E(),ft(D=>D+S.delta.length)}else if(S.type==="tool"){let b=Yt(S.name,S.input);b&&(L(),u({kind:"tool",title:b.title,sep:b.sep,arg:b.arg}),ht.current.set(S.id,{name:S.name,input:S.input}))}else if(S.type==="article_diff"){let b=Ne(S.oldContent,S.newContent,$e());b&&(L(),u({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)&&(wt.current=!0);let b=ht.current.get(S.id);if(b){ht.current.delete(S.id),L();let D=Vt(S.output,4,S.isError?void 0:b.name,b.input);u({kind:"tool-result",lines:D.lines,hidden:D.hidden,isError:S.isError}),S.isError||wn(b.name,b.input,S.output)}}else if(S.type==="result"){L(),$t(D=>D+S.outputTokens),bt(D=>D+S.costUsd),u({kind:"done",seconds:Math.round((Date.now()-Qe.current)/1e3),tokens:S.outputTokens,costUsd:S.costUsd,ok:S.ok});let b=S.ok?Kr(m,st.map(D=>D.name)):[];if(b.length>0?K(b):S.ok&&m.trim()&&Sa(a,m,e).then(D=>{D&&mt.current===i&&ae(D)}).catch(()=>{}),k.uuid&&(on(k.uuid),!k.titleFired)){k.titleFired=!0;let D=k.uuid,M=k.firstPrompt;M&&as(M,e).then(W=>{W&&(xa(D,W),Fe(W))}).catch(()=>{})}}else S.type==="error"&&(L(),S.kind==="auth"&&(wt.current=!0),u({kind:"note",text:`agent error: ${S.message}`,tone:"error"}))}finally{q(!1),_(),wt.current&&(wt.current=!1,xt.current=l?.display??a,K(["/login"])),yt.current&&(yt.current=!1,De.current.length>0&&(u({kind:"note",tone:"info",text:`(${De.current.length} queued message(s) discarded)`}),De.current=[],fo([])),u({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,u,wn,$e]),yo=G(a=>{let l=Oe(a);if(!l||Se(l)&&!l.refreshToken)return null;let i={...Ke(l.idToken)??{},...Ke(l.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),wo=G((a,l)=>{if(l){let i=se(e);i&&(i.defaultProfile=a,Ta(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.`}),yo(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,yo]),xn=G((a,l,i)=>{jt(e,a,l,i.id),u({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),ge(),cs(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]),Pt=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=Ne(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]),$n=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{Ra({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 Pt(),!0},[e,s,Pt,u]),bn=G(a=>{let l=mo.current;if(!l)return!1;if(mo.current=null,a.trim()!==l.repoName)return u({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:i,failed:k}=Jo(e,l.targets);u({kind:"note",tone:k.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${i.length} item${i.length===1?"":"s"}. The repo is back to its original state.`});for(let m of k)u({kind:"note",tone:"error",text:` \u2717 ${m.path}: ${m.error}`});return!0},[e,u]),vn=G(async a=>{let l=a.slice(1).trim().split(/\s+/),i=(l[0]??"").toLowerCase(),k=l.slice(1);switch(co(!0),i){case"help":u({kind:"note",tone:"info",text:Nt().join(`
72
+ `)});return;case"exit":case"quit":te.current?.close(),r();return;case"clear":ge(),uo.current=[],co(!1),ae(null),mt.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 fa(m.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},E=>u({kind:"note",tone:"info",text:E})),y=ga(m.name,g);ha(y),bo(y,m.name,E=>u({kind:"note",tone:"info",text:E})),u({kind:"note",tone:"ok",text:`\u2713 Logged in to "${m.name}" as ${Et(y)}`}),m.project.workspaceId||(u({kind:"note",tone:"info",text:"Next: pick the workspace your articles publish to."}),K(["/workspace"])),xt.current&&(ae(xt.current),xt.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}kt.current=!0,ge(),u({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let m=Ho(k.join(" ")),g=Re.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=Re.current.firstPrompt??"";as(y,e).then(E=>{E?(ae(`/rename ${E}`),u({kind:"note",tone:"info",text:`Suggestion: "${E}" \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}Pa(g,m),Fe(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 E=y.map(([S,b])=>({name:S,env:b.connection?.environment??"custom",prod:b.production===!0,who:yo(S)})),_=s??g?.defaultProfile,L=Math.max(0,E.findIndex(S=>S.name===_));Be({cursor:L,current:L,rows:E});return}if(m==="add"){let y=qo(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]}).`}),wo(k[1],!1);return}if(!g?.profiles?.[m]){u({kind:"note",tone:"error",text:`Unknown profile "${m}". Create it: /profile add ${m} <environment>`});return}wo(m,!0);return}case"doctor":await pt(k,{cwd:e});return;case"mcp":await Lt(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=Ea().map(g=>({name:g,apiUrl:Da(g).apiUrl}));He({cursor:0,rows:m});return}case"resume":{let m=k.join(" ").trim(),g=Ca(e).filter(E=>E.uuid!==Re.current.uuid);if(!m){if(!g.length){u({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}_e({query:"",cursor:0,sessions:g});return}let y=va(e,m);if(!y){u({kind:"note",tone:"error",text:`No session matches "${m}".`});return}ge(y.uuid),Re.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},Fe(y.name),on(y.uuid),u({kind:"note",tone:"ok",text:`Resumed "${y.name}".`});return}case"workspace":{let m=k.join(" ").trim(),g;try{g=await Rt(e,s)}catch(E){u({kind:"note",tone:"error",text:`Could not list workspaces: ${E.message}`});return}if(!m){let E=g.workspaces.map(L=>({id:L.id,name:L.name??L.id,type:L.workspace_type}));if(E.length===0){u({kind:"note",tone:"info",text:"No workspaces in this project."});return}let _=Math.max(0,E.findIndex(L=>L.id===g.current));qe({cursor:_,current:_,rows:E,profile:g.profile,projectId:g.projectId,environment:g.environment});return}let y=$o(g.workspaces,m);if(!y){u({kind:"note",tone:"error",text:`No workspace matches "${m}".`});return}xn(g.profile,g.projectId,y);return}case"publish":{if(k[0]&&k[0]!=="--all"){await be(Ut(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 nn({cwd:e,profileName:s}),y=Ot(g.entries);if(y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let _=g.counts["unknown-base"]??0;_>0&&u({kind:"note",tone:"info",text:`(${_} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(m){await be(at(y.map(_=>_.path)),{display:"/publish --all",echoDisplay:!0});return}let E=y.length>1?[{path:"--all",label:`publish all ${y.length} candidates in one run`},...y]:y;ze({cursor:0,rows:E})}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(cs(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}Ee({query:"",cursor:0,paths:y});return}let g=ma(m)?m:lo(e,m);if(da(g)){try{u({kind:"preview",name:so(g),text:ms(is(g,"utf8"))})}catch(y){u({kind:"note",tone:"error",text:`Could not read ${g}: ${y.message}`})}return}if(Ha.test(m)){try{let y=pe(e,s),E={profile:y.name,connection:y.connection},_=y.project.projectId??ya(E),L=await wa(E,_,m);u({kind:"preview",name:L.title??m,text:L.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 _=qt(sn(e));Ae({cursor:_,current:_});return}let{lines:g,changed:y,effective:E}=dt(e,m);for(let _ of g)u({kind:"note",tone:_.startsWith("\u26A0")?"warn":_.startsWith("\u2713")?"ok":"info",text:_});y&&(gn(_=>_+1),te.current?.setModel(E));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}=Lo(k),y=Na(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 E=Oo(y,m);if(E.length===0){u({kind:"note",tone:"error",text:`No tracked articles under "${m}". (${y.length} are tracked overall.)`});return}let _=Ia(E),L=3,S=`/convert${m?` --scope ${m}`:""} --run`,b=ds(e,"light");if(!g){let M=La({files:Uo(e,E),op:"convert",model:b.model}),W=m?`Scope: ${m} (${E.length} of ${y.length} tracked)
73
73
  `:"",re=`
74
- Model: ${b.model}${b.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;u({kind:"note",tone:"info",text:F+No(I,M,L).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 ${E.length} articles across ${I.length} partitions (\u2264${L} agents at once) on ${b.model}\u2026 (esc to stop)`});try{for await(let M of ns({cwd:e,partitions:I,promptFor:Mo,concurrency:L,profileName:s,allowProdWrites:ht.current,model:b.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((ws,xs)=>ws+xs.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+Lo(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 Qo({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 Qo({cwd:e,profileName:s})).entries.filter(L=>L.status==="remote-ahead"&&L.path).map(L=>L.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 E=[];for(let I of y)E.push(await xa({cwd:e,profileName:s,relPath:I}));we.current=E,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=$a(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=Fo(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:Ho(Bo(e,m.scope),m.scope).join(`
78
- `)});return}if(m.mode==="single"){let b=await zt(k);b.kind==="forward-to-agent"&&b.prompt&&await be(b.prompt,{display:b.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=Sa(e,{scope:g}).map(b=>b.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 E=6,I=Pa(y,3),L=rs(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${E} agents at once) on ${L.model}${L.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let b of ns({cwd:e,partitions:I,promptFor:jr,concurrency:E,profileName:s,allowProdWrites:ht.current,model:L.model,signal:S.signal}))if(b.type==="partition_status")b.status==="running"?(ce(D=>D&&{...D,active:[...D.active,b.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${b.label} \u2014 authoring\u2026`})):(ce(D=>D&&{...D,active:D.active.filter(M=>M!==b.label),done:D.done+1,lastAt:Date.now()}),u({kind:"note",tone:b.status==="done"?"ok":"error",text:` ${b.status==="done"?"\u2713":"\u2717"} ${b.label}`}));else if(b.type==="partition_event")ce(D=>{if(!D)return D;let M={...D,lastAt:Date.now()};return b.event.type==="tool"?M.tools=D.tools+1:b.event.type==="text"&&(M.chars=D.chars+b.event.delta.length),M});else if(b.type==="run_done"){$t(F=>F+b.totalCostUsd),xt(F=>F+b.results.reduce((re,tt)=>re+tt.outputTokens,0));let D=b.results.filter(F=>F.ok).length,M=b.aborted?"Stopped. ":"";u({kind:"note",tone:b.aborted?"warn":b.ok?"ok":"warn",text:`${M}Authored specs \u2014 ${D}/${b.results.length} batches ok. Next: /capture-setup, then d360-capture capture.`})}}catch(b){u({kind:"note",tone:"error",text:`Spec authoring failed: ${b.message}`})}finally{De.current=null,ce(null),q(!1)}return}case"reset":{let m=qo(e);if(m.length===0){u({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}lo.current={repoName:Ko(e),targets:m},u({kind:"note",tone:"warn",text:Go(e,m).join(`
79
- `)});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($(""),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||hn(l)||gn(l))return;io.current.push(l);let i=Xr(l,nn.current);l.startsWith("/")?kn(i):be(i,{display:l})},[R,hn,gn,kn,be]),yn=G(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=qr(a);if(zr(l)){let i=Gr(++fs.current,l);nn.current.set(i,l),j(i)}else j(l);return}j(a)},[j]),ze=Math.max(10,_e-6),wn=eo(()=>Qt(f.text,ze),[f.text,ze]),go=G(a=>w(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),St=G(a=>w(l=>({...l,pos:Yr(Qt(l.text,ze),l.pos,a)})),[ze]),et=G(a=>w(l=>({...l,pos:Kr(Qt(l.text,ze),l.pos,a)})),[ze]),ks=["\x1B[H","\x1B[1~","\x1BOH"],ys=["\x1B[F","\x1B[4~","\x1BOF"],xn=G((a,l)=>l.leftArrow?(go(-1),!0):l.rightArrow?(go(1),!0):a&&ks.includes(a)?(et("start"),!0):a&&ys.includes(a)?(et("end"),!0):l.ctrl&&a==="a"?(et("start"),!0):l.ctrl&&a==="e"?(et("end"),!0):!1,[go,et]);return ea((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),ao([...Ee.current]),$("");return}if(xn(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&&yn(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&&(cn(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),fo(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),!Wn(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),fn(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}bo(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:Ko(i),text:is(Zr(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},Jo(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){$(le),ae(null),pt.current++;return}if(!R&&Ke.length>0&&a&&/^[1-9]$/.test(a)){let i=Ke[Number(a)-1];if(i){$(i);return}}if(!xn(a,l)){if(pn){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(Mn(i.usage)&&!k){$("/"+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),$(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),$("")):(ft(k),$(i[k]??""));return}}if(l.return){Pt();return}if(l.backspace||l.delete){T();return}if(l.escape){$(""),Oe(0),ae(null),K([]);return}a&&!l.ctrl&&!l.meta&&yn(a)}}}),Ve(()=>{if(_||z)return;let a=Ee.current.shift();a!==void 0&&(ao([...Ee.current]),Pt(a))},[_,z,Pt]),O(V,{flexDirection:"column",width:_e,children:[po!==null&&O(V,{marginTop:1,flexDirection:"column",children:[po.truncated&&C(v,{dimColor:!0,children:"\u2026"}),C(v,{children:po.text})]}),_&&(sn?C(Oa,{p:sn}):C(Na,{startTime:Je.current,chars:ms})),C(V,{borderStyle:"round",borderColor:dn.prod?"yellow":H,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:R?wn.map((a,l)=>{let i=f.text.slice(a.start,a.end),k=l===Zt(wn,f.pos),m=Math.min(f.pos,a.end)-a.start;return O(v,{children:[C(v,{color:H,children:l===0?"> ":" "}),k?O(Zo,{children:[i.slice(0,m),C(to,{ch:i[m]??" "}),i.slice(m+1)]}):i||" "]},`${l}-${a.start}`)}):O(v,{children:[C(v,{color:H,children:"> "}),le&&!_?O(Zo,{children:[C(to,{ch:le[0],dim:!0}),C(v,{color:"gray",children:le.slice(1)}),C(v,{dimColor:!0,children:" (tab)"})]}):uo.isSetup||!ps?O(Zo,{children:[C(to,{ch:uo.text[0],dim:!0}),C(v,{color:"gray",children:uo.text.slice(1)})]}):C(to,{ch:" "})]})}),rn.length>0&&C(V,{flexDirection:"column",paddingX:1,children:rn.map((a,l)=>C(v,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),Re?O(V,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,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?H: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(V,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Switch connection profile"}),me.rows.map((a,l)=>O(v,{color:l===me.cursor?H: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(V,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Pick your Document360 environment"}),xe.rows.map((a,l)=>O(v,{color:l===xe.cursor?H: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(V,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,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?H: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(V,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,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?H: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(V,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Publish which article?"}),fe.rows.map((a,l)=>O(v,{color:l===fe.cursor?H: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(V,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,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?H:void 0,children:[l===oe.cursor?"\u276F ":" ",a]},a)),C(v,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ne?O(V,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,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?H:void 0,children:[l===ne.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),C(v,{color:l===ne.cursor?H:"gray",children:a.firstPrompt.slice(0,Math.max(10,_e-40))})]},a.uuid)),C(v,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):pn?C(V,{flexDirection:"column",children:Ze.map((a,l)=>O(v,{color:l===Qe?H:void 0,children:[l===Qe?"\u276F ":" ",a.usage.padEnd(22)," ",C(v,{color:l===Qe?H:"gray",children:a.name==="model"&&co?`${a.desc} (currently ${co})`:a.desc})]},a.name))}):!R&&Ke.length>0?O(V,{flexDirection:"column",paddingX:1,children:[Ke.map((a,l)=>O(v,{children:[C(v,{color:H,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(V,{paddingX:1,children:O(v,{color:"gray",children:[dn.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${co??"model n/a"}${t.kind==="api"?an>0?` \xB7 ${Ne(an)}`:"":ln>0?` \xB7 ${Bt(ln)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}B();import{jsx as Fa}from"react/jsx-runtime";async function cs(e=process.cwd(),t="auto",o,n="0.0.0"){let r=Wa(t);r.kind==="none"&&(console.error(""),console.error(x("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}=Ua(Fa(as,{cwd:e,auth:r,profileName:o,version:n}));await s(),process.stdout.write(`
80
- `),process.exit(0)}var za=Ha(import.meta.url),us=za("../package.json"),ke=new Ba;function tn(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=>{tn(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=>{tn(e),await An({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=>{tn(e),await jn({profile:e.profile})});var on=ke.command("profile").description("Manage connection profiles for the current repo");on.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Dt(process.cwd()));on.command("use <name>").description("Set the default profile for this repo").action(e=>It(process.cwd(),e));on.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>_t(process.cwd(),e));var ds=ke.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");ds.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));ds.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await Cn(process.cwd(),e,t.profile)});ke.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Dn());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(),vr)),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(us.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(qa.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 In(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 Br(e.cwd,e.auth,e.profile):await cs(e.cwd,e.auth,e.profile,us.version)});ke.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
74
+ Model: ${b.model}${b.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;u({kind:"note",tone:"info",text:W+Fo(_,M,L).join(`
75
+ `)+re}),K([S]);return}Qe.current=Date.now(),ft(0),q(!0);let D=new AbortController;Ie.current=D,ce({total:_.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Converting ${E.length} articles across ${_.length} partitions (\u2264${L} agents at once) on ${b.model}\u2026 (esc to stop)`});try{for await(let M of us({cwd:e,partitions:_,promptFor:Wo,concurrency:L,profileName:s,allowProdWrites:kt.current,model:b.model,signal:D.signal}))if(M.type==="partition_status")M.status==="running"?(ce(W=>W&&{...W,active:[...W.active,M.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${M.label} \u2014 converting\u2026`})):(ce(W=>W&&{...W,active:W.active.filter(re=>re!==M.label),done:W.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(W=>{if(!W)return W;let re={...W,lastAt:Date.now()};return M.event.type==="tool"?re.tools=W.tools+1:M.event.type==="text"&&(re.chars=W.chars+M.event.delta.length),re});else if(M.type==="run_done"){bt(ot=>ot+M.totalCostUsd),$t(ot=>ot+M.results.reduce((Ss,Ts)=>Ss+Ts.outputTokens,0));let W=M.aborted?"Stopped. ":"",re=t.kind==="api"?"api":"subscription";u({kind:"note",tone:M.aborted?"warn":M.ok?"ok":"warn",text:W+Bo(M.results,_,re).join(`
76
+ `)})}}catch(M){u({kind:"note",tone:"error",text:`Convert run failed: ${M.message}`})}finally{Ie.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 nn({cwd:e,profileName:s});u({kind:"note",tone:"info",text:Bt(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 nn({cwd:e,profileName:s})).entries.filter(L=>L.status==="remote-ahead"&&L.path).map(L=>L.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 E=[];for(let _ of y)E.push(await ja({cwd:e,profileName:s,relPath:_}));we.current=E,Pt();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}Ge({cursor:0,rows:m.map(g=>({...g,checked:g.recommended}))});return}case"audit":case"capture-setup":{let g=await(i==="audit"?Wt:Xt)(k,void 0);g.kind==="forward-to-agent"&&g.prompt&&await be(g.prompt,{display:g.display,echoDisplay:!0});return}case"screenshot":{let m=Xo(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:Vo(Yo(e,m.scope),m.scope).join(`
78
+ `)});return}if(m.mode==="single"){let b=await Gt(k);b.kind==="forward-to-agent"&&b.prompt&&await be(b.prompt,{display:b.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(b=>b.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 E=6,_=_a(y,3),L=ds(e,"standard");Qe.current=Date.now(),ft(0),q(!0);let S=new AbortController;Ie.current=S,ce({verb:"Authoring",total:_.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 ${_.length} partition${_.length===1?"":"s"} (\u2264${E} agents at once) on ${L.model}${L.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let b of us({cwd:e,partitions:_,promptFor:Mr,concurrency:E,profileName:s,allowProdWrites:kt.current,model:L.model,signal:S.signal}))if(b.type==="partition_status")b.status==="running"?(ce(D=>D&&{...D,active:[...D.active,b.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${b.label} \u2014 authoring\u2026`})):(ce(D=>D&&{...D,active:D.active.filter(M=>M!==b.label),done:D.done+1,lastAt:Date.now()}),u({kind:"note",tone:b.status==="done"?"ok":"error",text:` ${b.status==="done"?"\u2713":"\u2717"} ${b.label}`}));else if(b.type==="partition_event")ce(D=>{if(!D)return D;let M={...D,lastAt:Date.now()};return b.event.type==="tool"?M.tools=D.tools+1:b.event.type==="text"&&(M.chars=D.chars+b.event.delta.length),M});else if(b.type==="run_done"){bt(W=>W+b.totalCostUsd),$t(W=>W+b.results.reduce((re,ot)=>re+ot.outputTokens,0));let D=b.results.filter(W=>W.ok).length,M=b.aborted?"Stopped. ":"";u({kind:"note",tone:b.aborted?"warn":b.ok?"ok":"warn",text:`${M}Authored specs \u2014 ${D}/${b.results.length} batches ok. Next: /capture-setup, then d360-capture capture.`})}}catch(b){u({kind:"note",tone:"error",text:`Spec authoring failed: ${b.message}`})}finally{Ie.current=null,ce(null),q(!1)}return}case"reset":{let m=Ko(e);if(m.length===0){u({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}mo.current={repoName:so(e),targets:m},u({kind:"note",tone:"warn",text:Qo(e,m).join(`
79
+ `)});return}default:u({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,Pt,u,ge,be]),St=G(a=>{let l=(a??R).trim();if($(""),gt(null),Ue(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||bn(l)||$n(l))return;po.current.push(l);let i=es(l,cn.current);l.startsWith("/")?vn(i):be(i,{display:l})},[R,bn,$n,vn,be]),Cn=G(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=Jr(a);if(Qr(l)){let i=Zr(++$s.current,l);cn.current.set(i,l),j(i)}else j(l);return}j(a)},[j]),Xe=Math.max(10,Me-6),Pn=ro(()=>oo(f.text,Xe),[f.text,Xe]),xo=G(a=>w(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),Tt=G(a=>w(l=>({...l,pos:os(oo(l.text,Xe),l.pos,a)})),[Xe]),tt=G(a=>w(l=>({...l,pos:ns(oo(l.text,Xe),l.pos,a)})),[Xe]),Cs=["\x1B[H","\x1B[1~","\x1BOH"],Ps=["\x1B[F","\x1B[4~","\x1BOF"],Sn=G((a,l)=>l.leftArrow?(xo(-1),!0):l.rightArrow?(xo(1),!0):a&&Cs.includes(a)?(tt("start"),!0):a&&Ps.includes(a)?(tt("end"),!0):l.ctrl&&a==="a"?(tt("start"),!0):l.ctrl&&a==="e"?(tt("end"),!0):!1,[xo,tt]);return ca((a,l)=>{if(l.ctrl&&a==="c"){te.current?.close(),r();return}if(!z){if(I){if(l.escape){if(Ie.current){Ie.current.signal.aborted||(u({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),Ie.current.abort());return}yt.current||(yt.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;De.current.push(i),fo([...De.current]),$("");return}if(Sn(a,l))return;if(l.upArrow){Tt(-1);return}if(l.downArrow){Tt(1);return}if(l.backspace||l.delete){T();return}a&&!l.ctrl&&!l.meta&&Cn(a);return}if(je){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(de.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=de.length){Ae(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=de[je.cursor];Ae(null);let{lines:k,changed:m,effective:g}=dt(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&&(gn(y=>y+1),te.current?.setModel(g));return}if(a==="s"){let i=de[je.cursor];Ae(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){Ae(null);return}return}if(me){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)<=me.rows.length){Be(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=me.rows[me.cursor];Be(null),wo(i.name,l.return===!0);return}if(l.escape){Be(null);return}return}if(xe){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)<=xe.rows.length){He(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=xe.rows[xe.cursor].name;if(He(null),!Gn(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=Oe(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){He(null);return}return}if(J){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&&/^[1-9]$/.test(a)&&Number(a)<=J.rows.length){qe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=J.rows[J.cursor],{profile:k,projectId:m}=J;qe(null),xn(k,m,i);return}if(l.escape){qe(null);return}return}if(Q){if(l.upArrow){Ge(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ge(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){Ge(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(Ge(null),i.length===0){u({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}To(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){Ge(null);return}return}if(fe){if(l.upArrow){ze(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){ze(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){ze(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);ze(null),be(i.path==="--all"?at(k):Ut(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){ze(null);return}return}if(oe){if(l.escape){Ee(null);return}if(l.upArrow){Ee(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ee(i=>i&&{...i,cursor:Math.min(Math.max(0,vt.length-1),i.cursor+1)});return}if(l.return){let i=vt[oe.cursor];if(i){Ee(null);try{u({kind:"preview",name:so(i),text:ms(is(lo(e,i),"utf8"))})}catch(k){u({kind:"note",tone:"error",text:`Could not read ${i}: ${k.message}`})}}return}if(l.backspace||l.delete){Ee(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Ee(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(ne){if(l.escape){_e(null);return}if(l.upArrow){_e(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){_e(i=>i&&{...i,cursor:Math.min(Math.max(0,Ct.length-1),i.cursor+1)});return}if(l.return){let i=Ct[ne.cursor];i&&(_e(null),ge(i.uuid),Re.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},Fe(i.name),on(i.uuid),u({kind:"note",tone:"ok",text:`Resumed "${i.name}".`}));return}if(l.backspace||l.delete){_e(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){_e(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!R&&le){$(le),ae(null),mt.current++;return}if(!R&&Je.length>0&&a&&/^[1-9]$/.test(a)){let i=Je[Number(a)-1];if(i){$(i);return}}if(!Sn(a,l)){if(yn){if(l.upArrow){Ue(i=>Math.max(0,i-1));return}if(l.downArrow){Ue(i=>Math.min(et.length-1,i+1));return}if(l.tab){$("/"+(et[Ze]?.name??"")+" "),Ue(0);return}if(l.return){let i=et[Ze];if(i){let k=R.trim().slice(1).split(/\s+/).slice(1).join(" ");if(Fn(i.usage)&&!k){$("/"+i.name+" "),Ue(0);return}St("/"+i.name+(k?" "+k:""));return}}}else{if(l.upArrow){if(R!==""&&We===null){Tt(-1);return}let i=po.current;if(!i.length)return;let k=We===null?i.length-1:Math.max(0,We-1);gt(k),$(i[k]??"");return}if(l.downArrow){if(R!==""&&We===null){Tt(1);return}let i=po.current;if(We===null)return;let k=We+1;k>=i.length?(gt(null),$("")):(gt(k),$(i[k]??""));return}}if(l.return){St();return}if(l.backspace||l.delete){T();return}if(l.escape){$(""),Ue(0),ae(null),K([]);return}a&&!l.ctrl&&!l.meta&&Cn(a)}}}),Te(()=>{if(I||z)return;let a=De.current.shift();a!==void 0&&(fo([...De.current]),St(a))},[I,z,St]),U(Y,{flexDirection:"column",width:Me,children:[ko!==null&&U(Y,{marginTop:1,flexDirection:"column",children:[ko.truncated&&C(v,{dimColor:!0,children:"\u2026"}),C(v,{children:ko.text})]}),I&&(pn?C(Xa,{p:pn}):C(za,{startTime:Qe.current,chars:xs})),C(Y,{borderStyle:"round",borderColor:kn.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===no(Pn,f.pos),m=Math.min(f.pos,a.end)-a.start;return U(v,{children:[C(v,{color:H,children:l===0?"> ":" "}),k?U(rn,{children:[i.slice(0,m),C(io,{ch:i[m]??" "}),i.slice(m+1)]}):i||" "]},`${l}-${a.start}`)}):U(v,{children:[C(v,{color:H,children:"> "}),le&&!I?U(rn,{children:[C(io,{ch:le[0],dim:!0}),C(v,{color:"gray",children:le.slice(1)}),C(v,{dimColor:!0,children:" (tab)"})]}):ho.isSetup||!ws?U(rn,{children:[C(io,{ch:ho.text[0],dim:!0}),C(v,{color:"gray",children:ho.text.slice(1)})]}):C(io,{ch:" "})]})}),dn.length>0&&C(Y,{flexDirection:"column",paddingX:1,children:dn.map((a,l)=>C(v,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),je?U(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,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)=>U(v,{color:l===je.cursor?H:void 0,children:[l===je.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===je.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?U(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Switch connection profile"}),me.rows.map((a,l)=>U(v,{color:l===me.cursor?H: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?U(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Pick your Document360 environment"}),xe.rows.map((a,l)=>U(v,{color:l===xe.cursor?H: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?U(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,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)=>U(v,{color:l===J.cursor?H: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 U(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,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 U(v,{color:y===Q.cursor?H:void 0,children:[y===Q.cursor?"\u276F ":" ",m.checked?"\u25C9 ":"\u25CB ",m.path.padEnd(Math.min(48,Me-34)),C(v,{color:"gray",children:Ft(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?U(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Publish which article?"}),fe.rows.map((a,l)=>U(v,{color:l===fe.cursor?H:void 0,children:[l===fe.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,Me-30)),C(v,{color:"gray",children:a.label})]},a.path)),C(v,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):oe?U(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:`Preview article${oe.query?` \u2014 filter: ${oe.query}`:" (type to filter)"}`}),vt.length===0?C(v,{color:"gray",children:"no articles match"}):vt.map((a,l)=>U(v,{color:l===oe.cursor?H:void 0,children:[l===oe.cursor?"\u276F ":" ",a]},a)),C(v,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ne?U(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:`Resume session${ne.query?` \u2014 filter: ${ne.query}`:" (type to filter)"}`}),Ct.length===0?C(v,{color:"gray",children:"no sessions match"}):Ct.map((a,l)=>U(v,{color:l===ne.cursor?H:void 0,children:[l===ne.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),C(v,{color:l===ne.cursor?H:"gray",children:a.firstPrompt.slice(0,Math.max(10,Me-40))})]},a.uuid)),C(v,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):yn?C(Y,{flexDirection:"column",children:et.map((a,l)=>U(v,{color:l===Ze?H:void 0,children:[l===Ze?"\u276F ":" ",a.usage.padEnd(22)," ",C(v,{color:l===Ze?H:"gray",children:a.name==="model"&&go?`${a.desc} (currently ${go})`:a.desc})]},a.name))}):!R&&Je.length>0?U(Y,{flexDirection:"column",paddingX:1,children:[Je.map((a,l)=>U(v,{children:[C(v,{color:H,children:l+1})," ",a.slice(0,Math.max(20,Me-5))]},a)),C(v,{dimColor:!0,children:`press 1-${Je.length} to fill the command \xB7 esc dismiss`})]}):C(Y,{paddingX:1,children:U(v,{color:"gray",children:[kn.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${go??"model n/a"}${t.kind==="api"?fn>0?` \xB7 ${Le(fn)}`:"":mn>0?` \xB7 ${Ht(mn)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}B();import{jsx as Ka}from"react/jsx-runtime";async function hs(e=process.cwd(),t="auto",o,n="0.0.0"){let r=Va(t);r.kind==="none"&&(console.error(""),console.error(x("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}=Ya(Ka(gs,{cwd:e,auth:r,profileName:o,version:n}));await s(),process.stdout.write(`
80
+ `),process.exit(0)}var ec=Qa(import.meta.url),ks=ec("../package.json"),ke=new Ja;function ln(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=>{ln(e),await Dt({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=>{ln(e),await Nn({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=>{ln(e),await Mn({profile:e.profile})});var an=ke.command("profile").description("Manage connection profiles for the current repo");an.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>It(process.cwd()));an.command("use <name>").description("Set the default profile for this repo").action(e=>_t(process.cwd(),e));an.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Mt(process.cwd(),e));var ys=ke.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");ys.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>Ye(process.cwd(),e.profile));ys.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await An(process.cwd(),e,t.profile)});ke.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>On());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(()=>(zt(),jr)),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(ks.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(Za.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 Un(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 Yr(e.cwd,e.auth,e.profile):await hs(e.cwd,e.auth,e.profile,ks.version)});ke.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
@@ -1,3 +1,5 @@
1
1
  import type { SlashCommandResult } from './index.js';
2
2
  import type { ReplContext } from '../repl.js';
3
+ /** Drop one layer of matching wrapping quotes — `/rename "my name"` shouldn't store the quotes. */
4
+ export declare function stripWrappingQuotes(s: string): string;
3
5
  export declare function renameCommand(args: string[], ctx: ReplContext): Promise<SlashCommandResult>;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Terminal tab status — so you can tell, across several open tabs, which d360-writer is
3
+ * busy and which is waiting (Claude-Code style). Two OSC sequences, both SAFE to write
4
+ * while Ink is mounted: they set the window title / tab-progress and emit NO visible cells
5
+ * and NO cursor movement, so they don't race Ink's frame tracking (unlike clear/cursor
6
+ * escapes — see gotchas-writer-tui.md). On non-supporting terminals they're ignored.
7
+ */
8
+ /** OSC 0 — set the window/tab title. BEL-terminated (Windows Terminal accepts it). */
9
+ export declare const titleSeq: (text: string) => string;
10
+ /**
11
+ * OSC 9;4 — Windows-Terminal/ConEmu tab progress indicator:
12
+ * 0 = clear · 1 = normal/green (done) · 3 = indeterminate (busy, pulsing).
13
+ */
14
+ export declare const progressSeq: (state: 0 | 1 | 3) => string;
15
+ export declare function setTitle(text: string): void;
16
+ export declare function setProgress(state: 0 | 1 | 3): void;
17
+ /** Animation frames for the busy title prefix — a flicking dot trail. */
18
+ export declare const BUSY_FRAMES: readonly ["· ", "·· ", "···"];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document360-writer",
3
- "version": "0.4.24",
3
+ "version": "0.4.26",
4
4
  "description": "Standalone documentation agent CLI. Reads your code, writes your docs. Specialized for Document360 publishing.",
5
5
  "type": "module",
6
6
  "bin": {