document360-writer 0.4.56 → 0.4.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.js +42 -42
  2. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -1,26 +1,26 @@
1
1
  #!/usr/bin/env node
2
- var oa=Object.defineProperty;var $r=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}};var ra=(e,t)=>{for(var n in t)oa(e,n,{get:t[n],enumerable:!0})};import De from"picocolors";var G,sa,ia,aa,xr,ht,m,x,L,A,E,ee,Ce,q=$r(()=>{"use strict";G="#7f56d9",[sa,ia,aa]=[127,86,217],xr=e=>De.isColorSupported?`\x1B[38;2;${sa};${ia};${aa}m${e}\x1B[39m`:e,ht=e=>De.bold(xr(e)),m=e=>De.dim(e),x=e=>De.red(e),L=e=>De.yellow(e),A=e=>De.green(e),E=xr,ee=e=>De.gray(e),Ce=e=>De.bold(e)});var _s={};ra(_s,{doctorCommand:()=>Tt,renderDoctorChecks:()=>Ns,runDoctorChecks:()=>Is});import{existsSync as rc}from"node:fs";import{d360GetAll as sc,getAccessToken as ic,isExpired as To,loadProfileMap as ac,loadTokens as lc,packageSkillsDir as Es,projectConfigPath as cc,readProjectConfig as uc,DEFAULT_DOCS_DIR as dc,resolveActiveProfile as pc,resolveAuth as fc,resolveModelSetting as mc}from"document360-engine";async function Is(e){let t=[],n=Number(process.versions.node.split(".")[0]);t.push(n>=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 o=fc("auto");t.push(o.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: ${o.kind==="api"?"API key":"subscription"}`});let r=mc(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=uc(e);if(!s)return t.push({level:"fail",label:`No ${cc(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??dc).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/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let p=s.authoritativeSourceFiles??[];t.push(p.length>0?{level:"ok",label:`Sources: docs grounded in ${p.length} path(s) (${p.slice(0,3).join(", ")}${p.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let g;try{g=pc(e),t.push({level:"ok",label:`Profile: ${g.name} (${g.connection.name})${g.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 w=lc(g.name);w?To(w)&&!w.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):To(w)?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(w.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${g.name})`}),t.push(g.project.workspaceId?{level:"ok",label:`Workspace: ${g.project.workspaceName??g.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let $=ac(e,g.name);if($?$.projectId&&g.project.projectId&&$.projectId!==g.project.projectId?t.push({level:"fail",label:`Category map projectId (${$.projectId}) \u2260 profile projectId (${g.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer/config.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys($.articles).length} articles, ${Object.keys($.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${g.name}" (created on first /publish)`}),t.push(rc(Es())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Es()}`,fix:"Reinstall document360-writer (broken install)"}),w&&(!To(w)||w.refreshToken)){let R={profile:g.name,connection:g.connection};try{await ic(R);let y=await sc(R,"/v3/projects");t.push({level:"ok",label:`API reachable (${g.connection.apiUrl}) \u2014 ${y.length} project(s) visible`})}catch(y){t.push({level:"fail",label:`API call failed: ${y.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function Ns(e){let t=[""];for(let r of e){let s=r.level==="ok"?A("\u2713"):r.level==="warn"?L("\u26A0"):x("\u2717");t.push(` ${s} ${r.label}${r.detail?m(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${m("fix:")} ${E(r.fix)}`)}let n=e.filter(r=>r.level==="fail").length,o=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(n===0?A(`\u2713 ${o===0?"All checks passed":`Healthy (${o} warning${o===1?"":"s"})`}`):x(`\u2717 ${n} problem${n===1?"":"s"} found`)),t.push(""),t}async function Tt(e,t){console.log(m("Running checks\u2026"));for(let n of Ns(await Is(t.cwd)))console.log(n);return{kind:"continue"}}var pn=$r(()=>{"use strict";q()});import{Command as Pd}from"commander";import{createRequire as Td}from"node:module";import{AUTH_MODES as Sd}from"document360-engine";import{input as wa}from"@inquirer/prompts";import{loginPkce as ka,refreshTokens as ya,toStoredTokens as Pr,clearTokens as $a,decodeJwtClaims as Cr,isExpired as xa,loadTokens as ba,saveTokens as Tr,resolveActiveProfile as Vt,setProfileProject as va,readProjectConfig as Ca}from"document360-engine";q();import{select as la}from"@inquirer/prompts";import{resolveActiveProfile as ca,setProfileProject as ua,resolveProjectId as da,listWorkspaces as pa}from"document360-engine";async function wt(e,t){let n=ca(e,t),o={profile:n.name,connection:n.connection},r=n.project.projectId??da(o);return{workspaces:await pa(o,r),projectId:r,projectName:n.project.projectName,profile:n.name,environment:n.connection.name,current:n.project.workspaceId}}var br=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function Hn(e,t){let n=t.toLowerCase();return e.find(o=>(o.name??"").toLowerCase()===n)??e.find(o=>(o.name??"").toLowerCase().startsWith(n))??e.find(o=>o.id.startsWith(t))}function Yt(e,t,n,o,r){ua(e,t,{projectId:n,workspaceId:o,workspaceName:r})}async function vr(e,t,n){let o;try{o=await wt(e,n)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=Hn(o.workspaces,t);return r?(Yt(e,o.profile,o.projectId,r.id,r.name),console.log(A(`\u2713 Workspace set to "${r.name??r.id}" for profile "${o.profile}".`)),0):(console.log(x(`No workspace matches "${t}". Available: ${o.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function lt(e,t){let n;try{n=await wt(e,t)}catch(w){console.log(x(`Could not list workspaces: ${w.message}`));return}let{workspaces:o,projectId:r,profile:s,current:c}=n;if(o.length===0){console.log(m("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let w of o)console.log(` ${w.id===c?E("\u25CF"):" "} ${br(w)} ${m(w.id)}`);console.log(m("Run: d360-writer workspace use <name>"));return}let p=await la({message:"Select the Document360 workspace for this repo:",choices:o.map(w=>({name:`${br(w)}${w.id===c?" (current)":""}`,value:w.id}))}),g=o.find(w=>w.id===p);Yt(e,s,r,p,g?.name),console.log(A(`\u2713 Workspace set to "${g?.name??p}" for profile "${s}".`))}q();import fa from"picocolors";function ma(e=process.env){return e.FORCE_HYPERLINK==="0"||!fa.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 ga(e,t=e,n){return ma(n)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var ha=/https?:\/\/[^\s\x1b]+/g;function kt(e,t){return e.replace(ha,n=>ga(n,n,t))}function Sr(e){return{...Cr(e.idToken)??{},...Cr(e.accessToken)??{}}}function Xt(e){let t=Sr(e),n=t.email??t.preferred_username??t.sub??"unknown",o=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${n}${o}`}async function Kt(e){let t=Vt(process.cwd(),e.profile),n=t.connection;console.log(m(`Profile "${t.name}" \u2192 ${n.name} (${n.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let o=await ka(n,{manual:e.manual,promptForRedirect:s=>wa({message:s})},s=>console.log(kt(s))),r=Pr(t.name,o);if(Tr(r),Jt(r,t.name,s=>console.log(m(s))),console.log(""),console.log(A(`\u2713 Logged in to "${t.name}" as ${Xt(r)}`)),console.log(m(` access token expires: ${r.expiresAt}`)),console.log(m(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{Ca(process.cwd())?.profiles?.[t.name]&&(console.log(""),await lt(process.cwd(),t.name))}catch{}}function Jt(e,t,n){let r=Sr(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(Vt(process.cwd(),t).project.projectId)return;va(process.cwd(),t,{projectId:r}),n(` Project ${r} written to profile "${t}".`)}catch{}}async function Rr(e){let t=Vt(process.cwd(),e.profile),n=t.connection,o=ba(t.name);if(!o){console.log(x(`Not logged in to Document360 (profile "${t.name}").`)),console.log(m(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${E(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Xt(o)}`),xa(o))if(o.refreshToken)try{let r=Pr(t.name,await ya(n,o.refreshToken));Tr(r),console.log(A(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(L(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(m(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(L("Session expired (no refresh token).")),console.log(m(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(m(` expires: ${o.expiresAt}`))}async function jr(e){let t=Vt(process.cwd(),e.profile);$a(t.name)?console.log(A(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(m(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}q();import{readProjectConfig as Dr,writeProjectConfig as Pa,resolveActiveProfile as Ta,loadTokens as Sa,isExpired as Ra}from"document360-engine";function Qt(e){let t=Dr(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[n,o]of Object.entries(t.profiles)){let r=n===t.defaultProfile?E("\u25CF "):" ",s=o.production?L(" \u26A0 PRODUCTION"):"",c=o.connection.environment??"(inline)",p=Sa(n),g=p?Ra(p)&&!p.refreshToken?L("expired"):m("logged in"):m("not logged in");console.log(`${r}${E(n)} \u2192 ${c}${s} [${g}]`)}console.log(""),console.log(m("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Zt(e,t){let n=Dr(e);if(!n?.profiles?.[t]){let r=n?.profiles?Object.keys(n.profiles).join(", "):"(none \u2014 run init)";console.log(x(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}n.defaultProfile=t,Pa(n,e);let o=n.profiles[t].production?L(" \u26A0 PRODUCTION"):"";console.log(A(`\u2713 Default profile is now "${t}"${o}`))}function en(e,t){try{let n=Ta(e,t);console.log(""),console.log(`Profile ${E(n.name)}${n.production?L(" \u26A0 PRODUCTION"):""}`),console.log(m(` api: ${n.connection.apiUrl}`)),console.log(m(` identity: ${n.connection.authorizationUrl}`)),console.log(m(` clientId: ${n.connection.clientId}`)),console.log(m(` scopes: ${n.connection.scopes.join(" ")}`)),console.log(m(` project: ${n.project.projectId??"(set at login)"}`)),console.log(m(` workspace:${n.project.workspaceId?" "+n.project.workspaceId:" (none)"}`)),console.log("")}catch(n){console.log(x(n.message)),process.exitCode=1}}q();import{existsSync as ja,readdirSync as Da,statSync as Aa}from"node:fs";import{join as Ea}from"node:path";import{apiLogDir as Ia}from"document360-engine";function Ar(){let e=Ia();if(console.log(""),console.log(`Document360 API logs: ${E(e)}`),!ja(e)){console.log(m(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=Da(e).filter(n=>n.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(m(" No logs yet \u2014 they appear after the first Document360 API call."));for(let n of t.slice(0,14)){let o=(Aa(Ea(e,n)).size/1024).toFixed(1);console.log(` ${n} ${m(`${o} KB`)}`)}console.log(""),console.log(m("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(m("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}q();import{createSession as Na,resolveAuth as _a,findByName as Ma,slugify as Oa,touchSession as La,upsertSession as Ua,resolveActiveProfile as Wa}from"document360-engine";async function Er(e,t,n,o,r,s){let c=_a(n);c.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(m("Using your Claude subscription (no API key set)."));let p=null;try{p=Wa(e,r)}catch(y){console.error(x(`Document360 profile error: ${y.message}`)),process.exit(2)}p.production&&(console.error(L(`\u26A0 Profile "${p.name}" is PRODUCTION.`)),s||(console.error(x("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(m(" --yes given \u2014 proceeding against production.")));let g;if(o){let y=Ma(e,o);y||(console.error(x(`No saved session matches "${o}" in this repo.`)),console.error(m("List sessions inside the REPL with /resume.")),process.exit(2)),g=y.uuid,console.error(m(`Resuming "${y.name}"`))}let w=Na({cwd:e,resume:g,profileName:r,allowProdWrites:s===!0}),$=g??null,R=1;for await(let y of w.send(t))switch(y.type){case"session":if(!$){$=y.sessionId;let b=new Date().toISOString();Ua({uuid:$,name:Oa(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:b,updatedAt:b})}break;case"text":process.stdout.write(y.delta);break;case"tool":console.error(ee(` \u2699 ${y.name}`));break;case"result":R=y.ok?0:1,console.error(m(`(${y.inputTokens}\u2192${y.outputTokens} tokens`+(y.costUsd>0?`, $${y.costUsd<.01?y.costUsd.toFixed(4):y.costUsd.toFixed(2)}`:"")+")")),y.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${y.message}`)),process.exit(1)}$&&La($),process.stdout.write(`
3
- `),process.exit(R)}import{createInterface as ru}from"node:readline/promises";import{createSession as Fo,resolveAuth as su,getSession as iu,setTitle as au,slugify as lu,touchSession as di,upsertSession as cu,generateTitle as uu,resolveActiveProfile as pi,resolveModelSetting as du,readProjectConfig as pu,decodeJwtClaims as ci,isExpired as fu,loadTokens as mu}from"document360-engine";var yt=[{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:"project",usage:"/project [name]",desc:"Choose the Document360 project (picker; lists all you can access)",group:"start"},{name:"devhints",usage:"/devhints",desc:"Set up the dev\u2192docs hint channel (guide + paste block for your repo agent)",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:"draft",usage:"/draft [<feature>]",desc:"Draft a feature's articles from its PRDs/specs in .d360-writer/input/ + the code",group:"docs"},{name:"write",usage:"/write [--all|--scope <cat>|<path>] [--run]",desc:"Author the planned articles in parallel (bare = preview + cost; --run starts)",group:"docs"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: status; else author capture specs (bulk also refreshes the capture checklist; --no-setup skips)",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Re-run the data-staging checklist + dev test-id requests (auto-runs after /screenshot --all)",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:"logout",usage:"/logout",desc:"Sign out + clear project/workspace selection (then /login to re-pick)",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"}],Fa=[{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 tn(){let e=Math.max(...yt.map(n=>n.usage.length))+2,t=["document360-writer \u2014 commands","",'New here? /init \u2192 "write the docs for this repo" \u2192 /publish',""," Screenshots are optional \u2014 add them anytime:"," /screenshot --all \u2192 d360-capture capture \u2192 re-/publish"];for(let{key:n,label:o}of Fa){t.push("",o);for(let r of yt.filter(s=>s.group===n))t.push(` ${r.usage.padEnd(e)}${r.desc}`)}return t.push("","Tip: anything not starting with / is sent to the agent."),t}function Ir(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return yt.filter(n=>n.name.startsWith(t))}function Nr(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function Bn(){console.log("");for(let e of tn())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}q();import{getSession as Ha}from"document360-engine";async function _r(e,t){let n=t.currentUuid(),o=n?Ha(n):void 0;return console.log(o?m(`
2
+ var ra=Object.defineProperty;var $r=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}};var sa=(e,t)=>{for(var n in t)ra(e,n,{get:t[n],enumerable:!0})};import De from"picocolors";var G,ia,aa,la,xr,ht,m,x,L,A,E,ee,Ce,q=$r(()=>{"use strict";G="#7f56d9",[ia,aa,la]=[127,86,217],xr=e=>De.isColorSupported?`\x1B[38;2;${ia};${aa};${la}m${e}\x1B[39m`:e,ht=e=>De.bold(xr(e)),m=e=>De.dim(e),x=e=>De.red(e),L=e=>De.yellow(e),A=e=>De.green(e),E=xr,ee=e=>De.gray(e),Ce=e=>De.bold(e)});var _s={};sa(_s,{doctorCommand:()=>Tt,renderDoctorChecks:()=>Ns,runDoctorChecks:()=>Is});import{existsSync as sc}from"node:fs";import{d360GetAll as ic,getAccessToken as ac,isExpired as To,loadProfileMap as lc,loadTokens as cc,packageSkillsDir as Es,projectConfigPath as uc,readProjectConfig as dc,DEFAULT_DOCS_DIR as pc,resolveActiveProfile as fc,resolveAuth as mc,resolveModelSetting as gc}from"document360-engine";async function Is(e){let t=[],n=Number(process.versions.node.split(".")[0]);t.push(n>=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 o=mc("auto");t.push(o.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: ${o.kind==="api"?"API key":"subscription"}`});let r=gc(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=dc(e);if(!s)return t.push({level:"fail",label:`No ${uc(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??pc).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/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let p=s.authoritativeSourceFiles??[];t.push(p.length>0?{level:"ok",label:`Sources: docs grounded in ${p.length} path(s) (${p.slice(0,3).join(", ")}${p.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let g;try{g=fc(e),t.push({level:"ok",label:`Profile: ${g.name} (${g.connection.name})${g.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 w=cc(g.name);w?To(w)&&!w.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):To(w)?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(w.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${g.name})`}),t.push(g.project.workspaceId?{level:"ok",label:`Workspace: ${g.project.workspaceName??g.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let $=lc(e,g.name);if($?$.projectId&&g.project.projectId&&$.projectId!==g.project.projectId?t.push({level:"fail",label:`Category map projectId (${$.projectId}) \u2260 profile projectId (${g.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer/config.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys($.articles).length} articles, ${Object.keys($.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${g.name}" (created on first /publish)`}),t.push(sc(Es())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Es()}`,fix:"Reinstall document360-writer (broken install)"}),w&&(!To(w)||w.refreshToken)){let R={profile:g.name,connection:g.connection};try{await ac(R);let y=await ic(R,"/v3/projects");t.push({level:"ok",label:`API reachable (${g.connection.apiUrl}) \u2014 ${y.length} project(s) visible`})}catch(y){t.push({level:"fail",label:`API call failed: ${y.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function Ns(e){let t=[""];for(let r of e){let s=r.level==="ok"?A("\u2713"):r.level==="warn"?L("\u26A0"):x("\u2717");t.push(` ${s} ${r.label}${r.detail?m(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${m("fix:")} ${E(r.fix)}`)}let n=e.filter(r=>r.level==="fail").length,o=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(n===0?A(`\u2713 ${o===0?"All checks passed":`Healthy (${o} warning${o===1?"":"s"})`}`):x(`\u2717 ${n} problem${n===1?"":"s"} found`)),t.push(""),t}async function Tt(e,t){console.log(m("Running checks\u2026"));for(let n of Ns(await Is(t.cwd)))console.log(n);return{kind:"continue"}}var pn=$r(()=>{"use strict";q()});import{Command as Pd}from"commander";import{createRequire as Td}from"node:module";import{AUTH_MODES as Sd}from"document360-engine";import{input as ka}from"@inquirer/prompts";import{loginPkce as ya,refreshTokens as $a,toStoredTokens as Pr,clearTokens as xa,decodeJwtClaims as Cr,isExpired as ba,loadTokens as va,saveTokens as Tr,resolveActiveProfile as Vt,setProfileProject as Ca,readProjectConfig as Pa}from"document360-engine";q();import{select as ca}from"@inquirer/prompts";import{resolveActiveProfile as ua,setProfileProject as da,resolveProjectId as pa,listWorkspaces as fa}from"document360-engine";async function wt(e,t){let n=ua(e,t),o={profile:n.name,connection:n.connection},r=n.project.projectId??pa(o);return{workspaces:await fa(o,r),projectId:r,projectName:n.project.projectName,profile:n.name,environment:n.connection.name,current:n.project.workspaceId}}var br=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function Hn(e,t){let n=t.toLowerCase();return e.find(o=>(o.name??"").toLowerCase()===n)??e.find(o=>(o.name??"").toLowerCase().startsWith(n))??e.find(o=>o.id.startsWith(t))}function Yt(e,t,n,o,r){da(e,t,{projectId:n,workspaceId:o,workspaceName:r})}async function vr(e,t,n){let o;try{o=await wt(e,n)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=Hn(o.workspaces,t);return r?(Yt(e,o.profile,o.projectId,r.id,r.name),console.log(A(`\u2713 Workspace set to "${r.name??r.id}" for profile "${o.profile}".`)),0):(console.log(x(`No workspace matches "${t}". Available: ${o.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function lt(e,t){let n;try{n=await wt(e,t)}catch(w){console.log(x(`Could not list workspaces: ${w.message}`));return}let{workspaces:o,projectId:r,profile:s,current:c}=n;if(o.length===0){console.log(m("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let w of o)console.log(` ${w.id===c?E("\u25CF"):" "} ${br(w)} ${m(w.id)}`);console.log(m("Run: d360-writer workspace use <name>"));return}let p=await ca({message:"Select the Document360 workspace for this repo:",choices:o.map(w=>({name:`${br(w)}${w.id===c?" (current)":""}`,value:w.id}))}),g=o.find(w=>w.id===p);Yt(e,s,r,p,g?.name),console.log(A(`\u2713 Workspace set to "${g?.name??p}" for profile "${s}".`))}q();import ma from"picocolors";function ga(e=process.env){return e.FORCE_HYPERLINK==="0"||!ma.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 ha(e,t=e,n){return ga(n)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var wa=/https?:\/\/[^\s\x1b]+/g;function kt(e,t){return e.replace(wa,n=>ha(n,n,t))}function Sr(e){return{...Cr(e.idToken)??{},...Cr(e.accessToken)??{}}}function Xt(e){let t=Sr(e),n=t.email??t.preferred_username??t.sub??"unknown",o=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${n}${o}`}async function Kt(e){let t=Vt(process.cwd(),e.profile),n=t.connection;console.log(m(`Profile "${t.name}" \u2192 ${n.name} (${n.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let o=await ya(n,{manual:e.manual,promptForRedirect:s=>ka({message:s})},s=>console.log(kt(s))),r=Pr(t.name,o);if(Tr(r),Jt(r,t.name,s=>console.log(m(s))),console.log(""),console.log(A(`\u2713 Logged in to "${t.name}" as ${Xt(r)}`)),console.log(m(` access token expires: ${r.expiresAt}`)),console.log(m(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{Pa(process.cwd())?.profiles?.[t.name]&&(console.log(""),await lt(process.cwd(),t.name))}catch{}}function Jt(e,t,n){let r=Sr(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(Vt(process.cwd(),t).project.projectId)return;Ca(process.cwd(),t,{projectId:r}),n(` Project ${r} written to profile "${t}".`)}catch{}}async function Rr(e){let t=Vt(process.cwd(),e.profile),n=t.connection,o=va(t.name);if(!o){console.log(x(`Not logged in to Document360 (profile "${t.name}").`)),console.log(m(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${E(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Xt(o)}`),ba(o))if(o.refreshToken)try{let r=Pr(t.name,await $a(n,o.refreshToken));Tr(r),console.log(A(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(L(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(m(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(L("Session expired (no refresh token).")),console.log(m(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(m(` expires: ${o.expiresAt}`))}async function jr(e){let t=Vt(process.cwd(),e.profile);xa(t.name)?console.log(A(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(m(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}q();import{readProjectConfig as Dr,writeProjectConfig as Ta,resolveActiveProfile as Sa,loadTokens as Ra,isExpired as ja}from"document360-engine";function Qt(e){let t=Dr(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[n,o]of Object.entries(t.profiles)){let r=n===t.defaultProfile?E("\u25CF "):" ",s=o.production?L(" \u26A0 PRODUCTION"):"",c=o.connection.environment??"(inline)",p=Ra(n),g=p?ja(p)&&!p.refreshToken?L("expired"):m("logged in"):m("not logged in");console.log(`${r}${E(n)} \u2192 ${c}${s} [${g}]`)}console.log(""),console.log(m("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Zt(e,t){let n=Dr(e);if(!n?.profiles?.[t]){let r=n?.profiles?Object.keys(n.profiles).join(", "):"(none \u2014 run init)";console.log(x(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}n.defaultProfile=t,Ta(n,e);let o=n.profiles[t].production?L(" \u26A0 PRODUCTION"):"";console.log(A(`\u2713 Default profile is now "${t}"${o}`))}function en(e,t){try{let n=Sa(e,t);console.log(""),console.log(`Profile ${E(n.name)}${n.production?L(" \u26A0 PRODUCTION"):""}`),console.log(m(` api: ${n.connection.apiUrl}`)),console.log(m(` identity: ${n.connection.authorizationUrl}`)),console.log(m(` clientId: ${n.connection.clientId}`)),console.log(m(` scopes: ${n.connection.scopes.join(" ")}`)),console.log(m(` project: ${n.project.projectId??"(set at login)"}`)),console.log(m(` workspace:${n.project.workspaceId?" "+n.project.workspaceId:" (none)"}`)),console.log("")}catch(n){console.log(x(n.message)),process.exitCode=1}}q();import{existsSync as Da,readdirSync as Aa,statSync as Ea}from"node:fs";import{join as Ia}from"node:path";import{apiLogDir as Na}from"document360-engine";function Ar(){let e=Na();if(console.log(""),console.log(`Document360 API logs: ${E(e)}`),!Da(e)){console.log(m(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=Aa(e).filter(n=>n.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(m(" No logs yet \u2014 they appear after the first Document360 API call."));for(let n of t.slice(0,14)){let o=(Ea(Ia(e,n)).size/1024).toFixed(1);console.log(` ${n} ${m(`${o} KB`)}`)}console.log(""),console.log(m("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(m("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}q();import{createSession as _a,resolveAuth as Ma,findByName as Oa,slugify as La,touchSession as Ua,upsertSession as Wa,resolveActiveProfile as Fa}from"document360-engine";async function Er(e,t,n,o,r,s){let c=Ma(n);c.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(m("Using your Claude subscription (no API key set)."));let p=null;try{p=Fa(e,r)}catch(y){console.error(x(`Document360 profile error: ${y.message}`)),process.exit(2)}p.production&&(console.error(L(`\u26A0 Profile "${p.name}" is PRODUCTION.`)),s||(console.error(x("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(m(" --yes given \u2014 proceeding against production.")));let g;if(o){let y=Oa(e,o);y||(console.error(x(`No saved session matches "${o}" in this repo.`)),console.error(m("List sessions inside the REPL with /resume.")),process.exit(2)),g=y.uuid,console.error(m(`Resuming "${y.name}"`))}let w=_a({cwd:e,resume:g,profileName:r,allowProdWrites:s===!0}),$=g??null,R=1;for await(let y of w.send(t))switch(y.type){case"session":if(!$){$=y.sessionId;let b=new Date().toISOString();Wa({uuid:$,name:La(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:b,updatedAt:b})}break;case"text":process.stdout.write(y.delta);break;case"tool":console.error(ee(` \u2699 ${y.name}`));break;case"result":R=y.ok?0:1,console.error(m(`(${y.inputTokens}\u2192${y.outputTokens} tokens`+(y.costUsd>0?`, $${y.costUsd<.01?y.costUsd.toFixed(4):y.costUsd.toFixed(2)}`:"")+")")),y.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${y.message}`)),process.exit(1)}$&&Ua($),process.stdout.write(`
3
+ `),process.exit(R)}import{createInterface as su}from"node:readline/promises";import{createSession as Fo,resolveAuth as iu,getSession as au,setTitle as lu,slugify as cu,touchSession as di,upsertSession as uu,generateTitle as du,resolveActiveProfile as pi,resolveModelSetting as pu,readProjectConfig as fu,decodeJwtClaims as ci,isExpired as mu,loadTokens as gu}from"document360-engine";var yt=[{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:"project",usage:"/project [name]",desc:"Choose the Document360 project (picker; lists all you can access)",group:"start"},{name:"devhints",usage:"/devhints",desc:"Set up the dev\u2192docs hint channel (guide + paste block for your repo agent)",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:"draft",usage:"/draft [<feature>]",desc:"Draft a feature's articles from its PRDs/specs in .d360-writer/input/ + the code",group:"docs"},{name:"write",usage:"/write [--all|--scope <cat>|<path>] [--run]",desc:"Author the planned articles in parallel (bare = preview + cost; --run starts)",group:"docs"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: status; else author capture specs (bulk also refreshes the capture checklist; --no-setup skips)",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Re-run the data-staging checklist + dev test-id requests (auto-runs after /screenshot --all)",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:"logout",usage:"/logout",desc:"Sign out + clear project/workspace selection (then /login to re-pick)",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"}],Ha=[{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 tn(){let e=Math.max(...yt.map(n=>n.usage.length))+2,t=["document360-writer \u2014 commands","",'New here? /init \u2192 "write the docs for this repo" \u2192 /publish',""," Screenshots are optional \u2014 add them anytime:"," /screenshot --all \u2192 d360-capture capture \u2192 re-/publish"];for(let{key:n,label:o}of Ha){t.push("",o);for(let r of yt.filter(s=>s.group===n))t.push(` ${r.usage.padEnd(e)}${r.desc}`)}return t.push("","Tip: anything not starting with / is sent to the agent."),t}function Ir(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return yt.filter(n=>n.name.startsWith(t))}function Nr(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function Bn(){console.log("");for(let e of tn())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}q();import{getSession as Ba}from"document360-engine";async function _r(e,t){let n=t.currentUuid(),o=n?Ba(n):void 0;return console.log(o?m(`
4
4
  (conversation reset \u2014 "${o.name}" is still available via /resume)
5
5
  `):m(`
6
6
  (conversation reset \u2014 agent will start fresh on the next prompt)
7
- `)),{kind:"clear"}}async function qn(){return{kind:"exit"}}q();import{input as $t,confirm as Ba}from"@inquirer/prompts";import{basename as Mr,join as nn}from"node:path";import{existsSync as ct,readFileSync as qa,readdirSync as Ga,writeFileSync as za}from"node:fs";import{writeProjectConfig as Or,readProjectConfig as Ya,projectConfigPath as Lr,writerDir as Va,DEFAULT_DOCS_DIR as Ur,DEFAULT_CAPTURE_DIR as Wr,DEFAULT_OUTPUT_DIR as Fr}from"document360-engine";function Hr(e,t="berlin"){let n=Lr(e);if(ct(n))return{created:!1,path:n,profileName:""};let o={projectId:Gr(e)??Mr(e),docsDir:Ur,captureDir:Wr,outputDir:Fr,profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:zr(e)};return Or(o,e),Br(e),{created:!0,path:n,profileName:t}}function Br(e){let t=nn(Va(e),".gitignore");ct(t)||za(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
8
- `),"utf8")}async function qr(){let e=process.cwd(),t=Lr(e);if(ct(t)&&!await Ba({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(m("init cancelled.")),{kind:"continue"};let n=Gr(e)??Mr(e),o=await $t({message:"Project ID (used to scope sessions, screenshots, etc.):",default:n}),r=await $t({message:"Capture directory (where document360-capture .spec.ts files live):",default:Wr}),s=await $t({message:"Screenshot output directory:",default:Fr}),c=await $t({message:"Default connection profile name:",default:"berlin"}),p=await $t({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),g={projectId:o,docsDir:Ur,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:p},production:!1}},defaultProfile:c,authoritativeSourceFiles:zr(e)},w=Ya(e);return w?.terminologyGlossary&&(g.terminologyGlossary=w.terminologyGlossary),Or(g,e),Br(e),console.log(""),console.log(A(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${E(`d360-writer login --profile ${c}`)} ${m("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function Gr(e){let t=nn(e,"package.json");if(!ct(t))return null;try{return JSON.parse(qa(t,"utf8")).name??null}catch{return null}}function zr(e){let t=[];for(let n of["README.md","ARCHITECTURE.md","CLAUDE.md"])ct(nn(e,n))&&t.push(n);for(let n of["src","api","docs"]){let o=nn(e,n);ct(o)&&!Xa(o)&&t.push(n)}return t}function Xa(e){try{return Ga(e,{withFileTypes:!0}).filter(n=>n.isDirectory()&&!n.name.startsWith(".")).length>6}catch{return!1}}q();import{readMcpConfig as Gn,writeMcpConfig as Yr}from"document360-engine";async function on(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Ka(),{kind:"continue"}):t==="add"?(Ja(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Qa(e.slice(1)),{kind:"continue"}):(console.log(x(`Unknown /mcp subcommand: ${t}`)),console.log(m("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function Ka(){let e=Gn(),t=Object.keys(e.servers);if(t.length===0){console.log(m(`
7
+ `)),{kind:"clear"}}async function qn(){return{kind:"exit"}}q();import{input as $t,confirm as qa}from"@inquirer/prompts";import{basename as Mr,join as nn}from"node:path";import{existsSync as ct,readFileSync as Ga,readdirSync as za,writeFileSync as Ya}from"node:fs";import{writeProjectConfig as Or,readProjectConfig as Va,projectConfigPath as Lr,writerDir as Xa,DEFAULT_DOCS_DIR as Ur,DEFAULT_CAPTURE_DIR as Wr,DEFAULT_OUTPUT_DIR as Fr}from"document360-engine";function Hr(e,t="berlin"){let n=Lr(e);if(ct(n))return{created:!1,path:n,profileName:""};let o={projectId:Gr(e)??Mr(e),docsDir:Ur,captureDir:Wr,outputDir:Fr,profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:zr(e)};return Or(o,e),Br(e),{created:!0,path:n,profileName:t}}function Br(e){let t=nn(Xa(e),".gitignore");ct(t)||Ya(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
8
+ `),"utf8")}async function qr(){let e=process.cwd(),t=Lr(e);if(ct(t)&&!await qa({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(m("init cancelled.")),{kind:"continue"};let n=Gr(e)??Mr(e),o=await $t({message:"Project ID (used to scope sessions, screenshots, etc.):",default:n}),r=await $t({message:"Capture directory (where document360-capture .spec.ts files live):",default:Wr}),s=await $t({message:"Screenshot output directory:",default:Fr}),c=await $t({message:"Default connection profile name:",default:"berlin"}),p=await $t({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),g={projectId:o,docsDir:Ur,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:p},production:!1}},defaultProfile:c,authoritativeSourceFiles:zr(e)},w=Va(e);return w?.terminologyGlossary&&(g.terminologyGlossary=w.terminologyGlossary),Or(g,e),Br(e),console.log(""),console.log(A(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${E(`d360-writer login --profile ${c}`)} ${m("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function Gr(e){let t=nn(e,"package.json");if(!ct(t))return null;try{return JSON.parse(Ga(t,"utf8")).name??null}catch{return null}}function zr(e){let t=[];for(let n of["README.md","ARCHITECTURE.md","CLAUDE.md"])ct(nn(e,n))&&t.push(n);for(let n of["src","api","docs"]){let o=nn(e,n);ct(o)&&!Ka(o)&&t.push(n)}return t}function Ka(e){try{return za(e,{withFileTypes:!0}).filter(n=>n.isDirectory()&&!n.name.startsWith(".")).length>6}catch{return!1}}q();import{readMcpConfig as Gn,writeMcpConfig as Yr}from"document360-engine";async function on(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Ja(),{kind:"continue"}):t==="add"?(Qa(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Za(e.slice(1)),{kind:"continue"}):(console.log(x(`Unknown /mcp subcommand: ${t}`)),console.log(m("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function Ja(){let e=Gn(),t=Object.keys(e.servers);if(t.length===0){console.log(m(`
9
9
  No MCP servers registered. Add one with /mcp add <name> <type> <ref>
10
- `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let n of t){let o=e.servers[n];if(o.type==="stdio")console.log(` ${E(n)} ${m("(stdio)")} ${o.command} ${(o.args??[]).join(" ")}`);else{let r=Object.keys(o.headers??{}),s=r.length>0?m(` [headers: ${r.join(", ")}]`):"";console.log(` ${E(n)} ${m(`(${o.type})`)} ${o.url}${s}`)}}console.log("")}function Ja(e){let[t,n,...o]=e;if(!t||!n||o.length===0){console.log(x("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(n!=="stdio"&&n!=="http"&&n!=="sse"){console.log(x(`Unknown transport: ${n}. Use stdio, http, or sse.`));return}let r=Gn(),s;if(n==="stdio")s={type:"stdio",command:o[0],args:o.slice(1)};else{let c={};for(let p=1;p<o.length;p++)if(o[p]==="-H"||o[p]==="--header"){let g=o[++p],w=g?.match(/^([^:=]+)[:=](.+)$/);if(!w){console.log(x(`-H expects key:value (no spaces). Got: ${g??"(nothing)"}`));return}c[w[1].trim()]=w[2].trim()}else{console.log(x(`Unexpected argument: ${o[p]}. After the URL, only -H key:value is allowed.`));return}s={type:n,url:o[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,Yr(r),console.log(""),console.log(A(`\u2713 Registered "${t}" (${n})`)),console.log(L(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(m(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function Qa(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let n=Gn();if(!n.servers[t]){console.log(x(`No server named "${t}".`));return}delete n.servers[t],Yr(n),console.log(A(`\u2713 Removed "${t}".`)),console.log(m(" Run /clear to drop it from the current session immediately."))}q();import{select as cl}from"@inquirer/prompts";import{computeSyncStatus as eo,planPartitions as ul,relatedArticlePaths as dl,resolveModelForOperation as pl,runPartitioned as fl}from"document360-engine";import z from"picocolors";import Zr from"wrap-ansi";import Jn from"string-width";q();import Ae from"picocolors";import zn from"wrap-ansi";import Yn from"string-width";var Vr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Xr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Kr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function Za(e){let t=e.replace(/\r/g,"").split(`
11
- `),n=[],o=0;for(;o<t.length;){let r=t[o];if(/^\s*```/.test(r)){let p=[];for(o++;o<t.length&&!/^\s*```/.test(t[o]);)p.push(t[o++]);o++,n.push({kind:"code",lines:p});continue}if(Vr(r)){n.push({kind:"hr"}),o++;continue}if(r.includes("|")&&o+1<t.length&&Xr(t[o+1])){let p=Kr(r);o+=2;let g=[];for(;o<t.length&&t[o].includes("|")&&t[o].trim()!=="";)g.push(Kr(t[o++]));n.push({kind:"table",header:p,rows:g});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){n.push({kind:"heading",level:s[1].length,text:s[2]}),o++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let p=[];for(;o<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[o]);)p.push(t[o++].replace(/^\s*([-*]|\d+\.)\s+/,""));n.push({kind:"list",items:p});continue}if(r.trim()===""){o++;continue}let c=[];for(;o<t.length&&t[o].trim()!==""&&!/^\s*```/.test(t[o])&&!/^(#{1,6})\s/.test(t[o])&&!/^\s*([-*]|\d+\.)\s+/.test(t[o])&&!Vr(t[o])&&!(t[o].includes("|")&&o+1<t.length&&Xr(t[o+1]));)c.push(t[o++]);n.push({kind:"para",text:c.join(" ")})}return n}function Vn(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Ae.bold(t.slice(2,-2)):t.startsWith("`")?E(t.slice(1,-1)):Ae.italic(t.slice(1,-1)))}var el=(e,t)=>e+" ".repeat(Math.max(0,t-Yn(e)));function tl(e,t,n){let o=e.length,r=e.map((v,N)=>Math.max(Yn(v),...t.map(_=>Yn(_[N]??"")))),s=Math.max(24,n),c=3*o+1,p=[...r],g=()=>p.reduce((v,N)=>v+N,0)+c,w=0;for(;g()>s&&w++<1e4;){let v=-1,N=6;for(let _=0;_<o;_++)p[_]>N&&(N=p[_],v=_);if(v===-1)break;p[v]-=1}let $=(v,N,_)=>Ae.gray(v+p.map(H=>"\u2500".repeat(H+2)).join(N)+_),R=Ae.gray("\u2502"),y=(v,N)=>{let _=p.map(($e,te)=>{let we=v[te]??"",re=N?Ae.bold(we):Vn(we);return zn(re,$e,{hard:!0}).split(`
12
- `)}),H=Math.max(..._.map($e=>$e.length)),Z=[];for(let $e=0;$e<H;$e++)Z.push(p.map((te,we)=>`${R} ${el(_[we][$e]??"",te)} `).join("")+R);return Z.join(`
10
+ `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let n of t){let o=e.servers[n];if(o.type==="stdio")console.log(` ${E(n)} ${m("(stdio)")} ${o.command} ${(o.args??[]).join(" ")}`);else{let r=Object.keys(o.headers??{}),s=r.length>0?m(` [headers: ${r.join(", ")}]`):"";console.log(` ${E(n)} ${m(`(${o.type})`)} ${o.url}${s}`)}}console.log("")}function Qa(e){let[t,n,...o]=e;if(!t||!n||o.length===0){console.log(x("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(n!=="stdio"&&n!=="http"&&n!=="sse"){console.log(x(`Unknown transport: ${n}. Use stdio, http, or sse.`));return}let r=Gn(),s;if(n==="stdio")s={type:"stdio",command:o[0],args:o.slice(1)};else{let c={};for(let p=1;p<o.length;p++)if(o[p]==="-H"||o[p]==="--header"){let g=o[++p],w=g?.match(/^([^:=]+)[:=](.+)$/);if(!w){console.log(x(`-H expects key:value (no spaces). Got: ${g??"(nothing)"}`));return}c[w[1].trim()]=w[2].trim()}else{console.log(x(`Unexpected argument: ${o[p]}. After the URL, only -H key:value is allowed.`));return}s={type:n,url:o[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,Yr(r),console.log(""),console.log(A(`\u2713 Registered "${t}" (${n})`)),console.log(L(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(m(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function Za(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let n=Gn();if(!n.servers[t]){console.log(x(`No server named "${t}".`));return}delete n.servers[t],Yr(n),console.log(A(`\u2713 Removed "${t}".`)),console.log(m(" Run /clear to drop it from the current session immediately."))}q();import{select as ul}from"@inquirer/prompts";import{computeSyncStatus as eo,planPartitions as dl,relatedArticlePaths as pl,resolveModelForOperation as fl,runPartitioned as ml}from"document360-engine";import z from"picocolors";import Zr from"wrap-ansi";import Jn from"string-width";q();import Ae from"picocolors";import zn from"wrap-ansi";import Yn from"string-width";var Vr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Xr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Kr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function el(e){let t=e.replace(/\r/g,"").split(`
11
+ `),n=[],o=0;for(;o<t.length;){let r=t[o];if(/^\s*```/.test(r)){let p=[];for(o++;o<t.length&&!/^\s*```/.test(t[o]);)p.push(t[o++]);o++,n.push({kind:"code",lines:p});continue}if(Vr(r)){n.push({kind:"hr"}),o++;continue}if(r.includes("|")&&o+1<t.length&&Xr(t[o+1])){let p=Kr(r);o+=2;let g=[];for(;o<t.length&&t[o].includes("|")&&t[o].trim()!=="";)g.push(Kr(t[o++]));n.push({kind:"table",header:p,rows:g});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){n.push({kind:"heading",level:s[1].length,text:s[2]}),o++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let p=[];for(;o<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[o]);)p.push(t[o++].replace(/^\s*([-*]|\d+\.)\s+/,""));n.push({kind:"list",items:p});continue}if(r.trim()===""){o++;continue}let c=[];for(;o<t.length&&t[o].trim()!==""&&!/^\s*```/.test(t[o])&&!/^(#{1,6})\s/.test(t[o])&&!/^\s*([-*]|\d+\.)\s+/.test(t[o])&&!Vr(t[o])&&!(t[o].includes("|")&&o+1<t.length&&Xr(t[o+1]));)c.push(t[o++]);n.push({kind:"para",text:c.join(" ")})}return n}function Vn(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Ae.bold(t.slice(2,-2)):t.startsWith("`")?E(t.slice(1,-1)):Ae.italic(t.slice(1,-1)))}var tl=(e,t)=>e+" ".repeat(Math.max(0,t-Yn(e)));function nl(e,t,n){let o=e.length,r=e.map((v,N)=>Math.max(Yn(v),...t.map(_=>Yn(_[N]??"")))),s=Math.max(24,n),c=3*o+1,p=[...r],g=()=>p.reduce((v,N)=>v+N,0)+c,w=0;for(;g()>s&&w++<1e4;){let v=-1,N=6;for(let _=0;_<o;_++)p[_]>N&&(N=p[_],v=_);if(v===-1)break;p[v]-=1}let $=(v,N,_)=>Ae.gray(v+p.map(H=>"\u2500".repeat(H+2)).join(N)+_),R=Ae.gray("\u2502"),y=(v,N)=>{let _=p.map(($e,te)=>{let we=v[te]??"",re=N?Ae.bold(we):Vn(we);return zn(re,$e,{hard:!0}).split(`
12
+ `)}),H=Math.max(..._.map($e=>$e.length)),Z=[];for(let $e=0;$e<H;$e++)Z.push(p.map((te,we)=>`${R} ${tl(_[we][$e]??"",te)} `).join("")+R);return Z.join(`
13
13
  `)},b=[$("\u250C","\u252C","\u2510"),y(e,!0)];return b.push(t.length===0?$("\u2514","\u2534","\u2518"):$("\u251C","\u253C","\u2524")),t.forEach((v,N)=>{b.push(y(v,!1)),b.push(N===t.length-1?$("\u2514","\u2534","\u2518"):$("\u251C","\u253C","\u2524"))}),b.join(`
14
- `)}function nl(e,t){switch(e.kind){case"heading":return Ae.bold(e.text);case"hr":return Ae.gray("\u2500".repeat(t));case"para":return zn(Vn(e.text),t);case"list":return e.items.map(n=>{let[o="",...r]=zn(Vn(n),Math.max(10,t-4)).split(`
14
+ `)}function ol(e,t){switch(e.kind){case"heading":return Ae.bold(e.text);case"hr":return Ae.gray("\u2500".repeat(t));case"para":return zn(Vn(e.text),t);case"list":return e.items.map(n=>{let[o="",...r]=zn(Vn(n),Math.max(10,t-4)).split(`
15
15
  `);return" \u2022 "+o+r.map(s=>`
16
16
  `+s).join("")}).join(`
17
17
  `);case"code":return e.lines.map(n=>Ae.gray(" "+n)).join(`
18
- `);case"table":return tl(e.header,e.rows,t)}}function Xn(e,t){let n=Math.max(20,t);return Za(e).map(o=>nl(o,n)).join(`
18
+ `);case"table":return nl(e.header,e.rows,t)}}function Xn(e,t){let n=Math.max(20,t);return el(e).map(o=>ol(o,n)).join(`
19
19
 
20
- `)}q();import ol from"picocolors";var rl=[127,86,217],sl=[22,38,43],Jr={T:rl,E:sl},xt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Kn=([e,t,n])=>`\x1B[38;2;${e};${t};${n}m`,il=([e,t,n])=>`\x1B[48;2;${e};${t};${n}m`;function Qr(){if(!ol.isColorSupported)return[];let e=xt[0].length,t=[];for(let n=0;n<xt.length;n+=2){let o="";for(let r=0;r<e;r++){let s=Jr[xt[n][r]],c=n+1<xt.length?Jr[xt[n+1][r]]:void 0;s&&c?o+=`${Kn(s)}${il(c)}\u2580\x1B[49m\x1B[39m`:s?o+=`${Kn(s)}\u2580\x1B[39m`:c?o+=`${Kn(c)}\u2584\x1B[39m`:o+=" "}t.push(o)}return t}function bt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function ae(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Ee(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var ut=(e,t)=>` ${e.padEnd(13)}${t}`;function al(e,t){let n=[ht("\u270E document360-writer")+z.gray(` v${e.version}`),z.gray(" Reads your code, writes your docs."),"",ut("Claude:",`${e.claude}${z.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),ut("Document360:",e.configured?e.loggedOut?z.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?z.gray(` (${e.sessionHint})`):""}`:z.yellow("not set up \u2014 run /init")),ut("Profile:",e.configured?`${e.profile}${z.gray(` (${e.apiUrl})`)}${e.prod?z.bold(z.yellow(" \u26A0 PRODUCTION")):""}`:z.gray("\u2014 (run /init)")),ut("Project:",e.project),ut("Mode:",z.gray(e.mode)),ut("cwd:",z.gray(e.cwd))],o=Qr();if(o.length===0)return n.join(`
20
+ `)}q();import rl from"picocolors";var sl=[127,86,217],il=[22,38,43],Jr={T:sl,E:il},xt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Kn=([e,t,n])=>`\x1B[38;2;${e};${t};${n}m`,al=([e,t,n])=>`\x1B[48;2;${e};${t};${n}m`;function Qr(){if(!rl.isColorSupported)return[];let e=xt[0].length,t=[];for(let n=0;n<xt.length;n+=2){let o="";for(let r=0;r<e;r++){let s=Jr[xt[n][r]],c=n+1<xt.length?Jr[xt[n+1][r]]:void 0;s&&c?o+=`${Kn(s)}${al(c)}\u2580\x1B[49m\x1B[39m`:s?o+=`${Kn(s)}\u2580\x1B[39m`:c?o+=`${Kn(c)}\u2584\x1B[39m`:o+=" "}t.push(o)}return t}function bt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function ae(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Ee(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var ut=(e,t)=>` ${e.padEnd(13)}${t}`;function ll(e,t){let n=[ht("\u270E document360-writer")+z.gray(` v${e.version}`),z.gray(" Reads your code, writes your docs."),"",ut("Claude:",`${e.claude}${z.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),ut("Document360:",e.configured?e.loggedOut?z.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?z.gray(` (${e.sessionHint})`):""}`:z.yellow("not set up \u2014 run /init")),ut("Profile:",e.configured?`${e.profile}${z.gray(` (${e.apiUrl})`)}${e.prod?z.bold(z.yellow(" \u26A0 PRODUCTION")):""}`:z.gray("\u2014 (run /init)")),ut("Project:",e.project),ut("Mode:",z.gray(e.mode)),ut("cwd:",z.gray(e.cwd))],o=Qr();if(o.length===0)return n.join(`
21
21
  `);let r=2,s=3,c=Jn(o[0]);if(!(t>=r+c+s+Math.max(...n.map($=>Jn($)))))return[...o.map($=>" "+$),...n].join(`
22
22
  `);let g=Math.max(0,Math.floor((n.length-o.length)/2)),w=[];for(let $=0;$<Math.max(o.length+g,n.length);$++){let R=o[$-g]??" ".repeat(c);w.push((" ".repeat(r)+R+" ".repeat(s)+(n[$]??"")).trimEnd())}return w.join(`
23
- `)}var ll={error:z.red,warn:z.yellow,ok:z.green,info:z.gray};function Qn(e,t){let n=Math.max(20,t);switch(e.kind){case"banner":return al(e.info,n);case"user":{let o="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,n-4),c=50,p=e.text.split(`
23
+ `)}var cl={error:z.red,warn:z.yellow,ok:z.green,info:z.gray};function Qn(e,t){let n=Math.max(20,t);switch(e.kind){case"banner":return ll(e.info,n);case"user":{let o="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,n-4),c=50,p=e.text.split(`
24
24
  `).flatMap(w=>Zr(w,s,{hard:!0}).split(`
25
25
  `)),g=Math.max(0,p.length-c);return g>0&&(p=[...p.slice(0,c),z.dim(`\u2026 +${g} more lines`)]),`
26
26
  `+p.map((w,$)=>o+($===0?E(" \u276F "):" ")+w+" ".repeat(Math.max(0,s-Jn(w))+1)+r).join(`
@@ -33,21 +33,21 @@ No MCP servers registered. Add one with /mcp add <name> <type> <ref>
33
33
  `+ht(`\u25A3 Preview \u2014 ${e.name}`)+`
34
34
 
35
35
  `+Xn(e.text,n);case"note":return`
36
- `+ll[e.tone](kt(e.text));case"done":return`
36
+ `+cl[e.tone](kt(e.text));case"done":return`
37
37
  `+(e.ok?z.magenta("\u2736 "):z.red("\u2736 "))+z.gray(`Cooked for ${bt(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${ae(e.costUsd)}`:""))}}function es(e,t){return e.map(n=>Qn(n,t)).join(`
38
38
  `)}var vt=3;function to(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(`
39
- `)}var ts={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Ct(e){return e.filter(t=>t.path!==null&&t.status in ts).map(t=>({path:t.path,label:ts[t.status]}))}function ml(e){let t=e.replace(/\\/g,"/").split("/").pop()??e,n=t.replace(/^\d+[-_]/,"").replace(/[-_]+/g," ").trim();return n?n.charAt(0).toUpperCase()+n.slice(1):t}function ns(e){let t=[];e.length>1&&t.push({kind:"all",label:`Publish all ${e.length} articles`,paths:e.map(o=>o.path),indent:!1});let n=new Map;for(let o of e){let r=o.path.replace(/\\/g,"/").split("/").slice(0,-1).join("/"),s=n.get(r);s?s.push(o):n.set(r,[o])}for(let o of[...n.keys()].sort()){let r=n.get(o).sort((s,c)=>s.path.localeCompare(c.path));t.push({kind:"category",label:`${ml(o)} (${r.length})`,paths:r.map(s=>s.path),indent:!1});for(let s of r){let c=s.path.replace(/\\/g,"/").split("/").pop()??s.path;t.push({kind:"article",label:c,paths:[s.path],indent:!0,note:s.label})}}return t}function rn(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(`
40
- `)}function no(e,t,n){let o=y=>t[y.index]?.paths.length??0,r=e.filter(y=>y.ok),s=e.filter(y=>!y.ok),c=e.reduce((y,b)=>y+o(b),0),p=r.reduce((y,b)=>y+o(b),0),g=n==="api"?`${ae(e.reduce((y,b)=>y+b.costUsd,0))} total`:Ee(e.reduce((y,b)=>y+b.outputTokens,0)),w=`${r.length}/${e.length} partition${e.length===1?"":"s"}`,$=y=>`${y} article${y===1?"":"s"}`,R=[];if(s.length===0)R.push(`Published ${$(c)} (${w}) successfully \xB7 ${g}.`),R.push("All are DRAFTS \u2014 review and publish them in the Document360 portal.");else{R.push(`Published ${p}/${$(c)} (${w}) \xB7 ${g}.`),R.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /publish --all to retry just the rest (published articles are skipped):`);for(let y of s)R.push(` \u2717 ${y.label}${y.error?` \u2014 ${y.error}`:""}`)}return R}async function Zn(e,t){let n=ul(t),{model:o}=pl(e.cwd,"standard");console.log(m(`Publishing ${t.length} article${t.length===1?"":"s"} across ${n.length} partition${n.length===1?"":"s"} (\u2264${vt} agents at once) on ${o}\u2026`)),console.log(m(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let r of fl({cwd:e.cwd,partitions:n,promptFor:s=>rn(s.paths),concurrency:vt,profileName:e.profileName,allowProdWrites:e.allowProdWrites(),model:o}))if(r.type==="partition_status")r.status==="running"?console.log(m(` \u25B8 ${r.label} \u2014 publishing\u2026`)):r.status==="done"?console.log(A(` \u2713 ${r.label}`)):console.log(x(` \u2717 ${r.label}`));else if(r.type==="run_done"){console.log("");let s=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let c of no(r.results,n,s))console.log(r.ok?A(c):L(c))}}catch(r){console.log(x(`Publish run failed: ${r.message}`))}return console.log(""),{kind:"continue"}}async function sn(e,t,n){let o=dl(e,n);if(o.length===0)return[];let r=new Set(Ct((await eo({cwd:e,profileName:t})).entries).map(s=>s.path));return o.filter(s=>r.has(s))}async function os(e,t){let n=e[0];if(n==="--related"&&e[1]){if(!t)return{kind:"continue"};let o=await sn(t.cwd,t.profileName,e[1].replace(/\\/g,"/"));return o.length===0?(console.log(A("\u2713 All related articles are already published.")),{kind:"continue"}):await Zn(t,o)}if(n==="--all"){console.log(m("Checking what needs publishing\u2026"));try{let o=Ct((await eo({cwd:t?.cwd??process.cwd()})).entries);return o.length===0?(console.log(A("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):t?await Zn(t,o.map(r=>r.path)):{kind:"forward-to-agent",prompt:rn(o.map(r=>r.path)),display:"/publish --all"}}catch(o){return console.log(x(`Could not compute sync status: ${o.message}`)),{kind:"continue"}}}if(!n){console.log(m("Checking what needs publishing\u2026"));let o;try{o=Ct((await eo({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(x(`Could not compute sync status: ${r.message}`)),console.log(m("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(o.length===0)return console.log(A("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(m(" (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 o)console.log(` ${r.path} ${m(`(${r.label})`)}`);return console.log(m("Run: /publish <article-path>")),{kind:"continue"}}try{n=await t.withPausedInput(()=>cl({message:"Publish which article?",choices:[...o.length>1?[{name:`All ${o.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...o.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(m("Cancelled.")),{kind:"continue"}}if(n==="--all")return await Zn(t,o.map(r=>r.path))}return{kind:"forward-to-agent",prompt:to(n),display:`/publish ${n}`}}async function an(){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.","2.5. d360_pending_hints \u2014 the source agent's own notes on what it shipped. Verify each (sources exist, commit is an ancestor of HEAD, source shows the surface) before trusting; fold verified ones into the table, flag the rest.","3. Map changes (diff + hints) 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 and stamp consumed hints.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
41
- `),display:"/audit"}}q();import{checkbox as gl}from"@inquirer/prompts";import{inventoryRepo as hl,readProjectConfig as rs,writeProjectConfig as wl}from"document360-engine";function oo(e,t){let n=rs(e);n&&(n.authoritativeSourceFiles=t,wl(n,e))}function ln(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 ss(e,t){let n=t?.cwd??process.cwd();if(!rs(n))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=hl(n);if(o.length===0)return console.log(m('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(E("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of o)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${m(ln(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>gl({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:o.map(s=>({name:`${s.path} (${ln(s)})`,value:s.path,checked:s.recommended})),pageSize:20}))}catch{return console.log(m("Cancelled.")),{kind:"continue"}}if(r.length===0)return console.log(m("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};oo(n,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"}}q();import{confirm as bl}from"@inquirer/prompts";import{applyPull as us,computeSyncStatus as ds,planPull as vl,D360AuthError as Cl}from"document360-engine";q();var is=[{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:L,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:E,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:ee,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:m,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:m,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:ee,mark:"\xB7"}];function kl(e){return e.path?e.path:`${e.title??"(untitled)"} ${ee(`[${e.articleId}]`)}`}function cn(e){let t=[];for(let c of is){let p=e.entries.filter(g=>g.status===c.status);if(p.length!==0){t.push(""),t.push(Ce(c.header));for(let g of p)t.push(` ${c.paint(c.mark)} ${c.paint(kl(g))}${g.detail?ee(` (${g.detail})`):""}`)}}let n=e.counts["in-sync"]??0,o=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=o===n?A("\u2713 everything in sync"):[n>0?A(`\u2713 ${n} in sync`):null,...is.map(c=>{let p=e.counts[c.status]??0;return p>0?`${p} ${r[c.status]}`:null})].filter(Boolean).join(ee(" \xB7 "));return t.push(s+ee(` \xB7 ${o} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as yl}from"diff";import as from"picocolors";var ls=80,$l="\x1B[48;2;74;28;28m",xl="\x1B[48;2;24;66;24m",cs="\x1B[49m",ro=e=>String(e).padStart(5);function qe(e,t,n){let o=y=>{let b=y.replace(/\r\n/g,`
39
+ `)}var ts={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Ct(e){return e.filter(t=>t.path!==null&&t.status in ts).map(t=>({path:t.path,label:ts[t.status]}))}function gl(e){let t=e.replace(/\\/g,"/").split("/").pop()??e,n=t.replace(/^\d+[-_]/,"").replace(/[-_]+/g," ").trim();return n?n.charAt(0).toUpperCase()+n.slice(1):t}function ns(e){let t=[];e.length>1&&t.push({kind:"all",label:`Publish all ${e.length} articles`,paths:e.map(o=>o.path),indent:!1});let n=new Map;for(let o of e){let r=o.path.replace(/\\/g,"/").split("/").slice(0,-1).join("/"),s=n.get(r);s?s.push(o):n.set(r,[o])}for(let o of[...n.keys()].sort()){let r=n.get(o).sort((s,c)=>s.path.localeCompare(c.path));t.push({kind:"category",label:`${gl(o)} (${r.length})`,paths:r.map(s=>s.path),indent:!1});for(let s of r){let c=s.path.replace(/\\/g,"/").split("/").pop()??s.path;t.push({kind:"article",label:c,paths:[s.path],indent:!0,note:s.label})}}return t}function rn(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(`
40
+ `)}function no(e,t,n){let o=y=>t[y.index]?.paths.length??0,r=e.filter(y=>y.ok),s=e.filter(y=>!y.ok),c=e.reduce((y,b)=>y+o(b),0),p=r.reduce((y,b)=>y+o(b),0),g=n==="api"?`${ae(e.reduce((y,b)=>y+b.costUsd,0))} total`:Ee(e.reduce((y,b)=>y+b.outputTokens,0)),w=`${r.length}/${e.length} partition${e.length===1?"":"s"}`,$=y=>`${y} article${y===1?"":"s"}`,R=[];if(s.length===0)R.push(`Published ${$(c)} (${w}) successfully \xB7 ${g}.`),R.push("All are DRAFTS \u2014 review and publish them in the Document360 portal.");else{R.push(`Published ${p}/${$(c)} (${w}) \xB7 ${g}.`),R.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /publish --all to retry just the rest (published articles are skipped):`);for(let y of s)R.push(` \u2717 ${y.label}${y.error?` \u2014 ${y.error}`:""}`)}return R}async function Zn(e,t){let n=dl(t),{model:o}=fl(e.cwd,"standard");console.log(m(`Publishing ${t.length} article${t.length===1?"":"s"} across ${n.length} partition${n.length===1?"":"s"} (\u2264${vt} agents at once) on ${o}\u2026`)),console.log(m(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let r of ml({cwd:e.cwd,partitions:n,promptFor:s=>rn(s.paths),concurrency:vt,profileName:e.profileName,allowProdWrites:e.allowProdWrites(),model:o}))if(r.type==="partition_status")r.status==="running"?console.log(m(` \u25B8 ${r.label} \u2014 publishing\u2026`)):r.status==="done"?console.log(A(` \u2713 ${r.label}`)):console.log(x(` \u2717 ${r.label}`));else if(r.type==="run_done"){console.log("");let s=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let c of no(r.results,n,s))console.log(r.ok?A(c):L(c))}}catch(r){console.log(x(`Publish run failed: ${r.message}`))}return console.log(""),{kind:"continue"}}async function sn(e,t,n){let o=pl(e,n);if(o.length===0)return[];let r=new Set(Ct((await eo({cwd:e,profileName:t})).entries).map(s=>s.path));return o.filter(s=>r.has(s))}async function os(e,t){let n=e[0];if(n==="--related"&&e[1]){if(!t)return{kind:"continue"};let o=await sn(t.cwd,t.profileName,e[1].replace(/\\/g,"/"));return o.length===0?(console.log(A("\u2713 All related articles are already published.")),{kind:"continue"}):await Zn(t,o)}if(n==="--all"){console.log(m("Checking what needs publishing\u2026"));try{let o=Ct((await eo({cwd:t?.cwd??process.cwd()})).entries);return o.length===0?(console.log(A("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):t?await Zn(t,o.map(r=>r.path)):{kind:"forward-to-agent",prompt:rn(o.map(r=>r.path)),display:"/publish --all"}}catch(o){return console.log(x(`Could not compute sync status: ${o.message}`)),{kind:"continue"}}}if(!n){console.log(m("Checking what needs publishing\u2026"));let o;try{o=Ct((await eo({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(x(`Could not compute sync status: ${r.message}`)),console.log(m("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(o.length===0)return console.log(A("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(m(" (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 o)console.log(` ${r.path} ${m(`(${r.label})`)}`);return console.log(m("Run: /publish <article-path>")),{kind:"continue"}}try{n=await t.withPausedInput(()=>ul({message:"Publish which article?",choices:[...o.length>1?[{name:`All ${o.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...o.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(m("Cancelled.")),{kind:"continue"}}if(n==="--all")return await Zn(t,o.map(r=>r.path))}return{kind:"forward-to-agent",prompt:to(n),display:`/publish ${n}`}}async function an(){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.","2.5. d360_pending_hints \u2014 the source agent's own notes on what it shipped. Verify each (sources exist, commit is an ancestor of HEAD, source shows the surface) before trusting; fold verified ones into the table, flag the rest.","3. Map changes (diff + hints) 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 and stamp consumed hints.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
41
+ `),display:"/audit"}}q();import{checkbox as hl}from"@inquirer/prompts";import{inventoryRepo as wl,readProjectConfig as rs,writeProjectConfig as kl}from"document360-engine";function oo(e,t){let n=rs(e);n&&(n.authoritativeSourceFiles=t,kl(n,e))}function ln(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 ss(e,t){let n=t?.cwd??process.cwd();if(!rs(n))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=wl(n);if(o.length===0)return console.log(m('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(E("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of o)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${m(ln(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>hl({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:o.map(s=>({name:`${s.path} (${ln(s)})`,value:s.path,checked:s.recommended})),pageSize:20}))}catch{return console.log(m("Cancelled.")),{kind:"continue"}}if(r.length===0)return console.log(m("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};oo(n,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"}}q();import{confirm as vl}from"@inquirer/prompts";import{applyPull as us,computeSyncStatus as ds,planPull as Cl,D360AuthError as Pl}from"document360-engine";q();var is=[{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:L,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:E,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:ee,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:m,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:m,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:ee,mark:"\xB7"}];function yl(e){return e.path?e.path:`${e.title??"(untitled)"} ${ee(`[${e.articleId}]`)}`}function cn(e){let t=[];for(let c of is){let p=e.entries.filter(g=>g.status===c.status);if(p.length!==0){t.push(""),t.push(Ce(c.header));for(let g of p)t.push(` ${c.paint(c.mark)} ${c.paint(yl(g))}${g.detail?ee(` (${g.detail})`):""}`)}}let n=e.counts["in-sync"]??0,o=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=o===n?A("\u2713 everything in sync"):[n>0?A(`\u2713 ${n} in sync`):null,...is.map(c=>{let p=e.counts[c.status]??0;return p>0?`${p} ${r[c.status]}`:null})].filter(Boolean).join(ee(" \xB7 "));return t.push(s+ee(` \xB7 ${o} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as $l}from"diff";import as from"picocolors";var ls=80,xl="\x1B[48;2;74;28;28m",bl="\x1B[48;2;24;66;24m",cs="\x1B[49m",ro=e=>String(e).padStart(5);function qe(e,t,n){let o=y=>{let b=y.replace(/\r\n/g,`
42
42
  `);return b.endsWith(`
43
43
  `)||b===""?b:b+`
44
- `},r=o(e),s=o(t);if(r===s)return null;let c=Math.max(20,n-10),p=yl("a","b",r,s,"","",{context:3}),g=0,w=0,$=[];p.hunks.forEach((y,b)=>{b>0&&$.push(as.gray(" \u2026"));let v=y.oldStart,N=y.newStart;for(let _ of y.lines){let H=_[0],Z=_.slice(1).slice(0,c);H==="-"?(w++,$.push(`${$l}${ro(v++)} - ${Z}${cs}`)):H==="+"?(g++,$.push(`${xl}${ro(N++)} + ${Z}${cs}`)):($.push(as.gray(ro(N))+" "+Z),v++,N++)}});let R=$.slice(0,ls);return{added:g,removed:w,lines:R,hidden:Math.max(0,$.length-ls)}}async function ps(e,t){let n=(e[0]??"status").toLowerCase();try{if(n==="status")return await Pl(t.cwd),{kind:"continue"};if(n==="pull")return await Tl(t,e.slice(1)),{kind:"continue"};console.log(x(`Unknown subcommand: /sync ${n}`)),console.log(m("Usage: /sync drift report (local vs Document360)")),console.log(m(" /sync pull <path> pull portal edits into the local file")),console.log(m(" /sync pull --all pull every remote-ahead article"))}catch(o){o instanceof Cl?console.log(x(o.message)):console.log(x(`Sync failed: ${o.message}`))}return{kind:"continue"}}async function Pl(e){console.log(m("Checking Document360 for drift\u2026"));let t=await ds({cwd:e});for(let n of cn(t))console.log(n);console.log("")}async function Tl(e,t){let n=t[0];if(!n){console.log(x("Usage: /sync pull <article-path> | --all"));return}let o;if(n==="--all"){if(console.log(m("Checking Document360 for drift\u2026")),o=(await ds({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),o.length===0){console.log(A("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(m(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${Ce(String(o.length))} article(s) are remote-ahead.`)}else o=[n.replace(/\\/g,"/")];for(let r of o){let s=await vl({cwd:e.cwd,relPath:r});console.log(""),console.log(`${E("\u25CF")} ${Ce(s.title)} ${ee(`(${s.path})`)}`);for(let w of s.notes)console.log(L(` \u26A0 ${w}`));s.overwritesLocalChanges&&console.log(L(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=qe(s.oldContent,s.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!c){console.log(m(" Local file already matches the remote content \u2014 advancing the sync base only.")),us({cwd:e.cwd},s);continue}let p=w=>w===1?"":"s";console.log(ee(` \u23BF Added ${c.added} line${p(c.added)}, removed ${c.removed} line${p(c.removed)}`));for(let w of c.lines)console.log(w);if(c.hidden>0&&console.log(m(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>bl({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(m(" Skipped."));continue}us({cwd:e.cwd},s),console.log(A(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}q();import{statSync as Sl}from"node:fs";import{resolve as Rl}from"node:path";import{estimateBulkCost as jl,planPartitions as Dl,readProjectConfig as Al,resolveModelForOperation as El,runPartitioned as Il,trackedArticlePaths as Nl}from"document360-engine";var so=3;function io(e){let t,n=!1;for(let r=0;r<e.length;r++){let s=e[r];s==="--run"||s==="--yes"?n=!0:s==="--scope"?t=e[++r]:s?.startsWith("--scope=")&&(t=s.slice(8))}return{scope:t?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0,run:n}}function ao(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function lo(e,t){return t.map(n=>{let o=0;try{o=Sl(Rl(e,n)).size}catch{o=0}return{path:n,bytes:o}})}function co(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
+ `},r=o(e),s=o(t);if(r===s)return null;let c=Math.max(20,n-10),p=$l("a","b",r,s,"","",{context:3}),g=0,w=0,$=[];p.hunks.forEach((y,b)=>{b>0&&$.push(as.gray(" \u2026"));let v=y.oldStart,N=y.newStart;for(let _ of y.lines){let H=_[0],Z=_.slice(1).slice(0,c);H==="-"?(w++,$.push(`${xl}${ro(v++)} - ${Z}${cs}`)):H==="+"?(g++,$.push(`${bl}${ro(N++)} + ${Z}${cs}`)):($.push(as.gray(ro(N))+" "+Z),v++,N++)}});let R=$.slice(0,ls);return{added:g,removed:w,lines:R,hidden:Math.max(0,$.length-ls)}}async function ps(e,t){let n=(e[0]??"status").toLowerCase();try{if(n==="status")return await Tl(t.cwd),{kind:"continue"};if(n==="pull")return await Sl(t,e.slice(1)),{kind:"continue"};console.log(x(`Unknown subcommand: /sync ${n}`)),console.log(m("Usage: /sync drift report (local vs Document360)")),console.log(m(" /sync pull <path> pull portal edits into the local file")),console.log(m(" /sync pull --all pull every remote-ahead article"))}catch(o){o instanceof Pl?console.log(x(o.message)):console.log(x(`Sync failed: ${o.message}`))}return{kind:"continue"}}async function Tl(e){console.log(m("Checking Document360 for drift\u2026"));let t=await ds({cwd:e});for(let n of cn(t))console.log(n);console.log("")}async function Sl(e,t){let n=t[0];if(!n){console.log(x("Usage: /sync pull <article-path> | --all"));return}let o;if(n==="--all"){if(console.log(m("Checking Document360 for drift\u2026")),o=(await ds({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),o.length===0){console.log(A("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(m(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${Ce(String(o.length))} article(s) are remote-ahead.`)}else o=[n.replace(/\\/g,"/")];for(let r of o){let s=await Cl({cwd:e.cwd,relPath:r});console.log(""),console.log(`${E("\u25CF")} ${Ce(s.title)} ${ee(`(${s.path})`)}`);for(let w of s.notes)console.log(L(` \u26A0 ${w}`));s.overwritesLocalChanges&&console.log(L(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=qe(s.oldContent,s.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!c){console.log(m(" Local file already matches the remote content \u2014 advancing the sync base only.")),us({cwd:e.cwd},s);continue}let p=w=>w===1?"":"s";console.log(ee(` \u23BF Added ${c.added} line${p(c.added)}, removed ${c.removed} line${p(c.removed)}`));for(let w of c.lines)console.log(w);if(c.hidden>0&&console.log(m(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>vl({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(m(" Skipped."));continue}us({cwd:e.cwd},s),console.log(A(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}q();import{statSync as Rl}from"node:fs";import{resolve as jl}from"node:path";import{estimateBulkCost as Dl,planPartitions as Al,readProjectConfig as El,resolveModelForOperation as Il,runPartitioned as Nl,trackedArticlePaths as _l}from"document360-engine";var so=3;function io(e){let t,n=!1;for(let r=0;r<e.length;r++){let s=e[r];s==="--run"||s==="--yes"?n=!0:s==="--scope"?t=e[++r]:s?.startsWith("--scope=")&&(t=s.slice(8))}return{scope:t?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0,run:n}}function ao(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function lo(e,t){return t.map(n=>{let o=0;try{o=Rl(jl(e,n)).size}catch{o=0}return{path:n,bytes:o}})}function co(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:
45
45
  - 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.
46
46
  - 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.
47
47
  - 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.
48
48
  - Work only on these files:
49
49
  `+e.paths.map(t=>`- ${t}`).join(`
50
- `)}function uo(e,t,n){let o=e.reduce((p,g)=>p+g.paths.length,0),[r,s]=t.usd;return[`Convert ${o} article${o===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(p=>` \u2022 ${p.label} \u2014 ${p.paths.length} article${p.paths.length===1?"":"s"}`),"",`Estimated cost: ${ae(r)}\u2013${ae(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function po(e,t,n){let o=v=>t[v.index]?.paths.length??0,r=e.filter(v=>v.ok),s=e.filter(v=>!v.ok),c=e.reduce((v,N)=>v+o(N),0),p=r.reduce((v,N)=>v+o(N),0),g=e.reduce((v,N)=>v+N.costUsd,0),w=e.reduce((v,N)=>v+N.outputTokens,0),$=n==="api"?`${ae(g)} total`:Ee(w),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,y=v=>`${v} article${v===1?"":"s"}`,b=[];if(s.length===0)b.push(`Converted ${y(c)} (${R}) successfully \xB7 ${$}.`);else{b.push(`Converted ${p}/${y(c)} (${R}) \xB7 ${$}.`),b.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let v of s)b.push(` \u2717 ${v.label}${v.error?` \u2014 ${v.error}`:""}`)}return b}async function fs(e,t){if(!Al(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:n,run:o}=io(e),r=Nl(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=ao(r,n);if(s.length===0)return console.log(x(`No tracked articles under "${n}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=Dl(s),p=`/convert${n?` --scope ${n}`:""} --run`,{model:g,forced:w}=El(t.cwd,"light");if(!o){let $=jl({files:lo(t.cwd,s),op:"convert",model:g});n&&console.log(m(`Scope: ${n} (${s.length} of ${r.length} tracked articles).`));for(let R of uo(c,$,so))console.log(R);return console.log(m(`Model: ${g}${w?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(m(`Run ${p} to start.`)),console.log(""),{kind:"continue"}}console.log(m(`Converting ${s.length} articles across ${c.length} partitions (\u2264${so} agents at once) on ${g}\u2026`)),console.log(m(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let $ of Il({cwd:t.cwd,partitions:c,promptFor:co,concurrency:so,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:g}))if($.type==="partition_status")$.status==="running"?console.log(m(` \u25B8 ${$.label} \u2014 converting\u2026`)):$.status==="done"?console.log(A(` \u2713 ${$.label}`)):console.log(x(` \u2717 ${$.label}`));else if($.type==="run_done"){console.log("");let R=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let y of po($.results,c,R))console.log($.ok?A(y):L(y))}}catch($){console.log(x(`Convert run failed: ${$.message}`))}return console.log(""),{kind:"continue"}}q();import{statSync as ms}from"node:fs";import{resolve as gs}from"node:path";import{estimateBulkCost as _l,planPartitions as Ml,readDocsPlan as Ol,readProjectConfig as hs,DEFAULT_DOCS_DIR as Ll,resolveModelForOperation as Ul,runPartitioned as Wl}from"document360-engine";var fo=5;function mo(e){let t,n,o=!1;for(let s=0;s<e.length;s++){let c=e[s];if(c==="--run"||c==="--yes")o=!0;else{if(c==="--all")continue;c==="--scope"?t=e[++s]:c?.startsWith("--scope=")?t=c.slice(8):c&&!c.startsWith("--")&&(n=c)}}let r=s=>s?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0;return{scope:r(t),path:r(n),run:o}}function Fl(e,t){let n=e.replace(/\\/g,"/").replace(/\/+$/,""),o=`${t.replace(/\/+$/,"")}/`;return n.startsWith(o)?n.slice(o.length):n}function Hl(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function Bl(e,t,n){return n.filter(o=>{try{return ms(gs(e,t,o)).size===0}catch{return!0}})}function go(e,t,n){let o=new Map(t.map(r=>[r.path,r]));return n.map(r=>{let s=1;for(let c of o.get(r)?.sources??[])try{s+=ms(gs(e,c)).size}catch{}return{path:r,bytes:s}})}function ho(e,t){return`Run the write-article skill to author EACH documentation article listed below. For each path:
50
+ `)}function uo(e,t,n){let o=e.reduce((p,g)=>p+g.paths.length,0),[r,s]=t.usd;return[`Convert ${o} article${o===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(p=>` \u2022 ${p.label} \u2014 ${p.paths.length} article${p.paths.length===1?"":"s"}`),"",`Estimated cost: ${ae(r)}\u2013${ae(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function po(e,t,n){let o=v=>t[v.index]?.paths.length??0,r=e.filter(v=>v.ok),s=e.filter(v=>!v.ok),c=e.reduce((v,N)=>v+o(N),0),p=r.reduce((v,N)=>v+o(N),0),g=e.reduce((v,N)=>v+N.costUsd,0),w=e.reduce((v,N)=>v+N.outputTokens,0),$=n==="api"?`${ae(g)} total`:Ee(w),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,y=v=>`${v} article${v===1?"":"s"}`,b=[];if(s.length===0)b.push(`Converted ${y(c)} (${R}) successfully \xB7 ${$}.`);else{b.push(`Converted ${p}/${y(c)} (${R}) \xB7 ${$}.`),b.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let v of s)b.push(` \u2717 ${v.label}${v.error?` \u2014 ${v.error}`:""}`)}return b}async function fs(e,t){if(!El(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:n,run:o}=io(e),r=_l(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=ao(r,n);if(s.length===0)return console.log(x(`No tracked articles under "${n}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=Al(s),p=`/convert${n?` --scope ${n}`:""} --run`,{model:g,forced:w}=Il(t.cwd,"light");if(!o){let $=Dl({files:lo(t.cwd,s),op:"convert",model:g});n&&console.log(m(`Scope: ${n} (${s.length} of ${r.length} tracked articles).`));for(let R of uo(c,$,so))console.log(R);return console.log(m(`Model: ${g}${w?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(m(`Run ${p} to start.`)),console.log(""),{kind:"continue"}}console.log(m(`Converting ${s.length} articles across ${c.length} partitions (\u2264${so} agents at once) on ${g}\u2026`)),console.log(m(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let $ of Nl({cwd:t.cwd,partitions:c,promptFor:co,concurrency:so,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:g}))if($.type==="partition_status")$.status==="running"?console.log(m(` \u25B8 ${$.label} \u2014 converting\u2026`)):$.status==="done"?console.log(A(` \u2713 ${$.label}`)):console.log(x(` \u2717 ${$.label}`));else if($.type==="run_done"){console.log("");let R=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let y of po($.results,c,R))console.log($.ok?A(y):L(y))}}catch($){console.log(x(`Convert run failed: ${$.message}`))}return console.log(""),{kind:"continue"}}q();import{statSync as ms}from"node:fs";import{resolve as gs}from"node:path";import{estimateBulkCost as Ml,planPartitions as Ol,readDocsPlan as Ll,readProjectConfig as hs,DEFAULT_DOCS_DIR as Ul,resolveModelForOperation as Wl,runPartitioned as Fl}from"document360-engine";var fo=5;function mo(e){let t,n,o=!1;for(let s=0;s<e.length;s++){let c=e[s];if(c==="--run"||c==="--yes")o=!0;else{if(c==="--all")continue;c==="--scope"?t=e[++s]:c?.startsWith("--scope=")?t=c.slice(8):c&&!c.startsWith("--")&&(n=c)}}let r=s=>s?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0;return{scope:r(t),path:r(n),run:o}}function Hl(e,t){let n=e.replace(/\\/g,"/").replace(/\/+$/,""),o=`${t.replace(/\/+$/,"")}/`;return n.startsWith(o)?n.slice(o.length):n}function Bl(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function ql(e,t,n){return n.filter(o=>{try{return ms(gs(e,t,o)).size===0}catch{return!0}})}function go(e,t,n){let o=new Map(t.map(r=>[r.path,r]));return n.map(r=>{let s=1;for(let c of o.get(r)?.sources??[])try{s+=ms(gs(e,c)).size}catch{}return{path:r,bytes:s}})}function ho(e,t){return`Run the write-article skill to author EACH documentation article listed below. For each path:
51
51
  - It is relative to ${t}/. Look up its entry (matching "path") in .d360-writer/plan.json for its purpose and the source files it draws from (AGENT-PLAN.md has the same table).
52
52
  - Read ONLY those source files plus any shared context you genuinely need, and ground every statement in them. Never invent UI labels, routes, or behaviour \u2014 if the source does not say it, omit it.
53
53
  - Write the finished article to ${t}/<path>, following the write-article structure and the project terminologyGlossary. Add the \`sources:\` frontmatter listing the files you actually read.
@@ -55,15 +55,15 @@ No MCP servers registered. Add one with /mcp add <name> <type> <ref>
55
55
  - Each article is independent. Do NOT write, edit, or publish any article not in this list, and do NOT publish to Document360 \u2014 these are local drafts.
56
56
  Articles:
57
57
  `+e.paths.map(n=>`- ${n}`).join(`
58
- `)}function wo(e,t,n){let o=e.reduce((c,p)=>c+p.paths.length,0),[r,s]=t.usd;return[`Write ${o} article${o===1?"":"s"} across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(c=>` \u2022 ${c.label} \u2014 ${c.paths.length} article${c.paths.length===1?"":"s"}`),"",`Estimated cost: ${ae(r)}\u2013${ae(s)}. ${t.note}`,"","Articles are written as local drafts in the docs tree (nothing is published). Run /write --run to start."]}function ko(e,t,n){let o=v=>t[v.index]?.paths.length??0,r=e.filter(v=>v.ok),s=e.filter(v=>!v.ok),c=e.reduce((v,N)=>v+o(N),0),p=r.reduce((v,N)=>v+o(N),0),g=e.reduce((v,N)=>v+N.costUsd,0),w=e.reduce((v,N)=>v+N.outputTokens,0),$=n==="api"?`${ae(g)} total`:Ee(w),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,y=v=>`${v} article${v===1?"":"s"}`,b=[];if(s.length===0)b.push(`Wrote ${y(c)} (${R}) successfully \xB7 ${$}.`),b.push("Next \u2014 push them to Document360:"),b.push("/publish --all"),b.push("Or add screenshots first with /screenshot --all. Drafts are reviewable in the portal after publishing.");else{b.push(`Wrote ${p}/${y(c)} (${R}) \xB7 ${$}.`),b.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /write to retry the rest:`);for(let v of s)b.push(` \u2717 ${v.label}${v.error?` \u2014 ${v.error}`:""}`)}return b}function yo(e,t,n){let o=(hs(e)?.docsDir??Ll).replace(/\/+$/,""),r=t.map(p=>p.path);if(n.path){let p=Fl(n.path,o);return r.includes(p)?{docsDir:o,targets:[p],planCount:r.length}:{docsDir:o,targets:[],planCount:r.length,reason:`"${p}" isn't in the docs plan (.d360-writer/plan.json).`}}let s=Hl(r,n.scope);if(n.scope&&s.length===0)return{docsDir:o,targets:[],planCount:r.length,reason:`No planned articles under "${n.scope}".`};let c=Bl(e,o,s);if(c.length===0){let p=n.scope?` under "${n.scope}"`:"";return{docsDir:o,targets:[],planCount:r.length,reason:`All planned articles${p} are already written. Use /write <path> to rewrite one.`}}return{docsDir:o,targets:c,planCount:r.length}}async function ws(e,t){if(!hs(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let n=Ol(t.cwd);if(n.length===0)return console.log(x("No docs plan found (.d360-writer/plan.json). Analyze the repo and propose a structure first.")),{kind:"continue"};let{scope:o,path:r,run:s}=mo(e),{docsDir:c,targets:p,reason:g}=yo(t.cwd,n,{scope:o,path:r});if(p.length===0)return console.log(g?L(g):L("Nothing to write.")),{kind:"continue"};let w=Ml(p),{model:$,forced:R}=Ul(t.cwd,"standard");if(!s&&!!!r){let b=_l({files:go(t.cwd,n,p),op:"write",model:$});o&&console.log(m(`Scope: ${o} (${p.length} pending).`));for(let v of wo(w,b,fo))console.log(v);return console.log(m(`Model: ${$}${R?" (forced)":" \u2014 authoring; /model to override"}.`)),console.log(m(`Run /write${o?` --scope ${o}`:" --all"} --run to start.`)),console.log(""),{kind:"continue"}}console.log(m(`Writing ${p.length} article${p.length===1?"":"s"} across ${w.length} partition${w.length===1?"":"s"} (\u2264${fo} agents at once) on ${$}\u2026`)),console.log(m(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let b of Wl({cwd:t.cwd,partitions:w,promptFor:v=>ho(v,c),concurrency:fo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:$}))if(b.type==="partition_status")b.status==="running"?console.log(m(` \u25B8 ${b.label} \u2014 writing\u2026`)):b.status==="done"?console.log(A(` \u2713 ${b.label}`)):console.log(x(` \u2717 ${b.label}`));else if(b.type==="run_done"){console.log("");let v=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let N of ko(b.results,w,v))console.log(b.ok?A(N):L(N))}}catch(b){console.log(x(`Write run failed: ${b.message}`))}return console.log(""),{kind:"continue"}}q();import{existsSync as ql,readdirSync as ks,statSync as ys}from"node:fs";import{join as $s}from"node:path";import{inputDir as Gl,readProjectConfig as zl}from"document360-engine";var xs=/\.(md|markdown|pdf|txt)$/i;function $o(e){let t=Gl(e);if(!ql(t))return{features:[],looseDocs:0};let n=[],o=0;for(let r of ks(t)){let s=$s(t,r),c=!1;try{c=ys(s).isDirectory()}catch{continue}if(c){if(r.startsWith("."))continue;let p=Yl(s);p>0&&n.push({name:r,docCount:p})}else xs.test(r)&&o++}return n.sort((r,s)=>r.name.localeCompare(s.name)),{features:n,looseDocs:o}}function Yl(e){let t=0;for(let n of ks(e))try{ys($s(e,n)).isFile()&&xs.test(n)&&t++}catch{}return t}function xo(e,t){let n=t.trim().toLowerCase();return n?e.find(o=>o.name.toLowerCase()===n)?.name??e.find(o=>o.name.toLowerCase().startsWith(n))?.name??e.find(o=>o.name.toLowerCase().includes(n))?.name??null:null}function un(e,t){return["Run the draft-from-prd skill.","",`Target scope: ${e?`the feature documents in .d360-writer/input/${e}/`:t?`the input docs in .d360-writer/input/ relevant to: "${t}"`:"the input docs in .d360-writer/input/"}.`,t&&!e?`Use "${t}" as the feature framing.`:"","","Follow the skill: (1) read those PRDs/specs as the primary source (intent, user-facing surface,","terminology, user stories, prerequisites); (2) locate and read the CODE that implements that surface","to pin exact UI strings/routes/states \u2014 never fabricate; flag anything the PRD claims that you","cannot find in source; (3) propose a tight feature-scoped article set (\u22483\u20135) and pin it to",".d360-writer/plan.json + AGENT-PLAN.md, with each article's `sources` listing BOTH the PRD file(s)","AND the grounding code files; (4) flag overlaps with existing articles (do not merge). Then stop and","offer /write --all. Do NOT write the articles in this turn."].filter(Boolean).join(`
59
- `)}var bo="No input docs found. Drop this feature's PRDs/specs in .d360-writer/input/<feature>/ (.md or .pdf), then run /draft.";async function bs(e,t){let n=t?.cwd??process.cwd();if(!zl(n))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{features:o,looseDocs:r}=$o(n);if(o.length===0&&r===0)return console.log(m(bo)),{kind:"continue"};let s=e.join(" ").trim();if(!s){if(o.length>1){console.log(m("Multiple features in .d360-writer/input/ \u2014 draft which one? Run: /draft <feature>"));for(let g of o)console.log(` ${g.name} ${m(`(${g.docCount} doc${g.docCount===1?"":"s"})`)}`);return{kind:"continue"}}let p=o[0]?.name??"";return{kind:"forward-to-agent",prompt:un(p),display:p?`/draft ${p}`:"/draft"}}let c=xo(o,s);return{kind:"forward-to-agent",prompt:un(c??"",c?void 0:s),display:`/draft ${s}`}}q();import{search as Vl}from"@inquirer/prompts";import{findByName as Xl,getSession as Kl,listSessions as Jl,relativeTime as Cs}from"document360-engine";async function Ps(e,t){let n=Jl(t.cwd).filter(o=>o.uuid!==t.currentUuid());if(n.length===0)return console.log(m("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let o=e.join(" "),r=Xl(t.cwd,o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${o}".`)),vs(n),{kind:"continue"})}if(!process.stdin.isTTY)return vs(n),console.log(m("Run: /resume <name>")),{kind:"continue"};try{let o=await t.withPausedInput(()=>Vl({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return n.filter(p=>!c||p.name.toLowerCase().includes(c)||p.firstPrompt.toLowerCase().includes(c)).map(p=>({name:`${p.name} ${Cs(p.updatedAt)}`,value:p.uuid,description:p.firstPrompt.slice(0,100)}))}})),r=Kl(o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function vs(e){console.log("");for(let t of e.slice(0,15))console.log(` ${E(t.name)} ${m(Cs(t.updatedAt))}`),console.log(` ${m(t.firstPrompt.slice(0,80))}`);console.log("")}q();import{renameSession as Ql}from"document360-engine";function vo(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 Ts(e,t){let n=vo(e.join(" "));if(!n)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let o=t.currentUuid();return o?(Ql(o,n)?console.log(A(`\u2713 Session renamed to "${n}"`)):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 Ss,readProjectConfig as Zl,writeProjectConfig as ec}from"document360-engine";q();function Co(e,t,n){if(!t)return"Usage: /profile add <name> [environment]";let o=Zl(e);if(!o)return"No .d360-writer.json \u2014 run /init first.";if(o.profiles?.[t])return`Profile "${t}" already exists.`;let r=n??t;return Ss().includes(r)?(o.profiles={...o.profiles,[t]:{connection:{environment:r},production:!1}},ec(o,e),null):`Unknown environment "${r}". Known: ${Ss().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Rs(e,t){let n=e[0];if(!n)return Qt(t.cwd),{kind:"continue"};if(n==="add"){let o=Co(t.cwd,e[1],e[2]);return o?(console.log(x(o)),{kind:"continue"}):(console.log(A(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${E(`/profile ${e[1]}`)} then ${E("/login")}`),{kind:"continue"})}return Zt(t.cwd,n),en(t.cwd,n),console.log(m(" Restarting agent for the new profile\u2026")),{kind:"clear"}}q();import{select as tc}from"@inquirer/prompts";import{readProjectConfig as nc,readUserConfig as js,resolveModelSetting as Po,writeUserConfig as Ds}from"document360-engine";var ye=[{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 dn(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),n=ye.findIndex(o=>o.value!==null&&(o.value===t||o.label.toLowerCase()===t||t.includes(o.label.toLowerCase())));return n>=0?n:0}function oc(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 Pt(e,t){let n=()=>{let s=Po(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=js();return s.defaultModel?(delete s.defaultModel,Ds(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Po(e).model??"Claude Code default"} (applies from your next message)`],changed:!0,effective:n()}):{lines:["No personal model override set \u2014 nothing to clear."],changed:!1,effective:void 0}}Ds({...js(),defaultModel:t});let o=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=nc(e)?.defaultModel;return r&&o.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:o,changed:!0,effective:n()}}async function As(e,t){let n=e[0]?.trim();if(!n){let c=Po(t.cwd);if(!process.stdin.isTTY)return console.log(`${Ce("Model:")} ${E(c.model??"Claude Code default")}`),console.log(m(` source: ${oc(c)}`)),console.log(m(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let p=dn(c),g;try{g=await t.withPausedInput(()=>tc({message:`Select model (current: ${c.model??"Claude Code default"})`,default:ye[p].value,choices:ye.map((y,b)=>({name:`${y.label}${b===p?" \u2714":""}`,value:y.value,description:y.desc}))}))}catch{return console.log(m("Cancelled.")),{kind:"continue"}}let{lines:w,changed:$,effective:R}=Pt(t.cwd,g??"default");for(let y of w)console.log(y.startsWith("\u26A0")?L(y):y.startsWith("\u2713")?A(y):m(y));return $&&await t.setModel(R),{kind:"continue"}}let{lines:o,changed:r,effective:s}=Pt(t.cwd,n);for(let c of o)console.log(c.startsWith("\u26A0")?L(c):c.startsWith("\u2713")?A(c):m(c));return r&&await t.setModel(s),{kind:"continue"}}pn();async function Ms(e,t){return await t.withPausedInput(()=>lt(t.cwd)),{kind:"clear"}}q();import{select as gc}from"@inquirer/prompts";import{resolveActiveProfile as hc,setProfileProject as wc,listProjects as kc}from"document360-engine";async function fn(e,t){let n=hc(e,t),o={profile:n.name,connection:n.connection};return{projects:await kc(o),profile:n.name,environment:n.connection.name,current:n.project.projectId}}var Os=e=>`${e.name??e.id}${e.sub_domain?` \xB7 ${e.sub_domain}`:""}`;function Ls(e,t){let n=t.toLowerCase();return e.find(o=>(o.name??"").toLowerCase()===n)??e.find(o=>(o.name??"").toLowerCase().startsWith(n))??e.find(o=>o.id.startsWith(t))}function So(e,t,n,o){wc(e,t,{projectId:n,projectName:o,workspaceId:void 0,workspaceName:void 0})}async function Us(e,t){let n;try{n=await fn(e,t)}catch(g){console.log(x(`Could not list projects: ${g.message}`));return}let{projects:o,profile:r,current:s}=n;if(o.length===0){console.log(m("No projects found for this identity."));return}if(!process.stdin.isTTY){console.log("");for(let g of o)console.log(` ${g.id===s?E("\u25CF"):" "} ${Os(g)} ${m(g.id)}`);console.log(m("Run: d360-writer project use <name>"));return}let c=await gc({message:"Select the Document360 project for this repo:",choices:o.map(g=>({name:`${Os(g)}${g.id===s?" (current)":""}`,value:g.id}))}),p=o.find(g=>g.id===c);So(e,r,c,p?.name),console.log(A(`\u2713 Project set to "${p?.name??c}" for profile "${r}". Next: pick a workspace.`))}async function Ws(e,t){return await t.withPausedInput(()=>Us(t.cwd,t.profileName)),{kind:"clear"}}q();import{resolveActiveProfile as yc}from"document360-engine";async function Fs(e,t){let n=!1;try{n=yc(t.cwd).production}catch{}return n?(console.log(L("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(m("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}q();var Hs=async(e,t)=>{try{await t.withPausedInput(()=>Kt({}))}catch(n){console.log(x(`Login failed: ${n.message}`))}return{kind:"continue"}};q();import{resolveActiveProfile as $c,clearTokens as xc,clearProfileProject as bc}from"document360-engine";async function Bs(e,t){let n;try{n=$c(t.cwd,t.profileName).name}catch(r){return console.log(x(r.message)),{kind:"continue"}}let o=xc(n);return bc(t.cwd,n),console.log(o?A(`\u2713 Signed out of "${n}" and cleared its project/workspace selection.`):m(`Profile "${n}" was not signed in. Cleared any project/workspace selection.`)),console.log(m("Run /login to sign in and pick a project.")),{kind:"continue"}}import{existsSync as qs}from"node:fs";import{isAbsolute as vc,join as Cc,resolve as Pc}from"node:path";import{readProjectConfig as Tc,screenshotPlaceholderIds as Sc,DEFAULT_CAPTURE_DIR as Rc,DEFAULT_OUTPUT_DIR as jc}from"document360-engine";var Gs=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Ro(e){let t=!e.includes("--no-setup"),n=e.filter(r=>r!=="--no-setup");if(n[0]==="--list")return{mode:"list",scope:n[1]?Gs(n[1]):void 0};let o=n[0];return!o||o==="--all"?{mode:"all",setup:t}:/[\\/]/.test(o)||o.endsWith(".md")?{mode:"scope",scope:Gs(o),setup:t}:{mode:"single",id:o}}function jo(e,t){let n=Tc(e),o=n?.captureDir??Rc,r=n?.outputDir??jc,s=(c,p)=>vc(c)?Cc(c,p):Pc(e,c,p);return Sc(e,{scope:t}).map(({id:c,file:p})=>{let g=qs(s(r,`${c}.png`))?"captured":qs(s(o,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:p,state:g}})}var Dc={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},Ac={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Do(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let n=[...new Set(e.map(s=>s.file))].sort(),o=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${n.length} article${n.length===1?"":"s"} \xB7 ${o} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of n){r.push(s);for(let c of e.filter(p=>p.file===s))r.push(` ${Dc[c.state]} ${c.id.padEnd(34)} ${Ac[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function zs(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(`
58
+ `)}function wo(e,t,n){let o=e.reduce((c,p)=>c+p.paths.length,0),[r,s]=t.usd;return[`Write ${o} article${o===1?"":"s"} across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(c=>` \u2022 ${c.label} \u2014 ${c.paths.length} article${c.paths.length===1?"":"s"}`),"",`Estimated cost: ${ae(r)}\u2013${ae(s)}. ${t.note}`,"","Articles are written as local drafts in the docs tree (nothing is published). Run /write --run to start."]}function ko(e,t,n){let o=v=>t[v.index]?.paths.length??0,r=e.filter(v=>v.ok),s=e.filter(v=>!v.ok),c=e.reduce((v,N)=>v+o(N),0),p=r.reduce((v,N)=>v+o(N),0),g=e.reduce((v,N)=>v+N.costUsd,0),w=e.reduce((v,N)=>v+N.outputTokens,0),$=n==="api"?`${ae(g)} total`:Ee(w),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,y=v=>`${v} article${v===1?"":"s"}`,b=[];if(s.length===0)b.push(`Wrote ${y(c)} (${R}) successfully \xB7 ${$}.`),b.push("Next \u2014 push them to Document360:"),b.push("/publish --all"),b.push("Or add screenshots first with /screenshot --all. Drafts are reviewable in the portal after publishing.");else{b.push(`Wrote ${p}/${y(c)} (${R}) \xB7 ${$}.`),b.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /write to retry the rest:`);for(let v of s)b.push(` \u2717 ${v.label}${v.error?` \u2014 ${v.error}`:""}`)}return b}function yo(e,t,n){let o=(hs(e)?.docsDir??Ul).replace(/\/+$/,""),r=t.map(p=>p.path);if(n.path){let p=Hl(n.path,o);return r.includes(p)?{docsDir:o,targets:[p],planCount:r.length}:{docsDir:o,targets:[],planCount:r.length,reason:`"${p}" isn't in the docs plan (.d360-writer/plan.json).`}}let s=Bl(r,n.scope);if(n.scope&&s.length===0)return{docsDir:o,targets:[],planCount:r.length,reason:`No planned articles under "${n.scope}".`};let c=ql(e,o,s);if(c.length===0){let p=n.scope?` under "${n.scope}"`:"";return{docsDir:o,targets:[],planCount:r.length,reason:`All planned articles${p} are already written. Use /write <path> to rewrite one.`}}return{docsDir:o,targets:c,planCount:r.length}}async function ws(e,t){if(!hs(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let n=Ll(t.cwd);if(n.length===0)return console.log(x("No docs plan found (.d360-writer/plan.json). Analyze the repo and propose a structure first.")),{kind:"continue"};let{scope:o,path:r,run:s}=mo(e),{docsDir:c,targets:p,reason:g}=yo(t.cwd,n,{scope:o,path:r});if(p.length===0)return console.log(g?L(g):L("Nothing to write.")),{kind:"continue"};let w=Ol(p),{model:$,forced:R}=Wl(t.cwd,"standard");if(!s&&!!!r){let b=Ml({files:go(t.cwd,n,p),op:"write",model:$});o&&console.log(m(`Scope: ${o} (${p.length} pending).`));for(let v of wo(w,b,fo))console.log(v);return console.log(m(`Model: ${$}${R?" (forced)":" \u2014 authoring; /model to override"}.`)),console.log(m(`Run /write${o?` --scope ${o}`:" --all"} --run to start.`)),console.log(""),{kind:"continue"}}console.log(m(`Writing ${p.length} article${p.length===1?"":"s"} across ${w.length} partition${w.length===1?"":"s"} (\u2264${fo} agents at once) on ${$}\u2026`)),console.log(m(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let b of Fl({cwd:t.cwd,partitions:w,promptFor:v=>ho(v,c),concurrency:fo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:$}))if(b.type==="partition_status")b.status==="running"?console.log(m(` \u25B8 ${b.label} \u2014 writing\u2026`)):b.status==="done"?console.log(A(` \u2713 ${b.label}`)):console.log(x(` \u2717 ${b.label}`));else if(b.type==="run_done"){console.log("");let v=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let N of ko(b.results,w,v))console.log(b.ok?A(N):L(N))}}catch(b){console.log(x(`Write run failed: ${b.message}`))}return console.log(""),{kind:"continue"}}q();import{existsSync as Gl,readdirSync as ks,statSync as ys}from"node:fs";import{join as $s}from"node:path";import{inputDir as zl,readProjectConfig as Yl}from"document360-engine";var xs=/\.(md|markdown|pdf|txt)$/i;function $o(e){let t=zl(e);if(!Gl(t))return{features:[],looseDocs:0};let n=[],o=0;for(let r of ks(t)){let s=$s(t,r),c=!1;try{c=ys(s).isDirectory()}catch{continue}if(c){if(r.startsWith("."))continue;let p=Vl(s);p>0&&n.push({name:r,docCount:p})}else xs.test(r)&&o++}return n.sort((r,s)=>r.name.localeCompare(s.name)),{features:n,looseDocs:o}}function Vl(e){let t=0;for(let n of ks(e))try{ys($s(e,n)).isFile()&&xs.test(n)&&t++}catch{}return t}function xo(e,t){let n=t.trim().toLowerCase();return n?e.find(o=>o.name.toLowerCase()===n)?.name??e.find(o=>o.name.toLowerCase().startsWith(n))?.name??e.find(o=>o.name.toLowerCase().includes(n))?.name??null:null}function un(e,t){return["Run the draft-from-prd skill.","",`Target scope: ${e?`the feature documents in .d360-writer/input/${e}/`:t?`the input docs in .d360-writer/input/ relevant to: "${t}"`:"the input docs in .d360-writer/input/"}.`,t&&!e?`Use "${t}" as the feature framing.`:"","","Follow the skill: (1) read those PRDs/specs as the primary source (intent, user-facing surface,","terminology, user stories, prerequisites); (2) locate and read the CODE that implements that surface","to pin exact UI strings/routes/states \u2014 never fabricate; flag anything the PRD claims that you","cannot find in source; (3) propose a tight feature-scoped article set (\u22483\u20135) and pin it to",".d360-writer/plan.json + AGENT-PLAN.md, with each article's `sources` listing BOTH the PRD file(s)","AND the grounding code files; (4) flag overlaps with existing articles (do not merge). Then stop and","offer /write --all. Do NOT write the articles in this turn."].filter(Boolean).join(`
59
+ `)}var bo="No input docs found. Drop this feature's PRDs/specs in .d360-writer/input/<feature>/ (.md or .pdf), then run /draft.";async function bs(e,t){let n=t?.cwd??process.cwd();if(!Yl(n))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{features:o,looseDocs:r}=$o(n);if(o.length===0&&r===0)return console.log(m(bo)),{kind:"continue"};let s=e.join(" ").trim();if(!s){if(o.length>1){console.log(m("Multiple features in .d360-writer/input/ \u2014 draft which one? Run: /draft <feature>"));for(let g of o)console.log(` ${g.name} ${m(`(${g.docCount} doc${g.docCount===1?"":"s"})`)}`);return{kind:"continue"}}let p=o[0]?.name??"";return{kind:"forward-to-agent",prompt:un(p),display:p?`/draft ${p}`:"/draft"}}let c=xo(o,s);return{kind:"forward-to-agent",prompt:un(c??"",c?void 0:s),display:`/draft ${s}`}}q();import{search as Xl}from"@inquirer/prompts";import{findByName as Kl,getSession as Jl,listSessions as Ql,relativeTime as Cs}from"document360-engine";async function Ps(e,t){let n=Ql(t.cwd).filter(o=>o.uuid!==t.currentUuid());if(n.length===0)return console.log(m("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let o=e.join(" "),r=Kl(t.cwd,o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${o}".`)),vs(n),{kind:"continue"})}if(!process.stdin.isTTY)return vs(n),console.log(m("Run: /resume <name>")),{kind:"continue"};try{let o=await t.withPausedInput(()=>Xl({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return n.filter(p=>!c||p.name.toLowerCase().includes(c)||p.firstPrompt.toLowerCase().includes(c)).map(p=>({name:`${p.name} ${Cs(p.updatedAt)}`,value:p.uuid,description:p.firstPrompt.slice(0,100)}))}})),r=Jl(o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function vs(e){console.log("");for(let t of e.slice(0,15))console.log(` ${E(t.name)} ${m(Cs(t.updatedAt))}`),console.log(` ${m(t.firstPrompt.slice(0,80))}`);console.log("")}q();import{renameSession as Zl}from"document360-engine";function vo(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 Ts(e,t){let n=vo(e.join(" "));if(!n)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let o=t.currentUuid();return o?(Zl(o,n)?console.log(A(`\u2713 Session renamed to "${n}"`)):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 Ss,readProjectConfig as ec,writeProjectConfig as tc}from"document360-engine";q();function Co(e,t,n){if(!t)return"Usage: /profile add <name> [environment]";let o=ec(e);if(!o)return"No .d360-writer.json \u2014 run /init first.";if(o.profiles?.[t])return`Profile "${t}" already exists.`;let r=n??t;return Ss().includes(r)?(o.profiles={...o.profiles,[t]:{connection:{environment:r},production:!1}},tc(o,e),null):`Unknown environment "${r}". Known: ${Ss().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Rs(e,t){let n=e[0];if(!n)return Qt(t.cwd),{kind:"continue"};if(n==="add"){let o=Co(t.cwd,e[1],e[2]);return o?(console.log(x(o)),{kind:"continue"}):(console.log(A(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${E(`/profile ${e[1]}`)} then ${E("/login")}`),{kind:"continue"})}return Zt(t.cwd,n),en(t.cwd,n),console.log(m(" Restarting agent for the new profile\u2026")),{kind:"clear"}}q();import{select as nc}from"@inquirer/prompts";import{readProjectConfig as oc,readUserConfig as js,resolveModelSetting as Po,writeUserConfig as Ds}from"document360-engine";var ye=[{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 dn(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),n=ye.findIndex(o=>o.value!==null&&(o.value===t||o.label.toLowerCase()===t||t.includes(o.label.toLowerCase())));return n>=0?n:0}function rc(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 Pt(e,t){let n=()=>{let s=Po(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=js();return s.defaultModel?(delete s.defaultModel,Ds(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Po(e).model??"Claude Code default"} (applies from your next message)`],changed:!0,effective:n()}):{lines:["No personal model override set \u2014 nothing to clear."],changed:!1,effective:void 0}}Ds({...js(),defaultModel:t});let o=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=oc(e)?.defaultModel;return r&&o.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:o,changed:!0,effective:n()}}async function As(e,t){let n=e[0]?.trim();if(!n){let c=Po(t.cwd);if(!process.stdin.isTTY)return console.log(`${Ce("Model:")} ${E(c.model??"Claude Code default")}`),console.log(m(` source: ${rc(c)}`)),console.log(m(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let p=dn(c),g;try{g=await t.withPausedInput(()=>nc({message:`Select model (current: ${c.model??"Claude Code default"})`,default:ye[p].value,choices:ye.map((y,b)=>({name:`${y.label}${b===p?" \u2714":""}`,value:y.value,description:y.desc}))}))}catch{return console.log(m("Cancelled.")),{kind:"continue"}}let{lines:w,changed:$,effective:R}=Pt(t.cwd,g??"default");for(let y of w)console.log(y.startsWith("\u26A0")?L(y):y.startsWith("\u2713")?A(y):m(y));return $&&await t.setModel(R),{kind:"continue"}}let{lines:o,changed:r,effective:s}=Pt(t.cwd,n);for(let c of o)console.log(c.startsWith("\u26A0")?L(c):c.startsWith("\u2713")?A(c):m(c));return r&&await t.setModel(s),{kind:"continue"}}pn();async function Ms(e,t){return await t.withPausedInput(()=>lt(t.cwd)),{kind:"clear"}}q();import{select as hc}from"@inquirer/prompts";import{resolveActiveProfile as wc,setProfileProject as kc,listProjects as yc}from"document360-engine";async function fn(e,t){let n=wc(e,t),o={profile:n.name,connection:n.connection};return{projects:await yc(o),profile:n.name,environment:n.connection.name,current:n.project.projectId}}var Os=e=>`${e.name??e.id}${e.sub_domain?` \xB7 ${e.sub_domain}`:""}`;function Ls(e,t){let n=t.toLowerCase();return e.find(o=>(o.name??"").toLowerCase()===n)??e.find(o=>(o.name??"").toLowerCase().startsWith(n))??e.find(o=>o.id.startsWith(t))}function So(e,t,n,o){kc(e,t,{projectId:n,projectName:o,workspaceId:void 0,workspaceName:void 0})}async function Us(e,t){let n;try{n=await fn(e,t)}catch(g){console.log(x(`Could not list projects: ${g.message}`));return}let{projects:o,profile:r,current:s}=n;if(o.length===0){console.log(m("No projects found for this identity."));return}if(!process.stdin.isTTY){console.log("");for(let g of o)console.log(` ${g.id===s?E("\u25CF"):" "} ${Os(g)} ${m(g.id)}`);console.log(m("Run: d360-writer project use <name>"));return}let c=await hc({message:"Select the Document360 project for this repo:",choices:o.map(g=>({name:`${Os(g)}${g.id===s?" (current)":""}`,value:g.id}))}),p=o.find(g=>g.id===c);So(e,r,c,p?.name),console.log(A(`\u2713 Project set to "${p?.name??c}" for profile "${r}". Next: pick a workspace.`))}async function Ws(e,t){return await t.withPausedInput(()=>Us(t.cwd,t.profileName)),{kind:"clear"}}q();import{resolveActiveProfile as $c}from"document360-engine";async function Fs(e,t){let n=!1;try{n=$c(t.cwd).production}catch{}return n?(console.log(L("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(m("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}q();var Hs=async(e,t)=>{try{await t.withPausedInput(()=>Kt({}))}catch(n){console.log(x(`Login failed: ${n.message}`))}return{kind:"continue"}};q();import{resolveActiveProfile as xc,clearTokens as bc,clearProfileProject as vc}from"document360-engine";async function Bs(e,t){let n;try{n=xc(t.cwd,t.profileName).name}catch(r){return console.log(x(r.message)),{kind:"continue"}}let o=bc(n);return vc(t.cwd,n),console.log(o?A(`\u2713 Signed out of "${n}" and cleared its project/workspace selection.`):m(`Profile "${n}" was not signed in. Cleared any project/workspace selection.`)),console.log(m("Run /login to sign in and pick a project.")),{kind:"continue"}}import{existsSync as qs}from"node:fs";import{isAbsolute as Cc,join as Pc,resolve as Tc}from"node:path";import{readProjectConfig as Sc,screenshotPlaceholderIds as Rc,DEFAULT_CAPTURE_DIR as jc,DEFAULT_OUTPUT_DIR as Dc}from"document360-engine";var Gs=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Ro(e){let t=!e.includes("--no-setup"),n=e.filter(r=>r!=="--no-setup");if(n[0]==="--list")return{mode:"list",scope:n[1]?Gs(n[1]):void 0};let o=n[0];return!o||o==="--all"?{mode:"all",setup:t}:/[\\/]/.test(o)||o.endsWith(".md")?{mode:"scope",scope:Gs(o),setup:t}:{mode:"single",id:o}}function jo(e,t){let n=Sc(e),o=n?.captureDir??jc,r=n?.outputDir??Dc,s=(c,p)=>Cc(c)?Pc(c,p):Tc(e,c,p);return Rc(e,{scope:t}).map(({id:c,file:p})=>{let g=qs(s(r,`${c}.png`))?"captured":qs(s(o,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:p,state:g}})}var Ac={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},Ec={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Do(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let n=[...new Set(e.map(s=>s.file))].sort(),o=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${n.length} article${n.length===1?"":"s"} \xB7 ${o} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of n){r.push(s);for(let c of e.filter(p=>p.file===s))r.push(` ${Ac[c.state]} ${c.id.padEnd(34)} ${Ec[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function zs(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(`
60
60
  `),"","For each: locate its <!-- SCREENSHOT --> block in the docs tree (the configured docsDir); 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(`
61
61
  `)}async function mn(e,t){let n=Ro(e);if(n.mode==="list"){let c=t?.cwd??process.cwd();for(let p of Do(jo(c,n.scope),n.scope))console.log(p);return{kind:"continue"}}if(n.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${n.id}\`.`,"Locate its <!-- SCREENSHOT --> block in the docs tree (the configured docsDir), 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>/${n.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
62
62
  `),display:`/screenshot ${n.id}`};let r=[`Run the emit-screenshot-spec skill to (re)generate the document360-capture spec for ${n.mode==="scope"?`every <!-- SCREENSHOT --> placeholder in articles under ${n.scope}`:"every <!-- SCREENSHOT --> placeholder across the docs tree (the configured docsDir)"}.`,"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.${n.setup?" When done, run capture-setup-checklist to refresh CAPTURE-SETUP.md.":""} Report specs written, TODO data-testids, and data to stage.`].join(`
63
- `),s=n.mode==="scope"?`/screenshot ${n.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as Ec,readFileSync as Ic}from"node:fs";import{isAbsolute as Nc,join as _c,resolve as Mc}from"node:path";import{readProjectConfig as Oc,DEFAULT_CAPTURE_DIR as Lc}from"document360-engine";async function St(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across the docs tree (the configured docsDir), 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(`
64
- `),display:"/capture-setup"}}var Ys="-".repeat(64);function Ao(e){let t=Lc;try{let $=Oc(e);$?.captureDir&&(t=$.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let n=Nc(t)?_c(t,"CAPTURE-TESTID-REQUESTS.json"):Mc(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!Ec(n))return null;let o;try{let $=JSON.parse(Ic(n,"utf8").replace(/^/,""));o=Array.isArray($?.requests)?$.requests:[]}catch{return null}if(o.length===0)return null;let r=new Set(o.map($=>$.file).filter(Boolean)).size,s=o.length,c=`${s} data-testid attribute${s===1?"":"s"}`,p=`${r} source file${r===1?"":"s"}`,g=`${t}/CAPTURE-TESTID-REQUESTS.md`,w=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${p}.`,"\u26A0 Do NOT run this prompt here. d360-writer documents code; it does not change it \u2014"," these edits belong in your code repo. Open a SEPARATE terminal in:",` ${e}`," start Claude Code (or your coding agent), and paste everything between the lines."," Tip: run it in plan mode first \u2014 these are a hypothesis to verify, not a verified patch.","",Ys,`Read ${g} and ${w}. They list ${c} that d360-writer THINKS this repo needs for screenshot automation. Treat them as a hypothesis to VERIFY against the source \u2014 not a verified patch list.`,"","For EACH request in the JSON:",'1. Locate the element in THIS repo. Trace it from the route/page, not the feature name. If "anchorVerified" is false, find it by the "element"/"reason" description; otherwise confirm the "anchor" snippet still exists in "file".',"2. If the element already has a data-testid, skip it and note the existing one.","3. Confirm the attribute will reach the DOM: the rendering component must forward unknown props (a {...rest} spread or explicit pass-through). If it is a shared wrapper (e.g. Modal, ContextMenu) that destructures a fixed prop list, a data-testid is silently dropped \u2014 thread it through the component, or report it as needing a code change instead of forcing it.",'4. Only then add data-testid="<testid>" to the verified element. Add nothing else; do not rename, restructure, reformat, or change behaviour.',"","When done, report: which testids you added (file + element), which were already present, which requests were wrong (wrong file/anchor/page) with the correction, and which need threading through a shared component. Never force a request that does not match the source \u2014 flag it instead.",Ys]}import{existsSync as Uc,writeFileSync as Vs}from"node:fs";import{devHintsGuidePath as Wc,hintsDir as Fc,architectureDir as Hc,creatorNotesPath as Xs,ensureDir as Ks}from"document360-engine";var Js="-".repeat(64),Bc=2;function Eo(){return`# Dev \u2192 Docs hand-off \u2014 protocol guide
63
+ `),s=n.mode==="scope"?`/screenshot ${n.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as Ic,readFileSync as Nc}from"node:fs";import{isAbsolute as _c,join as Mc,resolve as Oc}from"node:path";import{readProjectConfig as Lc,DEFAULT_CAPTURE_DIR as Uc}from"document360-engine";async function St(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across the docs tree (the configured docsDir), 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(`
64
+ `),display:"/capture-setup"}}var Ys="-".repeat(64);function Ao(e){let t=Uc;try{let $=Lc(e);$?.captureDir&&(t=$.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let n=_c(t)?Mc(t,"CAPTURE-TESTID-REQUESTS.json"):Oc(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!Ic(n))return null;let o;try{let $=JSON.parse(Nc(n,"utf8").replace(/^/,""));o=Array.isArray($?.requests)?$.requests:[]}catch{return null}if(o.length===0)return null;let r=new Set(o.map($=>$.file).filter(Boolean)).size,s=o.length,c=`${s} data-testid attribute${s===1?"":"s"}`,p=`${r} source file${r===1?"":"s"}`,g=`${t}/CAPTURE-TESTID-REQUESTS.md`,w=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${p}.`,"\u26A0 Do NOT run this prompt here. d360-writer documents code; it does not change it \u2014"," these edits belong in your code repo. Open a SEPARATE terminal in:",` ${e}`," start Claude Code (or your coding agent), and paste everything between the lines."," Tip: run it in plan mode first \u2014 these are a hypothesis to verify, not a verified patch.","",Ys,`Read ${g} and ${w}. They list ${c} that d360-writer THINKS this repo needs for screenshot automation. Treat them as a hypothesis to VERIFY against the source \u2014 not a verified patch list.`,"","For EACH request in the JSON:",'1. Locate the element in THIS repo. Trace it from the route/page, not the feature name. If "anchorVerified" is false, find it by the "element"/"reason" description; otherwise confirm the "anchor" snippet still exists in "file".',"2. If the element already has a data-testid, skip it and note the existing one.","3. Confirm the attribute will reach the DOM: the rendering component must forward unknown props (a {...rest} spread or explicit pass-through). If it is a shared wrapper (e.g. Modal, ContextMenu) that destructures a fixed prop list, a data-testid is silently dropped \u2014 thread it through the component, or report it as needing a code change instead of forcing it.",'4. Only then add data-testid="<testid>" to the verified element. Add nothing else; do not rename, restructure, reformat, or change behaviour.',"","When done, report: which testids you added (file + element), which were already present, which requests were wrong (wrong file/anchor/page) with the correction, and which need threading through a shared component. Never force a request that does not match the source \u2014 flag it instead.",Ys]}import{existsSync as Wc,writeFileSync as Vs}from"node:fs";import{devHintsGuidePath as Fc,hintsDir as Hc,architectureDir as Bc,creatorNotesPath as Xs,ensureDir as Ks}from"document360-engine";var Js="-".repeat(64),qc=2;function Eo(){return`# Dev \u2192 Docs hand-off \u2014 protocol guide
65
65
 
66
- <!-- Guide version: ${Bc} \xB7 maintained by document360-writer (/devhints). -->
66
+ <!-- Guide version: ${qc} \xB7 maintained by document360-writer (/devhints). -->
67
67
 
68
68
  This repo's user documentation is written by **document360-writer** ("the docs agent"), a separate agent.
69
69
  It can read your code, but code alone can't tell it the *intent* \u2014 WHY a feature exists, which of a
@@ -158,42 +158,42 @@ Anything only a human knows that would help document this product. For example:
158
158
  - Gotchas or non-obvious behavior.
159
159
 
160
160
  (Replace these examples with your notes, or leave the file as-is to skip \u2014 it's optional.)
161
- `}async function Qs(e,t){Ks(Fc(t.cwd)),Ks(Hc(t.cwd)),Uc(Xs(t.cwd))||Vs(Xs(t.cwd),Io()),Vs(Wc(t.cwd),Eo());for(let n of No())console.log(n);return{kind:"continue"}}function No(){return["\u2713 Wrote .d360-writer/DEV-HINTS-GUIDE.md + the .d360-writer/architecture/ (brief) and .d360-writer/hints/ inboxes."," Optional: add anything only you know to .d360-writer/architecture/CREATOR-NOTES.md (folder map,"," conventions, what to skip) \u2014 most repos leave it empty.","","\u{1F4CB} One-time setup \u2014 connect this repo's coding agent to the docs agent."," Paste the block below into your SOURCE repo's agent memory (CLAUDE.md / AGENTS.md), then commit"," .d360-writer/ so teammates' agents get the same instruction:","",Js,"## Documentation hand-off (for the docs agent, document360-writer)","","Read `.d360-writer/DEV-HINTS-GUIDE.md` and follow it. In order:","1. Read `.d360-writer/architecture/CREATOR-NOTES.md` (the creator's notes \u2014 authoritative; may be"," empty; never modify it).","2. ONE-TIME: write the architecture brief into `.d360-writer/architecture/` per the guide \u2014 a"," user-doc-enabling brief (product intent, user-facing surfaces, features, roles, workflows,"," terminology; cite source files; skip internals). Do this now if it doesn't exist yet.","3. ONGOING: when you ship a user-facing change, drop a doc hint in `.d360-writer/hints/` (per the"," guide) and keep the architecture brief current. Skip internal-only changes.","4. (Optional) If you want screenshots automated later, add stable `data-testid`s to the user-facing"," elements as you build \u2014 see the guide.",Js,"","Then keep building as usual. document360-writer reads the brief to onboard, and the hints to stay","current \u2014 grounding every concrete claim in your code."]}q();import{existsSync as Zs,rmSync as qc}from"node:fs";import{basename as ei,resolve as _o}from"node:path";import{execFileSync as Gc}from"node:child_process";import{input as zc}from"@inquirer/prompts";import{readProjectConfig as Yc}from"document360-engine";var Vc=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"],Xc=new Set([".d360-writer.json","d360-category-map.json",".d360-capture.json",".d360-capture-cache"]),gn=e=>(e??"").replace(/\\/g,"/").replace(/\/+$/,""),ti=e=>e===".d360-writer"||e.startsWith(".d360-writer/");function ni(e){let t=gn(e);return t===""||t.startsWith("..")||t.startsWith("/")||/^[a-zA-Z]:/.test(t)?!1:ti(t)?!0:Xc.has(t)}function Mo(e){let t=null;try{t=Yc(e)}catch{t=null}let n=new Set(Vc);for(let o of[gn(t?.docsDir),gn(t?.captureDir),gn(t?.outputDir)])o&&ti(o)&&n.add(o);return[...n].filter(o=>ni(o)&&Zs(_o(e,o)))}function Oo(e,t){let n=[],o=[];for(let r of t){if(!ni(r)){o.push({path:r,error:"refused \u2014 outside .d360-writer/ (protected)"});continue}try{qc(_o(e,r),{recursive:!0,force:!0}),n.push(r)}catch(s){o.push({path:r,error:s.message})}}return{removed:n,failed:o}}function Kc(e){try{return Gc("git",["status","--porcelain","--",".d360-writer/hints"],{cwd:e,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).split(`
162
- `).filter(n=>n.trim()&&/\.md\s*$/.test(n)).length}catch{return 0}}function Lo(e,t){if(t.length===0)return["Nothing to reset \u2014 no d360-writer files found in this repo."];let n=Kc(e),o=Zs(_o(e,"user-docs"));return["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(r=>` \u2022 ${r}`),"",...n>0?[`\u26A0 ${n} doc hint(s) under .d360-writer/hints/ are not committed \u2014 commit them first or they're gone.`]:[],...o?["Protected: root user-docs/ is NOT touched (only .d360-writer/ is removed)."]:[],"Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${ei(e)}`]}async function oi(e,t){let n=Mo(t.cwd);for(let p of Lo(t.cwd,n))console.log(n.length===0?m(p):p);if(n.length===0)return{kind:"continue"};let o=ei(t.cwd);if((await t.withPausedInput(()=>zc({message:`Type "${o}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==o)return console.log(L("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=Oo(t.cwd,n);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 p of c)console.log(x(` \u2717 ${p.path}: ${p.error}`));return console.log(m("Set up d360-writer again with /init (then /login, /workspace).")),{kind:"continue"}}var ri={help:Bn,"?":Bn,clear:_r,exit:qn,quit:qn,init:qr,mcp:on,publish:os,audit:an,scope:ss,sync:ps,convert:fs,write:ws,draft:bs,resume:Ps,rename:Ts,profile:Rs,model:As,doctor:Tt,workspace:Ms,project:Ws,"allow-prod":Fs,login:Hs,logout:Bs,screenshot:mn,"capture-setup":St,devhints:Qs,reset:oi};function si(e){let t=e.trim();if(!t.startsWith("/"))return null;let n=t.slice(1).split(/\s+/),o=(n[0]??"").toLowerCase();return o?{name:o,args:n.slice(1)}:null}q();var Jc={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"},ai=160,Wo=200,li=40;function ue(e,t){let n=e.replace(/\s+/g," ").trim();return n.length<=t?n:n.slice(0,t-1)+"\u2026"}function ii(e){let t=Object.entries(e).filter(([,o])=>o!=null&&o!=="");if(t.length===0)return null;let n=t.slice(0,4).map(([o,r])=>`${o}: ${ue(typeof r=="string"?r:JSON.stringify(r),li)}`);return t.length>4&&n.push("\u2026"),ue(n.join(", "),ai)}var Ie=e=>typeof e=="string"&&e?e:null,Uo=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Qc(e){let t=Ie(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);if(n.length<2)return null;let o=n[n.length-2],r=o.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?o:r.map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}function Zc(e,t){let n=(o,r)=>({title:`Document360: ${o}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let o=Ie(t.title);if(!o)return null;let r=Qc(t.local_path);return n("Create article",`"${ue(o,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let o=Ie(t.title),r=Uo(t.article_id);return n("Update article",o?`"${ue(o,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return n("Fork article (new draft)",Uo(t.article_id)?`id ${Uo(t.article_id)}\u2026`:null);case"d360_publish_article":{let o=t.version_number;return n("Publish article LIVE",typeof o=="number"?`v${o}`:null)}case"d360_unpublish_article":return n("Unpublish article",null);case"d360_create_category":return n("Create category",Ie(t.name)?`"${ue(Ie(t.name),60)}"`:null);case"d360_upload_drive_file":{let o=Ie(t.file_path);return n("Upload image",o?ue(o.replace(/\\/g,"/").split("/").pop()??o,60):null)}case"d360_sync_status":return n("Check sync status",null);default:return null}}function hn(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let w=Zc(c,t);if(w)return w}let p=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${p}`,sep:" ",arg:ii(t)}}let n=Jc[e],o=n?t[n]:void 0;return typeof o=="string"&&o?{title:e,sep:"",arg:ue(o,ai)}:{title:e,sep:"",arg:ii(t)}}function eu(e){if(e===null||typeof e!="object")return typeof e=="string"?e:null;let t=e;for(let n of["name","title","slug","id"])if(typeof t[n]=="string"&&t[n])return t[n];return null}function tu(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let n=t.map(eu).filter(r=>r!==null),o=`${t.length} item${t.length===1?"":"s"}`;return n.length===0?[o]:[o,...n.map(r=>ue(r,Wo))]}if(t!==null&&typeof t=="object"){let n=Object.entries(t).filter(([,o])=>o!==null&&(typeof o=="string"||typeof o=="number"||typeof o=="boolean")).slice(0,6).map(([o,r])=>`${o}: ${ue(String(r),li)}`);return n.length>0?[ue(n.join(" \xB7 "),Wo)]:null}return null}function nu(e){let t=Ie(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);return n.length>1?n.slice(1).join("/"):t}function ou(e,t,n){switch(e){case"d360_create_article":{let o=nu(t?.local_path);return[`\u2713 draft created${o?` \xB7 ${o}`:""}`]}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 o=Ie(t?.name);return[`\u2713 category created${o?` \xB7 "${o}"`:""}`]}case"d360_upload_drive_file":{let o=n.match(/https?:\/\/\S+/);return[`\u2713 uploaded${o?` \xB7 ${ue(o[0],120)}`:""}`]}default:return null}}function wn(e,t=4,n,o){let r=e.replace(/\r\n/g,`
163
- `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(n?.startsWith("mcp__document360__")){let c=ou(n.slice(18),o,r);if(c)return{lines:c,hidden:0}}let s=tu(r)??r.split(`
164
- `);return{lines:s.slice(0,t).map(c=>ue(c,Wo)),hidden:Math.max(0,s.length-t)}}function kn(e,t,n,o="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${o}/${n}`}function yn(e,t){if(typeof e.article_id=="string"&&e.article_id)return e.article_id;try{let n=JSON.parse(t),r=(Array.isArray(n)?n[0]:n)?.id;return typeof r=="string"&&r?r:null}catch{return null}}function $n(e){try{let t=JSON.parse(e),o=(Array.isArray(t)?t[0]:t)?.url;return typeof o=="string"&&/^https?:\/\//.test(o)?o:null}catch{return null}}var gu=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function hu(e,t,n,o){if(gu.test(e))try{let r=pi(o),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=yn(t,n),p=$n(n);e.endsWith("publish_article")&&p&&console.log(E(` \u2B95 Live: ${p}`)),c&&s&&console.log(E(` \u2B95 Preview: ${kn(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function fi(e=process.cwd(),t="auto",n){let o=su(t);o.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${E("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${E('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),wu(e,n),o.kind==="subscription"&&(o.stored?console.log(m(" Using your Claude subscription (no API key set).")):console.log(m(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let r=!1,s=Fo({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},p=ru({input:process.stdin,output:process.stdout}),g=[],w=null,$=!1;p.on("line",b=>{if(w){let v=w;w=null,v(b)}else g.push(b)}),p.on("close",()=>{if($=!0,w){let b=w;w=null,b(null)}});function R(){return g.length>0?Promise.resolve(g.shift()):$?Promise.resolve(null):(process.stdout.write(E("> ")),new Promise(b=>{w=b}))}let y={cwd:e,profileName:n,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=Fo({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async b=>s.setModel(b),withPausedInput:async b=>{p.pause();try{return await b()}finally{p.resume()}}};try{for(;;){let b=await R();if(b===null)break;let v=b.trim();if(v){if(v.startsWith("/")){let N=si(v);if(!N)continue;let _=ri[N.name];if(!_){console.log(x(`Unknown command: /${N.name}`)),console.log(m("Type /help for the list."));continue}let H=await _(N.args,y);if(H.kind==="exit")break;if(H.kind==="clear"){y.restartAgent();continue}if(H.kind==="allow-prod"){r=!0,y.restartAgent(),console.log(A("\u2713 Production writes authorized for this session.")),console.log("");continue}if(H.kind==="resume"){s.close(),s=Fo({cwd:e,resume:H.uuid,profileName:n,allowProdWrites:r});let Z=iu(H.uuid);c={uuid:H.uuid,firstPrompt:Z?.firstPrompt??null,titleFired:!0},di(H.uuid),console.log(A(`\u2713 Resumed "${H.name}"`)),console.log("");continue}H.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=H.display??H.prompt),await ui(s,H.prompt,o,c,e));continue}c.firstPrompt||(c.firstPrompt=v),await ui(s,v,o,c,e)}}}finally{s.close(),p.close()}}function wu(e,t){console.log(""),console.log(ht("document360-writer")),console.log(m(` cwd: ${e}`));let n=du(e);console.log(m(` model: ${n.model??"auto (engine right-sizes per task)"}${n.model?` (${n.source})`:""}`)),console.log(ku(e,t)),pu(e)||console.log(L(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(m(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function ku(e,t){try{let n=pi(e,t),o=n.production?L(" \u26A0 PRODUCTION"):"",r=mu(n.name);if(!r)return m(` Document360: profile "${n.name}"${o} \u2014 not logged in (d360-writer login)`);let s={...ci(r.idToken)??{},...ci(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return fu(r)&&!r.refreshToken?L(` Document360: profile "${n.name}"${o} \u2014 session expired (d360-writer login)`):m(` Document360: ${c} \xB7 profile "${n.name}"${o}`)}catch(n){return m(` Document360: ${n.message.split(".")[0]}`)}}function yu(){console.error(""),console.error(`Sign in with your Claude subscription: run ${E("claude")} once, then retry.`),console.error(` (No Claude Code? ${E("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${E("https://console.anthropic.com/settings/keys")}`)}function $u(e,t,n){e.uuid=t;let o=new Date().toISOString();cu({uuid:t,name:lu(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:n,firstPrompt:e.firstPrompt??"",createdAt:o,updatedAt:o})}function xu(e,t){e.titleFired=!0;let n=e.uuid,o=e.firstPrompt;!n||!o||uu(o,t).then(r=>{r&&au(n,r)}).catch(()=>{})}async function ui(e,t,n,o,r){let s=new Map;for await(let c of e.send(t))bu(c,o,r,n,s)}function bu(e,t,n,o,r){switch(e.type){case"session":t.uuid||$u(t,e.sessionId,n);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=hn(e.name,e.input);s&&(process.stdout.write(`
165
-
166
- `),console.log(`${A("\u25CF")} ${Ce(s.title)}${s.arg!==null?ee(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=qe(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=p=>p===1?"":"s";console.log(ee(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let p of s.lines)console.log(p);s.hidden>0&&console.log(m(` \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=wn(e.output,4,e.isError?void 0:s.name,s.input),p=e.isError?x:ee;c.lines.forEach((g,w)=>console.log(p((w===0?" \u23BF ":" ")+g))),c.hidden>0&&console.log(m(` \u2026 +${c.hidden} lines`)),e.isError||hu(s.name,s.input,e.output,n);break}case"result":process.stdout.write(`
167
- `),console.log(m(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(di(t.uuid),t.titleFired||xu(t,n));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),o.kind==="subscription"&&e.kind==="auth"&&yu();break}}import{render as bd}from"ink";import{resolveAuth as vd}from"document360-engine";import{useCallback as V,useEffect as _e,useMemo as zo,useRef as Q,useState as B}from"react";import{Box as J,Text as C,useApp as Eu,useInput as Iu,useStdout as Nu}from"ink";import{existsSync as Yo,readFileSync as Ai,readdirSync as Bi,writeFileSync as Ei}from"node:fs";import{basename as Dt,isAbsolute as _u,join as At}from"node:path";import{createSession as Ii,loginPkce as Mu,toStoredTokens as Ou,saveTokens as Lu,getAccessToken as Uu,resolveActiveProfile as de,resolveProjectId as Wu,getArticle as Fu,decodeJwtClaims as dt,isExpired as Ne,loadTokens as Ge,clearTokens as Hu,clearProfileProject as Bu,setTitle as qu,slugify as Gu,touchSession as Vo,upsertSession as zu,generateTitle as Ni,findByName as Yu,listSessions as Vu,renameSession as Xu,suggestNextAction as Ku,readProjectConfig as oe,writeProjectConfig as Ju,resolveModelSetting as Qo,loadProfileMap as _i,applyPull as Qu,computeSyncStatus as Xo,planPull as Zu,inventoryRepo as ed,knownEnvironments as td,resolveEnvironment as nd,planPartitions as Ko,partitionEvenly as od,screenshotPlaceholderIds as rd,trackedArticlePaths as sd,runPartitioned as Tn,estimateBulkCost as Mi,resolveModelForOperation as Sn,readDocsPlan as id,ensureDir as Oi,hintsDir as ad,architectureDir as ld,creatorNotesPath as Li,devHintsGuidePath as cd,DEFAULT_DOCS_DIR as ud,setProfileProject as dd}from"document360-engine";import{existsSync as vu,mkdirSync as Cu,readFileSync as Pu,writeFileSync as Tu}from"node:fs";import{join as mi}from"node:path";import{writerDir as Su}from"document360-engine";function gi(e){return mi(Su(e),".sessions")}function hi(e,t){return mi(gi(e),`${t}.json`)}function wi(e,t,n){try{Cu(gi(e),{recursive:!0});let o=n.filter(r=>r.kind!=="banner");Tu(hi(e,t),JSON.stringify({v:1,items:o}),"utf8")}catch{}}function Ho(e,t){try{let n=hi(e,t);if(!vu(n))return[];let o=JSON.parse(Pu(n,"utf8"));return Array.isArray(o.items)?o.items:[]}catch{return[]}}var Ru=e=>`\x1B]0;${e}\x07`,ju=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function ki(e){process.stdout.isTTY&&process.stdout.write(e)}function Rt(e){ki(Ru(e))}function xn(e){ki(ju(e))}var bn=["\xB7 "," \xB7"],Bo="\u{1F7E3}";pn();var Du=/^(?:\d+[.)]|[-*•])\s+/;function yi(e,t,n=4){let o=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
168
- `)){let c=s.trim().replace(Du,""),p=c.match(/^`([^`]+)`$/);p&&(c=p[1].trim());let g=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!g||!o.has(g[1].toLowerCase()))continue;let w=`/${g[1].toLowerCase()}${g[2]?` ${g[2]}`:""}`;if(r.includes(w)||r.push(w),r.length>=n)break}return r}q();function jt(e){return e.map(t=>({label:t.label,items:t.paths.length,status:"pending"}))}function Pe(e,t,n){return e.map(o=>o.label===t?{...o,status:n}:o)}function qo(e){return e.replace(/^[\w.-]+\//,"")}var vn=16,$i={pending:"\u25CB",active:"\u25A0",done:"\u2714",failed:"\u2717"};function Go(e){return e.trim().split(/\s+/).slice(1).join(" ")}function xi(e,t){let n=Go(t);return e.some(r=>r.trim()===t.trim()||!!n&&Go(r)===n)?e.filter(r=>r.trim()!==t.trim()&&!(n&&Go(r)===n)):[]}var Au=/\[Pasted text #\d+ \+\d+ lines?\]/g;function bi(e){return e.replace(/\r\n?/g,`
161
+ `}async function Qs(e,t){Ks(Hc(t.cwd)),Ks(Bc(t.cwd)),Wc(Xs(t.cwd))||Vs(Xs(t.cwd),Io()),Vs(Fc(t.cwd),Eo());for(let n of No())console.log(n);return{kind:"continue"}}function No(){return["\u2713 Wrote .d360-writer/DEV-HINTS-GUIDE.md + the .d360-writer/architecture/ (brief) and .d360-writer/hints/ inboxes."," Optional: add anything only you know to .d360-writer/architecture/CREATOR-NOTES.md (folder map,"," conventions, what to skip) \u2014 most repos leave it empty.","","\u{1F4CB} One-time setup \u2014 connect this repo's coding agent to the docs agent."," Paste the block below into your SOURCE repo's agent memory (CLAUDE.md / AGENTS.md), then commit"," .d360-writer/ so teammates' agents get the same instruction:","",Js,"## Documentation hand-off (for the docs agent, document360-writer)","","Read `.d360-writer/DEV-HINTS-GUIDE.md` and follow it. In order:","1. Read `.d360-writer/architecture/CREATOR-NOTES.md` (the creator's notes \u2014 authoritative; may be"," empty; never modify it).","2. ONE-TIME: write the architecture brief into `.d360-writer/architecture/` per the guide \u2014 a"," user-doc-enabling brief (product intent, user-facing surfaces, features, roles, workflows,"," terminology; cite source files; skip internals). Do this now if it doesn't exist yet.","3. ONGOING: when you ship a user-facing change, drop a doc hint in `.d360-writer/hints/` (per the"," guide) and keep the architecture brief current. Skip internal-only changes.","4. (Optional) If you want screenshots automated later, add stable `data-testid`s to the user-facing"," elements as you build \u2014 see the guide.",Js,"","Then keep building as usual. document360-writer reads the brief to onboard, and the hints to stay","current \u2014 grounding every concrete claim in your code."]}q();import{existsSync as Zs,rmSync as Gc}from"node:fs";import{basename as ei,resolve as _o}from"node:path";import{execFileSync as zc}from"node:child_process";import{input as Yc}from"@inquirer/prompts";import{readProjectConfig as Vc}from"document360-engine";var Xc=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"],Kc=new Set([".d360-writer.json","d360-category-map.json",".d360-capture.json",".d360-capture-cache"]),gn=e=>(e??"").replace(/\\/g,"/").replace(/\/+$/,""),ti=e=>e===".d360-writer"||e.startsWith(".d360-writer/");function ni(e){let t=gn(e);return t===""||t.startsWith("..")||t.startsWith("/")||/^[a-zA-Z]:/.test(t)?!1:ti(t)?!0:Kc.has(t)}function Mo(e){let t=null;try{t=Vc(e)}catch{t=null}let n=new Set(Xc);for(let o of[gn(t?.docsDir),gn(t?.captureDir),gn(t?.outputDir)])o&&ti(o)&&n.add(o);return[...n].filter(o=>ni(o)&&Zs(_o(e,o)))}function Oo(e,t){let n=[],o=[];for(let r of t){if(!ni(r)){o.push({path:r,error:"refused \u2014 outside .d360-writer/ (protected)"});continue}try{Gc(_o(e,r),{recursive:!0,force:!0}),n.push(r)}catch(s){o.push({path:r,error:s.message})}}return{removed:n,failed:o}}function Jc(e){try{return zc("git",["status","--porcelain","--",".d360-writer/hints"],{cwd:e,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).split(`
162
+ `).filter(n=>n.trim()&&/\.md\s*$/.test(n)).length}catch{return 0}}function Lo(e,t){if(t.length===0)return["Nothing to reset \u2014 no d360-writer files found in this repo."];let n=Jc(e),o=Zs(_o(e,"user-docs"));return["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(r=>` \u2022 ${r}`),"",...n>0?[`\u26A0 ${n} doc hint(s) under .d360-writer/hints/ are not committed \u2014 commit them first or they're gone.`]:[],...o?["Protected: root user-docs/ is NOT touched (only .d360-writer/ is removed)."]:[],"Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${ei(e)}`]}async function oi(e,t){let n=Mo(t.cwd);for(let p of Lo(t.cwd,n))console.log(n.length===0?m(p):p);if(n.length===0)return{kind:"continue"};let o=ei(t.cwd);if((await t.withPausedInput(()=>Yc({message:`Type "${o}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==o)return console.log(L("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=Oo(t.cwd,n);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 p of c)console.log(x(` \u2717 ${p.path}: ${p.error}`));return console.log(m("Set up d360-writer again with /init (then /login, /workspace).")),{kind:"continue"}}var ri={help:Bn,"?":Bn,clear:_r,exit:qn,quit:qn,init:qr,mcp:on,publish:os,audit:an,scope:ss,sync:ps,convert:fs,write:ws,draft:bs,resume:Ps,rename:Ts,profile:Rs,model:As,doctor:Tt,workspace:Ms,project:Ws,"allow-prod":Fs,login:Hs,logout:Bs,screenshot:mn,"capture-setup":St,devhints:Qs,reset:oi};function si(e){let t=e.trim();if(!t.startsWith("/"))return null;let n=t.slice(1).split(/\s+/),o=(n[0]??"").toLowerCase();return o?{name:o,args:n.slice(1)}:null}q();var Qc={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"},ai=160,Wo=200,li=40;function ue(e,t){let n=e.replace(/\s+/g," ").trim();return n.length<=t?n:n.slice(0,t-1)+"\u2026"}function ii(e){let t=Object.entries(e).filter(([,o])=>o!=null&&o!=="");if(t.length===0)return null;let n=t.slice(0,4).map(([o,r])=>`${o}: ${ue(typeof r=="string"?r:JSON.stringify(r),li)}`);return t.length>4&&n.push("\u2026"),ue(n.join(", "),ai)}var Ie=e=>typeof e=="string"&&e?e:null,Uo=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Zc(e){let t=Ie(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);if(n.length<2)return null;let o=n[n.length-2],r=o.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?o:r.map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}function eu(e,t){let n=(o,r)=>({title:`Document360: ${o}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let o=Ie(t.title);if(!o)return null;let r=Zc(t.local_path);return n("Create article",`"${ue(o,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let o=Ie(t.title),r=Uo(t.article_id);return n("Update article",o?`"${ue(o,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return n("Fork article (new draft)",Uo(t.article_id)?`id ${Uo(t.article_id)}\u2026`:null);case"d360_publish_article":{let o=t.version_number;return n("Publish article LIVE",typeof o=="number"?`v${o}`:null)}case"d360_unpublish_article":return n("Unpublish article",null);case"d360_create_category":return n("Create category",Ie(t.name)?`"${ue(Ie(t.name),60)}"`:null);case"d360_upload_drive_file":{let o=Ie(t.file_path);return n("Upload image",o?ue(o.replace(/\\/g,"/").split("/").pop()??o,60):null)}case"d360_sync_status":return n("Check sync status",null);default:return null}}function hn(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let w=eu(c,t);if(w)return w}let p=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${p}`,sep:" ",arg:ii(t)}}let n=Qc[e],o=n?t[n]:void 0;return typeof o=="string"&&o?{title:e,sep:"",arg:ue(o,ai)}:{title:e,sep:"",arg:ii(t)}}function tu(e){if(e===null||typeof e!="object")return typeof e=="string"?e:null;let t=e;for(let n of["name","title","slug","id"])if(typeof t[n]=="string"&&t[n])return t[n];return null}function nu(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let n=t.map(tu).filter(r=>r!==null),o=`${t.length} item${t.length===1?"":"s"}`;return n.length===0?[o]:[o,...n.map(r=>ue(r,Wo))]}if(t!==null&&typeof t=="object"){let n=Object.entries(t).filter(([,o])=>o!==null&&(typeof o=="string"||typeof o=="number"||typeof o=="boolean")).slice(0,6).map(([o,r])=>`${o}: ${ue(String(r),li)}`);return n.length>0?[ue(n.join(" \xB7 "),Wo)]:null}return null}function ou(e){let t=Ie(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);return n.length>1?n.slice(1).join("/"):t}function ru(e,t,n){switch(e){case"d360_create_article":{let o=ou(t?.local_path);return[`\u2713 draft created${o?` \xB7 ${o}`:""}`]}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 o=Ie(t?.name);return[`\u2713 category created${o?` \xB7 "${o}"`:""}`]}case"d360_upload_drive_file":{let o=n.match(/https?:\/\/\S+/);return[`\u2713 uploaded${o?` \xB7 ${ue(o[0],120)}`:""}`]}default:return null}}function wn(e,t=4,n,o){let r=e.replace(/\r\n/g,`
163
+ `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(n?.startsWith("mcp__document360__")){let c=ru(n.slice(18),o,r);if(c)return{lines:c,hidden:0}}let s=nu(r)??r.split(`
164
+ `);return{lines:s.slice(0,t).map(c=>ue(c,Wo)),hidden:Math.max(0,s.length-t)}}function kn(e,t,n,o="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${o}/${n}`}function yn(e,t){if(typeof e.article_id=="string"&&e.article_id)return e.article_id;try{let n=JSON.parse(t),r=(Array.isArray(n)?n[0]:n)?.id;return typeof r=="string"&&r?r:null}catch{return null}}function $n(e){try{let t=JSON.parse(e),o=(Array.isArray(t)?t[0]:t)?.url;return typeof o=="string"&&/^https?:\/\//.test(o)?o:null}catch{return null}}var hu=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function wu(e,t,n,o){if(hu.test(e))try{let r=pi(o),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=yn(t,n),p=$n(n);e.endsWith("publish_article")&&p&&console.log(E(` \u2B95 Live: ${p}`)),c&&s&&console.log(E(` \u2B95 Preview: ${kn(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function fi(e=process.cwd(),t="auto",n){let o=iu(t);o.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${E("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${E('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),ku(e,n),o.kind==="subscription"&&(o.stored?console.log(m(" Using your Claude subscription (no API key set).")):console.log(m(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let r=!1,s=Fo({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},p=su({input:process.stdin,output:process.stdout}),g=[],w=null,$=!1;p.on("line",b=>{if(w){let v=w;w=null,v(b)}else g.push(b)}),p.on("close",()=>{if($=!0,w){let b=w;w=null,b(null)}});function R(){return g.length>0?Promise.resolve(g.shift()):$?Promise.resolve(null):(process.stdout.write(E("> ")),new Promise(b=>{w=b}))}let y={cwd:e,profileName:n,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=Fo({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async b=>s.setModel(b),withPausedInput:async b=>{p.pause();try{return await b()}finally{p.resume()}}};try{for(;;){let b=await R();if(b===null)break;let v=b.trim();if(v){if(v.startsWith("/")){let N=si(v);if(!N)continue;let _=ri[N.name];if(!_){console.log(x(`Unknown command: /${N.name}`)),console.log(m("Type /help for the list."));continue}let H=await _(N.args,y);if(H.kind==="exit")break;if(H.kind==="clear"){y.restartAgent();continue}if(H.kind==="allow-prod"){r=!0,y.restartAgent(),console.log(A("\u2713 Production writes authorized for this session.")),console.log("");continue}if(H.kind==="resume"){s.close(),s=Fo({cwd:e,resume:H.uuid,profileName:n,allowProdWrites:r});let Z=au(H.uuid);c={uuid:H.uuid,firstPrompt:Z?.firstPrompt??null,titleFired:!0},di(H.uuid),console.log(A(`\u2713 Resumed "${H.name}"`)),console.log("");continue}H.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=H.display??H.prompt),await ui(s,H.prompt,o,c,e));continue}c.firstPrompt||(c.firstPrompt=v),await ui(s,v,o,c,e)}}}finally{s.close(),p.close()}}function ku(e,t){console.log(""),console.log(ht("document360-writer")),console.log(m(` cwd: ${e}`));let n=pu(e);console.log(m(` model: ${n.model??"auto (engine right-sizes per task)"}${n.model?` (${n.source})`:""}`)),console.log(yu(e,t)),fu(e)||console.log(L(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(m(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function yu(e,t){try{let n=pi(e,t),o=n.production?L(" \u26A0 PRODUCTION"):"",r=gu(n.name);if(!r)return m(` Document360: profile "${n.name}"${o} \u2014 not logged in (d360-writer login)`);let s={...ci(r.idToken)??{},...ci(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return mu(r)&&!r.refreshToken?L(` Document360: profile "${n.name}"${o} \u2014 session expired (d360-writer login)`):m(` Document360: ${c} \xB7 profile "${n.name}"${o}`)}catch(n){return m(` Document360: ${n.message.split(".")[0]}`)}}function $u(){console.error(""),console.error(`Sign in with your Claude subscription: run ${E("claude")} once, then retry.`),console.error(` (No Claude Code? ${E("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${E("https://console.anthropic.com/settings/keys")}`)}function xu(e,t,n){e.uuid=t;let o=new Date().toISOString();uu({uuid:t,name:cu(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:n,firstPrompt:e.firstPrompt??"",createdAt:o,updatedAt:o})}function bu(e,t){e.titleFired=!0;let n=e.uuid,o=e.firstPrompt;!n||!o||du(o,t).then(r=>{r&&lu(n,r)}).catch(()=>{})}async function ui(e,t,n,o,r){let s=new Map;for await(let c of e.send(t))vu(c,o,r,n,s)}function vu(e,t,n,o,r){switch(e.type){case"session":t.uuid||xu(t,e.sessionId,n);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=hn(e.name,e.input);s&&(process.stdout.write(`
165
+
166
+ `),console.log(`${A("\u25CF")} ${Ce(s.title)}${s.arg!==null?ee(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=qe(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=p=>p===1?"":"s";console.log(ee(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let p of s.lines)console.log(p);s.hidden>0&&console.log(m(` \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=wn(e.output,4,e.isError?void 0:s.name,s.input),p=e.isError?x:ee;c.lines.forEach((g,w)=>console.log(p((w===0?" \u23BF ":" ")+g))),c.hidden>0&&console.log(m(` \u2026 +${c.hidden} lines`)),e.isError||wu(s.name,s.input,e.output,n);break}case"result":process.stdout.write(`
167
+ `),console.log(m(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(di(t.uuid),t.titleFired||bu(t,n));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),o.kind==="subscription"&&e.kind==="auth"&&$u();break}}import{render as bd}from"ink";import{resolveAuth as vd}from"document360-engine";import{useCallback as V,useEffect as _e,useMemo as zo,useRef as Q,useState as B}from"react";import{Box as J,Text as C,useApp as Iu,useInput as Nu,useStdout as _u}from"ink";import{existsSync as Yo,readFileSync as Ai,readdirSync as Bi,writeFileSync as Ei}from"node:fs";import{basename as Dt,isAbsolute as Mu,join as At}from"node:path";import{createSession as Ii,loginPkce as Ou,toStoredTokens as Lu,saveTokens as Uu,getAccessToken as Wu,resolveActiveProfile as de,resolveProjectId as Fu,getArticle as Hu,decodeJwtClaims as dt,isExpired as Ne,loadTokens as Ge,clearTokens as Bu,clearProfileProject as qu,setTitle as Gu,slugify as zu,touchSession as Vo,upsertSession as Yu,generateTitle as Ni,findByName as Vu,listSessions as Xu,renameSession as Ku,suggestNextAction as Ju,readProjectConfig as oe,writeProjectConfig as Qu,resolveModelSetting as Qo,loadProfileMap as _i,applyPull as Zu,computeSyncStatus as Xo,planPull as ed,inventoryRepo as td,knownEnvironments as nd,resolveEnvironment as od,planPartitions as Ko,partitionEvenly as rd,screenshotPlaceholderIds as sd,trackedArticlePaths as id,runPartitioned as Tn,estimateBulkCost as Mi,resolveModelForOperation as Sn,readDocsPlan as ad,ensureDir as Oi,hintsDir as ld,architectureDir as cd,creatorNotesPath as Li,devHintsGuidePath as ud,DEFAULT_DOCS_DIR as qi,setProfileProject as dd}from"document360-engine";import{existsSync as Cu,mkdirSync as Pu,readFileSync as Tu,writeFileSync as Su}from"node:fs";import{join as mi}from"node:path";import{writerDir as Ru}from"document360-engine";function gi(e){return mi(Ru(e),".sessions")}function hi(e,t){return mi(gi(e),`${t}.json`)}function wi(e,t,n){try{Pu(gi(e),{recursive:!0});let o=n.filter(r=>r.kind!=="banner");Su(hi(e,t),JSON.stringify({v:1,items:o}),"utf8")}catch{}}function Ho(e,t){try{let n=hi(e,t);if(!Cu(n))return[];let o=JSON.parse(Tu(n,"utf8"));return Array.isArray(o.items)?o.items:[]}catch{return[]}}var ju=e=>`\x1B]0;${e}\x07`,Du=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function ki(e){process.stdout.isTTY&&process.stdout.write(e)}function Rt(e){ki(ju(e))}function xn(e){ki(Du(e))}var bn=["\xB7 "," \xB7"],Bo="\u{1F7E3}";pn();var Au=/^(?:\d+[.)]|[-*•])\s+/;function yi(e,t,n=4){let o=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
168
+ `)){let c=s.trim().replace(Au,""),p=c.match(/^`([^`]+)`$/);p&&(c=p[1].trim());let g=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!g||!o.has(g[1].toLowerCase()))continue;let w=`/${g[1].toLowerCase()}${g[2]?` ${g[2]}`:""}`;if(r.includes(w)||r.push(w),r.length>=n)break}return r}q();function jt(e){return e.map(t=>({label:t.label,items:t.paths.length,status:"pending"}))}function Pe(e,t,n){return e.map(o=>o.label===t?{...o,status:n}:o)}function qo(e){return e.replace(/^[\w.-]+\//,"")}var vn=16,$i={pending:"\u25CB",active:"\u25A0",done:"\u2714",failed:"\u2717"};function Go(e){return e.trim().split(/\s+/).slice(1).join(" ")}function xi(e,t){let n=Go(t);return e.some(r=>r.trim()===t.trim()||!!n&&Go(r)===n)?e.filter(r=>r.trim()!==t.trim()&&!(n&&Go(r)===n)):[]}var Eu=/\[Pasted text #\d+ \+\d+ lines?\]/g;function bi(e){return e.replace(/\r\n?/g,`
169
169
  `)}function vi(e){return e.includes(`
170
170
  `)||e.length>200}function Ci(e,t){let n=t.split(`
171
- `).length;return`[Pasted text #${e} +${n} line${n===1?"":"s"}]`}function Pi(e,t){return e.replace(Au,n=>t.get(n)??n)}function Ti(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function Cn(e,t){let n=Math.max(1,t),o=[],r=0;for(let s of e.split(`
171
+ `).length;return`[Pasted text #${e} +${n} line${n===1?"":"s"}]`}function Pi(e,t){return e.replace(Eu,n=>t.get(n)??n)}function Ti(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function Cn(e,t){let n=Math.max(1,t),o=[],r=0;for(let s of e.split(`
172
172
  `)){if(s.length===0)o.push({start:r,end:r});else for(let c=0;c<s.length;c+=n)o.push({start:r+c,end:r+Math.min(c+n,s.length)});r+=s.length+1}return o.length>0?o:[{start:0,end:0}]}function Pn(e,t){let n=0;for(let o=0;o<e.length&&e[o].start<=t;o++)n=o;return n}function Si(e,t,n){let o=Pn(e,t),r=o+n;if(r<0||r>=e.length)return t;let s=Math.min(t,e[o].end)-e[o].start;return Math.min(e[r].start+s,e[r].end)}function Ri(e,t,n){let o=e[Pn(e,t)];return n==="start"?o.start:o.end}function ji(e,t,n){let o=Math.max(1,n),r=e.split(`
173
173
  `),s=0;for(let c=r.length-1;c>=0;c--){let p=r[c];if(s+=Math.max(1,Math.ceil(p.length/o)),s>=t){let g=s-t;return{text:[g>0?p.slice(g*o):p,...r.slice(c+1)].join(`
174
174
  `),truncated:c>0||g>0}}}return{text:e,truncated:!1}}function Di(e){let t=!1,n=0,o=0;for(let r of e.split(`
175
175
  `)){let s=o+r.length;/^\s*```/.test(r)?t=!t:!t&&r.trim()===""&&s<e.length&&(n=s+1),o=s+1}return n}import{Fragment as Jo,jsx as S,jsxs as F}from"react/jsx-runtime";var pd={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""},Ui=` 1. /init \u2014 pick your Document360 environment & scaffold config
176
176
  2. /login signs in to that environment
177
177
  3. /project then /workspace \u2014 pick the project and where articles go
178
- Press 1 to start.`;function fd(e,t,n,o){let r=n.kind==="api"?"API key":n.kind==="subscription"?"subscription":"not configured",s=Qo(e),c=oe(e),p=(c?.docsDir??ud).replace(/\/+$/,""),g=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${p}/ + config`,w={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:pd[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:g};if(c===null)return w;try{let $=de(e,o);w.profile=$.name,w.apiUrl=$.connection.apiUrl,w.prod=$.production,w.project=$.project.projectName??$.project.projectId??"(chosen at login)";let R=Ge($.name);if(R){let y={...dt(R.idToken)??{},...dt(R.accessToken)??{}},b=y.email??y.preferred_username??"signed in";Ne(R)?R.refreshToken&&(w.who=b,w.loggedOut=!1,w.sessionHint="session expired \u2014 refreshing\u2026"):(w.who=b,w.loggedOut=!1,w.sessionHint=`session valid until ${new Date(R.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return w}function md(e,t){try{let n=de(e,t),o=Ge(n.name);if(!o)return{text:`profile "${n.name}" \u2014 not logged in (/login)`,prod:n.production};let r={...dt(o.idToken)??{},...dt(o.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return Ne(o)&&!o.refreshToken?{text:`profile "${n.name}" \u2014 session expired (/login)`,prod:n.production}:{text:`${s} \xB7 profile "${n.name}"`,prod:n.production}}catch(n){return{text:n.message.split(".")[0],prod:!1}}}var Wi=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],jn=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],gd="Ask me to write or update an article\u2026";function Rn({ch:e,dim:t}){let[n,o]=B(!0);return _e(()=>{let r=setInterval(()=>o(s=>!s),530);return()=>clearInterval(r)},[]),S(C,{inverse:n,color:t&&!n?"gray":void 0,children:e})}var hd=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Fi(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var wd=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Hi(e){try{return Bi(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function kd(e,t){let n=t??[];return n.length===0?["src","api","services","packages","modules"].some(o=>Hi(At(e,o))):n.some(o=>!o.includes("/")&&!o.endsWith(".md")&&Hi(At(e,o)))}function yd({startTime:e,chars:t}){let[n,o]=B(0);_e(()=>{let g=setInterval(()=>o(w=>w+1),120);return()=>clearInterval(g)},[]);let r=jn[n%jn.length],s=Wi[Math.floor(n/16)%Wi.length],c=Math.floor((Date.now()-e)/1e3),p=Math.round(t/4);return F(J,{children:[S(C,{color:G,children:` ${r} ${s}\u2026 `}),S(C,{color:"gray",children:`(${bt(c)} \xB7 ~${p} tokens \xB7 esc to interrupt)`})]})}var $d=12e4;function xd({p:e}){let[t,n]=B(0);_e(()=>{let _=setInterval(()=>n(H=>H+1),150);return()=>clearInterval(_)},[]);let o=jn[t%jn.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),p=Date.now()-e.lastAt>=$d,g=e.verb??"Converting",w=e.unit??"article",$=e.rows.reduce((_,H)=>_+H.items,0),R=g.toLowerCase(),y=e.rows,b=0;if(y.length>vn){let _=y.findIndex(Z=>Z.status==="active"||Z.status==="pending"),H=Math.max(0,Math.min(_-2,y.length-vn));b=y.slice(0,H).filter(Z=>Z.status==="done").length,y=y.slice(H,H+vn)}let v=Math.min(40,Math.max(...y.map(_=>qo(_.label).length),0)),N=_=>_==="failed"?"red":_==="active"?G:void 0;return F(J,{flexDirection:"column",children:[F(J,{children:[S(C,{color:G,children:` ${o} ${g} ${$} ${w}${$===1?"":"s"} `}),S(C,{color:"gray",children:`\xB7 ${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${bt(r)} \xB7 esc to stop`})]}),b>0&&S(C,{dimColor:!0,children:` \u2714 +${b} done`}),y.map(_=>F(C,{color:N(_.status),dimColor:_.status==="done"||_.status==="pending",children:[` ${$i[_.status]} ${qo(_.label).padEnd(v)}`,_.status!=="pending"?` (${_.items} ${w}${_.items===1?"":"s"})`:"",_.status==="active"?` \u2190 ${R}\u2026`:""]},_.label)),p&&S(C,{color:"yellow",children:` \u26A0 no activity for ${bt(s)} \u2014 press esc to stop if it's wedged`})]})}function qi({cwd:e,auth:t,profileName:n,version:o}){let{exit:r}=Eu(),[s,c]=B(n),[p,g]=B(null),[w,$]=B({text:"",pos:0}),R=w.text,y=V(l=>{$(i=>{let a=typeof l=="function"?l(i.text):l;return{text:a,pos:a.length}})},[]),b=V(l=>{$(i=>({text:i.text.slice(0,i.pos)+l+i.text.slice(i.pos),pos:i.pos+l.length}))},[]),v=V(()=>{$(l=>{if(l.pos===0)return l;let i=l.text.slice(0,l.pos),a=Ti(i)??i.slice(0,-1);return{text:a+l.text.slice(l.pos),pos:a.length}})},[]),[N,_]=B(!1),[H,Z]=B(!1),[$e,te]=B(0),[we,re]=B(null),[pt,X]=B([]),ze=Q(0),[Vi,Et]=B(!1),[Xi,Ye]=B(0),Me=Q(0),[ft,Ve]=B(0),[Xe,It]=B(null),tr=Q(new Map),Ki=Q(0),Ke=Q([]),pe=Q(null);pe.current===null&&(pe.current=Ii({cwd:e,profileName:s,allowProdWrites:!1}));let xe=Q({uuid:null,firstPrompt:null,titleFired:!1}),Nt=Q(new Map),Je=Q(!1),Dn=Q([]),Se=Q([]),An=Q(null),[nr,Qe]=B(null),[Oe,Le]=B(null),[be,Ze]=B(null),[Re,et]=B(null),[se,tt]=B(null),[fe,nt]=B(null),[Ue,ot]=B(null),[or,_t]=B(null),[me,We]=B(null),[le,rt]=B(null),[rr,En]=B([]),Fe=Q([]),Mt=Q(!1),ge=Q(null),[sr,K]=B(null),Ot=Q(!1),Lt=Q(null),[he,He]=B(null),[ir,st]=B(0),[ar,it]=B(0),[Ji,lr]=B(0),In=zo(()=>{try{return Qo(e).model??"auto"}catch{return null}},[e,Ji]),{stdout:ke}=Nu(),[,Qi]=B(0),cr=Q(`${ke.columns??80}x${ke.rows??24}`),je=V(()=>Math.max(20,(ke.columns??80)-1),[ke]),d=V(l=>{Ke.current.push(l),console.log(Qn(l,je()))},[je]);_e(()=>{let l=`d360-writer \xB7 ${nr??Dt(e)}`;if(!N){xn(0),Rt(`${Bo} ${l}`);return}xn(3);let i=0;Rt(`${bn[0]} ${l}`);let a=setInterval(()=>{i=(i+1)%bn.length,Rt(`${bn[i]} ${l}`)},400);return()=>clearInterval(a)},[N,e,nr]),_e(()=>()=>xn(0),[]),_e(()=>{if(d({kind:"banner",info:fd(e,o,t,s)}),!oe(e)){d({kind:"note",tone:"info",text:`Welcome! This repo isn't set up for d360-writer yet \u2014 three steps and you're writing docs:
179
- ${Ui}`}),X(["/init"]);return}try{let l=de(e,s),i=Ge(l.name);i&&Ne(i)&&i.refreshToken?Uu({profile:l.name,connection:l.connection}).then(()=>{d({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),te(a=>a+1)}).catch(()=>{d({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),X(["/login"])}):(!i||Ne(i))&&(d({kind:"note",tone:"warn",text:`Profile "${l.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),X(["/login"]))}catch{}try{let l=oe(e),i=de(e,s),a=Ge(i.name);l&&a&&!Ne(a)&&kd(e,l.authoritativeSourceFiles)&&(d({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),X(["/scope"]))}catch{}},[]),_e(()=>{let l=null,i=null,a=()=>{l&&clearTimeout(l),l=setTimeout(()=>{l=null;let h=`${ke.columns??80}x${ke.rows??24}`;h!==cr.current&&(cr.current=h,Qi(u=>u+1),i&&clearTimeout(i),i=setTimeout(()=>{i=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+es(Ke.current,je())+"\x1B[?2026l")},80))},400)};return ke.on("resize",a),()=>{l&&clearTimeout(l),i&&clearTimeout(i),ke.off("resize",a)}},[ke,je]);let Be=Math.max(20,(ke.columns??80)-1),ur=zo(()=>md(e,s),[e,s,$e]),Nn=(()=>{let l=oe(e);if(!l)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};let i=At(e,(l.docsDir??"user-docs").replace(/\/+$/,"")),a=(()=>{try{return Yo(i)&&Bi(i).length>0}catch{return!1}})();try{let h=de(e,s),u=Ge(h.name);if(!(!!u&&!(Ne(u)&&!u.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${h.name}")\u2026`,isSetup:!0};if(!h.project.workspaceId&&!a)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}return a?{text:gd,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}})(),mt=Ir(R),dr=mt.length>0&&!N,_n=p!==null?ji(p,8,Be):null,Ut=me?me.paths.filter(l=>!me.query||l.toLowerCase().includes(me.query.toLowerCase())).slice(0,8):[],Wt=he?he.sessions.filter(l=>{let i=he.query.toLowerCase();return!i||l.name.toLowerCase().includes(i)||l.firstPrompt.toLowerCase().includes(i)}).slice(0,8):[],ce=V((l,i)=>{pe.current?.close(),pe.current=Ii({cwd:e,resume:l,profileName:i??s,allowProdWrites:Je.current}),l||(xe.current={uuid:null,firstPrompt:null,titleFired:!1},Qe(null)),st(0),it(0)},[e,s]),pr=V((l,i,a)=>{if(hd.test(l))try{let h=de(e,s),u=typeof i.project_id=="string"&&i.project_id||h.project.projectId,f=yn(i,a),k=[],P=$n(a);l.endsWith("publish_article")&&P&&k.push(`Live: ${P}`),f&&u&&k.push(`Preview: ${kn(h.connection.portalUrl,u,f,h.project.languageCode??"en")}`),k.length>0&&d({kind:"link",lines:k})}catch{}},[e,s,d]),ve=V(async(l,i)=>{Et(!0),re(null),X([]),_t(null);let a=++ze.current;d({kind:"user",text:i?.echoDisplay&&i.display?i.display:l});let h=xe.current;h.firstPrompt||(h.firstPrompt=i?.display??l),Me.current=Date.now(),Ye(0),_(!0),Nt.current.clear();let u="",f="",k=null,P=()=>{k||(k=setTimeout(()=>{k=null,g(f.length>0?f:null)},60))},M=()=>{k&&clearTimeout(k),k=null,g(null)},j=()=>{if(f.trim()){let I=f.trimEnd();d({kind:"assistant",text:I})}f="",M()};try{for await(let I of pe.current.send(l))if(I.type==="session"){if(!h.uuid){h.uuid=I.sessionId;let O=new Date().toISOString(),T=Gu(h.firstPrompt??"session");zu({uuid:I.sessionId,name:T,renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:O,updatedAt:O}),Qe(T)}}else if(I.type==="text"){f+=I.delta,u+=I.delta;let O=Di(f);if(O>0){let T=f.slice(0,O).trimEnd();T&&d({kind:"assistant",text:T}),f=f.slice(O)}P(),Ye(T=>T+I.delta.length)}else if(I.type==="tool"){let O=hn(I.name,I.input);O&&(j(),d({kind:"tool",title:O.title,sep:O.sep,arg:O.arg}),Nt.current.set(I.id,{name:I.name,input:I.input}))}else if(I.type==="article_diff"){let O=qe(I.oldContent,I.newContent,je());O&&(j(),d({kind:"diff",added:O.added,removed:O.removed,lines:O.lines,hidden:O.hidden}))}else if(I.type==="tool_result"){I.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(I.output)&&(Ot.current=!0);let O=Nt.current.get(I.id);if(O){Nt.current.delete(I.id),j();let T=wn(I.output,4,I.isError?void 0:O.name,O.input);d({kind:"tool-result",lines:T.lines,hidden:T.hidden,isError:I.isError}),I.isError||pr(O.name,O.input,I.output)}}else if(I.type==="result"){j(),st(T=>T+I.outputTokens),it(T=>T+I.costUsd),d({kind:"done",seconds:Math.round((Date.now()-Me.current)/1e3),tokens:I.outputTokens,costUsd:I.costUsd,ok:I.ok});let O=I.ok?yi(u,yt.map(T=>T.name)):[];if(O.length>0?X(O):I.ok&&u.trim()&&Ku(l,u,e).then(T=>{T&&ze.current===a&&re(T)}).catch(()=>{}),h.uuid&&(Vo(h.uuid),!h.titleFired)){h.titleFired=!0;let T=h.uuid,D=h.firstPrompt;D&&Ni(D,e).then(W=>{W&&(qu(T,W),Qe(W))}).catch(()=>{})}}else I.type==="error"&&(j(),I.kind==="auth"&&(Ot.current=!0),d({kind:"note",text:`agent error: ${I.message}`,tone:"error"}))}finally{_(!1),M(),te(I=>I+1),xe.current.uuid&&wi(e,xe.current.uuid,Ke.current),Ot.current&&(Ot.current=!1,Lt.current=i?.display??l,X(["/login"])),Mt.current&&(Mt.current=!1,Fe.current.length>0&&(d({kind:"note",tone:"info",text:`(${Fe.current.length} queued message(s) discarded)`}),Fe.current=[],En([])),d({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,d,pr,je]),Ft=V(async l=>{let i=Ko(l),a=Sn(e,"standard");Me.current=Date.now(),Ye(0),_(!0);let h=new AbortController;ge.current=h,K({verb:"Publishing",total:i.length,done:0,active:[],rows:jt(i),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Publishing ${l.length} article${l.length===1?"":"s"} across ${i.length} partition${i.length===1?"":"s"} (\u2264${vt} agents at once) on ${a.model}\u2026 (esc to stop)`});try{for await(let u of Tn({cwd:e,partitions:i,promptFor:f=>rn(f.paths),concurrency:vt,profileName:s,allowProdWrites:Je.current,model:a.model,signal:h.signal}))if(u.type==="partition_status")u.status==="running"?K(f=>f&&{...f,active:[...f.active,u.label],rows:Pe(f.rows,u.label,"active"),lastAt:Date.now()}):K(f=>f&&{...f,active:f.active.filter(k=>k!==u.label),done:f.done+1,rows:Pe(f.rows,u.label,u.status==="done"?"done":"failed"),lastAt:Date.now()});else if(u.type==="partition_event")K(f=>{if(!f)return f;let k={...f,lastAt:Date.now()};return u.event.type==="tool"?k.tools=f.tools+1:u.event.type==="text"&&(k.chars=f.chars+u.event.delta.length),k});else if(u.type==="run_done"){it(P=>P+u.totalCostUsd),st(P=>P+u.results.reduce((M,j)=>M+j.outputTokens,0));let f=u.aborted?"Stopped. ":"",k=t.kind==="api"?"api":"subscription";d({kind:"note",tone:u.aborted?"warn":u.ok?"ok":"warn",text:f+no(u.results,i,k).join(`
180
- `)})}}catch(u){d({kind:"note",tone:"error",text:`Publish run failed: ${u.message}`})}finally{ge.current=null,K(null),_(!1),te(u=>u+1)}},[e,s,d,t]),Mn=V(async l=>{await ve(to(l),{display:`/publish ${l}`,echoDisplay:!0});try{let i=await sn(e,s,l.replace(/\\/g,"/"));i.length>0&&(_t(`Related articles not yet published (${i.length}):`),X([`/publish --related ${l}`,...i.map(a=>`/publish ${a}`)]))}catch{}},[e,s,ve]),On=V(l=>{let i=Ge(l);if(!i||Ne(i)&&!i.refreshToken)return null;let a={...dt(i.idToken)??{},...dt(i.accessToken)??{}};return a.email??a.preferred_username??"signed in"},[]),Ln=V((l,i)=>{if(i){let a=oe(e);a&&(a.defaultProfile=l,Ju(a,e))}c(l),ce(void 0,l),d({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${l}"${i?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),On(l)||(d({kind:"note",tone:"warn",text:`Profile "${l}" is not signed in \u2014 do you want to log in now? (press 1)`}),X(["/login"])),te(a=>a+1)},[e,d,ce,On]),Ht=V((l,i,a)=>{Yt(e,l,i,a.id,a.name),d({kind:"note",tone:"ok",text:`Switched to workspace "${a.name??a.id}" (agent restarted).`}),ce(),_i(e,l)||(d({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),re("analyze this repo and propose a docs structure"))},[e,d,ce]),Bt=V(async l=>{let i;try{i=await wt(e,l)}catch{d({kind:"note",tone:"info",text:"Next: pick a workspace \u2014 run /workspace."}),X(["/workspace"]);return}if(i.workspaces.length===0){d({kind:"note",tone:"warn",text:"This project has no workspaces yet. Create one in Document360, then run /workspace."});return}let a=i.workspaces.filter(u=>(u.workspace_type??"").toLowerCase()!=="apidocumentation"),h=a.length>0?a:i.workspaces;if(h.length===1){let u=h[0],f=i.workspaces.length-h.length;d({kind:"note",tone:"ok",text:`Selected the "${u.name??u.id}" workspace${f>0?" (skipped the API-documentation workspace)":""}.`}),Ht(i.profile,i.projectId,u);return}d({kind:"note",tone:"info",text:`Next: pick the workspace your articles publish to (${h.length} available).`}),X(["/workspace"])},[e,d,Ht]),fr=V((l,i)=>{So(e,l,i.id,i.name),d({kind:"note",tone:"ok",text:`Project set to "${i.name??i.id}" (agent restarted).`}),ce(),Bt(l)},[e,d,ce,Bt]),Un=V(async l=>{let i;try{i=de(e,l)}catch{return}if(!i.project.workspaceId){if(i.project.projectId){if(!i.project.projectName)try{let{projects:a}=await fn(e,i.name),h=a.find(u=>u.id===i.project.projectId);h?.name&&dd(e,i.name,{projectName:h.name})}catch{}await Bt(i.name);return}d({kind:"note",tone:"info",text:"Next: pick the Document360 project."}),X(["/project"])}},[e,d,Bt]),qt=V(()=>{let l=Se.current[0];if(!l)return;d({kind:"note",tone:"info",text:`\u25CF ${l.title} (${l.path})`});for(let a of l.notes)d({kind:"note",tone:"warn",text:`\u26A0 ${a}`});l.overwritesLocalChanges&&d({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let i=qe(l.oldContent,l.newContent,je());d(i?{kind:"diff",added:i.added,removed:i.removed,lines:i.lines,hidden:i.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),d({kind:"note",tone:"info",text:`Write ${l.path}? (y/n \u2014 anything else cancels)`})},[d,je]),mr=V(l=>{if(Se.current.length===0)return!1;let i=l.trim().toLowerCase();if(i==="y"||i==="yes"){let a=Se.current.shift();try{Qu({cwd:e,profileName:s},a),d({kind:"note",tone:"ok",text:`\u2713 Pulled ${a.path} (sync base advanced).`}),Se.current.length===0&&X(h=>h.length>0?h:["/sync"])}catch(h){d({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(i==="n"||i==="no"){let a=Se.current.shift();d({kind:"note",tone:"info",text:`Skipped ${a.path}.`})}else{let a=Se.current.length;return Se.current=[],d({kind:"note",tone:"info",text:`Pull cancelled (${a} article(s) left untouched).`}),!0}return qt(),!0},[e,s,qt,d]),gr=V(l=>{let i=An.current;if(!i)return!1;if(An.current=null,re(null),ze.current++,l.trim()!==i.repoName)return d({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:a,failed:h}=Oo(e,i.targets);d({kind:"note",tone:h.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${a.length} item${a.length===1?"":"s"}. The repo is back to its original state.`});for(let u of h)d({kind:"note",tone:"error",text:` \u2717 ${u.path}: ${u.error}`});return d({kind:"note",tone:"info",text:`Clean slate \u2014 set up d360-writer again whenever you're ready:
178
+ Press 1 to start.`;function fd(e,t,n,o){let r=n.kind==="api"?"API key":n.kind==="subscription"?"subscription":"not configured",s=Qo(e),c=oe(e),p=(c?.docsDir??qi).replace(/\/+$/,""),g=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${p}/ + config`,w={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:pd[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:g};if(c===null)return w;try{let $=de(e,o);w.profile=$.name,w.apiUrl=$.connection.apiUrl,w.prod=$.production,w.project=$.project.projectName??$.project.projectId??"(chosen at login)";let R=Ge($.name);if(R){let y={...dt(R.idToken)??{},...dt(R.accessToken)??{}},b=y.email??y.preferred_username??"signed in";Ne(R)?R.refreshToken&&(w.who=b,w.loggedOut=!1,w.sessionHint="session expired \u2014 refreshing\u2026"):(w.who=b,w.loggedOut=!1,w.sessionHint=`session valid until ${new Date(R.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return w}function md(e,t){try{let n=de(e,t),o=Ge(n.name);if(!o)return{text:`profile "${n.name}" \u2014 not logged in (/login)`,prod:n.production};let r={...dt(o.idToken)??{},...dt(o.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return Ne(o)&&!o.refreshToken?{text:`profile "${n.name}" \u2014 session expired (/login)`,prod:n.production}:{text:`${s} \xB7 profile "${n.name}"`,prod:n.production}}catch(n){return{text:n.message.split(".")[0],prod:!1}}}var Wi=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],jn=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],gd="Ask me to write or update an article\u2026";function Rn({ch:e,dim:t}){let[n,o]=B(!0);return _e(()=>{let r=setInterval(()=>o(s=>!s),530);return()=>clearInterval(r)},[]),S(C,{inverse:n,color:t&&!n?"gray":void 0,children:e})}var hd=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Fi(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var wd=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Hi(e){try{return Bi(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function kd(e,t){let n=t??[];return n.length===0?["src","api","services","packages","modules"].some(o=>Hi(At(e,o))):n.some(o=>!o.includes("/")&&!o.endsWith(".md")&&Hi(At(e,o)))}function yd({startTime:e,chars:t}){let[n,o]=B(0);_e(()=>{let g=setInterval(()=>o(w=>w+1),120);return()=>clearInterval(g)},[]);let r=jn[n%jn.length],s=Wi[Math.floor(n/16)%Wi.length],c=Math.floor((Date.now()-e)/1e3),p=Math.round(t/4);return F(J,{children:[S(C,{color:G,children:` ${r} ${s}\u2026 `}),S(C,{color:"gray",children:`(${bt(c)} \xB7 ~${p} tokens \xB7 esc to interrupt)`})]})}var $d=12e4;function xd({p:e}){let[t,n]=B(0);_e(()=>{let _=setInterval(()=>n(H=>H+1),150);return()=>clearInterval(_)},[]);let o=jn[t%jn.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),p=Date.now()-e.lastAt>=$d,g=e.verb??"Converting",w=e.unit??"article",$=e.rows.reduce((_,H)=>_+H.items,0),R=g.toLowerCase(),y=e.rows,b=0;if(y.length>vn){let _=y.findIndex(Z=>Z.status==="active"||Z.status==="pending"),H=Math.max(0,Math.min(_-2,y.length-vn));b=y.slice(0,H).filter(Z=>Z.status==="done").length,y=y.slice(H,H+vn)}let v=Math.min(40,Math.max(...y.map(_=>qo(_.label).length),0)),N=_=>_==="failed"?"red":_==="active"?G:void 0;return F(J,{flexDirection:"column",children:[F(J,{children:[S(C,{color:G,children:` ${o} ${g} ${$} ${w}${$===1?"":"s"} `}),S(C,{color:"gray",children:`\xB7 ${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${bt(r)} \xB7 esc to stop`})]}),b>0&&S(C,{dimColor:!0,children:` \u2714 +${b} done`}),y.map(_=>F(C,{color:N(_.status),dimColor:_.status==="done"||_.status==="pending",children:[` ${$i[_.status]} ${qo(_.label).padEnd(v)}`,_.status!=="pending"?` (${_.items} ${w}${_.items===1?"":"s"})`:"",_.status==="active"?` \u2190 ${R}\u2026`:""]},_.label)),p&&S(C,{color:"yellow",children:` \u26A0 no activity for ${bt(s)} \u2014 press esc to stop if it's wedged`})]})}function Gi({cwd:e,auth:t,profileName:n,version:o}){let{exit:r}=Iu(),[s,c]=B(n),[p,g]=B(null),[w,$]=B({text:"",pos:0}),R=w.text,y=V(l=>{$(i=>{let a=typeof l=="function"?l(i.text):l;return{text:a,pos:a.length}})},[]),b=V(l=>{$(i=>({text:i.text.slice(0,i.pos)+l+i.text.slice(i.pos),pos:i.pos+l.length}))},[]),v=V(()=>{$(l=>{if(l.pos===0)return l;let i=l.text.slice(0,l.pos),a=Ti(i)??i.slice(0,-1);return{text:a+l.text.slice(l.pos),pos:a.length}})},[]),[N,_]=B(!1),[H,Z]=B(!1),[$e,te]=B(0),[we,re]=B(null),[pt,X]=B([]),ze=Q(0),[Xi,Et]=B(!1),[Ki,Ye]=B(0),Me=Q(0),[ft,Ve]=B(0),[Xe,It]=B(null),tr=Q(new Map),Ji=Q(0),Ke=Q([]),pe=Q(null);pe.current===null&&(pe.current=Ii({cwd:e,profileName:s,allowProdWrites:!1}));let xe=Q({uuid:null,firstPrompt:null,titleFired:!1}),Nt=Q(new Map),Je=Q(!1),Dn=Q([]),Se=Q([]),An=Q(null),[nr,Qe]=B(null),[Oe,Le]=B(null),[be,Ze]=B(null),[Re,et]=B(null),[se,tt]=B(null),[fe,nt]=B(null),[Ue,ot]=B(null),[or,_t]=B(null),[me,We]=B(null),[le,rt]=B(null),[rr,En]=B([]),Fe=Q([]),Mt=Q(!1),ge=Q(null),[sr,K]=B(null),Ot=Q(!1),Lt=Q(null),[he,He]=B(null),[ir,st]=B(0),[ar,it]=B(0),[Qi,lr]=B(0),In=zo(()=>{try{return Qo(e).model??"auto"}catch{return null}},[e,Qi]),{stdout:ke}=_u(),[,Zi]=B(0),cr=Q(`${ke.columns??80}x${ke.rows??24}`),je=V(()=>Math.max(20,(ke.columns??80)-1),[ke]),d=V(l=>{Ke.current.push(l),console.log(Qn(l,je()))},[je]);_e(()=>{let l=`d360-writer \xB7 ${nr??Dt(e)}`;if(!N){xn(0),Rt(`${Bo} ${l}`);return}xn(3);let i=0;Rt(`${bn[0]} ${l}`);let a=setInterval(()=>{i=(i+1)%bn.length,Rt(`${bn[i]} ${l}`)},400);return()=>clearInterval(a)},[N,e,nr]),_e(()=>()=>xn(0),[]),_e(()=>{if(d({kind:"banner",info:fd(e,o,t,s)}),!oe(e)){d({kind:"note",tone:"info",text:`Welcome! This repo isn't set up for d360-writer yet \u2014 three steps and you're writing docs:
179
+ ${Ui}`}),X(["/init"]);return}try{let l=de(e,s),i=Ge(l.name);i&&Ne(i)&&i.refreshToken?Wu({profile:l.name,connection:l.connection}).then(()=>{d({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),te(a=>a+1)}).catch(()=>{d({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),X(["/login"])}):(!i||Ne(i))&&(d({kind:"note",tone:"warn",text:`Profile "${l.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),X(["/login"]))}catch{}try{let l=oe(e),i=de(e,s),a=Ge(i.name);l&&a&&!Ne(a)&&kd(e,l.authoritativeSourceFiles)&&(d({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),X(["/scope"]))}catch{}},[]),_e(()=>{let l=null,i=null,a=()=>{l&&clearTimeout(l),l=setTimeout(()=>{l=null;let h=`${ke.columns??80}x${ke.rows??24}`;h!==cr.current&&(cr.current=h,Zi(u=>u+1),i&&clearTimeout(i),i=setTimeout(()=>{i=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+es(Ke.current,je())+"\x1B[?2026l")},80))},400)};return ke.on("resize",a),()=>{l&&clearTimeout(l),i&&clearTimeout(i),ke.off("resize",a)}},[ke,je]);let Be=Math.max(20,(ke.columns??80)-1),ur=zo(()=>md(e,s),[e,s,$e]),Nn=(()=>{let l=oe(e);if(!l)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};let i=At(e,(l.docsDir??qi).replace(/\/+$/,"")),a=(()=>{try{return Yo(i)&&Bi(i).length>0}catch{return!1}})();try{let h=de(e,s),u=Ge(h.name);if(!(!!u&&!(Ne(u)&&!u.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${h.name}")\u2026`,isSetup:!0};if(!h.project.workspaceId&&!a)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}return a?{text:gd,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}})(),mt=Ir(R),dr=mt.length>0&&!N,_n=p!==null?ji(p,8,Be):null,Ut=me?me.paths.filter(l=>!me.query||l.toLowerCase().includes(me.query.toLowerCase())).slice(0,8):[],Wt=he?he.sessions.filter(l=>{let i=he.query.toLowerCase();return!i||l.name.toLowerCase().includes(i)||l.firstPrompt.toLowerCase().includes(i)}).slice(0,8):[],ce=V((l,i)=>{pe.current?.close(),pe.current=Ii({cwd:e,resume:l,profileName:i??s,allowProdWrites:Je.current}),l||(xe.current={uuid:null,firstPrompt:null,titleFired:!1},Qe(null)),st(0),it(0)},[e,s]),pr=V((l,i,a)=>{if(hd.test(l))try{let h=de(e,s),u=typeof i.project_id=="string"&&i.project_id||h.project.projectId,f=yn(i,a),k=[],P=$n(a);l.endsWith("publish_article")&&P&&k.push(`Live: ${P}`),f&&u&&k.push(`Preview: ${kn(h.connection.portalUrl,u,f,h.project.languageCode??"en")}`),k.length>0&&d({kind:"link",lines:k})}catch{}},[e,s,d]),ve=V(async(l,i)=>{Et(!0),re(null),X([]),_t(null);let a=++ze.current;d({kind:"user",text:i?.echoDisplay&&i.display?i.display:l});let h=xe.current;h.firstPrompt||(h.firstPrompt=i?.display??l),Me.current=Date.now(),Ye(0),_(!0),Nt.current.clear();let u="",f="",k=null,P=()=>{k||(k=setTimeout(()=>{k=null,g(f.length>0?f:null)},60))},M=()=>{k&&clearTimeout(k),k=null,g(null)},j=()=>{if(f.trim()){let I=f.trimEnd();d({kind:"assistant",text:I})}f="",M()};try{for await(let I of pe.current.send(l))if(I.type==="session"){if(!h.uuid){h.uuid=I.sessionId;let O=new Date().toISOString(),T=zu(h.firstPrompt??"session");Yu({uuid:I.sessionId,name:T,renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:O,updatedAt:O}),Qe(T)}}else if(I.type==="text"){f+=I.delta,u+=I.delta;let O=Di(f);if(O>0){let T=f.slice(0,O).trimEnd();T&&d({kind:"assistant",text:T}),f=f.slice(O)}P(),Ye(T=>T+I.delta.length)}else if(I.type==="tool"){let O=hn(I.name,I.input);O&&(j(),d({kind:"tool",title:O.title,sep:O.sep,arg:O.arg}),Nt.current.set(I.id,{name:I.name,input:I.input}))}else if(I.type==="article_diff"){let O=qe(I.oldContent,I.newContent,je());O&&(j(),d({kind:"diff",added:O.added,removed:O.removed,lines:O.lines,hidden:O.hidden}))}else if(I.type==="tool_result"){I.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(I.output)&&(Ot.current=!0);let O=Nt.current.get(I.id);if(O){Nt.current.delete(I.id),j();let T=wn(I.output,4,I.isError?void 0:O.name,O.input);d({kind:"tool-result",lines:T.lines,hidden:T.hidden,isError:I.isError}),I.isError||pr(O.name,O.input,I.output)}}else if(I.type==="result"){j(),st(T=>T+I.outputTokens),it(T=>T+I.costUsd),d({kind:"done",seconds:Math.round((Date.now()-Me.current)/1e3),tokens:I.outputTokens,costUsd:I.costUsd,ok:I.ok});let O=I.ok?yi(u,yt.map(T=>T.name)):[];if(O.length>0?X(O):I.ok&&u.trim()&&Ju(l,u,e).then(T=>{T&&ze.current===a&&re(T)}).catch(()=>{}),h.uuid&&(Vo(h.uuid),!h.titleFired)){h.titleFired=!0;let T=h.uuid,D=h.firstPrompt;D&&Ni(D,e).then(W=>{W&&(Gu(T,W),Qe(W))}).catch(()=>{})}}else I.type==="error"&&(j(),I.kind==="auth"&&(Ot.current=!0),d({kind:"note",text:`agent error: ${I.message}`,tone:"error"}))}finally{_(!1),M(),te(I=>I+1),xe.current.uuid&&wi(e,xe.current.uuid,Ke.current),Ot.current&&(Ot.current=!1,Lt.current=i?.display??l,X(["/login"])),Mt.current&&(Mt.current=!1,Fe.current.length>0&&(d({kind:"note",tone:"info",text:`(${Fe.current.length} queued message(s) discarded)`}),Fe.current=[],En([])),d({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,d,pr,je]),Ft=V(async l=>{let i=Ko(l),a=Sn(e,"standard");Me.current=Date.now(),Ye(0),_(!0);let h=new AbortController;ge.current=h,K({verb:"Publishing",total:i.length,done:0,active:[],rows:jt(i),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Publishing ${l.length} article${l.length===1?"":"s"} across ${i.length} partition${i.length===1?"":"s"} (\u2264${vt} agents at once) on ${a.model}\u2026 (esc to stop)`});try{for await(let u of Tn({cwd:e,partitions:i,promptFor:f=>rn(f.paths),concurrency:vt,profileName:s,allowProdWrites:Je.current,model:a.model,signal:h.signal}))if(u.type==="partition_status")u.status==="running"?K(f=>f&&{...f,active:[...f.active,u.label],rows:Pe(f.rows,u.label,"active"),lastAt:Date.now()}):K(f=>f&&{...f,active:f.active.filter(k=>k!==u.label),done:f.done+1,rows:Pe(f.rows,u.label,u.status==="done"?"done":"failed"),lastAt:Date.now()});else if(u.type==="partition_event")K(f=>{if(!f)return f;let k={...f,lastAt:Date.now()};return u.event.type==="tool"?k.tools=f.tools+1:u.event.type==="text"&&(k.chars=f.chars+u.event.delta.length),k});else if(u.type==="run_done"){it(P=>P+u.totalCostUsd),st(P=>P+u.results.reduce((M,j)=>M+j.outputTokens,0));let f=u.aborted?"Stopped. ":"",k=t.kind==="api"?"api":"subscription";d({kind:"note",tone:u.aborted?"warn":u.ok?"ok":"warn",text:f+no(u.results,i,k).join(`
180
+ `)})}}catch(u){d({kind:"note",tone:"error",text:`Publish run failed: ${u.message}`})}finally{ge.current=null,K(null),_(!1),te(u=>u+1)}},[e,s,d,t]),Mn=V(async l=>{await ve(to(l),{display:`/publish ${l}`,echoDisplay:!0});try{let i=await sn(e,s,l.replace(/\\/g,"/"));i.length>0&&(_t(`Related articles not yet published (${i.length}):`),X([`/publish --related ${l}`,...i.map(a=>`/publish ${a}`)]))}catch{}},[e,s,ve]),On=V(l=>{let i=Ge(l);if(!i||Ne(i)&&!i.refreshToken)return null;let a={...dt(i.idToken)??{},...dt(i.accessToken)??{}};return a.email??a.preferred_username??"signed in"},[]),Ln=V((l,i)=>{if(i){let a=oe(e);a&&(a.defaultProfile=l,Qu(a,e))}c(l),ce(void 0,l),d({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${l}"${i?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),On(l)||(d({kind:"note",tone:"warn",text:`Profile "${l}" is not signed in \u2014 do you want to log in now? (press 1)`}),X(["/login"])),te(a=>a+1)},[e,d,ce,On]),Ht=V((l,i,a)=>{Yt(e,l,i,a.id,a.name),d({kind:"note",tone:"ok",text:`Switched to workspace "${a.name??a.id}" (agent restarted).`}),ce(),_i(e,l)||(d({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),re("analyze this repo and propose a docs structure"))},[e,d,ce]),Bt=V(async l=>{let i;try{i=await wt(e,l)}catch{d({kind:"note",tone:"info",text:"Next: pick a workspace \u2014 run /workspace."}),X(["/workspace"]);return}if(i.workspaces.length===0){d({kind:"note",tone:"warn",text:"This project has no workspaces yet. Create one in Document360, then run /workspace."});return}let a=i.workspaces.filter(u=>(u.workspace_type??"").toLowerCase()!=="apidocumentation"),h=a.length>0?a:i.workspaces;if(h.length===1){let u=h[0],f=i.workspaces.length-h.length;d({kind:"note",tone:"ok",text:`Selected the "${u.name??u.id}" workspace${f>0?" (skipped the API-documentation workspace)":""}.`}),Ht(i.profile,i.projectId,u);return}d({kind:"note",tone:"info",text:`Next: pick the workspace your articles publish to (${h.length} available).`}),X(["/workspace"])},[e,d,Ht]),fr=V((l,i)=>{So(e,l,i.id,i.name),d({kind:"note",tone:"ok",text:`Project set to "${i.name??i.id}" (agent restarted).`}),ce(),Bt(l)},[e,d,ce,Bt]),Un=V(async l=>{let i;try{i=de(e,l)}catch{return}if(!i.project.workspaceId){if(i.project.projectId){if(!i.project.projectName)try{let{projects:a}=await fn(e,i.name),h=a.find(u=>u.id===i.project.projectId);h?.name&&dd(e,i.name,{projectName:h.name})}catch{}await Bt(i.name);return}d({kind:"note",tone:"info",text:"Next: pick the Document360 project."}),X(["/project"])}},[e,d,Bt]),qt=V(()=>{let l=Se.current[0];if(!l)return;d({kind:"note",tone:"info",text:`\u25CF ${l.title} (${l.path})`});for(let a of l.notes)d({kind:"note",tone:"warn",text:`\u26A0 ${a}`});l.overwritesLocalChanges&&d({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let i=qe(l.oldContent,l.newContent,je());d(i?{kind:"diff",added:i.added,removed:i.removed,lines:i.lines,hidden:i.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),d({kind:"note",tone:"info",text:`Write ${l.path}? (y/n \u2014 anything else cancels)`})},[d,je]),mr=V(l=>{if(Se.current.length===0)return!1;let i=l.trim().toLowerCase();if(i==="y"||i==="yes"){let a=Se.current.shift();try{Zu({cwd:e,profileName:s},a),d({kind:"note",tone:"ok",text:`\u2713 Pulled ${a.path} (sync base advanced).`}),Se.current.length===0&&X(h=>h.length>0?h:["/sync"])}catch(h){d({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(i==="n"||i==="no"){let a=Se.current.shift();d({kind:"note",tone:"info",text:`Skipped ${a.path}.`})}else{let a=Se.current.length;return Se.current=[],d({kind:"note",tone:"info",text:`Pull cancelled (${a} article(s) left untouched).`}),!0}return qt(),!0},[e,s,qt,d]),gr=V(l=>{let i=An.current;if(!i)return!1;if(An.current=null,re(null),ze.current++,l.trim()!==i.repoName)return d({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:a,failed:h}=Oo(e,i.targets);d({kind:"note",tone:h.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${a.length} item${a.length===1?"":"s"}. The repo is back to its original state.`});for(let u of h)d({kind:"note",tone:"error",text:` \u2717 ${u.path}: ${u.error}`});return d({kind:"note",tone:"info",text:`Clean slate \u2014 set up d360-writer again whenever you're ready:
181
181
  ${Ui}`}),X(["/init"]),Et(!1),!0},[e,d]),hr=V(async l=>{let i=l.slice(1).trim().split(/\s+/),a=(i[0]??"").toLowerCase(),h=i.slice(1);switch(Et(!0),a){case"help":d({kind:"note",tone:"info",text:tn().join(`
182
- `)});return;case"exit":case"quit":pe.current?.close(),r();return;case"clear":ce(),Ke.current=[],Et(!1),re(null),ze.current++,d({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let u;try{u=de(e,s)}catch(f){d({kind:"note",tone:"error",text:f.message});return}d({kind:"note",tone:"info",text:`Profile "${u.name}" \u2192 ${u.connection.name} (${u.connection.apiUrl})${u.production?" \u26A0 PRODUCTION":""}`}),Z(!0);try{let f=await Mu(u.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},P=>d({kind:"note",tone:"info",text:P})),k=Ou(u.name,f);Lu(k),Jt(k,u.name,P=>d({kind:"note",tone:"info",text:P})),d({kind:"note",tone:"ok",text:`\u2713 Logged in to "${u.name}" as ${Xt(k)}`}),await Un(s),Lt.current&&(re(Lt.current),Lt.current=null,d({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(f){d({kind:"note",tone:"error",text:`Login failed: ${f.message}`})}finally{Z(!1),te(f=>f+1)}return}case"allow-prod":{let u=!1;try{u=de(e,s).production}catch{}if(!u){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}Je.current=!0,ce(),d({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let u=vo(h.join(" ")),f=xe.current.uuid;if(!f){d({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!u){d({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let k=xe.current.firstPrompt??"";Ni(k,e).then(P=>{P?(re(`/rename ${P}`),d({kind:"note",tone:"info",text:`Suggestion: "${P}" \u2014 press tab to accept, or type /rename <your name>.`})):d({kind:"note",tone:"info",text:"Usage: /rename <name>"})}).catch(()=>d({kind:"note",tone:"info",text:"Usage: /rename <name>"}));return}Xu(f,u),Qe(u),Rt(`${Bo} d360-writer \xB7 ${u}`),d({kind:"note",tone:"ok",text:`Session renamed to "${u}".`});return}case"profile":{let u=h[0],f=oe(e);if(!u){let k=Object.entries(f?.profiles??{});if(k.length===0){d({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let P=k.map(([I,O])=>({name:I,env:O.connection?.environment??"custom",prod:O.production===!0,who:On(I)})),M=s??f?.defaultProfile,j=Math.max(0,P.findIndex(I=>I.name===M));Ze({cursor:j,current:j,rows:P});return}if(u==="add"){let k=Co(e,h[1],h[2]);if(k){d({kind:"note",tone:"error",text:k});return}d({kind:"note",tone:"ok",text:`\u2713 Profile "${h[1]}" created (environment: ${h[2]??h[1]}).`}),Ln(h[1],!1);return}if(!f?.profiles?.[u]){d({kind:"note",tone:"error",text:`Unknown profile "${u}". Create it: /profile add ${u} <environment>`});return}Ln(u,!0);return}case"doctor":await Tt(h,{cwd:e});return;case"mcp":await on(h);return;case"init":{if(oe(e)){d({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let u=td().map(f=>({name:f,apiUrl:nd(f).apiUrl}));et({cursor:0,rows:u});return}case"resume":{let u=h.join(" ").trim(),f=Vu(e).filter(M=>M.uuid!==xe.current.uuid);if(!u){if(!f.length){d({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}He({query:"",cursor:0,sessions:f});return}let k=Yu(e,u);if(!k){d({kind:"note",tone:"error",text:`No session matches "${u}".`});return}ce(k.uuid),xe.current={uuid:k.uuid,firstPrompt:k.firstPrompt,titleFired:!0},Qe(k.name),Vo(k.uuid),Ke.current=[];let P=Ho(e,k.uuid);for(let M of P)d(M);d({kind:"note",tone:"ok",text:P.length?`Resumed "${k.name}" \u2014 restored ${P.length} message(s); continue where you left off.`:`Resumed "${k.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let u=h.join(" ").trim(),f;try{f=await wt(e,s)}catch(P){d({kind:"note",tone:"error",text:`Could not list workspaces: ${P.message}`});return}if(!u){let P=f.workspaces.map(j=>({id:j.id,name:j.name??j.id,type:j.workspace_type}));if(P.length===0){d({kind:"note",tone:"info",text:"No workspaces in this project."});return}let M=Math.max(0,P.findIndex(j=>j.id===f.current));tt({cursor:M,current:M,rows:P,profile:f.profile,projectId:f.projectId,projectName:f.projectName,environment:f.environment});return}let k=Hn(f.workspaces,u);if(!k){d({kind:"note",tone:"error",text:`No workspace matches "${u}".`});return}Ht(f.profile,f.projectId,k);return}case"project":{let u=h.join(" ").trim(),f;try{f=await fn(e,s)}catch(P){d({kind:"note",tone:"error",text:`Could not list projects: ${P.message} (signed in? try /login)`});return}if(!u){let P=f.projects.map(j=>({id:j.id,name:j.name??j.id,sub:j.sub_domain}));if(P.length===0){d({kind:"note",tone:"info",text:"No projects found for this identity."});return}let M=P.findIndex(j=>j.id===f.current);nt({cursor:Math.max(0,M),current:M,rows:P,profile:f.profile,environment:f.environment});return}let k=Ls(f.projects,u);if(!k){d({kind:"note",tone:"error",text:`No project matches "${u}". Available: ${f.projects.map(P=>P.name??P.id).join(", ")}`});return}fr(f.profile,k);return}case"logout":{let u;try{u=de(e,s).name}catch(k){d({kind:"note",tone:"error",text:k.message});return}let f=Hu(u);Bu(e,u),ce(),te(k=>k+1),d({kind:"note",tone:f?"ok":"info",text:f?`\u2713 Signed out of "${u}" and cleared its project/workspace selection.`:`Profile "${u}" was not signed in. Cleared any project/workspace selection.`}),d({kind:"note",tone:"info",text:"Run /login to sign in and pick a project."}),X(["/login"]);return}case"publish":{if(h[0]==="--related"&&h[1]){let f=await sn(e,s,h[1].replace(/\\/g,"/"));if(f.length===0){d({kind:"note",tone:"ok",text:"\u2713 All related articles are already published."});return}await Ft(f);return}if(h[0]&&h[0]!=="--all"){await Mn(h[0]);return}let u=h[0]==="--all";d({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let f=await Xo({cwd:e,profileName:s}),k=Ct(f.entries);if(k.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let P=f.counts["unknown-base"]??0;P>0&&d({kind:"note",tone:"info",text:`(${P} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(u){await Ft(k.map(P=>P.path));return}ot({cursor:0,rows:ns(k)})}catch(f){d({kind:"note",tone:"error",text:`Could not compute sync status: ${f.message}`}),d({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let u=h.join(" ").trim();if(!u){let k=[];try{k=Object.keys(_i(e,de(e,s).name)?.articles??{})}catch{}if(k.length===0){d({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}We({query:"",cursor:0,paths:k});return}let f=_u(u)?u:At(e,u);if(Yo(f)){try{d({kind:"preview",name:Dt(f),text:Fi(Ai(f,"utf8"))})}catch(k){d({kind:"note",tone:"error",text:`Could not read ${f}: ${k.message}`})}return}if(wd.test(u)){try{let k=de(e,s),P={profile:k.name,connection:k.connection},M=k.project.projectId??Wu(P),j=await Fu(P,M,u);d({kind:"preview",name:j.title??u,text:j.content??"*(article has no content)*"})}catch(k){d({kind:"note",tone:"error",text:`Could not fetch article: ${k.message}`})}return}d({kind:"note",tone:"error",text:`"${u}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let u=h[0]?.trim();if(!u){let M=dn(Qo(e));Le({cursor:M,current:M});return}let{lines:f,changed:k,effective:P}=Pt(e,u);for(let M of f)d({kind:"note",tone:M.startsWith("\u26A0")?"warn":M.startsWith("\u2713")?"ok":"info",text:M});k&&(lr(M=>M+1),pe.current?.setModel(P));return}case"convert":{if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:u,run:f}=io(h),k=sd(e,s);if(k.length===0){d({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let P=ao(k,u);if(P.length===0){d({kind:"note",tone:"error",text:`No tracked articles under "${u}". (${k.length} are tracked overall.)`});return}let M=Ko(P),j=3,I=`/convert${u?` --scope ${u}`:""} --run`,O=Sn(e,"light");if(!f){let D=Mi({files:lo(e,P),op:"convert",model:O.model}),W=u?`Scope: ${u} (${P.length} of ${k.length} tracked)
182
+ `)});return;case"exit":case"quit":pe.current?.close(),r();return;case"clear":ce(),Ke.current=[],Et(!1),re(null),ze.current++,d({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let u;try{u=de(e,s)}catch(f){d({kind:"note",tone:"error",text:f.message});return}d({kind:"note",tone:"info",text:`Profile "${u.name}" \u2192 ${u.connection.name} (${u.connection.apiUrl})${u.production?" \u26A0 PRODUCTION":""}`}),Z(!0);try{let f=await Ou(u.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},P=>d({kind:"note",tone:"info",text:P})),k=Lu(u.name,f);Uu(k),Jt(k,u.name,P=>d({kind:"note",tone:"info",text:P})),d({kind:"note",tone:"ok",text:`\u2713 Logged in to "${u.name}" as ${Xt(k)}`}),await Un(s),Lt.current&&(re(Lt.current),Lt.current=null,d({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(f){d({kind:"note",tone:"error",text:`Login failed: ${f.message}`})}finally{Z(!1),te(f=>f+1)}return}case"allow-prod":{let u=!1;try{u=de(e,s).production}catch{}if(!u){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}Je.current=!0,ce(),d({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let u=vo(h.join(" ")),f=xe.current.uuid;if(!f){d({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!u){d({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let k=xe.current.firstPrompt??"";Ni(k,e).then(P=>{P?(re(`/rename ${P}`),d({kind:"note",tone:"info",text:`Suggestion: "${P}" \u2014 press tab to accept, or type /rename <your name>.`})):d({kind:"note",tone:"info",text:"Usage: /rename <name>"})}).catch(()=>d({kind:"note",tone:"info",text:"Usage: /rename <name>"}));return}Ku(f,u),Qe(u),Rt(`${Bo} d360-writer \xB7 ${u}`),d({kind:"note",tone:"ok",text:`Session renamed to "${u}".`});return}case"profile":{let u=h[0],f=oe(e);if(!u){let k=Object.entries(f?.profiles??{});if(k.length===0){d({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let P=k.map(([I,O])=>({name:I,env:O.connection?.environment??"custom",prod:O.production===!0,who:On(I)})),M=s??f?.defaultProfile,j=Math.max(0,P.findIndex(I=>I.name===M));Ze({cursor:j,current:j,rows:P});return}if(u==="add"){let k=Co(e,h[1],h[2]);if(k){d({kind:"note",tone:"error",text:k});return}d({kind:"note",tone:"ok",text:`\u2713 Profile "${h[1]}" created (environment: ${h[2]??h[1]}).`}),Ln(h[1],!1);return}if(!f?.profiles?.[u]){d({kind:"note",tone:"error",text:`Unknown profile "${u}". Create it: /profile add ${u} <environment>`});return}Ln(u,!0);return}case"doctor":await Tt(h,{cwd:e});return;case"mcp":await on(h);return;case"init":{if(oe(e)){d({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let u=nd().map(f=>({name:f,apiUrl:od(f).apiUrl}));et({cursor:0,rows:u});return}case"resume":{let u=h.join(" ").trim(),f=Xu(e).filter(M=>M.uuid!==xe.current.uuid);if(!u){if(!f.length){d({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}He({query:"",cursor:0,sessions:f});return}let k=Vu(e,u);if(!k){d({kind:"note",tone:"error",text:`No session matches "${u}".`});return}ce(k.uuid),xe.current={uuid:k.uuid,firstPrompt:k.firstPrompt,titleFired:!0},Qe(k.name),Vo(k.uuid),Ke.current=[];let P=Ho(e,k.uuid);for(let M of P)d(M);d({kind:"note",tone:"ok",text:P.length?`Resumed "${k.name}" \u2014 restored ${P.length} message(s); continue where you left off.`:`Resumed "${k.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let u=h.join(" ").trim(),f;try{f=await wt(e,s)}catch(P){d({kind:"note",tone:"error",text:`Could not list workspaces: ${P.message}`});return}if(!u){let P=f.workspaces.map(j=>({id:j.id,name:j.name??j.id,type:j.workspace_type}));if(P.length===0){d({kind:"note",tone:"info",text:"No workspaces in this project."});return}let M=Math.max(0,P.findIndex(j=>j.id===f.current));tt({cursor:M,current:M,rows:P,profile:f.profile,projectId:f.projectId,projectName:f.projectName,environment:f.environment});return}let k=Hn(f.workspaces,u);if(!k){d({kind:"note",tone:"error",text:`No workspace matches "${u}".`});return}Ht(f.profile,f.projectId,k);return}case"project":{let u=h.join(" ").trim(),f;try{f=await fn(e,s)}catch(P){d({kind:"note",tone:"error",text:`Could not list projects: ${P.message} (signed in? try /login)`});return}if(!u){let P=f.projects.map(j=>({id:j.id,name:j.name??j.id,sub:j.sub_domain}));if(P.length===0){d({kind:"note",tone:"info",text:"No projects found for this identity."});return}let M=P.findIndex(j=>j.id===f.current);nt({cursor:Math.max(0,M),current:M,rows:P,profile:f.profile,environment:f.environment});return}let k=Ls(f.projects,u);if(!k){d({kind:"note",tone:"error",text:`No project matches "${u}". Available: ${f.projects.map(P=>P.name??P.id).join(", ")}`});return}fr(f.profile,k);return}case"logout":{let u;try{u=de(e,s).name}catch(k){d({kind:"note",tone:"error",text:k.message});return}let f=Bu(u);qu(e,u),ce(),te(k=>k+1),d({kind:"note",tone:f?"ok":"info",text:f?`\u2713 Signed out of "${u}" and cleared its project/workspace selection.`:`Profile "${u}" was not signed in. Cleared any project/workspace selection.`}),d({kind:"note",tone:"info",text:"Run /login to sign in and pick a project."}),X(["/login"]);return}case"publish":{if(h[0]==="--related"&&h[1]){let f=await sn(e,s,h[1].replace(/\\/g,"/"));if(f.length===0){d({kind:"note",tone:"ok",text:"\u2713 All related articles are already published."});return}await Ft(f);return}if(h[0]&&h[0]!=="--all"){await Mn(h[0]);return}let u=h[0]==="--all";d({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let f=await Xo({cwd:e,profileName:s}),k=Ct(f.entries);if(k.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let P=f.counts["unknown-base"]??0;P>0&&d({kind:"note",tone:"info",text:`(${P} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(u){await Ft(k.map(P=>P.path));return}ot({cursor:0,rows:ns(k)})}catch(f){d({kind:"note",tone:"error",text:`Could not compute sync status: ${f.message}`}),d({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let u=h.join(" ").trim();if(!u){let k=[];try{k=Object.keys(_i(e,de(e,s).name)?.articles??{})}catch{}if(k.length===0){d({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}We({query:"",cursor:0,paths:k});return}let f=Mu(u)?u:At(e,u);if(Yo(f)){try{d({kind:"preview",name:Dt(f),text:Fi(Ai(f,"utf8"))})}catch(k){d({kind:"note",tone:"error",text:`Could not read ${f}: ${k.message}`})}return}if(wd.test(u)){try{let k=de(e,s),P={profile:k.name,connection:k.connection},M=k.project.projectId??Fu(P),j=await Hu(P,M,u);d({kind:"preview",name:j.title??u,text:j.content??"*(article has no content)*"})}catch(k){d({kind:"note",tone:"error",text:`Could not fetch article: ${k.message}`})}return}d({kind:"note",tone:"error",text:`"${u}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let u=h[0]?.trim();if(!u){let M=dn(Qo(e));Le({cursor:M,current:M});return}let{lines:f,changed:k,effective:P}=Pt(e,u);for(let M of f)d({kind:"note",tone:M.startsWith("\u26A0")?"warn":M.startsWith("\u2713")?"ok":"info",text:M});k&&(lr(M=>M+1),pe.current?.setModel(P));return}case"convert":{if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:u,run:f}=io(h),k=id(e,s);if(k.length===0){d({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let P=ao(k,u);if(P.length===0){d({kind:"note",tone:"error",text:`No tracked articles under "${u}". (${k.length} are tracked overall.)`});return}let M=Ko(P),j=3,I=`/convert${u?` --scope ${u}`:""} --run`,O=Sn(e,"light");if(!f){let D=Mi({files:lo(e,P),op:"convert",model:O.model}),W=u?`Scope: ${u} (${P.length} of ${k.length} tracked)
183
183
  `:"",ne=`
184
184
  Model: ${O.model}${O.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;d({kind:"note",tone:"info",text:W+uo(M,D,j).join(`
185
185
  `)+ne}),X([I]);return}Me.current=Date.now(),Ye(0),_(!0);let T=new AbortController;ge.current=T,K({verb:"Converting",total:M.length,done:0,active:[],rows:jt(M),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Converting ${P.length} articles across ${M.length} partitions (\u2264${j} agents at once) on ${O.model}\u2026 (esc to stop)`});try{for await(let D of Tn({cwd:e,partitions:M,promptFor:co,concurrency:j,profileName:s,allowProdWrites:Je.current,model:O.model,signal:T.signal}))if(D.type==="partition_status")D.status==="running"?K(W=>W&&{...W,active:[...W.active,D.label],rows:Pe(W.rows,D.label,"active"),lastAt:Date.now()}):K(W=>W&&{...W,active:W.active.filter(ne=>ne!==D.label),done:W.done+1,rows:Pe(W.rows,D.label,D.status==="done"?"done":"failed"),lastAt:Date.now()});else if(D.type==="partition_event")K(W=>{if(!W)return W;let ne={...W,lastAt:Date.now()};return D.event.type==="tool"?ne.tools=W.tools+1:D.event.type==="text"&&(ne.chars=W.chars+D.event.delta.length),ne});else if(D.type==="run_done"){it(U=>U+D.totalCostUsd),st(U=>U+D.results.reduce((Y,ie)=>Y+ie.outputTokens,0));let W=D.aborted?"Stopped. ":"",ne=t.kind==="api"?"api":"subscription";d({kind:"note",tone:D.aborted?"warn":D.ok?"ok":"warn",text:W+po(D.results,M,ne).join(`
186
- `)})}}catch(D){d({kind:"note",tone:"error",text:`Convert run failed: ${D.message}`})}finally{ge.current=null,K(null),_(!1),te(D=>D+1)}return}case"write":{if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let u=id(e);if(u.length===0){d({kind:"note",tone:"error",text:"No docs plan found (.d360-writer/plan.json). Analyze the repo and propose a structure first \u2014 try: analyze this repo and propose a docs structure."});return}let{scope:f,path:k,run:P}=mo(h),{docsDir:M,targets:j,reason:I}=yo(e,u,{scope:f,path:k});if(j.length===0){d({kind:"note",tone:"warn",text:I??"Nothing to write."});return}let O=Ko(j),T=5,D=Sn(e,"standard");if(!P&&!!!k){let U=Mi({files:go(e,u,j),op:"write",model:D.model}),Y=f?`Scope: ${f} (${j.length} pending)
186
+ `)})}}catch(D){d({kind:"note",tone:"error",text:`Convert run failed: ${D.message}`})}finally{ge.current=null,K(null),_(!1),te(D=>D+1)}return}case"write":{if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let u=ad(e);if(u.length===0){d({kind:"note",tone:"error",text:"No docs plan found (.d360-writer/plan.json). Analyze the repo and propose a structure first \u2014 try: analyze this repo and propose a docs structure."});return}let{scope:f,path:k,run:P}=mo(h),{docsDir:M,targets:j,reason:I}=yo(e,u,{scope:f,path:k});if(j.length===0){d({kind:"note",tone:"warn",text:I??"Nothing to write."});return}let O=Ko(j),T=5,D=Sn(e,"standard");if(!P&&!!!k){let U=Mi({files:go(e,u,j),op:"write",model:D.model}),Y=f?`Scope: ${f} (${j.length} pending)
187
187
  `:"",ie=`
188
188
  Model: ${D.model}${D.forced?" (forced)":" \u2014 authoring; /model to override"}`;d({kind:"note",tone:"info",text:Y+wo(O,U,T).join(`
189
- `)+ie}),X([`/write${f?` --scope ${f}`:" --all"} --run`]);return}Me.current=Date.now(),Ye(0),_(!0);let ne=new AbortController;ge.current=ne,K({verb:"Writing",total:O.length,done:0,active:[],rows:jt(O),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Writing ${j.length} article${j.length===1?"":"s"} across ${O.length} partition${O.length===1?"":"s"} (\u2264${T} agents at once) on ${D.model}${D.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let U of Tn({cwd:e,partitions:O,promptFor:Y=>ho(Y,M),concurrency:T,profileName:s,allowProdWrites:Je.current,model:D.model,signal:ne.signal}))if(U.type==="partition_status")U.status==="running"?K(Y=>Y&&{...Y,active:[...Y.active,U.label],rows:Pe(Y.rows,U.label,"active"),lastAt:Date.now()}):K(Y=>Y&&{...Y,active:Y.active.filter(ie=>ie!==U.label),done:Y.done+1,rows:Pe(Y.rows,U.label,U.status==="done"?"done":"failed"),lastAt:Date.now()});else if(U.type==="partition_event")K(Y=>{if(!Y)return Y;let ie={...Y,lastAt:Date.now()};return U.event.type==="tool"?ie.tools=Y.tools+1:U.event.type==="text"&&(ie.chars=Y.chars+U.event.delta.length),ie});else if(U.type==="run_done"){it(Fn=>Fn+U.totalCostUsd),st(Fn=>Fn+U.results.reduce((ta,na)=>ta+na.outputTokens,0));let Y=U.aborted?"Stopped. ":"",ie=t.kind==="api"?"api":"subscription";d({kind:"note",tone:U.aborted?"warn":U.ok?"ok":"warn",text:Y+ko(U.results,O,ie).join(`
189
+ `)+ie}),X([`/write${f?` --scope ${f}`:" --all"} --run`]);return}Me.current=Date.now(),Ye(0),_(!0);let ne=new AbortController;ge.current=ne,K({verb:"Writing",total:O.length,done:0,active:[],rows:jt(O),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Writing ${j.length} article${j.length===1?"":"s"} across ${O.length} partition${O.length===1?"":"s"} (\u2264${T} agents at once) on ${D.model}${D.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let U of Tn({cwd:e,partitions:O,promptFor:Y=>ho(Y,M),concurrency:T,profileName:s,allowProdWrites:Je.current,model:D.model,signal:ne.signal}))if(U.type==="partition_status")U.status==="running"?K(Y=>Y&&{...Y,active:[...Y.active,U.label],rows:Pe(Y.rows,U.label,"active"),lastAt:Date.now()}):K(Y=>Y&&{...Y,active:Y.active.filter(ie=>ie!==U.label),done:Y.done+1,rows:Pe(Y.rows,U.label,U.status==="done"?"done":"failed"),lastAt:Date.now()});else if(U.type==="partition_event")K(Y=>{if(!Y)return Y;let ie={...Y,lastAt:Date.now()};return U.event.type==="tool"?ie.tools=Y.tools+1:U.event.type==="text"&&(ie.chars=Y.chars+U.event.delta.length),ie});else if(U.type==="run_done"){it(Fn=>Fn+U.totalCostUsd),st(Fn=>Fn+U.results.reduce((na,oa)=>na+oa.outputTokens,0));let Y=U.aborted?"Stopped. ":"",ie=t.kind==="api"?"api":"subscription";d({kind:"note",tone:U.aborted?"warn":U.ok?"ok":"warn",text:Y+ko(U.results,O,ie).join(`
190
190
  `)}),!U.aborted&&U.ok&&X(["/publish --all","/screenshot --all"])}}catch(U){d({kind:"note",tone:"error",text:`Write run failed: ${U.message}`})}finally{ge.current=null,K(null),_(!1),te(U=>U+1)}return}case"draft":{if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{features:u,looseDocs:f}=$o(e);if(u.length===0&&f===0){d({kind:"note",tone:"info",text:bo});return}let k=h.join(" ").trim();if(!k&&u.length>1){d({kind:"note",tone:"info",text:`Multiple features in .d360-writer/input/ \u2014 draft which one?
191
191
  ${u.map(j=>` \u2022 ${j.name} (${j.docCount} doc${j.docCount===1?"":"s"})`).join(`
192
192
  `)}`}),X(u.map(j=>`/draft ${j.name}`));return}let P="",M;if(k){let j=xo(u,k);j?P=j:M=k}else P=u[0]?.name??"";await ve(un(P,M),{display:k?`/draft ${k}`:"/draft",echoDisplay:!0});return}case"sync":{let u=(h[0]??"status").toLowerCase();try{if(u==="status"){d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let f=await Xo({cwd:e,profileName:s});d({kind:"note",tone:"info",text:cn(f).join(`
193
- `)});return}if(u==="pull"){let f=h[1];if(!f){d({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let k;if(f==="--all"){if(d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),k=(await Xo({cwd:e,profileName:s})).entries.filter(j=>j.status==="remote-ahead"&&j.path).map(j=>j.path),k.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is remote-ahead \u2014 no pulls needed. (Conflicts are never bulk-pulled; pull them one by one.)"});return}}else k=[f.replace(/\\/g,"/")];let P=[];for(let M of k)P.push(await Zu({cwd:e,profileName:s,relPath:M}));Se.current=P,qt();return}d({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${u} \u2014 use /sync or /sync pull <path>|--all.`})}catch(f){d({kind:"note",tone:"error",text:`Sync failed: ${f.message}`})}return}case"scope":{if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let u=ed(e);if(u.length===0){d({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}rt({cursor:0,rows:u.map(f=>({...f,checked:f.recommended}))});return}case"audit":case"capture-setup":{let f=await(a==="audit"?an:St)(h,void 0);if(f.kind==="forward-to-agent"&&f.prompt&&await ve(f.prompt,{display:f.display,echoDisplay:!0}),a==="capture-setup"){let k=Ao(e);k&&d({kind:"note",tone:"info",text:k.join(`
194
- `)})}return}case"devhints":{if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}Oi(ad(e)),Oi(ld(e)),Yo(Li(e))||Ei(Li(e),Io()),Ei(cd(e),Eo()),d({kind:"note",tone:"info",text:No().join(`
193
+ `)});return}if(u==="pull"){let f=h[1];if(!f){d({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let k;if(f==="--all"){if(d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),k=(await Xo({cwd:e,profileName:s})).entries.filter(j=>j.status==="remote-ahead"&&j.path).map(j=>j.path),k.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is remote-ahead \u2014 no pulls needed. (Conflicts are never bulk-pulled; pull them one by one.)"});return}}else k=[f.replace(/\\/g,"/")];let P=[];for(let M of k)P.push(await ed({cwd:e,profileName:s,relPath:M}));Se.current=P,qt();return}d({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${u} \u2014 use /sync or /sync pull <path>|--all.`})}catch(f){d({kind:"note",tone:"error",text:`Sync failed: ${f.message}`})}return}case"scope":{if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let u=td(e);if(u.length===0){d({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}rt({cursor:0,rows:u.map(f=>({...f,checked:f.recommended}))});return}case"audit":case"capture-setup":{let f=await(a==="audit"?an:St)(h,void 0);if(f.kind==="forward-to-agent"&&f.prompt&&await ve(f.prompt,{display:f.display,echoDisplay:!0}),a==="capture-setup"){let k=Ao(e);k&&d({kind:"note",tone:"info",text:k.join(`
194
+ `)})}return}case"devhints":{if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}Oi(ld(e)),Oi(cd(e)),Yo(Li(e))||Ei(Li(e),Io()),Ei(ud(e),Eo()),d({kind:"note",tone:"info",text:No().join(`
195
195
  `)});return}case"screenshot":{let u=Ro(h);if(u.mode==="list"){if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}d({kind:"note",tone:"info",text:Do(jo(e,u.scope),u.scope).join(`
196
- `)});return}if(u.mode==="single"){let T=await mn(h);T.kind==="forward-to-agent"&&T.prompt&&await ve(T.prompt,{display:T.display,echoDisplay:!0});return}if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let f=u.mode==="scope"?u.scope:void 0,k=rd(e,{scope:f}).map(T=>T.id);if(k.length===0){d({kind:"note",tone:"info",text:f?`No screenshot placeholders under ${f}.`:"No screenshot placeholders found in the docs."});return}let P=6,M=od(k,3),j=Sn(e,"standard");Me.current=Date.now(),Ye(0),_(!0);let I=new AbortController;ge.current=I,K({verb:"Authoring",unit:"spec",total:M.length,done:0,active:[],rows:jt(M),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Authoring ${k.length} screenshot spec${k.length===1?"":"s"} across ${M.length} partition${M.length===1?"":"s"} (\u2264${P} agents at once) on ${j.model}${j.forced?" (forced)":""}\u2026 (esc to stop)`});let O=!1;try{for await(let T of Tn({cwd:e,partitions:M,promptFor:zs,concurrency:P,profileName:s,allowProdWrites:Je.current,model:j.model,signal:I.signal}))if(T.type==="partition_status")T.status==="running"?K(D=>D&&{...D,active:[...D.active,T.label],rows:Pe(D.rows,T.label,"active"),lastAt:Date.now()}):K(D=>D&&{...D,active:D.active.filter(W=>W!==T.label),done:D.done+1,rows:Pe(D.rows,T.label,T.status==="done"?"done":"failed"),lastAt:Date.now()});else if(T.type==="partition_event")K(D=>{if(!D)return D;let W={...D,lastAt:Date.now()};return T.event.type==="tool"?W.tools=D.tools+1:T.event.type==="text"&&(W.chars=D.chars+T.event.delta.length),W});else if(T.type==="run_done"){it(U=>U+T.totalCostUsd),st(U=>U+T.results.reduce((Y,ie)=>Y+ie.outputTokens,0));let D=T.results.filter(U=>U.ok).length,W=T.aborted?"Stopped. ":"";O=!T.aborted&&u.setup;let ne=T.aborted?"":O?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";d({kind:"note",tone:T.aborted?"warn":T.ok?"ok":"warn",text:`${W}Authored specs \u2014 ${D}/${T.results.length} batches ok.${ne}`})}}catch(T){d({kind:"note",tone:"error",text:`Spec authoring failed: ${T.message}`})}finally{ge.current=null,K(null),_(!1),te(T=>T+1)}if(O){let T=await St();T.kind==="forward-to-agent"&&T.prompt&&await ve(T.prompt,{display:T.display,echoDisplay:!0});let D=Ao(e);D&&d({kind:"note",tone:"info",text:D.join(`
196
+ `)});return}if(u.mode==="single"){let T=await mn(h);T.kind==="forward-to-agent"&&T.prompt&&await ve(T.prompt,{display:T.display,echoDisplay:!0});return}if(!oe(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let f=u.mode==="scope"?u.scope:void 0,k=sd(e,{scope:f}).map(T=>T.id);if(k.length===0){d({kind:"note",tone:"info",text:f?`No screenshot placeholders under ${f}.`:"No screenshot placeholders found in the docs."});return}let P=6,M=rd(k,3),j=Sn(e,"standard");Me.current=Date.now(),Ye(0),_(!0);let I=new AbortController;ge.current=I,K({verb:"Authoring",unit:"spec",total:M.length,done:0,active:[],rows:jt(M),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Authoring ${k.length} screenshot spec${k.length===1?"":"s"} across ${M.length} partition${M.length===1?"":"s"} (\u2264${P} agents at once) on ${j.model}${j.forced?" (forced)":""}\u2026 (esc to stop)`});let O=!1;try{for await(let T of Tn({cwd:e,partitions:M,promptFor:zs,concurrency:P,profileName:s,allowProdWrites:Je.current,model:j.model,signal:I.signal}))if(T.type==="partition_status")T.status==="running"?K(D=>D&&{...D,active:[...D.active,T.label],rows:Pe(D.rows,T.label,"active"),lastAt:Date.now()}):K(D=>D&&{...D,active:D.active.filter(W=>W!==T.label),done:D.done+1,rows:Pe(D.rows,T.label,T.status==="done"?"done":"failed"),lastAt:Date.now()});else if(T.type==="partition_event")K(D=>{if(!D)return D;let W={...D,lastAt:Date.now()};return T.event.type==="tool"?W.tools=D.tools+1:T.event.type==="text"&&(W.chars=D.chars+T.event.delta.length),W});else if(T.type==="run_done"){it(U=>U+T.totalCostUsd),st(U=>U+T.results.reduce((Y,ie)=>Y+ie.outputTokens,0));let D=T.results.filter(U=>U.ok).length,W=T.aborted?"Stopped. ":"";O=!T.aborted&&u.setup;let ne=T.aborted?"":O?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";d({kind:"note",tone:T.aborted?"warn":T.ok?"ok":"warn",text:`${W}Authored specs \u2014 ${D}/${T.results.length} batches ok.${ne}`})}}catch(T){d({kind:"note",tone:"error",text:`Spec authoring failed: ${T.message}`})}finally{ge.current=null,K(null),_(!1),te(T=>T+1)}if(O){let T=await St();T.kind==="forward-to-agent"&&T.prompt&&await ve(T.prompt,{display:T.display,echoDisplay:!0});let D=Ao(e);D&&d({kind:"note",tone:"info",text:D.join(`
197
197
  `)})}return}case"reset":{let u=Mo(e);if(u.length===0){d({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}An.current={repoName:Dt(e),targets:u},d({kind:"note",tone:"warn",text:Lo(e,u).join(`
198
- `)}),re(Dt(e));return}default:d({kind:"note",tone:"error",text:`Unknown command: /${a} \u2014 type /help.`})}},[e,r,s,qt,d,ce,ve,Ft,Mn,Un]),Gt=V(l=>{let i=(l??R).trim();if(y(""),It(null),Ve(0),i.startsWith("/")&&(re(null),ze.current++,_t(null),X(h=>xi(h,i))),!i||gr(i)||mr(i))return;Dn.current.push(i);let a=Pi(i,tr.current);i.startsWith("/")?hr(a):ve(a,{display:i})},[R,gr,mr,hr,ve]),wr=V(l=>{if(l.length>1){if(l.includes("\x1B"))return;let i=bi(l);if(vi(i)){let a=Ci(++Ki.current,i);tr.current.set(a,i),b(a)}else b(i);return}b(l)},[b]),at=Math.max(10,Be-6),kr=zo(()=>Cn(w.text,at),[w.text,at]),Wn=V(l=>$(i=>({...i,pos:Math.max(0,Math.min(i.text.length,i.pos+l))})),[]),zt=V(l=>$(i=>({...i,pos:Si(Cn(i.text,at),i.pos,l)})),[at]),gt=V(l=>$(i=>({...i,pos:Ri(Cn(i.text,at),i.pos,l)})),[at]),Zi=["\x1B[H","\x1B[1~","\x1BOH"],ea=["\x1B[F","\x1B[4~","\x1BOF"],yr=V((l,i)=>i.leftArrow?(Wn(-1),!0):i.rightArrow?(Wn(1),!0):l&&Zi.includes(l)?(gt("start"),!0):l&&ea.includes(l)?(gt("end"),!0):i.ctrl&&l==="a"?(gt("start"),!0):i.ctrl&&l==="e"?(gt("end"),!0):!1,[Wn,gt]);return Iu((l,i)=>{if(i.ctrl&&l==="c"){pe.current?.close(),r();return}if(!H){if(N){if(i.escape){if(ge.current){ge.current.signal.aborted||(d({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),ge.current.abort());return}Mt.current||(Mt.current=!0,d({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),pe.current?.interrupt());return}if(i.return){let a=R.trim();if(!a)return;Fe.current.push(a),En([...Fe.current]),y("");return}if(yr(l,i))return;if(i.upArrow){zt(-1);return}if(i.downArrow){zt(1);return}if(i.backspace||i.delete){v();return}l&&!i.ctrl&&!i.meta&&wr(l);return}if(Oe){if(i.upArrow){Le(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){Le(a=>a&&{...a,cursor:Math.min(ye.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=ye.length){Le(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=ye[Oe.cursor];Le(null);let{lines:h,changed:u,effective:f}=Pt(e,a.value??"default");for(let k of h)d({kind:"note",tone:k.startsWith("\u26A0")?"warn":k.startsWith("\u2713")?"ok":"info",text:k});u&&(lr(k=>k+1),pe.current?.setModel(f));return}if(l==="s"){let a=ye[Oe.cursor];Le(null),pe.current?.setModel(a.value??void 0),d({kind:"note",tone:"ok",text:`\u2713 Using ${a.label} for this session only (your saved default is unchanged).`});return}if(i.escape){Le(null);return}return}if(be){if(i.upArrow){Ze(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){Ze(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=be.rows.length){Ze(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return||l==="s"){let a=be.rows[be.cursor];Ze(null),Ln(a.name,i.return===!0);return}if(i.escape){Ze(null);return}return}if(Re){if(i.upArrow){et(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){et(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=Re.rows.length){et(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=Re.rows[Re.cursor].name;if(et(null),!Hr(e,a).created){d({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}d({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${a}").`});let u=(()=>{try{let f=Ge(a);return f&&!(Ne(f)&&!f.refreshToken)?f:null}catch{return null}})();u?(Jt(u,a,f=>d({kind:"note",tone:"info",text:f})),d({kind:"note",tone:"info",text:`Already signed in to ${a}.`}),Un(a)):(d({kind:"note",tone:"info",text:`Next: sign in to Document360 (${a}).`}),X(["/login"])),te(f=>f+1);return}if(i.escape){et(null);return}return}if(se){if(i.upArrow){tt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){tt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=se.rows.length){tt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=se.rows[se.cursor],{profile:h,projectId:u}=se;tt(null),Ht(h,u,a);return}if(i.escape){tt(null);return}return}if(fe){if(i.upArrow){nt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){nt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=fe.rows.length){nt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=fe.rows[fe.cursor],{profile:h}=fe;nt(null),fr(h,a);return}if(i.escape){nt(null);return}return}if(le){if(i.upArrow){rt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){rt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l===" "){rt(a=>a&&{...a,rows:a.rows.map((h,u)=>u===a.cursor?{...h,checked:!h.checked}:h)});return}if(i.return){let a=le.rows.filter(h=>h.checked).map(h=>h.path);if(rt(null),a.length===0){d({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}oo(e,a),d({kind:"note",tone:"ok",text:`\u2713 Scoped to ${a.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of a)d({kind:"note",tone:"info",text:` ${h}`});d({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(i.escape){rt(null);return}return}if(Ue){if(i.upArrow){ot(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){ot(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=Ue.rows.length){ot(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=Ue.rows[Ue.cursor];ot(null),a.kind==="article"?Mn(a.paths[0]):Ft(a.paths);return}if(i.escape){ot(null);return}return}if(me){if(i.escape){We(null);return}if(i.upArrow){We(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){We(a=>a&&{...a,cursor:Math.min(Math.max(0,Ut.length-1),a.cursor+1)});return}if(i.return){let a=Ut[me.cursor];if(a){We(null);try{d({kind:"preview",name:Dt(a),text:Fi(Ai(At(e,a),"utf8"))})}catch(h){d({kind:"note",tone:"error",text:`Could not read ${a}: ${h.message}`})}}return}if(i.backspace||i.delete){We(a=>a&&{...a,query:a.query.slice(0,-1),cursor:0});return}if(l&&!i.ctrl&&!i.meta&&l.length===1){We(a=>a&&{...a,query:a.query+l,cursor:0});return}return}if(he){if(i.escape){He(null);return}if(i.upArrow){He(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){He(a=>a&&{...a,cursor:Math.min(Math.max(0,Wt.length-1),a.cursor+1)});return}if(i.return){let a=Wt[he.cursor];if(a){He(null),ce(a.uuid),xe.current={uuid:a.uuid,firstPrompt:a.firstPrompt,titleFired:!0},Qe(a.name),Vo(a.uuid),Ke.current=[];let h=Ho(e,a.uuid);for(let u of h)d(u);d({kind:"note",tone:"ok",text:h.length?`Resumed "${a.name}" \u2014 restored ${h.length} message(s); continue where you left off.`:`Resumed "${a.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(i.backspace||i.delete){He(a=>a&&{...a,query:a.query.slice(0,-1),cursor:0});return}if(l&&!i.ctrl&&!i.meta&&l.length===1){He(a=>a&&{...a,query:a.query+l,cursor:0});return}return}if(i.tab&&!R&&we){y(we),re(null),ze.current++;return}if(!R&&pt.length>0&&l&&/^[1-9]$/.test(l)){let a=pt[Number(l)-1];if(a){y(a);return}}if(!yr(l,i)){if(dr){if(i.upArrow){Ve(a=>Math.max(0,a-1));return}if(i.downArrow){Ve(a=>Math.min(mt.length-1,a+1));return}if(i.tab){y("/"+(mt[ft]?.name??"")+" "),Ve(0);return}if(i.return){let a=mt[ft];if(a){let h=R.trim().slice(1).split(/\s+/).slice(1).join(" ");if(Nr(a.usage)&&!h){y("/"+a.name+" "),Ve(0);return}Gt("/"+a.name+(h?" "+h:""));return}}}else{if(i.upArrow){if(R!==""&&Xe===null){zt(-1);return}let a=Dn.current;if(!a.length)return;let h=Xe===null?a.length-1:Math.max(0,Xe-1);It(h),y(a[h]??"");return}if(i.downArrow){if(R!==""&&Xe===null){zt(1);return}let a=Dn.current;if(Xe===null)return;let h=Xe+1;h>=a.length?(It(null),y("")):(It(h),y(a[h]??""));return}}if(i.return){Gt();return}if(i.backspace||i.delete){v();return}if(i.escape){y(""),Ve(0),re(null),X([]),_t(null);return}l&&!i.ctrl&&!i.meta&&wr(l)}}}),_e(()=>{if(N||H)return;let l=Fe.current.shift();l!==void 0&&(En([...Fe.current]),Gt(l))},[N,H,Gt]),F(J,{flexDirection:"column",width:Be,children:[_n!==null&&F(J,{marginTop:1,flexDirection:"column",children:[_n.truncated&&S(C,{dimColor:!0,children:"\u2026"}),S(C,{children:_n.text})]}),N&&(sr?S(xd,{p:sr}):S(yd,{startTime:Me.current,chars:Xi})),S(J,{borderStyle:"round",borderColor:ur.prod?"yellow":G,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:R?kr.map((l,i)=>{let a=w.text.slice(l.start,l.end),h=i===Pn(kr,w.pos),u=Math.min(w.pos,l.end)-l.start;return F(C,{children:[S(C,{color:G,children:i===0?"> ":" "}),h?F(Jo,{children:[a.slice(0,u),S(Rn,{ch:a[u]??" "}),a.slice(u+1)]}):a||" "]},`${i}-${l.start}`)}):F(C,{children:[S(C,{color:G,children:"> "}),we&&!N?F(Jo,{children:[S(Rn,{ch:we[0],dim:!0}),S(C,{color:"gray",children:we.slice(1)}),S(C,{dimColor:!0,children:" (tab)"})]}):Nn.isSetup||!Vi?F(Jo,{children:[S(Rn,{ch:Nn.text[0],dim:!0}),S(C,{color:"gray",children:Nn.text.slice(1)})]}):S(Rn,{ch:" "})]})}),rr.length>0&&S(J,{flexDirection:"column",paddingX:1,children:rr.map((l,i)=>S(C,{color:"gray",children:`\u29D7 queued: ${l}`},`${i}-${l.slice(0,24)}`))}),Oe?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Select model"}),S(C,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),ye.map((l,i)=>F(C,{color:i===Oe.cursor?G:void 0,children:[i===Oe.cursor?"\u276F ":" ",`${i+1}. ${l.label}${i===Oe.current?" \u2714":""}`.padEnd(16),S(C,{color:"gray",children:l.desc})]},l.label)),S(C,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):be?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Switch connection profile"}),be.rows.map((l,i)=>F(C,{color:i===be.cursor?G:void 0,children:[i===be.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===be.current?" \u2714":""}`.padEnd(20),S(C,{color:"gray",children:`${l.env} \xB7 ${l.who??"not signed in"}`}),l.prod?S(C,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},l.name)),S(C,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):Re?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Pick your Document360 environment"}),Re.rows.map((l,i)=>F(C,{color:i===Re.cursor?G:void 0,children:[i===Re.cursor?"\u276F ":" ",`${i+1}. ${l.name}`.padEnd(16),S(C,{color:"gray",children:l.apiUrl})]},l.name)),S(C,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):se?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Switch workspace"}),S(C,{dimColor:!0,children:`environment ${se.environment} \xB7 project ${se.projectName??`${se.projectId.slice(0,8)}\u2026`}`}),se.rows.map((l,i)=>F(C,{color:i===se.cursor?G:void 0,children:[i===se.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===se.current?" \u2714":""}`.padEnd(30),S(C,{color:"gray",children:l.type??""})]},l.id)),S(C,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):fe?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Choose project"}),S(C,{dimColor:!0,children:`environment ${fe.environment}`}),fe.rows.map((l,i)=>F(C,{color:i===fe.cursor?G:void 0,children:[i===fe.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===fe.current?" \u2714":""}`.padEnd(30),S(C,{color:"gray",children:l.sub??""})]},l.id)),S(C,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):le?(()=>{let i=Math.min(Math.max(0,le.cursor-Math.floor(7)),Math.max(0,le.rows.length-14)),a=le.rows.slice(i,i+14),h=le.rows.filter(u=>u.checked).length;return F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:`Which folders back the user docs? (${h} selected of ${le.rows.length})`}),i>0?S(C,{dimColor:!0,children:` \u2191 ${i} more`}):null,a.map((u,f)=>{let k=i+f;return F(C,{color:k===le.cursor?G:void 0,children:[k===le.cursor?"\u276F ":" ",u.checked?"\u25C9 ":"\u25CB ",u.path.padEnd(Math.min(48,Be-34)),S(C,{color:"gray",children:ln(u)})]},u.path)}),i+14<le.rows.length?S(C,{dimColor:!0,children:` \u2193 ${le.rows.length-i-14} more`}):null,S(C,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():Ue?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Publish to Document360 \u2014 pick a category (whole) or an article:"}),Ue.rows.map((l,i)=>{let a=i===Ue.cursor,h=l.indent?" ":" ",u=`${i+1}.`.padEnd(3);return F(C,{color:a?G:void 0,bold:l.kind==="category"&&!a,children:[a?"\u276F ":" ",`${h}${u} ${l.label}`.padEnd(Math.min(56,Be-24)),l.note?S(C,{color:"gray",children:l.note}):null]},`${l.kind}:${l.paths.join(",")}`)}),S(C,{dimColor:!0,children:"\u2191\u2193 move \xB7 1-9 jump \xB7 enter publish (draft) \xB7 esc cancel"})]}):me?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:`Preview article${me.query?` \u2014 filter: ${me.query}`:" (type to filter)"}`}),Ut.length===0?S(C,{color:"gray",children:"no articles match"}):Ut.map((l,i)=>F(C,{color:i===me.cursor?G:void 0,children:[i===me.cursor?"\u276F ":" ",l]},l)),S(C,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):he?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:`Resume session${he.query?` \u2014 filter: ${he.query}`:" (type to filter)"}`}),Wt.length===0?S(C,{color:"gray",children:"no sessions match"}):Wt.map((l,i)=>F(C,{color:i===he.cursor?G:void 0,children:[i===he.cursor?"\u276F ":" ",l.name.slice(0,28).padEnd(30),S(C,{color:i===he.cursor?G:"gray",children:l.firstPrompt.slice(0,Math.max(10,Be-40))})]},l.uuid)),S(C,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):dr?S(J,{flexDirection:"column",children:mt.map((l,i)=>F(C,{color:i===ft?G:void 0,children:[i===ft?"\u276F ":" ",l.usage.padEnd(22)," ",S(C,{color:i===ft?G:"gray",children:l.name==="model"&&In?`${l.desc} (currently ${In})`:l.desc})]},l.name))}):!R&&pt.length>0?F(J,{flexDirection:"column",paddingX:1,children:[or?S(C,{color:G,children:or}):null,pt.map((l,i)=>F(C,{children:[S(C,{color:G,children:i+1})," ",l.slice(0,Math.max(20,Be-5))]},l)),S(C,{dimColor:!0,children:`press 1-${pt.length} to fill the command \xB7 esc dismiss`})]}):S(J,{paddingX:1,children:F(C,{color:"gray",children:[ur.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${In??"model n/a"}${t.kind==="api"?ar>0?` \xB7 ${ae(ar)}`:"":ir>0?` \xB7 ${Ee(ir)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}q();import{jsx as Cd}from"react/jsx-runtime";async function Gi(e=process.cwd(),t="auto",n,o="0.0.0"){let r=vd(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 ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=bd(Cd(qi,{cwd:e,auth:r,profileName:n,version:o}));await s(),process.stdout.write(`
199
- `),process.exit(0)}var Rd=Td(import.meta.url),zi=Rd("../package.json"),Te=new Pd;function Zo(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}Te.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=>{Zo(e),await Kt({profile:e.profile,manual:e.manual})});Te.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Zo(e),await jr({profile:e.profile})});Te.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Zo(e),await Rr({profile:e.profile})});var er=Te.command("profile").description("Manage connection profiles for the current repo");er.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Qt(process.cwd()));er.command("use <name>").description("Set the default profile for this repo").action(e=>Zt(process.cwd(),e));er.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>en(process.cwd(),e));var Yi=Te.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");Yi.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>lt(process.cwd(),e.profile));Yi.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await vr(process.cwd(),e,t.profile)});Te.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Ar());Te.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(()=>(pn(),_s)),n=await t(process.cwd());for(let o of e(n))console.log(o);process.exitCode=n.some(o=>o.level==="fail")?1:0});Te.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(zi.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(Sd.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 Er(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 fi(e.cwd,e.auth,e.profile):await Gi(e.cwd,e.auth,e.profile,zi.version)});Te.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
198
+ `)}),re(Dt(e));return}default:d({kind:"note",tone:"error",text:`Unknown command: /${a} \u2014 type /help.`})}},[e,r,s,qt,d,ce,ve,Ft,Mn,Un]),Gt=V(l=>{let i=(l??R).trim();if(y(""),It(null),Ve(0),i.startsWith("/")&&(re(null),ze.current++,_t(null),X(h=>xi(h,i))),!i||gr(i)||mr(i))return;Dn.current.push(i);let a=Pi(i,tr.current);i.startsWith("/")?hr(a):ve(a,{display:i})},[R,gr,mr,hr,ve]),wr=V(l=>{if(l.length>1){if(l.includes("\x1B"))return;let i=bi(l);if(vi(i)){let a=Ci(++Ji.current,i);tr.current.set(a,i),b(a)}else b(i);return}b(l)},[b]),at=Math.max(10,Be-6),kr=zo(()=>Cn(w.text,at),[w.text,at]),Wn=V(l=>$(i=>({...i,pos:Math.max(0,Math.min(i.text.length,i.pos+l))})),[]),zt=V(l=>$(i=>({...i,pos:Si(Cn(i.text,at),i.pos,l)})),[at]),gt=V(l=>$(i=>({...i,pos:Ri(Cn(i.text,at),i.pos,l)})),[at]),ea=["\x1B[H","\x1B[1~","\x1BOH"],ta=["\x1B[F","\x1B[4~","\x1BOF"],yr=V((l,i)=>i.leftArrow?(Wn(-1),!0):i.rightArrow?(Wn(1),!0):l&&ea.includes(l)?(gt("start"),!0):l&&ta.includes(l)?(gt("end"),!0):i.ctrl&&l==="a"?(gt("start"),!0):i.ctrl&&l==="e"?(gt("end"),!0):!1,[Wn,gt]);return Nu((l,i)=>{if(i.ctrl&&l==="c"){pe.current?.close(),r();return}if(!H){if(N){if(i.escape){if(ge.current){ge.current.signal.aborted||(d({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),ge.current.abort());return}Mt.current||(Mt.current=!0,d({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),pe.current?.interrupt());return}if(i.return){let a=R.trim();if(!a)return;Fe.current.push(a),En([...Fe.current]),y("");return}if(yr(l,i))return;if(i.upArrow){zt(-1);return}if(i.downArrow){zt(1);return}if(i.backspace||i.delete){v();return}l&&!i.ctrl&&!i.meta&&wr(l);return}if(Oe){if(i.upArrow){Le(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){Le(a=>a&&{...a,cursor:Math.min(ye.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=ye.length){Le(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=ye[Oe.cursor];Le(null);let{lines:h,changed:u,effective:f}=Pt(e,a.value??"default");for(let k of h)d({kind:"note",tone:k.startsWith("\u26A0")?"warn":k.startsWith("\u2713")?"ok":"info",text:k});u&&(lr(k=>k+1),pe.current?.setModel(f));return}if(l==="s"){let a=ye[Oe.cursor];Le(null),pe.current?.setModel(a.value??void 0),d({kind:"note",tone:"ok",text:`\u2713 Using ${a.label} for this session only (your saved default is unchanged).`});return}if(i.escape){Le(null);return}return}if(be){if(i.upArrow){Ze(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){Ze(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=be.rows.length){Ze(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return||l==="s"){let a=be.rows[be.cursor];Ze(null),Ln(a.name,i.return===!0);return}if(i.escape){Ze(null);return}return}if(Re){if(i.upArrow){et(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){et(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=Re.rows.length){et(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=Re.rows[Re.cursor].name;if(et(null),!Hr(e,a).created){d({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}d({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${a}").`});let u=(()=>{try{let f=Ge(a);return f&&!(Ne(f)&&!f.refreshToken)?f:null}catch{return null}})();u?(Jt(u,a,f=>d({kind:"note",tone:"info",text:f})),d({kind:"note",tone:"info",text:`Already signed in to ${a}.`}),Un(a)):(d({kind:"note",tone:"info",text:`Next: sign in to Document360 (${a}).`}),X(["/login"])),te(f=>f+1);return}if(i.escape){et(null);return}return}if(se){if(i.upArrow){tt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){tt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=se.rows.length){tt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=se.rows[se.cursor],{profile:h,projectId:u}=se;tt(null),Ht(h,u,a);return}if(i.escape){tt(null);return}return}if(fe){if(i.upArrow){nt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){nt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=fe.rows.length){nt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=fe.rows[fe.cursor],{profile:h}=fe;nt(null),fr(h,a);return}if(i.escape){nt(null);return}return}if(le){if(i.upArrow){rt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){rt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l===" "){rt(a=>a&&{...a,rows:a.rows.map((h,u)=>u===a.cursor?{...h,checked:!h.checked}:h)});return}if(i.return){let a=le.rows.filter(h=>h.checked).map(h=>h.path);if(rt(null),a.length===0){d({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}oo(e,a),d({kind:"note",tone:"ok",text:`\u2713 Scoped to ${a.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of a)d({kind:"note",tone:"info",text:` ${h}`});d({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(i.escape){rt(null);return}return}if(Ue){if(i.upArrow){ot(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){ot(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=Ue.rows.length){ot(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=Ue.rows[Ue.cursor];ot(null),a.kind==="article"?Mn(a.paths[0]):Ft(a.paths);return}if(i.escape){ot(null);return}return}if(me){if(i.escape){We(null);return}if(i.upArrow){We(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){We(a=>a&&{...a,cursor:Math.min(Math.max(0,Ut.length-1),a.cursor+1)});return}if(i.return){let a=Ut[me.cursor];if(a){We(null);try{d({kind:"preview",name:Dt(a),text:Fi(Ai(At(e,a),"utf8"))})}catch(h){d({kind:"note",tone:"error",text:`Could not read ${a}: ${h.message}`})}}return}if(i.backspace||i.delete){We(a=>a&&{...a,query:a.query.slice(0,-1),cursor:0});return}if(l&&!i.ctrl&&!i.meta&&l.length===1){We(a=>a&&{...a,query:a.query+l,cursor:0});return}return}if(he){if(i.escape){He(null);return}if(i.upArrow){He(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){He(a=>a&&{...a,cursor:Math.min(Math.max(0,Wt.length-1),a.cursor+1)});return}if(i.return){let a=Wt[he.cursor];if(a){He(null),ce(a.uuid),xe.current={uuid:a.uuid,firstPrompt:a.firstPrompt,titleFired:!0},Qe(a.name),Vo(a.uuid),Ke.current=[];let h=Ho(e,a.uuid);for(let u of h)d(u);d({kind:"note",tone:"ok",text:h.length?`Resumed "${a.name}" \u2014 restored ${h.length} message(s); continue where you left off.`:`Resumed "${a.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(i.backspace||i.delete){He(a=>a&&{...a,query:a.query.slice(0,-1),cursor:0});return}if(l&&!i.ctrl&&!i.meta&&l.length===1){He(a=>a&&{...a,query:a.query+l,cursor:0});return}return}if(i.tab&&!R&&we){y(we),re(null),ze.current++;return}if(!R&&pt.length>0&&l&&/^[1-9]$/.test(l)){let a=pt[Number(l)-1];if(a){y(a);return}}if(!yr(l,i)){if(dr){if(i.upArrow){Ve(a=>Math.max(0,a-1));return}if(i.downArrow){Ve(a=>Math.min(mt.length-1,a+1));return}if(i.tab){y("/"+(mt[ft]?.name??"")+" "),Ve(0);return}if(i.return){let a=mt[ft];if(a){let h=R.trim().slice(1).split(/\s+/).slice(1).join(" ");if(Nr(a.usage)&&!h){y("/"+a.name+" "),Ve(0);return}Gt("/"+a.name+(h?" "+h:""));return}}}else{if(i.upArrow){if(R!==""&&Xe===null){zt(-1);return}let a=Dn.current;if(!a.length)return;let h=Xe===null?a.length-1:Math.max(0,Xe-1);It(h),y(a[h]??"");return}if(i.downArrow){if(R!==""&&Xe===null){zt(1);return}let a=Dn.current;if(Xe===null)return;let h=Xe+1;h>=a.length?(It(null),y("")):(It(h),y(a[h]??""));return}}if(i.return){Gt();return}if(i.backspace||i.delete){v();return}if(i.escape){y(""),Ve(0),re(null),X([]),_t(null);return}l&&!i.ctrl&&!i.meta&&wr(l)}}}),_e(()=>{if(N||H)return;let l=Fe.current.shift();l!==void 0&&(En([...Fe.current]),Gt(l))},[N,H,Gt]),F(J,{flexDirection:"column",width:Be,children:[_n!==null&&F(J,{marginTop:1,flexDirection:"column",children:[_n.truncated&&S(C,{dimColor:!0,children:"\u2026"}),S(C,{children:_n.text})]}),N&&(sr?S(xd,{p:sr}):S(yd,{startTime:Me.current,chars:Ki})),S(J,{borderStyle:"round",borderColor:ur.prod?"yellow":G,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:R?kr.map((l,i)=>{let a=w.text.slice(l.start,l.end),h=i===Pn(kr,w.pos),u=Math.min(w.pos,l.end)-l.start;return F(C,{children:[S(C,{color:G,children:i===0?"> ":" "}),h?F(Jo,{children:[a.slice(0,u),S(Rn,{ch:a[u]??" "}),a.slice(u+1)]}):a||" "]},`${i}-${l.start}`)}):F(C,{children:[S(C,{color:G,children:"> "}),we&&!N?F(Jo,{children:[S(Rn,{ch:we[0],dim:!0}),S(C,{color:"gray",children:we.slice(1)}),S(C,{dimColor:!0,children:" (tab)"})]}):Nn.isSetup||!Xi?F(Jo,{children:[S(Rn,{ch:Nn.text[0],dim:!0}),S(C,{color:"gray",children:Nn.text.slice(1)})]}):S(Rn,{ch:" "})]})}),rr.length>0&&S(J,{flexDirection:"column",paddingX:1,children:rr.map((l,i)=>S(C,{color:"gray",children:`\u29D7 queued: ${l}`},`${i}-${l.slice(0,24)}`))}),Oe?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Select model"}),S(C,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),ye.map((l,i)=>F(C,{color:i===Oe.cursor?G:void 0,children:[i===Oe.cursor?"\u276F ":" ",`${i+1}. ${l.label}${i===Oe.current?" \u2714":""}`.padEnd(16),S(C,{color:"gray",children:l.desc})]},l.label)),S(C,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):be?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Switch connection profile"}),be.rows.map((l,i)=>F(C,{color:i===be.cursor?G:void 0,children:[i===be.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===be.current?" \u2714":""}`.padEnd(20),S(C,{color:"gray",children:`${l.env} \xB7 ${l.who??"not signed in"}`}),l.prod?S(C,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},l.name)),S(C,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):Re?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Pick your Document360 environment"}),Re.rows.map((l,i)=>F(C,{color:i===Re.cursor?G:void 0,children:[i===Re.cursor?"\u276F ":" ",`${i+1}. ${l.name}`.padEnd(16),S(C,{color:"gray",children:l.apiUrl})]},l.name)),S(C,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):se?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Switch workspace"}),S(C,{dimColor:!0,children:`environment ${se.environment} \xB7 project ${se.projectName??`${se.projectId.slice(0,8)}\u2026`}`}),se.rows.map((l,i)=>F(C,{color:i===se.cursor?G:void 0,children:[i===se.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===se.current?" \u2714":""}`.padEnd(30),S(C,{color:"gray",children:l.type??""})]},l.id)),S(C,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):fe?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Choose project"}),S(C,{dimColor:!0,children:`environment ${fe.environment}`}),fe.rows.map((l,i)=>F(C,{color:i===fe.cursor?G:void 0,children:[i===fe.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===fe.current?" \u2714":""}`.padEnd(30),S(C,{color:"gray",children:l.sub??""})]},l.id)),S(C,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):le?(()=>{let i=Math.min(Math.max(0,le.cursor-Math.floor(7)),Math.max(0,le.rows.length-14)),a=le.rows.slice(i,i+14),h=le.rows.filter(u=>u.checked).length;return F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:`Which folders back the user docs? (${h} selected of ${le.rows.length})`}),i>0?S(C,{dimColor:!0,children:` \u2191 ${i} more`}):null,a.map((u,f)=>{let k=i+f;return F(C,{color:k===le.cursor?G:void 0,children:[k===le.cursor?"\u276F ":" ",u.checked?"\u25C9 ":"\u25CB ",u.path.padEnd(Math.min(48,Be-34)),S(C,{color:"gray",children:ln(u)})]},u.path)}),i+14<le.rows.length?S(C,{dimColor:!0,children:` \u2193 ${le.rows.length-i-14} more`}):null,S(C,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():Ue?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Publish to Document360 \u2014 pick a category (whole) or an article:"}),Ue.rows.map((l,i)=>{let a=i===Ue.cursor,h=l.indent?" ":" ",u=`${i+1}.`.padEnd(3);return F(C,{color:a?G:void 0,bold:l.kind==="category"&&!a,children:[a?"\u276F ":" ",`${h}${u} ${l.label}`.padEnd(Math.min(56,Be-24)),l.note?S(C,{color:"gray",children:l.note}):null]},`${l.kind}:${l.paths.join(",")}`)}),S(C,{dimColor:!0,children:"\u2191\u2193 move \xB7 1-9 jump \xB7 enter publish (draft) \xB7 esc cancel"})]}):me?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:`Preview article${me.query?` \u2014 filter: ${me.query}`:" (type to filter)"}`}),Ut.length===0?S(C,{color:"gray",children:"no articles match"}):Ut.map((l,i)=>F(C,{color:i===me.cursor?G:void 0,children:[i===me.cursor?"\u276F ":" ",l]},l)),S(C,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):he?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:`Resume session${he.query?` \u2014 filter: ${he.query}`:" (type to filter)"}`}),Wt.length===0?S(C,{color:"gray",children:"no sessions match"}):Wt.map((l,i)=>F(C,{color:i===he.cursor?G:void 0,children:[i===he.cursor?"\u276F ":" ",l.name.slice(0,28).padEnd(30),S(C,{color:i===he.cursor?G:"gray",children:l.firstPrompt.slice(0,Math.max(10,Be-40))})]},l.uuid)),S(C,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):dr?S(J,{flexDirection:"column",children:mt.map((l,i)=>F(C,{color:i===ft?G:void 0,children:[i===ft?"\u276F ":" ",l.usage.padEnd(22)," ",S(C,{color:i===ft?G:"gray",children:l.name==="model"&&In?`${l.desc} (currently ${In})`:l.desc})]},l.name))}):!R&&pt.length>0?F(J,{flexDirection:"column",paddingX:1,children:[or?S(C,{color:G,children:or}):null,pt.map((l,i)=>F(C,{children:[S(C,{color:G,children:i+1})," ",l.slice(0,Math.max(20,Be-5))]},l)),S(C,{dimColor:!0,children:`press 1-${pt.length} to fill the command \xB7 esc dismiss`})]}):S(J,{paddingX:1,children:F(C,{color:"gray",children:[ur.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${In??"model n/a"}${t.kind==="api"?ar>0?` \xB7 ${ae(ar)}`:"":ir>0?` \xB7 ${Ee(ir)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}q();import{jsx as Cd}from"react/jsx-runtime";async function zi(e=process.cwd(),t="auto",n,o="0.0.0"){let r=vd(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 ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=bd(Cd(Gi,{cwd:e,auth:r,profileName:n,version:o}));await s(),process.stdout.write(`
199
+ `),process.exit(0)}var Rd=Td(import.meta.url),Yi=Rd("../package.json"),Te=new Pd;function Zo(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}Te.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=>{Zo(e),await Kt({profile:e.profile,manual:e.manual})});Te.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Zo(e),await jr({profile:e.profile})});Te.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Zo(e),await Rr({profile:e.profile})});var er=Te.command("profile").description("Manage connection profiles for the current repo");er.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Qt(process.cwd()));er.command("use <name>").description("Set the default profile for this repo").action(e=>Zt(process.cwd(),e));er.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>en(process.cwd(),e));var Vi=Te.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");Vi.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>lt(process.cwd(),e.profile));Vi.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await vr(process.cwd(),e,t.profile)});Te.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Ar());Te.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(()=>(pn(),_s)),n=await t(process.cwd());for(let o of e(n))console.log(o);process.exitCode=n.some(o=>o.level==="fail")?1:0});Te.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(Yi.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(Sd.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 Er(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 fi(e.cwd,e.auth,e.profile):await zi(e.cwd,e.auth,e.profile,Yi.version)});Te.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document360-writer",
3
- "version": "0.4.56",
3
+ "version": "0.4.57",
4
4
  "description": "Standalone documentation agent CLI. Reads your code, writes your docs. Specialized for Document360 publishing.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -34,7 +34,7 @@
34
34
  "@inquirer/prompts": "^8.4.3",
35
35
  "commander": "^14.0.3",
36
36
  "diff": "^8.0.4",
37
- "document360-engine": "^0.2.33",
37
+ "document360-engine": "^0.2.34",
38
38
  "ink": "^5.2.1",
39
39
  "picocolors": "^1.1.1",
40
40
  "react": "^18.3.1",