document360-writer 0.4.34 → 0.4.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -1,83 +1,95 @@
1
1
  #!/usr/bin/env node
2
- var Os=Object.defineProperty;var En=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var Us=(e,t)=>{for(var o in t)Os(e,o,{get:t[o],enumerable:!0})};import Se from"picocolors";var B,Ws,Fs,Hs,Dn,st,f,x,N,D,S,V,we,H=En(()=>{"use strict";B="#7f56d9",[Ws,Fs,Hs]=[127,86,217],Dn=e=>Se.isColorSupported?`\x1B[38;2;${Ws};${Fs};${Hs}m${e}\x1B[39m`:e,st=e=>Se.bold(Dn(e)),f=e=>Se.dim(e),x=e=>Se.red(e),N=e=>Se.yellow(e),D=e=>Se.green(e),S=Dn,V=e=>Se.gray(e),we=e=>Se.bold(e)});var _r={};Us(_r,{doctorCommand:()=>ft,renderDoctorChecks:()=>Ir,runDoctorChecks:()=>Dr});import{existsSync as $l}from"node:fs";import{d360GetAll as bl,getAccessToken as vl,isExpired as Xo,loadProfileMap as Cl,loadTokens as Sl,packageSkillsDir as Er,projectConfigPath as Tl,readProjectConfig as Pl,resolveActiveProfile as Rl,resolveAuth as Al,resolveModelSetting as jl}from"document360-engine";async function Dr(e){let t=[],o=Number(process.versions.node.split(".")[0]);t.push(o>=20?{level:"ok",label:`Node ${process.versions.node}`}:{level:"fail",label:`Node ${process.versions.node} \u2014 20+ required`,fix:"Install Node 20 or later (nodejs.org)"});let n=Al("auto");t.push(n.kind==="none"?{level:"fail",label:"Claude auth: not configured",fix:"Set ANTHROPIC_API_KEY, or sign in to Claude Code once (subscription reuse)"}:{level:"ok",label:`Claude auth: ${n.kind==="api"?"API key":"subscription"}`});let r=jl(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=Pl(e);if(!s)return t.push({level:"fail",label:`No ${Tl(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/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 d=s.authoritativeSourceFiles??[];t.push(d.length>0?{level:"ok",label:`Sources: docs grounded in ${d.length} path(s) (${d.slice(0,3).join(", ")}${d.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 h;try{h=Rl(e),t.push({level:"ok",label:`Profile: ${h.name} (${h.connection.name})${h.production?" \u26A0 PRODUCTION":""}`})}catch(A){return t.push({level:"fail",label:`Profile config: ${A.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let g=Sl(h.name);g?Xo(g)&&!g.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Xo(g)?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(g.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${h.name})`}),t.push(h.project.workspaceId?{level:"ok",label:`Workspace: ${h.project.workspaceName??h.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let w=Cl(e,h.name);if(w?w.projectId&&h.project.projectId&&w.projectId!==h.project.projectId?t.push({level:"fail",label:`Category map projectId (${w.projectId}) \u2260 profile projectId (${h.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer/config.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys(w.articles).length} articles, ${Object.keys(w.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${h.name}" (created on first /publish)`}),t.push($l(Er())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Er()}`,fix:"Reinstall document360-writer (broken install)"}),g&&(!Xo(g)||g.refreshToken)){let A={profile:h.name,connection:h.connection};try{await vl(A);let $=await bl(A,"/v3/projects");t.push({level:"ok",label:`API reachable (${h.connection.apiUrl}) \u2014 ${$.length} project(s) visible`})}catch($){t.push({level:"fail",label:`API call failed: ${$.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function Ir(e){let t=[""];for(let r of e){let s=r.level==="ok"?D("\u2713"):r.level==="warn"?N("\u26A0"):x("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${S(r.fix)}`)}let o=e.filter(r=>r.level==="fail").length,n=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(o===0?D(`\u2713 ${n===0?"All checks passed":`Healthy (${n} warning${n===1?"":"s"})`}`):x(`\u2717 ${o} problem${o===1?"":"s"} found`)),t.push(""),t}async function ft(e,t){console.log(f("Running checks\u2026"));for(let o of Ir(await Dr(t.cwd)))console.log(o);return{kind:"continue"}}var Jt=En(()=>{"use strict";H()});import{Command as yc}from"commander";import{createRequire as wc}from"node:module";import{AUTH_MODES as xc}from"document360-engine";import{input as Qs}from"@inquirer/prompts";import{loginPkce as Zs,refreshTokens as ei,toStoredTokens as Nn,clearTokens as ti,decodeJwtClaims as Mn,isExpired as oi,loadTokens as ni,saveTokens as Ln,resolveActiveProfile as _t,setProfileProject as ri,readProjectConfig as si}from"document360-engine";H();import{select as Bs}from"@inquirer/prompts";import{resolveActiveProfile as qs,setProfileProject as zs,resolveProjectId as Gs,listWorkspaces as Ys}from"document360-engine";async function Dt(e,t){let o=qs(e,t),n={profile:o.name,connection:o.connection},r=o.project.projectId??Gs(n);return{workspaces:await Ys(n,r),projectId:r,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var In=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function Co(e,t){let o=t.toLowerCase();return e.find(n=>(n.name??"").toLowerCase()===o)??e.find(n=>(n.name??"").toLowerCase().startsWith(o))??e.find(n=>n.id.startsWith(t))}function It(e,t,o,n,r){zs(e,t,{projectId:o,workspaceId:n,workspaceName:r})}async function _n(e,t,o){let n;try{n=await Dt(e,o)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=Co(n.workspaces,t);return r?(It(e,n.profile,n.projectId,r.id,r.name),console.log(D(`\u2713 Workspace set to "${r.name??r.id}" for profile "${n.profile}".`)),0):(console.log(x(`No workspace matches "${t}". Available: ${n.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function Je(e,t){let o;try{o=await Dt(e,t)}catch(g){console.log(x(`Could not list workspaces: ${g.message}`));return}let{workspaces:n,projectId:r,profile:s,current:c}=o;if(n.length===0){console.log(f("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let g of n)console.log(` ${g.id===c?S("\u25CF"):" "} ${In(g)} ${f(g.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let d=await Bs({message:"Select the Document360 workspace for this repo:",choices:n.map(g=>({name:`${In(g)}${g.id===c?" (current)":""}`,value:g.id}))}),h=n.find(g=>g.id===d);It(e,s,r,d,h?.name),console.log(D(`\u2713 Workspace set to "${h?.name??d}" for profile "${s}".`))}H();import Xs from"picocolors";function Vs(e=process.env){return e.FORCE_HYPERLINK==="0"||!Xs.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 Js(e,t=e,o){return Vs(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Ks=/https?:\/\/[^\s\x1b]+/g;function it(e,t){return e.replace(Ks,o=>Js(o,o,t))}function On(e){return{...Mn(e.idToken)??{},...Mn(e.accessToken)??{}}}function Mt(e){let t=On(e),o=t.email??t.preferred_username??t.sub??"unknown",n=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${o}${n}`}async function Nt(e){let t=_t(process.cwd(),e.profile),o=t.connection;console.log(f(`Profile "${t.name}" \u2192 ${o.name} (${o.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let n=await Zs(o,{manual:e.manual,promptForRedirect:s=>Qs({message:s})},s=>console.log(it(s))),r=Nn(t.name,n);if(Ln(r),So(r,t.name,s=>console.log(f(s))),console.log(""),console.log(D(`\u2713 Logged in to "${t.name}" as ${Mt(r)}`)),console.log(f(` access token expires: ${r.expiresAt}`)),console.log(f(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{si(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Je(process.cwd(),t.name))}catch{}}function So(e,t,o){let r=On(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(_t(process.cwd(),t).project.projectId)return;ri(process.cwd(),t,{projectId:r}),o(` Project ${r} written to profile "${t}".`)}catch{}}async function Un(e){let t=_t(process.cwd(),e.profile),o=t.connection,n=ni(t.name);if(!n){console.log(x(`Not logged in to Document360 (profile "${t.name}").`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${S(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Mt(n)}`),oi(n))if(n.refreshToken)try{let r=Nn(t.name,await ei(o,n.refreshToken));Ln(r),console.log(D(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(N(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(N("Session expired (no refresh token).")),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(f(` expires: ${n.expiresAt}`))}async function Wn(e){let t=_t(process.cwd(),e.profile);ti(t.name)?console.log(D(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}H();import{readProjectConfig as Fn,writeProjectConfig as ii,resolveActiveProfile as li,loadTokens as ai,isExpired as ci}from"document360-engine";function Lt(e){let t=Fn(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(x("No profiles in .d360-writer.json. Run: d360-writer init")),process.exitCode=1;return}console.log("");for(let[o,n]of Object.entries(t.profiles)){let r=o===t.defaultProfile?S("\u25CF "):" ",s=n.production?N(" \u26A0 PRODUCTION"):"",c=n.connection.environment??"(inline)",d=ai(o),h=d?ci(d)&&!d.refreshToken?N("expired"):f("logged in"):f("not logged in");console.log(`${r}${S(o)} \u2192 ${c}${s} [${h}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Ot(e,t){let o=Fn(e);if(!o?.profiles?.[t]){let r=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log(x(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}o.defaultProfile=t,ii(o,e);let n=o.profiles[t].production?N(" \u26A0 PRODUCTION"):"";console.log(D(`\u2713 Default profile is now "${t}"${n}`))}function Ut(e,t){try{let o=li(e,t);console.log(""),console.log(`Profile ${S(o.name)}${o.production?N(" \u26A0 PRODUCTION"):""}`),console.log(f(` api: ${o.connection.apiUrl}`)),console.log(f(` identity: ${o.connection.authorizationUrl}`)),console.log(f(` clientId: ${o.connection.clientId}`)),console.log(f(` scopes: ${o.connection.scopes.join(" ")}`)),console.log(f(` project: ${o.project.projectId??"(set at login)"}`)),console.log(f(` workspace:${o.project.workspaceId?" "+o.project.workspaceId:" (none)"}`)),console.log("")}catch(o){console.log(x(o.message)),process.exitCode=1}}H();import{existsSync as ui,readdirSync as di,statSync as pi}from"node:fs";import{join as fi}from"node:path";import{apiLogDir as mi}from"document360-engine";function Hn(){let e=mi();if(console.log(""),console.log(`Document360 API logs: ${S(e)}`),!ui(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=di(e).filter(o=>o.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(f(" No logs yet \u2014 they appear after the first Document360 API call."));for(let o of t.slice(0,14)){let n=(pi(fi(e,o)).size/1024).toFixed(1);console.log(` ${o} ${f(`${n} KB`)}`)}console.log(""),console.log(f("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(f("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}H();import{createSession as gi,resolveAuth as hi,findByName as ki,slugify as yi,touchSession as wi,upsertSession as xi,resolveActiveProfile as $i}from"document360-engine";async function Bn(e,t,o,n,r,s){let c=hi(o);c.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let d=null;try{d=$i(e,r)}catch($){console.error(x(`Document360 profile error: ${$.message}`)),process.exit(2)}d.production&&(console.error(N(`\u26A0 Profile "${d.name}" is PRODUCTION.`)),s||(console.error(x("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(f(" --yes given \u2014 proceeding against production.")));let h;if(n){let $=ki(e,n);$||(console.error(x(`No saved session matches "${n}" in this repo.`)),console.error(f("List sessions inside the REPL with /resume.")),process.exit(2)),h=$.uuid,console.error(f(`Resuming "${$.name}"`))}let g=gi({cwd:e,resume:h,profileName:r,allowProdWrites:s===!0}),w=h??null,A=1;for await(let $ of g.send(t))switch($.type){case"session":if(!w){w=$.sessionId;let j=new Date().toISOString();xi({uuid:w,name:yi(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:j,updatedAt:j})}break;case"text":process.stdout.write($.delta);break;case"tool":console.error(V(` \u2699 ${$.name}`));break;case"result":A=$.ok?0:1,console.error(f(`(${$.inputTokens}\u2192${$.outputTokens} tokens`+($.costUsd>0?`, $${$.costUsd<.01?$.costUsd.toFixed(4):$.costUsd.toFixed(2)}`:"")+")")),$.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${$.message}`)),process.exit(1)}w&&wi(w),process.stdout.write(`
3
- `),process.exit(A)}import{createInterface as na}from"node:readline/promises";import{createSession as rn,resolveAuth as ra,getSession as sa,setTitle as ia,slugify as la,touchSession as Qr,upsertSession as aa,generateTitle as ca,resolveActiveProfile as Zr,resolveModelSetting as ua,readProjectConfig as da,decodeJwtClaims as Jr,isExpired as pa,loadTokens as fa}from"document360-engine";var lt=[{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json",group:"start"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser)",group:"start"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: 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:"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"}],bi=[{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 Wt(){let e=Math.max(...lt.map(o=>o.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:o,label:n}of bi){t.push("",n);for(let r of lt.filter(s=>s.group===o))t.push(` ${r.usage.padEnd(e)}${r.desc}`)}return t.push("","Tip: anything not starting with / is sent to the agent."),t}function qn(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return lt.filter(o=>o.name.startsWith(t))}function zn(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function To(){console.log("");for(let e of Wt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}H();import{getSession as vi}from"document360-engine";async function Gn(e,t){let o=t.currentUuid(),n=o?vi(o):void 0;return console.log(n?f(`
4
- (conversation reset \u2014 "${n.name}" is still available via /resume)
2
+ var ti=Object.defineProperty;var Ho=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}};var ni=(e,t)=>{for(var n in t)ti(e,n,{get:t[n],enumerable:!0})};import Ae from"picocolors";var z,oi,ri,si,Bo,pt,f,x,L,D,R,Q,ve,H=Ho(()=>{"use strict";z="#7f56d9",[oi,ri,si]=[127,86,217],Bo=e=>Ae.isColorSupported?`\x1B[38;2;${oi};${ri};${si}m${e}\x1B[39m`:e,pt=e=>Ae.bold(Bo(e)),f=e=>Ae.dim(e),x=e=>Ae.red(e),L=e=>Ae.yellow(e),D=e=>Ae.green(e),R=Bo,Q=e=>Ae.gray(e),ve=e=>Ae.bold(e)});var Vr={};ni(Vr,{doctorCommand:()=>$t,renderDoctorChecks:()=>Xr,runDoctorChecks:()=>Yr});import{existsSync as Gl}from"node:fs";import{d360GetAll as Yl,getAccessToken as Xl,isExpired as no,loadProfileMap as Vl,loadTokens as Jl,packageSkillsDir as Gr,projectConfigPath as Kl,readProjectConfig as Ql,resolveActiveProfile as Zl,resolveAuth as ea,resolveModelSetting as ta}from"document360-engine";async function Yr(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=ea("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=ta(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=Ql(e);if(!s)return t.push({level:"fail",label:`No ${Kl(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/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 u=s.authoritativeSourceFiles??[];t.push(u.length>0?{level:"ok",label:`Sources: docs grounded in ${u.length} path(s) (${u.slice(0,3).join(", ")}${u.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=Zl(e),t.push({level:"ok",label:`Profile: ${g.name} (${g.connection.name})${g.production?" \u26A0 PRODUCTION":""}`})}catch(j){return t.push({level:"fail",label:`Profile config: ${j.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let m=Jl(g.name);m?no(m)&&!m.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):no(m)?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(m.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 w=Vl(e,g.name);if(w?w.projectId&&g.project.projectId&&w.projectId!==g.project.projectId?t.push({level:"fail",label:`Category map projectId (${w.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(w.articles).length} articles, ${Object.keys(w.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${g.name}" (created on first /publish)`}),t.push(Gl(Gr())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Gr()}`,fix:"Reinstall document360-writer (broken install)"}),m&&(!no(m)||m.refreshToken)){let j={profile:g.name,connection:g.connection};try{await Xl(j);let b=await Yl(j,"/v3/projects");t.push({level:"ok",label:`API reachable (${g.connection.apiUrl}) \u2014 ${b.length} project(s) visible`})}catch(b){t.push({level:"fail",label:`API call failed: ${b.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function Xr(e){let t=[""];for(let r of e){let s=r.level==="ok"?D("\u2713"):r.level==="warn"?L("\u26A0"):x("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${R(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?D(`\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 $t(e,t){console.log(f("Running checks\u2026"));for(let n of Xr(await Yr(t.cwd)))console.log(n);return{kind:"continue"}}var Qt=Ho(()=>{"use strict";H()});import{Command as Hc}from"commander";import{createRequire as Bc}from"node:module";import{AUTH_MODES as qc}from"document360-engine";import{input as gi}from"@inquirer/prompts";import{loginPkce as hi,refreshTokens as ki,toStoredTokens as Yo,clearTokens as yi,decodeJwtClaims as Go,isExpired as wi,loadTokens as $i,saveTokens as Xo,resolveActiveProfile as Lt,setProfileProject as xi,readProjectConfig as bi}from"document360-engine";H();import{select as ii}from"@inquirer/prompts";import{resolveActiveProfile as li,setProfileProject as ai,resolveProjectId as ci,listWorkspaces as ui}from"document360-engine";async function Mt(e,t){let n=li(e,t),o={profile:n.name,connection:n.connection},r=n.project.projectId??ci(o);return{workspaces:await ui(o,r),projectId:r,profile:n.name,environment:n.connection.name,current:n.project.workspaceId}}var qo=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function Pn(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 Nt(e,t,n,o,r){ai(e,t,{projectId:n,workspaceId:o,workspaceName:r})}async function zo(e,t,n){let o;try{o=await Mt(e,n)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=Pn(o.workspaces,t);return r?(Nt(e,o.profile,o.projectId,r.id,r.name),console.log(D(`\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 Ze(e,t){let n;try{n=await Mt(e,t)}catch(m){console.log(x(`Could not list workspaces: ${m.message}`));return}let{workspaces:o,projectId:r,profile:s,current:c}=n;if(o.length===0){console.log(f("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let m of o)console.log(` ${m.id===c?R("\u25CF"):" "} ${qo(m)} ${f(m.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let u=await ii({message:"Select the Document360 workspace for this repo:",choices:o.map(m=>({name:`${qo(m)}${m.id===c?" (current)":""}`,value:m.id}))}),g=o.find(m=>m.id===u);Nt(e,s,r,u,g?.name),console.log(D(`\u2713 Workspace set to "${g?.name??u}" for profile "${s}".`))}H();import di from"picocolors";function pi(e=process.env){return e.FORCE_HYPERLINK==="0"||!di.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 fi(e,t=e,n){return pi(n)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var mi=/https?:\/\/[^\s\x1b]+/g;function ft(e,t){return e.replace(mi,n=>fi(n,n,t))}function Vo(e){return{...Go(e.idToken)??{},...Go(e.accessToken)??{}}}function Ot(e){let t=Vo(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 Ut(e){let t=Lt(process.cwd(),e.profile),n=t.connection;console.log(f(`Profile "${t.name}" \u2192 ${n.name} (${n.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let o=await hi(n,{manual:e.manual,promptForRedirect:s=>gi({message:s})},s=>console.log(ft(s))),r=Yo(t.name,o);if(Xo(r),Sn(r,t.name,s=>console.log(f(s))),console.log(""),console.log(D(`\u2713 Logged in to "${t.name}" as ${Ot(r)}`)),console.log(f(` access token expires: ${r.expiresAt}`)),console.log(f(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{bi(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Ze(process.cwd(),t.name))}catch{}}function Sn(e,t,n){let r=Vo(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(Lt(process.cwd(),t).project.projectId)return;xi(process.cwd(),t,{projectId:r}),n(` Project ${r} written to profile "${t}".`)}catch{}}async function Jo(e){let t=Lt(process.cwd(),e.profile),n=t.connection,o=$i(t.name);if(!o){console.log(x(`Not logged in to Document360 (profile "${t.name}").`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${R(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Ot(o)}`),wi(o))if(o.refreshToken)try{let r=Yo(t.name,await ki(n,o.refreshToken));Xo(r),console.log(D(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(L(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(L("Session expired (no refresh token).")),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(f(` expires: ${o.expiresAt}`))}async function Ko(e){let t=Lt(process.cwd(),e.profile);yi(t.name)?console.log(D(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}H();import{readProjectConfig as Qo,writeProjectConfig as vi,resolveActiveProfile as Ci,loadTokens as Pi,isExpired as Si}from"document360-engine";function Wt(e){let t=Qo(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?R("\u25CF "):" ",s=o.production?L(" \u26A0 PRODUCTION"):"",c=o.connection.environment??"(inline)",u=Pi(n),g=u?Si(u)&&!u.refreshToken?L("expired"):f("logged in"):f("not logged in");console.log(`${r}${R(n)} \u2192 ${c}${s} [${g}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Ft(e,t){let n=Qo(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,vi(n,e);let o=n.profiles[t].production?L(" \u26A0 PRODUCTION"):"";console.log(D(`\u2713 Default profile is now "${t}"${o}`))}function Ht(e,t){try{let n=Ci(e,t);console.log(""),console.log(`Profile ${R(n.name)}${n.production?L(" \u26A0 PRODUCTION"):""}`),console.log(f(` api: ${n.connection.apiUrl}`)),console.log(f(` identity: ${n.connection.authorizationUrl}`)),console.log(f(` clientId: ${n.connection.clientId}`)),console.log(f(` scopes: ${n.connection.scopes.join(" ")}`)),console.log(f(` project: ${n.project.projectId??"(set at login)"}`)),console.log(f(` workspace:${n.project.workspaceId?" "+n.project.workspaceId:" (none)"}`)),console.log("")}catch(n){console.log(x(n.message)),process.exitCode=1}}H();import{existsSync as Ti,readdirSync as Ri,statSync as Ai}from"node:fs";import{join as ji}from"node:path";import{apiLogDir as Ei}from"document360-engine";function Zo(){let e=Ei();if(console.log(""),console.log(`Document360 API logs: ${R(e)}`),!Ti(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=Ri(e).filter(n=>n.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(f(" No logs yet \u2014 they appear after the first Document360 API call."));for(let n of t.slice(0,14)){let o=(Ai(ji(e,n)).size/1024).toFixed(1);console.log(` ${n} ${f(`${o} KB`)}`)}console.log(""),console.log(f("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(f("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}H();import{createSession as Di,resolveAuth as Ii,findByName as _i,slugify as Mi,touchSession as Ni,upsertSession as Li,resolveActiveProfile as Oi}from"document360-engine";async function er(e,t,n,o,r,s){let c=Ii(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 ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let u=null;try{u=Oi(e,r)}catch(b){console.error(x(`Document360 profile error: ${b.message}`)),process.exit(2)}u.production&&(console.error(L(`\u26A0 Profile "${u.name}" is PRODUCTION.`)),s||(console.error(x("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(f(" --yes given \u2014 proceeding against production.")));let g;if(o){let b=_i(e,o);b||(console.error(x(`No saved session matches "${o}" in this repo.`)),console.error(f("List sessions inside the REPL with /resume.")),process.exit(2)),g=b.uuid,console.error(f(`Resuming "${b.name}"`))}let m=Di({cwd:e,resume:g,profileName:r,allowProdWrites:s===!0}),w=g??null,j=1;for await(let b of m.send(t))switch(b.type){case"session":if(!w){w=b.sessionId;let v=new Date().toISOString();Li({uuid:w,name:Mi(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:v,updatedAt:v})}break;case"text":process.stdout.write(b.delta);break;case"tool":console.error(Q(` \u2699 ${b.name}`));break;case"result":j=b.ok?0:1,console.error(f(`(${b.inputTokens}\u2192${b.outputTokens} tokens`+(b.costUsd>0?`, $${b.costUsd<.01?b.costUsd.toFixed(4):b.costUsd.toFixed(2)}`:"")+")")),b.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${b.message}`)),process.exit(1)}w&&Ni(w),process.stdout.write(`
3
+ `),process.exit(j)}import{createInterface as Ra}from"node:readline/promises";import{createSession as fo,resolveAuth as Aa,getSession as ja,setTitle as Ea,slugify as Da,touchSession as fs,upsertSession as Ia,generateTitle as _a,resolveActiveProfile as ms,resolveModelSetting as Ma,readProjectConfig as Na,decodeJwtClaims as ds,isExpired as La,loadTokens as Oa}from"document360-engine";var mt=[{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json",group:"start"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser)",group:"start"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"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:"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"}],Ui=[{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 Bt(){let e=Math.max(...mt.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 Ui){t.push("",o);for(let r of mt.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 tr(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return mt.filter(n=>n.name.startsWith(t))}function nr(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function Tn(){console.log("");for(let e of Bt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}H();import{getSession as Wi}from"document360-engine";async function or(e,t){let n=t.currentUuid(),o=n?Wi(n):void 0;return console.log(o?f(`
4
+ (conversation reset \u2014 "${o.name}" is still available via /resume)
5
5
  `):f(`
6
6
  (conversation reset \u2014 agent will start fresh on the next prompt)
7
- `)),{kind:"clear"}}async function Po(){return{kind:"exit"}}H();import{input as at,confirm as Ci}from"@inquirer/prompts";import{basename as Yn,join as Ft}from"node:path";import{existsSync as Ke,readFileSync as Si,readdirSync as Ti,writeFileSync as Pi}from"node:fs";import{writeProjectConfig as Xn,readProjectConfig as Ri,projectConfigPath as Vn,writerDir as Ai}from"document360-engine";function Jn(e,t="berlin"){let o=Vn(e);if(Ke(o))return{created:!1,path:o,profileName:""};let n={projectId:Zn(e)??Yn(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:er(e)};return Xn(n,e),Kn(e),{created:!0,path:o,profileName:t}}function Kn(e){let t=Ft(Ai(e),".gitignore");Ke(t)||Pi(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
8
- `),"utf8")}async function Qn(){let e=process.cwd(),t=Vn(e);if(Ke(t)&&!await Ci({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let o=Zn(e)??Yn(e),n=await at({message:"Project ID (used to scope sessions, screenshots, etc.):",default:o}),r=await at({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await at({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await at({message:"Default connection profile name:",default:"berlin"}),d=await at({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),h={projectId:n,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:d},production:!1}},defaultProfile:c,authoritativeSourceFiles:er(e)},g=Ri(e);return g?.terminologyGlossary&&(h.terminologyGlossary=g.terminologyGlossary),Xn(h,e),Kn(e),console.log(""),console.log(D(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${S(`d360-writer login --profile ${c}`)} ${f("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function Zn(e){let t=Ft(e,"package.json");if(!Ke(t))return null;try{return JSON.parse(Si(t,"utf8")).name??null}catch{return null}}function er(e){let t=[];for(let o of["README.md","ARCHITECTURE.md","CLAUDE.md"])Ke(Ft(e,o))&&t.push(o);for(let o of["src","api","docs"]){let n=Ft(e,o);Ke(n)&&!ji(n)&&t.push(o)}return t}function ji(e){try{return Ti(e,{withFileTypes:!0}).filter(o=>o.isDirectory()&&!o.name.startsWith(".")).length>6}catch{return!1}}H();import{readMcpConfig as Ro,writeMcpConfig as tr}from"document360-engine";async function Ht(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Ei(),{kind:"continue"}):t==="add"?(Di(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Ii(e.slice(1)),{kind:"continue"}):(console.log(x(`Unknown /mcp subcommand: ${t}`)),console.log(f("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function Ei(){let e=Ro(),t=Object.keys(e.servers);if(t.length===0){console.log(f(`
7
+ `)),{kind:"clear"}}async function Rn(){return{kind:"exit"}}H();import{input as gt,confirm as Fi}from"@inquirer/prompts";import{basename as rr,join as qt}from"node:path";import{existsSync as et,readFileSync as Hi,readdirSync as Bi,writeFileSync as qi}from"node:fs";import{writeProjectConfig as sr,readProjectConfig as zi,projectConfigPath as ir,writerDir as Gi}from"document360-engine";function lr(e,t="berlin"){let n=ir(e);if(et(n))return{created:!1,path:n,profileName:""};let o={projectId:ur(e)??rr(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:dr(e)};return sr(o,e),ar(e),{created:!0,path:n,profileName:t}}function ar(e){let t=qt(Gi(e),".gitignore");et(t)||qi(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
8
+ `),"utf8")}async function cr(){let e=process.cwd(),t=ir(e);if(et(t)&&!await Fi({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let n=ur(e)??rr(e),o=await gt({message:"Project ID (used to scope sessions, screenshots, etc.):",default:n}),r=await gt({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await gt({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await gt({message:"Default connection profile name:",default:"berlin"}),u=await gt({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),g={projectId:o,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:u},production:!1}},defaultProfile:c,authoritativeSourceFiles:dr(e)},m=zi(e);return m?.terminologyGlossary&&(g.terminologyGlossary=m.terminologyGlossary),sr(g,e),ar(e),console.log(""),console.log(D(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${R(`d360-writer login --profile ${c}`)} ${f("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function ur(e){let t=qt(e,"package.json");if(!et(t))return null;try{return JSON.parse(Hi(t,"utf8")).name??null}catch{return null}}function dr(e){let t=[];for(let n of["README.md","ARCHITECTURE.md","CLAUDE.md"])et(qt(e,n))&&t.push(n);for(let n of["src","api","docs"]){let o=qt(e,n);et(o)&&!Yi(o)&&t.push(n)}return t}function Yi(e){try{return Bi(e,{withFileTypes:!0}).filter(n=>n.isDirectory()&&!n.name.startsWith(".")).length>6}catch{return!1}}H();import{readMcpConfig as An,writeMcpConfig as pr}from"document360-engine";async function zt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Xi(),{kind:"continue"}):t==="add"?(Vi(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Ji(e.slice(1)),{kind:"continue"}):(console.log(x(`Unknown /mcp subcommand: ${t}`)),console.log(f("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function Xi(){let e=An(),t=Object.keys(e.servers);if(t.length===0){console.log(f(`
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 o of t){let n=e.servers[o];if(n.type==="stdio")console.log(` ${S(o)} ${f("(stdio)")} ${n.command} ${(n.args??[]).join(" ")}`);else{let r=Object.keys(n.headers??{}),s=r.length>0?f(` [headers: ${r.join(", ")}]`):"";console.log(` ${S(o)} ${f(`(${n.type})`)} ${n.url}${s}`)}}console.log("")}function Di(e){let[t,o,...n]=e;if(!t||!o||n.length===0){console.log(x("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(o!=="stdio"&&o!=="http"&&o!=="sse"){console.log(x(`Unknown transport: ${o}. Use stdio, http, or sse.`));return}let r=Ro(),s;if(o==="stdio")s={type:"stdio",command:n[0],args:n.slice(1)};else{let c={};for(let d=1;d<n.length;d++)if(n[d]==="-H"||n[d]==="--header"){let h=n[++d],g=h?.match(/^([^:=]+)[:=](.+)$/);if(!g){console.log(x(`-H expects key:value (no spaces). Got: ${h??"(nothing)"}`));return}c[g[1].trim()]=g[2].trim()}else{console.log(x(`Unexpected argument: ${n[d]}. After the URL, only -H key:value is allowed.`));return}s={type:o,url:n[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,tr(r),console.log(""),console.log(D(`\u2713 Registered "${t}" (${o})`)),console.log(N(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(f(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function Ii(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let o=Ro();if(!o.servers[t]){console.log(x(`No server named "${t}".`));return}delete o.servers[t],tr(o),console.log(D(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}H();import{select as _i}from"@inquirer/prompts";import{computeSyncStatus as or}from"document360-engine";function qt(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(`
11
- `)}var nr={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Bt(e){return e.filter(t=>t.path!==null&&t.status in nr).map(t=>({path:t.path,label:nr[t.status]}))}function ct(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(`
12
- `)}async function rr(e,t){let o=e[0];if(o==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let n=Bt((await or({cwd:t?.cwd??process.cwd()})).entries);return n.length===0?(console.log(D("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:ct(n.map(r=>r.path)),display:"/publish --all"}}catch(n){return console.log(x(`Could not compute sync status: ${n.message}`)),{kind:"continue"}}}if(!o){console.log(f("Checking what needs publishing\u2026"));let n;try{n=Bt((await or({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(x(`Could not compute sync status: ${r.message}`)),console.log(f("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(n.length===0)return console.log(D("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(f(" (Articles without a sync base are unverified \u2014 publish those by path if needed.)")),{kind:"continue"};if(!process.stdin.isTTY||!t){for(let r of n)console.log(` ${r.path} ${f(`(${r.label})`)}`);return console.log(f("Run: /publish <article-path>")),{kind:"continue"}}try{o=await t.withPausedInput(()=>_i({message:"Publish which article?",choices:[...n.length>1?[{name:`All ${n.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...n.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(o==="--all")return{kind:"forward-to-agent",prompt:ct(n.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:qt(o),display:`/publish ${o}`}}async function zt(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","3. Map changed files to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
13
- `),display:"/audit"}}H();import{checkbox as Mi}from"@inquirer/prompts";import{inventoryRepo as Ni,readProjectConfig as sr,writeProjectConfig as Li}from"document360-engine";function Ao(e,t){let o=sr(e);o&&(o.authoritativeSourceFiles=t,Li(o,e))}function Gt(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 ir(e,t){let o=t?.cwd??process.cwd();if(!sr(o))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let n=Ni(o);if(n.length===0)return console.log(f('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(S("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of n)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(Gt(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>Mi({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:n.map(s=>({name:`${s.path} (${Gt(s)})`,value:s.path,checked:s.recommended})),pageSize:20}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(r.length===0)return console.log(f("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};Ao(o,r),console.log(D(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let s of r)console.log(` ${s}`);return console.log(""),{kind:"continue"}}H();import{confirm as Hi}from"@inquirer/prompts";import{applyPull as dr,computeSyncStatus as pr,planPull as Bi,D360AuthError as qi}from"document360-engine";H();var lr=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:x,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:N,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:S,mark:"M"},{status:"deleted-local",header:"Deleted locally \u2014 still on Document360 (pull to restore, or remove from the map):",paint:x,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:x,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:V,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:f,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:f,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:V,mark:"\xB7"}];function Oi(e){return e.path?e.path:`${e.title??"(untitled)"} ${V(`[${e.articleId}]`)}`}function Yt(e){let t=[];for(let c of lr){let d=e.entries.filter(h=>h.status===c.status);if(d.length!==0){t.push(""),t.push(we(c.header));for(let h of d)t.push(` ${c.paint(c.mark)} ${c.paint(Oi(h))}${h.detail?V(` (${h.detail})`):""}`)}}let o=e.counts["in-sync"]??0,n=e.entries.length;t.push("");let r={"in-sync":"in sync","local-ahead":"local ahead","remote-ahead":"remote ahead",conflict:"conflicts","untracked-local":"untracked local","untracked-remote":"untracked remote","deleted-local":"deleted locally","deleted-remote":"deleted on D360",orphaned:"orphaned","unknown-base":"no base yet"},s=n===o?D("\u2713 everything in sync"):[o>0?D(`\u2713 ${o} in sync`):null,...lr.map(c=>{let d=e.counts[c.status]??0;return d>0?`${d} ${r[c.status]}`:null})].filter(Boolean).join(V(" \xB7 "));return t.push(s+V(` \xB7 ${n} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as Ui}from"diff";import ar from"picocolors";var cr=80,Wi="\x1B[48;2;74;28;28m",Fi="\x1B[48;2;24;66;24m",ur="\x1B[49m",jo=e=>String(e).padStart(5);function Le(e,t,o){let n=$=>{let j=$.replace(/\r\n/g,`
14
- `);return j.endsWith(`
15
- `)||j===""?j:j+`
16
- `},r=n(e),s=n(t);if(r===s)return null;let c=Math.max(20,o-10),d=Ui("a","b",r,s,"","",{context:3}),h=0,g=0,w=[];d.hunks.forEach(($,j)=>{j>0&&w.push(ar.gray(" \u2026"));let P=$.oldStart,M=$.newStart;for(let q of $.lines){let z=q[0],ee=q.slice(1).slice(0,c);z==="-"?(g++,w.push(`${Wi}${jo(P++)} - ${ee}${ur}`)):z==="+"?(h++,w.push(`${Fi}${jo(M++)} + ${ee}${ur}`)):(w.push(ar.gray(jo(M))+" "+ee),P++,M++)}});let A=w.slice(0,cr);return{added:h,removed:g,lines:A,hidden:Math.max(0,w.length-cr)}}async function fr(e,t){let o=(e[0]??"status").toLowerCase();try{if(o==="status")return await zi(t.cwd),{kind:"continue"};if(o==="pull")return await Gi(t,e.slice(1)),{kind:"continue"};console.log(x(`Unknown subcommand: /sync ${o}`)),console.log(f("Usage: /sync drift report (local vs Document360)")),console.log(f(" /sync pull <path> pull portal edits into the local file")),console.log(f(" /sync pull --all pull every remote-ahead article"))}catch(n){n instanceof qi?console.log(x(n.message)):console.log(x(`Sync failed: ${n.message}`))}return{kind:"continue"}}async function zi(e){console.log(f("Checking Document360 for drift\u2026"));let t=await pr({cwd:e});for(let o of Yt(t))console.log(o);console.log("")}async function Gi(e,t){let o=t[0];if(!o){console.log(x("Usage: /sync pull <article-path> | --all"));return}let n;if(o==="--all"){if(console.log(f("Checking Document360 for drift\u2026")),n=(await pr({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),n.length===0){console.log(D("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(f(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${we(String(n.length))} article(s) are remote-ahead.`)}else n=[o.replace(/\\/g,"/")];for(let r of n){let s=await Bi({cwd:e.cwd,relPath:r});console.log(""),console.log(`${S("\u25CF")} ${we(s.title)} ${V(`(${s.path})`)}`);for(let g of s.notes)console.log(N(` \u26A0 ${g}`));s.overwritesLocalChanges&&console.log(N(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=Le(s.oldContent,s.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!c){console.log(f(" Local file already matches the remote content \u2014 advancing the sync base only.")),dr({cwd:e.cwd},s);continue}let d=g=>g===1?"":"s";console.log(V(` \u23BF Added ${c.added} line${d(c.added)}, removed ${c.removed} line${d(c.removed)}`));for(let g of c.lines)console.log(g);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>Hi({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}dr({cwd:e.cwd},s),console.log(D(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}H();import{statSync as nl}from"node:fs";import{resolve as rl}from"node:path";import{estimateBulkCost as sl,planPartitions as il,readProjectConfig as ll,resolveModelForOperation as al,runPartitioned as cl,trackedArticlePaths as ul}from"document360-engine";import F from"picocolors";import wr from"wrap-ansi";import No from"string-width";H();import Te from"picocolors";import Eo from"wrap-ansi";import Do from"string-width";var mr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),gr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),hr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function Yi(e){let t=e.replace(/\r/g,"").split(`
17
- `),o=[],n=0;for(;n<t.length;){let r=t[n];if(/^\s*```/.test(r)){let d=[];for(n++;n<t.length&&!/^\s*```/.test(t[n]);)d.push(t[n++]);n++,o.push({kind:"code",lines:d});continue}if(mr(r)){o.push({kind:"hr"}),n++;continue}if(r.includes("|")&&n+1<t.length&&gr(t[n+1])){let d=hr(r);n+=2;let h=[];for(;n<t.length&&t[n].includes("|")&&t[n].trim()!=="";)h.push(hr(t[n++]));o.push({kind:"table",header:d,rows:h});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){o.push({kind:"heading",level:s[1].length,text:s[2]}),n++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let d=[];for(;n<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[n]);)d.push(t[n++].replace(/^\s*([-*]|\d+\.)\s+/,""));o.push({kind:"list",items:d});continue}if(r.trim()===""){n++;continue}let c=[];for(;n<t.length&&t[n].trim()!==""&&!/^\s*```/.test(t[n])&&!/^(#{1,6})\s/.test(t[n])&&!/^\s*([-*]|\d+\.)\s+/.test(t[n])&&!mr(t[n])&&!(t[n].includes("|")&&n+1<t.length&&gr(t[n+1]));)c.push(t[n++]);o.push({kind:"para",text:c.join(" ")})}return o}function Io(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Te.bold(t.slice(2,-2)):t.startsWith("`")?S(t.slice(1,-1)):Te.italic(t.slice(1,-1)))}var Xi=(e,t)=>e+" ".repeat(Math.max(0,t-Do(e)));function Vi(e,t,o){let n=e.length,r=e.map((P,M)=>Math.max(Do(P),...t.map(q=>Do(q[M]??"")))),s=Math.max(24,o),c=3*n+1,d=[...r],h=()=>d.reduce((P,M)=>P+M,0)+c,g=0;for(;h()>s&&g++<1e4;){let P=-1,M=6;for(let q=0;q<n;q++)d[q]>M&&(M=d[q],P=q);if(P===-1)break;d[P]-=1}let w=(P,M,q)=>Te.gray(P+d.map(z=>"\u2500".repeat(z+2)).join(M)+q),A=Te.gray("\u2502"),$=(P,M)=>{let q=d.map((ie,$e)=>{let le=P[$e]??"",ae=M?Te.bold(le):Io(le);return Eo(ae,ie,{hard:!0}).split(`
18
- `)}),z=Math.max(...q.map(ie=>ie.length)),ee=[];for(let ie=0;ie<z;ie++)ee.push(d.map(($e,le)=>`${A} ${Xi(q[le][ie]??"",$e)} `).join("")+A);return ee.join(`
19
- `)},j=[w("\u250C","\u252C","\u2510"),$(e,!0)];return j.push(t.length===0?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524")),t.forEach((P,M)=>{j.push($(P,!1)),j.push(M===t.length-1?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524"))}),j.join(`
20
- `)}function Ji(e,t){switch(e.kind){case"heading":return Te.bold(e.text);case"hr":return Te.gray("\u2500".repeat(t));case"para":return Eo(Io(e.text),t);case"list":return e.items.map(o=>{let[n="",...r]=Eo(Io(o),Math.max(10,t-4)).split(`
21
- `);return" \u2022 "+n+r.map(s=>`
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(` ${R(n)} ${f("(stdio)")} ${o.command} ${(o.args??[]).join(" ")}`);else{let r=Object.keys(o.headers??{}),s=r.length>0?f(` [headers: ${r.join(", ")}]`):"";console.log(` ${R(n)} ${f(`(${o.type})`)} ${o.url}${s}`)}}console.log("")}function Vi(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=An(),s;if(n==="stdio")s={type:"stdio",command:o[0],args:o.slice(1)};else{let c={};for(let u=1;u<o.length;u++)if(o[u]==="-H"||o[u]==="--header"){let g=o[++u],m=g?.match(/^([^:=]+)[:=](.+)$/);if(!m){console.log(x(`-H expects key:value (no spaces). Got: ${g??"(nothing)"}`));return}c[m[1].trim()]=m[2].trim()}else{console.log(x(`Unexpected argument: ${o[u]}. 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,pr(r),console.log(""),console.log(D(`\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(f(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function Ji(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let n=An();if(!n.servers[t]){console.log(x(`No server named "${t}".`));return}delete n.servers[t],pr(n),console.log(D(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}H();import{select as Ki}from"@inquirer/prompts";import{computeSyncStatus as fr}from"document360-engine";function Yt(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(`
11
+ `)}var mr={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Gt(e){return e.filter(t=>t.path!==null&&t.status in mr).map(t=>({path:t.path,label:mr[t.status]}))}function ht(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(`
12
+ `)}async function gr(e,t){let n=e[0];if(n==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let o=Gt((await fr({cwd:t?.cwd??process.cwd()})).entries);return o.length===0?(console.log(D("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:ht(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(f("Checking what needs publishing\u2026"));let o;try{o=Gt((await fr({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(x(`Could not compute sync status: ${r.message}`)),console.log(f("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(o.length===0)return console.log(D("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(f(" (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} ${f(`(${r.label})`)}`);return console.log(f("Run: /publish <article-path>")),{kind:"continue"}}try{n=await t.withPausedInput(()=>Ki({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(f("Cancelled.")),{kind:"continue"}}if(n==="--all")return{kind:"forward-to-agent",prompt:ht(o.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:Yt(n),display:`/publish ${n}`}}async function Xt(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","3. Map changed files to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
13
+ `),display:"/audit"}}H();import{checkbox as Qi}from"@inquirer/prompts";import{inventoryRepo as Zi,readProjectConfig as hr,writeProjectConfig as el}from"document360-engine";function jn(e,t){let n=hr(e);n&&(n.authoritativeSourceFiles=t,el(n,e))}function Vt(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 kr(e,t){let n=t?.cwd??process.cwd();if(!hr(n))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=Zi(n);if(o.length===0)return console.log(f('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(R("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of o)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(Vt(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>Qi({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:o.map(s=>({name:`${s.path} (${Vt(s)})`,value:s.path,checked:s.recommended})),pageSize:20}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(r.length===0)return console.log(f("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};jn(n,r),console.log(D(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let s of r)console.log(` ${s}`);return console.log(""),{kind:"continue"}}H();import{confirm as sl}from"@inquirer/prompts";import{applyPull as br,computeSyncStatus as vr,planPull as il,D360AuthError as ll}from"document360-engine";H();var yr=[{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:R,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:Q,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:f,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:f,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:Q,mark:"\xB7"}];function tl(e){return e.path?e.path:`${e.title??"(untitled)"} ${Q(`[${e.articleId}]`)}`}function Jt(e){let t=[];for(let c of yr){let u=e.entries.filter(g=>g.status===c.status);if(u.length!==0){t.push(""),t.push(ve(c.header));for(let g of u)t.push(` ${c.paint(c.mark)} ${c.paint(tl(g))}${g.detail?Q(` (${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?D("\u2713 everything in sync"):[n>0?D(`\u2713 ${n} in sync`):null,...yr.map(c=>{let u=e.counts[c.status]??0;return u>0?`${u} ${r[c.status]}`:null})].filter(Boolean).join(Q(" \xB7 "));return t.push(s+Q(` \xB7 ${o} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as nl}from"diff";import wr from"picocolors";var $r=80,ol="\x1B[48;2;74;28;28m",rl="\x1B[48;2;24;66;24m",xr="\x1B[49m",En=e=>String(e).padStart(5);function We(e,t,n){let o=b=>{let v=b.replace(/\r\n/g,`
14
+ `);return v.endsWith(`
15
+ `)||v===""?v:v+`
16
+ `},r=o(e),s=o(t);if(r===s)return null;let c=Math.max(20,n-10),u=nl("a","b",r,s,"","",{context:3}),g=0,m=0,w=[];u.hunks.forEach((b,v)=>{v>0&&w.push(wr.gray(" \u2026"));let $=b.oldStart,E=b.newStart;for(let q of b.lines){let G=q[0],ie=q.slice(1).slice(0,c);G==="-"?(m++,w.push(`${ol}${En($++)} - ${ie}${xr}`)):G==="+"?(g++,w.push(`${rl}${En(E++)} + ${ie}${xr}`)):(w.push(wr.gray(En(E))+" "+ie),$++,E++)}});let j=w.slice(0,$r);return{added:g,removed:m,lines:j,hidden:Math.max(0,w.length-$r)}}async function Cr(e,t){let n=(e[0]??"status").toLowerCase();try{if(n==="status")return await al(t.cwd),{kind:"continue"};if(n==="pull")return await cl(t,e.slice(1)),{kind:"continue"};console.log(x(`Unknown subcommand: /sync ${n}`)),console.log(f("Usage: /sync drift report (local vs Document360)")),console.log(f(" /sync pull <path> pull portal edits into the local file")),console.log(f(" /sync pull --all pull every remote-ahead article"))}catch(o){o instanceof ll?console.log(x(o.message)):console.log(x(`Sync failed: ${o.message}`))}return{kind:"continue"}}async function al(e){console.log(f("Checking Document360 for drift\u2026"));let t=await vr({cwd:e});for(let n of Jt(t))console.log(n);console.log("")}async function cl(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(f("Checking Document360 for drift\u2026")),o=(await vr({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),o.length===0){console.log(D("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(f(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${ve(String(o.length))} article(s) are remote-ahead.`)}else o=[n.replace(/\\/g,"/")];for(let r of o){let s=await il({cwd:e.cwd,relPath:r});console.log(""),console.log(`${R("\u25CF")} ${ve(s.title)} ${Q(`(${s.path})`)}`);for(let m of s.notes)console.log(L(` \u26A0 ${m}`));s.overwritesLocalChanges&&console.log(L(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=We(s.oldContent,s.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!c){console.log(f(" Local file already matches the remote content \u2014 advancing the sync base only.")),br({cwd:e.cwd},s);continue}let u=m=>m===1?"":"s";console.log(Q(` \u23BF Added ${c.added} line${u(c.added)}, removed ${c.removed} line${u(c.removed)}`));for(let m of c.lines)console.log(m);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>sl({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}br({cwd:e.cwd},s),console.log(D(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}H();import{statSync as $l}from"node:fs";import{resolve as xl}from"node:path";import{estimateBulkCost as bl,planPartitions as vl,readProjectConfig as Cl,resolveModelForOperation as Pl,runPartitioned as Sl,trackedArticlePaths as Tl}from"document360-engine";import B from"picocolors";import jr from"wrap-ansi";import Ln from"string-width";H();import je from"picocolors";import Dn from"wrap-ansi";import In from"string-width";var Pr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Sr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Tr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function ul(e){let t=e.replace(/\r/g,"").split(`
17
+ `),n=[],o=0;for(;o<t.length;){let r=t[o];if(/^\s*```/.test(r)){let u=[];for(o++;o<t.length&&!/^\s*```/.test(t[o]);)u.push(t[o++]);o++,n.push({kind:"code",lines:u});continue}if(Pr(r)){n.push({kind:"hr"}),o++;continue}if(r.includes("|")&&o+1<t.length&&Sr(t[o+1])){let u=Tr(r);o+=2;let g=[];for(;o<t.length&&t[o].includes("|")&&t[o].trim()!=="";)g.push(Tr(t[o++]));n.push({kind:"table",header:u,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 u=[];for(;o<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[o]);)u.push(t[o++].replace(/^\s*([-*]|\d+\.)\s+/,""));n.push({kind:"list",items:u});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])&&!Pr(t[o])&&!(t[o].includes("|")&&o+1<t.length&&Sr(t[o+1]));)c.push(t[o++]);n.push({kind:"para",text:c.join(" ")})}return n}function _n(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?je.bold(t.slice(2,-2)):t.startsWith("`")?R(t.slice(1,-1)):je.italic(t.slice(1,-1)))}var dl=(e,t)=>e+" ".repeat(Math.max(0,t-In(e)));function pl(e,t,n){let o=e.length,r=e.map(($,E)=>Math.max(In($),...t.map(q=>In(q[E]??"")))),s=Math.max(24,n),c=3*o+1,u=[...r],g=()=>u.reduce(($,E)=>$+E,0)+c,m=0;for(;g()>s&&m++<1e4;){let $=-1,E=6;for(let q=0;q<o;q++)u[q]>E&&(E=u[q],$=q);if($===-1)break;u[$]-=1}let w=($,E,q)=>je.gray($+u.map(G=>"\u2500".repeat(G+2)).join(E)+q),j=je.gray("\u2502"),b=($,E)=>{let q=u.map((de,Pe)=>{let pe=$[Pe]??"",fe=E?je.bold(pe):_n(pe);return Dn(fe,de,{hard:!0}).split(`
18
+ `)}),G=Math.max(...q.map(de=>de.length)),ie=[];for(let de=0;de<G;de++)ie.push(u.map((Pe,pe)=>`${j} ${dl(q[pe][de]??"",Pe)} `).join("")+j);return ie.join(`
19
+ `)},v=[w("\u250C","\u252C","\u2510"),b(e,!0)];return v.push(t.length===0?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524")),t.forEach(($,E)=>{v.push(b($,!1)),v.push(E===t.length-1?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524"))}),v.join(`
20
+ `)}function fl(e,t){switch(e.kind){case"heading":return je.bold(e.text);case"hr":return je.gray("\u2500".repeat(t));case"para":return Dn(_n(e.text),t);case"list":return e.items.map(n=>{let[o="",...r]=Dn(_n(n),Math.max(10,t-4)).split(`
21
+ `);return" \u2022 "+o+r.map(s=>`
22
22
  `+s).join("")}).join(`
23
- `);case"code":return e.lines.map(o=>Te.gray(" "+o)).join(`
24
- `);case"table":return Vi(e.header,e.rows,t)}}function _o(e,t){let o=Math.max(20,t);return Yi(e).map(n=>Ji(n,o)).join(`
23
+ `);case"code":return e.lines.map(n=>je.gray(" "+n)).join(`
24
+ `);case"table":return pl(e.header,e.rows,t)}}function Mn(e,t){let n=Math.max(20,t);return ul(e).map(o=>fl(o,n)).join(`
25
25
 
26
- `)}H();import Ki from"picocolors";var Qi=[127,86,217],Zi=[22,38,43],kr={T:Qi,E:Zi},ut=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Mo=([e,t,o])=>`\x1B[38;2;${e};${t};${o}m`,el=([e,t,o])=>`\x1B[48;2;${e};${t};${o}m`;function yr(){if(!Ki.isColorSupported)return[];let e=ut[0].length,t=[];for(let o=0;o<ut.length;o+=2){let n="";for(let r=0;r<e;r++){let s=kr[ut[o][r]],c=o+1<ut.length?kr[ut[o+1][r]]:void 0;s&&c?n+=`${Mo(s)}${el(c)}\u2580\x1B[49m\x1B[39m`:s?n+=`${Mo(s)}\u2580\x1B[39m`:c?n+=`${Mo(c)}\u2584\x1B[39m`:n+=" "}t.push(n)}return t}function dt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function Oe(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Xt(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var Qe=(e,t)=>` ${e.padEnd(13)}${t}`;function tl(e,t){let o=[st("\u270E document360-writer")+F.gray(` v${e.version}`),F.gray(" Reads your code, writes your docs."),"",Qe("Claude:",`${e.claude}${F.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),Qe("Document360:",e.configured?e.loggedOut?F.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?F.gray(` (${e.sessionHint})`):""}`:F.yellow("not set up \u2014 run /init")),Qe("Profile:",e.configured?`${e.profile}${F.gray(` (${e.apiUrl})`)}${e.prod?F.bold(F.yellow(" \u26A0 PRODUCTION")):""}`:F.gray("\u2014 (run /init)")),Qe("Project:",e.project),Qe("Mode:",F.gray(e.mode)),Qe("cwd:",F.gray(e.cwd))],n=yr();if(n.length===0)return o.join(`
27
- `);let r=2,s=3,c=No(n[0]);if(!(t>=r+c+s+Math.max(...o.map(w=>No(w)))))return[...n.map(w=>" "+w),...o].join(`
28
- `);let h=Math.max(0,Math.floor((o.length-n.length)/2)),g=[];for(let w=0;w<Math.max(n.length+h,o.length);w++){let A=n[w-h]??" ".repeat(c);g.push((" ".repeat(r)+A+" ".repeat(s)+(o[w]??"")).trimEnd())}return g.join(`
29
- `)}var ol={error:F.red,warn:F.yellow,ok:F.green,info:F.gray};function Lo(e,t){let o=Math.max(20,t);switch(e.kind){case"banner":return tl(e.info,o);case"user":{let n="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,o-4),c=50,d=e.text.split(`
30
- `).flatMap(g=>wr(g,s,{hard:!0}).split(`
31
- `)),h=Math.max(0,d.length-c);return h>0&&(d=[...d.slice(0,c),F.dim(`\u2026 +${h} more lines`)]),`
32
- `+d.map((g,w)=>n+(w===0?S(" \u276F "):" ")+g+" ".repeat(Math.max(0,s-No(g))+1)+r).join(`
26
+ `)}H();import ml from"picocolors";var gl=[127,86,217],hl=[22,38,43],Rr={T:gl,E:hl},kt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Nn=([e,t,n])=>`\x1B[38;2;${e};${t};${n}m`,kl=([e,t,n])=>`\x1B[48;2;${e};${t};${n}m`;function Ar(){if(!ml.isColorSupported)return[];let e=kt[0].length,t=[];for(let n=0;n<kt.length;n+=2){let o="";for(let r=0;r<e;r++){let s=Rr[kt[n][r]],c=n+1<kt.length?Rr[kt[n+1][r]]:void 0;s&&c?o+=`${Nn(s)}${kl(c)}\u2580\x1B[49m\x1B[39m`:s?o+=`${Nn(s)}\u2580\x1B[39m`:c?o+=`${Nn(c)}\u2584\x1B[39m`:o+=" "}t.push(o)}return t}function yt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function ue(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function nt(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var tt=(e,t)=>` ${e.padEnd(13)}${t}`;function yl(e,t){let n=[pt("\u270E document360-writer")+B.gray(` v${e.version}`),B.gray(" Reads your code, writes your docs."),"",tt("Claude:",`${e.claude}${B.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),tt("Document360:",e.configured?e.loggedOut?B.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?B.gray(` (${e.sessionHint})`):""}`:B.yellow("not set up \u2014 run /init")),tt("Profile:",e.configured?`${e.profile}${B.gray(` (${e.apiUrl})`)}${e.prod?B.bold(B.yellow(" \u26A0 PRODUCTION")):""}`:B.gray("\u2014 (run /init)")),tt("Project:",e.project),tt("Mode:",B.gray(e.mode)),tt("cwd:",B.gray(e.cwd))],o=Ar();if(o.length===0)return n.join(`
27
+ `);let r=2,s=3,c=Ln(o[0]);if(!(t>=r+c+s+Math.max(...n.map(w=>Ln(w)))))return[...o.map(w=>" "+w),...n].join(`
28
+ `);let g=Math.max(0,Math.floor((n.length-o.length)/2)),m=[];for(let w=0;w<Math.max(o.length+g,n.length);w++){let j=o[w-g]??" ".repeat(c);m.push((" ".repeat(r)+j+" ".repeat(s)+(n[w]??"")).trimEnd())}return m.join(`
29
+ `)}var wl={error:B.red,warn:B.yellow,ok:B.green,info:B.gray};function On(e,t){let n=Math.max(20,t);switch(e.kind){case"banner":return yl(e.info,n);case"user":{let o="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,n-4),c=50,u=e.text.split(`
30
+ `).flatMap(m=>jr(m,s,{hard:!0}).split(`
31
+ `)),g=Math.max(0,u.length-c);return g>0&&(u=[...u.slice(0,c),B.dim(`\u2026 +${g} more lines`)]),`
32
+ `+u.map((m,w)=>o+(w===0?R(" \u276F "):" ")+m+" ".repeat(Math.max(0,s-Ln(m))+1)+r).join(`
33
33
  `)}case"assistant":return`
34
- `+_o(e.text,o);case"tool":{let n=e.arg!==null?F.gray(`${e.sep}(${e.arg})`):"";return`
35
- `+wr(F.green("\u25CF ")+F.bold(e.title)+n,o)}case"tool-result":{let n=e.isError?F.red:F.gray,r=e.lines.map((s,c)=>n((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(F.dim(` \u2026 +${e.hidden} lines`)),r.join(`
36
- `)}case"diff":{let n=c=>c===1?"":"s",s=[F.gray(` \u23BF Added ${e.added} line${n(e.added)}, removed ${e.removed} line${n(e.removed)}`),...e.lines];return e.hidden>0&&s.push(F.dim(` \u2026 +${e.hidden} more diff lines`)),s.join(`
37
- `)}case"link":return e.lines.map(n=>S(it(` \u2B95 ${n}`))).join(`
34
+ `+Mn(e.text,n);case"tool":{let o=e.arg!==null?B.gray(`${e.sep}(${e.arg})`):"";return`
35
+ `+jr(B.green("\u25CF ")+B.bold(e.title)+o,n)}case"tool-result":{let o=e.isError?B.red:B.gray,r=e.lines.map((s,c)=>o((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(B.dim(` \u2026 +${e.hidden} lines`)),r.join(`
36
+ `)}case"diff":{let o=c=>c===1?"":"s",s=[B.gray(` \u23BF Added ${e.added} line${o(e.added)}, removed ${e.removed} line${o(e.removed)}`),...e.lines];return e.hidden>0&&s.push(B.dim(` \u2026 +${e.hidden} more diff lines`)),s.join(`
37
+ `)}case"link":return e.lines.map(o=>R(ft(` \u2B95 ${o}`))).join(`
38
38
  `);case"preview":return`
39
- `+st(`\u25A3 Preview \u2014 ${e.name}`)+`
39
+ `+pt(`\u25A3 Preview \u2014 ${e.name}`)+`
40
40
 
41
- `+_o(e.text,o);case"note":return`
42
- `+ol[e.tone](it(e.text));case"done":return`
43
- `+(e.ok?F.magenta("\u2736 "):F.red("\u2736 "))+F.gray(`Cooked for ${dt(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${Oe(e.costUsd)}`:""))}}function xr(e,t){return e.map(o=>Lo(o,t)).join(`
44
- `)}var Oo=3;function Uo(e){let t,o=!1;for(let r=0;r<e.length;r++){let s=e[r];s==="--run"||s==="--yes"?o=!0:s==="--scope"?t=e[++r]:s?.startsWith("--scope=")&&(t=s.slice(8))}return{scope:t?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0,run:o}}function Wo(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function Fo(e,t){return t.map(o=>{let n=0;try{n=nl(rl(e,o)).size}catch{n=0}return{path:o,bytes:n}})}function Ho(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:
41
+ `+Mn(e.text,n);case"note":return`
42
+ `+wl[e.tone](ft(e.text));case"done":return`
43
+ `+(e.ok?B.magenta("\u2736 "):B.red("\u2736 "))+B.gray(`Cooked for ${yt(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${ue(e.costUsd)}`:""))}}function Er(e,t){return e.map(n=>On(n,t)).join(`
44
+ `)}var Un=3;function Wn(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 Fn(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function Hn(e,t){return t.map(n=>{let o=0;try{o=$l(xl(e,n)).size}catch{o=0}return{path:n,bytes:o}})}function Bn(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 Bo(e,t,o){let n=e.reduce((d,h)=>d+h.paths.length,0),[r,s]=t.usd;return[`Convert ${n} article${n===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${o} agents at once):`,...e.map(d=>` \u2022 ${d.label} \u2014 ${d.paths.length} article${d.paths.length===1?"":"s"}`),"",`Estimated cost: ${Oe(r)}\u2013${Oe(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function qo(e,t,o){let n=P=>t[P.index]?.paths.length??0,r=e.filter(P=>P.ok),s=e.filter(P=>!P.ok),c=e.reduce((P,M)=>P+n(M),0),d=r.reduce((P,M)=>P+n(M),0),h=e.reduce((P,M)=>P+M.costUsd,0),g=e.reduce((P,M)=>P+M.outputTokens,0),w=o==="api"?`${Oe(h)} total`:Xt(g),A=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,$=P=>`${P} article${P===1?"":"s"}`,j=[];if(s.length===0)j.push(`Converted ${$(c)} (${A}) successfully \xB7 ${w}.`);else{j.push(`Converted ${d}/${$(c)} (${A}) \xB7 ${w}.`),j.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let P of s)j.push(` \u2717 ${P.label}${P.error?` \u2014 ${P.error}`:""}`)}return j}async function $r(e,t){if(!ll(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:o,run:n}=Uo(e),r=ul(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=Wo(r,o);if(s.length===0)return console.log(x(`No tracked articles under "${o}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=il(s),d=`/convert${o?` --scope ${o}`:""} --run`,{model:h,forced:g}=al(t.cwd,"light");if(!n){let w=sl({files:Fo(t.cwd,s),op:"convert",model:h});o&&console.log(f(`Scope: ${o} (${s.length} of ${r.length} tracked articles).`));for(let A of Bo(c,w,Oo))console.log(A);return console.log(f(`Model: ${h}${g?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${d} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${Oo} agents at once) on ${h}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let w of cl({cwd:t.cwd,partitions:c,promptFor:Ho,concurrency:Oo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:h}))if(w.type==="partition_status")w.status==="running"?console.log(f(` \u25B8 ${w.label} \u2014 converting\u2026`)):w.status==="done"?console.log(D(` \u2713 ${w.label}`)):console.log(x(` \u2717 ${w.label}`));else if(w.type==="run_done"){console.log("");let A=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let $ of qo(w.results,c,A))console.log(w.ok?D($):N($))}}catch(w){console.log(x(`Convert run failed: ${w.message}`))}return console.log(""),{kind:"continue"}}H();import{search as dl}from"@inquirer/prompts";import{findByName as pl,getSession as fl,listSessions as ml,relativeTime as vr}from"document360-engine";async function Cr(e,t){let o=ml(t.cwd).filter(n=>n.uuid!==t.currentUuid());if(o.length===0)return console.log(f("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let n=e.join(" "),r=pl(t.cwd,n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${n}".`)),br(o),{kind:"continue"})}if(!process.stdin.isTTY)return br(o),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>dl({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return o.filter(d=>!c||d.name.toLowerCase().includes(c)||d.firstPrompt.toLowerCase().includes(c)).map(d=>({name:`${d.name} ${vr(d.updatedAt)}`,value:d.uuid,description:d.firstPrompt.slice(0,100)}))}})),r=fl(n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function br(e){console.log("");for(let t of e.slice(0,15))console.log(` ${S(t.name)} ${f(vr(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}H();import{renameSession as gl}from"document360-engine";function zo(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 Sr(e,t){let o=zo(e.join(" "));if(!o)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(gl(n,o)?console.log(D(`\u2713 Session renamed to "${o}"`)):console.log(x("Could not find the current session record.")),{kind:"continue"}):(console.log(x("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as Tr,readProjectConfig as hl,writeProjectConfig as kl}from"document360-engine";H();function Go(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=hl(e);if(!n)return"No .d360-writer.json \u2014 run /init first.";if(n.profiles?.[t])return`Profile "${t}" already exists.`;let r=o??t;return Tr().includes(r)?(n.profiles={...n.profiles,[t]:{connection:{environment:r},production:!1}},kl(n,e),null):`Unknown environment "${r}". Known: ${Tr().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Pr(e,t){let o=e[0];if(!o)return Lt(t.cwd),{kind:"continue"};if(o==="add"){let n=Go(t.cwd,e[1],e[2]);return n?(console.log(x(n)),{kind:"continue"}):(console.log(D(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${S(`/profile ${e[1]}`)} then ${S("/login")}`),{kind:"continue"})}return Ot(t.cwd,o),Ut(t.cwd,o),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}H();import{select as yl}from"@inquirer/prompts";import{readProjectConfig as wl,readUserConfig as Rr,resolveModelSetting as Yo,writeUserConfig as Ar}from"document360-engine";var de=[{value:null,label:"Auto",desc:"Engine right-sizes per task \u2014 Sonnet for routine work, Opus for analysis (recommended)"},{value:"claude-fable-5",label:"Fable",desc:"Fable 5 \xB7 most capable, for the hardest and longest-running tasks"},{value:"opus",label:"Opus",desc:"Opus 4.8 \xB7 best for everyday complex tasks"},{value:"sonnet",label:"Sonnet",desc:"Sonnet 4.6 \xB7 efficient for routine tasks"},{value:"haiku",label:"Haiku",desc:"Haiku 4.5 \xB7 fastest for quick answers"}];function Vt(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),o=de.findIndex(n=>n.value!==null&&(n.value===t||n.label.toLowerCase()===t||t.includes(n.label.toLowerCase())));return o>=0?o:0}function xl(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 o=()=>{let s=Yo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=Rr();return s.defaultModel?(delete s.defaultModel,Ar(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Yo(e).model??"Claude Code default"} (applies from your next message)`],changed:!0,effective:o()}):{lines:["No personal model override set \u2014 nothing to clear."],changed:!1,effective:void 0}}Ar({...Rr(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=wl(e)?.defaultModel;return r&&n.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:n,changed:!0,effective:o()}}async function jr(e,t){let o=e[0]?.trim();if(!o){let c=Yo(t.cwd);if(!process.stdin.isTTY)return console.log(`${we("Model:")} ${S(c.model??"Claude Code default")}`),console.log(f(` source: ${xl(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let d=Vt(c),h;try{h=await t.withPausedInput(()=>yl({message:`Select model (current: ${c.model??"Claude Code default"})`,default:de[d].value,choices:de.map(($,j)=>({name:`${$.label}${j===d?" \u2714":""}`,value:$.value,description:$.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:g,changed:w,effective:A}=pt(t.cwd,h??"default");for(let $ of g)console.log($.startsWith("\u26A0")?N($):$.startsWith("\u2713")?D($):f($));return w&&await t.setModel(A),{kind:"continue"}}let{lines:n,changed:r,effective:s}=pt(t.cwd,o);for(let c of n)console.log(c.startsWith("\u26A0")?N(c):c.startsWith("\u2713")?D(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}Jt();async function Mr(e,t){return await t.withPausedInput(()=>Je(t.cwd)),{kind:"clear"}}H();import{resolveActiveProfile as El}from"document360-engine";async function Nr(e,t){let o=!1;try{o=El(t.cwd).production}catch{}return o?(console.log(N("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(f("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}H();var Lr=async(e,t)=>{try{await t.withPausedInput(()=>Nt({}))}catch(o){console.log(x(`Login failed: ${o.message}`))}return{kind:"continue"}};import{existsSync as Or}from"node:fs";import{isAbsolute as Dl,join as Il,resolve as _l}from"node:path";import{readProjectConfig as Ml,screenshotPlaceholderIds as Nl}from"document360-engine";var Ur=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Vo(e){let t=!e.includes("--no-setup"),o=e.filter(r=>r!=="--no-setup");if(o[0]==="--list")return{mode:"list",scope:o[1]?Ur(o[1]):void 0};let n=o[0];return!n||n==="--all"?{mode:"all",setup:t}:/[\\/]/.test(n)||n.endsWith(".md")?{mode:"scope",scope:Ur(n),setup:t}:{mode:"single",id:n}}function Jo(e,t){let o=Ml(e),n=o?.captureDir??"user-docs/_capture",r=o?.outputDir??"user-docs/_screenshots",s=(c,d)=>Dl(c)?Il(c,d):_l(e,c,d);return Nl(e,{scope:t}).map(({id:c,file:d})=>{let h=Or(s(r,`${c}.png`))?"captured":Or(s(n,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:d,state:h}})}var Ll={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},Ol={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Ko(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let o=[...new Set(e.map(s=>s.file))].sort(),n=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${o.length} article${o.length===1?"":"s"} \xB7 ${n} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of o){r.push(s);for(let c of e.filter(d=>d.file===s))r.push(` ${Ll[c.state]} ${c.id.padEnd(34)} ${Ol[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function Wr(e){return["Run the emit-screenshot-spec skill to author the document360-capture spec for EACH of these","SCREENSHOT placeholder ids:",e.paths.map(t=>`- ${t}`).join(`
50
+ `)}function qn(e,t,n){let o=e.reduce((u,g)=>u+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(u=>` \u2022 ${u.label} \u2014 ${u.paths.length} article${u.paths.length===1?"":"s"}`),"",`Estimated cost: ${ue(r)}\u2013${ue(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function zn(e,t,n){let o=$=>t[$.index]?.paths.length??0,r=e.filter($=>$.ok),s=e.filter($=>!$.ok),c=e.reduce(($,E)=>$+o(E),0),u=r.reduce(($,E)=>$+o(E),0),g=e.reduce(($,E)=>$+E.costUsd,0),m=e.reduce(($,E)=>$+E.outputTokens,0),w=n==="api"?`${ue(g)} total`:nt(m),j=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,b=$=>`${$} article${$===1?"":"s"}`,v=[];if(s.length===0)v.push(`Converted ${b(c)} (${j}) successfully \xB7 ${w}.`);else{v.push(`Converted ${u}/${b(c)} (${j}) \xB7 ${w}.`),v.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let $ of s)v.push(` \u2717 ${$.label}${$.error?` \u2014 ${$.error}`:""}`)}return v}async function Dr(e,t){if(!Cl(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:n,run:o}=Wn(e),r=Tl(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=Fn(r,n);if(s.length===0)return console.log(x(`No tracked articles under "${n}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=vl(s),u=`/convert${n?` --scope ${n}`:""} --run`,{model:g,forced:m}=Pl(t.cwd,"light");if(!o){let w=bl({files:Hn(t.cwd,s),op:"convert",model:g});n&&console.log(f(`Scope: ${n} (${s.length} of ${r.length} tracked articles).`));for(let j of qn(c,w,Un))console.log(j);return console.log(f(`Model: ${g}${m?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${u} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${Un} agents at once) on ${g}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let w of Sl({cwd:t.cwd,partitions:c,promptFor:Bn,concurrency:Un,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:g}))if(w.type==="partition_status")w.status==="running"?console.log(f(` \u25B8 ${w.label} \u2014 converting\u2026`)):w.status==="done"?console.log(D(` \u2713 ${w.label}`)):console.log(x(` \u2717 ${w.label}`));else if(w.type==="run_done"){console.log("");let j=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let b of zn(w.results,c,j))console.log(w.ok?D(b):L(b))}}catch(w){console.log(x(`Convert run failed: ${w.message}`))}return console.log(""),{kind:"continue"}}H();import{statSync as Ir}from"node:fs";import{resolve as _r}from"node:path";import{estimateBulkCost as Rl,planPartitions as Al,readDocsPlan as jl,readProjectConfig as Mr,resolveModelForOperation as El,runPartitioned as Dl}from"document360-engine";var Gn=5;function Yn(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 Il(e,t){let n=e.replace(/\\/g,"/").replace(/\/+$/,""),o=`${t.replace(/\/+$/,"")}/`;return n.startsWith(o)?n.slice(o.length):n}function _l(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function Ml(e,t,n){return n.filter(o=>{try{return Ir(_r(e,t,o)).size===0}catch{return!0}})}function Xn(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+=Ir(_r(e,c)).size}catch{}return{path:r,bytes:s}})}function Vn(e,t){return`Run the write-article skill to author EACH documentation article listed below. For each path:
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
+ - 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
+ - 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.
54
+ - Emit \`<!-- SCREENSHOT -->\` placeholders where a visual helps, but do NOT author capture specs now.
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
+ Articles:
57
+ `+e.paths.map(n=>`- ${n}`).join(`
58
+ `)}function Jn(e,t,n){let o=e.reduce((c,u)=>c+u.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: ${ue(r)}\u2013${ue(s)}. ${t.note}`,"","Articles are written as local drafts in the docs tree (nothing is published). Run /write --run to start."]}function Kn(e,t,n){let o=$=>t[$.index]?.paths.length??0,r=e.filter($=>$.ok),s=e.filter($=>!$.ok),c=e.reduce(($,E)=>$+o(E),0),u=r.reduce(($,E)=>$+o(E),0),g=e.reduce(($,E)=>$+E.costUsd,0),m=e.reduce(($,E)=>$+E.outputTokens,0),w=n==="api"?`${ue(g)} total`:nt(m),j=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,b=$=>`${$} article${$===1?"":"s"}`,v=[];if(s.length===0)v.push(`Wrote ${b(c)} (${j}) successfully \xB7 ${w}.`),v.push("Review them in the docs tree, then /publish when ready.");else{v.push(`Wrote ${u}/${b(c)} (${j}) \xB7 ${w}.`),v.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /write to retry the rest:`);for(let $ of s)v.push(` \u2717 ${$.label}${$.error?` \u2014 ${$.error}`:""}`)}return v}function Qn(e,t,n){let o=(Mr(e)?.docsDir??"user-docs").replace(/\/+$/,""),r=t.map(u=>u.path);if(n.path){let u=Il(n.path,o);return r.includes(u)?{docsDir:o,targets:[u],planCount:r.length}:{docsDir:o,targets:[],planCount:r.length,reason:`"${u}" isn't in the docs plan (.d360-writer/plan.json).`}}let s=_l(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=Ml(e,o,s);if(c.length===0){let u=n.scope?` under "${n.scope}"`:"";return{docsDir:o,targets:[],planCount:r.length,reason:`All planned articles${u} are already written. Use /write <path> to rewrite one.`}}return{docsDir:o,targets:c,planCount:r.length}}async function Nr(e,t){if(!Mr(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let n=jl(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}=Yn(e),{docsDir:c,targets:u,reason:g}=Qn(t.cwd,n,{scope:o,path:r});if(u.length===0)return console.log(g?L(g):L("Nothing to write.")),{kind:"continue"};let m=Al(u),{model:w,forced:j}=El(t.cwd,"standard");if(!s&&!!!r){let v=Rl({files:Xn(t.cwd,n,u),op:"write",model:w});o&&console.log(f(`Scope: ${o} (${u.length} pending).`));for(let $ of Jn(m,v,Gn))console.log($);return console.log(f(`Model: ${w}${j?" (forced)":" \u2014 authoring; /model to override"}.`)),console.log(f(`Run /write${o?` --scope ${o}`:" --all"} --run to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Writing ${u.length} article${u.length===1?"":"s"} across ${m.length} partition${m.length===1?"":"s"} (\u2264${Gn} agents at once) on ${w}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let v of Dl({cwd:t.cwd,partitions:m,promptFor:$=>Vn($,c),concurrency:Gn,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:w}))if(v.type==="partition_status")v.status==="running"?console.log(f(` \u25B8 ${v.label} \u2014 writing\u2026`)):v.status==="done"?console.log(D(` \u2713 ${v.label}`)):console.log(x(` \u2717 ${v.label}`));else if(v.type==="run_done"){console.log("");let $=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let E of Kn(v.results,m,$))console.log(v.ok?D(E):L(E))}}catch(v){console.log(x(`Write run failed: ${v.message}`))}return console.log(""),{kind:"continue"}}H();import{search as Nl}from"@inquirer/prompts";import{findByName as Ll,getSession as Ol,listSessions as Ul,relativeTime as Or}from"document360-engine";async function Ur(e,t){let n=Ul(t.cwd).filter(o=>o.uuid!==t.currentUuid());if(n.length===0)return console.log(f("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=Ll(t.cwd,o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${o}".`)),Lr(n),{kind:"continue"})}if(!process.stdin.isTTY)return Lr(n),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let o=await t.withPausedInput(()=>Nl({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return n.filter(u=>!c||u.name.toLowerCase().includes(c)||u.firstPrompt.toLowerCase().includes(c)).map(u=>({name:`${u.name} ${Or(u.updatedAt)}`,value:u.uuid,description:u.firstPrompt.slice(0,100)}))}})),r=Ol(o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function Lr(e){console.log("");for(let t of e.slice(0,15))console.log(` ${R(t.name)} ${f(Or(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}H();import{renameSession as Wl}from"document360-engine";function Zn(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 Wr(e,t){let n=Zn(e.join(" "));if(!n)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let o=t.currentUuid();return o?(Wl(o,n)?console.log(D(`\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 Fr,readProjectConfig as Fl,writeProjectConfig as Hl}from"document360-engine";H();function eo(e,t,n){if(!t)return"Usage: /profile add <name> [environment]";let o=Fl(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 Fr().includes(r)?(o.profiles={...o.profiles,[t]:{connection:{environment:r},production:!1}},Hl(o,e),null):`Unknown environment "${r}". Known: ${Fr().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Hr(e,t){let n=e[0];if(!n)return Wt(t.cwd),{kind:"continue"};if(n==="add"){let o=eo(t.cwd,e[1],e[2]);return o?(console.log(x(o)),{kind:"continue"}):(console.log(D(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${R(`/profile ${e[1]}`)} then ${R("/login")}`),{kind:"continue"})}return Ft(t.cwd,n),Ht(t.cwd,n),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}H();import{select as Bl}from"@inquirer/prompts";import{readProjectConfig as ql,readUserConfig as Br,resolveModelSetting as to,writeUserConfig as qr}from"document360-engine";var ge=[{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 Kt(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),n=ge.findIndex(o=>o.value!==null&&(o.value===t||o.label.toLowerCase()===t||t.includes(o.label.toLowerCase())));return n>=0?n:0}function zl(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 wt(e,t){let n=()=>{let s=to(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=Br();return s.defaultModel?(delete s.defaultModel,qr(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${to(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}}qr({...Br(),defaultModel:t});let o=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=ql(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 zr(e,t){let n=e[0]?.trim();if(!n){let c=to(t.cwd);if(!process.stdin.isTTY)return console.log(`${ve("Model:")} ${R(c.model??"Claude Code default")}`),console.log(f(` source: ${zl(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let u=Kt(c),g;try{g=await t.withPausedInput(()=>Bl({message:`Select model (current: ${c.model??"Claude Code default"})`,default:ge[u].value,choices:ge.map((b,v)=>({name:`${b.label}${v===u?" \u2714":""}`,value:b.value,description:b.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:m,changed:w,effective:j}=wt(t.cwd,g??"default");for(let b of m)console.log(b.startsWith("\u26A0")?L(b):b.startsWith("\u2713")?D(b):f(b));return w&&await t.setModel(j),{kind:"continue"}}let{lines:o,changed:r,effective:s}=wt(t.cwd,n);for(let c of o)console.log(c.startsWith("\u26A0")?L(c):c.startsWith("\u2713")?D(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}Qt();async function Jr(e,t){return await t.withPausedInput(()=>Ze(t.cwd)),{kind:"clear"}}H();import{resolveActiveProfile as na}from"document360-engine";async function Kr(e,t){let n=!1;try{n=na(t.cwd).production}catch{}return n?(console.log(L("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(f("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}H();var Qr=async(e,t)=>{try{await t.withPausedInput(()=>Ut({}))}catch(n){console.log(x(`Login failed: ${n.message}`))}return{kind:"continue"}};import{existsSync as Zr}from"node:fs";import{isAbsolute as oa,join as ra,resolve as sa}from"node:path";import{readProjectConfig as ia,screenshotPlaceholderIds as la}from"document360-engine";var es=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function oo(e){let t=!e.includes("--no-setup"),n=e.filter(r=>r!=="--no-setup");if(n[0]==="--list")return{mode:"list",scope:n[1]?es(n[1]):void 0};let o=n[0];return!o||o==="--all"?{mode:"all",setup:t}:/[\\/]/.test(o)||o.endsWith(".md")?{mode:"scope",scope:es(o),setup:t}:{mode:"single",id:o}}function ro(e,t){let n=ia(e),o=n?.captureDir??"user-docs/_capture",r=n?.outputDir??"user-docs/_screenshots",s=(c,u)=>oa(c)?ra(c,u):sa(e,c,u);return la(e,{scope:t}).map(({id:c,file:u})=>{let g=Zr(s(r,`${c}.png`))?"captured":Zr(s(o,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:u,state:g}})}var aa={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},ca={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function so(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(u=>u.file===s))r.push(` ${aa[c.state]} ${c.id.padEnd(34)} ${ca[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function ts(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(`
51
59
  `),"","For each: locate its <!-- SCREENSHOT --> block in user-docs/**/*.md; read the product source for","EXACT routes + stable data-testid selectors (never guess); enter the prepared context via","captureScope(); guard data prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts","(skip-with-TODO if a stable selector is missing). Do NOT run other skills. Report the specs you","wrote and any TODO data-testids."].join(`
52
- `)}async function Kt(e,t){let o=Vo(e);if(o.mode==="list"){let c=t?.cwd??process.cwd();for(let d of Ko(Jo(c,o.scope),o.scope))console.log(d);return{kind:"continue"}}if(o.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${o.id}\`.`,"Locate its <!-- SCREENSHOT --> block in user-docs/**/*.md, then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${o.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
53
- `),display:`/screenshot ${o.id}`};let r=[`Run the emit-screenshot-spec skill to (re)generate the document360-capture spec for ${o.mode==="scope"?`every <!-- SCREENSHOT --> placeholder in articles under ${o.scope}`:"every <!-- SCREENSHOT --> placeholder across user-docs/**/*.md"}.`,"Follow that skill exactly for each: read the SCREENSHOT block; read the product source for EXACT routes","and stable data-testid selectors (never guess); enter the prepared context via captureScope(); guard data",`prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts.${o.setup?" When done, run capture-setup-checklist to refresh CAPTURE-SETUP.md.":""} Report specs written, TODO data-testids, and data to stage.`].join(`
54
- `),s=o.mode==="scope"?`/screenshot ${o.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as Ul,readFileSync as Wl}from"node:fs";import{isAbsolute as Fl,join as Hl,resolve as Bl}from"node:path";import{readProjectConfig as ql}from"document360-engine";async function mt(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across user-docs/**/*.md, read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
55
- `),display:"/capture-setup"}}var Fr="-".repeat(64);function Qo(e){let t="user-docs/_capture";try{let w=ql(e);w?.captureDir&&(t=w.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let o=Fl(t)?Hl(t,"CAPTURE-TESTID-REQUESTS.json"):Bl(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!Ul(o))return null;let n;try{let w=JSON.parse(Wl(o,"utf8").replace(/^/,""));n=Array.isArray(w?.requests)?w.requests:[]}catch{return null}if(n.length===0)return null;let r=new Set(n.map(w=>w.file).filter(Boolean)).size,s=n.length,c=`${s} data-testid attribute${s===1?"":"s"}`,d=`${r} source file${r===1?"":"s"}`,h=`${t}/CAPTURE-TESTID-REQUESTS.md`,g=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${d}.`,"\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.","",Fr,`Read ${h} and ${g}. 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.",Fr]}H();import{existsSync as zl,rmSync as Gl}from"node:fs";import{basename as Hr,resolve as Br}from"node:path";import{input as Yl}from"@inquirer/prompts";import{readProjectConfig as Xl}from"document360-engine";var Vl=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function Zo(e){let t=null;try{t=Xl(e)}catch{t=null}let o=s=>s?.replace(/\\/g,"/").replace(/\/+$/,""),n=o(t?.docsDir)??"user-docs",r=new Set([n]);for(let s of[o(t?.captureDir),o(t?.outputDir)])s&&s!==n&&!s.startsWith(`${n}/`)&&r.add(s);return[...r,...Vl].filter(s=>zl(Br(e,s)))}function en(e,t){let o=[],n=[];for(let r of t)try{Gl(Br(e,r),{recursive:!0,force:!0}),o.push(r)}catch(s){n.push({path:r,error:s.message})}return{removed:o,failed:n}}function tn(e,t){return t.length===0?["Nothing to reset \u2014 no d360-writer files found in this repo."]:["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(o=>` \u2022 ${o}`),"","Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${Hr(e)}`]}async function qr(e,t){let o=Zo(t.cwd);for(let d of tn(t.cwd,o))console.log(o.length===0?f(d):d);if(o.length===0)return{kind:"continue"};let n=Hr(t.cwd);if((await t.withPausedInput(()=>Yl({message:`Type "${n}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==n)return console.log(N("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=en(t.cwd,o);console.log(D(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let d of c)console.log(x(` \u2717 ${d.path}: ${d.error}`));return{kind:"continue"}}var zr={help:To,"?":To,clear:Gn,exit:Po,quit:Po,init:Qn,mcp:Ht,publish:rr,audit:zt,scope:ir,sync:fr,convert:$r,resume:Cr,rename:Sr,profile:Pr,model:jr,doctor:ft,workspace:Mr,"allow-prod":Nr,login:Lr,screenshot:Kt,"capture-setup":mt,reset:qr};function Gr(e){let t=e.trim();if(!t.startsWith("/"))return null;let o=t.slice(1).split(/\s+/),n=(o[0]??"").toLowerCase();return n?{name:n,args:o.slice(1)}:null}H();var Jl={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"},Xr=160,nn=200,Vr=40;function Z(e,t){let o=e.replace(/\s+/g," ").trim();return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function Yr(e){let t=Object.entries(e).filter(([,n])=>n!=null&&n!=="");if(t.length===0)return null;let o=t.slice(0,4).map(([n,r])=>`${n}: ${Z(typeof r=="string"?r:JSON.stringify(r),Vr)}`);return t.length>4&&o.push("\u2026"),Z(o.join(", "),Xr)}var Pe=e=>typeof e=="string"&&e?e:null,on=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Kl(e){let t=Pe(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);if(o.length<2)return null;let n=o[o.length-2],r=n.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?n:r.map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}function Ql(e,t){let o=(n,r)=>({title:`Document360: ${n}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let n=Pe(t.title);if(!n)return null;let r=Kl(t.local_path);return o("Create article",`"${Z(n,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let n=Pe(t.title),r=on(t.article_id);return o("Update article",n?`"${Z(n,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return o("Fork article (new draft)",on(t.article_id)?`id ${on(t.article_id)}\u2026`:null);case"d360_publish_article":{let n=t.version_number;return o("Publish article LIVE",typeof n=="number"?`v${n}`:null)}case"d360_unpublish_article":return o("Unpublish article",null);case"d360_create_category":return o("Create category",Pe(t.name)?`"${Z(Pe(t.name),60)}"`:null);case"d360_upload_drive_file":{let n=Pe(t.file_path);return o("Upload image",n?Z(n.replace(/\\/g,"/").split("/").pop()??n,60):null)}case"d360_sync_status":return o("Check sync status",null);default:return null}}function Qt(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let g=Ql(c,t);if(g)return g}let d=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${d}`,sep:" ",arg:Yr(t)}}let o=Jl[e],n=o?t[o]:void 0;return typeof n=="string"&&n?{title:e,sep:"",arg:Z(n,Xr)}:{title:e,sep:"",arg:Yr(t)}}function Zl(e){if(e===null||typeof e!="object")return typeof e=="string"?e:null;let t=e;for(let o of["name","title","slug","id"])if(typeof t[o]=="string"&&t[o])return t[o];return null}function ea(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let o=t.map(Zl).filter(r=>r!==null),n=`${t.length} item${t.length===1?"":"s"}`;return o.length===0?[n]:[n,...o.map(r=>Z(r,nn))]}if(t!==null&&typeof t=="object"){let o=Object.entries(t).filter(([,n])=>n!==null&&(typeof n=="string"||typeof n=="number"||typeof n=="boolean")).slice(0,6).map(([n,r])=>`${n}: ${Z(String(r),Vr)}`);return o.length>0?[Z(o.join(" \xB7 "),nn)]:null}return null}function ta(e){let t=Pe(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);return o.length>1?o.slice(1).join("/"):t}function oa(e,t,o){switch(e){case"d360_create_article":{let n=ta(t?.local_path);return[`\u2713 draft created${n?` \xB7 ${n}`:""}`]}case"d360_update_article":return["\u2713 draft updated"];case"d360_fork_article":return["\u2713 forked to a new draft"];case"d360_publish_article":return["\u2713 PUBLISHED LIVE \u2014 visible to readers"];case"d360_unpublish_article":return["\u2713 reverted to draft (removed from readers)"];case"d360_create_category":{let n=Pe(t?.name);return[`\u2713 category created${n?` \xB7 "${n}"`:""}`]}case"d360_upload_drive_file":{let n=o.match(/https?:\/\/\S+/);return[`\u2713 uploaded${n?` \xB7 ${Z(n[0],120)}`:""}`]}default:return null}}function Zt(e,t=4,o,n){let r=e.replace(/\r\n/g,`
56
- `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(o?.startsWith("mcp__document360__")){let c=oa(o.slice(18),n,r);if(c)return{lines:c,hidden:0}}let s=ea(r)??r.split(`
57
- `);return{lines:s.slice(0,t).map(c=>Z(c,nn)),hidden:Math.max(0,s.length-t)}}function eo(e,t,o,n="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${n}/${o}`}function to(e,t){if(typeof e.article_id=="string"&&e.article_id)return e.article_id;try{let o=JSON.parse(t),r=(Array.isArray(o)?o[0]:o)?.id;return typeof r=="string"&&r?r:null}catch{return null}}function oo(e){try{let t=JSON.parse(e),n=(Array.isArray(t)?t[0]:t)?.url;return typeof n=="string"&&/^https?:\/\//.test(n)?n:null}catch{return null}}var ma=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function ga(e,t,o,n){if(ma.test(e))try{let r=Zr(n),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=to(t,o),d=oo(o);e.endsWith("publish_article")&&d&&console.log(S(` \u2B95 Live: ${d}`)),c&&s&&console.log(S(` \u2B95 Preview: ${eo(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function es(e=process.cwd(),t="auto",o){let n=ra(t);n.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${S("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${S('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),ha(e,o),n.kind==="subscription"&&(n.stored?console.log(f(" Using your Claude subscription (no API key set).")):console.log(f(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let r=!1,s=rn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},d=na({input:process.stdin,output:process.stdout}),h=[],g=null,w=!1;d.on("line",j=>{if(g){let P=g;g=null,P(j)}else h.push(j)}),d.on("close",()=>{if(w=!0,g){let j=g;g=null,j(null)}});function A(){return h.length>0?Promise.resolve(h.shift()):w?Promise.resolve(null):(process.stdout.write(S("> ")),new Promise(j=>{g=j}))}let $={cwd:e,profileName:o,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=rn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async j=>s.setModel(j),withPausedInput:async j=>{d.pause();try{return await j()}finally{d.resume()}}};try{for(;;){let j=await A();if(j===null)break;let P=j.trim();if(P){if(P.startsWith("/")){let M=Gr(P);if(!M)continue;let q=zr[M.name];if(!q){console.log(x(`Unknown command: /${M.name}`)),console.log(f("Type /help for the list."));continue}let z=await q(M.args,$);if(z.kind==="exit")break;if(z.kind==="clear"){$.restartAgent();continue}if(z.kind==="allow-prod"){r=!0,$.restartAgent(),console.log(D("\u2713 Production writes authorized for this session.")),console.log("");continue}if(z.kind==="resume"){s.close(),s=rn({cwd:e,resume:z.uuid,profileName:o,allowProdWrites:r});let ee=sa(z.uuid);c={uuid:z.uuid,firstPrompt:ee?.firstPrompt??null,titleFired:!0},Qr(z.uuid),console.log(D(`\u2713 Resumed "${z.name}"`)),console.log("");continue}z.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=z.display??z.prompt),await Kr(s,z.prompt,n,c,e));continue}c.firstPrompt||(c.firstPrompt=P),await Kr(s,P,n,c,e)}}}finally{s.close(),d.close()}}function ha(e,t){console.log(""),console.log(st("document360-writer")),console.log(f(` cwd: ${e}`));let o=ua(e);console.log(f(` model: ${o.model??"auto (engine right-sizes per task)"}${o.model?` (${o.source})`:""}`)),console.log(ka(e,t)),da(e)||console.log(N(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(f(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function ka(e,t){try{let o=Zr(e,t),n=o.production?N(" \u26A0 PRODUCTION"):"",r=fa(o.name);if(!r)return f(` Document360: profile "${o.name}"${n} \u2014 not logged in (d360-writer login)`);let s={...Jr(r.idToken)??{},...Jr(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return pa(r)&&!r.refreshToken?N(` Document360: profile "${o.name}"${n} \u2014 session expired (d360-writer login)`):f(` Document360: ${c} \xB7 profile "${o.name}"${n}`)}catch(o){return f(` Document360: ${o.message.split(".")[0]}`)}}function ya(){console.error(""),console.error(`Sign in with your Claude subscription: run ${S("claude")} once, then retry.`),console.error(` (No Claude Code? ${S("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${S("https://console.anthropic.com/settings/keys")}`)}function wa(e,t,o){e.uuid=t;let n=new Date().toISOString();aa({uuid:t,name:la(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:o,firstPrompt:e.firstPrompt??"",createdAt:n,updatedAt:n})}function xa(e,t){e.titleFired=!0;let o=e.uuid,n=e.firstPrompt;!o||!n||ca(n,t).then(r=>{r&&ia(o,r)}).catch(()=>{})}async function Kr(e,t,o,n,r){let s=new Map;for await(let c of e.send(t))$a(c,n,r,o,s)}function $a(e,t,o,n,r){switch(e.type){case"session":t.uuid||wa(t,e.sessionId,o);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=Qt(e.name,e.input);s&&(process.stdout.write(`
60
+ `)}async function Zt(e,t){let n=oo(e);if(n.mode==="list"){let c=t?.cwd??process.cwd();for(let u of so(ro(c,n.scope),n.scope))console.log(u);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 user-docs/**/*.md, then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${n.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
61
+ `),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 user-docs/**/*.md"}.`,"Follow that skill exactly for each: read the SCREENSHOT block; read the product source for EXACT routes","and stable data-testid selectors (never guess); enter the prepared context via captureScope(); guard data",`prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts.${n.setup?" When done, run capture-setup-checklist to refresh CAPTURE-SETUP.md.":""} Report specs written, TODO data-testids, and data to stage.`].join(`
62
+ `),s=n.mode==="scope"?`/screenshot ${n.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as ua,readFileSync as da}from"node:fs";import{isAbsolute as pa,join as fa,resolve as ma}from"node:path";import{readProjectConfig as ga}from"document360-engine";async function xt(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across user-docs/**/*.md, read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
63
+ `),display:"/capture-setup"}}var ns="-".repeat(64);function io(e){let t="user-docs/_capture";try{let w=ga(e);w?.captureDir&&(t=w.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let n=pa(t)?fa(t,"CAPTURE-TESTID-REQUESTS.json"):ma(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!ua(n))return null;let o;try{let w=JSON.parse(da(n,"utf8").replace(/^/,""));o=Array.isArray(w?.requests)?w.requests:[]}catch{return null}if(o.length===0)return null;let r=new Set(o.map(w=>w.file).filter(Boolean)).size,s=o.length,c=`${s} data-testid attribute${s===1?"":"s"}`,u=`${r} source file${r===1?"":"s"}`,g=`${t}/CAPTURE-TESTID-REQUESTS.md`,m=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${u}.`,"\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.","",ns,`Read ${g} and ${m}. 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.",ns]}H();import{existsSync as ha,rmSync as ka}from"node:fs";import{basename as os,resolve as rs}from"node:path";import{input as ya}from"@inquirer/prompts";import{readProjectConfig as wa}from"document360-engine";var $a=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function lo(e){let t=null;try{t=wa(e)}catch{t=null}let n=s=>s?.replace(/\\/g,"/").replace(/\/+$/,""),o=n(t?.docsDir)??"user-docs",r=new Set([o]);for(let s of[n(t?.captureDir),n(t?.outputDir)])s&&s!==o&&!s.startsWith(`${o}/`)&&r.add(s);return[...r,...$a].filter(s=>ha(rs(e,s)))}function ao(e,t){let n=[],o=[];for(let r of t)try{ka(rs(e,r),{recursive:!0,force:!0}),n.push(r)}catch(s){o.push({path:r,error:s.message})}return{removed:n,failed:o}}function co(e,t){return t.length===0?["Nothing to reset \u2014 no d360-writer files found in this repo."]:["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(n=>` \u2022 ${n}`),"","Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${os(e)}`]}async function ss(e,t){let n=lo(t.cwd);for(let u of co(t.cwd,n))console.log(n.length===0?f(u):u);if(n.length===0)return{kind:"continue"};let o=os(t.cwd);if((await t.withPausedInput(()=>ya({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}=ao(t.cwd,n);console.log(D(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let u of c)console.log(x(` \u2717 ${u.path}: ${u.error}`));return{kind:"continue"}}var is={help:Tn,"?":Tn,clear:or,exit:Rn,quit:Rn,init:cr,mcp:zt,publish:gr,audit:Xt,scope:kr,sync:Cr,convert:Dr,write:Nr,resume:Ur,rename:Wr,profile:Hr,model:zr,doctor:$t,workspace:Jr,"allow-prod":Kr,login:Qr,screenshot:Zt,"capture-setup":xt,reset:ss};function ls(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}H();var xa={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"},cs=160,po=200,us=40;function re(e,t){let n=e.replace(/\s+/g," ").trim();return n.length<=t?n:n.slice(0,t-1)+"\u2026"}function as(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}: ${re(typeof r=="string"?r:JSON.stringify(r),us)}`);return t.length>4&&n.push("\u2026"),re(n.join(", "),cs)}var Ee=e=>typeof e=="string"&&e?e:null,uo=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function ba(e){let t=Ee(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 va(e,t){let n=(o,r)=>({title:`Document360: ${o}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let o=Ee(t.title);if(!o)return null;let r=ba(t.local_path);return n("Create article",`"${re(o,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let o=Ee(t.title),r=uo(t.article_id);return n("Update article",o?`"${re(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",Ee(t.name)?`"${re(Ee(t.name),60)}"`:null);case"d360_upload_drive_file":{let o=Ee(t.file_path);return n("Upload image",o?re(o.replace(/\\/g,"/").split("/").pop()??o,60):null)}case"d360_sync_status":return n("Check sync status",null);default:return null}}function en(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let m=va(c,t);if(m)return m}let u=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${u}`,sep:" ",arg:as(t)}}let n=xa[e],o=n?t[n]:void 0;return typeof o=="string"&&o?{title:e,sep:"",arg:re(o,cs)}:{title:e,sep:"",arg:as(t)}}function Ca(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 Pa(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let n=t.map(Ca).filter(r=>r!==null),o=`${t.length} item${t.length===1?"":"s"}`;return n.length===0?[o]:[o,...n.map(r=>re(r,po))]}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}: ${re(String(r),us)}`);return n.length>0?[re(n.join(" \xB7 "),po)]:null}return null}function Sa(e){let t=Ee(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);return n.length>1?n.slice(1).join("/"):t}function Ta(e,t,n){switch(e){case"d360_create_article":{let o=Sa(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=Ee(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 ${re(o[0],120)}`:""}`]}default:return null}}function tn(e,t=4,n,o){let r=e.replace(/\r\n/g,`
64
+ `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(n?.startsWith("mcp__document360__")){let c=Ta(n.slice(18),o,r);if(c)return{lines:c,hidden:0}}let s=Pa(r)??r.split(`
65
+ `);return{lines:s.slice(0,t).map(c=>re(c,po)),hidden:Math.max(0,s.length-t)}}function nn(e,t,n,o="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${o}/${n}`}function on(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 rn(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 Ua=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Wa(e,t,n,o){if(Ua.test(e))try{let r=ms(o),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=on(t,n),u=rn(n);e.endsWith("publish_article")&&u&&console.log(R(` \u2B95 Live: ${u}`)),c&&s&&console.log(R(` \u2B95 Preview: ${nn(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function gs(e=process.cwd(),t="auto",n){let o=Aa(t);o.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${R("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${R('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),Fa(e,n),o.kind==="subscription"&&(o.stored?console.log(f(" Using your Claude subscription (no API key set).")):console.log(f(" 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},u=Ra({input:process.stdin,output:process.stdout}),g=[],m=null,w=!1;u.on("line",v=>{if(m){let $=m;m=null,$(v)}else g.push(v)}),u.on("close",()=>{if(w=!0,m){let v=m;m=null,v(null)}});function j(){return g.length>0?Promise.resolve(g.shift()):w?Promise.resolve(null):(process.stdout.write(R("> ")),new Promise(v=>{m=v}))}let b={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 v=>s.setModel(v),withPausedInput:async v=>{u.pause();try{return await v()}finally{u.resume()}}};try{for(;;){let v=await j();if(v===null)break;let $=v.trim();if($){if($.startsWith("/")){let E=ls($);if(!E)continue;let q=is[E.name];if(!q){console.log(x(`Unknown command: /${E.name}`)),console.log(f("Type /help for the list."));continue}let G=await q(E.args,b);if(G.kind==="exit")break;if(G.kind==="clear"){b.restartAgent();continue}if(G.kind==="allow-prod"){r=!0,b.restartAgent(),console.log(D("\u2713 Production writes authorized for this session.")),console.log("");continue}if(G.kind==="resume"){s.close(),s=fo({cwd:e,resume:G.uuid,profileName:n,allowProdWrites:r});let ie=ja(G.uuid);c={uuid:G.uuid,firstPrompt:ie?.firstPrompt??null,titleFired:!0},fs(G.uuid),console.log(D(`\u2713 Resumed "${G.name}"`)),console.log("");continue}G.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=G.display??G.prompt),await ps(s,G.prompt,o,c,e));continue}c.firstPrompt||(c.firstPrompt=$),await ps(s,$,o,c,e)}}}finally{s.close(),u.close()}}function Fa(e,t){console.log(""),console.log(pt("document360-writer")),console.log(f(` cwd: ${e}`));let n=Ma(e);console.log(f(` model: ${n.model??"auto (engine right-sizes per task)"}${n.model?` (${n.source})`:""}`)),console.log(Ha(e,t)),Na(e)||console.log(L(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(f(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function Ha(e,t){try{let n=ms(e,t),o=n.production?L(" \u26A0 PRODUCTION"):"",r=Oa(n.name);if(!r)return f(` Document360: profile "${n.name}"${o} \u2014 not logged in (d360-writer login)`);let s={...ds(r.idToken)??{},...ds(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return La(r)&&!r.refreshToken?L(` Document360: profile "${n.name}"${o} \u2014 session expired (d360-writer login)`):f(` Document360: ${c} \xB7 profile "${n.name}"${o}`)}catch(n){return f(` Document360: ${n.message.split(".")[0]}`)}}function Ba(){console.error(""),console.error(`Sign in with your Claude subscription: run ${R("claude")} once, then retry.`),console.error(` (No Claude Code? ${R("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${R("https://console.anthropic.com/settings/keys")}`)}function qa(e,t,n){e.uuid=t;let o=new Date().toISOString();Ia({uuid:t,name:Da(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:n,firstPrompt:e.firstPrompt??"",createdAt:o,updatedAt:o})}function za(e,t){e.titleFired=!0;let n=e.uuid,o=e.firstPrompt;!n||!o||_a(o,t).then(r=>{r&&Ea(n,r)}).catch(()=>{})}async function ps(e,t,n,o,r){let s=new Map;for await(let c of e.send(t))Ga(c,o,r,n,s)}function Ga(e,t,n,o,r){switch(e.type){case"session":t.uuid||qa(t,e.sessionId,n);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=en(e.name,e.input);s&&(process.stdout.write(`
58
66
 
59
- `),console.log(`${D("\u25CF")} ${we(s.title)}${s.arg!==null?V(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=Le(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=d=>d===1?"":"s";console.log(V(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let d of s.lines)console.log(d);s.hidden>0&&console.log(f(` \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=Zt(e.output,4,e.isError?void 0:s.name,s.input),d=e.isError?x:V;c.lines.forEach((h,g)=>console.log(d((g===0?" \u23BF ":" ")+h))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||ga(s.name,s.input,e.output,o);break}case"result":process.stdout.write(`
60
- `),console.log(f(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(Qr(t.uuid),t.titleFired||xa(t,o));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),n.kind==="subscription"&&e.kind==="auth"&&ya();break}}import{render as gc}from"ink";import{resolveAuth as hc}from"document360-engine";import{useCallback as G,useEffect as Ae,useMemo as lo,useRef as Y,useState as U}from"react";import{Box as X,Text as v,useApp as Ea,useInput as Da,useStdout as Ia}from"ink";import{existsSync as hs,readFileSync as ks,readdirSync as Ts}from"node:fs";import{basename as ao,isAbsolute as _a,join as ht}from"node:path";import{createSession as ys,loginPkce as Ma,toStoredTokens as Na,saveTokens as La,getAccessToken as Oa,resolveActiveProfile as pe,resolveProjectId as Ua,getArticle as Wa,decodeJwtClaims as Ze,isExpired as Re,loadTokens as Ue,setTitle as Fa,slugify as Ha,touchSession as ln,upsertSession as Ba,generateTitle as ws,findByName as qa,listSessions as za,renameSession as Ga,suggestNextAction as Ya,readProjectConfig as se,writeProjectConfig as Xa,resolveModelSetting as un,loadProfileMap as xs,applyPull as Va,computeSyncStatus as an,planPull as Ja,inventoryRepo as Ka,knownEnvironments as Qa,resolveEnvironment as Za,planPartitions as ec,partitionEvenly as tc,screenshotPlaceholderIds as oc,trackedArticlePaths as nc,runPartitioned as $s,estimateBulkCost as rc,resolveModelForOperation as bs}from"document360-engine";import{existsSync as ba,mkdirSync as va,readFileSync as Ca,writeFileSync as Sa}from"node:fs";import{join as ts}from"node:path";import{writerDir as Ta}from"document360-engine";function os(e){return ts(Ta(e),".sessions")}function ns(e,t){return ts(os(e),`${t}.json`)}function rs(e,t,o){try{va(os(e),{recursive:!0});let n=o.filter(r=>r.kind!=="banner");Sa(ns(e,t),JSON.stringify({v:1,items:n}),"utf8")}catch{}}function sn(e,t){try{let o=ns(e,t);if(!ba(o))return[];let n=JSON.parse(Ca(o,"utf8"));return Array.isArray(n.items)?n.items:[]}catch{return[]}}var Pa=e=>`\x1B]0;${e}\x07`,Ra=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function ss(e){process.stdout.isTTY&&process.stdout.write(e)}function gt(e){ss(Pa(e))}function no(e){ss(Ra(e))}var ro=["\xB7 ","\xB7\xB7 ","\xB7\xB7\xB7"];Jt();var Aa=/^(?:\d+[.)]|[-*•])\s+/;function is(e,t,o=4){let n=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
61
- `)){let c=s.trim().replace(Aa,""),d=c.match(/^`([^`]+)`$/);d&&(c=d[1].trim());let h=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!h||!n.has(h[1].toLowerCase()))continue;let g=`/${h[1].toLowerCase()}${h[2]?` ${h[2]}`:""}`;if(r.includes(g)||r.push(g),r.length>=o)break}return r}H();var ja=/\[Pasted text #\d+ \+\d+ lines?\]/g;function ls(e){return e.replace(/\r\n?/g,`
62
- `)}function as(e){return e.includes(`
63
- `)||e.length>200}function cs(e,t){let o=t.split(`
64
- `).length;return`[Pasted text #${e} +${o} line${o===1?"":"s"}]`}function us(e,t){return e.replace(ja,o=>t.get(o)??o)}function ds(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function so(e,t){let o=Math.max(1,t),n=[],r=0;for(let s of e.split(`
65
- `)){if(s.length===0)n.push({start:r,end:r});else for(let c=0;c<s.length;c+=o)n.push({start:r+c,end:r+Math.min(c+o,s.length)});r+=s.length+1}return n.length>0?n:[{start:0,end:0}]}function io(e,t){let o=0;for(let n=0;n<e.length&&e[n].start<=t;n++)o=n;return o}function ps(e,t,o){let n=io(e,t),r=n+o;if(r<0||r>=e.length)return t;let s=Math.min(t,e[n].end)-e[n].start;return Math.min(e[r].start+s,e[r].end)}function fs(e,t,o){let n=e[io(e,t)];return o==="start"?n.start:n.end}function ms(e,t,o){let n=Math.max(1,o),r=e.split(`
66
- `),s=0;for(let c=r.length-1;c>=0;c--){let d=r[c];if(s+=Math.max(1,Math.ceil(d.length/n)),s>=t){let h=s-t;return{text:[h>0?d.slice(h*n):d,...r.slice(c+1)].join(`
67
- `),truncated:c>0||h>0}}}return{text:e,truncated:!1}}function gs(e){let t=!1,o=0,n=0;for(let r of e.split(`
68
- `)){let s=n+r.length;/^\s*```/.test(r)?t=!t:!t&&r.trim()===""&&s<e.length&&(o=s+1),n=s+1}return o}import{Fragment as cn,jsx as C,jsxs as W}from"react/jsx-runtime";var sc={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function ic(e,t,o,n){let r=o.kind==="api"?"API key":o.kind==="subscription"?"subscription":"not configured",s=un(e),c=se(e),d=(c?.docsDir??"user-docs").replace(/\/+$/,""),h=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${d}/ + config`,g={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:sc[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:h};if(c===null)return g;try{let w=pe(e,n);g.profile=w.name,g.apiUrl=w.connection.apiUrl,g.prod=w.production,g.project=w.project.projectId??"(chosen at login)";let A=Ue(w.name);if(A){let $={...Ze(A.idToken)??{},...Ze(A.accessToken)??{}},j=$.email??$.preferred_username??"signed in";Re(A)?A.refreshToken&&(g.who=j,g.loggedOut=!1,g.sessionHint="session expired \u2014 refreshing\u2026"):(g.who=j,g.loggedOut=!1,g.sessionHint=`session valid until ${new Date(A.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return g}function lc(e,t){try{let o=pe(e,t),n=Ue(o.name);if(!n)return{text:`profile "${o.name}" \u2014 not logged in (/login)`,prod:o.production};let r={...Ze(n.idToken)??{},...Ze(n.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return Re(n)&&!n.refreshToken?{text:`profile "${o.name}" \u2014 session expired (/login)`,prod:o.production}:{text:`${s} \xB7 profile "${o.name}"`,prod:o.production}}catch(o){return{text:o.message.split(".")[0],prod:!1}}}var vs=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],uo=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],ac="Ask me to write or update an article\u2026";function co({ch:e,dim:t}){let[o,n]=U(!0);return Ae(()=>{let r=setInterval(()=>n(s=>!s),530);return()=>clearInterval(r)},[]),C(v,{inverse:o,color:t&&!o?"gray":void 0,children:e})}var cc=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Cs(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var uc=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Ss(e){try{return Ts(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function dc(e,t){let o=t??[];return o.length===0?["src","api","services","packages","modules"].some(n=>Ss(ht(e,n))):o.some(n=>!n.includes("/")&&!n.endsWith(".md")&&Ss(ht(e,n)))}function pc({startTime:e,chars:t}){let[o,n]=U(0);Ae(()=>{let h=setInterval(()=>n(g=>g+1),120);return()=>clearInterval(h)},[]);let r=uo[o%uo.length],s=vs[Math.floor(o/16)%vs.length],c=Math.floor((Date.now()-e)/1e3),d=Math.round(t/4);return W(X,{children:[C(v,{color:B,children:` ${r} ${s}\u2026 `}),C(v,{color:"gray",children:`(${dt(c)} \xB7 ~${d} tokens \xB7 esc to interrupt)`})]})}var fc=12e4;function mc({p:e}){let[t,o]=U(0);Ae(()=>{let h=setInterval(()=>o(g=>g+1),150);return()=>clearInterval(h)},[]);let n=uo[t%uo.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),d=Date.now()-e.lastAt>=fc;return W(X,{flexDirection:"column",children:[W(X,{children:[C(v,{color:B,children:` ${n} ${e.verb??"Converting"} `}),C(v,{color:"gray",children:`${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${dt(r)} \xB7 esc to stop`})]}),e.active.length>0&&C(v,{dimColor:!0,children:` \u25B8 ${e.active.join(", ")} \xB7 last activity ${s}s ago`}),d&&C(v,{color:"yellow",children:` \u26A0 no activity for ${dt(s)} \u2014 press esc to stop if it's wedged`})]})}function Ps({cwd:e,auth:t,profileName:o,version:n}){let{exit:r}=Ea(),[s,c]=U(o),[d,h]=U(null),[g,w]=U({text:"",pos:0}),A=g.text,$=G(a=>{w(l=>{let i=typeof a=="function"?a(l.text):a;return{text:i,pos:i.length}})},[]),j=G(a=>{w(l=>({text:l.text.slice(0,l.pos)+a+l.text.slice(l.pos),pos:l.pos+a.length}))},[]),P=G(()=>{w(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),i=ds(l)??l.slice(0,-1);return{text:i+a.text.slice(a.pos),pos:i.length}})},[]),[M,q]=U(!1),[z,ee]=U(!1),[ie,$e]=U(0),[le,ae]=U(null),[et,J]=U([]),kt=Y(0),[Es,po]=U(!1),[Ds,yt]=U(0),tt=Y(0),[ot,We]=U(0),[Fe,wt]=U(null),fn=Y(new Map),Is=Y(0),He=Y([]),te=Y(null);te.current===null&&(te.current=ys({cwd:e,profileName:s,allowProdWrites:!1}));let fe=Y({uuid:null,firstPrompt:null,titleFired:!1}),xt=Y(new Map),$t=Y(!1),fo=Y([]),be=Y([]),mo=Y(null),[mn,Be]=U(null),[je,Ee]=U(null),[me,qe]=U(null),[ve,ze]=U(null),[K,Ge]=U(null),[ge,Ye]=U(null),[oe,De]=U(null),[Q,Xe]=U(null),[gn,go]=U([]),Ie=Y([]),bt=Y(!1),_e=Y(null),[hn,ce]=U(null),vt=Y(!1),Ct=Y(null),[ne,Me]=U(null),[kn,St]=U(0),[yn,Tt]=U(0),[_s,wn]=U(0),ho=lo(()=>{try{return un(e).model??"auto"}catch{return null}},[e,_s]),{stdout:ue}=Ia(),[,Ms]=U(0),xn=Y(`${ue.columns??80}x${ue.rows??24}`),Ce=G(()=>Math.max(20,(ue.columns??80)-1),[ue]),u=G(a=>{He.current.push(a),console.log(Lo(a,Ce()))},[Ce]);Ae(()=>{let a=`d360-writer \xB7 ${mn??ao(e)}`;if(!M){no(1),gt(a);return}no(3);let l=0;gt(`${ro[0]} ${a}`);let i=setInterval(()=>{l=(l+1)%ro.length,gt(`${ro[l]} ${a}`)},450);return()=>clearInterval(i)},[M,e,mn]),Ae(()=>()=>no(0),[]),Ae(()=>{if(u({kind:"banner",info:ic(e,n,t,s)}),!se(e)){u({kind:"note",tone:"info",text:`Welcome! This repo isn't set up for d360-writer yet \u2014 three steps and you're writing docs:
67
+ `),console.log(`${D("\u25CF")} ${ve(s.title)}${s.arg!==null?Q(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=We(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=u=>u===1?"":"s";console.log(Q(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let u of s.lines)console.log(u);s.hidden>0&&console.log(f(` \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=tn(e.output,4,e.isError?void 0:s.name,s.input),u=e.isError?x:Q;c.lines.forEach((g,m)=>console.log(u((m===0?" \u23BF ":" ")+g))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||Wa(s.name,s.input,e.output,n);break}case"result":process.stdout.write(`
68
+ `),console.log(f(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(fs(t.uuid),t.titleFired||za(t,n));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),o.kind==="subscription"&&e.kind==="auth"&&Ba();break}}import{render as Uc}from"ink";import{resolveAuth as Wc}from"document360-engine";import{useCallback as X,useEffect as Ie,useMemo as un,useRef as V,useState as W}from"react";import{Box as J,Text as P,useApp as nc,useInput as oc,useStdout as rc}from"ink";import{existsSync as Es,readFileSync as Ds,readdirSync as Fs}from"node:fs";import{basename as dn,isAbsolute as sc,join as vt}from"node:path";import{createSession as Is,loginPkce as ic,toStoredTokens as lc,saveTokens as ac,getAccessToken as cc,resolveActiveProfile as he,resolveProjectId as uc,getArticle as dc,decodeJwtClaims as ot,isExpired as De,loadTokens as Fe,setTitle as pc,slugify as fc,touchSession as ho,upsertSession as mc,generateTitle as _s,findByName as gc,listSessions as hc,renameSession as kc,suggestNextAction as yc,readProjectConfig as se,writeProjectConfig as wc,resolveModelSetting as xo,loadProfileMap as Ms,applyPull as $c,computeSyncStatus as ko,planPull as xc,inventoryRepo as bc,knownEnvironments as vc,resolveEnvironment as Cc,planPartitions as Ns,partitionEvenly as Pc,screenshotPlaceholderIds as Sc,trackedArticlePaths as Tc,runPartitioned as yo,estimateBulkCost as Ls,resolveModelForOperation as wo,readDocsPlan as Rc}from"document360-engine";import{existsSync as Ya,mkdirSync as Xa,readFileSync as Va,writeFileSync as Ja}from"node:fs";import{join as hs}from"node:path";import{writerDir as Ka}from"document360-engine";function ks(e){return hs(Ka(e),".sessions")}function ys(e,t){return hs(ks(e),`${t}.json`)}function ws(e,t,n){try{Xa(ks(e),{recursive:!0});let o=n.filter(r=>r.kind!=="banner");Ja(ys(e,t),JSON.stringify({v:1,items:o}),"utf8")}catch{}}function mo(e,t){try{let n=ys(e,t);if(!Ya(n))return[];let o=JSON.parse(Va(n,"utf8"));return Array.isArray(o.items)?o.items:[]}catch{return[]}}var Qa=e=>`\x1B]0;${e}\x07`,Za=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function $s(e){process.stdout.isTTY&&process.stdout.write(e)}function bt(e){$s(Qa(e))}function sn(e){$s(Za(e))}var ln=["\xB7 "," \xB7"],go="\u{1F7E3}";Qt();var ec=/^(?:\d+[.)]|[-*•])\s+/;function xs(e,t,n=4){let o=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
69
+ `)){let c=s.trim().replace(ec,""),u=c.match(/^`([^`]+)`$/);u&&(c=u[1].trim());let g=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!g||!o.has(g[1].toLowerCase()))continue;let m=`/${g[1].toLowerCase()}${g[2]?` ${g[2]}`:""}`;if(r.includes(m)||r.push(m),r.length>=n)break}return r}H();var tc=/\[Pasted text #\d+ \+\d+ lines?\]/g;function bs(e){return e.replace(/\r\n?/g,`
70
+ `)}function vs(e){return e.includes(`
71
+ `)||e.length>200}function Cs(e,t){let n=t.split(`
72
+ `).length;return`[Pasted text #${e} +${n} line${n===1?"":"s"}]`}function Ps(e,t){return e.replace(tc,n=>t.get(n)??n)}function Ss(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function an(e,t){let n=Math.max(1,t),o=[],r=0;for(let s of e.split(`
73
+ `)){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 cn(e,t){let n=0;for(let o=0;o<e.length&&e[o].start<=t;o++)n=o;return n}function Ts(e,t,n){let o=cn(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 Rs(e,t,n){let o=e[cn(e,t)];return n==="start"?o.start:o.end}function As(e,t,n){let o=Math.max(1,n),r=e.split(`
74
+ `),s=0;for(let c=r.length-1;c>=0;c--){let u=r[c];if(s+=Math.max(1,Math.ceil(u.length/o)),s>=t){let g=s-t;return{text:[g>0?u.slice(g*o):u,...r.slice(c+1)].join(`
75
+ `),truncated:c>0||g>0}}}return{text:e,truncated:!1}}function js(e){let t=!1,n=0,o=0;for(let r of e.split(`
76
+ `)){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 $o,jsx as S,jsxs as F}from"react/jsx-runtime";var Ac={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function jc(e,t,n,o){let r=n.kind==="api"?"API key":n.kind==="subscription"?"subscription":"not configured",s=xo(e),c=se(e),u=(c?.docsDir??"user-docs").replace(/\/+$/,""),g=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${u}/ + config`,m={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:Ac[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 m;try{let w=he(e,o);m.profile=w.name,m.apiUrl=w.connection.apiUrl,m.prod=w.production,m.project=w.project.projectId??"(chosen at login)";let j=Fe(w.name);if(j){let b={...ot(j.idToken)??{},...ot(j.accessToken)??{}},v=b.email??b.preferred_username??"signed in";De(j)?j.refreshToken&&(m.who=v,m.loggedOut=!1,m.sessionHint="session expired \u2014 refreshing\u2026"):(m.who=v,m.loggedOut=!1,m.sessionHint=`session valid until ${new Date(j.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return m}function Ec(e,t){try{let n=he(e,t),o=Fe(n.name);if(!o)return{text:`profile "${n.name}" \u2014 not logged in (/login)`,prod:n.production};let r={...ot(o.idToken)??{},...ot(o.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return De(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 Os=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],fn=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],Dc="Ask me to write or update an article\u2026";function pn({ch:e,dim:t}){let[n,o]=W(!0);return Ie(()=>{let r=setInterval(()=>o(s=>!s),530);return()=>clearInterval(r)},[]),S(P,{inverse:n,color:t&&!n?"gray":void 0,children:e})}var Ic=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Us(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var _c=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Ws(e){try{return Fs(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function Mc(e,t){let n=t??[];return n.length===0?["src","api","services","packages","modules"].some(o=>Ws(vt(e,o))):n.some(o=>!o.includes("/")&&!o.endsWith(".md")&&Ws(vt(e,o)))}function Nc({startTime:e,chars:t}){let[n,o]=W(0);Ie(()=>{let g=setInterval(()=>o(m=>m+1),120);return()=>clearInterval(g)},[]);let r=fn[n%fn.length],s=Os[Math.floor(n/16)%Os.length],c=Math.floor((Date.now()-e)/1e3),u=Math.round(t/4);return F(J,{children:[S(P,{color:z,children:` ${r} ${s}\u2026 `}),S(P,{color:"gray",children:`(${yt(c)} \xB7 ~${u} tokens \xB7 esc to interrupt)`})]})}var Lc=12e4;function Oc({p:e}){let[t,n]=W(0);Ie(()=>{let g=setInterval(()=>n(m=>m+1),150);return()=>clearInterval(g)},[]);let o=fn[t%fn.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),u=Date.now()-e.lastAt>=Lc;return F(J,{flexDirection:"column",children:[F(J,{children:[S(P,{color:z,children:` ${o} ${e.verb??"Converting"} `}),S(P,{color:"gray",children:`${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${yt(r)} \xB7 esc to stop`})]}),e.active.length>0&&S(P,{dimColor:!0,children:` \u25B8 ${e.active.join(", ")} \xB7 last activity ${s}s ago`}),u&&S(P,{color:"yellow",children:` \u26A0 no activity for ${yt(s)} \u2014 press esc to stop if it's wedged`})]})}function Hs({cwd:e,auth:t,profileName:n,version:o}){let{exit:r}=nc(),[s,c]=W(n),[u,g]=W(null),[m,w]=W({text:"",pos:0}),j=m.text,b=X(a=>{w(l=>{let i=typeof a=="function"?a(l.text):a;return{text:i,pos:i.length}})},[]),v=X(a=>{w(l=>({text:l.text.slice(0,l.pos)+a+l.text.slice(l.pos),pos:l.pos+a.length}))},[]),$=X(()=>{w(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),i=Ss(l)??l.slice(0,-1);return{text:i+a.text.slice(a.pos),pos:i.length}})},[]),[E,q]=W(!1),[G,ie]=W(!1),[de,Pe]=W(0),[pe,fe]=W(null),[rt,K]=W([]),Ct=V(0),[Gs,mn]=W(!1),[Ys,st]=W(0),He=V(0),[it,Be]=W(0),[qe,Pt]=W(null),Co=V(new Map),Xs=V(0),ze=V([]),le=V(null);le.current===null&&(le.current=Is({cwd:e,profileName:s,allowProdWrites:!1}));let ke=V({uuid:null,firstPrompt:null,titleFired:!1}),St=V(new Map),lt=V(!1),gn=V([]),Se=V([]),hn=V(null),[Po,Ge]=W(null),[_e,Me]=W(null),[ye,Ye]=W(null),[Te,Xe]=W(null),[ne,Ve]=W(null),[we,Je]=W(null),[ae,Ne]=W(null),[oe,Ke]=W(null),[So,kn]=W([]),Le=V([]),Tt=V(!1),$e=V(null),[To,Z]=W(null),Rt=V(!1),At=V(null),[ce,Oe]=W(null),[Ro,at]=W(0),[Ao,ct]=W(0),[Vs,jo]=W(0),yn=un(()=>{try{return xo(e).model??"auto"}catch{return null}},[e,Vs]),{stdout:me}=rc(),[,Js]=W(0),Eo=V(`${me.columns??80}x${me.rows??24}`),Re=X(()=>Math.max(20,(me.columns??80)-1),[me]),d=X(a=>{ze.current.push(a),console.log(On(a,Re()))},[Re]);Ie(()=>{let a=`d360-writer \xB7 ${Po??dn(e)}`;if(!E){sn(0),bt(`${go} ${a}`);return}sn(3);let l=0;bt(`${ln[0]} ${a}`);let i=setInterval(()=>{l=(l+1)%ln.length,bt(`${ln[l]} ${a}`)},400);return()=>clearInterval(i)},[E,e,Po]),Ie(()=>()=>sn(0),[]),Ie(()=>{if(d({kind:"banner",info:jc(e,o,t,s)}),!se(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:
69
77
  1. /init \u2014 pick your Document360 environment & scaffold config
70
78
  2. /login signs in to that environment
71
79
  3. /workspace picks where articles go
72
- Press 1 to start.`}),J(["/init"]);return}try{let a=pe(e,s),l=Ue(a.name);l&&Re(l)&&l.refreshToken?Oa({profile:a.name,connection:a.connection}).then(()=>{u({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),$e(i=>i+1)}).catch(()=>{u({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),J(["/login"])}):(!l||Re(l))&&(u({kind:"note",tone:"warn",text:`Profile "${a.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),J(["/login"]))}catch{}try{let a=se(e),l=pe(e,s),i=Ue(l.name);a&&i&&!Re(i)&&dc(e,a.authoritativeSourceFiles)&&(u({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),J(["/scope"]))}catch{}},[]),Ae(()=>{let a=null,l=null,i=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let m=`${ue.columns??80}x${ue.rows??24}`;m!==xn.current&&(xn.current=m,Ms(p=>p+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+xr(He.current,Ce())+"\x1B[?2026l")},80))},400)};return ue.on("resize",i),()=>{a&&clearTimeout(a),l&&clearTimeout(l),ue.off("resize",i)}},[ue,Ce]);let Ne=Math.max(20,(ue.columns??80)-1),$n=lo(()=>lc(e,s),[e,s,ie]),ko=lo(()=>{let a=se(e);if(!a)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};try{let m=pe(e,s),p=Ue(m.name);if(!(!!p&&!(Re(p)&&!p.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${m.name}")\u2026`,isSetup:!0};if(!m.project.workspaceId)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}let l=ht(e,(a.docsDir??"user-docs").replace(/\/+$/,""));return(()=>{try{return hs(l)&&Ts(l).length>0}catch{return!1}})()?{text:ac,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}},[e,s,ie]),nt=qn(A),bn=nt.length>0&&!M,yo=d!==null?ms(d,8,Ne):null,Pt=oe?oe.paths.filter(a=>!oe.query||a.toLowerCase().includes(oe.query.toLowerCase())).slice(0,8):[],Rt=ne?ne.sessions.filter(a=>{let l=ne.query.toLowerCase();return!l||a.name.toLowerCase().includes(l)||a.firstPrompt.toLowerCase().includes(l)}).slice(0,8):[],he=G((a,l)=>{te.current?.close(),te.current=ys({cwd:e,resume:a,profileName:l??s,allowProdWrites:$t.current}),a||(fe.current={uuid:null,firstPrompt:null,titleFired:!1},Be(null)),St(0),Tt(0)},[e,s]),vn=G((a,l,i)=>{if(cc.test(a))try{let m=pe(e,s),p=typeof l.project_id=="string"&&l.project_id||m.project.projectId,k=to(l,i),y=[],E=oo(i);a.endsWith("publish_article")&&E&&y.push(`Live: ${E}`),k&&p&&y.push(`Preview: ${eo(m.connection.portalUrl,p,k,m.project.languageCode??"en")}`),y.length>0&&u({kind:"link",lines:y})}catch{}},[e,s,u]),ke=G(async(a,l)=>{po(!0),ae(null),J([]);let i=++kt.current;u({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let m=fe.current;m.firstPrompt||(m.firstPrompt=l?.display??a),tt.current=Date.now(),yt(0),q(!0),xt.current.clear();let p="",k="",y=null,E=()=>{y||(y=setTimeout(()=>{y=null,h(k.length>0?k:null)},60))},I=()=>{y&&clearTimeout(y),y=null,h(null)},L=()=>{if(k.trim()){let T=k.trimEnd();u({kind:"assistant",text:T})}k="",I()};try{for await(let T of te.current.send(a))if(T.type==="session"){if(!m.uuid){m.uuid=T.sessionId;let _=new Date().toISOString(),b=Ha(m.firstPrompt??"session");Ba({uuid:T.sessionId,name:b,renamed:!1,titled:!1,cwd:e,firstPrompt:m.firstPrompt??"",createdAt:_,updatedAt:_}),Be(b)}}else if(T.type==="text"){k+=T.delta,p+=T.delta;let _=gs(k);if(_>0){let b=k.slice(0,_).trimEnd();b&&u({kind:"assistant",text:b}),k=k.slice(_)}E(),yt(b=>b+T.delta.length)}else if(T.type==="tool"){let _=Qt(T.name,T.input);_&&(L(),u({kind:"tool",title:_.title,sep:_.sep,arg:_.arg}),xt.current.set(T.id,{name:T.name,input:T.input}))}else if(T.type==="article_diff"){let _=Le(T.oldContent,T.newContent,Ce());_&&(L(),u({kind:"diff",added:_.added,removed:_.removed,lines:_.lines,hidden:_.hidden}))}else if(T.type==="tool_result"){T.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(T.output)&&(vt.current=!0);let _=xt.current.get(T.id);if(_){xt.current.delete(T.id),L();let b=Zt(T.output,4,T.isError?void 0:_.name,_.input);u({kind:"tool-result",lines:b.lines,hidden:b.hidden,isError:T.isError}),T.isError||vn(_.name,_.input,T.output)}}else if(T.type==="result"){L(),St(b=>b+T.outputTokens),Tt(b=>b+T.costUsd),u({kind:"done",seconds:Math.round((Date.now()-tt.current)/1e3),tokens:T.outputTokens,costUsd:T.costUsd,ok:T.ok});let _=T.ok?is(p,lt.map(b=>b.name)):[];if(_.length>0?J(_):T.ok&&p.trim()&&Ya(a,p,e).then(b=>{b&&kt.current===i&&ae(b)}).catch(()=>{}),m.uuid&&(ln(m.uuid),!m.titleFired)){m.titleFired=!0;let b=m.uuid,R=m.firstPrompt;R&&ws(R,e).then(O=>{O&&(Fa(b,O),Be(O))}).catch(()=>{})}}else T.type==="error"&&(L(),T.kind==="auth"&&(vt.current=!0),u({kind:"note",text:`agent error: ${T.message}`,tone:"error"}))}finally{q(!1),I(),fe.current.uuid&&rs(e,fe.current.uuid,He.current),vt.current&&(vt.current=!1,Ct.current=l?.display??a,J(["/login"])),bt.current&&(bt.current=!1,Ie.current.length>0&&(u({kind:"note",tone:"info",text:`(${Ie.current.length} queued message(s) discarded)`}),Ie.current=[],go([])),u({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,u,vn,Ce]),wo=G(a=>{let l=Ue(a);if(!l||Re(l)&&!l.refreshToken)return null;let i={...Ze(l.idToken)??{},...Ze(l.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),xo=G((a,l)=>{if(l){let i=se(e);i&&(i.defaultProfile=a,Xa(i,e))}c(a),he(void 0,a),u({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${a}"${l?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),wo(a)||(u({kind:"note",tone:"warn",text:`Profile "${a}" is not signed in \u2014 do you want to log in now? (press 1)`}),J(["/login"])),$e(i=>i+1)},[e,u,he,wo]),Cn=G((a,l,i)=>{It(e,a,l,i.id,i.name),u({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),he(),xs(e,a)||(u({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),ae("analyze this repo and propose a docs structure"))},[e,u,he]),At=G(()=>{let a=be.current[0];if(!a)return;u({kind:"note",tone:"info",text:`\u25CF ${a.title} (${a.path})`});for(let i of a.notes)u({kind:"note",tone:"warn",text:`\u26A0 ${i}`});a.overwritesLocalChanges&&u({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let l=Le(a.oldContent,a.newContent,Ce());u(l?{kind:"diff",added:l.added,removed:l.removed,lines:l.lines,hidden:l.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),u({kind:"note",tone:"info",text:`Write ${a.path}? (y/n \u2014 anything else cancels)`})},[u,Ce]),Sn=G(a=>{if(be.current.length===0)return!1;let l=a.trim().toLowerCase();if(l==="y"||l==="yes"){let i=be.current.shift();try{Va({cwd:e,profileName:s},i),u({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),be.current.length===0&&J(m=>m.length>0?m:["/sync"])}catch(m){u({kind:"note",tone:"error",text:`Pull failed: ${m.message}`})}}else if(l==="n"||l==="no"){let i=be.current.shift();u({kind:"note",tone:"info",text:`Skipped ${i.path}.`})}else{let i=be.current.length;return be.current=[],u({kind:"note",tone:"info",text:`Pull cancelled (${i} article(s) left untouched).`}),!0}return At(),!0},[e,s,At,u]),Tn=G(a=>{let l=mo.current;if(!l)return!1;if(mo.current=null,a.trim()!==l.repoName)return u({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:i,failed:m}=en(e,l.targets);u({kind:"note",tone:m.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${i.length} item${i.length===1?"":"s"}. The repo is back to its original state.`});for(let p of m)u({kind:"note",tone:"error",text:` \u2717 ${p.path}: ${p.error}`});return!0},[e,u]),Pn=G(async a=>{let l=a.slice(1).trim().split(/\s+/),i=(l[0]??"").toLowerCase(),m=l.slice(1);switch(po(!0),i){case"help":u({kind:"note",tone:"info",text:Wt().join(`
73
- `)});return;case"exit":case"quit":te.current?.close(),r();return;case"clear":he(),He.current=[],po(!1),ae(null),kt.current++,u({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let p;try{p=pe(e,s)}catch(k){u({kind:"note",tone:"error",text:k.message});return}u({kind:"note",tone:"info",text:`Profile "${p.name}" \u2192 ${p.connection.name} (${p.connection.apiUrl})${p.production?" \u26A0 PRODUCTION":""}`}),ee(!0);try{let k=await Ma(p.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},E=>u({kind:"note",tone:"info",text:E})),y=Na(p.name,k);La(y),So(y,p.name,E=>u({kind:"note",tone:"info",text:E})),u({kind:"note",tone:"ok",text:`\u2713 Logged in to "${p.name}" as ${Mt(y)}`}),p.project.workspaceId||(u({kind:"note",tone:"info",text:"Next: pick the workspace your articles publish to."}),J(["/workspace"])),Ct.current&&(ae(Ct.current),Ct.current=null,u({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(k){u({kind:"note",tone:"error",text:`Login failed: ${k.message}`})}finally{ee(!1),$e(k=>k+1)}return}case"allow-prod":{let p=!1;try{p=pe(e,s).production}catch{}if(!p){u({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}$t.current=!0,he(),u({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let p=zo(m.join(" ")),k=fe.current.uuid;if(!k){u({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!p){u({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let y=fe.current.firstPrompt??"";ws(y,e).then(E=>{E?(ae(`/rename ${E}`),u({kind:"note",tone:"info",text:`Suggestion: "${E}" \u2014 press tab to accept, or type /rename <your name>.`})):u({kind:"note",tone:"info",text:"Usage: /rename <name>"})}).catch(()=>u({kind:"note",tone:"info",text:"Usage: /rename <name>"}));return}Ga(k,p),Be(p),gt(`d360-writer \xB7 ${p}`),u({kind:"note",tone:"ok",text:`Session renamed to "${p}".`});return}case"profile":{let p=m[0],k=se(e);if(!p){let y=Object.entries(k?.profiles??{});if(y.length===0){u({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let E=y.map(([T,_])=>({name:T,env:_.connection?.environment??"custom",prod:_.production===!0,who:wo(T)})),I=s??k?.defaultProfile,L=Math.max(0,E.findIndex(T=>T.name===I));qe({cursor:L,current:L,rows:E});return}if(p==="add"){let y=Go(e,m[1],m[2]);if(y){u({kind:"note",tone:"error",text:y});return}u({kind:"note",tone:"ok",text:`\u2713 Profile "${m[1]}" created (environment: ${m[2]??m[1]}).`}),xo(m[1],!1);return}if(!k?.profiles?.[p]){u({kind:"note",tone:"error",text:`Unknown profile "${p}". Create it: /profile add ${p} <environment>`});return}xo(p,!0);return}case"doctor":await ft(m,{cwd:e});return;case"mcp":await Ht(m);return;case"init":{if(se(e)){u({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let p=Qa().map(k=>({name:k,apiUrl:Za(k).apiUrl}));ze({cursor:0,rows:p});return}case"resume":{let p=m.join(" ").trim(),k=za(e).filter(I=>I.uuid!==fe.current.uuid);if(!p){if(!k.length){u({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Me({query:"",cursor:0,sessions:k});return}let y=qa(e,p);if(!y){u({kind:"note",tone:"error",text:`No session matches "${p}".`});return}he(y.uuid),fe.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},Be(y.name),ln(y.uuid),He.current=[];let E=sn(e,y.uuid);for(let I of E)u(I);u({kind:"note",tone:"ok",text:E.length?`Resumed "${y.name}" \u2014 restored ${E.length} message(s); continue where you left off.`:`Resumed "${y.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let p=m.join(" ").trim(),k;try{k=await Dt(e,s)}catch(E){u({kind:"note",tone:"error",text:`Could not list workspaces: ${E.message}`});return}if(!p){let E=k.workspaces.map(L=>({id:L.id,name:L.name??L.id,type:L.workspace_type}));if(E.length===0){u({kind:"note",tone:"info",text:"No workspaces in this project."});return}let I=Math.max(0,E.findIndex(L=>L.id===k.current));Ge({cursor:I,current:I,rows:E,profile:k.profile,projectId:k.projectId,environment:k.environment});return}let y=Co(k.workspaces,p);if(!y){u({kind:"note",tone:"error",text:`No workspace matches "${p}".`});return}Cn(k.profile,k.projectId,y);return}case"publish":{if(m[0]&&m[0]!=="--all"){await ke(qt(m[0]),{display:`/publish ${m[0]}`,echoDisplay:!0});return}let p=m[0]==="--all";u({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let k=await an({cwd:e,profileName:s}),y=Bt(k.entries);if(y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let I=k.counts["unknown-base"]??0;I>0&&u({kind:"note",tone:"info",text:`(${I} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(p){await ke(ct(y.map(I=>I.path)),{display:"/publish --all",echoDisplay:!0});return}let E=y.length>1?[{path:"--all",label:`publish all ${y.length} candidates in one run`},...y]:y;Ye({cursor:0,rows:E})}catch(k){u({kind:"note",tone:"error",text:`Could not compute sync status: ${k.message}`}),u({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let p=m.join(" ").trim();if(!p){let y=[];try{y=Object.keys(xs(e,pe(e,s).name)?.articles??{})}catch{}if(y.length===0){u({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}De({query:"",cursor:0,paths:y});return}let k=_a(p)?p:ht(e,p);if(hs(k)){try{u({kind:"preview",name:ao(k),text:Cs(ks(k,"utf8"))})}catch(y){u({kind:"note",tone:"error",text:`Could not read ${k}: ${y.message}`})}return}if(uc.test(p)){try{let y=pe(e,s),E={profile:y.name,connection:y.connection},I=y.project.projectId??Ua(E),L=await Wa(E,I,p);u({kind:"preview",name:L.title??p,text:L.content??"*(article has no content)*"})}catch(y){u({kind:"note",tone:"error",text:`Could not fetch article: ${y.message}`})}return}u({kind:"note",tone:"error",text:`"${p}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let p=m[0]?.trim();if(!p){let I=Vt(un(e));Ee({cursor:I,current:I});return}let{lines:k,changed:y,effective:E}=pt(e,p);for(let I of k)u({kind:"note",tone:I.startsWith("\u26A0")?"warn":I.startsWith("\u2713")?"ok":"info",text:I});y&&(wn(I=>I+1),te.current?.setModel(E));return}case"convert":{if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:p,run:k}=Uo(m),y=nc(e,s);if(y.length===0){u({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let E=Wo(y,p);if(E.length===0){u({kind:"note",tone:"error",text:`No tracked articles under "${p}". (${y.length} are tracked overall.)`});return}let I=ec(E),L=3,T=`/convert${p?` --scope ${p}`:""} --run`,_=bs(e,"light");if(!k){let R=rc({files:Fo(e,E),op:"convert",model:_.model}),O=p?`Scope: ${p} (${E.length} of ${y.length} tracked)
74
- `:"",re=`
75
- Model: ${_.model}${_.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;u({kind:"note",tone:"info",text:O+Bo(I,R,L).join(`
76
- `)+re}),J([T]);return}tt.current=Date.now(),yt(0),q(!0);let b=new AbortController;_e.current=b,ce({total:I.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Converting ${E.length} articles across ${I.length} partitions (\u2264${L} agents at once) on ${_.model}\u2026 (esc to stop)`});try{for await(let R of $s({cwd:e,partitions:I,promptFor:Ho,concurrency:L,profileName:s,allowProdWrites:$t.current,model:_.model,signal:b.signal}))if(R.type==="partition_status")R.status==="running"?(ce(O=>O&&{...O,active:[...O.active,R.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${R.label} \u2014 converting\u2026`})):(ce(O=>O&&{...O,active:O.active.filter(re=>re!==R.label),done:O.done+1,lastAt:Date.now()}),u({kind:"note",tone:R.status==="done"?"ok":"error",text:` ${R.status==="done"?"\u2713":"\u2717"} ${R.label}`}));else if(R.type==="partition_event")ce(O=>{if(!O)return O;let re={...O,lastAt:Date.now()};return R.event.type==="tool"?re.tools=O.tools+1:R.event.type==="text"&&(re.chars=O.chars+R.event.delta.length),re});else if(R.type==="run_done"){Tt(ye=>ye+R.totalCostUsd),St(ye=>ye+R.results.reduce((bo,vo)=>bo+vo.outputTokens,0));let O=R.aborted?"Stopped. ":"",re=t.kind==="api"?"api":"subscription";u({kind:"note",tone:R.aborted?"warn":R.ok?"ok":"warn",text:O+qo(R.results,I,re).join(`
77
- `)})}}catch(R){u({kind:"note",tone:"error",text:`Convert run failed: ${R.message}`})}finally{_e.current=null,ce(null),q(!1)}return}case"sync":{let p=(m[0]??"status").toLowerCase();try{if(p==="status"){u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let k=await an({cwd:e,profileName:s});u({kind:"note",tone:"info",text:Yt(k).join(`
78
- `)});return}if(p==="pull"){let k=m[1];if(!k){u({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let y;if(k==="--all"){if(u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),y=(await an({cwd:e,profileName:s})).entries.filter(L=>L.status==="remote-ahead"&&L.path).map(L=>L.path),y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is remote-ahead \u2014 no pulls needed. (Conflicts are never bulk-pulled; pull them one by one.)"});return}}else y=[k.replace(/\\/g,"/")];let E=[];for(let I of y)E.push(await Ja({cwd:e,profileName:s,relPath:I}));be.current=E,At();return}u({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${p} \u2014 use /sync or /sync pull <path>|--all.`})}catch(k){u({kind:"note",tone:"error",text:`Sync failed: ${k.message}`})}return}case"scope":{if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=Ka(e);if(p.length===0){u({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}Xe({cursor:0,rows:p.map(k=>({...k,checked:k.recommended}))});return}case"audit":case"capture-setup":{let k=await(i==="audit"?zt:mt)(m,void 0);if(k.kind==="forward-to-agent"&&k.prompt&&await ke(k.prompt,{display:k.display,echoDisplay:!0}),i==="capture-setup"){let y=Qo(e);y&&u({kind:"note",tone:"info",text:y.join(`
79
- `)})}return}case"screenshot":{let p=Vo(m);if(p.mode==="list"){if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}u({kind:"note",tone:"info",text:Ko(Jo(e,p.scope),p.scope).join(`
80
- `)});return}if(p.mode==="single"){let b=await Kt(m);b.kind==="forward-to-agent"&&b.prompt&&await ke(b.prompt,{display:b.display,echoDisplay:!0});return}if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let k=p.mode==="scope"?p.scope:void 0,y=oc(e,{scope:k}).map(b=>b.id);if(y.length===0){u({kind:"note",tone:"info",text:k?`No screenshot placeholders under ${k}.`:"No screenshot placeholders found in the docs."});return}let E=6,I=tc(y,3),L=bs(e,"standard");tt.current=Date.now(),yt(0),q(!0);let T=new AbortController;_e.current=T,ce({verb:"Authoring",total:I.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Authoring ${y.length} screenshot spec${y.length===1?"":"s"} across ${I.length} partition${I.length===1?"":"s"} (\u2264${E} agents at once) on ${L.model}${L.forced?" (forced)":""}\u2026 (esc to stop)`});let _=!1;try{for await(let b of $s({cwd:e,partitions:I,promptFor:Wr,concurrency:E,profileName:s,allowProdWrites:$t.current,model:L.model,signal:T.signal}))if(b.type==="partition_status")b.status==="running"?(ce(R=>R&&{...R,active:[...R.active,b.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${b.label} \u2014 authoring\u2026`})):(ce(R=>R&&{...R,active:R.active.filter(O=>O!==b.label),done:R.done+1,lastAt:Date.now()}),u({kind:"note",tone:b.status==="done"?"ok":"error",text:` ${b.status==="done"?"\u2713":"\u2717"} ${b.label}`}));else if(b.type==="partition_event")ce(R=>{if(!R)return R;let O={...R,lastAt:Date.now()};return b.event.type==="tool"?O.tools=R.tools+1:b.event.type==="text"&&(O.chars=R.chars+b.event.delta.length),O});else if(b.type==="run_done"){Tt(ye=>ye+b.totalCostUsd),St(ye=>ye+b.results.reduce((bo,vo)=>bo+vo.outputTokens,0));let R=b.results.filter(ye=>ye.ok).length,O=b.aborted?"Stopped. ":"";_=!b.aborted&&p.setup;let re=b.aborted?"":_?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";u({kind:"note",tone:b.aborted?"warn":b.ok?"ok":"warn",text:`${O}Authored specs \u2014 ${R}/${b.results.length} batches ok.${re}`})}}catch(b){u({kind:"note",tone:"error",text:`Spec authoring failed: ${b.message}`})}finally{_e.current=null,ce(null),q(!1)}if(_){let b=await mt();b.kind==="forward-to-agent"&&b.prompt&&await ke(b.prompt,{display:b.display,echoDisplay:!0});let R=Qo(e);R&&u({kind:"note",tone:"info",text:R.join(`
81
- `)})}return}case"reset":{let p=Zo(e);if(p.length===0){u({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}mo.current={repoName:ao(e),targets:p},u({kind:"note",tone:"warn",text:tn(e,p).join(`
82
- `)});return}default:u({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,At,u,he,ke]),jt=G(a=>{let l=(a??A).trim();if($(""),wt(null),We(0),l.startsWith("/")){let m=k=>k.trim().split(/\s+/).slice(1).join(" "),p=m(l);J(k=>k.filter(y=>y.trim()!==l&&!(p&&m(y)===p)))}if(!l||Tn(l)||Sn(l))return;fo.current.push(l);let i=us(l,fn.current);l.startsWith("/")?Pn(i):ke(i,{display:l})},[A,Tn,Sn,Pn,ke]),Rn=G(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=ls(a);if(as(l)){let i=cs(++Is.current,l);fn.current.set(i,l),j(i)}else j(l);return}j(a)},[j]),Ve=Math.max(10,Ne-6),An=lo(()=>so(g.text,Ve),[g.text,Ve]),$o=G(a=>w(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),Et=G(a=>w(l=>({...l,pos:ps(so(l.text,Ve),l.pos,a)})),[Ve]),rt=G(a=>w(l=>({...l,pos:fs(so(l.text,Ve),l.pos,a)})),[Ve]),Ns=["\x1B[H","\x1B[1~","\x1BOH"],Ls=["\x1B[F","\x1B[4~","\x1BOF"],jn=G((a,l)=>l.leftArrow?($o(-1),!0):l.rightArrow?($o(1),!0):a&&Ns.includes(a)?(rt("start"),!0):a&&Ls.includes(a)?(rt("end"),!0):l.ctrl&&a==="a"?(rt("start"),!0):l.ctrl&&a==="e"?(rt("end"),!0):!1,[$o,rt]);return Da((a,l)=>{if(l.ctrl&&a==="c"){te.current?.close(),r();return}if(!z){if(M){if(l.escape){if(_e.current){_e.current.signal.aborted||(u({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),_e.current.abort());return}bt.current||(bt.current=!0,u({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),te.current?.interrupt());return}if(l.return){let i=A.trim();if(!i)return;Ie.current.push(i),go([...Ie.current]),$("");return}if(jn(a,l))return;if(l.upArrow){Et(-1);return}if(l.downArrow){Et(1);return}if(l.backspace||l.delete){P();return}a&&!l.ctrl&&!l.meta&&Rn(a);return}if(je){if(l.upArrow){Ee(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ee(i=>i&&{...i,cursor:Math.min(de.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=de.length){Ee(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=de[je.cursor];Ee(null);let{lines:m,changed:p,effective:k}=pt(e,i.value??"default");for(let y of m)u({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});p&&(wn(y=>y+1),te.current?.setModel(k));return}if(a==="s"){let i=de[je.cursor];Ee(null),te.current?.setModel(i.value??void 0),u({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(l.escape){Ee(null);return}return}if(me){if(l.upArrow){qe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){qe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=me.rows.length){qe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=me.rows[me.cursor];qe(null),xo(i.name,l.return===!0);return}if(l.escape){qe(null);return}return}if(ve){if(l.upArrow){ze(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){ze(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ve.rows.length){ze(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ve.rows[ve.cursor].name;if(ze(null),!Jn(e,i).created){u({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}u({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let p=!1;try{let k=Ue(i);p=!!k&&!(Re(k)&&!k.refreshToken)}catch{}p?(u({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),J(["/workspace"])):(u({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),J(["/login"])),$e(k=>k+1);return}if(l.escape){ze(null);return}return}if(K){if(l.upArrow){Ge(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ge(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=K.rows.length){Ge(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=K.rows[K.cursor],{profile:m,projectId:p}=K;Ge(null),Cn(m,p,i);return}if(l.escape){Ge(null);return}return}if(Q){if(l.upArrow){Xe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Xe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){Xe(i=>i&&{...i,rows:i.rows.map((m,p)=>p===i.cursor?{...m,checked:!m.checked}:m)});return}if(l.return){let i=Q.rows.filter(m=>m.checked).map(m=>m.path);if(Xe(null),i.length===0){u({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}Ao(e,i),u({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let m of i)u({kind:"note",tone:"info",text:` ${m}`});u({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(l.escape){Xe(null);return}return}if(ge){if(l.upArrow){Ye(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ye(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ge.rows.length){Ye(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ge.rows[ge.cursor],m=ge.rows.filter(p=>p.path!=="--all").map(p=>p.path);Ye(null),ke(i.path==="--all"?ct(m):qt(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){Ye(null);return}return}if(oe){if(l.escape){De(null);return}if(l.upArrow){De(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){De(i=>i&&{...i,cursor:Math.min(Math.max(0,Pt.length-1),i.cursor+1)});return}if(l.return){let i=Pt[oe.cursor];if(i){De(null);try{u({kind:"preview",name:ao(i),text:Cs(ks(ht(e,i),"utf8"))})}catch(m){u({kind:"note",tone:"error",text:`Could not read ${i}: ${m.message}`})}}return}if(l.backspace||l.delete){De(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){De(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(ne){if(l.escape){Me(null);return}if(l.upArrow){Me(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Me(i=>i&&{...i,cursor:Math.min(Math.max(0,Rt.length-1),i.cursor+1)});return}if(l.return){let i=Rt[ne.cursor];if(i){Me(null),he(i.uuid),fe.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},Be(i.name),ln(i.uuid),He.current=[];let m=sn(e,i.uuid);for(let p of m)u(p);u({kind:"note",tone:"ok",text:m.length?`Resumed "${i.name}" \u2014 restored ${m.length} message(s); continue where you left off.`:`Resumed "${i.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(l.backspace||l.delete){Me(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Me(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!A&&le){$(le),ae(null),kt.current++;return}if(!A&&et.length>0&&a&&/^[1-9]$/.test(a)){let i=et[Number(a)-1];if(i){$(i);return}}if(!jn(a,l)){if(bn){if(l.upArrow){We(i=>Math.max(0,i-1));return}if(l.downArrow){We(i=>Math.min(nt.length-1,i+1));return}if(l.tab){$("/"+(nt[ot]?.name??"")+" "),We(0);return}if(l.return){let i=nt[ot];if(i){let m=A.trim().slice(1).split(/\s+/).slice(1).join(" ");if(zn(i.usage)&&!m){$("/"+i.name+" "),We(0);return}jt("/"+i.name+(m?" "+m:""));return}}}else{if(l.upArrow){if(A!==""&&Fe===null){Et(-1);return}let i=fo.current;if(!i.length)return;let m=Fe===null?i.length-1:Math.max(0,Fe-1);wt(m),$(i[m]??"");return}if(l.downArrow){if(A!==""&&Fe===null){Et(1);return}let i=fo.current;if(Fe===null)return;let m=Fe+1;m>=i.length?(wt(null),$("")):(wt(m),$(i[m]??""));return}}if(l.return){jt();return}if(l.backspace||l.delete){P();return}if(l.escape){$(""),We(0),ae(null),J([]);return}a&&!l.ctrl&&!l.meta&&Rn(a)}}}),Ae(()=>{if(M||z)return;let a=Ie.current.shift();a!==void 0&&(go([...Ie.current]),jt(a))},[M,z,jt]),W(X,{flexDirection:"column",width:Ne,children:[yo!==null&&W(X,{marginTop:1,flexDirection:"column",children:[yo.truncated&&C(v,{dimColor:!0,children:"\u2026"}),C(v,{children:yo.text})]}),M&&(hn?C(mc,{p:hn}):C(pc,{startTime:tt.current,chars:Ds})),C(X,{borderStyle:"round",borderColor:$n.prod?"yellow":B,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:A?An.map((a,l)=>{let i=g.text.slice(a.start,a.end),m=l===io(An,g.pos),p=Math.min(g.pos,a.end)-a.start;return W(v,{children:[C(v,{color:B,children:l===0?"> ":" "}),m?W(cn,{children:[i.slice(0,p),C(co,{ch:i[p]??" "}),i.slice(p+1)]}):i||" "]},`${l}-${a.start}`)}):W(v,{children:[C(v,{color:B,children:"> "}),le&&!M?W(cn,{children:[C(co,{ch:le[0],dim:!0}),C(v,{color:"gray",children:le.slice(1)}),C(v,{dimColor:!0,children:" (tab)"})]}):ko.isSetup||!Es?W(cn,{children:[C(co,{ch:ko.text[0],dim:!0}),C(v,{color:"gray",children:ko.text.slice(1)})]}):C(co,{ch:" "})]})}),gn.length>0&&C(X,{flexDirection:"column",paddingX:1,children:gn.map((a,l)=>C(v,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),je?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Select model"}),C(v,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),de.map((a,l)=>W(v,{color:l===je.cursor?B:void 0,children:[l===je.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===je.current?" \u2714":""}`.padEnd(16),C(v,{color:"gray",children:a.desc})]},a.label)),C(v,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):me?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Switch connection profile"}),me.rows.map((a,l)=>W(v,{color:l===me.cursor?B:void 0,children:[l===me.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===me.current?" \u2714":""}`.padEnd(20),C(v,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?C(v,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),C(v,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):ve?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Pick your Document360 environment"}),ve.rows.map((a,l)=>W(v,{color:l===ve.cursor?B:void 0,children:[l===ve.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),C(v,{color:"gray",children:a.apiUrl})]},a.name)),C(v,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):K?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Switch workspace"}),C(v,{dimColor:!0,children:`environment ${K.environment} \xB7 project ${K.projectId.slice(0,8)}\u2026`}),K.rows.map((a,l)=>W(v,{color:l===K.cursor?B:void 0,children:[l===K.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===K.current?" \u2714":""}`.padEnd(30),C(v,{color:"gray",children:a.type??""})]},a.id)),C(v,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):Q?(()=>{let l=Math.min(Math.max(0,Q.cursor-Math.floor(7)),Math.max(0,Q.rows.length-14)),i=Q.rows.slice(l,l+14),m=Q.rows.filter(p=>p.checked).length;return W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:`Which folders back the user docs? (${m} selected of ${Q.rows.length})`}),l>0?C(v,{dimColor:!0,children:` \u2191 ${l} more`}):null,i.map((p,k)=>{let y=l+k;return W(v,{color:y===Q.cursor?B:void 0,children:[y===Q.cursor?"\u276F ":" ",p.checked?"\u25C9 ":"\u25CB ",p.path.padEnd(Math.min(48,Ne-34)),C(v,{color:"gray",children:Gt(p)})]},p.path)}),l+14<Q.rows.length?C(v,{dimColor:!0,children:` \u2193 ${Q.rows.length-l-14} more`}):null,C(v,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():ge?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Publish which article?"}),ge.rows.map((a,l)=>W(v,{color:l===ge.cursor?B:void 0,children:[l===ge.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,Ne-30)),C(v,{color:"gray",children:a.label})]},a.path)),C(v,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):oe?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:`Preview article${oe.query?` \u2014 filter: ${oe.query}`:" (type to filter)"}`}),Pt.length===0?C(v,{color:"gray",children:"no articles match"}):Pt.map((a,l)=>W(v,{color:l===oe.cursor?B:void 0,children:[l===oe.cursor?"\u276F ":" ",a]},a)),C(v,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ne?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:`Resume session${ne.query?` \u2014 filter: ${ne.query}`:" (type to filter)"}`}),Rt.length===0?C(v,{color:"gray",children:"no sessions match"}):Rt.map((a,l)=>W(v,{color:l===ne.cursor?B:void 0,children:[l===ne.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),C(v,{color:l===ne.cursor?B:"gray",children:a.firstPrompt.slice(0,Math.max(10,Ne-40))})]},a.uuid)),C(v,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):bn?C(X,{flexDirection:"column",children:nt.map((a,l)=>W(v,{color:l===ot?B:void 0,children:[l===ot?"\u276F ":" ",a.usage.padEnd(22)," ",C(v,{color:l===ot?B:"gray",children:a.name==="model"&&ho?`${a.desc} (currently ${ho})`:a.desc})]},a.name))}):!A&&et.length>0?W(X,{flexDirection:"column",paddingX:1,children:[et.map((a,l)=>W(v,{children:[C(v,{color:B,children:l+1})," ",a.slice(0,Math.max(20,Ne-5))]},a)),C(v,{dimColor:!0,children:`press 1-${et.length} to fill the command \xB7 esc dismiss`})]}):C(X,{paddingX:1,children:W(v,{color:"gray",children:[$n.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${ho??"model n/a"}${t.kind==="api"?yn>0?` \xB7 ${Oe(yn)}`:"":kn>0?` \xB7 ${Xt(kn)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}H();import{jsx as kc}from"react/jsx-runtime";async function Rs(e=process.cwd(),t="auto",o,n="0.0.0"){let r=hc(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 ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=gc(kc(Ps,{cwd:e,auth:r,profileName:o,version:n}));await s(),process.stdout.write(`
83
- `),process.exit(0)}var $c=wc(import.meta.url),As=$c("../package.json"),xe=new yc;function dn(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}xe.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=>{dn(e),await Nt({profile:e.profile,manual:e.manual})});xe.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{dn(e),await Wn({profile:e.profile})});xe.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{dn(e),await Un({profile:e.profile})});var pn=xe.command("profile").description("Manage connection profiles for the current repo");pn.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Lt(process.cwd()));pn.command("use <name>").description("Set the default profile for this repo").action(e=>Ot(process.cwd(),e));pn.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Ut(process.cwd(),e));var js=xe.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");js.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>Je(process.cwd(),e.profile));js.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await _n(process.cwd(),e,t.profile)});xe.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Hn());xe.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(()=>(Jt(),_r)),o=await t(process.cwd());for(let n of e(o))console.log(n);process.exitCode=o.some(n=>n.level==="fail")?1:0});xe.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(As.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(xc.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 Bn(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 es(e.cwd,e.auth,e.profile):await Rs(e.cwd,e.auth,e.profile,As.version)});xe.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
80
+ Press 1 to start.`}),K(["/init"]);return}try{let a=he(e,s),l=Fe(a.name);l&&De(l)&&l.refreshToken?cc({profile:a.name,connection:a.connection}).then(()=>{d({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),Pe(i=>i+1)}).catch(()=>{d({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),K(["/login"])}):(!l||De(l))&&(d({kind:"note",tone:"warn",text:`Profile "${a.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),K(["/login"]))}catch{}try{let a=se(e),l=he(e,s),i=Fe(l.name);a&&i&&!De(i)&&Mc(e,a.authoritativeSourceFiles)&&(d({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),K(["/scope"]))}catch{}},[]),Ie(()=>{let a=null,l=null,i=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let h=`${me.columns??80}x${me.rows??24}`;h!==Eo.current&&(Eo.current=h,Js(p=>p+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+Er(ze.current,Re())+"\x1B[?2026l")},80))},400)};return me.on("resize",i),()=>{a&&clearTimeout(a),l&&clearTimeout(l),me.off("resize",i)}},[me,Re]);let Ue=Math.max(20,(me.columns??80)-1),Do=un(()=>Ec(e,s),[e,s,de]),wn=un(()=>{let a=se(e);if(!a)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};try{let h=he(e,s),p=Fe(h.name);if(!(!!p&&!(De(p)&&!p.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${h.name}")\u2026`,isSetup:!0};if(!h.project.workspaceId)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}let l=vt(e,(a.docsDir??"user-docs").replace(/\/+$/,""));return(()=>{try{return Es(l)&&Fs(l).length>0}catch{return!1}})()?{text:Dc,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}},[e,s,de]),ut=tr(j),Io=ut.length>0&&!E,$n=u!==null?As(u,8,Ue):null,jt=ae?ae.paths.filter(a=>!ae.query||a.toLowerCase().includes(ae.query.toLowerCase())).slice(0,8):[],Et=ce?ce.sessions.filter(a=>{let l=ce.query.toLowerCase();return!l||a.name.toLowerCase().includes(l)||a.firstPrompt.toLowerCase().includes(l)}).slice(0,8):[],xe=X((a,l)=>{le.current?.close(),le.current=Is({cwd:e,resume:a,profileName:l??s,allowProdWrites:lt.current}),a||(ke.current={uuid:null,firstPrompt:null,titleFired:!1},Ge(null)),at(0),ct(0)},[e,s]),_o=X((a,l,i)=>{if(Ic.test(a))try{let h=he(e,s),p=typeof l.project_id=="string"&&l.project_id||h.project.projectId,k=on(l,i),y=[],I=rn(i);a.endsWith("publish_article")&&I&&y.push(`Live: ${I}`),k&&p&&y.push(`Preview: ${nn(h.connection.portalUrl,p,k,h.project.languageCode??"en")}`),y.length>0&&d({kind:"link",lines:y})}catch{}},[e,s,d]),be=X(async(a,l)=>{mn(!0),fe(null),K([]);let i=++Ct.current;d({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let h=ke.current;h.firstPrompt||(h.firstPrompt=l?.display??a),He.current=Date.now(),st(0),q(!0),St.current.clear();let p="",k="",y=null,I=()=>{y||(y=setTimeout(()=>{y=null,g(k.length>0?k:null)},60))},M=()=>{y&&clearTimeout(y),y=null,g(null)},N=()=>{if(k.trim()){let A=k.trimEnd();d({kind:"assistant",text:A})}k="",M()};try{for await(let A of le.current.send(a))if(A.type==="session"){if(!h.uuid){h.uuid=A.sessionId;let _=new Date().toISOString(),C=fc(h.firstPrompt??"session");mc({uuid:A.sessionId,name:C,renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:_,updatedAt:_}),Ge(C)}}else if(A.type==="text"){k+=A.delta,p+=A.delta;let _=js(k);if(_>0){let C=k.slice(0,_).trimEnd();C&&d({kind:"assistant",text:C}),k=k.slice(_)}I(),st(C=>C+A.delta.length)}else if(A.type==="tool"){let _=en(A.name,A.input);_&&(N(),d({kind:"tool",title:_.title,sep:_.sep,arg:_.arg}),St.current.set(A.id,{name:A.name,input:A.input}))}else if(A.type==="article_diff"){let _=We(A.oldContent,A.newContent,Re());_&&(N(),d({kind:"diff",added:_.added,removed:_.removed,lines:_.lines,hidden:_.hidden}))}else if(A.type==="tool_result"){A.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(A.output)&&(Rt.current=!0);let _=St.current.get(A.id);if(_){St.current.delete(A.id),N();let C=tn(A.output,4,A.isError?void 0:_.name,_.input);d({kind:"tool-result",lines:C.lines,hidden:C.hidden,isError:A.isError}),A.isError||_o(_.name,_.input,A.output)}}else if(A.type==="result"){N(),at(C=>C+A.outputTokens),ct(C=>C+A.costUsd),d({kind:"done",seconds:Math.round((Date.now()-He.current)/1e3),tokens:A.outputTokens,costUsd:A.costUsd,ok:A.ok});let _=A.ok?xs(p,mt.map(C=>C.name)):[];if(_.length>0?K(_):A.ok&&p.trim()&&yc(a,p,e).then(C=>{C&&Ct.current===i&&fe(C)}).catch(()=>{}),h.uuid&&(ho(h.uuid),!h.titleFired)){h.titleFired=!0;let C=h.uuid,T=h.firstPrompt;T&&_s(T,e).then(U=>{U&&(pc(C,U),Ge(U))}).catch(()=>{})}}else A.type==="error"&&(N(),A.kind==="auth"&&(Rt.current=!0),d({kind:"note",text:`agent error: ${A.message}`,tone:"error"}))}finally{q(!1),M(),ke.current.uuid&&ws(e,ke.current.uuid,ze.current),Rt.current&&(Rt.current=!1,At.current=l?.display??a,K(["/login"])),Tt.current&&(Tt.current=!1,Le.current.length>0&&(d({kind:"note",tone:"info",text:`(${Le.current.length} queued message(s) discarded)`}),Le.current=[],kn([])),d({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,d,_o,Re]),xn=X(a=>{let l=Fe(a);if(!l||De(l)&&!l.refreshToken)return null;let i={...ot(l.idToken)??{},...ot(l.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),bn=X((a,l)=>{if(l){let i=se(e);i&&(i.defaultProfile=a,wc(i,e))}c(a),xe(void 0,a),d({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${a}"${l?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),xn(a)||(d({kind:"note",tone:"warn",text:`Profile "${a}" is not signed in \u2014 do you want to log in now? (press 1)`}),K(["/login"])),Pe(i=>i+1)},[e,d,xe,xn]),Mo=X((a,l,i)=>{Nt(e,a,l,i.id,i.name),d({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),xe(),Ms(e,a)||(d({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),fe("analyze this repo and propose a docs structure"))},[e,d,xe]),Dt=X(()=>{let a=Se.current[0];if(!a)return;d({kind:"note",tone:"info",text:`\u25CF ${a.title} (${a.path})`});for(let i of a.notes)d({kind:"note",tone:"warn",text:`\u26A0 ${i}`});a.overwritesLocalChanges&&d({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let l=We(a.oldContent,a.newContent,Re());d(l?{kind:"diff",added:l.added,removed:l.removed,lines:l.lines,hidden:l.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),d({kind:"note",tone:"info",text:`Write ${a.path}? (y/n \u2014 anything else cancels)`})},[d,Re]),No=X(a=>{if(Se.current.length===0)return!1;let l=a.trim().toLowerCase();if(l==="y"||l==="yes"){let i=Se.current.shift();try{$c({cwd:e,profileName:s},i),d({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),Se.current.length===0&&K(h=>h.length>0?h:["/sync"])}catch(h){d({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(l==="n"||l==="no"){let i=Se.current.shift();d({kind:"note",tone:"info",text:`Skipped ${i.path}.`})}else{let i=Se.current.length;return Se.current=[],d({kind:"note",tone:"info",text:`Pull cancelled (${i} article(s) left untouched).`}),!0}return Dt(),!0},[e,s,Dt,d]),Lo=X(a=>{let l=hn.current;if(!l)return!1;if(hn.current=null,a.trim()!==l.repoName)return d({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:i,failed:h}=ao(e,l.targets);d({kind:"note",tone:h.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${i.length} item${i.length===1?"":"s"}. The repo is back to its original state.`});for(let p of h)d({kind:"note",tone:"error",text:` \u2717 ${p.path}: ${p.error}`});return!0},[e,d]),Oo=X(async a=>{let l=a.slice(1).trim().split(/\s+/),i=(l[0]??"").toLowerCase(),h=l.slice(1);switch(mn(!0),i){case"help":d({kind:"note",tone:"info",text:Bt().join(`
81
+ `)});return;case"exit":case"quit":le.current?.close(),r();return;case"clear":xe(),ze.current=[],mn(!1),fe(null),Ct.current++,d({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let p;try{p=he(e,s)}catch(k){d({kind:"note",tone:"error",text:k.message});return}d({kind:"note",tone:"info",text:`Profile "${p.name}" \u2192 ${p.connection.name} (${p.connection.apiUrl})${p.production?" \u26A0 PRODUCTION":""}`}),ie(!0);try{let k=await ic(p.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},I=>d({kind:"note",tone:"info",text:I})),y=lc(p.name,k);ac(y),Sn(y,p.name,I=>d({kind:"note",tone:"info",text:I})),d({kind:"note",tone:"ok",text:`\u2713 Logged in to "${p.name}" as ${Ot(y)}`}),p.project.workspaceId||(d({kind:"note",tone:"info",text:"Next: pick the workspace your articles publish to."}),K(["/workspace"])),At.current&&(fe(At.current),At.current=null,d({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(k){d({kind:"note",tone:"error",text:`Login failed: ${k.message}`})}finally{ie(!1),Pe(k=>k+1)}return}case"allow-prod":{let p=!1;try{p=he(e,s).production}catch{}if(!p){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}lt.current=!0,xe(),d({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let p=Zn(h.join(" ")),k=ke.current.uuid;if(!k){d({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!p){d({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let y=ke.current.firstPrompt??"";_s(y,e).then(I=>{I?(fe(`/rename ${I}`),d({kind:"note",tone:"info",text:`Suggestion: "${I}" \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}kc(k,p),Ge(p),bt(`${go} d360-writer \xB7 ${p}`),d({kind:"note",tone:"ok",text:`Session renamed to "${p}".`});return}case"profile":{let p=h[0],k=se(e);if(!p){let y=Object.entries(k?.profiles??{});if(y.length===0){d({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let I=y.map(([A,_])=>({name:A,env:_.connection?.environment??"custom",prod:_.production===!0,who:xn(A)})),M=s??k?.defaultProfile,N=Math.max(0,I.findIndex(A=>A.name===M));Ye({cursor:N,current:N,rows:I});return}if(p==="add"){let y=eo(e,h[1],h[2]);if(y){d({kind:"note",tone:"error",text:y});return}d({kind:"note",tone:"ok",text:`\u2713 Profile "${h[1]}" created (environment: ${h[2]??h[1]}).`}),bn(h[1],!1);return}if(!k?.profiles?.[p]){d({kind:"note",tone:"error",text:`Unknown profile "${p}". Create it: /profile add ${p} <environment>`});return}bn(p,!0);return}case"doctor":await $t(h,{cwd:e});return;case"mcp":await zt(h);return;case"init":{if(se(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 p=vc().map(k=>({name:k,apiUrl:Cc(k).apiUrl}));Xe({cursor:0,rows:p});return}case"resume":{let p=h.join(" ").trim(),k=hc(e).filter(M=>M.uuid!==ke.current.uuid);if(!p){if(!k.length){d({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Oe({query:"",cursor:0,sessions:k});return}let y=gc(e,p);if(!y){d({kind:"note",tone:"error",text:`No session matches "${p}".`});return}xe(y.uuid),ke.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},Ge(y.name),ho(y.uuid),ze.current=[];let I=mo(e,y.uuid);for(let M of I)d(M);d({kind:"note",tone:"ok",text:I.length?`Resumed "${y.name}" \u2014 restored ${I.length} message(s); continue where you left off.`:`Resumed "${y.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let p=h.join(" ").trim(),k;try{k=await Mt(e,s)}catch(I){d({kind:"note",tone:"error",text:`Could not list workspaces: ${I.message}`});return}if(!p){let I=k.workspaces.map(N=>({id:N.id,name:N.name??N.id,type:N.workspace_type}));if(I.length===0){d({kind:"note",tone:"info",text:"No workspaces in this project."});return}let M=Math.max(0,I.findIndex(N=>N.id===k.current));Ve({cursor:M,current:M,rows:I,profile:k.profile,projectId:k.projectId,environment:k.environment});return}let y=Pn(k.workspaces,p);if(!y){d({kind:"note",tone:"error",text:`No workspace matches "${p}".`});return}Mo(k.profile,k.projectId,y);return}case"publish":{if(h[0]&&h[0]!=="--all"){await be(Yt(h[0]),{display:`/publish ${h[0]}`,echoDisplay:!0});return}let p=h[0]==="--all";d({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let k=await ko({cwd:e,profileName:s}),y=Gt(k.entries);if(y.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let M=k.counts["unknown-base"]??0;M>0&&d({kind:"note",tone:"info",text:`(${M} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(p){await be(ht(y.map(M=>M.path)),{display:"/publish --all",echoDisplay:!0});return}let I=y.length>1?[{path:"--all",label:`publish all ${y.length} candidates in one run`},...y]:y;Je({cursor:0,rows:I})}catch(k){d({kind:"note",tone:"error",text:`Could not compute sync status: ${k.message}`}),d({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let p=h.join(" ").trim();if(!p){let y=[];try{y=Object.keys(Ms(e,he(e,s).name)?.articles??{})}catch{}if(y.length===0){d({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}Ne({query:"",cursor:0,paths:y});return}let k=sc(p)?p:vt(e,p);if(Es(k)){try{d({kind:"preview",name:dn(k),text:Us(Ds(k,"utf8"))})}catch(y){d({kind:"note",tone:"error",text:`Could not read ${k}: ${y.message}`})}return}if(_c.test(p)){try{let y=he(e,s),I={profile:y.name,connection:y.connection},M=y.project.projectId??uc(I),N=await dc(I,M,p);d({kind:"preview",name:N.title??p,text:N.content??"*(article has no content)*"})}catch(y){d({kind:"note",tone:"error",text:`Could not fetch article: ${y.message}`})}return}d({kind:"note",tone:"error",text:`"${p}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let p=h[0]?.trim();if(!p){let M=Kt(xo(e));Me({cursor:M,current:M});return}let{lines:k,changed:y,effective:I}=wt(e,p);for(let M of k)d({kind:"note",tone:M.startsWith("\u26A0")?"warn":M.startsWith("\u2713")?"ok":"info",text:M});y&&(jo(M=>M+1),le.current?.setModel(I));return}case"convert":{if(!se(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:p,run:k}=Wn(h),y=Tc(e,s);if(y.length===0){d({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let I=Fn(y,p);if(I.length===0){d({kind:"note",tone:"error",text:`No tracked articles under "${p}". (${y.length} are tracked overall.)`});return}let M=Ns(I),N=3,A=`/convert${p?` --scope ${p}`:""} --run`,_=wo(e,"light");if(!k){let T=Ls({files:Hn(e,I),op:"convert",model:_.model}),U=p?`Scope: ${p} (${I.length} of ${y.length} tracked)
82
+ `:"",ee=`
83
+ Model: ${_.model}${_.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;d({kind:"note",tone:"info",text:U+qn(M,T,N).join(`
84
+ `)+ee}),K([A]);return}He.current=Date.now(),st(0),q(!0);let C=new AbortController;$e.current=C,Z({total:M.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Converting ${I.length} articles across ${M.length} partitions (\u2264${N} agents at once) on ${_.model}\u2026 (esc to stop)`});try{for await(let T of yo({cwd:e,partitions:M,promptFor:Bn,concurrency:N,profileName:s,allowProdWrites:lt.current,model:_.model,signal:C.signal}))if(T.type==="partition_status")T.status==="running"?(Z(U=>U&&{...U,active:[...U.active,T.label],lastAt:Date.now()}),d({kind:"note",tone:"info",text:` \u25B8 ${T.label} \u2014 converting\u2026`})):(Z(U=>U&&{...U,active:U.active.filter(ee=>ee!==T.label),done:U.done+1,lastAt:Date.now()}),d({kind:"note",tone:T.status==="done"?"ok":"error",text:` ${T.status==="done"?"\u2713":"\u2717"} ${T.label}`}));else if(T.type==="partition_event")Z(U=>{if(!U)return U;let ee={...U,lastAt:Date.now()};return T.event.type==="tool"?ee.tools=U.tools+1:T.event.type==="text"&&(ee.chars=U.chars+T.event.delta.length),ee});else if(T.type==="run_done"){ct(O=>O+T.totalCostUsd),at(O=>O+T.results.reduce((Y,te)=>Y+te.outputTokens,0));let U=T.aborted?"Stopped. ":"",ee=t.kind==="api"?"api":"subscription";d({kind:"note",tone:T.aborted?"warn":T.ok?"ok":"warn",text:U+zn(T.results,M,ee).join(`
85
+ `)})}}catch(T){d({kind:"note",tone:"error",text:`Convert run failed: ${T.message}`})}finally{$e.current=null,Z(null),q(!1)}return}case"write":{if(!se(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=Rc(e);if(p.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:k,path:y,run:I}=Yn(h),{docsDir:M,targets:N,reason:A}=Qn(e,p,{scope:k,path:y});if(N.length===0){d({kind:"note",tone:"warn",text:A??"Nothing to write."});return}let _=Ns(N),C=5,T=wo(e,"standard");if(!I&&!!!y){let O=Ls({files:Xn(e,p,N),op:"write",model:T.model}),Y=k?`Scope: ${k} (${N.length} pending)
86
+ `:"",te=`
87
+ Model: ${T.model}${T.forced?" (forced)":" \u2014 authoring; /model to override"}`;d({kind:"note",tone:"info",text:Y+Jn(_,O,C).join(`
88
+ `)+te}),K([`/write${k?` --scope ${k}`:" --all"} --run`]);return}He.current=Date.now(),st(0),q(!0);let ee=new AbortController;$e.current=ee,Z({verb:"Writing",total:_.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Writing ${N.length} article${N.length===1?"":"s"} across ${_.length} partition${_.length===1?"":"s"} (\u2264${C} agents at once) on ${T.model}${T.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let O of yo({cwd:e,partitions:_,promptFor:Y=>Vn(Y,M),concurrency:C,profileName:s,allowProdWrites:lt.current,model:T.model,signal:ee.signal}))if(O.type==="partition_status")O.status==="running"?(Z(Y=>Y&&{...Y,active:[...Y.active,O.label],lastAt:Date.now()}),d({kind:"note",tone:"info",text:` \u25B8 ${O.label} \u2014 writing\u2026`})):(Z(Y=>Y&&{...Y,active:Y.active.filter(te=>te!==O.label),done:Y.done+1,lastAt:Date.now()}),d({kind:"note",tone:O.status==="done"?"ok":"error",text:` ${O.status==="done"?"\u2713":"\u2717"} ${O.label}`}));else if(O.type==="partition_event")Z(Y=>{if(!Y)return Y;let te={...Y,lastAt:Date.now()};return O.event.type==="tool"?te.tools=Y.tools+1:O.event.type==="text"&&(te.chars=Y.chars+O.event.delta.length),te});else if(O.type==="run_done"){ct(Cn=>Cn+O.totalCostUsd),at(Cn=>Cn+O.results.reduce((Zs,ei)=>Zs+ei.outputTokens,0));let Y=O.aborted?"Stopped. ":"",te=t.kind==="api"?"api":"subscription";d({kind:"note",tone:O.aborted?"warn":O.ok?"ok":"warn",text:Y+Kn(O.results,_,te).join(`
89
+ `)})}}catch(O){d({kind:"note",tone:"error",text:`Write run failed: ${O.message}`})}finally{$e.current=null,Z(null),q(!1)}return}case"sync":{let p=(h[0]??"status").toLowerCase();try{if(p==="status"){d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let k=await ko({cwd:e,profileName:s});d({kind:"note",tone:"info",text:Jt(k).join(`
90
+ `)});return}if(p==="pull"){let k=h[1];if(!k){d({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let y;if(k==="--all"){if(d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),y=(await ko({cwd:e,profileName:s})).entries.filter(N=>N.status==="remote-ahead"&&N.path).map(N=>N.path),y.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 y=[k.replace(/\\/g,"/")];let I=[];for(let M of y)I.push(await xc({cwd:e,profileName:s,relPath:M}));Se.current=I,Dt();return}d({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${p} \u2014 use /sync or /sync pull <path>|--all.`})}catch(k){d({kind:"note",tone:"error",text:`Sync failed: ${k.message}`})}return}case"scope":{if(!se(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=bc(e);if(p.length===0){d({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}Ke({cursor:0,rows:p.map(k=>({...k,checked:k.recommended}))});return}case"audit":case"capture-setup":{let k=await(i==="audit"?Xt:xt)(h,void 0);if(k.kind==="forward-to-agent"&&k.prompt&&await be(k.prompt,{display:k.display,echoDisplay:!0}),i==="capture-setup"){let y=io(e);y&&d({kind:"note",tone:"info",text:y.join(`
91
+ `)})}return}case"screenshot":{let p=oo(h);if(p.mode==="list"){if(!se(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}d({kind:"note",tone:"info",text:so(ro(e,p.scope),p.scope).join(`
92
+ `)});return}if(p.mode==="single"){let C=await Zt(h);C.kind==="forward-to-agent"&&C.prompt&&await be(C.prompt,{display:C.display,echoDisplay:!0});return}if(!se(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let k=p.mode==="scope"?p.scope:void 0,y=Sc(e,{scope:k}).map(C=>C.id);if(y.length===0){d({kind:"note",tone:"info",text:k?`No screenshot placeholders under ${k}.`:"No screenshot placeholders found in the docs."});return}let I=6,M=Pc(y,3),N=wo(e,"standard");He.current=Date.now(),st(0),q(!0);let A=new AbortController;$e.current=A,Z({verb:"Authoring",total:M.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Authoring ${y.length} screenshot spec${y.length===1?"":"s"} across ${M.length} partition${M.length===1?"":"s"} (\u2264${I} agents at once) on ${N.model}${N.forced?" (forced)":""}\u2026 (esc to stop)`});let _=!1;try{for await(let C of yo({cwd:e,partitions:M,promptFor:ts,concurrency:I,profileName:s,allowProdWrites:lt.current,model:N.model,signal:A.signal}))if(C.type==="partition_status")C.status==="running"?(Z(T=>T&&{...T,active:[...T.active,C.label],lastAt:Date.now()}),d({kind:"note",tone:"info",text:` \u25B8 ${C.label} \u2014 authoring\u2026`})):(Z(T=>T&&{...T,active:T.active.filter(U=>U!==C.label),done:T.done+1,lastAt:Date.now()}),d({kind:"note",tone:C.status==="done"?"ok":"error",text:` ${C.status==="done"?"\u2713":"\u2717"} ${C.label}`}));else if(C.type==="partition_event")Z(T=>{if(!T)return T;let U={...T,lastAt:Date.now()};return C.event.type==="tool"?U.tools=T.tools+1:C.event.type==="text"&&(U.chars=T.chars+C.event.delta.length),U});else if(C.type==="run_done"){ct(O=>O+C.totalCostUsd),at(O=>O+C.results.reduce((Y,te)=>Y+te.outputTokens,0));let T=C.results.filter(O=>O.ok).length,U=C.aborted?"Stopped. ":"";_=!C.aborted&&p.setup;let ee=C.aborted?"":_?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";d({kind:"note",tone:C.aborted?"warn":C.ok?"ok":"warn",text:`${U}Authored specs \u2014 ${T}/${C.results.length} batches ok.${ee}`})}}catch(C){d({kind:"note",tone:"error",text:`Spec authoring failed: ${C.message}`})}finally{$e.current=null,Z(null),q(!1)}if(_){let C=await xt();C.kind==="forward-to-agent"&&C.prompt&&await be(C.prompt,{display:C.display,echoDisplay:!0});let T=io(e);T&&d({kind:"note",tone:"info",text:T.join(`
93
+ `)})}return}case"reset":{let p=lo(e);if(p.length===0){d({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}hn.current={repoName:dn(e),targets:p},d({kind:"note",tone:"warn",text:co(e,p).join(`
94
+ `)});return}default:d({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,Dt,d,xe,be]),It=X(a=>{let l=(a??j).trim();if(b(""),Pt(null),Be(0),l.startsWith("/")){let h=k=>k.trim().split(/\s+/).slice(1).join(" "),p=h(l);K(k=>k.filter(y=>y.trim()!==l&&!(p&&h(y)===p)))}if(!l||Lo(l)||No(l))return;gn.current.push(l);let i=Ps(l,Co.current);l.startsWith("/")?Oo(i):be(i,{display:l})},[j,Lo,No,Oo,be]),Uo=X(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=bs(a);if(vs(l)){let i=Cs(++Xs.current,l);Co.current.set(i,l),v(i)}else v(l);return}v(a)},[v]),Qe=Math.max(10,Ue-6),Wo=un(()=>an(m.text,Qe),[m.text,Qe]),vn=X(a=>w(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),_t=X(a=>w(l=>({...l,pos:Ts(an(l.text,Qe),l.pos,a)})),[Qe]),dt=X(a=>w(l=>({...l,pos:Rs(an(l.text,Qe),l.pos,a)})),[Qe]),Ks=["\x1B[H","\x1B[1~","\x1BOH"],Qs=["\x1B[F","\x1B[4~","\x1BOF"],Fo=X((a,l)=>l.leftArrow?(vn(-1),!0):l.rightArrow?(vn(1),!0):a&&Ks.includes(a)?(dt("start"),!0):a&&Qs.includes(a)?(dt("end"),!0):l.ctrl&&a==="a"?(dt("start"),!0):l.ctrl&&a==="e"?(dt("end"),!0):!1,[vn,dt]);return oc((a,l)=>{if(l.ctrl&&a==="c"){le.current?.close(),r();return}if(!G){if(E){if(l.escape){if($e.current){$e.current.signal.aborted||(d({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),$e.current.abort());return}Tt.current||(Tt.current=!0,d({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),le.current?.interrupt());return}if(l.return){let i=j.trim();if(!i)return;Le.current.push(i),kn([...Le.current]),b("");return}if(Fo(a,l))return;if(l.upArrow){_t(-1);return}if(l.downArrow){_t(1);return}if(l.backspace||l.delete){$();return}a&&!l.ctrl&&!l.meta&&Uo(a);return}if(_e){if(l.upArrow){Me(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Me(i=>i&&{...i,cursor:Math.min(ge.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ge.length){Me(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ge[_e.cursor];Me(null);let{lines:h,changed:p,effective:k}=wt(e,i.value??"default");for(let y of h)d({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});p&&(jo(y=>y+1),le.current?.setModel(k));return}if(a==="s"){let i=ge[_e.cursor];Me(null),le.current?.setModel(i.value??void 0),d({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(l.escape){Me(null);return}return}if(ye){if(l.upArrow){Ye(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ye(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ye.rows.length){Ye(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=ye.rows[ye.cursor];Ye(null),bn(i.name,l.return===!0);return}if(l.escape){Ye(null);return}return}if(Te){if(l.upArrow){Xe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Xe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=Te.rows.length){Xe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=Te.rows[Te.cursor].name;if(Xe(null),!lr(e,i).created){d({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}d({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let p=!1;try{let k=Fe(i);p=!!k&&!(De(k)&&!k.refreshToken)}catch{}p?(d({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),K(["/workspace"])):(d({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),K(["/login"])),Pe(k=>k+1);return}if(l.escape){Xe(null);return}return}if(ne){if(l.upArrow){Ve(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ve(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ne.rows.length){Ve(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ne.rows[ne.cursor],{profile:h,projectId:p}=ne;Ve(null),Mo(h,p,i);return}if(l.escape){Ve(null);return}return}if(oe){if(l.upArrow){Ke(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ke(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){Ke(i=>i&&{...i,rows:i.rows.map((h,p)=>p===i.cursor?{...h,checked:!h.checked}:h)});return}if(l.return){let i=oe.rows.filter(h=>h.checked).map(h=>h.path);if(Ke(null),i.length===0){d({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}jn(e,i),d({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of i)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(l.escape){Ke(null);return}return}if(we){if(l.upArrow){Je(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Je(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=we.rows.length){Je(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=we.rows[we.cursor],h=we.rows.filter(p=>p.path!=="--all").map(p=>p.path);Je(null),be(i.path==="--all"?ht(h):Yt(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){Je(null);return}return}if(ae){if(l.escape){Ne(null);return}if(l.upArrow){Ne(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ne(i=>i&&{...i,cursor:Math.min(Math.max(0,jt.length-1),i.cursor+1)});return}if(l.return){let i=jt[ae.cursor];if(i){Ne(null);try{d({kind:"preview",name:dn(i),text:Us(Ds(vt(e,i),"utf8"))})}catch(h){d({kind:"note",tone:"error",text:`Could not read ${i}: ${h.message}`})}}return}if(l.backspace||l.delete){Ne(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Ne(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(ce){if(l.escape){Oe(null);return}if(l.upArrow){Oe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Oe(i=>i&&{...i,cursor:Math.min(Math.max(0,Et.length-1),i.cursor+1)});return}if(l.return){let i=Et[ce.cursor];if(i){Oe(null),xe(i.uuid),ke.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},Ge(i.name),ho(i.uuid),ze.current=[];let h=mo(e,i.uuid);for(let p of h)d(p);d({kind:"note",tone:"ok",text:h.length?`Resumed "${i.name}" \u2014 restored ${h.length} message(s); continue where you left off.`:`Resumed "${i.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(l.backspace||l.delete){Oe(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Oe(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!j&&pe){b(pe),fe(null),Ct.current++;return}if(!j&&rt.length>0&&a&&/^[1-9]$/.test(a)){let i=rt[Number(a)-1];if(i){b(i);return}}if(!Fo(a,l)){if(Io){if(l.upArrow){Be(i=>Math.max(0,i-1));return}if(l.downArrow){Be(i=>Math.min(ut.length-1,i+1));return}if(l.tab){b("/"+(ut[it]?.name??"")+" "),Be(0);return}if(l.return){let i=ut[it];if(i){let h=j.trim().slice(1).split(/\s+/).slice(1).join(" ");if(nr(i.usage)&&!h){b("/"+i.name+" "),Be(0);return}It("/"+i.name+(h?" "+h:""));return}}}else{if(l.upArrow){if(j!==""&&qe===null){_t(-1);return}let i=gn.current;if(!i.length)return;let h=qe===null?i.length-1:Math.max(0,qe-1);Pt(h),b(i[h]??"");return}if(l.downArrow){if(j!==""&&qe===null){_t(1);return}let i=gn.current;if(qe===null)return;let h=qe+1;h>=i.length?(Pt(null),b("")):(Pt(h),b(i[h]??""));return}}if(l.return){It();return}if(l.backspace||l.delete){$();return}if(l.escape){b(""),Be(0),fe(null),K([]);return}a&&!l.ctrl&&!l.meta&&Uo(a)}}}),Ie(()=>{if(E||G)return;let a=Le.current.shift();a!==void 0&&(kn([...Le.current]),It(a))},[E,G,It]),F(J,{flexDirection:"column",width:Ue,children:[$n!==null&&F(J,{marginTop:1,flexDirection:"column",children:[$n.truncated&&S(P,{dimColor:!0,children:"\u2026"}),S(P,{children:$n.text})]}),E&&(To?S(Oc,{p:To}):S(Nc,{startTime:He.current,chars:Ys})),S(J,{borderStyle:"round",borderColor:Do.prod?"yellow":z,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:j?Wo.map((a,l)=>{let i=m.text.slice(a.start,a.end),h=l===cn(Wo,m.pos),p=Math.min(m.pos,a.end)-a.start;return F(P,{children:[S(P,{color:z,children:l===0?"> ":" "}),h?F($o,{children:[i.slice(0,p),S(pn,{ch:i[p]??" "}),i.slice(p+1)]}):i||" "]},`${l}-${a.start}`)}):F(P,{children:[S(P,{color:z,children:"> "}),pe&&!E?F($o,{children:[S(pn,{ch:pe[0],dim:!0}),S(P,{color:"gray",children:pe.slice(1)}),S(P,{dimColor:!0,children:" (tab)"})]}):wn.isSetup||!Gs?F($o,{children:[S(pn,{ch:wn.text[0],dim:!0}),S(P,{color:"gray",children:wn.text.slice(1)})]}):S(pn,{ch:" "})]})}),So.length>0&&S(J,{flexDirection:"column",paddingX:1,children:So.map((a,l)=>S(P,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),_e?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:"Select model"}),S(P,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),ge.map((a,l)=>F(P,{color:l===_e.cursor?z:void 0,children:[l===_e.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===_e.current?" \u2714":""}`.padEnd(16),S(P,{color:"gray",children:a.desc})]},a.label)),S(P,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):ye?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:"Switch connection profile"}),ye.rows.map((a,l)=>F(P,{color:l===ye.cursor?z:void 0,children:[l===ye.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===ye.current?" \u2714":""}`.padEnd(20),S(P,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?S(P,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),S(P,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):Te?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:"Pick your Document360 environment"}),Te.rows.map((a,l)=>F(P,{color:l===Te.cursor?z:void 0,children:[l===Te.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),S(P,{color:"gray",children:a.apiUrl})]},a.name)),S(P,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):ne?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:"Switch workspace"}),S(P,{dimColor:!0,children:`environment ${ne.environment} \xB7 project ${ne.projectId.slice(0,8)}\u2026`}),ne.rows.map((a,l)=>F(P,{color:l===ne.cursor?z:void 0,children:[l===ne.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===ne.current?" \u2714":""}`.padEnd(30),S(P,{color:"gray",children:a.type??""})]},a.id)),S(P,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):oe?(()=>{let l=Math.min(Math.max(0,oe.cursor-Math.floor(7)),Math.max(0,oe.rows.length-14)),i=oe.rows.slice(l,l+14),h=oe.rows.filter(p=>p.checked).length;return F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:`Which folders back the user docs? (${h} selected of ${oe.rows.length})`}),l>0?S(P,{dimColor:!0,children:` \u2191 ${l} more`}):null,i.map((p,k)=>{let y=l+k;return F(P,{color:y===oe.cursor?z:void 0,children:[y===oe.cursor?"\u276F ":" ",p.checked?"\u25C9 ":"\u25CB ",p.path.padEnd(Math.min(48,Ue-34)),S(P,{color:"gray",children:Vt(p)})]},p.path)}),l+14<oe.rows.length?S(P,{dimColor:!0,children:` \u2193 ${oe.rows.length-l-14} more`}):null,S(P,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():we?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:"Publish which article?"}),we.rows.map((a,l)=>F(P,{color:l===we.cursor?z:void 0,children:[l===we.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,Ue-30)),S(P,{color:"gray",children:a.label})]},a.path)),S(P,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):ae?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:`Preview article${ae.query?` \u2014 filter: ${ae.query}`:" (type to filter)"}`}),jt.length===0?S(P,{color:"gray",children:"no articles match"}):jt.map((a,l)=>F(P,{color:l===ae.cursor?z:void 0,children:[l===ae.cursor?"\u276F ":" ",a]},a)),S(P,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ce?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:`Resume session${ce.query?` \u2014 filter: ${ce.query}`:" (type to filter)"}`}),Et.length===0?S(P,{color:"gray",children:"no sessions match"}):Et.map((a,l)=>F(P,{color:l===ce.cursor?z:void 0,children:[l===ce.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),S(P,{color:l===ce.cursor?z:"gray",children:a.firstPrompt.slice(0,Math.max(10,Ue-40))})]},a.uuid)),S(P,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):Io?S(J,{flexDirection:"column",children:ut.map((a,l)=>F(P,{color:l===it?z:void 0,children:[l===it?"\u276F ":" ",a.usage.padEnd(22)," ",S(P,{color:l===it?z:"gray",children:a.name==="model"&&yn?`${a.desc} (currently ${yn})`:a.desc})]},a.name))}):!j&&rt.length>0?F(J,{flexDirection:"column",paddingX:1,children:[rt.map((a,l)=>F(P,{children:[S(P,{color:z,children:l+1})," ",a.slice(0,Math.max(20,Ue-5))]},a)),S(P,{dimColor:!0,children:`press 1-${rt.length} to fill the command \xB7 esc dismiss`})]}):S(J,{paddingX:1,children:F(P,{color:"gray",children:[Do.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${yn??"model n/a"}${t.kind==="api"?Ao>0?` \xB7 ${ue(Ao)}`:"":Ro>0?` \xB7 ${nt(Ro)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}H();import{jsx as Fc}from"react/jsx-runtime";async function Bs(e=process.cwd(),t="auto",n,o="0.0.0"){let r=Wc(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 ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=Uc(Fc(Hs,{cwd:e,auth:r,profileName:n,version:o}));await s(),process.stdout.write(`
95
+ `),process.exit(0)}var zc=Bc(import.meta.url),qs=zc("../package.json"),Ce=new Hc;function bo(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}Ce.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=>{bo(e),await Ut({profile:e.profile,manual:e.manual})});Ce.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{bo(e),await Ko({profile:e.profile})});Ce.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{bo(e),await Jo({profile:e.profile})});var vo=Ce.command("profile").description("Manage connection profiles for the current repo");vo.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Wt(process.cwd()));vo.command("use <name>").description("Set the default profile for this repo").action(e=>Ft(process.cwd(),e));vo.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Ht(process.cwd(),e));var zs=Ce.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");zs.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>Ze(process.cwd(),e.profile));zs.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await zo(process.cwd(),e,t.profile)});Ce.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Zo());Ce.command("doctor").description("Health-check: node, Claude auth, Document360 login, profile/workspace, category map, API reachability").action(async()=>{let{renderDoctorChecks:e,runDoctorChecks:t}=await Promise.resolve().then(()=>(Qt(),Vr)),n=await t(process.cwd());for(let o of e(n))console.log(o);process.exitCode=n.some(o=>o.level==="fail")?1:0});Ce.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(qs.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(qc.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 gs(e.cwd,e.auth,e.profile):await Bs(e.cwd,e.auth,e.profile,qs.version)});Ce.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
@@ -0,0 +1,51 @@
1
+ import { type CostEstimate, type DocsPlanEntry, type Partition, type PartitionResult, type TargetFile } from 'document360-engine';
2
+ import type { ReplContext } from '../repl.js';
3
+ import type { SlashCommandResult } from './index.js';
4
+ /** Partitions running at once — local-only work (no D360 rate limit), so higher than /convert. */
5
+ export declare const WRITE_CONCURRENCY = 5;
6
+ /** Parse /write args: `--run`/`--yes`, `--scope <cat>` (or `--scope=…`), `--all`, or a bare `<path>`. */
7
+ export declare function parseWriteArgs(args: string[]): {
8
+ scope?: string;
9
+ path?: string;
10
+ run: boolean;
11
+ };
12
+ /** Drop a leading `<docsDir>/` so a path given repo-relative matches the docsDir-relative plan keys. */
13
+ export declare function toPlanRelative(p: string, docsDir: string): string;
14
+ /** Keep only plan paths under `scope` (a category prefix or an exact .md path). No scope → all. */
15
+ export declare function applyScope(paths: string[], scope?: string): string[];
16
+ /** Of the given docsDir-relative paths, the ones not yet written (file missing or empty). FS. */
17
+ export declare function pendingArticles(cwd: string, docsDir: string, paths: string[]): string[];
18
+ /** Size each target by the SUM of its plan SOURCE bytes (the article file doesn't exist yet). The
19
+ 1-byte floor keeps every planned article in the estimate so its per-article overhead counts. */
20
+ export declare function sizeWriteTargets(cwd: string, entries: DocsPlanEntry[], paths: string[]): TargetFile[];
21
+ /** The agent prompt for one partition: author each listed article from the plan. Each is
22
+ independent; purpose + sources are looked up from the pinned plan, grounded in source. */
23
+ export declare function buildWritePrompt(partition: Partition, docsDir: string): string;
24
+ /** Preview lines for bare `/write`: the partition plan + the cost band, before any spend. */
25
+ export declare function previewLines(partitions: Partition[], estimate: CostEstimate, concurrency: number): string[];
26
+ /**
27
+ * Final summary, a bookend to the opening "Writing N articles across M partitions…" line. Usage
28
+ * shows dollars in API mode but OUTPUT TOKENS on a Claude subscription (per-token cost is
29
+ * meaningless there). `partitions` maps each result back to its article count (results carry only
30
+ * an index).
31
+ */
32
+ export declare function summaryLines(results: PartitionResult[], partitions: Partition[], usage: 'api' | 'subscription'): string[];
33
+ /** Resolve the docsDir-relative article paths a /write invocation targets, from the pinned plan.
34
+ Returns the target list + context for messaging. Single path → that article (forced, even if it
35
+ exists); scope/all → not-yet-written articles only. FS-touching (existence checks). */
36
+ export declare function selectWriteTargets(cwd: string, entries: DocsPlanEntry[], args: {
37
+ scope?: string;
38
+ path?: string;
39
+ }): {
40
+ docsDir: string;
41
+ targets: string[];
42
+ planCount: number;
43
+ reason?: string;
44
+ };
45
+ /**
46
+ * Classic-REPL `/write` — parity with the TUI handler (App.tsx): bare `/write [--scope x]` previews
47
+ * (plan + cost band, no spend); `/write --run` fans the planned articles out across parallel agents.
48
+ * A single `<path>` writes that article immediately. Mid-run abort is TUI-only (Esc); Ctrl+C exits
49
+ * the REPL here.
50
+ */
51
+ export declare function writeCommand(args: string[], ctx: ReplContext): Promise<SlashCommandResult>;
@@ -14,5 +14,15 @@ export declare const titleSeq: (text: string) => string;
14
14
  export declare const progressSeq: (state: 0 | 1 | 3) => string;
15
15
  export declare function setTitle(text: string): void;
16
16
  export declare function setProgress(state: 0 | 1 | 3): void;
17
- /** Animation frames for the busy title prefix — a flicking dot trail. */
18
- export declare const BUSY_FRAMES: readonly ["· ", "·· ", "···"];
17
+ /**
18
+ * Busy title prefix — a single dot bouncing left↔right (Claude-Code style: ". " / " ."), which
19
+ * reads as a calm "processing" pulse rather than a growing trail. Two frames, alternated.
20
+ */
21
+ export declare const BUSY_FRAMES: readonly ["· ", " ·"];
22
+ /**
23
+ * The idle/done tab mark — d360-writer's brand "icon" next to the PowerShell profile icon (the
24
+ * purple analog of Claude Code's green done-icon). It's a title glyph, not the OSC 9;4 progress
25
+ * ring, because that ring only renders in fixed states (green/red/yellow) and can't be purple;
26
+ * a title emoji also lets the profile icon stay visible (we clear the ring when idle).
27
+ */
28
+ export declare const BRAND_GLYPH = "\uD83D\uDFE3";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document360-writer",
3
- "version": "0.4.34",
3
+ "version": "0.4.36",
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.22",
37
+ "document360-engine": "^0.2.23",
38
38
  "ink": "^5.2.1",
39
39
  "picocolors": "^1.1.1",
40
40
  "react": "^18.3.1",