document360-writer 0.4.67 → 0.4.68

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,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- var Ua=Object.defineProperty;var to=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}};var Ma=(e,t)=>{for(var n in t)Ua(e,n,{get:t[n],enumerable:!0})};import Ae from"picocolors";var H,La,Wa,Fa,Br,$t,f,x,M,j,A,te,Pe,L=to(()=>{"use strict";H="#7f56d9",[La,Wa,Fa]=[127,86,217],Br=e=>Ae.isColorSupported?`\x1B[38;2;${La};${Wa};${Fa}m${e}\x1B[39m`:e,$t=e=>Ae.bold(Br(e)),f=e=>Ae.dim(e),x=e=>Ae.red(e),M=e=>Ae.yellow(e),j=e=>Ae.green(e),A=Br,te=e=>Ae.gray(e),Pe=e=>Ae.bold(e)});import{existsSync as li,writeFileSync as bn,readFileSync as Bc}from"node:fs";import{devHintsGuidePath as Go,hintsDir as Gc,architectureDir as Vc,messagesDir as ci,creatorNotesPath as si,devhintsStatePath as ui,ensureDir as kn,writeMessage as Yc,listOpenForDev as zc}from"document360-engine";function xn(){return`# Dev \u2192 Docs hand-off \u2014 protocol guide
2
+ var Na=Object.defineProperty;var eo=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}};var _a=(e,t)=>{for(var n in t)Na(e,n,{get:t[n],enumerable:!0})};import Ae from"picocolors";var H,Oa,Ua,Ma,Wr,Pt,f,x,M,j,A,te,Pe,L=eo(()=>{"use strict";H="#7f56d9",[Oa,Ua,Ma]=[127,86,217],Wr=e=>Ae.isColorSupported?`\x1B[38;2;${Oa};${Ua};${Ma}m${e}\x1B[39m`:e,Pt=e=>Ae.bold(Wr(e)),f=e=>Ae.dim(e),x=e=>Ae.red(e),M=e=>Ae.yellow(e),j=e=>Ae.green(e),A=Wr,te=e=>Ae.gray(e),Pe=e=>Ae.bold(e)});import{existsSync as as,writeFileSync as pn,readFileSync as Cl}from"node:fs";import{devHintsGuidePath as so,hintsDir as Pl,architectureDir as Tl,messagesDir as ls,creatorNotesPath as rs,devhintsStatePath as cs,ensureDir as dn,writeMessage as Sl,listOpenForDev as Rl}from"document360-engine";function us(){return`# Dev \u2192 Docs hand-off \u2014 protocol guide
3
3
 
4
4
  <!-- devhints script v${ne} \xB7 maintained by document360-writer (/devhints). -->
5
5
 
@@ -141,7 +141,7 @@ output shape; the writer judges it like a technical writer would. Two ways:
141
141
  \`--yes\` if the target profile is production.
142
142
  - **Queued (async)** \u2014 drop a \`request\` message (Part 3); the writer documents it on its next session or
143
143
  via CI. Use this when a human should review before publishing.
144
- `}function Vo(){return`# Creator notes (optional \u2014 for the docs agent and the builder)
144
+ `}function Dl(){return`# Creator notes (optional \u2014 for the docs agent and the builder)
145
145
 
146
146
  <!-- The ORIGINAL CREATOR's context. Most repos leave this empty \u2014 that's fine.
147
147
  The builder agent reads this FIRST and treats it as authoritative when writing the architecture
@@ -155,58 +155,58 @@ Anything only a human knows that would help document this product. For example:
155
155
  - Gotchas or non-obvious behavior.
156
156
 
157
157
  (Replace these examples with your notes, or leave the file as-is to skip \u2014 it's optional.)
158
- `}async function di(e,t){kn(Gc(t.cwd)),kn(Vc(t.cwd)),kn(ci(t.cwd)),li(si(t.cwd))||bn(si(t.cwd),Vo()),bn(Go(t.cwd),xn());let n=Et(t.cwd);$n(t.cwd,new Date().toISOString());for(let o of zo(n))console.log(o);return{kind:"continue"}}function pi(){return[`This is document360-writer's dev hand-off setup (v${ne}).`,'Connect this repository to document360-writer (its "docs agent" \u2014 a separate tool that writes this',"product's user documentation from your code, but needs your intent). This prompt is SAFE TO RUN MORE","THAN ONCE \u2014 paste it again whenever the product changes. At each step: create what is missing, reconcile","what already exists, and never duplicate, clobber a refined file, or delete human-written notes. In order:","","1. Read `.d360-writer/DEV-HINTS-GUIDE.md` \u2014 the full dev\u2192docs hand-off protocol (Parts 1\u20134). Follow it.","2. Architecture brief in `.d360-writer/architecture/` (per the guide). First read"," `.d360-writer/architecture/CREATOR-NOTES.md` and treat it as authoritative (it may be empty; never"," modify it). Then:"," \u2022 If no brief exists yet, write it now \u2014 a USER-DOC-enabling brief: product intent, user-facing"," surfaces, features and why a user reaches for each, roles/permission gating, core workflows,"," terminology \u2014 every claim citing the source file(s) that prove it. Skip internals (services, data"," access, infra, build/CI, tests)."," \u2022 If a brief already exists, UPDATE it to match the current code: add new surfaces/features, correct"," what changed, remove what was deleted \u2014 but preserve accurate existing content and any human edits."," Do NOT rewrite it from scratch. If nothing changed, leave it as is.","3. Persist the ongoing rule so teammates' agents inherit it: this repo's agent-memory file (CLAUDE.md,"," or AGENTS.md \u2014 whichever this repo already uses; create CLAUDE.md if neither exists) should contain a",' short "Documentation hand-off" section saying: on every user-facing change, drop a hint file in'," `.d360-writer/hints/` and keep `.d360-writer/architecture/` current; AND each session, check"," `.d360-writer/messages/` for open questions from the docs agent and answer them \u2014 all per"," `.d360-writer/DEV-HINTS-GUIDE.md`. If that section is already there, leave it (refine in place only if"," wrong) \u2014 do NOT add a duplicate. Then commit `.d360-writer/` and the memory-file change ONLY if"," something actually changed.","4. Two-way messages (Part 3): check `.d360-writer/messages/` for open questions addressed to you"," (`from: docs`, `status: open`). Answer each \u2014 fold durable facts into the architecture brief and/or drop"," a reply (`from: dev`, `kind: answer`, `re: <id>`) \u2014 then set the question's frontmatter `status: done`."," If there are none, continue.",'5. Then ask me: "Any tribal knowledge that would help document this product? (folder map, terminology,',` what to prioritize or skip, gotchas \u2014 or say 'none')". If I give you anything, phrase it clearly and`," append it to `.d360-writer/architecture/CREATOR-NOTES.md` (keep the existing header comment and every"," note already there; only add).",`6. Record completion: write/merge \`.d360-writer/devhints-state.json\` with {"completedVersion": ${ne},`,' "completedAt": "<UTC now>", "completedCommit": "<current HEAD short sha>"} (preserve any other keys'," already in the file, e.g. emittedAt). Include this file in the commit from step 3.","7. When everything is done, print exactly ONE of:",' \u2022 first-time setup: "\u2705 Setup complete \u2014 go back to document360-writer and run: write the docs for this repo"',' \u2022 a refresh (brief/notes already existed): "\u2705 Hand-off refreshed \u2014 back in document360-writer, run: write the docs for this repo"'].join(`
159
- `)}function Yo(e){try{let t=Bc(ui(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}function $n(e,t){let o={...Yo(e)??{},emittedVersion:ne,emittedAt:t};bn(ui(e),`${JSON.stringify(o,null,2)}
160
- `)}function Et(e){let n=Yo(e)?.completedVersion;return typeof n!="number"?{status:"unset"}:{status:n<ne?"stale":"current",completedVersion:n}}function vn(e,t=new Date().toISOString()){let n={guideRefreshed:!1,notifiedBuilder:!1,toVersion:ne};try{if(!li(Go(e)))return n;let o=Yo(e),r=o?.emittedVersion??0;n.completedVersion=o?.completedVersion;let s=o?.completedVersion??0;ne>r&&(kn(ci(e)),bn(Go(e),xn()),$n(e,t),n.guideRefreshed=!0),ne>s&&(zc(e).some(p=>p.kind==="request"&&p.subject.startsWith(ai))||(Yc(e,{id:`onb${ne}`,created:t,from:"docs",kind:"request",re:"",sources:[],subject:`${ai}${ne})`,status:"open",body:[`The dev\u2194docs onboarding protocol moved to v${ne}${s?` (you last completed v${s})`:""}.`,"Re-run the hand-off so the docs agent and this repo stay in step \u2014 run this prompt here:","",pi()].join(`
161
- `)}),n.notifiedBuilder=!0))}catch{}return n}function zo(e){return[...e?.status==="stale"?[`\u26A0 Your builder last completed devhints v${e.completedVersion}; this hand-off is v${ne}.`," Have them run the prompt below again \u2014 it now sets up two-way messages + on-demand docs.",""]:[],"\u2713 Wrote .d360-writer/DEV-HINTS-GUIDE.md + the .d360-writer/architecture/ (brief), .d360-writer/hints/, and .d360-writer/messages/ inboxes.","","\u{1F4CB} Copy this one prompt and run it in your repo's coding agent (Claude Code / Codex / Cursor):",ii,pi(),ii,"","That one run writes the architecture brief, adds the ongoing rule to your agent memory (CLAUDE.md / AGENTS.md)","and commits it so teammates inherit it, answers any open questions the writer left, and asks you for tribal","knowledge \u2014 no manual file edits. It also sets up the two-way channel: the writer can ask you questions","(`/inbox` on its side), and you can ask it to document a session on demand (Part 4 of the guide). When it","finishes, come back here and run: write the docs for this repo"]}var ii,ne,ai,It=to(()=>{"use strict";ii="-".repeat(64),ne=4;ai="Re-run dev\u2194docs onboarding (protocol v"});var yi={};Ma(yi,{doctorCommand:()=>Nt,renderDoctorChecks:()=>wi,runDoctorChecks:()=>gi});import{existsSync as fi}from"node:fs";import{d360GetAll as Xc,getAccessToken as Kc,isExpired as Xo,loadProfileMap as Jc,loadTokens as Qc,packageSkillsDir as mi,projectConfigPath as Zc,readProjectConfig as eu,DEFAULT_DOCS_DIR as tu,resolveActiveProfile as nu,classifyClaudeAuth as ou,CLAUDE_CODE_SETUP_HINT as hi,resolveModelSetting as ru,devHintsGuidePath as su}from"document360-engine";async function gi(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=ou();o.state==="ready"?t.push({level:"ok",label:`Claude auth: ${o.kind==="api"?"API key":"subscription (Claude Code login found)"}`}):o.state==="maybe"?t.push({level:"warn",label:"Claude: no Claude Code login found (may be in the macOS Keychain)",fix:`If a docs turn ends with no output, sign in to Claude Code. ${hi}`}):t.push({level:"fail",label:"Claude: not signed in \u2014 the agent cannot run (no Claude Code credentials on this machine)",fix:hi});let r=ru(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=eu(e);if(!s)return t.push({level:"fail",label:`No ${Zc(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??tu).replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let p=s.authoritativeSourceFiles??[];t.push(p.length>0?{level:"ok",label:`Sources: docs grounded in ${p.length} path(s) (${p.slice(0,3).join(", ")}${p.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let g;try{g=nu(e),t.push({level:"ok",label:`Profile: ${g.name} (${g.connection.name})${g.production?" \u26A0 PRODUCTION":""}`})}catch(w){return t.push({level:"fail",label:`Profile config: ${w.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let b=Qc(g.name);b?Xo(b)&&!b.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Xo(b)?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(b.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 k=Jc(e,g.name);k?k.projectId&&g.project.projectId&&k.projectId!==g.project.projectId?t.push({level:"fail",label:`Category map projectId (${k.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(k.articles).length} articles, ${Object.keys(k.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${g.name}" (created on first /publish)`}),t.push(fi(mi())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${mi()}`,fix:"Reinstall document360-writer (broken install)"});let D=Et(e);if((fi(su(e))||D.status!=="unset")&&(D.status==="current"?t.push({level:"ok",label:`Dev hand-off: builder up to date (devhints v${ne})`}):D.status==="stale"?t.push({level:"warn",label:`Dev hand-off: builder on devhints v${D.completedVersion}, current is v${ne}`,fix:"Run /devhints and have the repo's coding agent re-run the prompt (adds two-way messages + on-demand docs)"}):t.push({level:"warn",label:"Dev hand-off: guide emitted but the builder hasn't completed it",fix:"Have the repo's coding agent run the /devhints prompt (sets up the two-way channel)"})),b&&(!Xo(b)||b.refreshToken)){let w={profile:g.name,connection:g.connection};try{await Kc(w);let v=await Xc(w,"/v3/projects");t.push({level:"ok",label:`API reachable (${g.connection.apiUrl}) \u2014 ${v.length} project(s) visible`})}catch(v){t.push({level:"fail",label:`API call failed: ${v.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function wi(e){let t=[""];for(let r of e){let s=r.level==="ok"?j("\u2713"):r.level==="warn"?M("\u26A0"):x("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${A(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?j(`\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 Nt(e,t){console.log(f("Running checks\u2026"));for(let n of wi(await gi(t.cwd)))console.log(n);return{kind:"continue"}}var Cn=to(()=>{"use strict";It();L()});import{Command as yp}from"commander";import{createRequire as kp}from"node:module";import{AUTH_MODES as bp}from"document360-engine";import{input as tl}from"@inquirer/prompts";import{loginPkce as nl,refreshTokens as ol,toStoredTokens as Jr,clearTokens as rl,decodeJwtClaims as Kr,isExpired as sl,loadTokens as il,saveTokens as Qr,resolveActiveProfile as tn,setProfileProject as al,readProjectConfig as ll}from"document360-engine";L();import{select as qa}from"@inquirer/prompts";import{resolveActiveProfile as Ha,setProfileProject as Ba,resolveProjectId as Ga,listWorkspaces as Va}from"document360-engine";async function vt(e,t){let n=Ha(e,t),o={profile:n.name,connection:n.connection},r=n.project.projectId??Ga(o);return{workspaces:await Va(o,r),projectId:r,projectName:n.project.projectName,profile:n.name,environment:n.connection.name,current:n.project.workspaceId}}var Gr=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function no(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 en(e,t,n,o,r){Ba(e,t,{projectId:n,workspaceId:o,workspaceName:r})}async function Vr(e,t,n){let o;try{o=await vt(e,n)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=no(o.workspaces,t);return r?(en(e,o.profile,o.projectId,r.id,r.name),console.log(j(`\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 ut(e,t){let n;try{n=await vt(e,t)}catch(b){console.log(x(`Could not list workspaces: ${b.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 b of o)console.log(` ${b.id===c?A("\u25CF"):" "} ${Gr(b)} ${f(b.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let p=await qa({message:"Select the Document360 workspace for this repo:",choices:o.map(b=>({name:`${Gr(b)}${b.id===c?" (current)":""}`,value:b.id}))}),g=o.find(b=>b.id===p);en(e,s,r,p,g?.name),console.log(j(`\u2713 Workspace set to "${g?.name??p}" for profile "${s}".`))}L();import{select as Ya}from"@inquirer/prompts";import{resolveActiveProfile as za,setProfileProject as Xa,listProjects as Ka}from"document360-engine";async function dt(e,t){let n=za(e,t),o={profile:n.name,connection:n.connection};return{projects:await Ka(o),profile:n.name,environment:n.connection.name,current:n.project.projectId}}var Yr=e=>`${e.name??e.id}${e.sub_domain?` \xB7 ${e.sub_domain}`:""}`;function zr(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 oo(e,t,n,o){Xa(e,t,{projectId:n,projectName:o,workspaceId:void 0,workspaceName:void 0})}async function Xr(e,t){let n;try{n=await dt(e,t)}catch(g){console.log(x(`Could not list projects: ${g.message}`));return}let{projects:o,profile:r,current:s}=n;if(o.length===0){console.log(f("No projects found for this identity."));return}if(!process.stdin.isTTY){console.log("");for(let g of o)console.log(` ${g.id===s?A("\u25CF"):" "} ${Yr(g)} ${f(g.id)}`);console.log(f("Run: d360-writer project use <name>"));return}let c=await Ya({message:"Select the Document360 project for this repo:",choices:o.map(g=>({name:`${Yr(g)}${g.id===s?" (current)":""}`,value:g.id}))}),p=o.find(g=>g.id===c);oo(e,r,c,p?.name),console.log(j(`\u2713 Project set to "${p?.name??c}" for profile "${r}". Next: pick a workspace.`))}L();import Ja from"picocolors";function Qa(e=process.env){return e.FORCE_HYPERLINK==="0"||!Ja.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 Za(e,t=e,n){return Qa(n)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var el=/https?:\/\/[^\s\x1b]+/g;function Ct(e,t){return e.replace(el,n=>Za(n,n,t))}function Zr(e){return{...Kr(e.idToken)??{},...Kr(e.accessToken)??{}}}function nn(e){let t=Zr(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 on(e){let t=tn(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 nl(n,{manual:e.manual,promptForRedirect:s=>tl({message:s})},s=>console.log(Ct(s))),r=Jr(t.name,o);if(Qr(r),await rn(r,t.name,s=>console.log(f(s))),console.log(""),console.log(j(`\u2713 Logged in to "${t.name}" as ${nn(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{ll(process.cwd())?.profiles?.[t.name]&&(console.log(""),await ut(process.cwd(),t.name))}catch{}}async function rn(e,t,n){let r=Zr(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(tn(process.cwd(),t).project.projectId)return;let c;try{let{projects:p}=await dt(process.cwd(),t);c=p.find(g=>g.id===r)?.name}catch{}al(process.cwd(),t,{projectId:r,projectName:c}),n(` Project "${c??r}" written to profile "${t}".`)}catch{}}async function es(e){let t=tn(process.cwd(),e.profile),n=t.connection,o=il(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 ${A(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${nn(o)}`),sl(o))if(o.refreshToken)try{let r=Jr(t.name,await ol(n,o.refreshToken));Qr(r),console.log(j(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(M(`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(M("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 ts(e){let t=tn(process.cwd(),e.profile);rl(t.name)?console.log(j(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}L();import{readProjectConfig as ns,writeProjectConfig as cl,resolveActiveProfile as ul,loadTokens as dl,isExpired as pl}from"document360-engine";function sn(e){let t=ns(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?A("\u25CF "):" ",s=o.production?M(" \u26A0 PRODUCTION"):"",c=o.connection.environment??"(inline)",p=dl(n),g=p?pl(p)&&!p.refreshToken?M("expired"):f("logged in"):f("not logged in");console.log(`${r}${A(n)} \u2192 ${c}${s} [${g}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function an(e,t){let n=ns(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,cl(n,e);let o=n.profiles[t].production?M(" \u26A0 PRODUCTION"):"";console.log(j(`\u2713 Default profile is now "${t}"${o}`))}function ln(e,t){try{let n=ul(e,t);console.log(""),console.log(`Profile ${A(n.name)}${n.production?M(" \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}}L();import{existsSync as fl,readdirSync as ml,statSync as hl}from"node:fs";import{join as gl}from"node:path";import{apiLogDir as wl}from"document360-engine";function os(){let e=wl();if(console.log(""),console.log(`Document360 API logs: ${A(e)}`),!fl(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=ml(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=(hl(gl(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("")}L();import{createSession as yl,resolveAuth as kl,findByName as bl,slugify as xl,touchSession as $l,upsertSession as vl,resolveActiveProfile as Cl}from"document360-engine";async function rs(e,t,n,o,r,s){let c=kl(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 ${A("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${A("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let p=null;try{p=Cl(e,r)}catch(w){console.error(x(`Document360 profile error: ${w.message}`)),process.exit(2)}p.production&&(console.error(M(`\u26A0 Profile "${p.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 w=bl(e,o);w||(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=w.uuid,console.error(f(`Resuming "${w.name}"`))}let b=yl({cwd:e,resume:g,profileName:r,allowProdWrites:s===!0}),k=g??null,D=1;for await(let w of b.send(t))switch(w.type){case"session":if(!k){k=w.sessionId;let v=new Date().toISOString();vl({uuid:k,name:xl(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:v,updatedAt:v})}break;case"text":process.stdout.write(w.delta);break;case"tool":console.error(te(` \u2699 ${w.name}`));break;case"result":D=w.ok?0:1,console.error(f(`(${w.inputTokens}\u2192${w.outputTokens} tokens`+(w.costUsd>0?`, $${w.costUsd<.01?w.costUsd.toFixed(4):w.costUsd.toFixed(2)}`:"")+")")),w.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${w.message}`)),process.exit(1)}k&&$l(k),process.stdout.write(`
162
- `),process.exit(D)}import{createInterface as Yu}from"node:readline/promises";import{createSession as ur,resolveAuth as zu,getSession as Xu,setTitle as Ku,slugify as Ju,touchSession as Bi,upsertSession as Qu,generateTitle as Zu,resolveActiveProfile as Gi,resolveModelSetting as ed,readProjectConfig as td,decodeJwtClaims as qi,isExpired as nd,loadTokens as od}from"document360-engine";var Pt=[{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json",group:"start"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser)",group:"start"},{name:"project",usage:"/project [name]",desc:"Choose the Document360 project (picker; lists all you can access)",group:"start"},{name:"devhints",usage:"/devhints",desc:"Set up the dev\u2192docs hint channel (guide + paste block for your repo agent)",group:"start"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"draft",usage:"/draft [<feature>]",desc:"Draft a feature's articles from its PRDs/specs in .d360-writer/input/ + the code",group:"docs"},{name:"document",usage:"/document [<what>] [--publish]",desc:"Document a shipped change \u2014 the agent picks the action (new section/article/update); bare: act on builder requests",group:"docs"},{name:"write",usage:"/write [--all|--scope <cat>|<path>] [--run]",desc:"Author the planned articles in parallel (bare = preview + cost; --run starts)",group:"docs"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: status; else author capture specs (bulk also refreshes the capture checklist; --no-setup skips)",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Re-run the data-staging checklist + dev test-id requests (auto-runs after /screenshot --all)",group:"docs"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)",group:"docs"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)",group:"publish"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (what changed)",group:"publish"},{name:"inbox",usage:"/inbox",desc:"Act on messages from the repo's coding agent (requests to document, answers to your questions)",group:"publish"},{name:"catch-up",usage:"/catch-up [--publish]",desc:"Process ALL pending work (hints + requests + drift) \u2192 write \u2192 push to Document360 (bare = drafts; --publish goes live)",group:"publish"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits",group:"publish"},{name:"convert",usage:"/convert [--scope <folder>] [--run]",desc:"Convert tracked articles to DFM (one-off legacy migration)",group:"publish"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch/create a Document360 connection (picker; s = session)",group:"setup"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)",group:"setup"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer",group:"setup"},{name:"logout",usage:"/logout",desc:"Sign out + clear project/workspace selection (then /login to re-pick)",group:"setup"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile",group:"setup"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API",group:"setup"},{name:"update",usage:"/update",desc:"Update d360-writer to the latest version (then relaunch)",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"}],Pl=[{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 cn(){let e=Math.max(...Pt.map(n=>n.usage.length))+2,t=["document360-writer \u2014 commands","","New here? Five steps from a fresh repo to published docs:"," 1. /init \u2192 connect to Document360 & scaffold .d360-writer.json"," 2. /devhints \u2192 get a one-paste hand-off prompt for your source repo"," 3. run that prompt in your repo's coding agent (Claude Code / Codex / Cursor)",' 4. "write the docs for this repo" \u2192 back here, the agent authors the articles'," 5. /publish \u2192 dual-publish to Document360",""," Screenshots are optional \u2014 add them anytime:"," /screenshot --all \u2192 d360-capture capture \u2192 re-/publish"];for(let{key:n,label:o}of Pl){t.push("",o);for(let r of Pt.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 ss(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return Pt.filter(n=>n.name.startsWith(t))}function is(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function ro(){console.log("");for(let e of cn())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}L();import{getSession as Sl}from"document360-engine";async function as(e,t){let n=t.currentUuid(),o=n?Sl(n):void 0;return console.log(o?f(`
158
+ `}function pt(e){dn(Pl(e)),dn(Tl(e)),dn(ls(e)),as(rs(e))||pn(rs(e),Dl()),pn(so(e),us());let t=ao(e);return fs(e,new Date().toISOString()),t}async function ds(e,t){for(let n of ft(pt(t.cwd)))console.log(n);return{kind:"continue"}}function ps(){return[`This is document360-writer's dev hand-off setup (v${ne}).`,'Connect this repository to document360-writer (its "docs agent" \u2014 a separate tool that writes this',"product's user documentation from your code, but needs your intent). This prompt is SAFE TO RUN MORE","THAN ONCE \u2014 paste it again whenever the product changes. At each step: create what is missing, reconcile","what already exists, and never duplicate, clobber a refined file, or delete human-written notes. In order:","","1. Read `.d360-writer/DEV-HINTS-GUIDE.md` \u2014 the full dev\u2192docs hand-off protocol (Parts 1\u20134). Follow it.","2. Architecture brief in `.d360-writer/architecture/` (per the guide). First read"," `.d360-writer/architecture/CREATOR-NOTES.md` and treat it as authoritative (it may be empty; never"," modify it). Then:"," \u2022 If no brief exists yet, write it now \u2014 a USER-DOC-enabling brief: product intent, user-facing"," surfaces, features and why a user reaches for each, roles/permission gating, core workflows,"," terminology \u2014 every claim citing the source file(s) that prove it. Skip internals (services, data"," access, infra, build/CI, tests)."," \u2022 If a brief already exists, UPDATE it to match the current code: add new surfaces/features, correct"," what changed, remove what was deleted \u2014 but preserve accurate existing content and any human edits."," Do NOT rewrite it from scratch. If nothing changed, leave it as is.","3. Persist the ongoing rule so teammates' agents inherit it: this repo's agent-memory file (CLAUDE.md,"," or AGENTS.md \u2014 whichever this repo already uses; create CLAUDE.md if neither exists) should contain a",' short "Documentation hand-off" section saying: on every user-facing change, drop a hint file in'," `.d360-writer/hints/` and keep `.d360-writer/architecture/` current; AND each session, check"," `.d360-writer/messages/` for open questions from the docs agent and answer them \u2014 all per"," `.d360-writer/DEV-HINTS-GUIDE.md`. If that section is already there, leave it (refine in place only if"," wrong) \u2014 do NOT add a duplicate. Then commit `.d360-writer/` and the memory-file change ONLY if"," something actually changed.","4. Two-way messages (Part 3): check `.d360-writer/messages/` for open questions addressed to you"," (`from: docs`, `status: open`). Answer each \u2014 fold durable facts into the architecture brief and/or drop"," a reply (`from: dev`, `kind: answer`, `re: <id>`) \u2014 then set the question's frontmatter `status: done`."," If there are none, continue.",'5. Then ask me: "Any tribal knowledge that would help document this product? (folder map, terminology,',` what to prioritize or skip, gotchas \u2014 or say 'none')". If I give you anything, phrase it clearly and`," append it to `.d360-writer/architecture/CREATOR-NOTES.md` (keep the existing header comment and every"," note already there; only add).",`6. Record completion: write/merge \`.d360-writer/devhints-state.json\` with {"completedVersion": ${ne},`,' "completedAt": "<UTC now>", "completedCommit": "<current HEAD short sha>"} (preserve any other keys'," already in the file, e.g. emittedAt). Include this file in the commit from step 3.","7. When everything is done, print exactly ONE of:",' \u2022 first-time setup: "\u2705 Setup complete \u2014 go back to document360-writer and run: write the docs for this repo"',' \u2022 a refresh (brief/notes already existed): "\u2705 Hand-off refreshed \u2014 back in document360-writer, run: write the docs for this repo"'].join(`
159
+ `)}function io(e){try{let t=Cl(cs(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}function fs(e,t){let o={...io(e)??{},emittedVersion:ne,emittedAt:t};pn(cs(e),`${JSON.stringify(o,null,2)}
160
+ `)}function ao(e){let n=io(e)?.completedVersion;return typeof n!="number"?{status:"unset"}:{status:n<ne?"stale":"current",completedVersion:n}}function fn(e,t=new Date().toISOString()){let n={guideRefreshed:!1,notifiedBuilder:!1,toVersion:ne};try{if(!as(so(e)))return n;let o=io(e),r=o?.emittedVersion??0;n.completedVersion=o?.completedVersion;let s=o?.completedVersion??0;ne>r&&(dn(ls(e)),pn(so(e),us()),fs(e,t),n.guideRefreshed=!0),ne>s&&(Rl(e).some(p=>p.kind==="request"&&p.subject.startsWith(is))||(Sl(e,{id:`onb${ne}`,created:t,from:"docs",kind:"request",re:"",sources:[],subject:`${is}${ne})`,status:"open",body:[`The dev\u2194docs onboarding protocol moved to v${ne}${s?` (you last completed v${s})`:""}.`,"Re-run the hand-off so the docs agent and this repo stay in step \u2014 run this prompt here:","",ps()].join(`
161
+ `)}),n.notifiedBuilder=!0))}catch{}return n}function ft(e){return[...e?.status==="stale"?[`\u26A0 Your builder last completed devhints v${e.completedVersion}; this hand-off is v${ne}.`," Have them run the prompt below again \u2014 it now sets up two-way messages + on-demand docs.",""]:[],"\u2713 Wrote .d360-writer/DEV-HINTS-GUIDE.md + the .d360-writer/architecture/ (brief), .d360-writer/hints/, and .d360-writer/messages/ inboxes.","","\u{1F4CB} Copy this one prompt and run it in your repo's coding agent (Claude Code / Codex / Cursor):",ss,ps(),ss,"","That one run writes the architecture brief, adds the ongoing rule to your agent memory (CLAUDE.md / AGENTS.md)","and commits it so teammates inherit it, answers any open questions the writer left, and asks you for tribal","knowledge \u2014 no manual file edits. It also sets up the two-way channel: the writer can ask you questions","(`/inbox` on its side), and you can ask it to document a session on demand (Part 4 of the guide). When it","finishes, come back here and run: write the docs for this repo"]}var ss,ne,is,mt=eo(()=>{"use strict";ss="-".repeat(64),ne=4;is="Re-run dev\u2194docs onboarding (protocol v"});var gi={};_a(gi,{doctorCommand:()=>_t,renderDoctorChecks:()=>hi,runDoctorChecks:()=>mi});import{existsSync as di}from"node:fs";import{d360GetAll as Yc,getAccessToken as zc,isExpired as Yo,loadProfileMap as Xc,loadTokens as Kc,packageSkillsDir as pi,projectConfigPath as Jc,readProjectConfig as Qc,DEFAULT_DOCS_DIR as Zc,resolveActiveProfile as eu,classifyClaudeAuth as tu,CLAUDE_CODE_SETUP_HINT as fi,resolveModelSetting as nu,devHintsGuidePath as ou}from"document360-engine";async function mi(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=tu();o.state==="ready"?t.push({level:"ok",label:`Claude auth: ${o.kind==="api"?"API key":"subscription (Claude Code login found)"}`}):o.state==="maybe"?t.push({level:"warn",label:"Claude: no Claude Code login found (may be in the macOS Keychain)",fix:`If a docs turn ends with no output, sign in to Claude Code. ${fi}`}):t.push({level:"fail",label:"Claude: not signed in \u2014 the agent cannot run (no Claude Code credentials on this machine)",fix:fi});let r=nu(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=Qc(e);if(!s)return t.push({level:"fail",label:`No ${Jc(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??Zc).replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let p=s.authoritativeSourceFiles??[];t.push(p.length>0?{level:"ok",label:`Sources: docs grounded in ${p.length} path(s) (${p.slice(0,3).join(", ")}${p.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let g;try{g=eu(e),t.push({level:"ok",label:`Profile: ${g.name} (${g.connection.name})${g.production?" \u26A0 PRODUCTION":""}`})}catch(w){return t.push({level:"fail",label:`Profile config: ${w.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let b=Kc(g.name);b?Yo(b)&&!b.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Yo(b)?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(b.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 k=Xc(e,g.name);k?k.projectId&&g.project.projectId&&k.projectId!==g.project.projectId?t.push({level:"fail",label:`Category map projectId (${k.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(k.articles).length} articles, ${Object.keys(k.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${g.name}" (created on first /publish)`}),t.push(di(pi())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${pi()}`,fix:"Reinstall document360-writer (broken install)"});let D=ao(e);if((di(ou(e))||D.status!=="unset")&&(D.status==="current"?t.push({level:"ok",label:`Dev hand-off: builder up to date (devhints v${ne})`}):D.status==="stale"?t.push({level:"warn",label:`Dev hand-off: builder on devhints v${D.completedVersion}, current is v${ne}`,fix:"Run /devhints and have the repo's coding agent re-run the prompt (adds two-way messages + on-demand docs)"}):t.push({level:"warn",label:"Dev hand-off: guide emitted but the builder hasn't completed it",fix:"Have the repo's coding agent run the /devhints prompt (sets up the two-way channel)"})),b&&(!Yo(b)||b.refreshToken)){let w={profile:g.name,connection:g.connection};try{await zc(w);let v=await Yc(w,"/v3/projects");t.push({level:"ok",label:`API reachable (${g.connection.apiUrl}) \u2014 ${v.length} project(s) visible`})}catch(v){t.push({level:"fail",label:`API call failed: ${v.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function hi(e){let t=[""];for(let r of e){let s=r.level==="ok"?j("\u2713"):r.level==="warn"?M("\u26A0"):x("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${A(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?j(`\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 hi(await mi(t.cwd)))console.log(n);return{kind:"continue"}}var vn=eo(()=>{"use strict";mt();L()});import{Command as pp}from"commander";import{createRequire as fp}from"node:module";import{AUTH_MODES as mp}from"document360-engine";import{input as Qa}from"@inquirer/prompts";import{loginPkce as Za,refreshTokens as el,toStoredTokens as Yr,clearTokens as tl,decodeJwtClaims as Vr,isExpired as nl,loadTokens as ol,saveTokens as zr,resolveActiveProfile as nn,setProfileProject as rl,readProjectConfig as sl}from"document360-engine";L();import{select as La}from"@inquirer/prompts";import{resolveActiveProfile as Wa,setProfileProject as Fa,resolveProjectId as qa,listWorkspaces as Ha}from"document360-engine";async function Tt(e,t){let n=Wa(e,t),o={profile:n.name,connection:n.connection},r=n.project.projectId??qa(o);return{workspaces:await Ha(o,r),projectId:r,projectName:n.project.projectName,profile:n.name,environment:n.connection.name,current:n.project.workspaceId}}var Fr=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function to(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 tn(e,t,n,o,r){Fa(e,t,{projectId:n,workspaceId:o,workspaceName:r})}async function qr(e,t,n){let o;try{o=await Tt(e,n)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=to(o.workspaces,t);return r?(tn(e,o.profile,o.projectId,r.id,r.name),console.log(j(`\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 ut(e,t){let n;try{n=await Tt(e,t)}catch(b){console.log(x(`Could not list workspaces: ${b.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 b of o)console.log(` ${b.id===c?A("\u25CF"):" "} ${Fr(b)} ${f(b.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let p=await La({message:"Select the Document360 workspace for this repo:",choices:o.map(b=>({name:`${Fr(b)}${b.id===c?" (current)":""}`,value:b.id}))}),g=o.find(b=>b.id===p);tn(e,s,r,p,g?.name),console.log(j(`\u2713 Workspace set to "${g?.name??p}" for profile "${s}".`))}L();import{select as Ba}from"@inquirer/prompts";import{resolveActiveProfile as Ga,setProfileProject as Va,listProjects as Ya}from"document360-engine";async function dt(e,t){let n=Ga(e,t),o={profile:n.name,connection:n.connection};return{projects:await Ya(o),profile:n.name,environment:n.connection.name,current:n.project.projectId}}var Hr=e=>`${e.name??e.id}${e.sub_domain?` \xB7 ${e.sub_domain}`:""}`;function Br(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 no(e,t,n,o){Va(e,t,{projectId:n,projectName:o,workspaceId:void 0,workspaceName:void 0})}async function Gr(e,t){let n;try{n=await dt(e,t)}catch(g){console.log(x(`Could not list projects: ${g.message}`));return}let{projects:o,profile:r,current:s}=n;if(o.length===0){console.log(f("No projects found for this identity."));return}if(!process.stdin.isTTY){console.log("");for(let g of o)console.log(` ${g.id===s?A("\u25CF"):" "} ${Hr(g)} ${f(g.id)}`);console.log(f("Run: d360-writer project use <name>"));return}let c=await Ba({message:"Select the Document360 project for this repo:",choices:o.map(g=>({name:`${Hr(g)}${g.id===s?" (current)":""}`,value:g.id}))}),p=o.find(g=>g.id===c);no(e,r,c,p?.name),console.log(j(`\u2713 Project set to "${p?.name??c}" for profile "${r}". Next: pick a workspace.`))}L();import za from"picocolors";function Xa(e=process.env){return e.FORCE_HYPERLINK==="0"||!za.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 Ka(e,t=e,n){return Xa(n)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Ja=/https?:\/\/[^\s\x1b]+/g;function St(e,t){return e.replace(Ja,n=>Ka(n,n,t))}function Xr(e){return{...Vr(e.idToken)??{},...Vr(e.accessToken)??{}}}function on(e){let t=Xr(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 rn(e){let t=nn(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 Za(n,{manual:e.manual,promptForRedirect:s=>Qa({message:s})},s=>console.log(St(s))),r=Yr(t.name,o);if(zr(r),await sn(r,t.name,s=>console.log(f(s))),console.log(""),console.log(j(`\u2713 Logged in to "${t.name}" as ${on(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{sl(process.cwd())?.profiles?.[t.name]&&(console.log(""),await ut(process.cwd(),t.name))}catch{}}async function sn(e,t,n){let r=Xr(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(nn(process.cwd(),t).project.projectId)return;let c;try{let{projects:p}=await dt(process.cwd(),t);c=p.find(g=>g.id===r)?.name}catch{}rl(process.cwd(),t,{projectId:r,projectName:c}),n(` Project "${c??r}" written to profile "${t}".`)}catch{}}async function Kr(e){let t=nn(process.cwd(),e.profile),n=t.connection,o=ol(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 ${A(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${on(o)}`),nl(o))if(o.refreshToken)try{let r=Yr(t.name,await el(n,o.refreshToken));zr(r),console.log(j(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(M(`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(M("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 Jr(e){let t=nn(process.cwd(),e.profile);tl(t.name)?console.log(j(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}L();import{readProjectConfig as Qr,writeProjectConfig as il,resolveActiveProfile as al,loadTokens as ll,isExpired as cl}from"document360-engine";function an(e){let t=Qr(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?A("\u25CF "):" ",s=o.production?M(" \u26A0 PRODUCTION"):"",c=o.connection.environment??"(inline)",p=ll(n),g=p?cl(p)&&!p.refreshToken?M("expired"):f("logged in"):f("not logged in");console.log(`${r}${A(n)} \u2192 ${c}${s} [${g}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function ln(e,t){let n=Qr(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,il(n,e);let o=n.profiles[t].production?M(" \u26A0 PRODUCTION"):"";console.log(j(`\u2713 Default profile is now "${t}"${o}`))}function cn(e,t){try{let n=al(e,t);console.log(""),console.log(`Profile ${A(n.name)}${n.production?M(" \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}}L();import{existsSync as ul,readdirSync as dl,statSync as pl}from"node:fs";import{join as fl}from"node:path";import{apiLogDir as ml}from"document360-engine";function Zr(){let e=ml();if(console.log(""),console.log(`Document360 API logs: ${A(e)}`),!ul(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=dl(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=(pl(fl(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("")}L();import{createSession as hl,resolveAuth as gl,findByName as wl,slugify as yl,touchSession as kl,upsertSession as bl,resolveActiveProfile as xl}from"document360-engine";async function es(e,t,n,o,r,s){let c=gl(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 ${A("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${A("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let p=null;try{p=xl(e,r)}catch(w){console.error(x(`Document360 profile error: ${w.message}`)),process.exit(2)}p.production&&(console.error(M(`\u26A0 Profile "${p.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 w=wl(e,o);w||(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=w.uuid,console.error(f(`Resuming "${w.name}"`))}let b=hl({cwd:e,resume:g,profileName:r,allowProdWrites:s===!0}),k=g??null,D=1;for await(let w of b.send(t))switch(w.type){case"session":if(!k){k=w.sessionId;let v=new Date().toISOString();bl({uuid:k,name:yl(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:v,updatedAt:v})}break;case"text":process.stdout.write(w.delta);break;case"tool":console.error(te(` \u2699 ${w.name}`));break;case"result":D=w.ok?0:1,console.error(f(`(${w.inputTokens}\u2192${w.outputTokens} tokens`+(w.costUsd>0?`, $${w.costUsd<.01?w.costUsd.toFixed(4):w.costUsd.toFixed(2)}`:"")+")")),w.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${w.message}`)),process.exit(1)}k&&kl(k),process.stdout.write(`
162
+ `),process.exit(D)}import{createInterface as Gu}from"node:readline/promises";import{createSession as lr,resolveAuth as Vu,getSession as Yu,setTitle as zu,slugify as Xu,touchSession as qi,upsertSession as Ku,generateTitle as Ju,resolveActiveProfile as Hi,resolveModelSetting as Qu,readProjectConfig as Zu,decodeJwtClaims as Wi,isExpired as ed,loadTokens as td}from"document360-engine";var Rt=[{name:"init",usage:"/init",desc:"Pick an environment, scaffold .d360-writer.json, and set up the builder hand-off (prints the one-paste prompt)",group:"start"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser)",group:"start"},{name:"project",usage:"/project [name]",desc:"Choose the Document360 project (picker; lists all you can access)",group:"start"},{name:"devhints",usage:"/devhints",desc:"Re-show/refresh the builder hand-off prompt (set up by /init; run anytime to hand your repo agent the latest)",group:"start"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"draft",usage:"/draft [<feature>]",desc:"Draft a feature's articles from its PRDs/specs in .d360-writer/input/ + the code",group:"docs"},{name:"document",usage:"/document [<what>] [--publish]",desc:"Document a shipped change \u2014 the agent picks the action (new section/article/update); bare: act on builder requests",group:"docs"},{name:"write",usage:"/write [--all|--scope <cat>|<path>] [--run]",desc:"Author the planned articles in parallel (bare = preview + cost; --run starts)",group:"docs"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: status; else author capture specs (bulk also refreshes the capture checklist; --no-setup skips)",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Re-run the data-staging checklist + dev test-id requests (auto-runs after /screenshot --all)",group:"docs"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)",group:"docs"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)",group:"publish"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (what changed)",group:"publish"},{name:"inbox",usage:"/inbox",desc:"Act on messages from the repo's coding agent (requests to document, answers to your questions)",group:"publish"},{name:"catch-up",usage:"/catch-up [--publish]",desc:"Process ALL pending work (hints + requests + drift) \u2192 write \u2192 push to Document360 (bare = drafts; --publish goes live)",group:"publish"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits",group:"publish"},{name:"convert",usage:"/convert [--scope <folder>] [--run]",desc:"Convert tracked articles to DFM (one-off legacy migration)",group:"publish"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch/create a Document360 connection (picker; s = session)",group:"setup"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)",group:"setup"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer",group:"setup"},{name:"logout",usage:"/logout",desc:"Sign out + clear project/workspace selection (then /login to re-pick)",group:"setup"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile",group:"setup"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API",group:"setup"},{name:"update",usage:"/update",desc:"Update d360-writer to the latest version (then relaunch)",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"}],$l=[{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 un(){let e=Math.max(...Rt.map(n=>n.usage.length))+2,t=["document360-writer \u2014 commands","","New here? Four steps from a fresh repo to published docs:"," 1. /init \u2192 connect, scaffold config, AND print a one-paste hand-off prompt for your source repo"," 2. run that prompt in your repo's coding agent (Claude Code / Codex / Cursor)",' 3. "write the docs for this repo" \u2192 back here, the agent authors the articles'," 4. /publish \u2192 dual-publish to Document360",""," Screenshots are optional \u2014 add them anytime:"," /screenshot --all \u2192 d360-capture capture \u2192 re-/publish"];for(let{key:n,label:o}of $l){t.push("",o);for(let r of Rt.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 ts(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return Rt.filter(n=>n.name.startsWith(t))}function ns(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function oo(){console.log("");for(let e of un())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}L();import{getSession as vl}from"document360-engine";async function os(e,t){let n=t.currentUuid(),o=n?vl(n):void 0;return console.log(o?f(`
163
163
  (conversation reset \u2014 "${o.name}" is still available via /resume)
164
164
  `):f(`
165
165
  (conversation reset \u2014 agent will start fresh on the next prompt)
166
- `)),{kind:"clear"}}async function so(){return{kind:"exit"}}L();import{input as St,confirm as Tl}from"@inquirer/prompts";import{basename as ls,join as un}from"node:path";import{existsSync as pt,readFileSync as Rl,readdirSync as Dl,writeFileSync as jl}from"node:fs";import{writeProjectConfig as cs,readProjectConfig as Al,projectConfigPath as us,writerDir as El,DEFAULT_DOCS_DIR as ds,DEFAULT_CAPTURE_DIR as ps,DEFAULT_OUTPUT_DIR as fs}from"document360-engine";function ms(e,t="berlin"){let n=us(e);if(pt(n))return{created:!1,path:n,profileName:""};let o={projectId:ws(e)??ls(e),docsDir:ds,captureDir:ps,outputDir:fs,profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:ys(e)};return cs(o,e),hs(e),{created:!0,path:n,profileName:t}}function hs(e){let t=un(El(e),".gitignore");pt(t)||jl(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
167
- `),"utf8")}async function gs(){let e=process.cwd(),t=us(e);if(pt(t)&&!await Tl({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let n=ws(e)??ls(e),o=await St({message:"Project ID (used to scope sessions, screenshots, etc.):",default:n}),r=await St({message:"Capture directory (where document360-capture .spec.ts files live):",default:ps}),s=await St({message:"Screenshot output directory:",default:fs}),c=await St({message:"Default connection profile name:",default:"berlin"}),p=await St({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),g={projectId:o,docsDir:ds,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:p},production:!1}},defaultProfile:c,authoritativeSourceFiles:ys(e)},b=Al(e);return b?.terminologyGlossary&&(g.terminologyGlossary=b.terminologyGlossary),cs(g,e),hs(e),console.log(""),console.log(j(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${A(`d360-writer login --profile ${c}`)} ${f("(sign in; project & workspace auto-selected)")}`),console.log(` Then in d360-writer run ${A("/devhints")} ${f("(builder hands over an architecture brief \u2014 best docs)")}, and ask: "write the user docs for this repo"`),console.log(""),{kind:"continue"}}function ws(e){let t=un(e,"package.json");if(!pt(t))return null;try{return JSON.parse(Rl(t,"utf8")).name??null}catch{return null}}function ys(e){let t=[];for(let n of["README.md","ARCHITECTURE.md","CLAUDE.md"])pt(un(e,n))&&t.push(n);for(let n of["src","api","docs"]){let o=un(e,n);pt(o)&&!Il(o)&&t.push(n)}return t}function Il(e){try{return Dl(e,{withFileTypes:!0}).filter(n=>n.isDirectory()&&!n.name.startsWith(".")).length>6}catch{return!1}}L();import{readMcpConfig as io,writeMcpConfig as ks}from"document360-engine";async function dn(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Nl(),{kind:"continue"}):t==="add"?(_l(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Ol(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 Nl(){let e=io(),t=Object.keys(e.servers);if(t.length===0){console.log(f(`
166
+ `)),{kind:"clear"}}async function ro(){return{kind:"exit"}}L();mt();import{input as Dt,confirm as jl}from"@inquirer/prompts";import{basename as ms,join as mn}from"node:path";import{existsSync as ht,readFileSync as Al,readdirSync as El,writeFileSync as Il}from"node:fs";import{writeProjectConfig as hs,readProjectConfig as Nl,projectConfigPath as gs,writerDir as _l,DEFAULT_DOCS_DIR as ws,DEFAULT_CAPTURE_DIR as ys,DEFAULT_OUTPUT_DIR as ks}from"document360-engine";function bs(e,t="berlin"){let n=gs(e);if(ht(n))return{created:!1,path:n,profileName:""};let o={projectId:vs(e)??ms(e),docsDir:ws,captureDir:ys,outputDir:ks,profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:Cs(e)};return hs(o,e),xs(e),{created:!0,path:n,profileName:t}}function xs(e){let t=mn(_l(e),".gitignore");ht(t)||Il(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
167
+ `),"utf8")}async function $s(){let e=process.cwd(),t=gs(e);if(ht(t)&&!await jl({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let n=vs(e)??ms(e),o=await Dt({message:"Project ID (used to scope sessions, screenshots, etc.):",default:n}),r=await Dt({message:"Capture directory (where document360-capture .spec.ts files live):",default:ys}),s=await Dt({message:"Screenshot output directory:",default:ks}),c=await Dt({message:"Default connection profile name:",default:"berlin"}),p=await Dt({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),g={projectId:o,docsDir:ws,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:p},production:!1}},defaultProfile:c,authoritativeSourceFiles:Cs(e)},b=Nl(e);b?.terminologyGlossary&&(g.terminologyGlossary=b.terminologyGlossary),hs(g,e),xs(e);let k=pt(e);console.log(""),console.log(j(`\u2713 Wrote ${t}`)),console.log(""),console.log(`Next: ${A(`d360-writer login --profile ${c}`)} ${f("(sign in; project & workspace auto-selected)")}`),console.log("");for(let D of ft(k))console.log(D);return console.log(""),console.log(`${f("Then ask:")} ${A('"write the user docs for this repo"')}`),console.log(""),{kind:"continue"}}function vs(e){let t=mn(e,"package.json");if(!ht(t))return null;try{return JSON.parse(Al(t,"utf8")).name??null}catch{return null}}function Cs(e){let t=[];for(let n of["README.md","ARCHITECTURE.md","CLAUDE.md"])ht(mn(e,n))&&t.push(n);for(let n of["src","api","docs"]){let o=mn(e,n);ht(o)&&!Ol(o)&&t.push(n)}return t}function Ol(e){try{return El(e,{withFileTypes:!0}).filter(n=>n.isDirectory()&&!n.name.startsWith(".")).length>6}catch{return!1}}L();import{readMcpConfig as lo,writeMcpConfig as Ps}from"document360-engine";async function hn(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Ul(),{kind:"continue"}):t==="add"?(Ml(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Ll(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 Ul(){let e=lo(),t=Object.keys(e.servers);if(t.length===0){console.log(f(`
168
168
  No MCP servers registered. Add one with /mcp add <name> <type> <ref>
169
- `));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(` ${A(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(` ${A(n)} ${f(`(${o.type})`)} ${o.url}${s}`)}}console.log("")}function _l(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=io(),s;if(n==="stdio")s={type:"stdio",command:o[0],args:o.slice(1)};else{let c={};for(let p=1;p<o.length;p++)if(o[p]==="-H"||o[p]==="--header"){let g=o[++p],b=g?.match(/^([^:=]+)[:=](.+)$/);if(!b){console.log(x(`-H expects key:value (no spaces). Got: ${g??"(nothing)"}`));return}c[b[1].trim()]=b[2].trim()}else{console.log(x(`Unexpected argument: ${o[p]}. After the URL, only -H key:value is allowed.`));return}s={type:n,url:o[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,ks(r),console.log(""),console.log(j(`\u2713 Registered "${t}" (${n})`)),console.log(M(" 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 Ol(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let n=io();if(!n.servers[t]){console.log(x(`No server named "${t}".`));return}delete n.servers[t],ks(n),console.log(j(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}L();import{select as Yl}from"@inquirer/prompts";import{computeSyncStatus as go,planPartitions as zl,relatedArticlePaths as Xl,resolveModelForOperation as Kl,runPartitioned as Jl}from"document360-engine";import B from"picocolors";import Ps from"wrap-ansi";import fo from"string-width";L();import Ee from"picocolors";import ao from"wrap-ansi";import lo from"string-width";var bs=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),xs=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),$s=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function Ul(e){let t=e.replace(/\r/g,"").split(`
170
- `),n=[],o=0;for(;o<t.length;){let r=t[o];if(/^\s*```/.test(r)){let p=[];for(o++;o<t.length&&!/^\s*```/.test(t[o]);)p.push(t[o++]);o++,n.push({kind:"code",lines:p});continue}if(bs(r)){n.push({kind:"hr"}),o++;continue}if(r.includes("|")&&o+1<t.length&&xs(t[o+1])){let p=$s(r);o+=2;let g=[];for(;o<t.length&&t[o].includes("|")&&t[o].trim()!=="";)g.push($s(t[o++]));n.push({kind:"table",header:p,rows:g});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){n.push({kind:"heading",level:s[1].length,text:s[2]}),o++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let p=[];for(;o<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[o]);)p.push(t[o++].replace(/^\s*([-*]|\d+\.)\s+/,""));n.push({kind:"list",items:p});continue}if(r.trim()===""){o++;continue}let c=[];for(;o<t.length&&t[o].trim()!==""&&!/^\s*```/.test(t[o])&&!/^(#{1,6})\s/.test(t[o])&&!/^\s*([-*]|\d+\.)\s+/.test(t[o])&&!bs(t[o])&&!(t[o].includes("|")&&o+1<t.length&&xs(t[o+1]));)c.push(t[o++]);n.push({kind:"para",text:c.join(" ")})}return n}function co(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Ee.bold(t.slice(2,-2)):t.startsWith("`")?A(t.slice(1,-1)):Ee.italic(t.slice(1,-1)))}var Ml=(e,t)=>e+" ".repeat(Math.max(0,t-lo(e)));function Ll(e,t,n){let o=e.length,r=e.map((C,N)=>Math.max(lo(C),...t.map(_=>lo(_[N]??"")))),s=Math.max(24,n),c=3*o+1,p=[...r],g=()=>p.reduce((C,N)=>C+N,0)+c,b=0;for(;g()>s&&b++<1e4;){let C=-1,N=6;for(let _=0;_<o;_++)p[_]>N&&(N=p[_],C=_);if(C===-1)break;p[C]-=1}let k=(C,N,_)=>Ee.gray(C+p.map(F=>"\u2500".repeat(F+2)).join(N)+_),D=Ee.gray("\u2502"),w=(C,N)=>{let _=p.map(($e,oe)=>{let ke=C[oe]??"",se=N?Ee.bold(ke):co(ke);return ao(se,$e,{hard:!0}).split(`
171
- `)}),F=Math.max(..._.map($e=>$e.length)),ee=[];for(let $e=0;$e<F;$e++)ee.push(p.map((oe,ke)=>`${D} ${Ml(_[ke][$e]??"",oe)} `).join("")+D);return ee.join(`
169
+ `));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(` ${A(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(` ${A(n)} ${f(`(${o.type})`)} ${o.url}${s}`)}}console.log("")}function Ml(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=lo(),s;if(n==="stdio")s={type:"stdio",command:o[0],args:o.slice(1)};else{let c={};for(let p=1;p<o.length;p++)if(o[p]==="-H"||o[p]==="--header"){let g=o[++p],b=g?.match(/^([^:=]+)[:=](.+)$/);if(!b){console.log(x(`-H expects key:value (no spaces). Got: ${g??"(nothing)"}`));return}c[b[1].trim()]=b[2].trim()}else{console.log(x(`Unexpected argument: ${o[p]}. After the URL, only -H key:value is allowed.`));return}s={type:n,url:o[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,Ps(r),console.log(""),console.log(j(`\u2713 Registered "${t}" (${n})`)),console.log(M(" 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 Ll(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let n=lo();if(!n.servers[t]){console.log(x(`No server named "${t}".`));return}delete n.servers[t],Ps(n),console.log(j(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}L();import{select as Kl}from"@inquirer/prompts";import{computeSyncStatus as yo,planPartitions as Jl,relatedArticlePaths as Ql,resolveModelForOperation as Zl,runPartitioned as ec}from"document360-engine";import B from"picocolors";import As from"wrap-ansi";import ho from"string-width";L();import Ee from"picocolors";import co from"wrap-ansi";import uo from"string-width";var Ts=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Ss=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Rs=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function Wl(e){let t=e.replace(/\r/g,"").split(`
170
+ `),n=[],o=0;for(;o<t.length;){let r=t[o];if(/^\s*```/.test(r)){let p=[];for(o++;o<t.length&&!/^\s*```/.test(t[o]);)p.push(t[o++]);o++,n.push({kind:"code",lines:p});continue}if(Ts(r)){n.push({kind:"hr"}),o++;continue}if(r.includes("|")&&o+1<t.length&&Ss(t[o+1])){let p=Rs(r);o+=2;let g=[];for(;o<t.length&&t[o].includes("|")&&t[o].trim()!=="";)g.push(Rs(t[o++]));n.push({kind:"table",header:p,rows:g});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){n.push({kind:"heading",level:s[1].length,text:s[2]}),o++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let p=[];for(;o<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[o]);)p.push(t[o++].replace(/^\s*([-*]|\d+\.)\s+/,""));n.push({kind:"list",items:p});continue}if(r.trim()===""){o++;continue}let c=[];for(;o<t.length&&t[o].trim()!==""&&!/^\s*```/.test(t[o])&&!/^(#{1,6})\s/.test(t[o])&&!/^\s*([-*]|\d+\.)\s+/.test(t[o])&&!Ts(t[o])&&!(t[o].includes("|")&&o+1<t.length&&Ss(t[o+1]));)c.push(t[o++]);n.push({kind:"para",text:c.join(" ")})}return n}function po(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Ee.bold(t.slice(2,-2)):t.startsWith("`")?A(t.slice(1,-1)):Ee.italic(t.slice(1,-1)))}var Fl=(e,t)=>e+" ".repeat(Math.max(0,t-uo(e)));function ql(e,t,n){let o=e.length,r=e.map((C,N)=>Math.max(uo(C),...t.map(_=>uo(_[N]??"")))),s=Math.max(24,n),c=3*o+1,p=[...r],g=()=>p.reduce((C,N)=>C+N,0)+c,b=0;for(;g()>s&&b++<1e4;){let C=-1,N=6;for(let _=0;_<o;_++)p[_]>N&&(N=p[_],C=_);if(C===-1)break;p[C]-=1}let k=(C,N,_)=>Ee.gray(C+p.map(F=>"\u2500".repeat(F+2)).join(N)+_),D=Ee.gray("\u2502"),w=(C,N)=>{let _=p.map(($e,oe)=>{let ke=C[oe]??"",se=N?Ee.bold(ke):po(ke);return co(se,$e,{hard:!0}).split(`
171
+ `)}),F=Math.max(..._.map($e=>$e.length)),ee=[];for(let $e=0;$e<F;$e++)ee.push(p.map((oe,ke)=>`${D} ${Fl(_[ke][$e]??"",oe)} `).join("")+D);return ee.join(`
172
172
  `)},v=[k("\u250C","\u252C","\u2510"),w(e,!0)];return v.push(t.length===0?k("\u2514","\u2534","\u2518"):k("\u251C","\u253C","\u2524")),t.forEach((C,N)=>{v.push(w(C,!1)),v.push(N===t.length-1?k("\u2514","\u2534","\u2518"):k("\u251C","\u253C","\u2524"))}),v.join(`
173
- `)}function Wl(e,t){switch(e.kind){case"heading":return Ee.bold(e.text);case"hr":return Ee.gray("\u2500".repeat(t));case"para":return ao(co(e.text),t);case"list":return e.items.map(n=>{let[o="",...r]=ao(co(n),Math.max(10,t-4)).split(`
173
+ `)}function Hl(e,t){switch(e.kind){case"heading":return Ee.bold(e.text);case"hr":return Ee.gray("\u2500".repeat(t));case"para":return co(po(e.text),t);case"list":return e.items.map(n=>{let[o="",...r]=co(po(n),Math.max(10,t-4)).split(`
174
174
  `);return" \u2022 "+o+r.map(s=>`
175
175
  `+s).join("")}).join(`
176
176
  `);case"code":return e.lines.map(n=>Ee.gray(" "+n)).join(`
177
- `);case"table":return Ll(e.header,e.rows,t)}}function uo(e,t){let n=Math.max(20,t);return Ul(e).map(o=>Wl(o,n)).join(`
177
+ `);case"table":return ql(e.header,e.rows,t)}}function fo(e,t){let n=Math.max(20,t);return Wl(e).map(o=>Hl(o,n)).join(`
178
178
 
179
- `)}L();import Fl from"picocolors";var ql=[127,86,217],Hl=[22,38,43],vs={T:ql,E:Hl},Tt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],po=([e,t,n])=>`\x1B[38;2;${e};${t};${n}m`,Bl=([e,t,n])=>`\x1B[48;2;${e};${t};${n}m`;function Cs(){if(!Fl.isColorSupported)return[];let e=Tt[0].length,t=[];for(let n=0;n<Tt.length;n+=2){let o="";for(let r=0;r<e;r++){let s=vs[Tt[n][r]],c=n+1<Tt.length?vs[Tt[n+1][r]]:void 0;s&&c?o+=`${po(s)}${Bl(c)}\u2580\x1B[49m\x1B[39m`:s?o+=`${po(s)}\u2580\x1B[39m`:c?o+=`${po(c)}\u2584\x1B[39m`:o+=" "}t.push(o)}return t}function Rt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function le(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Ie(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var ft=(e,t)=>` ${e.padEnd(13)}${t}`;function Gl(e,t){let n=[$t("\u270E document360-writer")+B.gray(` v${e.version}`),B.gray(" Reads your code, writes your docs."),"",ft("Claude:",`${e.claude}${B.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),ft("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")),ft("Profile:",e.configured?`${e.profile}${B.gray(` (${e.apiUrl})`)}${e.prod?B.bold(B.yellow(" \u26A0 PRODUCTION")):""}`:B.gray("\u2014 (run /init)")),ft("Project:",e.project),ft("Mode:",B.gray(e.mode)),ft("cwd:",B.gray(e.cwd))],o=Cs();if(o.length===0)return n.join(`
180
- `);let r=2,s=3,c=fo(o[0]);if(!(t>=r+c+s+Math.max(...n.map(k=>fo(k)))))return[...o.map(k=>" "+k),...n].join(`
179
+ `)}L();import Bl from"picocolors";var Gl=[127,86,217],Vl=[22,38,43],Ds={T:Gl,E:Vl},jt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],mo=([e,t,n])=>`\x1B[38;2;${e};${t};${n}m`,Yl=([e,t,n])=>`\x1B[48;2;${e};${t};${n}m`;function js(){if(!Bl.isColorSupported)return[];let e=jt[0].length,t=[];for(let n=0;n<jt.length;n+=2){let o="";for(let r=0;r<e;r++){let s=Ds[jt[n][r]],c=n+1<jt.length?Ds[jt[n+1][r]]:void 0;s&&c?o+=`${mo(s)}${Yl(c)}\u2580\x1B[49m\x1B[39m`:s?o+=`${mo(s)}\u2580\x1B[39m`:c?o+=`${mo(c)}\u2584\x1B[39m`:o+=" "}t.push(o)}return t}function At(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function le(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Ie(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var gt=(e,t)=>` ${e.padEnd(13)}${t}`;function zl(e,t){let n=[Pt("\u270E document360-writer")+B.gray(` v${e.version}`),B.gray(" Reads your code, writes your docs."),"",gt("Claude:",`${e.claude}${B.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),gt("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")),gt("Profile:",e.configured?`${e.profile}${B.gray(` (${e.apiUrl})`)}${e.prod?B.bold(B.yellow(" \u26A0 PRODUCTION")):""}`:B.gray("\u2014 (run /init)")),gt("Project:",e.project),gt("Mode:",B.gray(e.mode)),gt("cwd:",B.gray(e.cwd))],o=js();if(o.length===0)return n.join(`
180
+ `);let r=2,s=3,c=ho(o[0]);if(!(t>=r+c+s+Math.max(...n.map(k=>ho(k)))))return[...o.map(k=>" "+k),...n].join(`
181
181
  `);let g=Math.max(0,Math.floor((n.length-o.length)/2)),b=[];for(let k=0;k<Math.max(o.length+g,n.length);k++){let D=o[k-g]??" ".repeat(c);b.push((" ".repeat(r)+D+" ".repeat(s)+(n[k]??"")).trimEnd())}return b.join(`
182
- `)}var Vl={error:B.red,warn:B.yellow,ok:B.green,info:B.gray};function mo(e,t){let n=Math.max(20,t);switch(e.kind){case"banner":return Gl(e.info,n);case"user":{let o="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,n-4),c=50,p=e.text.split(`
183
- `).flatMap(b=>Ps(b,s,{hard:!0}).split(`
182
+ `)}var Xl={error:B.red,warn:B.yellow,ok:B.green,info:B.gray};function go(e,t){let n=Math.max(20,t);switch(e.kind){case"banner":return zl(e.info,n);case"user":{let o="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,n-4),c=50,p=e.text.split(`
183
+ `).flatMap(b=>As(b,s,{hard:!0}).split(`
184
184
  `)),g=Math.max(0,p.length-c);return g>0&&(p=[...p.slice(0,c),B.dim(`\u2026 +${g} more lines`)]),`
185
- `+p.map((b,k)=>o+(k===0?A(" \u276F "):" ")+b+" ".repeat(Math.max(0,s-fo(b))+1)+r).join(`
185
+ `+p.map((b,k)=>o+(k===0?A(" \u276F "):" ")+b+" ".repeat(Math.max(0,s-ho(b))+1)+r).join(`
186
186
  `)}case"assistant":return`
187
- `+uo(e.text,n);case"tool":{let o=e.arg!==null?B.gray(`${e.sep}(${e.arg})`):"";return`
188
- `+Ps(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(`
187
+ `+fo(e.text,n);case"tool":{let o=e.arg!==null?B.gray(`${e.sep}(${e.arg})`):"";return`
188
+ `+As(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(`
189
189
  `)}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(`
190
- `)}case"link":return e.lines.map(o=>A(Ct(` \u2B95 ${o}`))).join(`
190
+ `)}case"link":return e.lines.map(o=>A(St(` \u2B95 ${o}`))).join(`
191
191
  `);case"preview":return`
192
- `+$t(`\u25A3 Preview \u2014 ${e.name}`)+`
193
-
194
- `+uo(e.text,n);case"note":return`
195
- `+Vl[e.tone](Ct(e.text));case"done":return`
196
- `+(e.ok?B.magenta("\u2736 "):B.red("\u2736 "))+B.gray(`Cooked for ${Rt(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${le(e.costUsd)}`:""))}}function Ss(e,t){return e.map(n=>mo(n,t)).join(`
197
- `)}var Dt=3;function wo(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(`
198
- `)}var Ts={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function jt(e){return e.filter(t=>t.path!==null&&t.status in Ts).map(t=>({path:t.path,label:Ts[t.status]}))}function Ql(e){let t=e.replace(/\\/g,"/").split("/").pop()??e,n=t.replace(/^\d+[-_]/,"").replace(/[-_]+/g," ").trim();return n?n.charAt(0).toUpperCase()+n.slice(1):t}function Rs(e){let t=[];e.length>1&&t.push({kind:"all",label:`Publish all ${e.length} articles`,paths:e.map(o=>o.path),indent:!1});let n=new Map;for(let o of e){let r=o.path.replace(/\\/g,"/").split("/").slice(0,-1).join("/"),s=n.get(r);s?s.push(o):n.set(r,[o])}for(let o of[...n.keys()].sort()){let r=n.get(o).sort((s,c)=>s.path.localeCompare(c.path));t.push({kind:"category",label:`${Ql(o)} (${r.length})`,paths:r.map(s=>s.path),indent:!1});for(let s of r){let c=s.path.replace(/\\/g,"/").split("/").pop()??s.path;t.push({kind:"article",label:c,paths:[s.path],indent:!0,note:s.label})}}return t}function pn(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(`
199
- `)}function yo(e,t,n){let o=w=>t[w.index]?.paths.length??0,r=e.filter(w=>w.ok),s=e.filter(w=>!w.ok),c=e.reduce((w,v)=>w+o(v),0),p=r.reduce((w,v)=>w+o(v),0),g=n==="api"?`${le(e.reduce((w,v)=>w+v.costUsd,0))} total`:Ie(e.reduce((w,v)=>w+v.outputTokens,0)),b=`${r.length}/${e.length} partition${e.length===1?"":"s"}`,k=w=>`${w} article${w===1?"":"s"}`,D=[];if(s.length===0)D.push(`Published ${k(c)} (${b}) successfully \xB7 ${g}.`),D.push("All are DRAFTS \u2014 review and publish them in the Document360 portal.");else{D.push(`Published ${p}/${k(c)} (${b}) \xB7 ${g}.`),D.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /publish --all to retry just the rest (published articles are skipped):`);for(let w of s)D.push(` \u2717 ${w.label}${w.error?` \u2014 ${w.error}`:""}`)}return D}async function ho(e,t){let n=zl(t),{model:o}=Kl(e.cwd,"standard");console.log(f(`Publishing ${t.length} article${t.length===1?"":"s"} across ${n.length} partition${n.length===1?"":"s"} (\u2264${Dt} agents at once) on ${o}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let r of Jl({cwd:e.cwd,partitions:n,promptFor:s=>pn(s.paths),concurrency:Dt,profileName:e.profileName,allowProdWrites:e.allowProdWrites(),model:o}))if(r.type==="partition_status")r.status==="running"?console.log(f(` \u25B8 ${r.label} \u2014 publishing\u2026`)):r.status==="done"?console.log(j(` \u2713 ${r.label}`)):console.log(x(` \u2717 ${r.label}`));else if(r.type==="run_done"){console.log("");let s=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let c of yo(r.results,n,s))console.log(r.ok?j(c):M(c))}}catch(r){console.log(x(`Publish run failed: ${r.message}`))}return console.log(""),{kind:"continue"}}async function fn(e,t,n){let o=Xl(e,n);if(o.length===0)return[];let r=new Set(jt((await go({cwd:e,profileName:t})).entries).map(s=>s.path));return o.filter(s=>r.has(s))}async function Ds(e,t){let n=e[0];if(n==="--related"&&e[1]){if(!t)return{kind:"continue"};let o=await fn(t.cwd,t.profileName,e[1].replace(/\\/g,"/"));return o.length===0?(console.log(j("\u2713 All related articles are already published.")),{kind:"continue"}):await ho(t,o)}if(n==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let o=jt((await go({cwd:t?.cwd??process.cwd()})).entries);return o.length===0?(console.log(j("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):t?await ho(t,o.map(r=>r.path)):{kind:"forward-to-agent",prompt:pn(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=jt((await go({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(j("\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(()=>Yl({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 await ho(t,o.map(r=>r.path))}return{kind:"forward-to-agent",prompt:wo(n),display:`/publish ${n}`}}async function mn(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","2.5. d360_pending_hints \u2014 the source agent's own notes on what it shipped. Verify each (sources exist, commit is an ancestor of HEAD, source shows the surface) before trusting; fold verified ones into the table, flag the rest.","3. Map changes (diff + hints) to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit and stamp consumed hints.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
200
- `),display:"/audit"}}L();import{checkbox as Zl}from"@inquirer/prompts";import{inventoryRepo as ec,readProjectConfig as js,writeProjectConfig as tc}from"document360-engine";function ko(e,t){let n=js(e);n&&(n.authoritativeSourceFiles=t,tc(n,e))}function hn(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 As(e,t){let n=t?.cwd??process.cwd();if(!js(n))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=ec(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(A("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of o)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(hn(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>Zl({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:o.map(s=>({name:`${s.path} (${hn(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"};ko(n,r),console.log(j(`\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"}}L();import{confirm as ic}from"@inquirer/prompts";import{applyPull as Os,computeSyncStatus as Us,planPull as ac,D360AuthError as lc}from"document360-engine";L();var Es=[{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:M,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:A,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:te,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:te,mark:"\xB7"}];function nc(e){return e.path?e.path:`${e.title??"(untitled)"} ${te(`[${e.articleId}]`)}`}function gn(e){let t=[];for(let c of Es){let p=e.entries.filter(g=>g.status===c.status);if(p.length!==0){t.push(""),t.push(Pe(c.header));for(let g of p)t.push(` ${c.paint(c.mark)} ${c.paint(nc(g))}${g.detail?te(` (${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?j("\u2713 everything in sync"):[n>0?j(`\u2713 ${n} in sync`):null,...Es.map(c=>{let p=e.counts[c.status]??0;return p>0?`${p} ${r[c.status]}`:null})].filter(Boolean).join(te(" \xB7 "));return t.push(s+te(` \xB7 ${o} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as oc}from"diff";import Is from"picocolors";var Ns=80,rc="\x1B[48;2;74;28;28m",sc="\x1B[48;2;24;66;24m",_s="\x1B[49m",bo=e=>String(e).padStart(5);function Ge(e,t,n){let o=w=>{let v=w.replace(/\r\n/g,`
192
+ `+Pt(`\u25A3 Preview \u2014 ${e.name}`)+`
193
+
194
+ `+fo(e.text,n);case"note":return`
195
+ `+Xl[e.tone](St(e.text));case"done":return`
196
+ `+(e.ok?B.magenta("\u2736 "):B.red("\u2736 "))+B.gray(`Cooked for ${At(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${le(e.costUsd)}`:""))}}function Es(e,t){return e.map(n=>go(n,t)).join(`
197
+ `)}var Et=3;function ko(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(`
198
+ `)}var Is={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function It(e){return e.filter(t=>t.path!==null&&t.status in Is).map(t=>({path:t.path,label:Is[t.status]}))}function tc(e){let t=e.replace(/\\/g,"/").split("/").pop()??e,n=t.replace(/^\d+[-_]/,"").replace(/[-_]+/g," ").trim();return n?n.charAt(0).toUpperCase()+n.slice(1):t}function Ns(e){let t=[];e.length>1&&t.push({kind:"all",label:`Publish all ${e.length} articles`,paths:e.map(o=>o.path),indent:!1});let n=new Map;for(let o of e){let r=o.path.replace(/\\/g,"/").split("/").slice(0,-1).join("/"),s=n.get(r);s?s.push(o):n.set(r,[o])}for(let o of[...n.keys()].sort()){let r=n.get(o).sort((s,c)=>s.path.localeCompare(c.path));t.push({kind:"category",label:`${tc(o)} (${r.length})`,paths:r.map(s=>s.path),indent:!1});for(let s of r){let c=s.path.replace(/\\/g,"/").split("/").pop()??s.path;t.push({kind:"article",label:c,paths:[s.path],indent:!0,note:s.label})}}return t}function gn(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(`
199
+ `)}function bo(e,t,n){let o=w=>t[w.index]?.paths.length??0,r=e.filter(w=>w.ok),s=e.filter(w=>!w.ok),c=e.reduce((w,v)=>w+o(v),0),p=r.reduce((w,v)=>w+o(v),0),g=n==="api"?`${le(e.reduce((w,v)=>w+v.costUsd,0))} total`:Ie(e.reduce((w,v)=>w+v.outputTokens,0)),b=`${r.length}/${e.length} partition${e.length===1?"":"s"}`,k=w=>`${w} article${w===1?"":"s"}`,D=[];if(s.length===0)D.push(`Published ${k(c)} (${b}) successfully \xB7 ${g}.`),D.push("All are DRAFTS \u2014 review and publish them in the Document360 portal.");else{D.push(`Published ${p}/${k(c)} (${b}) \xB7 ${g}.`),D.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /publish --all to retry just the rest (published articles are skipped):`);for(let w of s)D.push(` \u2717 ${w.label}${w.error?` \u2014 ${w.error}`:""}`)}return D}async function wo(e,t){let n=Jl(t),{model:o}=Zl(e.cwd,"standard");console.log(f(`Publishing ${t.length} article${t.length===1?"":"s"} across ${n.length} partition${n.length===1?"":"s"} (\u2264${Et} agents at once) on ${o}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let r of ec({cwd:e.cwd,partitions:n,promptFor:s=>gn(s.paths),concurrency:Et,profileName:e.profileName,allowProdWrites:e.allowProdWrites(),model:o}))if(r.type==="partition_status")r.status==="running"?console.log(f(` \u25B8 ${r.label} \u2014 publishing\u2026`)):r.status==="done"?console.log(j(` \u2713 ${r.label}`)):console.log(x(` \u2717 ${r.label}`));else if(r.type==="run_done"){console.log("");let s=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let c of bo(r.results,n,s))console.log(r.ok?j(c):M(c))}}catch(r){console.log(x(`Publish run failed: ${r.message}`))}return console.log(""),{kind:"continue"}}async function wn(e,t,n){let o=Ql(e,n);if(o.length===0)return[];let r=new Set(It((await yo({cwd:e,profileName:t})).entries).map(s=>s.path));return o.filter(s=>r.has(s))}async function _s(e,t){let n=e[0];if(n==="--related"&&e[1]){if(!t)return{kind:"continue"};let o=await wn(t.cwd,t.profileName,e[1].replace(/\\/g,"/"));return o.length===0?(console.log(j("\u2713 All related articles are already published.")),{kind:"continue"}):await wo(t,o)}if(n==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let o=It((await yo({cwd:t?.cwd??process.cwd()})).entries);return o.length===0?(console.log(j("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):t?await wo(t,o.map(r=>r.path)):{kind:"forward-to-agent",prompt:gn(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=It((await yo({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(j("\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(()=>Kl({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 await wo(t,o.map(r=>r.path))}return{kind:"forward-to-agent",prompt:ko(n),display:`/publish ${n}`}}async function yn(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","2.5. d360_pending_hints \u2014 the source agent's own notes on what it shipped. Verify each (sources exist, commit is an ancestor of HEAD, source shows the surface) before trusting; fold verified ones into the table, flag the rest.","3. Map changes (diff + hints) to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit and stamp consumed hints.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
200
+ `),display:"/audit"}}L();import{checkbox as nc}from"@inquirer/prompts";import{inventoryRepo as oc,readProjectConfig as Os,writeProjectConfig as rc}from"document360-engine";function xo(e,t){let n=Os(e);n&&(n.authoritativeSourceFiles=t,rc(n,e))}function kn(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 Us(e,t){let n=t?.cwd??process.cwd();if(!Os(n))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=oc(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(A("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of o)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(kn(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>nc({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:o.map(s=>({name:`${s.path} (${kn(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"};xo(n,r),console.log(j(`\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"}}L();import{confirm as cc}from"@inquirer/prompts";import{applyPull as qs,computeSyncStatus as Hs,planPull as uc,D360AuthError as dc}from"document360-engine";L();var Ms=[{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:M,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:A,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:te,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:te,mark:"\xB7"}];function sc(e){return e.path?e.path:`${e.title??"(untitled)"} ${te(`[${e.articleId}]`)}`}function bn(e){let t=[];for(let c of Ms){let p=e.entries.filter(g=>g.status===c.status);if(p.length!==0){t.push(""),t.push(Pe(c.header));for(let g of p)t.push(` ${c.paint(c.mark)} ${c.paint(sc(g))}${g.detail?te(` (${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?j("\u2713 everything in sync"):[n>0?j(`\u2713 ${n} in sync`):null,...Ms.map(c=>{let p=e.counts[c.status]??0;return p>0?`${p} ${r[c.status]}`:null})].filter(Boolean).join(te(" \xB7 "));return t.push(s+te(` \xB7 ${o} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as ic}from"diff";import Ls from"picocolors";var Ws=80,ac="\x1B[48;2;74;28;28m",lc="\x1B[48;2;24;66;24m",Fs="\x1B[49m",$o=e=>String(e).padStart(5);function Ge(e,t,n){let o=w=>{let v=w.replace(/\r\n/g,`
201
201
  `);return v.endsWith(`
202
202
  `)||v===""?v:v+`
203
- `},r=o(e),s=o(t);if(r===s)return null;let c=Math.max(20,n-10),p=oc("a","b",r,s,"","",{context:3}),g=0,b=0,k=[];p.hunks.forEach((w,v)=>{v>0&&k.push(Is.gray(" \u2026"));let C=w.oldStart,N=w.newStart;for(let _ of w.lines){let F=_[0],ee=_.slice(1).slice(0,c);F==="-"?(b++,k.push(`${rc}${bo(C++)} - ${ee}${_s}`)):F==="+"?(g++,k.push(`${sc}${bo(N++)} + ${ee}${_s}`)):(k.push(Is.gray(bo(N))+" "+ee),C++,N++)}});let D=k.slice(0,Ns);return{added:g,removed:b,lines:D,hidden:Math.max(0,k.length-Ns)}}async function Ms(e,t){let n=(e[0]??"status").toLowerCase();try{if(n==="status")return await cc(t.cwd),{kind:"continue"};if(n==="pull")return await uc(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 lc?console.log(x(o.message)):console.log(x(`Sync failed: ${o.message}`))}return{kind:"continue"}}async function cc(e){console.log(f("Checking Document360 for drift\u2026"));let t=await Us({cwd:e});for(let n of gn(t))console.log(n);console.log("")}async function uc(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 Us({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),o.length===0){console.log(j("\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(`${Pe(String(o.length))} article(s) are remote-ahead.`)}else o=[n.replace(/\\/g,"/")];for(let r of o){let s=await ac({cwd:e.cwd,relPath:r});console.log(""),console.log(`${A("\u25CF")} ${Pe(s.title)} ${te(`(${s.path})`)}`);for(let b of s.notes)console.log(M(` \u26A0 ${b}`));s.overwritesLocalChanges&&console.log(M(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=Ge(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.")),Os({cwd:e.cwd},s);continue}let p=b=>b===1?"":"s";console.log(te(` \u23BF Added ${c.added} line${p(c.added)}, removed ${c.removed} line${p(c.removed)}`));for(let b of c.lines)console.log(b);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>ic({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}Os({cwd:e.cwd},s),console.log(j(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}L();import{statSync as dc}from"node:fs";import{resolve as pc}from"node:path";import{estimateBulkCost as fc,planPartitions as mc,readProjectConfig as hc,resolveModelForOperation as gc,runPartitioned as wc,trackedArticlePaths as yc}from"document360-engine";var xo=3;function $o(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 vo(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function Co(e,t){return t.map(n=>{let o=0;try{o=dc(pc(e,n)).size}catch{o=0}return{path:n,bytes:o}})}function Po(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:
203
+ `},r=o(e),s=o(t);if(r===s)return null;let c=Math.max(20,n-10),p=ic("a","b",r,s,"","",{context:3}),g=0,b=0,k=[];p.hunks.forEach((w,v)=>{v>0&&k.push(Ls.gray(" \u2026"));let C=w.oldStart,N=w.newStart;for(let _ of w.lines){let F=_[0],ee=_.slice(1).slice(0,c);F==="-"?(b++,k.push(`${ac}${$o(C++)} - ${ee}${Fs}`)):F==="+"?(g++,k.push(`${lc}${$o(N++)} + ${ee}${Fs}`)):(k.push(Ls.gray($o(N))+" "+ee),C++,N++)}});let D=k.slice(0,Ws);return{added:g,removed:b,lines:D,hidden:Math.max(0,k.length-Ws)}}async function Bs(e,t){let n=(e[0]??"status").toLowerCase();try{if(n==="status")return await pc(t.cwd),{kind:"continue"};if(n==="pull")return await fc(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 dc?console.log(x(o.message)):console.log(x(`Sync failed: ${o.message}`))}return{kind:"continue"}}async function pc(e){console.log(f("Checking Document360 for drift\u2026"));let t=await Hs({cwd:e});for(let n of bn(t))console.log(n);console.log("")}async function fc(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 Hs({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),o.length===0){console.log(j("\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(`${Pe(String(o.length))} article(s) are remote-ahead.`)}else o=[n.replace(/\\/g,"/")];for(let r of o){let s=await uc({cwd:e.cwd,relPath:r});console.log(""),console.log(`${A("\u25CF")} ${Pe(s.title)} ${te(`(${s.path})`)}`);for(let b of s.notes)console.log(M(` \u26A0 ${b}`));s.overwritesLocalChanges&&console.log(M(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=Ge(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.")),qs({cwd:e.cwd},s);continue}let p=b=>b===1?"":"s";console.log(te(` \u23BF Added ${c.added} line${p(c.added)}, removed ${c.removed} line${p(c.removed)}`));for(let b of c.lines)console.log(b);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>cc({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}qs({cwd:e.cwd},s),console.log(j(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}L();import{statSync as mc}from"node:fs";import{resolve as hc}from"node:path";import{estimateBulkCost as gc,planPartitions as wc,readProjectConfig as yc,resolveModelForOperation as kc,runPartitioned as bc,trackedArticlePaths as xc}from"document360-engine";var vo=3;function Co(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 Po(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function To(e,t){return t.map(n=>{let o=0;try{o=mc(hc(e,n)).size}catch{o=0}return{path:n,bytes:o}})}function So(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:
204
204
  - 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.
205
205
  - 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.
206
206
  - 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.
207
207
  - Work only on these files:
208
208
  `+e.paths.map(t=>`- ${t}`).join(`
209
- `)}function So(e,t,n){let o=e.reduce((p,g)=>p+g.paths.length,0),[r,s]=t.usd;return[`Convert ${o} article${o===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(p=>` \u2022 ${p.label} \u2014 ${p.paths.length} article${p.paths.length===1?"":"s"}`),"",`Estimated cost: ${le(r)}\u2013${le(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function To(e,t,n){let o=C=>t[C.index]?.paths.length??0,r=e.filter(C=>C.ok),s=e.filter(C=>!C.ok),c=e.reduce((C,N)=>C+o(N),0),p=r.reduce((C,N)=>C+o(N),0),g=e.reduce((C,N)=>C+N.costUsd,0),b=e.reduce((C,N)=>C+N.outputTokens,0),k=n==="api"?`${le(g)} total`:Ie(b),D=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,w=C=>`${C} article${C===1?"":"s"}`,v=[];if(s.length===0)v.push(`Converted ${w(c)} (${D}) successfully \xB7 ${k}.`);else{v.push(`Converted ${p}/${w(c)} (${D}) \xB7 ${k}.`),v.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let C of s)v.push(` \u2717 ${C.label}${C.error?` \u2014 ${C.error}`:""}`)}return v}async function Ls(e,t){if(!hc(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:n,run:o}=$o(e),r=yc(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=vo(r,n);if(s.length===0)return console.log(x(`No tracked articles under "${n}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=mc(s),p=`/convert${n?` --scope ${n}`:""} --run`,{model:g,forced:b}=gc(t.cwd,"light");if(!o){let k=fc({files:Co(t.cwd,s),op:"convert",model:g});n&&console.log(f(`Scope: ${n} (${s.length} of ${r.length} tracked articles).`));for(let D of So(c,k,xo))console.log(D);return console.log(f(`Model: ${g}${b?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${p} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${xo} 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 k of wc({cwd:t.cwd,partitions:c,promptFor:Po,concurrency:xo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:g}))if(k.type==="partition_status")k.status==="running"?console.log(f(` \u25B8 ${k.label} \u2014 converting\u2026`)):k.status==="done"?console.log(j(` \u2713 ${k.label}`)):console.log(x(` \u2717 ${k.label}`));else if(k.type==="run_done"){console.log("");let D=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let w of To(k.results,c,D))console.log(k.ok?j(w):M(w))}}catch(k){console.log(x(`Convert run failed: ${k.message}`))}return console.log(""),{kind:"continue"}}L();import{statSync as Ws}from"node:fs";import{resolve as Fs}from"node:path";import{estimateBulkCost as kc,planPartitions as bc,readDocsPlan as xc,readProjectConfig as qs,DEFAULT_DOCS_DIR as $c,resolveModelForOperation as vc,runPartitioned as Cc}from"document360-engine";var Ro=5;function Do(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 Pc(e,t){let n=e.replace(/\\/g,"/").replace(/\/+$/,""),o=`${t.replace(/\/+$/,"")}/`;return n.startsWith(o)?n.slice(o.length):n}function Sc(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function Tc(e,t,n){return n.filter(o=>{try{return Ws(Fs(e,t,o)).size===0}catch{return!0}})}function jo(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+=Ws(Fs(e,c)).size}catch{}return{path:r,bytes:s}})}function Ao(e,t){return`Run the write-article skill to author EACH documentation article listed below. For each path:
209
+ `)}function Ro(e,t,n){let o=e.reduce((p,g)=>p+g.paths.length,0),[r,s]=t.usd;return[`Convert ${o} article${o===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(p=>` \u2022 ${p.label} \u2014 ${p.paths.length} article${p.paths.length===1?"":"s"}`),"",`Estimated cost: ${le(r)}\u2013${le(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function Do(e,t,n){let o=C=>t[C.index]?.paths.length??0,r=e.filter(C=>C.ok),s=e.filter(C=>!C.ok),c=e.reduce((C,N)=>C+o(N),0),p=r.reduce((C,N)=>C+o(N),0),g=e.reduce((C,N)=>C+N.costUsd,0),b=e.reduce((C,N)=>C+N.outputTokens,0),k=n==="api"?`${le(g)} total`:Ie(b),D=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,w=C=>`${C} article${C===1?"":"s"}`,v=[];if(s.length===0)v.push(`Converted ${w(c)} (${D}) successfully \xB7 ${k}.`);else{v.push(`Converted ${p}/${w(c)} (${D}) \xB7 ${k}.`),v.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let C of s)v.push(` \u2717 ${C.label}${C.error?` \u2014 ${C.error}`:""}`)}return v}async function Gs(e,t){if(!yc(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:n,run:o}=Co(e),r=xc(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=Po(r,n);if(s.length===0)return console.log(x(`No tracked articles under "${n}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=wc(s),p=`/convert${n?` --scope ${n}`:""} --run`,{model:g,forced:b}=kc(t.cwd,"light");if(!o){let k=gc({files:To(t.cwd,s),op:"convert",model:g});n&&console.log(f(`Scope: ${n} (${s.length} of ${r.length} tracked articles).`));for(let D of Ro(c,k,vo))console.log(D);return console.log(f(`Model: ${g}${b?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${p} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${vo} 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 k of bc({cwd:t.cwd,partitions:c,promptFor:So,concurrency:vo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:g}))if(k.type==="partition_status")k.status==="running"?console.log(f(` \u25B8 ${k.label} \u2014 converting\u2026`)):k.status==="done"?console.log(j(` \u2713 ${k.label}`)):console.log(x(` \u2717 ${k.label}`));else if(k.type==="run_done"){console.log("");let D=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let w of Do(k.results,c,D))console.log(k.ok?j(w):M(w))}}catch(k){console.log(x(`Convert run failed: ${k.message}`))}return console.log(""),{kind:"continue"}}L();import{statSync as Vs}from"node:fs";import{resolve as Ys}from"node:path";import{estimateBulkCost as $c,planPartitions as vc,readDocsPlan as Cc,readProjectConfig as zs,DEFAULT_DOCS_DIR as Pc,resolveModelForOperation as Tc,runPartitioned as Sc}from"document360-engine";var jo=5;function Ao(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 Rc(e,t){let n=e.replace(/\\/g,"/").replace(/\/+$/,""),o=`${t.replace(/\/+$/,"")}/`;return n.startsWith(o)?n.slice(o.length):n}function Dc(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function jc(e,t,n){return n.filter(o=>{try{return Vs(Ys(e,t,o)).size===0}catch{return!0}})}function Eo(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+=Vs(Ys(e,c)).size}catch{}return{path:r,bytes:s}})}function Io(e,t){return`Run the write-article skill to author EACH documentation article listed below. For each path:
210
210
  - 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).
211
211
  - 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.
212
212
  - 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.
@@ -214,52 +214,53 @@ No MCP servers registered. Add one with /mcp add <name> <type> <ref>
214
214
  - 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.
215
215
  Articles:
216
216
  `+e.paths.map(n=>`- ${n}`).join(`
217
- `)}function Eo(e,t,n){let o=e.reduce((c,p)=>c+p.paths.length,0),[r,s]=t.usd;return[`Write ${o} article${o===1?"":"s"} across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(c=>` \u2022 ${c.label} \u2014 ${c.paths.length} article${c.paths.length===1?"":"s"}`),"",`Estimated cost: ${le(r)}\u2013${le(s)}. ${t.note}`,"","Articles are written as local drafts in the docs tree (nothing is published). Run /write --run to start."]}function Io(e,t,n){let o=C=>t[C.index]?.paths.length??0,r=e.filter(C=>C.ok),s=e.filter(C=>!C.ok),c=e.reduce((C,N)=>C+o(N),0),p=r.reduce((C,N)=>C+o(N),0),g=e.reduce((C,N)=>C+N.costUsd,0),b=e.reduce((C,N)=>C+N.outputTokens,0),k=n==="api"?`${le(g)} total`:Ie(b),D=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,w=C=>`${C} article${C===1?"":"s"}`,v=[];if(s.length===0)v.push(`Wrote ${w(c)} (${D}) successfully \xB7 ${k}.`),v.push("Next \u2014 push them to Document360:"),v.push("/publish --all"),v.push("Or add screenshots first with /screenshot --all. Drafts are reviewable in the portal after publishing.");else{v.push(`Wrote ${p}/${w(c)} (${D}) \xB7 ${k}.`),v.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /write to retry the rest:`);for(let C of s)v.push(` \u2717 ${C.label}${C.error?` \u2014 ${C.error}`:""}`)}return v}function No(e,t,n){let o=(qs(e)?.docsDir??$c).replace(/\/+$/,""),r=t.map(p=>p.path);if(n.path){let p=Pc(n.path,o);return r.includes(p)?{docsDir:o,targets:[p],planCount:r.length}:{docsDir:o,targets:[],planCount:r.length,reason:`"${p}" isn't in the docs plan (.d360-writer/plan.json).`}}let s=Sc(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=Tc(e,o,s);if(c.length===0){let p=n.scope?` under "${n.scope}"`:"";return{docsDir:o,targets:[],planCount:r.length,reason:`All planned articles${p} are already written. Use /write <path> to rewrite one.`}}return{docsDir:o,targets:c,planCount:r.length}}async function Hs(e,t){if(!qs(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let n=xc(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}=Do(e),{docsDir:c,targets:p,reason:g}=No(t.cwd,n,{scope:o,path:r});if(p.length===0)return console.log(g?M(g):M("Nothing to write.")),{kind:"continue"};let b=bc(p),{model:k,forced:D}=vc(t.cwd,"standard");if(!s&&!!!r){let v=kc({files:jo(t.cwd,n,p),op:"write",model:k});o&&console.log(f(`Scope: ${o} (${p.length} pending).`));for(let C of Eo(b,v,Ro))console.log(C);return console.log(f(`Model: ${k}${D?" (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 ${p.length} article${p.length===1?"":"s"} across ${b.length} partition${b.length===1?"":"s"} (\u2264${Ro} agents at once) on ${k}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let v of Cc({cwd:t.cwd,partitions:b,promptFor:C=>Ao(C,c),concurrency:Ro,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:k}))if(v.type==="partition_status")v.status==="running"?console.log(f(` \u25B8 ${v.label} \u2014 writing\u2026`)):v.status==="done"?console.log(j(` \u2713 ${v.label}`)):console.log(x(` \u2717 ${v.label}`));else if(v.type==="run_done"){console.log("");let C=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let N of Io(v.results,b,C))console.log(v.ok?j(N):M(N))}}catch(v){console.log(x(`Write run failed: ${v.message}`))}return console.log(""),{kind:"continue"}}L();import{existsSync as Rc,readdirSync as Bs,statSync as Gs}from"node:fs";import{join as Vs}from"node:path";import{inputDir as Dc,readProjectConfig as jc}from"document360-engine";var Ys=/\.(md|markdown|pdf|txt)$/i;function _o(e){let t=Dc(e);if(!Rc(t))return{features:[],looseDocs:0};let n=[],o=0;for(let r of Bs(t)){let s=Vs(t,r),c=!1;try{c=Gs(s).isDirectory()}catch{continue}if(c){if(r.startsWith("."))continue;let p=Ac(s);p>0&&n.push({name:r,docCount:p})}else Ys.test(r)&&o++}return n.sort((r,s)=>r.name.localeCompare(s.name)),{features:n,looseDocs:o}}function Ac(e){let t=0;for(let n of Bs(e))try{Gs(Vs(e,n)).isFile()&&Ys.test(n)&&t++}catch{}return t}function Oo(e,t){let n=t.trim().toLowerCase();return n?e.find(o=>o.name.toLowerCase()===n)?.name??e.find(o=>o.name.toLowerCase().startsWith(n))?.name??e.find(o=>o.name.toLowerCase().includes(n))?.name??null:null}function wn(e,t){return["Run the draft-from-prd skill.","",`Target scope: ${e?`the feature documents in .d360-writer/input/${e}/`:t?`the input docs in .d360-writer/input/ relevant to: "${t}"`:"the input docs in .d360-writer/input/"}.`,t&&!e?`Use "${t}" as the feature framing.`:"","","Follow the skill: (1) read those PRDs/specs as the primary source (intent, user-facing surface,","terminology, user stories, prerequisites); (2) locate and read the CODE that implements that surface","to pin exact UI strings/routes/states \u2014 never fabricate; flag anything the PRD claims that you","cannot find in source; (3) propose a tight feature-scoped article set (\u22483\u20135) and pin it to",".d360-writer/plan.json + AGENT-PLAN.md, with each article's `sources` listing BOTH the PRD file(s)","AND the grounding code files; (4) flag overlaps with existing articles (do not merge). Then stop and","offer /write --all. Do NOT write the articles in this turn."].filter(Boolean).join(`
218
- `)}var Uo="No input docs found. Drop this feature's PRDs/specs in .d360-writer/input/<feature>/ (.md or .pdf), then run /draft.";async function zs(e,t){let n=t?.cwd??process.cwd();if(!jc(n))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{features:o,looseDocs:r}=_o(n);if(o.length===0&&r===0)return console.log(f(Uo)),{kind:"continue"};let s=e.join(" ").trim();if(!s){if(o.length>1){console.log(f("Multiple features in .d360-writer/input/ \u2014 draft which one? Run: /draft <feature>"));for(let g of o)console.log(` ${g.name} ${f(`(${g.docCount} doc${g.docCount===1?"":"s"})`)}`);return{kind:"continue"}}let p=o[0]?.name??"";return{kind:"forward-to-agent",prompt:wn(p),display:p?`/draft ${p}`:"/draft"}}let c=Oo(o,s);return{kind:"forward-to-agent",prompt:wn(c??"",c?void 0:s),display:`/draft ${s}`}}L();import{listOpenForDocs as Ec,readProjectConfig as Ic}from"document360-engine";function Mo(e){try{return Ec(e).filter(t=>t.kind==="request").length}catch{return 0}}function Lo(e){let t=e.includes("--publish"),n=e.filter(o=>o!=="--publish").join(" ").trim();return{publish:t,what:n}}function Wo(e,t=!1){let n=["Run the document-handoff skill.",""];return e?n.push(`The builder hand-off (what was shipped this session): "${e}".`,"Treat this as the Stage 1 hand-off: ground it in the cited/changed code + the architecture brief,","then decide and APPLY the right doc action \u2014 a new section, a new article, an update to existing","article(s), or a retirement, or a mix. No fixed shape; pick the smallest set that keeps the docs correct."):n.push("No description was given \u2014 call d360_inbox and act on the OPEN `request` messages the builder left:","for each, run the document-handoff flow (ground \u2192 decide \u2192 apply) and mark the request `status: done`","with `resulting_articles` when finished."),n.push("","Draft only \u2014 do not publish. If you would create multiple new articles (a section), call","d360_estimate_cost and present the count + cost band first, then wait for the go-ahead."),t&&n.push("","After the drafts exist and the user confirms any bulk gate, publish the new/updated articles to","Document360 (the publish-to-d360 skill). On a production profile, respect the write-guard."),n.join(`
219
- `)}var Fo="Nothing to document yet. Say what changed: /document <what you shipped> \u2014 or have the builder drop a request via their /devhints hand-off.";async function Xs(e,t){let n=t?.cwd??process.cwd();if(!Ic(n))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{publish:o,what:r}=Lo(e);if(!r&&Mo(n)===0)return console.log(f(Fo)),{kind:"continue"};let s=`/document${r?` ${r}`:""}${o?" --publish":""}`;return{kind:"forward-to-agent",prompt:Wo(r,o),display:s}}L();import{search as Nc}from"@inquirer/prompts";import{findByName as _c,getSession as Oc,listSessions as Uc,relativeTime as Js}from"document360-engine";async function Qs(e,t){let n=Uc(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=_c(t.cwd,o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${o}".`)),Ks(n),{kind:"continue"})}if(!process.stdin.isTTY)return Ks(n),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let o=await t.withPausedInput(()=>Nc({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return n.filter(p=>!c||p.name.toLowerCase().includes(c)||p.firstPrompt.toLowerCase().includes(c)).map(p=>({name:`${p.name} ${Js(p.updatedAt)}`,value:p.uuid,description:p.firstPrompt.slice(0,100)}))}})),r=Oc(o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function Ks(e){console.log("");for(let t of e.slice(0,15))console.log(` ${A(t.name)} ${f(Js(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}L();import{renameSession as Mc}from"document360-engine";function qo(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 Zs(e,t){let n=qo(e.join(" "));if(!n)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let o=t.currentUuid();return o?(Mc(o,n)?console.log(j(`\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 ei,readProjectConfig as Lc,writeProjectConfig as Wc}from"document360-engine";L();function Ho(e,t,n){if(!t)return"Usage: /profile add <name> [environment]";let o=Lc(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 ei().includes(r)?(o.profiles={...o.profiles,[t]:{connection:{environment:r},production:!1}},Wc(o,e),null):`Unknown environment "${r}". Known: ${ei().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function ti(e,t){let n=e[0];if(!n)return sn(t.cwd),{kind:"continue"};if(n==="add"){let o=Ho(t.cwd,e[1],e[2]);return o?(console.log(x(o)),{kind:"continue"}):(console.log(j(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${A(`/profile ${e[1]}`)} then ${A("/login")}`),{kind:"continue"})}return an(t.cwd,n),ln(t.cwd,n),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}L();import{select as Fc}from"@inquirer/prompts";import{readProjectConfig as qc,readUserConfig as ni,resolveModelSetting as Bo,writeUserConfig as oi}from"document360-engine";var xe=[{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 yn(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),n=xe.findIndex(o=>o.value!==null&&(o.value===t||o.label.toLowerCase()===t||t.includes(o.label.toLowerCase())));return n>=0?n:0}function Hc(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 At(e,t){let n=()=>{let s=Bo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=ni();return s.defaultModel?(delete s.defaultModel,oi(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Bo(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}}oi({...ni(),defaultModel:t});let o=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=qc(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 ri(e,t){let n=e[0]?.trim();if(!n){let c=Bo(t.cwd);if(!process.stdin.isTTY)return console.log(`${Pe("Model:")} ${A(c.model??"Claude Code default")}`),console.log(f(` source: ${Hc(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let p=yn(c),g;try{g=await t.withPausedInput(()=>Fc({message:`Select model (current: ${c.model??"Claude Code default"})`,default:xe[p].value,choices:xe.map((w,v)=>({name:`${w.label}${v===p?" \u2714":""}`,value:w.value,description:w.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:b,changed:k,effective:D}=At(t.cwd,g??"default");for(let w of b)console.log(w.startsWith("\u26A0")?M(w):w.startsWith("\u2713")?j(w):f(w));return k&&await t.setModel(D),{kind:"continue"}}let{lines:o,changed:r,effective:s}=At(t.cwd,n);for(let c of o)console.log(c.startsWith("\u26A0")?M(c):c.startsWith("\u2713")?j(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}Cn();async function ki(e,t){return await t.withPausedInput(()=>ut(t.cwd)),{kind:"clear"}}async function bi(e,t){return await t.withPausedInput(()=>Xr(t.cwd,t.profileName)),{kind:"clear"}}L();import{resolveActiveProfile as iu}from"document360-engine";async function xi(e,t){let n=!1;try{n=iu(t.cwd).production}catch{}return n?(console.log(M("\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"})}L();var $i=async(e,t)=>{try{await t.withPausedInput(()=>on({}))}catch(n){console.log(x(`Login failed: ${n.message}`))}return{kind:"continue"}};L();import{resolveActiveProfile as au,clearTokens as lu,clearProfileProject as cu}from"document360-engine";async function vi(e,t){let n;try{n=au(t.cwd,t.profileName).name}catch(r){return console.log(x(r.message)),{kind:"continue"}}let o=lu(n);return cu(t.cwd,n),console.log(o?j(`\u2713 Signed out of "${n}" and cleared its project/workspace selection.`):f(`Profile "${n}" was not signed in. Cleared any project/workspace selection.`)),console.log(f("Run /login to sign in and pick a project.")),{kind:"continue"}}import{existsSync as Ci}from"node:fs";import{isAbsolute as uu,join as du,resolve as pu}from"node:path";import{readProjectConfig as fu,screenshotPlaceholderIds as mu,DEFAULT_CAPTURE_DIR as hu,DEFAULT_OUTPUT_DIR as gu}from"document360-engine";var Pi=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Ko(e){let t=!e.includes("--no-setup"),n=e.filter(r=>r!=="--no-setup");if(n[0]==="--list")return{mode:"list",scope:n[1]?Pi(n[1]):void 0};let o=n[0];return!o||o==="--all"?{mode:"all",setup:t}:/[\\/]/.test(o)||o.endsWith(".md")?{mode:"scope",scope:Pi(o),setup:t}:{mode:"single",id:o}}function Jo(e,t){let n=fu(e),o=n?.captureDir??hu,r=n?.outputDir??gu,s=(c,p)=>uu(c)?du(c,p):pu(e,c,p);return mu(e,{scope:t}).map(({id:c,file:p})=>{let g=Ci(s(r,`${c}.png`))?"captured":Ci(s(o,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:p,state:g}})}var wu={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},yu={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Qo(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let n=[...new Set(e.map(s=>s.file))].sort(),o=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${n.length} article${n.length===1?"":"s"} \xB7 ${o} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of n){r.push(s);for(let c of e.filter(p=>p.file===s))r.push(` ${wu[c.state]} ${c.id.padEnd(34)} ${yu[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function Si(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(`
217
+ `)}function No(e,t,n){let o=e.reduce((c,p)=>c+p.paths.length,0),[r,s]=t.usd;return[`Write ${o} article${o===1?"":"s"} across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(c=>` \u2022 ${c.label} \u2014 ${c.paths.length} article${c.paths.length===1?"":"s"}`),"",`Estimated cost: ${le(r)}\u2013${le(s)}. ${t.note}`,"","Articles are written as local drafts in the docs tree (nothing is published). Run /write --run to start."]}function _o(e,t,n){let o=C=>t[C.index]?.paths.length??0,r=e.filter(C=>C.ok),s=e.filter(C=>!C.ok),c=e.reduce((C,N)=>C+o(N),0),p=r.reduce((C,N)=>C+o(N),0),g=e.reduce((C,N)=>C+N.costUsd,0),b=e.reduce((C,N)=>C+N.outputTokens,0),k=n==="api"?`${le(g)} total`:Ie(b),D=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,w=C=>`${C} article${C===1?"":"s"}`,v=[];if(s.length===0)v.push(`Wrote ${w(c)} (${D}) successfully \xB7 ${k}.`),v.push("Next \u2014 push them to Document360:"),v.push("/publish --all"),v.push("Or add screenshots first with /screenshot --all. Drafts are reviewable in the portal after publishing.");else{v.push(`Wrote ${p}/${w(c)} (${D}) \xB7 ${k}.`),v.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /write to retry the rest:`);for(let C of s)v.push(` \u2717 ${C.label}${C.error?` \u2014 ${C.error}`:""}`)}return v}function Oo(e,t,n){let o=(zs(e)?.docsDir??Pc).replace(/\/+$/,""),r=t.map(p=>p.path);if(n.path){let p=Rc(n.path,o);return r.includes(p)?{docsDir:o,targets:[p],planCount:r.length}:{docsDir:o,targets:[],planCount:r.length,reason:`"${p}" isn't in the docs plan (.d360-writer/plan.json).`}}let s=Dc(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=jc(e,o,s);if(c.length===0){let p=n.scope?` under "${n.scope}"`:"";return{docsDir:o,targets:[],planCount:r.length,reason:`All planned articles${p} are already written. Use /write <path> to rewrite one.`}}return{docsDir:o,targets:c,planCount:r.length}}async function Xs(e,t){if(!zs(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let n=Cc(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}=Ao(e),{docsDir:c,targets:p,reason:g}=Oo(t.cwd,n,{scope:o,path:r});if(p.length===0)return console.log(g?M(g):M("Nothing to write.")),{kind:"continue"};let b=vc(p),{model:k,forced:D}=Tc(t.cwd,"standard");if(!s&&!!!r){let v=$c({files:Eo(t.cwd,n,p),op:"write",model:k});o&&console.log(f(`Scope: ${o} (${p.length} pending).`));for(let C of No(b,v,jo))console.log(C);return console.log(f(`Model: ${k}${D?" (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 ${p.length} article${p.length===1?"":"s"} across ${b.length} partition${b.length===1?"":"s"} (\u2264${jo} agents at once) on ${k}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let v of Sc({cwd:t.cwd,partitions:b,promptFor:C=>Io(C,c),concurrency:jo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:k}))if(v.type==="partition_status")v.status==="running"?console.log(f(` \u25B8 ${v.label} \u2014 writing\u2026`)):v.status==="done"?console.log(j(` \u2713 ${v.label}`)):console.log(x(` \u2717 ${v.label}`));else if(v.type==="run_done"){console.log("");let C=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let N of _o(v.results,b,C))console.log(v.ok?j(N):M(N))}}catch(v){console.log(x(`Write run failed: ${v.message}`))}return console.log(""),{kind:"continue"}}L();import{existsSync as Ac,readdirSync as Ks,statSync as Js}from"node:fs";import{join as Qs}from"node:path";import{inputDir as Ec,readProjectConfig as Ic}from"document360-engine";var Zs=/\.(md|markdown|pdf|txt)$/i;function Uo(e){let t=Ec(e);if(!Ac(t))return{features:[],looseDocs:0};let n=[],o=0;for(let r of Ks(t)){let s=Qs(t,r),c=!1;try{c=Js(s).isDirectory()}catch{continue}if(c){if(r.startsWith("."))continue;let p=Nc(s);p>0&&n.push({name:r,docCount:p})}else Zs.test(r)&&o++}return n.sort((r,s)=>r.name.localeCompare(s.name)),{features:n,looseDocs:o}}function Nc(e){let t=0;for(let n of Ks(e))try{Js(Qs(e,n)).isFile()&&Zs.test(n)&&t++}catch{}return t}function Mo(e,t){let n=t.trim().toLowerCase();return n?e.find(o=>o.name.toLowerCase()===n)?.name??e.find(o=>o.name.toLowerCase().startsWith(n))?.name??e.find(o=>o.name.toLowerCase().includes(n))?.name??null:null}function xn(e,t){return["Run the draft-from-prd skill.","",`Target scope: ${e?`the feature documents in .d360-writer/input/${e}/`:t?`the input docs in .d360-writer/input/ relevant to: "${t}"`:"the input docs in .d360-writer/input/"}.`,t&&!e?`Use "${t}" as the feature framing.`:"","","Follow the skill: (1) read those PRDs/specs as the primary source (intent, user-facing surface,","terminology, user stories, prerequisites); (2) locate and read the CODE that implements that surface","to pin exact UI strings/routes/states \u2014 never fabricate; flag anything the PRD claims that you","cannot find in source; (3) propose a tight feature-scoped article set (\u22483\u20135) and pin it to",".d360-writer/plan.json + AGENT-PLAN.md, with each article's `sources` listing BOTH the PRD file(s)","AND the grounding code files; (4) flag overlaps with existing articles (do not merge). Then stop and","offer /write --all. Do NOT write the articles in this turn."].filter(Boolean).join(`
218
+ `)}var Lo="No input docs found. Drop this feature's PRDs/specs in .d360-writer/input/<feature>/ (.md or .pdf), then run /draft.";async function ei(e,t){let n=t?.cwd??process.cwd();if(!Ic(n))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{features:o,looseDocs:r}=Uo(n);if(o.length===0&&r===0)return console.log(f(Lo)),{kind:"continue"};let s=e.join(" ").trim();if(!s){if(o.length>1){console.log(f("Multiple features in .d360-writer/input/ \u2014 draft which one? Run: /draft <feature>"));for(let g of o)console.log(` ${g.name} ${f(`(${g.docCount} doc${g.docCount===1?"":"s"})`)}`);return{kind:"continue"}}let p=o[0]?.name??"";return{kind:"forward-to-agent",prompt:xn(p),display:p?`/draft ${p}`:"/draft"}}let c=Mo(o,s);return{kind:"forward-to-agent",prompt:xn(c??"",c?void 0:s),display:`/draft ${s}`}}L();import{listOpenForDocs as _c,readProjectConfig as Oc}from"document360-engine";function Wo(e){try{return _c(e).filter(t=>t.kind==="request").length}catch{return 0}}function Fo(e){let t=e.includes("--publish"),n=e.filter(o=>o!=="--publish").join(" ").trim();return{publish:t,what:n}}function qo(e,t=!1){let n=["Run the document-handoff skill.",""];return e?n.push(`The builder hand-off (what was shipped this session): "${e}".`,"Treat this as the Stage 1 hand-off: ground it in the cited/changed code + the architecture brief,","then decide and APPLY the right doc action \u2014 a new section, a new article, an update to existing","article(s), or a retirement, or a mix. No fixed shape; pick the smallest set that keeps the docs correct."):n.push("No description was given \u2014 call d360_inbox and act on the OPEN `request` messages the builder left:","for each, run the document-handoff flow (ground \u2192 decide \u2192 apply) and mark the request `status: done`","with `resulting_articles` when finished."),n.push("","Draft only \u2014 do not publish. If you would create multiple new articles (a section), call","d360_estimate_cost and present the count + cost band first, then wait for the go-ahead."),t&&n.push("","After the drafts exist and the user confirms any bulk gate, publish the new/updated articles to","Document360 (the publish-to-d360 skill). On a production profile, respect the write-guard."),n.join(`
219
+ `)}var Ho="Nothing to document yet. Say what changed: /document <what you shipped> \u2014 or have the builder drop a request via their /devhints hand-off.";async function ti(e,t){let n=t?.cwd??process.cwd();if(!Oc(n))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{publish:o,what:r}=Fo(e);if(!r&&Wo(n)===0)return console.log(f(Ho)),{kind:"continue"};let s=`/document${r?` ${r}`:""}${o?" --publish":""}`;return{kind:"forward-to-agent",prompt:qo(r,o),display:s}}L();import{search as Uc}from"@inquirer/prompts";import{findByName as Mc,getSession as Lc,listSessions as Wc,relativeTime as oi}from"document360-engine";async function ri(e,t){let n=Wc(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=Mc(t.cwd,o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${o}".`)),ni(n),{kind:"continue"})}if(!process.stdin.isTTY)return ni(n),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let o=await t.withPausedInput(()=>Uc({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return n.filter(p=>!c||p.name.toLowerCase().includes(c)||p.firstPrompt.toLowerCase().includes(c)).map(p=>({name:`${p.name} ${oi(p.updatedAt)}`,value:p.uuid,description:p.firstPrompt.slice(0,100)}))}})),r=Lc(o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function ni(e){console.log("");for(let t of e.slice(0,15))console.log(` ${A(t.name)} ${f(oi(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}L();import{renameSession as Fc}from"document360-engine";function Bo(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 si(e,t){let n=Bo(e.join(" "));if(!n)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let o=t.currentUuid();return o?(Fc(o,n)?console.log(j(`\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 ii,readProjectConfig as qc,writeProjectConfig as Hc}from"document360-engine";L();function Go(e,t,n){if(!t)return"Usage: /profile add <name> [environment]";let o=qc(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 ii().includes(r)?(o.profiles={...o.profiles,[t]:{connection:{environment:r},production:!1}},Hc(o,e),null):`Unknown environment "${r}". Known: ${ii().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function ai(e,t){let n=e[0];if(!n)return an(t.cwd),{kind:"continue"};if(n==="add"){let o=Go(t.cwd,e[1],e[2]);return o?(console.log(x(o)),{kind:"continue"}):(console.log(j(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${A(`/profile ${e[1]}`)} then ${A("/login")}`),{kind:"continue"})}return ln(t.cwd,n),cn(t.cwd,n),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}L();import{select as Bc}from"@inquirer/prompts";import{readProjectConfig as Gc,readUserConfig as li,resolveModelSetting as Vo,writeUserConfig as ci}from"document360-engine";var xe=[{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 $n(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),n=xe.findIndex(o=>o.value!==null&&(o.value===t||o.label.toLowerCase()===t||t.includes(o.label.toLowerCase())));return n>=0?n:0}function Vc(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 Nt(e,t){let n=()=>{let s=Vo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=li();return s.defaultModel?(delete s.defaultModel,ci(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Vo(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}}ci({...li(),defaultModel:t});let o=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=Gc(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 ui(e,t){let n=e[0]?.trim();if(!n){let c=Vo(t.cwd);if(!process.stdin.isTTY)return console.log(`${Pe("Model:")} ${A(c.model??"Claude Code default")}`),console.log(f(` source: ${Vc(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let p=$n(c),g;try{g=await t.withPausedInput(()=>Bc({message:`Select model (current: ${c.model??"Claude Code default"})`,default:xe[p].value,choices:xe.map((w,v)=>({name:`${w.label}${v===p?" \u2714":""}`,value:w.value,description:w.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:b,changed:k,effective:D}=Nt(t.cwd,g??"default");for(let w of b)console.log(w.startsWith("\u26A0")?M(w):w.startsWith("\u2713")?j(w):f(w));return k&&await t.setModel(D),{kind:"continue"}}let{lines:o,changed:r,effective:s}=Nt(t.cwd,n);for(let c of o)console.log(c.startsWith("\u26A0")?M(c):c.startsWith("\u2713")?j(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}vn();async function wi(e,t){return await t.withPausedInput(()=>ut(t.cwd)),{kind:"clear"}}async function yi(e,t){return await t.withPausedInput(()=>Gr(t.cwd,t.profileName)),{kind:"clear"}}L();import{resolveActiveProfile as ru}from"document360-engine";async function ki(e,t){let n=!1;try{n=ru(t.cwd).production}catch{}return n?(console.log(M("\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"})}L();var bi=async(e,t)=>{try{await t.withPausedInput(()=>rn({}))}catch(n){console.log(x(`Login failed: ${n.message}`))}return{kind:"continue"}};L();import{resolveActiveProfile as su,clearTokens as iu,clearProfileProject as au}from"document360-engine";async function xi(e,t){let n;try{n=su(t.cwd,t.profileName).name}catch(r){return console.log(x(r.message)),{kind:"continue"}}let o=iu(n);return au(t.cwd,n),console.log(o?j(`\u2713 Signed out of "${n}" and cleared its project/workspace selection.`):f(`Profile "${n}" was not signed in. Cleared any project/workspace selection.`)),console.log(f("Run /login to sign in and pick a project.")),{kind:"continue"}}import{existsSync as $i}from"node:fs";import{isAbsolute as lu,join as cu,resolve as uu}from"node:path";import{readProjectConfig as du,screenshotPlaceholderIds as pu,DEFAULT_CAPTURE_DIR as fu,DEFAULT_OUTPUT_DIR as mu}from"document360-engine";var vi=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function zo(e){let t=!e.includes("--no-setup"),n=e.filter(r=>r!=="--no-setup");if(n[0]==="--list")return{mode:"list",scope:n[1]?vi(n[1]):void 0};let o=n[0];return!o||o==="--all"?{mode:"all",setup:t}:/[\\/]/.test(o)||o.endsWith(".md")?{mode:"scope",scope:vi(o),setup:t}:{mode:"single",id:o}}function Xo(e,t){let n=du(e),o=n?.captureDir??fu,r=n?.outputDir??mu,s=(c,p)=>lu(c)?cu(c,p):uu(e,c,p);return pu(e,{scope:t}).map(({id:c,file:p})=>{let g=$i(s(r,`${c}.png`))?"captured":$i(s(o,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:p,state:g}})}var hu={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},gu={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 n=[...new Set(e.map(s=>s.file))].sort(),o=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${n.length} article${n.length===1?"":"s"} \xB7 ${o} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of n){r.push(s);for(let c of e.filter(p=>p.file===s))r.push(` ${hu[c.state]} ${c.id.padEnd(34)} ${gu[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function Ci(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(`
220
220
  `),"","For each: locate its <!-- SCREENSHOT --> block in the docs tree (the configured docsDir); read the product source for","EXACT routes + stable data-testid selectors (never guess); enter the prepared context via","captureScope(); guard data prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts","(skip-with-TODO if a stable selector is missing). Do NOT run other skills. Report the specs you","wrote and any TODO data-testids."].join(`
221
- `)}async function Pn(e,t){let n=Ko(e);if(n.mode==="list"){let c=t?.cwd??process.cwd();for(let p of Qo(Jo(c,n.scope),n.scope))console.log(p);return{kind:"continue"}}if(n.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${n.id}\`.`,"Locate its <!-- SCREENSHOT --> block in the docs tree (the configured docsDir), then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${n.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
221
+ `)}async function Cn(e,t){let n=zo(e);if(n.mode==="list"){let c=t?.cwd??process.cwd();for(let p of Ko(Xo(c,n.scope),n.scope))console.log(p);return{kind:"continue"}}if(n.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${n.id}\`.`,"Locate its <!-- SCREENSHOT --> block in the docs tree (the configured docsDir), then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${n.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
222
222
  `),display:`/screenshot ${n.id}`};let r=[`Run the emit-screenshot-spec skill to (re)generate the document360-capture spec for ${n.mode==="scope"?`every <!-- SCREENSHOT --> placeholder in articles under ${n.scope}`:"every <!-- SCREENSHOT --> placeholder across the docs tree (the configured docsDir)"}.`,"Follow that skill exactly for each: read the SCREENSHOT block; read the product source for EXACT routes","and stable data-testid selectors (never guess); enter the prepared context via captureScope(); guard data",`prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts.${n.setup?" When done, run capture-setup-checklist to refresh CAPTURE-SETUP.md.":""} Report specs written, TODO data-testids, and data to stage.`].join(`
223
- `),s=n.mode==="scope"?`/screenshot ${n.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as ku,readFileSync as bu}from"node:fs";import{isAbsolute as xu,join as $u,resolve as vu}from"node:path";import{readProjectConfig as Cu,DEFAULT_CAPTURE_DIR as Pu}from"document360-engine";async function _t(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across the docs tree (the configured docsDir), read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
224
- `),display:"/capture-setup"}}var Ti="-".repeat(64);function Zo(e){let t=Pu;try{let k=Cu(e);k?.captureDir&&(t=k.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let n=xu(t)?$u(t,"CAPTURE-TESTID-REQUESTS.json"):vu(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!ku(n))return null;let o;try{let k=JSON.parse(bu(n,"utf8").replace(/^/,""));o=Array.isArray(k?.requests)?k.requests:[]}catch{return null}if(o.length===0)return null;let r=new Set(o.map(k=>k.file).filter(Boolean)).size,s=o.length,c=`${s} data-testid attribute${s===1?"":"s"}`,p=`${r} source file${r===1?"":"s"}`,g=`${t}/CAPTURE-TESTID-REQUESTS.md`,b=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${p}.`,"\u26A0 Do NOT run this prompt here. d360-writer documents code; it does not change it \u2014"," these edits belong in your code repo. Open a SEPARATE terminal in:",` ${e}`," start Claude Code (or your coding agent), and paste everything between the lines."," Tip: run it in plan mode first \u2014 these are a hypothesis to verify, not a verified patch.","",Ti,`Read ${g} and ${b}. 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.",Ti]}It();L();import{listOpenForDocs as Su,readProjectConfig as Tu}from"document360-engine";function er(e){try{return Su(e).length}catch{return 0}}function tr(){return["Process the two-way message inbox from the source repo's coding agent (the builder).","","1. Call d360_inbox to list the OPEN messages addressed to you.","2. For each message, act by its kind:",' \u2022 request ("document the X we shipped") \u2192 treat it as a session hand-off: run the document-handoff'," skill \u2014 read what changed, ground it in the cited sources + the architecture brief, then decide and"," apply the RIGHT doc action (new section, new article, update existing, or retire). No fixed shape."," \u2022 answer (a reply to a question you asked; `re` = your question id) \u2192 fold the fact into the affected"," article(s), and into `.d360-writer/architecture/` if it is durable product knowledge.","3. After acting on a message, mark it resolved: edit its file in `.d360-writer/messages/` and set the"," frontmatter `status: done` (add `done_at` with the current UTC time). Never delete the file.","4. Drafts only \u2014 do not publish unless explicitly asked. End with a one-line summary of what you did per"," message (and, for any request you could not fully ground, what you still need)."].join(`
225
- `)}async function Ri(e,t){let n=t?.cwd??process.cwd();return Tu(n)?er(n)===0?(console.log(f("Inbox empty \u2014 no open messages from the builder. (They arrive via the builder's /devhints hand-off.)")),{kind:"continue"}):{kind:"forward-to-agent",prompt:tr(),display:"/inbox"}:(console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"})}L();import{listOpenHints as Ru,listOpenForDocs as Du,readProjectConfig as ju}from"document360-engine";function mt(e){let t=0,n=0,o=0;try{t=Ru(e).length}catch{}try{let r=Du(e);n=r.filter(s=>s.kind==="request").length,o=r.filter(s=>s.kind==="answer").length}catch{}return{hints:t,requests:n,answers:o}}function ht(e){let t=[];return e.hints&&t.push(`${e.hints} hint${e.hints===1?"":"s"}`),e.requests&&t.push(`${e.requests} request${e.requests===1?"":"s"}`),e.answers&&t.push(`${e.answers} answer${e.answers===1?"":"s"}`),t.length?t.join(", "):null}function nr(e){return{publish:e.includes("--publish")}}function or({publish:e}){let t=["Catch the documentation up: process ALL the pending work the builder has queued, end to end.","","1. Trust the inventory first \u2014 call d360_sync_status. If anything is in conflict or remote-ahead, STOP and tell me what to resolve (never write over a copy you can't trust).","2. Gather the pending work: d360_pending_hints (the builder's per-change hints) AND d360_inbox (open requests + answers).","3. Decide the right doc actions across all of it: gap-analysis for the hints (verify each against its sources + commit), document-handoff for each request (no fixed shape \u2014 new section / new article / update / retire), and fold any answers into the affected article(s) + the architecture brief.","4. If that means writing more than one article, call d360_estimate_cost and show me the count + cost band first (the bulk gate), then proceed.","5. Write/update the articles as drafts, grounded in the cited sources.","6. Close the loop: stamp consumed hints, and mark handled messages status: done with resulting_articles.","7. Push the drafts to Document360."];return t.push(e?"8. Then publish the new/updated articles live (publish-to-d360 skill), respecting the production write-guard.":"Leave everything as DRAFTS for review \u2014 do not publish live (run /publish when you want them live)."),t.push("","End with a one-line-per-item summary of what you wrote/updated, and anything you couldn't ground (plus the d360_ask_builder question you left for it)."),t.join(`
226
- `)}async function Di(e,t){let n=t?.cwd??process.cwd();if(!ju(n))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{publish:o}=nr(e),r=ht(mt(n));return console.log(f(r?`Catching up on ${r} (+ checking Document360 for drift)\u2026`:"No local queue \u2014 checking for code changes and drift\u2026")),{kind:"forward-to-agent",prompt:or({publish:o}),display:`/catch-up${o?" --publish":""}`}}L();import{spawn as Au}from"node:child_process";var Ve="document360-writer",Sn=`npm i -g ${Ve}@latest`;async function Tn(){return new Promise(e=>{let t="";try{let n=Au("npm",["i","-g",`${Ve}@latest`],{stdio:["ignore","pipe","pipe"],shell:process.platform==="win32"});n.stdout?.on("data",o=>t+=String(o)),n.stderr?.on("data",o=>t+=String(o)),n.on("error",o=>e({ok:!1,output:String(o)})),n.on("close",o=>e({ok:o===0,output:t.trim()}))}catch(n){e({ok:!1,output:String(n)})}})}async function ji(){return console.log(f(`Updating ${Ve}\u2026`)),(await Tn()).ok?console.log(j("\u2713 Updated. Exit (/exit) and relaunch d360-writer to apply.")):console.log(x(`Update failed \u2014 run manually: ${Sn}`)),{kind:"continue"}}L();import{existsSync as Ai,rmSync as Eu}from"node:fs";import{basename as Ei,resolve as rr}from"node:path";import{execFileSync as Iu}from"node:child_process";import{input as Nu}from"@inquirer/prompts";import{readProjectConfig as _u}from"document360-engine";var Ou=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"],Uu=new Set([".d360-writer.json","d360-category-map.json",".d360-capture.json",".d360-capture-cache"]),Rn=e=>(e??"").replace(/\\/g,"/").replace(/\/+$/,""),Ii=e=>e===".d360-writer"||e.startsWith(".d360-writer/");function Ni(e){let t=Rn(e);return t===""||t.startsWith("..")||t.startsWith("/")||/^[a-zA-Z]:/.test(t)?!1:Ii(t)?!0:Uu.has(t)}function sr(e){let t=null;try{t=_u(e)}catch{t=null}let n=new Set(Ou);for(let o of[Rn(t?.docsDir),Rn(t?.captureDir),Rn(t?.outputDir)])o&&Ii(o)&&n.add(o);return[...n].filter(o=>Ni(o)&&Ai(rr(e,o)))}function ir(e,t){let n=[],o=[];for(let r of t){if(!Ni(r)){o.push({path:r,error:"refused \u2014 outside .d360-writer/ (protected)"});continue}try{Eu(rr(e,r),{recursive:!0,force:!0}),n.push(r)}catch(s){o.push({path:r,error:s.message})}}return{removed:n,failed:o}}function _i(e,t){try{return Iu("git",["status","--porcelain","--",t],{cwd:e,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).split(`
227
- `).filter(o=>o.trim()&&/\.md\s*$/.test(o)).length}catch{return 0}}function Mu(e){return _i(e,".d360-writer/hints")}function Lu(e){return _i(e,".d360-writer/messages")}function ar(e,t){if(t.length===0)return["Nothing to reset \u2014 no d360-writer files found in this repo."];let n=Mu(e),o=Lu(e),r=Ai(rr(e,"user-docs"));return["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(s=>` \u2022 ${s}`),"",...n>0?[`\u26A0 ${n} doc hint(s) under .d360-writer/hints/ are not committed \u2014 commit them first or they're gone.`]:[],...o>0?[`\u26A0 ${o} message(s) under .d360-writer/messages/ are not committed \u2014 commit them first or they're gone.`]:[],...r?["Protected: root user-docs/ is NOT touched (only .d360-writer/ is removed)."]:[],"Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${Ei(e)}`]}async function Oi(e,t){let n=sr(t.cwd);for(let p of ar(t.cwd,n))console.log(n.length===0?f(p):p);if(n.length===0)return{kind:"continue"};let o=Ei(t.cwd);if((await t.withPausedInput(()=>Nu({message:`Type "${o}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==o)return console.log(M("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=ir(t.cwd,n);console.log(j(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let p of c)console.log(x(` \u2717 ${p.path}: ${p.error}`));return console.log(f("Set up d360-writer again with /init (then /login, /workspace).")),{kind:"continue"}}var Ui={help:ro,"?":ro,clear:as,exit:so,quit:so,init:gs,mcp:dn,publish:Ds,audit:mn,scope:As,sync:Ms,convert:Ls,write:Hs,draft:zs,document:Xs,resume:Qs,rename:Zs,profile:ti,model:ri,doctor:Nt,workspace:ki,project:bi,"allow-prod":xi,login:$i,logout:vi,screenshot:Pn,"capture-setup":_t,devhints:di,inbox:Ri,"catch-up":Di,update:ji,reset:Oi};function Mi(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}It();L();var Wu={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"},Wi=160,cr=200,Fi=40;function pe(e,t){let n=e.replace(/\s+/g," ").trim();return n.length<=t?n:n.slice(0,t-1)+"\u2026"}function Li(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}: ${pe(typeof r=="string"?r:JSON.stringify(r),Fi)}`);return t.length>4&&n.push("\u2026"),pe(n.join(", "),Wi)}var Ne=e=>typeof e=="string"&&e?e:null,lr=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Fu(e){let t=Ne(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 qu(e,t){let n=(o,r)=>({title:`Document360: ${o}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let o=Ne(t.title);if(!o)return null;let r=Fu(t.local_path);return n("Create article",`"${pe(o,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let o=Ne(t.title),r=lr(t.article_id);return n("Update article",o?`"${pe(o,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return n("Fork article (new draft)",lr(t.article_id)?`id ${lr(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",Ne(t.name)?`"${pe(Ne(t.name),60)}"`:null);case"d360_upload_drive_file":{let o=Ne(t.file_path);return n("Upload image",o?pe(o.replace(/\\/g,"/").split("/").pop()??o,60):null)}case"d360_sync_status":return n("Check sync status",null);default:return null}}function Dn(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let b=qu(c,t);if(b)return b}let p=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${p}`,sep:" ",arg:Li(t)}}let n=Wu[e],o=n?t[n]:void 0;return typeof o=="string"&&o?{title:e,sep:"",arg:pe(o,Wi)}:{title:e,sep:"",arg:Li(t)}}function Hu(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 Bu(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let n=t.map(Hu).filter(r=>r!==null),o=`${t.length} item${t.length===1?"":"s"}`;return n.length===0?[o]:[o,...n.map(r=>pe(r,cr))]}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}: ${pe(String(r),Fi)}`);return n.length>0?[pe(n.join(" \xB7 "),cr)]:null}return null}function Gu(e){let t=Ne(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);return n.length>1?n.slice(1).join("/"):t}function Vu(e,t,n){switch(e){case"d360_create_article":{let o=Gu(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=Ne(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 ${pe(o[0],120)}`:""}`]}default:return null}}function jn(e,t=4,n,o){let r=e.replace(/\r\n/g,`
228
- `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(n?.startsWith("mcp__document360__")){let c=Vu(n.slice(18),o,r);if(c)return{lines:c,hidden:0}}let s=Bu(r)??r.split(`
229
- `);return{lines:s.slice(0,t).map(c=>pe(c,cr)),hidden:Math.max(0,s.length-t)}}function An(e,t,n,o="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${o}/${n}`}function En(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 In(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 rd=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function sd(e,t,n,o){if(rd.test(e))try{let r=Gi(o),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=En(t,n),p=In(n);e.endsWith("publish_article")&&p&&console.log(A(` \u2B95 Live: ${p}`)),c&&s&&console.log(A(` \u2B95 Preview: ${An(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function Vi(e=process.cwd(),t="auto",n){let o=zu(t);o.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${A("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${A('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${A("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${A("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),id(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=ur({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},p=Yu({input:process.stdin,output:process.stdout}),g=[],b=null,k=!1;p.on("line",v=>{if(b){let C=b;b=null,C(v)}else g.push(v)}),p.on("close",()=>{if(k=!0,b){let v=b;b=null,v(null)}});function D(){return g.length>0?Promise.resolve(g.shift()):k?Promise.resolve(null):(process.stdout.write(A("> ")),new Promise(v=>{b=v}))}let w={cwd:e,profileName:n,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=ur({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=>{p.pause();try{return await v()}finally{p.resume()}}};try{for(;;){let v=await D();if(v===null)break;let C=v.trim();if(C){if(C.startsWith("/")){let N=Mi(C);if(!N)continue;let _=Ui[N.name];if(!_){console.log(x(`Unknown command: /${N.name}`)),console.log(f("Type /help for the list."));continue}let F=await _(N.args,w);if(F.kind==="exit")break;if(F.kind==="clear"){w.restartAgent();continue}if(F.kind==="allow-prod"){r=!0,w.restartAgent(),console.log(j("\u2713 Production writes authorized for this session.")),console.log("");continue}if(F.kind==="resume"){s.close(),s=ur({cwd:e,resume:F.uuid,profileName:n,allowProdWrites:r});let ee=Xu(F.uuid);c={uuid:F.uuid,firstPrompt:ee?.firstPrompt??null,titleFired:!0},Bi(F.uuid),console.log(j(`\u2713 Resumed "${F.name}"`)),console.log("");continue}F.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=F.display??F.prompt),await Hi(s,F.prompt,o,c,e));continue}c.firstPrompt||(c.firstPrompt=C),await Hi(s,C,o,c,e)}}}finally{s.close(),p.close()}}function id(e,t){console.log(""),console.log($t("document360-writer")),console.log(f(` cwd: ${e}`));let n=ed(e);if(console.log(f(` model: ${n.model??"auto (engine right-sizes per task)"}${n.model?` (${n.source})`:""}`)),console.log(ad(e,t)),!td(e))console.log(M(" First run: /init \u2192 /login (project & workspace auto-selected) \u2192 /devhints, then ask me to write the docs."));else{try{let o=vn(e);(o.guideRefreshed||o.notifiedBuilder)&&console.log(M(` \u{1F504} Onboarding protocol updated to v${o.toVersion}${o.notifiedBuilder?" \u2014 left the builder a message to re-run it":""}.`))}catch{}try{let o=ht(mt(e));o&&console.log(M(` \u{1F4CB} ${o} from the repo's coding agent \u2014 run /catch-up.`))}catch{}}console.log(f(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function ad(e,t){try{let n=Gi(e,t),o=n.production?M(" \u26A0 PRODUCTION"):"",r=od(n.name);if(!r)return f(` Document360: profile "${n.name}"${o} \u2014 not logged in (d360-writer login)`);let s={...qi(r.idToken)??{},...qi(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return nd(r)&&!r.refreshToken?M(` 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 ld(){console.error(""),console.error(`Sign in with your Claude subscription: run ${A("claude")} once, then retry.`),console.error(` (No Claude Code? ${A("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${A("https://console.anthropic.com/settings/keys")}`)}function cd(e,t,n){e.uuid=t;let o=new Date().toISOString();Qu({uuid:t,name:Ju(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:n,firstPrompt:e.firstPrompt??"",createdAt:o,updatedAt:o})}function ud(e,t){e.titleFired=!0;let n=e.uuid,o=e.firstPrompt;!n||!o||Zu(o,t).then(r=>{r&&Ku(n,r)}).catch(()=>{})}async function Hi(e,t,n,o,r){let s=new Map;for await(let c of e.send(t))dd(c,o,r,n,s)}function dd(e,t,n,o,r){switch(e.type){case"session":t.uuid||cd(t,e.sessionId,n);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=Dn(e.name,e.input);s&&(process.stdout.write(`
230
-
231
- `),console.log(`${j("\u25CF")} ${Pe(s.title)}${s.arg!==null?te(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=Ge(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=p=>p===1?"":"s";console.log(te(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let p of s.lines)console.log(p);s.hidden>0&&console.log(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=jn(e.output,4,e.isError?void 0:s.name,s.input),p=e.isError?x:te;c.lines.forEach((g,b)=>console.log(p((b===0?" \u23BF ":" ")+g))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||sd(s.name,s.input,e.output,n);break}case"result":process.stdout.write(`
232
- `),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&&(Bi(t.uuid),t.titleFired||ud(t,n));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),o.kind==="subscription"&&e.kind==="auth"&&ld();break}}import{render as hp}from"ink";import{resolveAuth as gp}from"document360-engine";import{useCallback as z,useEffect as Oe,useMemo as hr,useRef as J,useState as q}from"react";import{Box as K,Text as T,useApp as xd,useInput as $d,useStdout as vd}from"ink";import{existsSync as gr,readFileSync as ua,readdirSync as xa,writeFileSync as da}from"node:fs";import{basename as Mt,isAbsolute as Cd,join as Lt}from"node:path";import{createSession as pa,classifyClaudeAuth as $a,CLAUDE_CODE_SETUP_HINT as Pd,loginPkce as Sd,toStoredTokens as Td,saveTokens as Rd,getAccessToken as Dd,resolveActiveProfile as fe,resolveProjectId as jd,getArticle as Ad,decodeJwtClaims as gt,isExpired as _e,loadTokens as Ye,clearTokens as Ed,clearProfileProject as Id,setTitle as Nd,slugify as _d,touchSession as wr,upsertSession as Od,generateTitle as fa,findByName as Ud,listSessions as Md,renameSession as Ld,suggestNextAction as Wd,readProjectConfig as Q,writeProjectConfig as Fd,resolveModelSetting as $r,loadProfileMap as ma,applyPull as qd,computeSyncStatus as yr,planPull as Hd,inventoryRepo as Bd,knownEnvironments as Gd,resolveEnvironment as Vd,planPartitions as kr,partitionEvenly as Yd,screenshotPlaceholderIds as zd,trackedArticlePaths as Xd,runPartitioned as Ln,estimateBulkCost as ha,resolveModelForOperation as Wn,readDocsPlan as Kd,ensureDir as br,hintsDir as Jd,architectureDir as Qd,messagesDir as Zd,creatorNotesPath as ga,checkForUpdate as ep,detectInstallKind as tp,isSelfUpdatable as np,devHintsGuidePath as op,DEFAULT_DOCS_DIR as va,setProfileProject as rp}from"document360-engine";It();import{existsSync as pd,mkdirSync as fd,readFileSync as md,writeFileSync as hd}from"node:fs";import{join as Yi}from"node:path";import{writerDir as gd}from"document360-engine";function zi(e){return Yi(gd(e),".sessions")}function Xi(e,t){return Yi(zi(e),`${t}.json`)}function Ki(e,t,n){try{fd(zi(e),{recursive:!0});let o=n.filter(r=>r.kind!=="banner");hd(Xi(e,t),JSON.stringify({v:1,items:o}),"utf8")}catch{}}function dr(e,t){try{let n=Xi(e,t);if(!pd(n))return[];let o=JSON.parse(md(n,"utf8"));return Array.isArray(o.items)?o.items:[]}catch{return[]}}var wd=e=>`\x1B]0;${e}\x07`,yd=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function Ji(e){process.stdout.isTTY&&process.stdout.write(e)}function Ot(e){Ji(wd(e))}function Nn(e){Ji(yd(e))}var _n=["\xB7 "," \xB7"],pr="\u{1F7E3}";Cn();var kd=/^(?:\d+[.)]|[-*•])\s+/;function Qi(e,t,n=4){let o=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
233
- `)){let c=s.trim().replace(kd,""),p=c.match(/^`([^`]+)`$/);p&&(c=p[1].trim());let g=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!g||!o.has(g[1].toLowerCase()))continue;let b=`/${g[1].toLowerCase()}${g[2]?` ${g[2]}`:""}`;if(r.includes(b)||r.push(b),r.length>=n)break}return r}L();function Ut(e){return e.map(t=>({label:t.label,items:t.paths.length,status:"pending"}))}function Se(e,t,n){return e.map(o=>o.label===t?{...o,status:n}:o)}function fr(e){return e.replace(/^[\w.-]+\//,"")}var On=16,Zi={pending:"\u25CB",active:"\u25A0",done:"\u2714",failed:"\u2717"};function mr(e){return e.trim().split(/\s+/).slice(1).join(" ")}function ea(e,t){let n=mr(t);return e.some(r=>r.trim()===t.trim()||!!n&&mr(r)===n)?e.filter(r=>r.trim()!==t.trim()&&!(n&&mr(r)===n)):[]}var bd=/\[Pasted text #\d+ \+\d+ lines?\]/g;function ta(e){return e.replace(/\r\n?/g,`
234
- `)}function na(e){return e.includes(`
235
- `)||e.length>200}function oa(e,t){let n=t.split(`
236
- `).length;return`[Pasted text #${e} +${n} line${n===1?"":"s"}]`}function ra(e,t){return e.replace(bd,n=>t.get(n)??n)}function sa(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function Un(e,t){let n=Math.max(1,t),o=[],r=0;for(let s of e.split(`
237
- `)){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 Mn(e,t){let n=0;for(let o=0;o<e.length&&e[o].start<=t;o++)n=o;return n}function ia(e,t,n){let o=Mn(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 aa(e,t,n){let o=e[Mn(e,t)];return n==="start"?o.start:o.end}function la(e,t,n){let o=Math.max(1,n),r=e.split(`
223
+ `),s=n.mode==="scope"?`/screenshot ${n.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as wu,readFileSync as yu}from"node:fs";import{isAbsolute as ku,join as bu,resolve as xu}from"node:path";import{readProjectConfig as $u,DEFAULT_CAPTURE_DIR as vu}from"document360-engine";async function Ot(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across the docs tree (the configured docsDir), read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
224
+ `),display:"/capture-setup"}}var Pi="-".repeat(64);function Jo(e){let t=vu;try{let k=$u(e);k?.captureDir&&(t=k.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let n=ku(t)?bu(t,"CAPTURE-TESTID-REQUESTS.json"):xu(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!wu(n))return null;let o;try{let k=JSON.parse(yu(n,"utf8").replace(/^/,""));o=Array.isArray(k?.requests)?k.requests:[]}catch{return null}if(o.length===0)return null;let r=new Set(o.map(k=>k.file).filter(Boolean)).size,s=o.length,c=`${s} data-testid attribute${s===1?"":"s"}`,p=`${r} source file${r===1?"":"s"}`,g=`${t}/CAPTURE-TESTID-REQUESTS.md`,b=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${p}.`,"\u26A0 Do NOT run this prompt here. d360-writer documents code; it does not change it \u2014"," these edits belong in your code repo. Open a SEPARATE terminal in:",` ${e}`," start Claude Code (or your coding agent), and paste everything between the lines."," Tip: run it in plan mode first \u2014 these are a hypothesis to verify, not a verified patch.","",Pi,`Read ${g} and ${b}. 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.",Pi]}mt();L();import{listOpenForDocs as Cu,readProjectConfig as Pu}from"document360-engine";function Qo(e){try{return Cu(e).length}catch{return 0}}function Zo(){return["Process the two-way message inbox from the source repo's coding agent (the builder).","","1. Call d360_inbox to list the OPEN messages addressed to you.","2. For each message, act by its kind:",' \u2022 request ("document the X we shipped") \u2192 treat it as a session hand-off: run the document-handoff'," skill \u2014 read what changed, ground it in the cited sources + the architecture brief, then decide and"," apply the RIGHT doc action (new section, new article, update existing, or retire). No fixed shape."," \u2022 answer (a reply to a question you asked; `re` = your question id) \u2192 fold the fact into the affected"," article(s), and into `.d360-writer/architecture/` if it is durable product knowledge.","3. After acting on a message, mark it resolved: edit its file in `.d360-writer/messages/` and set the"," frontmatter `status: done` (add `done_at` with the current UTC time). Never delete the file.","4. Drafts only \u2014 do not publish unless explicitly asked. End with a one-line summary of what you did per"," message (and, for any request you could not fully ground, what you still need)."].join(`
225
+ `)}async function Ti(e,t){let n=t?.cwd??process.cwd();return Pu(n)?Qo(n)===0?(console.log(f("Inbox empty \u2014 no open messages from the builder. (They arrive via the builder's /devhints hand-off.)")),{kind:"continue"}):{kind:"forward-to-agent",prompt:Zo(),display:"/inbox"}:(console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"})}L();import{listOpenHints as Tu,listOpenForDocs as Su,readProjectConfig as Ru}from"document360-engine";function wt(e){let t=0,n=0,o=0;try{t=Tu(e).length}catch{}try{let r=Su(e);n=r.filter(s=>s.kind==="request").length,o=r.filter(s=>s.kind==="answer").length}catch{}return{hints:t,requests:n,answers:o}}function yt(e){let t=[];return e.hints&&t.push(`${e.hints} hint${e.hints===1?"":"s"}`),e.requests&&t.push(`${e.requests} request${e.requests===1?"":"s"}`),e.answers&&t.push(`${e.answers} answer${e.answers===1?"":"s"}`),t.length?t.join(", "):null}function er(e){return{publish:e.includes("--publish")}}function tr({publish:e}){let t=["Catch the documentation up: process ALL the pending work the builder has queued, end to end.","","1. Trust the inventory first \u2014 call d360_sync_status. If anything is in conflict or remote-ahead, STOP and tell me what to resolve (never write over a copy you can't trust).","2. Gather the pending work: d360_pending_hints (the builder's per-change hints) AND d360_inbox (open requests + answers).","3. Decide the right doc actions across all of it: gap-analysis for the hints (verify each against its sources + commit), document-handoff for each request (no fixed shape \u2014 new section / new article / update / retire), and fold any answers into the affected article(s) + the architecture brief.","4. If that means writing more than one article, call d360_estimate_cost and show me the count + cost band first (the bulk gate), then proceed.","5. Write/update the articles as drafts, grounded in the cited sources.","6. Close the loop: stamp consumed hints, and mark handled messages status: done with resulting_articles.","7. Push the drafts to Document360."];return t.push(e?"8. Then publish the new/updated articles live (publish-to-d360 skill), respecting the production write-guard.":"Leave everything as DRAFTS for review \u2014 do not publish live (run /publish when you want them live)."),t.push("","End with a one-line-per-item summary of what you wrote/updated, and anything you couldn't ground (plus the d360_ask_builder question you left for it)."),t.join(`
226
+ `)}async function Si(e,t){let n=t?.cwd??process.cwd();if(!Ru(n))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{publish:o}=er(e),r=yt(wt(n));return console.log(f(r?`Catching up on ${r} (+ checking Document360 for drift)\u2026`:"No local queue \u2014 checking for code changes and drift\u2026")),{kind:"forward-to-agent",prompt:tr({publish:o}),display:`/catch-up${o?" --publish":""}`}}L();import{spawn as Du}from"node:child_process";var Ve="document360-writer",Pn=`npm i -g ${Ve}@latest`;async function Tn(){return new Promise(e=>{let t="";try{let n=Du("npm",["i","-g",`${Ve}@latest`],{stdio:["ignore","pipe","pipe"],shell:process.platform==="win32"});n.stdout?.on("data",o=>t+=String(o)),n.stderr?.on("data",o=>t+=String(o)),n.on("error",o=>e({ok:!1,output:String(o)})),n.on("close",o=>e({ok:o===0,output:t.trim()}))}catch(n){e({ok:!1,output:String(n)})}})}async function Ri(){return console.log(f(`Updating ${Ve}\u2026`)),(await Tn()).ok?console.log(j("\u2713 Updated. Exit (/exit) and relaunch d360-writer to apply.")):console.log(x(`Update failed \u2014 run manually: ${Pn}`)),{kind:"continue"}}L();import{existsSync as Di,rmSync as ju}from"node:fs";import{basename as ji,resolve as nr}from"node:path";import{execFileSync as Au}from"node:child_process";import{input as Eu}from"@inquirer/prompts";import{readProjectConfig as Iu}from"document360-engine";var Nu=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"],_u=new Set([".d360-writer.json","d360-category-map.json",".d360-capture.json",".d360-capture-cache"]),Sn=e=>(e??"").replace(/\\/g,"/").replace(/\/+$/,""),Ai=e=>e===".d360-writer"||e.startsWith(".d360-writer/");function Ei(e){let t=Sn(e);return t===""||t.startsWith("..")||t.startsWith("/")||/^[a-zA-Z]:/.test(t)?!1:Ai(t)?!0:_u.has(t)}function or(e){let t=null;try{t=Iu(e)}catch{t=null}let n=new Set(Nu);for(let o of[Sn(t?.docsDir),Sn(t?.captureDir),Sn(t?.outputDir)])o&&Ai(o)&&n.add(o);return[...n].filter(o=>Ei(o)&&Di(nr(e,o)))}function rr(e,t){let n=[],o=[];for(let r of t){if(!Ei(r)){o.push({path:r,error:"refused \u2014 outside .d360-writer/ (protected)"});continue}try{ju(nr(e,r),{recursive:!0,force:!0}),n.push(r)}catch(s){o.push({path:r,error:s.message})}}return{removed:n,failed:o}}function Ii(e,t){try{return Au("git",["status","--porcelain","--",t],{cwd:e,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).split(`
227
+ `).filter(o=>o.trim()&&/\.md\s*$/.test(o)).length}catch{return 0}}function Ou(e){return Ii(e,".d360-writer/hints")}function Uu(e){return Ii(e,".d360-writer/messages")}function sr(e,t){if(t.length===0)return["Nothing to reset \u2014 no d360-writer files found in this repo."];let n=Ou(e),o=Uu(e),r=Di(nr(e,"user-docs"));return["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(s=>` \u2022 ${s}`),"",...n>0?[`\u26A0 ${n} doc hint(s) under .d360-writer/hints/ are not committed \u2014 commit them first or they're gone.`]:[],...o>0?[`\u26A0 ${o} message(s) under .d360-writer/messages/ are not committed \u2014 commit them first or they're gone.`]:[],...r?["Protected: root user-docs/ is NOT touched (only .d360-writer/ is removed)."]:[],"Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${ji(e)}`]}async function Ni(e,t){let n=or(t.cwd);for(let p of sr(t.cwd,n))console.log(n.length===0?f(p):p);if(n.length===0)return{kind:"continue"};let o=ji(t.cwd);if((await t.withPausedInput(()=>Eu({message:`Type "${o}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==o)return console.log(M("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=rr(t.cwd,n);console.log(j(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let p of c)console.log(x(` \u2717 ${p.path}: ${p.error}`));return console.log(f("Set up d360-writer again with /init (then /login, /workspace).")),{kind:"continue"}}var _i={help:oo,"?":oo,clear:os,exit:ro,quit:ro,init:$s,mcp:hn,publish:_s,audit:yn,scope:Us,sync:Bs,convert:Gs,write:Xs,draft:ei,document:ti,resume:ri,rename:si,profile:ai,model:ui,doctor:_t,workspace:wi,project:yi,"allow-prod":ki,login:bi,logout:xi,screenshot:Cn,"capture-setup":Ot,devhints:ds,inbox:Ti,"catch-up":Si,update:Ri,reset:Ni};function Oi(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}mt();L();var Mu={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"},Mi=160,ar=200,Li=40;function pe(e,t){let n=e.replace(/\s+/g," ").trim();return n.length<=t?n:n.slice(0,t-1)+"\u2026"}function Ui(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}: ${pe(typeof r=="string"?r:JSON.stringify(r),Li)}`);return t.length>4&&n.push("\u2026"),pe(n.join(", "),Mi)}var Ne=e=>typeof e=="string"&&e?e:null,ir=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Lu(e){let t=Ne(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 Wu(e,t){let n=(o,r)=>({title:`Document360: ${o}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let o=Ne(t.title);if(!o)return null;let r=Lu(t.local_path);return n("Create article",`"${pe(o,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let o=Ne(t.title),r=ir(t.article_id);return n("Update article",o?`"${pe(o,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return n("Fork article (new draft)",ir(t.article_id)?`id ${ir(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",Ne(t.name)?`"${pe(Ne(t.name),60)}"`:null);case"d360_upload_drive_file":{let o=Ne(t.file_path);return n("Upload image",o?pe(o.replace(/\\/g,"/").split("/").pop()??o,60):null)}case"d360_sync_status":return n("Check sync status",null);default:return null}}function Rn(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let b=Wu(c,t);if(b)return b}let p=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${p}`,sep:" ",arg:Ui(t)}}let n=Mu[e],o=n?t[n]:void 0;return typeof o=="string"&&o?{title:e,sep:"",arg:pe(o,Mi)}:{title:e,sep:"",arg:Ui(t)}}function Fu(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 qu(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let n=t.map(Fu).filter(r=>r!==null),o=`${t.length} item${t.length===1?"":"s"}`;return n.length===0?[o]:[o,...n.map(r=>pe(r,ar))]}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}: ${pe(String(r),Li)}`);return n.length>0?[pe(n.join(" \xB7 "),ar)]:null}return null}function Hu(e){let t=Ne(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);return n.length>1?n.slice(1).join("/"):t}function Bu(e,t,n){switch(e){case"d360_create_article":{let o=Hu(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=Ne(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 ${pe(o[0],120)}`:""}`]}default:return null}}function Dn(e,t=4,n,o){let r=e.replace(/\r\n/g,`
228
+ `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(n?.startsWith("mcp__document360__")){let c=Bu(n.slice(18),o,r);if(c)return{lines:c,hidden:0}}let s=qu(r)??r.split(`
229
+ `);return{lines:s.slice(0,t).map(c=>pe(c,ar)),hidden:Math.max(0,s.length-t)}}function jn(e,t,n,o="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${o}/${n}`}function An(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 En(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 nd=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function od(e,t,n,o){if(nd.test(e))try{let r=Hi(o),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=An(t,n),p=En(n);e.endsWith("publish_article")&&p&&console.log(A(` \u2B95 Live: ${p}`)),c&&s&&console.log(A(` \u2B95 Preview: ${jn(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function Bi(e=process.cwd(),t="auto",n){let o=Vu(t);o.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${A("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${A('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${A("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${A("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),rd(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=lr({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},p=Gu({input:process.stdin,output:process.stdout}),g=[],b=null,k=!1;p.on("line",v=>{if(b){let C=b;b=null,C(v)}else g.push(v)}),p.on("close",()=>{if(k=!0,b){let v=b;b=null,v(null)}});function D(){return g.length>0?Promise.resolve(g.shift()):k?Promise.resolve(null):(process.stdout.write(A("> ")),new Promise(v=>{b=v}))}let w={cwd:e,profileName:n,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=lr({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=>{p.pause();try{return await v()}finally{p.resume()}}};try{for(;;){let v=await D();if(v===null)break;let C=v.trim();if(C){if(C.startsWith("/")){let N=Oi(C);if(!N)continue;let _=_i[N.name];if(!_){console.log(x(`Unknown command: /${N.name}`)),console.log(f("Type /help for the list."));continue}let F=await _(N.args,w);if(F.kind==="exit")break;if(F.kind==="clear"){w.restartAgent();continue}if(F.kind==="allow-prod"){r=!0,w.restartAgent(),console.log(j("\u2713 Production writes authorized for this session.")),console.log("");continue}if(F.kind==="resume"){s.close(),s=lr({cwd:e,resume:F.uuid,profileName:n,allowProdWrites:r});let ee=Yu(F.uuid);c={uuid:F.uuid,firstPrompt:ee?.firstPrompt??null,titleFired:!0},qi(F.uuid),console.log(j(`\u2713 Resumed "${F.name}"`)),console.log("");continue}F.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=F.display??F.prompt),await Fi(s,F.prompt,o,c,e));continue}c.firstPrompt||(c.firstPrompt=C),await Fi(s,C,o,c,e)}}}finally{s.close(),p.close()}}function rd(e,t){console.log(""),console.log(Pt("document360-writer")),console.log(f(` cwd: ${e}`));let n=Qu(e);if(console.log(f(` model: ${n.model??"auto (engine right-sizes per task)"}${n.model?` (${n.source})`:""}`)),console.log(sd(e,t)),!Zu(e))console.log(M(" First run: /init (scaffolds config + prints the builder hand-off prompt) \u2192 /login, then ask me to write the docs."));else{try{let o=fn(e);(o.guideRefreshed||o.notifiedBuilder)&&console.log(M(` \u{1F504} Onboarding protocol updated to v${o.toVersion}${o.notifiedBuilder?" \u2014 left the builder a message to re-run it":""}.`))}catch{}try{let o=yt(wt(e));o&&console.log(M(` \u{1F4CB} ${o} from the repo's coding agent \u2014 run /catch-up.`))}catch{}}console.log(f(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function sd(e,t){try{let n=Hi(e,t),o=n.production?M(" \u26A0 PRODUCTION"):"",r=td(n.name);if(!r)return f(` Document360: profile "${n.name}"${o} \u2014 not logged in (d360-writer login)`);let s={...Wi(r.idToken)??{},...Wi(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return ed(r)&&!r.refreshToken?M(` 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 id(){console.error(""),console.error(`Sign in with your Claude subscription: run ${A("claude")} once, then retry.`),console.error(` (No Claude Code? ${A("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${A("https://console.anthropic.com/settings/keys")}`)}function ad(e,t,n){e.uuid=t;let o=new Date().toISOString();Ku({uuid:t,name:Xu(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:n,firstPrompt:e.firstPrompt??"",createdAt:o,updatedAt:o})}function ld(e,t){e.titleFired=!0;let n=e.uuid,o=e.firstPrompt;!n||!o||Ju(o,t).then(r=>{r&&zu(n,r)}).catch(()=>{})}async function Fi(e,t,n,o,r){let s=new Map;for await(let c of e.send(t))cd(c,o,r,n,s)}function cd(e,t,n,o,r){switch(e.type){case"session":t.uuid||ad(t,e.sessionId,n);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=Rn(e.name,e.input);s&&(process.stdout.write(`
230
+
231
+ `),console.log(`${j("\u25CF")} ${Pe(s.title)}${s.arg!==null?te(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=Ge(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=p=>p===1?"":"s";console.log(te(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let p of s.lines)console.log(p);s.hidden>0&&console.log(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=Dn(e.output,4,e.isError?void 0:s.name,s.input),p=e.isError?x:te;c.lines.forEach((g,b)=>console.log(p((b===0?" \u23BF ":" ")+g))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||od(s.name,s.input,e.output,n);break}case"result":process.stdout.write(`
232
+ `),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&&(qi(t.uuid),t.titleFired||ld(t,n));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),o.kind==="subscription"&&e.kind==="auth"&&id();break}}import{render as cp}from"ink";import{resolveAuth as up}from"document360-engine";import{useCallback as z,useEffect as Oe,useMemo as fr,useRef as J,useState as q}from"react";import{Box as K,Text as S,useApp as kd,useInput as bd,useStdout as xd}from"ink";import{existsSync as la,readFileSync as ca,readdirSync as ya}from"node:fs";import{basename as Lt,isAbsolute as $d,join as Wt}from"node:path";import{createSession as ua,classifyClaudeAuth as ka,CLAUDE_CODE_SETUP_HINT as vd,loginPkce as Cd,toStoredTokens as Pd,saveTokens as Td,getAccessToken as Sd,resolveActiveProfile as fe,resolveProjectId as Rd,getArticle as Dd,decodeJwtClaims as kt,isExpired as _e,loadTokens as Ye,clearTokens as jd,clearProfileProject as Ad,setTitle as Ed,slugify as Id,touchSession as mr,upsertSession as Nd,generateTitle as da,findByName as _d,listSessions as Od,renameSession as Ud,suggestNextAction as Md,readProjectConfig as Q,writeProjectConfig as Ld,resolveModelSetting as yr,loadProfileMap as pa,applyPull as Wd,computeSyncStatus as hr,planPull as Fd,inventoryRepo as qd,knownEnvironments as Hd,resolveEnvironment as Bd,planPartitions as gr,partitionEvenly as Gd,screenshotPlaceholderIds as Vd,trackedArticlePaths as Yd,runPartitioned as Mn,estimateBulkCost as fa,resolveModelForOperation as Ln,readDocsPlan as zd,checkForUpdate as Xd,detectInstallKind as Kd,isSelfUpdatable as Jd,DEFAULT_DOCS_DIR as ba,setProfileProject as Qd}from"document360-engine";mt();import{existsSync as ud,mkdirSync as dd,readFileSync as pd,writeFileSync as fd}from"node:fs";import{join as Gi}from"node:path";import{writerDir as md}from"document360-engine";function Vi(e){return Gi(md(e),".sessions")}function Yi(e,t){return Gi(Vi(e),`${t}.json`)}function zi(e,t,n){try{dd(Vi(e),{recursive:!0});let o=n.filter(r=>r.kind!=="banner");fd(Yi(e,t),JSON.stringify({v:1,items:o}),"utf8")}catch{}}function cr(e,t){try{let n=Yi(e,t);if(!ud(n))return[];let o=JSON.parse(pd(n,"utf8"));return Array.isArray(o.items)?o.items:[]}catch{return[]}}var hd=e=>`\x1B]0;${e}\x07`,gd=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function Xi(e){process.stdout.isTTY&&process.stdout.write(e)}function Ut(e){Xi(hd(e))}function In(e){Xi(gd(e))}var Nn=["\xB7 "," \xB7"],ur="\u{1F7E3}";vn();var wd=/^(?:\d+[.)]|[-*•])\s+/;function Ki(e,t,n=4){let o=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
233
+ `)){let c=s.trim().replace(wd,""),p=c.match(/^`([^`]+)`$/);p&&(c=p[1].trim());let g=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!g||!o.has(g[1].toLowerCase()))continue;let b=`/${g[1].toLowerCase()}${g[2]?` ${g[2]}`:""}`;if(r.includes(b)||r.push(b),r.length>=n)break}return r}L();function Mt(e){return e.map(t=>({label:t.label,items:t.paths.length,status:"pending"}))}function Te(e,t,n){return e.map(o=>o.label===t?{...o,status:n}:o)}function dr(e){return e.replace(/^[\w.-]+\//,"")}var _n=16,Ji={pending:"\u25CB",active:"\u25A0",done:"\u2714",failed:"\u2717"};function pr(e){return e.trim().split(/\s+/).slice(1).join(" ")}function Qi(e,t){let n=pr(t);return e.some(r=>r.trim()===t.trim()||!!n&&pr(r)===n)?e.filter(r=>r.trim()!==t.trim()&&!(n&&pr(r)===n)):[]}var yd=/\[Pasted text #\d+ \+\d+ lines?\]/g;function Zi(e){return e.replace(/\r\n?/g,`
234
+ `)}function ea(e){return e.includes(`
235
+ `)||e.length>200}function ta(e,t){let n=t.split(`
236
+ `).length;return`[Pasted text #${e} +${n} line${n===1?"":"s"}]`}function na(e,t){return e.replace(yd,n=>t.get(n)??n)}function oa(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function On(e,t){let n=Math.max(1,t),o=[],r=0;for(let s of e.split(`
237
+ `)){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 Un(e,t){let n=0;for(let o=0;o<e.length&&e[o].start<=t;o++)n=o;return n}function ra(e,t,n){let o=Un(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 sa(e,t,n){let o=e[Un(e,t)];return n==="start"?o.start:o.end}function ia(e,t,n){let o=Math.max(1,n),r=e.split(`
238
238
  `),s=0;for(let c=r.length-1;c>=0;c--){let p=r[c];if(s+=Math.max(1,Math.ceil(p.length/o)),s>=t){let g=s-t;return{text:[g>0?p.slice(g*o):p,...r.slice(c+1)].join(`
239
- `),truncated:c>0||g>0}}}return{text:e,truncated:!1}}function ca(e){let t=!1,n=0,o=0;for(let r of e.split(`
240
- `)){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 xr,jsx as R,jsxs as W}from"react/jsx-runtime";var sp={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""},wa=` 1. /init \u2014 pick your Document360 environment & scaffold config
239
+ `),truncated:c>0||g>0}}}return{text:e,truncated:!1}}function aa(e){let t=!1,n=0,o=0;for(let r of e.split(`
240
+ `)){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 wr,jsx as R,jsxs as W}from"react/jsx-runtime";var Zd={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""},ma=` 1. /init \u2014 pick your Document360 environment & scaffold config
241
241
  2. /login \u2014 sign in; your project & workspace are selected automatically
242
- 3. /devhints \u2014 have your builder hand over an architecture brief, then ask me to write the docs`;function ip(e,t,n,o){let r=$a(n),s=r.state==="ready"?r.kind==="api"?"API key":"subscription":r.state==="maybe"?"subscription":"not signed in \u2014 run /doctor (needs Claude Code, not Claude Desktop)",c=$r(e),p=Q(e),g=(p?.docsDir??va).replace(/\/+$/,""),b=p?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${g}/ + config`,k={version:t,claude:s,model:c.model??"Claude Code default model",modelSource:sp[c.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:p!==null,mode:b};if(p===null)return k;try{let D=fe(e,o);k.profile=D.name,k.apiUrl=D.connection.apiUrl,k.prod=D.production,k.project=D.project.projectName??D.project.projectId??"(chosen at login)";let w=Ye(D.name);if(w){let v={...gt(w.idToken)??{},...gt(w.accessToken)??{}},C=v.email??v.preferred_username??"signed in";_e(w)?w.refreshToken&&(k.who=C,k.loggedOut=!1,k.sessionHint="session expired \u2014 refreshing\u2026"):(k.who=C,k.loggedOut=!1,k.sessionHint=`session valid until ${new Date(w.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return k}function ap(e,t){try{let n=fe(e,t),o=Ye(n.name);if(!o)return{text:`profile "${n.name}" \u2014 not logged in (/login)`,prod:n.production};let r={...gt(o.idToken)??{},...gt(o.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return _e(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 ya=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],qn=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],lp="Ask me to write or update an article\u2026";function Fn({ch:e,dim:t}){let[n,o]=q(!0);return Oe(()=>{let r=setInterval(()=>o(s=>!s),530);return()=>clearInterval(r)},[]),R(T,{inverse:n,color:t&&!n?"gray":void 0,children:e})}var cp=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function ka(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var up=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function ba(e){try{return xa(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function dp(e,t){let n=t??[];return n.length===0?["src","api","services","packages","modules"].some(o=>ba(Lt(e,o))):n.some(o=>!o.includes("/")&&!o.endsWith(".md")&&ba(Lt(e,o)))}function pp({startTime:e,chars:t}){let[n,o]=q(0);Oe(()=>{let g=setInterval(()=>o(b=>b+1),120);return()=>clearInterval(g)},[]);let r=qn[n%qn.length],s=ya[Math.floor(n/16)%ya.length],c=Math.floor((Date.now()-e)/1e3),p=Math.round(t/4);return W(K,{children:[R(T,{color:H,children:` ${r} ${s}\u2026 `}),R(T,{color:"gray",children:`(${Rt(c)} \xB7 ~${p} tokens \xB7 esc to interrupt)`})]})}var fp=12e4;function mp({p:e}){let[t,n]=q(0);Oe(()=>{let _=setInterval(()=>n(F=>F+1),150);return()=>clearInterval(_)},[]);let o=qn[t%qn.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),p=Date.now()-e.lastAt>=fp,g=e.verb??"Converting",b=e.unit??"article",k=e.rows.reduce((_,F)=>_+F.items,0),D=g.toLowerCase(),w=e.rows,v=0;if(w.length>On){let _=w.findIndex(ee=>ee.status==="active"||ee.status==="pending"),F=Math.max(0,Math.min(_-2,w.length-On));v=w.slice(0,F).filter(ee=>ee.status==="done").length,w=w.slice(F,F+On)}let C=Math.min(40,Math.max(...w.map(_=>fr(_.label).length),0)),N=_=>_==="failed"?"red":_==="active"?H:void 0;return W(K,{flexDirection:"column",children:[W(K,{children:[R(T,{color:H,children:` ${o} ${g} ${k} ${b}${k===1?"":"s"} `}),R(T,{color:"gray",children:`\xB7 ${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${Rt(r)} \xB7 esc to stop`})]}),v>0&&R(T,{dimColor:!0,children:` \u2714 +${v} done`}),w.map(_=>W(T,{color:N(_.status),dimColor:_.status==="done"||_.status==="pending",children:[` ${Zi[_.status]} ${fr(_.label).padEnd(C)}`,_.status!=="pending"?` (${_.items} ${b}${_.items===1?"":"s"})`:"",_.status==="active"?` \u2190 ${D}\u2026`:""]},_.label)),p&&R(T,{color:"yellow",children:` \u26A0 no activity for ${Rt(s)} \u2014 press esc to stop if it's wedged`})]})}function Ca({cwd:e,auth:t,profileName:n,version:o}){let{exit:r}=xd(),[s,c]=q(n),[p,g]=q(null),[b,k]=q({text:"",pos:0}),D=b.text,w=z(l=>{k(i=>{let a=typeof l=="function"?l(i.text):l;return{text:a,pos:a.length}})},[]),v=z(l=>{k(i=>({text:i.text.slice(0,i.pos)+l+i.text.slice(i.pos),pos:i.pos+l.length}))},[]),C=z(()=>{k(l=>{if(l.pos===0)return l;let i=l.text.slice(0,l.pos),a=sa(i)??i.slice(0,-1);return{text:a+l.text.slice(l.pos),pos:a.length}})},[]),[N,_]=q(!1),[F,ee]=q(!1),[$e,oe]=q(0),[ke,se]=q(null),[wt,G]=q([]),ze=J(0),[Ra,Wt]=q(!1),[Da,Xe]=q(0),Ue=J(0),[yt,Ke]=q(0),[Je,Ft]=q(null),Pr=J(new Map),ja=J(0),Qe=J([]),me=J(null);me.current===null&&(me.current=pa({cwd:e,profileName:s,allowProdWrites:!1}));let ve=J({uuid:null,firstPrompt:null,titleFired:!1}),qt=J(new Map),Ze=J(!1),Hn=J([]),Re=J([]),Bn=J(null),[Sr,et]=q(null),[Me,Le]=q(null),[Ce,tt]=q(null),[De,nt]=q(null),[ie,ot]=q(null),[he,rt]=q(null),[We,st]=q(null),[Tr,Ht]=q(null),[ge,Fe]=q(null),[ce,it]=q(null),[Rr,Gn]=q([]),qe=J([]),kt=J(!1),we=J(null),[Dr,X]=q(null),Bt=J(!1),Gt=J(null),[ye,He]=q(null),[jr,at]=q(0),[Ar,lt]=q(0),[Aa,Er]=q(0),Vn=hr(()=>{try{return $r(e).model??"auto"}catch{return null}},[e,Aa]),{stdout:be}=vd(),[,Ea]=q(0),Ir=J(`${be.columns??80}x${be.rows??24}`),je=z(()=>Math.max(20,(be.columns??80)-1),[be]),d=z(l=>{Qe.current.push(l),console.log(mo(l,je()))},[je]);Oe(()=>{let l=`d360-writer \xB7 ${Sr??Mt(e)}`;if(!N){Nn(0),Ot(`${pr} ${l}`);return}Nn(3);let i=0;Ot(`${_n[0]} ${l}`);let a=setInterval(()=>{i=(i+1)%_n.length,Ot(`${_n[i]} ${l}`)},400);return()=>clearInterval(a)},[N,e,Sr]),Oe(()=>()=>Nn(0),[]),Oe(()=>{d({kind:"banner",info:ip(e,o,t,s)});let l=$a(t).state==="missing";if(l&&d({kind:"note",tone:"warn",text:`\u26A0 Set up Claude first \u2014 d360-writer's agent runs on Claude Code, and it's not signed in on this machine yet.
243
- ${Pd}
244
- Run /doctor anytime to re-check and see the fix. (You can configure Document360 meanwhile \u2014 the agent just needs Claude before it writes.)`}),!Q(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:
245
- ${wa}`}),G(l?["/doctor","/init"]:["/init"]);return}l&&G(["/doctor"]);try{let i=fe(e,s),a=Ye(i.name);a&&_e(a)&&a.refreshToken?Dd({profile:i.name,connection:i.connection}).then(()=>{d({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),oe(h=>h+1)}).catch(()=>{d({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),G(["/login"])}):(!a||_e(a))&&(d({kind:"note",tone:"warn",text:`Profile "${i.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),G(["/login"]))}catch{}try{let i=Q(e),a=fe(e,s),h=Ye(a.name);i&&h&&!_e(h)&&dp(e,i.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."}),G(["/scope"]))}catch{}try{let i=vn(e),a=[];i.guideRefreshed&&a.push("refreshed the dev hand-off guide"),i.notifiedBuilder&&a.push(`left the builder a message to re-run onboarding (v${i.completedVersion??"?"}\u2192v${i.toVersion})`),a.length&&d({kind:"note",tone:"info",text:`\u{1F504} Onboarding protocol updated \u2014 ${a.join(" and ")}.`})}catch{}try{let i=ht(mt(e));i&&(d({kind:"note",tone:"info",text:`\u{1F4CB} ${i} from the repo's coding agent \u2014 press 1 to /catch-up.`}),G(a=>a.includes("/catch-up")?a:[...a,"/catch-up"]))}catch{}try{np(tp())&&Q(e)?.mode!=="engineer"&&ep({pkg:Ve,currentVersion:o}).then(async i=>{if(!i.updateAvailable||!i.latest)return;d({kind:"note",tone:"info",text:`\u2B06 Updating ${Ve} ${i.current} \u2192 ${i.latest}\u2026`}),(await Tn()).ok?(d({kind:"note",tone:"ok",text:`\u2713 Updated to v${i.latest} \u2014 /exit and relaunch to apply (press 1).`}),G(h=>h.includes("/exit")?h:[...h,"/exit"])):d({kind:"note",tone:"warn",text:`Update available (v${i.latest}) but auto-update failed \u2014 run: ${Sn}`})}).catch(()=>{})}catch{}},[]),Oe(()=>{let l=null,i=null,a=()=>{l&&clearTimeout(l),l=setTimeout(()=>{l=null;let h=`${be.columns??80}x${be.rows??24}`;h!==Ir.current&&(Ir.current=h,Ea(u=>u+1),i&&clearTimeout(i),i=setTimeout(()=>{i=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+Ss(Qe.current,je())+"\x1B[?2026l")},80))},400)};return be.on("resize",a),()=>{l&&clearTimeout(l),i&&clearTimeout(i),be.off("resize",a)}},[be,je]);let Be=Math.max(20,(be.columns??80)-1),Nr=hr(()=>ap(e,s),[e,s,$e]),Yn=(()=>{let l=Q(e);if(!l)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};let i=Lt(e,(l.docsDir??va).replace(/\/+$/,"")),a=(()=>{try{return gr(i)&&xa(i).length>0}catch{return!1}})();try{let h=fe(e,s),u=Ye(h.name);if(!(!!u&&!(_e(u)&&!u.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${h.name}")\u2026`,isSetup:!0};if(!h.project.workspaceId&&!a)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}return a?{text:lp,isSetup:!1}:{text:"Let's get started \u2014 try /devhints (best docs), or: write the docs for this repo",isSetup:!1}})(),bt=ss(D),_r=bt.length>0&&!N,zn=p!==null?la(p,8,Be):null,Vt=ge?ge.paths.filter(l=>!ge.query||l.toLowerCase().includes(ge.query.toLowerCase())).slice(0,8):[],Yt=ye?ye.sessions.filter(l=>{let i=ye.query.toLowerCase();return!i||l.name.toLowerCase().includes(i)||l.firstPrompt.toLowerCase().includes(i)}).slice(0,8):[],ue=z((l,i)=>{me.current?.close(),me.current=pa({cwd:e,resume:l,profileName:i??s,allowProdWrites:Ze.current}),l||(ve.current={uuid:null,firstPrompt:null,titleFired:!1},et(null)),at(0),lt(0)},[e,s]),Or=z((l,i,a)=>{if(cp.test(l))try{let h=fe(e,s),u=typeof i.project_id=="string"&&i.project_id||h.project.projectId,m=En(i,a),y=[],S=In(a);l.endsWith("publish_article")&&S&&y.push(`Live: ${S}`),m&&u&&y.push(`Preview: ${An(h.connection.portalUrl,u,m,h.project.languageCode??"en")}`),y.length>0&&d({kind:"link",lines:y})}catch{}},[e,s,d]),de=z(async(l,i)=>{Wt(!0),se(null),G([]),Ht(null);let a=++ze.current;i?.quietUser||d({kind:"user",text:i?.echoDisplay&&i.display?i.display:l});let h=ve.current;h.firstPrompt||(h.firstPrompt=i?.display??l),Ue.current=Date.now(),Xe(0),_(!0),qt.current.clear();let u="",m=!1,y=!1,S="",E=null,I=()=>{E||(E=setTimeout(()=>{E=null,g(S.length>0?S:null)},60))},re=()=>{E&&clearTimeout(E),E=null,g(null)},V=()=>{if(S.trim()){let $=S.trimEnd();d({kind:"assistant",text:$})}S="",re()};try{for await(let $ of me.current.send(l))if($.type==="session"){if(!h.uuid){h.uuid=$.sessionId;let P=new Date().toISOString(),O=_d(h.firstPrompt??"session");Od({uuid:$.sessionId,name:O,renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:P,updatedAt:P}),et(O)}}else if($.type==="text"){S+=$.delta,u+=$.delta;let P=ca(S);if(P>0){let O=S.slice(0,P).trimEnd();O&&d({kind:"assistant",text:O}),S=S.slice(P)}I(),Xe(O=>O+$.delta.length)}else if($.type==="tool"){m=!0;let P=Dn($.name,$.input);P&&(V(),d({kind:"tool",title:P.title,sep:P.sep,arg:P.arg}),qt.current.set($.id,{name:$.name,input:$.input}))}else if($.type==="article_diff"){let P=Ge($.oldContent,$.newContent,je());P&&(V(),d({kind:"diff",added:P.added,removed:P.removed,lines:P.lines,hidden:P.hidden}))}else if($.type==="tool_result"){$.isError&&/run \/login|not logged in|session expired|rejected the token/i.test($.output)&&(Bt.current=!0);let P=qt.current.get($.id);if(P){qt.current.delete($.id),V();let O=jn($.output,4,$.isError?void 0:P.name,P.input);d({kind:"tool-result",lines:O.lines,hidden:O.hidden,isError:$.isError}),$.isError||Or(P.name,P.input,$.output)}}else if($.type==="result"){V(),at(O=>O+$.outputTokens),lt(O=>O+$.costUsd),d({kind:"done",seconds:Math.round((Date.now()-Ue.current)/1e3),tokens:$.outputTokens,costUsd:$.costUsd,ok:$.ok}),!y&&!m&&!u.trim()&&!kt.current&&d({kind:"note",tone:"warn",text:"The agent produced no output. This usually means the Claude model/runtime did not start \u2014 check sign-in and model with /doctor (on a subscription, make sure Claude Code is logged in on this machine)."});let P=$.ok?Qi(u,Pt.map(O=>O.name)):[];if(P.length>0?G(P):$.ok&&u.trim()&&Wd(l,u,e).then(O=>{O&&ze.current===a&&se(O)}).catch(()=>{}),h.uuid&&(wr(h.uuid),!h.titleFired)){h.titleFired=!0;let O=h.uuid,Z=h.firstPrompt;Z&&fa(Z,e).then(U=>{U&&(Nd(O,U),et(U))}).catch(()=>{})}}else $.type==="error"&&(V(),y=!0,$.kind==="auth"&&(Bt.current=!0),d({kind:"note",text:`agent error: ${$.message}`,tone:"error"}))}finally{_(!1),re(),oe($=>$+1),ve.current.uuid&&Ki(e,ve.current.uuid,Qe.current),Bt.current&&(Bt.current=!1,Gt.current=i?.display??l,G(["/login"])),kt.current&&(kt.current=!1,qe.current.length>0&&(d({kind:"note",tone:"info",text:`(${qe.current.length} queued message(s) discarded)`}),qe.current=[],Gn([])),d({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,d,Or,je]),zt=z(async l=>{let i=kr(l),a=Wn(e,"standard");Ue.current=Date.now(),Xe(0),_(!0);let h=new AbortController;we.current=h,X({verb:"Publishing",total:i.length,done:0,active:[],rows:Ut(i),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Publishing ${l.length} article${l.length===1?"":"s"} across ${i.length} partition${i.length===1?"":"s"} (\u2264${Dt} agents at once) on ${a.model}\u2026 (esc to stop)`});try{for await(let u of Ln({cwd:e,partitions:i,promptFor:m=>pn(m.paths),concurrency:Dt,profileName:s,allowProdWrites:Ze.current,model:a.model,signal:h.signal}))if(u.type==="partition_status")u.status==="running"?X(m=>m&&{...m,active:[...m.active,u.label],rows:Se(m.rows,u.label,"active"),lastAt:Date.now()}):X(m=>m&&{...m,active:m.active.filter(y=>y!==u.label),done:m.done+1,rows:Se(m.rows,u.label,u.status==="done"?"done":"failed"),lastAt:Date.now()});else if(u.type==="partition_event")X(m=>{if(!m)return m;let y={...m,lastAt:Date.now()};return u.event.type==="tool"?y.tools=m.tools+1:u.event.type==="text"&&(y.chars=m.chars+u.event.delta.length),y});else if(u.type==="run_done"){lt(S=>S+u.totalCostUsd),at(S=>S+u.results.reduce((E,I)=>E+I.outputTokens,0));let m=u.aborted?"Stopped. ":"",y=t.kind==="api"?"api":"subscription";d({kind:"note",tone:u.aborted?"warn":u.ok?"ok":"warn",text:m+yo(u.results,i,y).join(`
246
- `)})}}catch(u){d({kind:"note",tone:"error",text:`Publish run failed: ${u.message}`})}finally{we.current=null,X(null),_(!1),oe(u=>u+1)}},[e,s,d,t]),Xn=z(async l=>{await de(wo(l),{display:`/publish ${l}`,quietUser:!0});try{let i=await fn(e,s,l.replace(/\\/g,"/"));i.length>0&&(Ht(`Related articles not yet published (${i.length}):`),G([`/publish --related ${l}`,...i.map(a=>`/publish ${a}`)]))}catch{}},[e,s,de]),Kn=z(l=>{let i=Ye(l);if(!i||_e(i)&&!i.refreshToken)return null;let a={...gt(i.idToken)??{},...gt(i.accessToken)??{}};return a.email??a.preferred_username??"signed in"},[]),Jn=z((l,i)=>{if(i){let a=Q(e);a&&(a.defaultProfile=l,Fd(a,e))}c(l),ue(void 0,l),d({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${l}"${i?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),Kn(l)||(d({kind:"note",tone:"warn",text:`Profile "${l}" is not signed in \u2014 do you want to log in now? (press 1)`}),G(["/login"])),oe(a=>a+1)},[e,d,ue,Kn]),Xt=z((l,i,a)=>{en(e,l,i,a.id,a.name),d({kind:"note",tone:"ok",text:`Switched to workspace "${a.name??a.id}" (agent restarted).`}),ue(),ma(e,l)||(d({kind:"note",tone:"info",text:"Setup complete. For the best docs, press tab to run /devhints \u2014 your builder hands over an architecture brief, then ask me to write the docs. (Or just ask me to analyze this repo.)"}),se("/devhints"))},[e,d,ue]),Kt=z(async l=>{let i;try{i=await vt(e,l)}catch{d({kind:"note",tone:"info",text:"Next: pick a workspace \u2014 run /workspace."}),G(["/workspace"]);return}if(i.workspaces.length===0){d({kind:"note",tone:"warn",text:"This project has no workspaces yet. Create one in Document360, then run /workspace."});return}let a=i.workspaces.filter(u=>(u.workspace_type??"").toLowerCase()!=="apidocumentation"),h=a.length>0?a:i.workspaces;if(h.length===1){let u=h[0],m=i.workspaces.length-h.length;d({kind:"note",tone:"ok",text:`Selected the "${u.name??u.id}" workspace${m>0?" (skipped the API-documentation workspace)":""}.`}),Xt(i.profile,i.projectId,u);return}d({kind:"note",tone:"info",text:`Next: pick the workspace your articles publish to (${h.length} available).`}),G(["/workspace"])},[e,d,Xt]),Ur=z((l,i)=>{oo(e,l,i.id,i.name),d({kind:"note",tone:"ok",text:`Project set to "${i.name??i.id}" (agent restarted).`}),ue(),Kt(l)},[e,d,ue,Kt]),Qn=z(async l=>{let i;try{i=fe(e,l)}catch{return}if(!i.project.workspaceId){if(i.project.projectId){if(!i.project.projectName)try{let{projects:a}=await dt(e,i.name),h=a.find(u=>u.id===i.project.projectId);h?.name&&rp(e,i.name,{projectName:h.name})}catch{}await Kt(i.name);return}d({kind:"note",tone:"info",text:"Next: pick the Document360 project."}),G(["/project"])}},[e,d,Kt]),Jt=z(()=>{let l=Re.current[0];if(!l)return;d({kind:"note",tone:"info",text:`\u25CF ${l.title} (${l.path})`});for(let a of l.notes)d({kind:"note",tone:"warn",text:`\u26A0 ${a}`});l.overwritesLocalChanges&&d({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let i=Ge(l.oldContent,l.newContent,je());d(i?{kind:"diff",added:i.added,removed:i.removed,lines:i.lines,hidden:i.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),d({kind:"note",tone:"info",text:`Write ${l.path}? (y/n \u2014 anything else cancels)`})},[d,je]),Mr=z(l=>{if(Re.current.length===0)return!1;let i=l.trim().toLowerCase();if(i==="y"||i==="yes"){let a=Re.current.shift();try{qd({cwd:e,profileName:s},a),d({kind:"note",tone:"ok",text:`\u2713 Pulled ${a.path} (sync base advanced).`}),Re.current.length===0&&G(h=>h.length>0?h:["/sync"])}catch(h){d({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(i==="n"||i==="no"){let a=Re.current.shift();d({kind:"note",tone:"info",text:`Skipped ${a.path}.`})}else{let a=Re.current.length;return Re.current=[],d({kind:"note",tone:"info",text:`Pull cancelled (${a} article(s) left untouched).`}),!0}return Jt(),!0},[e,s,Jt,d]),Lr=z(l=>{let i=Bn.current;if(!i)return!1;if(Bn.current=null,se(null),ze.current++,l.trim()!==i.repoName)return d({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:a,failed:h}=ir(e,i.targets);d({kind:"note",tone:h.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${a.length} item${a.length===1?"":"s"}. The repo is back to its original state.`});for(let u of h)d({kind:"note",tone:"error",text:` \u2717 ${u.path}: ${u.error}`});return d({kind:"note",tone:"info",text:`Clean slate \u2014 set up d360-writer again whenever you're ready:
247
- ${wa}`}),G(["/init"]),Wt(!1),!0},[e,d]),Wr=z(async l=>{let i=l.slice(1).trim().split(/\s+/),a=(i[0]??"").toLowerCase(),h=i.slice(1);switch(Wt(!0),a){case"help":d({kind:"note",tone:"info",text:cn().join(`
248
- `)});return;case"exit":case"quit":me.current?.close(),r();return;case"clear":ue(),Qe.current=[],Wt(!1),se(null),ze.current++,d({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let u;try{u=fe(e,s)}catch(m){d({kind:"note",tone:"error",text:m.message});return}d({kind:"note",tone:"info",text:`Profile "${u.name}" \u2192 ${u.connection.name} (${u.connection.apiUrl})${u.production?" \u26A0 PRODUCTION":""}`}),ee(!0);try{let m=await Sd(u.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},S=>d({kind:"note",tone:"info",text:S})),y=Td(u.name,m);Rd(y),await rn(y,u.name,S=>d({kind:"note",tone:"info",text:S})),d({kind:"note",tone:"ok",text:`\u2713 Logged in to "${u.name}" as ${nn(y)}`}),await Qn(s),Gt.current&&(se(Gt.current),Gt.current=null,d({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(m){d({kind:"note",tone:"error",text:`Login failed: ${m.message}`})}finally{ee(!1),oe(m=>m+1)}return}case"allow-prod":{let u=!1;try{u=fe(e,s).production}catch{}if(!u){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}Ze.current=!0,ue(),d({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let u=qo(h.join(" ")),m=ve.current.uuid;if(!m){d({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!u){d({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let y=ve.current.firstPrompt??"";fa(y,e).then(S=>{S?(se(`/rename ${S}`),d({kind:"note",tone:"info",text:`Suggestion: "${S}" \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}Ld(m,u),et(u),Ot(`${pr} d360-writer \xB7 ${u}`),d({kind:"note",tone:"ok",text:`Session renamed to "${u}".`});return}case"profile":{let u=h[0],m=Q(e);if(!u){let y=Object.entries(m?.profiles??{});if(y.length===0){d({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let S=y.map(([re,V])=>({name:re,env:V.connection?.environment??"custom",prod:V.production===!0,who:Kn(re)})),E=s??m?.defaultProfile,I=Math.max(0,S.findIndex(re=>re.name===E));tt({cursor:I,current:I,rows:S});return}if(u==="add"){let y=Ho(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]}).`}),Jn(h[1],!1);return}if(!m?.profiles?.[u]){d({kind:"note",tone:"error",text:`Unknown profile "${u}". Create it: /profile add ${u} <environment>`});return}Jn(u,!0);return}case"doctor":await Nt(h,{cwd:e});return;case"mcp":await dn(h);return;case"init":{if(Q(e)){d({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let u=Gd().map(m=>({name:m,apiUrl:Vd(m).apiUrl}));nt({cursor:0,rows:u});return}case"resume":{let u=h.join(" ").trim(),m=Md(e).filter(E=>E.uuid!==ve.current.uuid);if(!u){if(!m.length){d({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}He({query:"",cursor:0,sessions:m});return}let y=Ud(e,u);if(!y){d({kind:"note",tone:"error",text:`No session matches "${u}".`});return}ue(y.uuid),ve.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},et(y.name),wr(y.uuid),Qe.current=[];let S=dr(e,y.uuid);for(let E of S)d(E);d({kind:"note",tone:"ok",text:S.length?`Resumed "${y.name}" \u2014 restored ${S.length} message(s); continue where you left off.`:`Resumed "${y.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let u=h.join(" ").trim(),m;try{m=await vt(e,s)}catch(S){d({kind:"note",tone:"error",text:`Could not list workspaces: ${S.message}`});return}if(!u){let S=m.workspaces.map(I=>({id:I.id,name:I.name??I.id,type:I.workspace_type}));if(S.length===0){d({kind:"note",tone:"info",text:"No workspaces in this project."});return}let E=Math.max(0,S.findIndex(I=>I.id===m.current));ot({cursor:E,current:E,rows:S,profile:m.profile,projectId:m.projectId,projectName:m.projectName,environment:m.environment});return}let y=no(m.workspaces,u);if(!y){d({kind:"note",tone:"error",text:`No workspace matches "${u}".`});return}Xt(m.profile,m.projectId,y);return}case"project":{let u=h.join(" ").trim(),m;try{m=await dt(e,s)}catch(S){d({kind:"note",tone:"error",text:`Could not list projects: ${S.message} (signed in? try /login)`});return}if(!u){let S=m.projects.map(I=>({id:I.id,name:I.name??I.id,sub:I.sub_domain}));if(S.length===0){d({kind:"note",tone:"info",text:"No projects found for this identity."});return}let E=S.findIndex(I=>I.id===m.current);rt({cursor:Math.max(0,E),current:E,rows:S,profile:m.profile,environment:m.environment});return}let y=zr(m.projects,u);if(!y){d({kind:"note",tone:"error",text:`No project matches "${u}". Available: ${m.projects.map(S=>S.name??S.id).join(", ")}`});return}Ur(m.profile,y);return}case"logout":{let u;try{u=fe(e,s).name}catch(y){d({kind:"note",tone:"error",text:y.message});return}let m=Ed(u);Id(e,u),ue(),oe(y=>y+1),d({kind:"note",tone:m?"ok":"info",text:m?`\u2713 Signed out of "${u}" and cleared its project/workspace selection.`:`Profile "${u}" was not signed in. Cleared any project/workspace selection.`}),d({kind:"note",tone:"info",text:"Run /login to sign in and pick a project."}),G(["/login"]);return}case"publish":{if(h[0]==="--related"&&h[1]){let m=await fn(e,s,h[1].replace(/\\/g,"/"));if(m.length===0){d({kind:"note",tone:"ok",text:"\u2713 All related articles are already published."});return}await zt(m);return}if(h[0]&&h[0]!=="--all"){await Xn(h[0]);return}let u=h[0]==="--all";d({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let m=await yr({cwd:e,profileName:s}),y=jt(m.entries);if(y.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let S=m.counts["unknown-base"]??0;S>0&&d({kind:"note",tone:"info",text:`(${S} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(u){await zt(y.map(S=>S.path));return}st({cursor:0,rows:Rs(y)})}catch(m){d({kind:"note",tone:"error",text:`Could not compute sync status: ${m.message}`}),d({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let u=h.join(" ").trim();if(!u){let y=[];try{y=Object.keys(ma(e,fe(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}Fe({query:"",cursor:0,paths:y});return}let m=Cd(u)?u:Lt(e,u);if(gr(m)){try{d({kind:"preview",name:Mt(m),text:ka(ua(m,"utf8"))})}catch(y){d({kind:"note",tone:"error",text:`Could not read ${m}: ${y.message}`})}return}if(up.test(u)){try{let y=fe(e,s),S={profile:y.name,connection:y.connection},E=y.project.projectId??jd(S),I=await Ad(S,E,u);d({kind:"preview",name:I.title??u,text:I.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:`"${u}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let u=h[0]?.trim();if(!u){let E=yn($r(e));Le({cursor:E,current:E});return}let{lines:m,changed:y,effective:S}=At(e,u);for(let E of m)d({kind:"note",tone:E.startsWith("\u26A0")?"warn":E.startsWith("\u2713")?"ok":"info",text:E});y&&(Er(E=>E+1),me.current?.setModel(S));return}case"convert":{if(!Q(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:u,run:m}=$o(h),y=Xd(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 S=vo(y,u);if(S.length===0){d({kind:"note",tone:"error",text:`No tracked articles under "${u}". (${y.length} are tracked overall.)`});return}let E=kr(S),I=3,re=`/convert${u?` --scope ${u}`:""} --run`,V=Wn(e,"light");if(!m){let P=ha({files:Co(e,S),op:"convert",model:V.model}),O=u?`Scope: ${u} (${S.length} of ${y.length} tracked)
242
+ 3. I'll hand you a one-paste prompt for your builder \u2014 then just ask me to write the docs`;function ep(e,t,n,o){let r=ka(n),s=r.state==="ready"?r.kind==="api"?"API key":"subscription":r.state==="maybe"?"subscription":"not signed in \u2014 run /doctor (needs Claude Code, not Claude Desktop)",c=yr(e),p=Q(e),g=(p?.docsDir??ba).replace(/\/+$/,""),b=p?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${g}/ + config`,k={version:t,claude:s,model:c.model??"Claude Code default model",modelSource:Zd[c.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:p!==null,mode:b};if(p===null)return k;try{let D=fe(e,o);k.profile=D.name,k.apiUrl=D.connection.apiUrl,k.prod=D.production,k.project=D.project.projectName??D.project.projectId??"(chosen at login)";let w=Ye(D.name);if(w){let v={...kt(w.idToken)??{},...kt(w.accessToken)??{}},C=v.email??v.preferred_username??"signed in";_e(w)?w.refreshToken&&(k.who=C,k.loggedOut=!1,k.sessionHint="session expired \u2014 refreshing\u2026"):(k.who=C,k.loggedOut=!1,k.sessionHint=`session valid until ${new Date(w.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return k}function tp(e,t){try{let n=fe(e,t),o=Ye(n.name);if(!o)return{text:`profile "${n.name}" \u2014 not logged in (/login)`,prod:n.production};let r={...kt(o.idToken)??{},...kt(o.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return _e(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 ha=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],Fn=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],np="Ask me to write or update an article\u2026";function Wn({ch:e,dim:t}){let[n,o]=q(!0);return Oe(()=>{let r=setInterval(()=>o(s=>!s),530);return()=>clearInterval(r)},[]),R(S,{inverse:n,color:t&&!n?"gray":void 0,children:e})}var op=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function ga(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var rp=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function wa(e){try{return ya(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function sp(e,t){let n=t??[];return n.length===0?["src","api","services","packages","modules"].some(o=>wa(Wt(e,o))):n.some(o=>!o.includes("/")&&!o.endsWith(".md")&&wa(Wt(e,o)))}function ip({startTime:e,chars:t}){let[n,o]=q(0);Oe(()=>{let g=setInterval(()=>o(b=>b+1),120);return()=>clearInterval(g)},[]);let r=Fn[n%Fn.length],s=ha[Math.floor(n/16)%ha.length],c=Math.floor((Date.now()-e)/1e3),p=Math.round(t/4);return W(K,{children:[R(S,{color:H,children:` ${r} ${s}\u2026 `}),R(S,{color:"gray",children:`(${At(c)} \xB7 ~${p} tokens \xB7 esc to interrupt)`})]})}var ap=12e4;function lp({p:e}){let[t,n]=q(0);Oe(()=>{let _=setInterval(()=>n(F=>F+1),150);return()=>clearInterval(_)},[]);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),p=Date.now()-e.lastAt>=ap,g=e.verb??"Converting",b=e.unit??"article",k=e.rows.reduce((_,F)=>_+F.items,0),D=g.toLowerCase(),w=e.rows,v=0;if(w.length>_n){let _=w.findIndex(ee=>ee.status==="active"||ee.status==="pending"),F=Math.max(0,Math.min(_-2,w.length-_n));v=w.slice(0,F).filter(ee=>ee.status==="done").length,w=w.slice(F,F+_n)}let C=Math.min(40,Math.max(...w.map(_=>dr(_.label).length),0)),N=_=>_==="failed"?"red":_==="active"?H:void 0;return W(K,{flexDirection:"column",children:[W(K,{children:[R(S,{color:H,children:` ${o} ${g} ${k} ${b}${k===1?"":"s"} `}),R(S,{color:"gray",children:`\xB7 ${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${At(r)} \xB7 esc to stop`})]}),v>0&&R(S,{dimColor:!0,children:` \u2714 +${v} done`}),w.map(_=>W(S,{color:N(_.status),dimColor:_.status==="done"||_.status==="pending",children:[` ${Ji[_.status]} ${dr(_.label).padEnd(C)}`,_.status!=="pending"?` (${_.items} ${b}${_.items===1?"":"s"})`:"",_.status==="active"?` \u2190 ${D}\u2026`:""]},_.label)),p&&R(S,{color:"yellow",children:` \u26A0 no activity for ${At(s)} \u2014 press esc to stop if it's wedged`})]})}function xa({cwd:e,auth:t,profileName:n,version:o}){let{exit:r}=kd(),[s,c]=q(n),[p,g]=q(null),[b,k]=q({text:"",pos:0}),D=b.text,w=z(l=>{k(i=>{let a=typeof l=="function"?l(i.text):l;return{text:a,pos:a.length}})},[]),v=z(l=>{k(i=>({text:i.text.slice(0,i.pos)+l+i.text.slice(i.pos),pos:i.pos+l.length}))},[]),C=z(()=>{k(l=>{if(l.pos===0)return l;let i=l.text.slice(0,l.pos),a=oa(i)??i.slice(0,-1);return{text:a+l.text.slice(l.pos),pos:a.length}})},[]),[N,_]=q(!1),[F,ee]=q(!1),[$e,oe]=q(0),[ke,se]=q(null),[bt,G]=q([]),ze=J(0),[Pa,Ft]=q(!1),[Ta,Xe]=q(0),Ue=J(0),[xt,Ke]=q(0),[Je,qt]=q(null),xr=J(new Map),Sa=J(0),Qe=J([]),me=J(null);me.current===null&&(me.current=ua({cwd:e,profileName:s,allowProdWrites:!1}));let ve=J({uuid:null,firstPrompt:null,titleFired:!1}),Ht=J(new Map),Ze=J(!1),qn=J([]),Re=J([]),Hn=J(null),[$r,et]=q(null),[Me,Le]=q(null),[Ce,tt]=q(null),[De,nt]=q(null),[ie,ot]=q(null),[he,rt]=q(null),[We,st]=q(null),[vr,Bt]=q(null),[ge,Fe]=q(null),[ce,it]=q(null),[Cr,Bn]=q([]),qe=J([]),$t=J(!1),we=J(null),[Pr,X]=q(null),Gt=J(!1),Vt=J(null),[ye,He]=q(null),[Tr,at]=q(0),[Sr,lt]=q(0),[Ra,Rr]=q(0),Gn=fr(()=>{try{return yr(e).model??"auto"}catch{return null}},[e,Ra]),{stdout:be}=xd(),[,Da]=q(0),Dr=J(`${be.columns??80}x${be.rows??24}`),je=z(()=>Math.max(20,(be.columns??80)-1),[be]),u=z(l=>{Qe.current.push(l),console.log(go(l,je()))},[je]);Oe(()=>{let l=`d360-writer \xB7 ${$r??Lt(e)}`;if(!N){In(0),Ut(`${ur} ${l}`);return}In(3);let i=0;Ut(`${Nn[0]} ${l}`);let a=setInterval(()=>{i=(i+1)%Nn.length,Ut(`${Nn[i]} ${l}`)},400);return()=>clearInterval(a)},[N,e,$r]),Oe(()=>()=>In(0),[]),Oe(()=>{u({kind:"banner",info:ep(e,o,t,s)});let l=ka(t).state==="missing";if(l&&u({kind:"note",tone:"warn",text:`\u26A0 Set up Claude first \u2014 d360-writer's agent runs on Claude Code, and it's not signed in on this machine yet.
243
+ ${vd}
244
+ Run /doctor anytime to re-check and see the fix. (You can configure Document360 meanwhile \u2014 the agent just needs Claude before it writes.)`}),!Q(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:
245
+ ${ma}`}),G(l?["/doctor","/init"]:["/init"]);return}l&&G(["/doctor"]);try{let i=fe(e,s),a=Ye(i.name);a&&_e(a)&&a.refreshToken?Sd({profile:i.name,connection:i.connection}).then(()=>{u({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),oe(h=>h+1)}).catch(()=>{u({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),G(["/login"])}):(!a||_e(a))&&(u({kind:"note",tone:"warn",text:`Profile "${i.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),G(["/login"]))}catch{}try{let i=Q(e),a=fe(e,s),h=Ye(a.name);i&&h&&!_e(h)&&sp(e,i.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."}),G(["/scope"]))}catch{}try{let i=fn(e),a=[];i.guideRefreshed&&a.push("refreshed the dev hand-off guide"),i.notifiedBuilder&&a.push(`left the builder a message to re-run onboarding (v${i.completedVersion??"?"}\u2192v${i.toVersion})`),a.length&&u({kind:"note",tone:"info",text:`\u{1F504} Onboarding protocol updated \u2014 ${a.join(" and ")}.`})}catch{}try{let i=yt(wt(e));i&&(u({kind:"note",tone:"info",text:`\u{1F4CB} ${i} from the repo's coding agent \u2014 press 1 to /catch-up.`}),G(a=>a.includes("/catch-up")?a:[...a,"/catch-up"]))}catch{}try{Jd(Kd())&&Q(e)?.mode!=="engineer"&&Xd({pkg:Ve,currentVersion:o}).then(async i=>{if(!i.updateAvailable||!i.latest)return;u({kind:"note",tone:"info",text:`\u2B06 Updating ${Ve} ${i.current} \u2192 ${i.latest}\u2026`}),(await Tn()).ok?(u({kind:"note",tone:"ok",text:`\u2713 Updated to v${i.latest} \u2014 /exit and relaunch to apply (press 1).`}),G(h=>h.includes("/exit")?h:[...h,"/exit"])):u({kind:"note",tone:"warn",text:`Update available (v${i.latest}) but auto-update failed \u2014 run: ${Pn}`})}).catch(()=>{})}catch{}},[]),Oe(()=>{let l=null,i=null,a=()=>{l&&clearTimeout(l),l=setTimeout(()=>{l=null;let h=`${be.columns??80}x${be.rows??24}`;h!==Dr.current&&(Dr.current=h,Da(d=>d+1),i&&clearTimeout(i),i=setTimeout(()=>{i=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+Es(Qe.current,je())+"\x1B[?2026l")},80))},400)};return be.on("resize",a),()=>{l&&clearTimeout(l),i&&clearTimeout(i),be.off("resize",a)}},[be,je]);let Be=Math.max(20,(be.columns??80)-1),jr=fr(()=>tp(e,s),[e,s,$e]),Vn=(()=>{let l=Q(e);if(!l)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};let i=Wt(e,(l.docsDir??ba).replace(/\/+$/,"")),a=(()=>{try{return la(i)&&ya(i).length>0}catch{return!1}})();try{let h=fe(e,s),d=Ye(h.name);if(!(!!d&&!(_e(d)&&!d.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${h.name}")\u2026`,isSetup:!0};if(!h.project.workspaceId&&!a)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}return a?{text:np,isSetup:!1}:{text:"Let's get started \u2014 try /devhints (best docs), or: write the docs for this repo",isSetup:!1}})(),vt=ts(D),Ar=vt.length>0&&!N,Yn=p!==null?ia(p,8,Be):null,Yt=ge?ge.paths.filter(l=>!ge.query||l.toLowerCase().includes(ge.query.toLowerCase())).slice(0,8):[],zt=ye?ye.sessions.filter(l=>{let i=ye.query.toLowerCase();return!i||l.name.toLowerCase().includes(i)||l.firstPrompt.toLowerCase().includes(i)}).slice(0,8):[],ue=z((l,i)=>{me.current?.close(),me.current=ua({cwd:e,resume:l,profileName:i??s,allowProdWrites:Ze.current}),l||(ve.current={uuid:null,firstPrompt:null,titleFired:!1},et(null)),at(0),lt(0)},[e,s]),Er=z((l,i,a)=>{if(op.test(l))try{let h=fe(e,s),d=typeof i.project_id=="string"&&i.project_id||h.project.projectId,m=An(i,a),y=[],T=En(a);l.endsWith("publish_article")&&T&&y.push(`Live: ${T}`),m&&d&&y.push(`Preview: ${jn(h.connection.portalUrl,d,m,h.project.languageCode??"en")}`),y.length>0&&u({kind:"link",lines:y})}catch{}},[e,s,u]),de=z(async(l,i)=>{Ft(!0),se(null),G([]),Bt(null);let a=++ze.current;i?.quietUser||u({kind:"user",text:i?.echoDisplay&&i.display?i.display:l});let h=ve.current;h.firstPrompt||(h.firstPrompt=i?.display??l),Ue.current=Date.now(),Xe(0),_(!0),Ht.current.clear();let d="",m=!1,y=!1,T="",E=null,I=()=>{E||(E=setTimeout(()=>{E=null,g(T.length>0?T:null)},60))},re=()=>{E&&clearTimeout(E),E=null,g(null)},V=()=>{if(T.trim()){let $=T.trimEnd();u({kind:"assistant",text:$})}T="",re()};try{for await(let $ of me.current.send(l))if($.type==="session"){if(!h.uuid){h.uuid=$.sessionId;let P=new Date().toISOString(),O=Id(h.firstPrompt??"session");Nd({uuid:$.sessionId,name:O,renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:P,updatedAt:P}),et(O)}}else if($.type==="text"){T+=$.delta,d+=$.delta;let P=aa(T);if(P>0){let O=T.slice(0,P).trimEnd();O&&u({kind:"assistant",text:O}),T=T.slice(P)}I(),Xe(O=>O+$.delta.length)}else if($.type==="tool"){m=!0;let P=Rn($.name,$.input);P&&(V(),u({kind:"tool",title:P.title,sep:P.sep,arg:P.arg}),Ht.current.set($.id,{name:$.name,input:$.input}))}else if($.type==="article_diff"){let P=Ge($.oldContent,$.newContent,je());P&&(V(),u({kind:"diff",added:P.added,removed:P.removed,lines:P.lines,hidden:P.hidden}))}else if($.type==="tool_result"){$.isError&&/run \/login|not logged in|session expired|rejected the token/i.test($.output)&&(Gt.current=!0);let P=Ht.current.get($.id);if(P){Ht.current.delete($.id),V();let O=Dn($.output,4,$.isError?void 0:P.name,P.input);u({kind:"tool-result",lines:O.lines,hidden:O.hidden,isError:$.isError}),$.isError||Er(P.name,P.input,$.output)}}else if($.type==="result"){V(),at(O=>O+$.outputTokens),lt(O=>O+$.costUsd),u({kind:"done",seconds:Math.round((Date.now()-Ue.current)/1e3),tokens:$.outputTokens,costUsd:$.costUsd,ok:$.ok}),!y&&!m&&!d.trim()&&!$t.current&&u({kind:"note",tone:"warn",text:"The agent produced no output. This usually means the Claude model/runtime did not start \u2014 check sign-in and model with /doctor (on a subscription, make sure Claude Code is logged in on this machine)."});let P=$.ok?Ki(d,Rt.map(O=>O.name)):[];if(P.length>0?G(P):$.ok&&d.trim()&&Md(l,d,e).then(O=>{O&&ze.current===a&&se(O)}).catch(()=>{}),h.uuid&&(mr(h.uuid),!h.titleFired)){h.titleFired=!0;let O=h.uuid,Z=h.firstPrompt;Z&&da(Z,e).then(U=>{U&&(Ed(O,U),et(U))}).catch(()=>{})}}else $.type==="error"&&(V(),y=!0,$.kind==="auth"&&(Gt.current=!0),u({kind:"note",text:`agent error: ${$.message}`,tone:"error"}))}finally{_(!1),re(),oe($=>$+1),ve.current.uuid&&zi(e,ve.current.uuid,Qe.current),Gt.current&&(Gt.current=!1,Vt.current=i?.display??l,G(["/login"])),$t.current&&($t.current=!1,qe.current.length>0&&(u({kind:"note",tone:"info",text:`(${qe.current.length} queued message(s) discarded)`}),qe.current=[],Bn([])),u({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,u,Er,je]),Xt=z(async l=>{let i=gr(l),a=Ln(e,"standard");Ue.current=Date.now(),Xe(0),_(!0);let h=new AbortController;we.current=h,X({verb:"Publishing",total:i.length,done:0,active:[],rows:Mt(i),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Publishing ${l.length} article${l.length===1?"":"s"} across ${i.length} partition${i.length===1?"":"s"} (\u2264${Et} agents at once) on ${a.model}\u2026 (esc to stop)`});try{for await(let d of Mn({cwd:e,partitions:i,promptFor:m=>gn(m.paths),concurrency:Et,profileName:s,allowProdWrites:Ze.current,model:a.model,signal:h.signal}))if(d.type==="partition_status")d.status==="running"?X(m=>m&&{...m,active:[...m.active,d.label],rows:Te(m.rows,d.label,"active"),lastAt:Date.now()}):X(m=>m&&{...m,active:m.active.filter(y=>y!==d.label),done:m.done+1,rows:Te(m.rows,d.label,d.status==="done"?"done":"failed"),lastAt:Date.now()});else if(d.type==="partition_event")X(m=>{if(!m)return m;let y={...m,lastAt:Date.now()};return d.event.type==="tool"?y.tools=m.tools+1:d.event.type==="text"&&(y.chars=m.chars+d.event.delta.length),y});else if(d.type==="run_done"){lt(T=>T+d.totalCostUsd),at(T=>T+d.results.reduce((E,I)=>E+I.outputTokens,0));let m=d.aborted?"Stopped. ":"",y=t.kind==="api"?"api":"subscription";u({kind:"note",tone:d.aborted?"warn":d.ok?"ok":"warn",text:m+bo(d.results,i,y).join(`
246
+ `)})}}catch(d){u({kind:"note",tone:"error",text:`Publish run failed: ${d.message}`})}finally{we.current=null,X(null),_(!1),oe(d=>d+1)}},[e,s,u,t]),zn=z(async l=>{await de(ko(l),{display:`/publish ${l}`,quietUser:!0});try{let i=await wn(e,s,l.replace(/\\/g,"/"));i.length>0&&(Bt(`Related articles not yet published (${i.length}):`),G([`/publish --related ${l}`,...i.map(a=>`/publish ${a}`)]))}catch{}},[e,s,de]),Xn=z(l=>{let i=Ye(l);if(!i||_e(i)&&!i.refreshToken)return null;let a={...kt(i.idToken)??{},...kt(i.accessToken)??{}};return a.email??a.preferred_username??"signed in"},[]),Kn=z((l,i)=>{if(i){let a=Q(e);a&&(a.defaultProfile=l,Ld(a,e))}c(l),ue(void 0,l),u({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${l}"${i?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),Xn(l)||(u({kind:"note",tone:"warn",text:`Profile "${l}" is not signed in \u2014 do you want to log in now? (press 1)`}),G(["/login"])),oe(a=>a+1)},[e,u,ue,Xn]),Kt=z((l,i,a)=>{tn(e,l,i,a.id,a.name),u({kind:"note",tone:"ok",text:`Switched to workspace "${a.name??a.id}" (agent restarted).`}),ue(),pa(e,l)||(u({kind:"note",tone:"ok",text:"Setup complete \u2014 you're ready to write docs."}),u({kind:"note",tone:"info",text:ft(pt(e)).join(`
247
+ `)}),se("write the docs for this repo"))},[e,u,ue]),Jt=z(async l=>{let i;try{i=await Tt(e,l)}catch{u({kind:"note",tone:"info",text:"Next: pick a workspace \u2014 run /workspace."}),G(["/workspace"]);return}if(i.workspaces.length===0){u({kind:"note",tone:"warn",text:"This project has no workspaces yet. Create one in Document360, then run /workspace."});return}let a=i.workspaces.filter(d=>(d.workspace_type??"").toLowerCase()!=="apidocumentation"),h=a.length>0?a:i.workspaces;if(h.length===1){let d=h[0],m=i.workspaces.length-h.length;u({kind:"note",tone:"ok",text:`Selected the "${d.name??d.id}" workspace${m>0?" (skipped the API-documentation workspace)":""}.`}),Kt(i.profile,i.projectId,d);return}u({kind:"note",tone:"info",text:`Next: pick the workspace your articles publish to (${h.length} available).`}),G(["/workspace"])},[e,u,Kt]),Ir=z((l,i)=>{no(e,l,i.id,i.name),u({kind:"note",tone:"ok",text:`Project set to "${i.name??i.id}" (agent restarted).`}),ue(),Jt(l)},[e,u,ue,Jt]),Jn=z(async l=>{let i;try{i=fe(e,l)}catch{return}if(!i.project.workspaceId){if(i.project.projectId){if(!i.project.projectName)try{let{projects:a}=await dt(e,i.name),h=a.find(d=>d.id===i.project.projectId);h?.name&&Qd(e,i.name,{projectName:h.name})}catch{}await Jt(i.name);return}u({kind:"note",tone:"info",text:"Next: pick the Document360 project."}),G(["/project"])}},[e,u,Jt]),Qt=z(()=>{let l=Re.current[0];if(!l)return;u({kind:"note",tone:"info",text:`\u25CF ${l.title} (${l.path})`});for(let a of l.notes)u({kind:"note",tone:"warn",text:`\u26A0 ${a}`});l.overwritesLocalChanges&&u({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let i=Ge(l.oldContent,l.newContent,je());u(i?{kind:"diff",added:i.added,removed:i.removed,lines:i.lines,hidden:i.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),u({kind:"note",tone:"info",text:`Write ${l.path}? (y/n \u2014 anything else cancels)`})},[u,je]),Nr=z(l=>{if(Re.current.length===0)return!1;let i=l.trim().toLowerCase();if(i==="y"||i==="yes"){let a=Re.current.shift();try{Wd({cwd:e,profileName:s},a),u({kind:"note",tone:"ok",text:`\u2713 Pulled ${a.path} (sync base advanced).`}),Re.current.length===0&&G(h=>h.length>0?h:["/sync"])}catch(h){u({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(i==="n"||i==="no"){let a=Re.current.shift();u({kind:"note",tone:"info",text:`Skipped ${a.path}.`})}else{let a=Re.current.length;return Re.current=[],u({kind:"note",tone:"info",text:`Pull cancelled (${a} article(s) left untouched).`}),!0}return Qt(),!0},[e,s,Qt,u]),_r=z(l=>{let i=Hn.current;if(!i)return!1;if(Hn.current=null,se(null),ze.current++,l.trim()!==i.repoName)return u({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:a,failed:h}=rr(e,i.targets);u({kind:"note",tone:h.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${a.length} item${a.length===1?"":"s"}. The repo is back to its original state.`});for(let d of h)u({kind:"note",tone:"error",text:` \u2717 ${d.path}: ${d.error}`});return u({kind:"note",tone:"info",text:`Clean slate \u2014 set up d360-writer again whenever you're ready:
248
+ ${ma}`}),G(["/init"]),Ft(!1),!0},[e,u]),Or=z(async l=>{let i=l.slice(1).trim().split(/\s+/),a=(i[0]??"").toLowerCase(),h=i.slice(1);switch(Ft(!0),a){case"help":u({kind:"note",tone:"info",text:un().join(`
249
+ `)});return;case"exit":case"quit":me.current?.close(),r();return;case"clear":ue(),Qe.current=[],Ft(!1),se(null),ze.current++,u({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let d;try{d=fe(e,s)}catch(m){u({kind:"note",tone:"error",text:m.message});return}u({kind:"note",tone:"info",text:`Profile "${d.name}" \u2192 ${d.connection.name} (${d.connection.apiUrl})${d.production?" \u26A0 PRODUCTION":""}`}),ee(!0);try{let m=await Cd(d.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},T=>u({kind:"note",tone:"info",text:T})),y=Pd(d.name,m);Td(y),await sn(y,d.name,T=>u({kind:"note",tone:"info",text:T})),u({kind:"note",tone:"ok",text:`\u2713 Logged in to "${d.name}" as ${on(y)}`}),await Jn(s),Vt.current&&(se(Vt.current),Vt.current=null,u({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(m){u({kind:"note",tone:"error",text:`Login failed: ${m.message}`})}finally{ee(!1),oe(m=>m+1)}return}case"allow-prod":{let d=!1;try{d=fe(e,s).production}catch{}if(!d){u({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}Ze.current=!0,ue(),u({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let d=Bo(h.join(" ")),m=ve.current.uuid;if(!m){u({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!d){u({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let y=ve.current.firstPrompt??"";da(y,e).then(T=>{T?(se(`/rename ${T}`),u({kind:"note",tone:"info",text:`Suggestion: "${T}" \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}Ud(m,d),et(d),Ut(`${ur} d360-writer \xB7 ${d}`),u({kind:"note",tone:"ok",text:`Session renamed to "${d}".`});return}case"profile":{let d=h[0],m=Q(e);if(!d){let y=Object.entries(m?.profiles??{});if(y.length===0){u({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let T=y.map(([re,V])=>({name:re,env:V.connection?.environment??"custom",prod:V.production===!0,who:Xn(re)})),E=s??m?.defaultProfile,I=Math.max(0,T.findIndex(re=>re.name===E));tt({cursor:I,current:I,rows:T});return}if(d==="add"){let y=Go(e,h[1],h[2]);if(y){u({kind:"note",tone:"error",text:y});return}u({kind:"note",tone:"ok",text:`\u2713 Profile "${h[1]}" created (environment: ${h[2]??h[1]}).`}),Kn(h[1],!1);return}if(!m?.profiles?.[d]){u({kind:"note",tone:"error",text:`Unknown profile "${d}". Create it: /profile add ${d} <environment>`});return}Kn(d,!0);return}case"doctor":await _t(h,{cwd:e});return;case"mcp":await hn(h);return;case"init":{if(Q(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 d=Hd().map(m=>({name:m,apiUrl:Bd(m).apiUrl}));nt({cursor:0,rows:d});return}case"resume":{let d=h.join(" ").trim(),m=Od(e).filter(E=>E.uuid!==ve.current.uuid);if(!d){if(!m.length){u({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}He({query:"",cursor:0,sessions:m});return}let y=_d(e,d);if(!y){u({kind:"note",tone:"error",text:`No session matches "${d}".`});return}ue(y.uuid),ve.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},et(y.name),mr(y.uuid),Qe.current=[];let T=cr(e,y.uuid);for(let E of T)u(E);u({kind:"note",tone:"ok",text:T.length?`Resumed "${y.name}" \u2014 restored ${T.length} message(s); continue where you left off.`:`Resumed "${y.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let d=h.join(" ").trim(),m;try{m=await Tt(e,s)}catch(T){u({kind:"note",tone:"error",text:`Could not list workspaces: ${T.message}`});return}if(!d){let T=m.workspaces.map(I=>({id:I.id,name:I.name??I.id,type:I.workspace_type}));if(T.length===0){u({kind:"note",tone:"info",text:"No workspaces in this project."});return}let E=Math.max(0,T.findIndex(I=>I.id===m.current));ot({cursor:E,current:E,rows:T,profile:m.profile,projectId:m.projectId,projectName:m.projectName,environment:m.environment});return}let y=to(m.workspaces,d);if(!y){u({kind:"note",tone:"error",text:`No workspace matches "${d}".`});return}Kt(m.profile,m.projectId,y);return}case"project":{let d=h.join(" ").trim(),m;try{m=await dt(e,s)}catch(T){u({kind:"note",tone:"error",text:`Could not list projects: ${T.message} (signed in? try /login)`});return}if(!d){let T=m.projects.map(I=>({id:I.id,name:I.name??I.id,sub:I.sub_domain}));if(T.length===0){u({kind:"note",tone:"info",text:"No projects found for this identity."});return}let E=T.findIndex(I=>I.id===m.current);rt({cursor:Math.max(0,E),current:E,rows:T,profile:m.profile,environment:m.environment});return}let y=Br(m.projects,d);if(!y){u({kind:"note",tone:"error",text:`No project matches "${d}". Available: ${m.projects.map(T=>T.name??T.id).join(", ")}`});return}Ir(m.profile,y);return}case"logout":{let d;try{d=fe(e,s).name}catch(y){u({kind:"note",tone:"error",text:y.message});return}let m=jd(d);Ad(e,d),ue(),oe(y=>y+1),u({kind:"note",tone:m?"ok":"info",text:m?`\u2713 Signed out of "${d}" and cleared its project/workspace selection.`:`Profile "${d}" was not signed in. Cleared any project/workspace selection.`}),u({kind:"note",tone:"info",text:"Run /login to sign in and pick a project."}),G(["/login"]);return}case"publish":{if(h[0]==="--related"&&h[1]){let m=await wn(e,s,h[1].replace(/\\/g,"/"));if(m.length===0){u({kind:"note",tone:"ok",text:"\u2713 All related articles are already published."});return}await Xt(m);return}if(h[0]&&h[0]!=="--all"){await zn(h[0]);return}let d=h[0]==="--all";u({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let m=await hr({cwd:e,profileName:s}),y=It(m.entries);if(y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let T=m.counts["unknown-base"]??0;T>0&&u({kind:"note",tone:"info",text:`(${T} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(d){await Xt(y.map(T=>T.path));return}st({cursor:0,rows:Ns(y)})}catch(m){u({kind:"note",tone:"error",text:`Could not compute sync status: ${m.message}`}),u({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let d=h.join(" ").trim();if(!d){let y=[];try{y=Object.keys(pa(e,fe(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}Fe({query:"",cursor:0,paths:y});return}let m=$d(d)?d:Wt(e,d);if(la(m)){try{u({kind:"preview",name:Lt(m),text:ga(ca(m,"utf8"))})}catch(y){u({kind:"note",tone:"error",text:`Could not read ${m}: ${y.message}`})}return}if(rp.test(d)){try{let y=fe(e,s),T={profile:y.name,connection:y.connection},E=y.project.projectId??Rd(T),I=await Dd(T,E,d);u({kind:"preview",name:I.title??d,text:I.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:`"${d}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let d=h[0]?.trim();if(!d){let E=$n(yr(e));Le({cursor:E,current:E});return}let{lines:m,changed:y,effective:T}=Nt(e,d);for(let E of m)u({kind:"note",tone:E.startsWith("\u26A0")?"warn":E.startsWith("\u2713")?"ok":"info",text:E});y&&(Rr(E=>E+1),me.current?.setModel(T));return}case"convert":{if(!Q(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:d,run:m}=Co(h),y=Yd(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 T=Po(y,d);if(T.length===0){u({kind:"note",tone:"error",text:`No tracked articles under "${d}". (${y.length} are tracked overall.)`});return}let E=gr(T),I=3,re=`/convert${d?` --scope ${d}`:""} --run`,V=Ln(e,"light");if(!m){let P=fa({files:To(e,T),op:"convert",model:V.model}),O=d?`Scope: ${d} (${T.length} of ${y.length} tracked)
249
250
  `:"",Z=`
250
- Model: ${V.model}${V.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;d({kind:"note",tone:"info",text:O+So(E,P,I).join(`
251
- `)+Z}),G([re]);return}Ue.current=Date.now(),Xe(0),_(!0);let $=new AbortController;we.current=$,X({verb:"Converting",total:E.length,done:0,active:[],rows:Ut(E),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Converting ${S.length} articles across ${E.length} partitions (\u2264${I} agents at once) on ${V.model}\u2026 (esc to stop)`});try{for await(let P of Ln({cwd:e,partitions:E,promptFor:Po,concurrency:I,profileName:s,allowProdWrites:Ze.current,model:V.model,signal:$.signal}))if(P.type==="partition_status")P.status==="running"?X(O=>O&&{...O,active:[...O.active,P.label],rows:Se(O.rows,P.label,"active"),lastAt:Date.now()}):X(O=>O&&{...O,active:O.active.filter(Z=>Z!==P.label),done:O.done+1,rows:Se(O.rows,P.label,P.status==="done"?"done":"failed"),lastAt:Date.now()});else if(P.type==="partition_event")X(O=>{if(!O)return O;let Z={...O,lastAt:Date.now()};return P.event.type==="tool"?Z.tools=O.tools+1:P.event.type==="text"&&(Z.chars=O.chars+P.event.delta.length),Z});else if(P.type==="run_done"){lt(U=>U+P.totalCostUsd),at(U=>U+P.results.reduce((Y,ae)=>Y+ae.outputTokens,0));let O=P.aborted?"Stopped. ":"",Z=t.kind==="api"?"api":"subscription";d({kind:"note",tone:P.aborted?"warn":P.ok?"ok":"warn",text:O+To(P.results,E,Z).join(`
252
- `)})}}catch(P){d({kind:"note",tone:"error",text:`Convert run failed: ${P.message}`})}finally{we.current=null,X(null),_(!1),oe(P=>P+1)}return}case"write":{if(!Q(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let u=Kd(e);if(u.length===0){d({kind:"note",tone:"error",text:"No docs plan found (.d360-writer/plan.json). Analyze the repo and propose a structure first \u2014 try: analyze this repo and propose a docs structure."});return}let{scope:m,path:y,run:S}=Do(h),{docsDir:E,targets:I,reason:re}=No(e,u,{scope:m,path:y});if(I.length===0){d({kind:"note",tone:"warn",text:re??"Nothing to write."});return}let V=kr(I),$=5,P=Wn(e,"standard");if(!S&&!!!y){let U=ha({files:jo(e,u,I),op:"write",model:P.model}),Y=m?`Scope: ${m} (${I.length} pending)
251
+ Model: ${V.model}${V.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;u({kind:"note",tone:"info",text:O+Ro(E,P,I).join(`
252
+ `)+Z}),G([re]);return}Ue.current=Date.now(),Xe(0),_(!0);let $=new AbortController;we.current=$,X({verb:"Converting",total:E.length,done:0,active:[],rows:Mt(E),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Converting ${T.length} articles across ${E.length} partitions (\u2264${I} agents at once) on ${V.model}\u2026 (esc to stop)`});try{for await(let P of Mn({cwd:e,partitions:E,promptFor:So,concurrency:I,profileName:s,allowProdWrites:Ze.current,model:V.model,signal:$.signal}))if(P.type==="partition_status")P.status==="running"?X(O=>O&&{...O,active:[...O.active,P.label],rows:Te(O.rows,P.label,"active"),lastAt:Date.now()}):X(O=>O&&{...O,active:O.active.filter(Z=>Z!==P.label),done:O.done+1,rows:Te(O.rows,P.label,P.status==="done"?"done":"failed"),lastAt:Date.now()});else if(P.type==="partition_event")X(O=>{if(!O)return O;let Z={...O,lastAt:Date.now()};return P.event.type==="tool"?Z.tools=O.tools+1:P.event.type==="text"&&(Z.chars=O.chars+P.event.delta.length),Z});else if(P.type==="run_done"){lt(U=>U+P.totalCostUsd),at(U=>U+P.results.reduce((Y,ae)=>Y+ae.outputTokens,0));let O=P.aborted?"Stopped. ":"",Z=t.kind==="api"?"api":"subscription";u({kind:"note",tone:P.aborted?"warn":P.ok?"ok":"warn",text:O+Do(P.results,E,Z).join(`
253
+ `)})}}catch(P){u({kind:"note",tone:"error",text:`Convert run failed: ${P.message}`})}finally{we.current=null,X(null),_(!1),oe(P=>P+1)}return}case"write":{if(!Q(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let d=zd(e);if(d.length===0){u({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:m,path:y,run:T}=Ao(h),{docsDir:E,targets:I,reason:re}=Oo(e,d,{scope:m,path:y});if(I.length===0){u({kind:"note",tone:"warn",text:re??"Nothing to write."});return}let V=gr(I),$=5,P=Ln(e,"standard");if(!T&&!!!y){let U=fa({files:Eo(e,d,I),op:"write",model:P.model}),Y=m?`Scope: ${m} (${I.length} pending)
253
254
  `:"",ae=`
254
- Model: ${P.model}${P.forced?" (forced)":" \u2014 authoring; /model to override"}`;d({kind:"note",tone:"info",text:Y+Eo(V,U,$).join(`
255
- `)+ae}),G([`/write${m?` --scope ${m}`:" --all"} --run`]);return}Ue.current=Date.now(),Xe(0),_(!0);let Z=new AbortController;we.current=Z,X({verb:"Writing",total:V.length,done:0,active:[],rows:Ut(V),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Writing ${I.length} article${I.length===1?"":"s"} across ${V.length} partition${V.length===1?"":"s"} (\u2264${$} agents at once) on ${P.model}${P.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let U of Ln({cwd:e,partitions:V,promptFor:Y=>Ao(Y,E),concurrency:$,profileName:s,allowProdWrites:Ze.current,model:P.model,signal:Z.signal}))if(U.type==="partition_status")U.status==="running"?X(Y=>Y&&{...Y,active:[...Y.active,U.label],rows:Se(Y.rows,U.label,"active"),lastAt:Date.now()}):X(Y=>Y&&{...Y,active:Y.active.filter(ae=>ae!==U.label),done:Y.done+1,rows:Se(Y.rows,U.label,U.status==="done"?"done":"failed"),lastAt:Date.now()});else if(U.type==="partition_event")X(Y=>{if(!Y)return Y;let ae={...Y,lastAt:Date.now()};return U.event.type==="tool"?ae.tools=Y.tools+1:U.event.type==="text"&&(ae.chars=Y.chars+U.event.delta.length),ae});else if(U.type==="run_done"){lt(eo=>eo+U.totalCostUsd),at(eo=>eo+U.results.reduce((_a,Oa)=>_a+Oa.outputTokens,0));let Y=U.aborted?"Stopped. ":"",ae=t.kind==="api"?"api":"subscription";d({kind:"note",tone:U.aborted?"warn":U.ok?"ok":"warn",text:Y+Io(U.results,V,ae).join(`
256
- `)}),!U.aborted&&U.ok&&G(["/publish --all","/screenshot --all"])}}catch(U){d({kind:"note",tone:"error",text:`Write run failed: ${U.message}`})}finally{we.current=null,X(null),_(!1),oe(U=>U+1)}return}case"draft":{if(!Q(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{features:u,looseDocs:m}=_o(e);if(u.length===0&&m===0){d({kind:"note",tone:"info",text:Uo});return}let y=h.join(" ").trim();if(!y&&u.length>1){d({kind:"note",tone:"info",text:`Multiple features in .d360-writer/input/ \u2014 draft which one?
257
- ${u.map(I=>` \u2022 ${I.name} (${I.docCount} doc${I.docCount===1?"":"s"})`).join(`
258
- `)}`}),G(u.map(I=>`/draft ${I.name}`));return}let S="",E;if(y){let I=Oo(u,y);I?S=I:E=y}else S=u[0]?.name??"";await de(wn(S,E),{display:y?`/draft ${y}`:"/draft",quietUser:!0});return}case"sync":{let u=(h[0]??"status").toLowerCase();try{if(u==="status"){d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let m=await yr({cwd:e,profileName:s});d({kind:"note",tone:"info",text:gn(m).join(`
259
- `)});return}if(u==="pull"){let m=h[1];if(!m){d({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let y;if(m==="--all"){if(d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),y=(await yr({cwd:e,profileName:s})).entries.filter(I=>I.status==="remote-ahead"&&I.path).map(I=>I.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=[m.replace(/\\/g,"/")];let S=[];for(let E of y)S.push(await Hd({cwd:e,profileName:s,relPath:E}));Re.current=S,Jt();return}d({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${u} \u2014 use /sync or /sync pull <path>|--all.`})}catch(m){d({kind:"note",tone:"error",text:`Sync failed: ${m.message}`})}return}case"scope":{if(!Q(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let u=Bd(e);if(u.length===0){d({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}it({cursor:0,rows:u.map(m=>({...m,checked:m.recommended}))});return}case"audit":case"capture-setup":{let m=await(a==="audit"?mn:_t)(h,void 0);if(m.kind==="forward-to-agent"&&m.prompt&&await de(m.prompt,{display:m.display,quietUser:!0}),a==="capture-setup"){let y=Zo(e);y&&d({kind:"note",tone:"info",text:y.join(`
260
- `)})}return}case"devhints":{if(!Q(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}br(Jd(e)),br(Qd(e)),br(Zd(e)),gr(ga(e))||da(ga(e),Vo()),da(op(e),xn());let u=Et(e);$n(e,new Date().toISOString()),d({kind:"note",tone:"info",text:zo(u).join(`
261
- `)});return}case"inbox":{if(!Q(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}if(er(e)===0){d({kind:"note",tone:"info",text:"Inbox empty \u2014 no open messages from the builder. (They arrive via the builder's /devhints hand-off.)"});return}await de(tr(),{display:"/inbox",quietUser:!0});return}case"document":{if(!Q(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{publish:u,what:m}=Lo(h);if(!m&&Mo(e)===0){d({kind:"note",tone:"info",text:Fo});return}let y=`/document${m?` ${m}`:""}${u?" --publish":""}`;await de(Wo(m,u),{display:y,quietUser:!0});return}case"catch-up":{if(!Q(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{publish:u}=nr(h),m=ht(mt(e));d({kind:"note",tone:"info",text:m?`Catching up on ${m} (+ checking Document360 for drift)\u2026`:"No local queue \u2014 checking for code changes and drift\u2026"}),await de(or({publish:u}),{display:`/catch-up${u?" --publish":""}`,quietUser:!0});return}case"update":{d({kind:"note",tone:"info",text:`Updating ${Ve}\u2026`}),(await Tn()).ok?(d({kind:"note",tone:"ok",text:"\u2713 Updated \u2014 /exit and relaunch to apply (press 1)."}),G(m=>m.includes("/exit")?m:[...m,"/exit"])):d({kind:"note",tone:"warn",text:`Update failed \u2014 run manually: ${Sn}`});return}case"screenshot":{let u=Ko(h);if(u.mode==="list"){if(!Q(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}d({kind:"note",tone:"info",text:Qo(Jo(e,u.scope),u.scope).join(`
262
- `)});return}if(u.mode==="single"){let $=await Pn(h);$.kind==="forward-to-agent"&&$.prompt&&await de($.prompt,{display:$.display,quietUser:!0});return}if(!Q(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let m=u.mode==="scope"?u.scope:void 0,y=zd(e,{scope:m}).map($=>$.id);if(y.length===0){d({kind:"note",tone:"info",text:m?`No screenshot placeholders under ${m}.`:"No screenshot placeholders found in the docs."});return}let S=6,E=Yd(y,3),I=Wn(e,"standard");Ue.current=Date.now(),Xe(0),_(!0);let re=new AbortController;we.current=re,X({verb:"Authoring",unit:"spec",total:E.length,done:0,active:[],rows:Ut(E),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 ${E.length} partition${E.length===1?"":"s"} (\u2264${S} agents at once) on ${I.model}${I.forced?" (forced)":""}\u2026 (esc to stop)`});let V=!1;try{for await(let $ of Ln({cwd:e,partitions:E,promptFor:Si,concurrency:S,profileName:s,allowProdWrites:Ze.current,model:I.model,signal:re.signal}))if($.type==="partition_status")$.status==="running"?X(P=>P&&{...P,active:[...P.active,$.label],rows:Se(P.rows,$.label,"active"),lastAt:Date.now()}):X(P=>P&&{...P,active:P.active.filter(O=>O!==$.label),done:P.done+1,rows:Se(P.rows,$.label,$.status==="done"?"done":"failed"),lastAt:Date.now()});else if($.type==="partition_event")X(P=>{if(!P)return P;let O={...P,lastAt:Date.now()};return $.event.type==="tool"?O.tools=P.tools+1:$.event.type==="text"&&(O.chars=P.chars+$.event.delta.length),O});else if($.type==="run_done"){lt(U=>U+$.totalCostUsd),at(U=>U+$.results.reduce((Y,ae)=>Y+ae.outputTokens,0));let P=$.results.filter(U=>U.ok).length,O=$.aborted?"Stopped. ":"";V=!$.aborted&&u.setup;let Z=$.aborted?"":V?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";d({kind:"note",tone:$.aborted?"warn":$.ok?"ok":"warn",text:`${O}Authored specs \u2014 ${P}/${$.results.length} batches ok.${Z}`})}}catch($){d({kind:"note",tone:"error",text:`Spec authoring failed: ${$.message}`})}finally{we.current=null,X(null),_(!1),oe($=>$+1)}if(V){let $=await _t();$.kind==="forward-to-agent"&&$.prompt&&await de($.prompt,{display:$.display,quietUser:!0});let P=Zo(e);P&&d({kind:"note",tone:"info",text:P.join(`
263
- `)})}return}case"reset":{let u=sr(e);if(u.length===0){d({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}Bn.current={repoName:Mt(e),targets:u},d({kind:"note",tone:"warn",text:ar(e,u).join(`
264
- `)}),se(Mt(e));return}default:d({kind:"note",tone:"error",text:`Unknown command: /${a} \u2014 type /help.`})}},[e,r,s,Jt,d,ue,de,zt,Xn,Qn]),Qt=z(l=>{let i=(l??D).trim();if(w(""),Ft(null),Ke(0),i.startsWith("/")&&(se(null),ze.current++,Ht(null),G(h=>ea(h,i))),!i||Lr(i)||Mr(i))return;Hn.current.push(i);let a=ra(i,Pr.current);i.startsWith("/")?(d({kind:"user",text:i}),Wr(a)):de(a,{display:i})},[D,Lr,Mr,Wr,de]),Fr=z(l=>{if(l.length>1){if(l.includes("\x1B"))return;let i=ta(l);if(na(i)){let a=oa(++ja.current,i);Pr.current.set(a,i),v(a)}else v(i);return}v(l)},[v]),ct=Math.max(10,Be-6),qr=hr(()=>Un(b.text,ct),[b.text,ct]),Zn=z(l=>k(i=>({...i,pos:Math.max(0,Math.min(i.text.length,i.pos+l))})),[]),Zt=z(l=>k(i=>({...i,pos:ia(Un(i.text,ct),i.pos,l)})),[ct]),xt=z(l=>k(i=>({...i,pos:aa(Un(i.text,ct),i.pos,l)})),[ct]),Ia=["\x1B[H","\x1B[1~","\x1BOH"],Na=["\x1B[F","\x1B[4~","\x1BOF"],Hr=z((l,i)=>i.leftArrow?(Zn(-1),!0):i.rightArrow?(Zn(1),!0):l&&Ia.includes(l)?(xt("start"),!0):l&&Na.includes(l)?(xt("end"),!0):i.ctrl&&l==="a"?(xt("start"),!0):i.ctrl&&l==="e"?(xt("end"),!0):!1,[Zn,xt]);return $d((l,i)=>{if(i.ctrl&&l==="c"){me.current?.close(),r();return}if(!F){if(N){if(i.escape){if(we.current){we.current.signal.aborted||(d({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),we.current.abort());return}kt.current||(kt.current=!0,d({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),me.current?.interrupt());return}if(i.return){let a=D.trim();if(!a)return;qe.current.push(a),Gn([...qe.current]),w("");return}if(Hr(l,i))return;if(i.upArrow){Zt(-1);return}if(i.downArrow){Zt(1);return}if(i.backspace||i.delete){C();return}l&&!i.ctrl&&!i.meta&&Fr(l);return}if(Me){if(i.upArrow){Le(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){Le(a=>a&&{...a,cursor:Math.min(xe.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=xe.length){Le(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=xe[Me.cursor];Le(null);let{lines:h,changed:u,effective:m}=At(e,a.value??"default");for(let y of h)d({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});u&&(Er(y=>y+1),me.current?.setModel(m));return}if(l==="s"){let a=xe[Me.cursor];Le(null),me.current?.setModel(a.value??void 0),d({kind:"note",tone:"ok",text:`\u2713 Using ${a.label} for this session only (your saved default is unchanged).`});return}if(i.escape){Le(null);return}return}if(Ce){if(i.upArrow){tt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){tt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=Ce.rows.length){tt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return||l==="s"){let a=Ce.rows[Ce.cursor];tt(null),Jn(a.name,i.return===!0);return}if(i.escape){tt(null);return}return}if(De){if(i.upArrow){nt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){nt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=De.rows.length){nt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=De.rows[De.cursor].name;if(nt(null),!ms(e,a).created){d({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}d({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${a}").`});let u=(()=>{try{let m=Ye(a);return m&&!(_e(m)&&!m.refreshToken)?m:null}catch{return null}})();u?(d({kind:"note",tone:"info",text:`Already signed in to ${a}.`}),(async()=>(await rn(u,a,m=>d({kind:"note",tone:"info",text:m})),await Qn(a)))()):(d({kind:"note",tone:"info",text:`Next: sign in to Document360 (${a}).`}),G(["/login"])),oe(m=>m+1);return}if(i.escape){nt(null);return}return}if(ie){if(i.upArrow){ot(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){ot(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=ie.rows.length){ot(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=ie.rows[ie.cursor],{profile:h,projectId:u}=ie;ot(null),Xt(h,u,a);return}if(i.escape){ot(null);return}return}if(he){if(i.upArrow){rt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){rt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=he.rows.length){rt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=he.rows[he.cursor],{profile:h}=he;rt(null),Ur(h,a);return}if(i.escape){rt(null);return}return}if(ce){if(i.upArrow){it(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){it(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l===" "){it(a=>a&&{...a,rows:a.rows.map((h,u)=>u===a.cursor?{...h,checked:!h.checked}:h)});return}if(i.return){let a=ce.rows.filter(h=>h.checked).map(h=>h.path);if(it(null),a.length===0){d({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}ko(e,a),d({kind:"note",tone:"ok",text:`\u2713 Scoped to ${a.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of a)d({kind:"note",tone:"info",text:` ${h}`});d({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(i.escape){it(null);return}return}if(We){if(i.upArrow){st(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){st(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=We.rows.length){st(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=We.rows[We.cursor];st(null),a.kind==="article"?Xn(a.paths[0]):zt(a.paths);return}if(i.escape){st(null);return}return}if(ge){if(i.escape){Fe(null);return}if(i.upArrow){Fe(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){Fe(a=>a&&{...a,cursor:Math.min(Math.max(0,Vt.length-1),a.cursor+1)});return}if(i.return){let a=Vt[ge.cursor];if(a){Fe(null);try{d({kind:"preview",name:Mt(a),text:ka(ua(Lt(e,a),"utf8"))})}catch(h){d({kind:"note",tone:"error",text:`Could not read ${a}: ${h.message}`})}}return}if(i.backspace||i.delete){Fe(a=>a&&{...a,query:a.query.slice(0,-1),cursor:0});return}if(l&&!i.ctrl&&!i.meta&&l.length===1){Fe(a=>a&&{...a,query:a.query+l,cursor:0});return}return}if(ye){if(i.escape){He(null);return}if(i.upArrow){He(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){He(a=>a&&{...a,cursor:Math.min(Math.max(0,Yt.length-1),a.cursor+1)});return}if(i.return){let a=Yt[ye.cursor];if(a){He(null),ue(a.uuid),ve.current={uuid:a.uuid,firstPrompt:a.firstPrompt,titleFired:!0},et(a.name),wr(a.uuid),Qe.current=[];let h=dr(e,a.uuid);for(let u of h)d(u);d({kind:"note",tone:"ok",text:h.length?`Resumed "${a.name}" \u2014 restored ${h.length} message(s); continue where you left off.`:`Resumed "${a.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(i.backspace||i.delete){He(a=>a&&{...a,query:a.query.slice(0,-1),cursor:0});return}if(l&&!i.ctrl&&!i.meta&&l.length===1){He(a=>a&&{...a,query:a.query+l,cursor:0});return}return}if(i.tab&&!D&&ke){w(ke),se(null),ze.current++;return}if(!D&&wt.length>0&&l&&/^[1-9]$/.test(l)){let a=wt[Number(l)-1];if(a){w(a);return}}if(!Hr(l,i)){if(_r){if(i.upArrow){Ke(a=>Math.max(0,a-1));return}if(i.downArrow){Ke(a=>Math.min(bt.length-1,a+1));return}if(i.tab){w("/"+(bt[yt]?.name??"")+" "),Ke(0);return}if(i.return){let a=bt[yt];if(a){let h=D.trim().slice(1).split(/\s+/).slice(1).join(" ");if(is(a.usage)&&!h){w("/"+a.name+" "),Ke(0);return}Qt("/"+a.name+(h?" "+h:""));return}}}else{if(i.upArrow){if(D!==""&&Je===null){Zt(-1);return}let a=Hn.current;if(!a.length)return;let h=Je===null?a.length-1:Math.max(0,Je-1);Ft(h),w(a[h]??"");return}if(i.downArrow){if(D!==""&&Je===null){Zt(1);return}let a=Hn.current;if(Je===null)return;let h=Je+1;h>=a.length?(Ft(null),w("")):(Ft(h),w(a[h]??""));return}}if(i.return){Qt();return}if(i.backspace||i.delete){C();return}if(i.escape){w(""),Ke(0),se(null),G([]),Ht(null);return}l&&!i.ctrl&&!i.meta&&Fr(l)}}}),Oe(()=>{if(N||F)return;let l=qe.current.shift();l!==void 0&&(Gn([...qe.current]),Qt(l))},[N,F,Qt]),W(K,{flexDirection:"column",width:Be,children:[zn!==null&&W(K,{marginTop:1,flexDirection:"column",children:[zn.truncated&&R(T,{dimColor:!0,children:"\u2026"}),R(T,{children:zn.text})]}),N&&(Dr?R(mp,{p:Dr}):R(pp,{startTime:Ue.current,chars:Da})),R(K,{borderStyle:"round",borderColor:Nr.prod?"yellow":H,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:D?qr.map((l,i)=>{let a=b.text.slice(l.start,l.end),h=i===Mn(qr,b.pos),u=Math.min(b.pos,l.end)-l.start;return W(T,{children:[R(T,{color:H,children:i===0?"> ":" "}),h?W(xr,{children:[a.slice(0,u),R(Fn,{ch:a[u]??" "}),a.slice(u+1)]}):a||" "]},`${i}-${l.start}`)}):W(T,{children:[R(T,{color:H,children:"> "}),ke&&!N?W(xr,{children:[R(Fn,{ch:ke[0],dim:!0}),R(T,{color:"gray",children:ke.slice(1)}),R(T,{dimColor:!0,children:" (tab)"})]}):Yn.isSetup||!Ra?W(xr,{children:[R(Fn,{ch:Yn.text[0],dim:!0}),R(T,{color:"gray",children:Yn.text.slice(1)})]}):R(Fn,{ch:" "})]})}),Rr.length>0&&R(K,{flexDirection:"column",paddingX:1,children:Rr.map((l,i)=>R(T,{color:"gray",children:`\u29D7 queued: ${l}`},`${i}-${l.slice(0,24)}`))}),Me?W(K,{flexDirection:"column",paddingX:1,children:[R(T,{color:H,bold:!0,children:"Select model"}),R(T,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),xe.map((l,i)=>W(T,{color:i===Me.cursor?H:void 0,children:[i===Me.cursor?"\u276F ":" ",`${i+1}. ${l.label}${i===Me.current?" \u2714":""}`.padEnd(16),R(T,{color:"gray",children:l.desc})]},l.label)),R(T,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):Ce?W(K,{flexDirection:"column",paddingX:1,children:[R(T,{color:H,bold:!0,children:"Switch connection profile"}),Ce.rows.map((l,i)=>W(T,{color:i===Ce.cursor?H:void 0,children:[i===Ce.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===Ce.current?" \u2714":""}`.padEnd(20),R(T,{color:"gray",children:`${l.env} \xB7 ${l.who??"not signed in"}`}),l.prod?R(T,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},l.name)),R(T,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):De?W(K,{flexDirection:"column",paddingX:1,children:[R(T,{color:H,bold:!0,children:"Pick your Document360 environment"}),De.rows.map((l,i)=>W(T,{color:i===De.cursor?H:void 0,children:[i===De.cursor?"\u276F ":" ",`${i+1}. ${l.name}`.padEnd(16),R(T,{color:"gray",children:l.apiUrl})]},l.name)),R(T,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):ie?W(K,{flexDirection:"column",paddingX:1,children:[R(T,{color:H,bold:!0,children:"Switch workspace"}),R(T,{dimColor:!0,children:`environment ${ie.environment} \xB7 project ${ie.projectName??`${ie.projectId.slice(0,8)}\u2026`}`}),ie.rows.map((l,i)=>W(T,{color:i===ie.cursor?H:void 0,children:[i===ie.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===ie.current?" \u2714":""}`.padEnd(30),R(T,{color:"gray",children:l.type??""})]},l.id)),R(T,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):he?W(K,{flexDirection:"column",paddingX:1,children:[R(T,{color:H,bold:!0,children:"Choose project"}),R(T,{dimColor:!0,children:`environment ${he.environment}`}),he.rows.map((l,i)=>W(T,{color:i===he.cursor?H:void 0,children:[i===he.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===he.current?" \u2714":""}`.padEnd(30),R(T,{color:"gray",children:l.sub??""})]},l.id)),R(T,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):ce?(()=>{let i=Math.min(Math.max(0,ce.cursor-Math.floor(7)),Math.max(0,ce.rows.length-14)),a=ce.rows.slice(i,i+14),h=ce.rows.filter(u=>u.checked).length;return W(K,{flexDirection:"column",paddingX:1,children:[R(T,{color:H,bold:!0,children:`Which folders back the user docs? (${h} selected of ${ce.rows.length})`}),i>0?R(T,{dimColor:!0,children:` \u2191 ${i} more`}):null,a.map((u,m)=>{let y=i+m;return W(T,{color:y===ce.cursor?H:void 0,children:[y===ce.cursor?"\u276F ":" ",u.checked?"\u25C9 ":"\u25CB ",u.path.padEnd(Math.min(48,Be-34)),R(T,{color:"gray",children:hn(u)})]},u.path)}),i+14<ce.rows.length?R(T,{dimColor:!0,children:` \u2193 ${ce.rows.length-i-14} more`}):null,R(T,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():We?W(K,{flexDirection:"column",paddingX:1,children:[R(T,{color:H,bold:!0,children:"Publish to Document360 \u2014 pick a category (whole) or an article:"}),We.rows.map((l,i)=>{let a=i===We.cursor,h=l.indent?" ":" ",u=`${i+1}.`.padEnd(3);return W(T,{color:a?H:void 0,bold:l.kind==="category"&&!a,children:[a?"\u276F ":" ",`${h}${u} ${l.label}`.padEnd(Math.min(56,Be-24)),l.note?R(T,{color:"gray",children:l.note}):null]},`${l.kind}:${l.paths.join(",")}`)}),R(T,{dimColor:!0,children:"\u2191\u2193 move \xB7 1-9 jump \xB7 enter publish (draft) \xB7 esc cancel"})]}):ge?W(K,{flexDirection:"column",paddingX:1,children:[R(T,{color:H,bold:!0,children:`Preview article${ge.query?` \u2014 filter: ${ge.query}`:" (type to filter)"}`}),Vt.length===0?R(T,{color:"gray",children:"no articles match"}):Vt.map((l,i)=>W(T,{color:i===ge.cursor?H:void 0,children:[i===ge.cursor?"\u276F ":" ",l]},l)),R(T,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ye?W(K,{flexDirection:"column",paddingX:1,children:[R(T,{color:H,bold:!0,children:`Resume session${ye.query?` \u2014 filter: ${ye.query}`:" (type to filter)"}`}),Yt.length===0?R(T,{color:"gray",children:"no sessions match"}):Yt.map((l,i)=>W(T,{color:i===ye.cursor?H:void 0,children:[i===ye.cursor?"\u276F ":" ",l.name.slice(0,28).padEnd(30),R(T,{color:i===ye.cursor?H:"gray",children:l.firstPrompt.slice(0,Math.max(10,Be-40))})]},l.uuid)),R(T,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):_r?R(K,{flexDirection:"column",children:bt.map((l,i)=>W(T,{color:i===yt?H:void 0,children:[i===yt?"\u276F ":" ",l.usage.padEnd(22)," ",R(T,{color:i===yt?H:"gray",children:l.name==="model"&&Vn?`${l.desc} (currently ${Vn})`:l.desc})]},l.name))}):!D&&wt.length>0?W(K,{flexDirection:"column",paddingX:1,children:[Tr?R(T,{color:H,children:Tr}):null,wt.map((l,i)=>W(T,{children:[R(T,{color:H,children:i+1})," ",l.slice(0,Math.max(20,Be-5))]},l)),R(T,{dimColor:!0,children:`press 1-${wt.length} to fill the command \xB7 esc dismiss`})]}):R(K,{paddingX:1,children:W(T,{color:"gray",children:[Nr.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${Vn??"model n/a"}${t.kind==="api"?Ar>0?` \xB7 ${le(Ar)}`:"":jr>0?` \xB7 ${Ie(jr)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}L();import{jsx as wp}from"react/jsx-runtime";async function Pa(e=process.cwd(),t="auto",n,o="0.0.0"){let r=gp(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 ${A("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${A("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=hp(wp(Ca,{cwd:e,auth:r,profileName:n,version:o}));await s(),process.stdout.write(`
265
- `),process.exit(0)}var xp=kp(import.meta.url),Sa=xp("../package.json"),Te=new yp;function vr(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}Te.command("login").description("Sign in to Document360 (browser OAuth; project chosen during login)").option("--profile <name>","Connection profile (defaults to the repo's defaultProfile)").option("--env <name>",!1).option("--manual","No local listener \u2014 paste the redirect URL instead (SSH/locked-down setups)").action(async e=>{vr(e),await on({profile:e.profile,manual:e.manual})});Te.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{vr(e),await ts({profile:e.profile})});Te.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{vr(e),await es({profile:e.profile})});var Cr=Te.command("profile").description("Manage connection profiles for the current repo");Cr.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>sn(process.cwd()));Cr.command("use <name>").description("Set the default profile for this repo").action(e=>an(process.cwd(),e));Cr.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>ln(process.cwd(),e));var Ta=Te.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");Ta.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>ut(process.cwd(),e.profile));Ta.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await Vr(process.cwd(),e,t.profile)});Te.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>os());Te.command("doctor").description("Health-check: node, Claude auth, Document360 login, profile/workspace, category map, API reachability").action(async()=>{let{renderDoctorChecks:e,runDoctorChecks:t}=await Promise.resolve().then(()=>(Cn(),yi)),n=await t(process.cwd());for(let o of e(n))console.log(o);process.exitCode=n.some(o=>o.level==="fail")?1:0});Te.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(Sa.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(bp.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 rs(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 Vi(e.cwd,e.auth,e.profile):await Pa(e.cwd,e.auth,e.profile,Sa.version)});Te.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
255
+ Model: ${P.model}${P.forced?" (forced)":" \u2014 authoring; /model to override"}`;u({kind:"note",tone:"info",text:Y+No(V,U,$).join(`
256
+ `)+ae}),G([`/write${m?` --scope ${m}`:" --all"} --run`]);return}Ue.current=Date.now(),Xe(0),_(!0);let Z=new AbortController;we.current=Z,X({verb:"Writing",total:V.length,done:0,active:[],rows:Mt(V),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Writing ${I.length} article${I.length===1?"":"s"} across ${V.length} partition${V.length===1?"":"s"} (\u2264${$} agents at once) on ${P.model}${P.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let U of Mn({cwd:e,partitions:V,promptFor:Y=>Io(Y,E),concurrency:$,profileName:s,allowProdWrites:Ze.current,model:P.model,signal:Z.signal}))if(U.type==="partition_status")U.status==="running"?X(Y=>Y&&{...Y,active:[...Y.active,U.label],rows:Te(Y.rows,U.label,"active"),lastAt:Date.now()}):X(Y=>Y&&{...Y,active:Y.active.filter(ae=>ae!==U.label),done:Y.done+1,rows:Te(Y.rows,U.label,U.status==="done"?"done":"failed"),lastAt:Date.now()});else if(U.type==="partition_event")X(Y=>{if(!Y)return Y;let ae={...Y,lastAt:Date.now()};return U.event.type==="tool"?ae.tools=Y.tools+1:U.event.type==="text"&&(ae.chars=Y.chars+U.event.delta.length),ae});else if(U.type==="run_done"){lt(Zn=>Zn+U.totalCostUsd),at(Zn=>Zn+U.results.reduce((Ea,Ia)=>Ea+Ia.outputTokens,0));let Y=U.aborted?"Stopped. ":"",ae=t.kind==="api"?"api":"subscription";u({kind:"note",tone:U.aborted?"warn":U.ok?"ok":"warn",text:Y+_o(U.results,V,ae).join(`
257
+ `)}),!U.aborted&&U.ok&&G(["/publish --all","/screenshot --all"])}}catch(U){u({kind:"note",tone:"error",text:`Write run failed: ${U.message}`})}finally{we.current=null,X(null),_(!1),oe(U=>U+1)}return}case"draft":{if(!Q(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{features:d,looseDocs:m}=Uo(e);if(d.length===0&&m===0){u({kind:"note",tone:"info",text:Lo});return}let y=h.join(" ").trim();if(!y&&d.length>1){u({kind:"note",tone:"info",text:`Multiple features in .d360-writer/input/ \u2014 draft which one?
258
+ ${d.map(I=>` \u2022 ${I.name} (${I.docCount} doc${I.docCount===1?"":"s"})`).join(`
259
+ `)}`}),G(d.map(I=>`/draft ${I.name}`));return}let T="",E;if(y){let I=Mo(d,y);I?T=I:E=y}else T=d[0]?.name??"";await de(xn(T,E),{display:y?`/draft ${y}`:"/draft",quietUser:!0});return}case"sync":{let d=(h[0]??"status").toLowerCase();try{if(d==="status"){u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let m=await hr({cwd:e,profileName:s});u({kind:"note",tone:"info",text:bn(m).join(`
260
+ `)});return}if(d==="pull"){let m=h[1];if(!m){u({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let y;if(m==="--all"){if(u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),y=(await hr({cwd:e,profileName:s})).entries.filter(I=>I.status==="remote-ahead"&&I.path).map(I=>I.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=[m.replace(/\\/g,"/")];let T=[];for(let E of y)T.push(await Fd({cwd:e,profileName:s,relPath:E}));Re.current=T,Qt();return}u({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${d} \u2014 use /sync or /sync pull <path>|--all.`})}catch(m){u({kind:"note",tone:"error",text:`Sync failed: ${m.message}`})}return}case"scope":{if(!Q(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let d=qd(e);if(d.length===0){u({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}it({cursor:0,rows:d.map(m=>({...m,checked:m.recommended}))});return}case"audit":case"capture-setup":{let m=await(a==="audit"?yn:Ot)(h,void 0);if(m.kind==="forward-to-agent"&&m.prompt&&await de(m.prompt,{display:m.display,quietUser:!0}),a==="capture-setup"){let y=Jo(e);y&&u({kind:"note",tone:"info",text:y.join(`
261
+ `)})}return}case"devhints":{if(!Q(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}u({kind:"note",tone:"info",text:ft(pt(e)).join(`
262
+ `)});return}case"inbox":{if(!Q(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}if(Qo(e)===0){u({kind:"note",tone:"info",text:"Inbox empty \u2014 no open messages from the builder. (They arrive via the builder's /devhints hand-off.)"});return}await de(Zo(),{display:"/inbox",quietUser:!0});return}case"document":{if(!Q(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{publish:d,what:m}=Fo(h);if(!m&&Wo(e)===0){u({kind:"note",tone:"info",text:Ho});return}let y=`/document${m?` ${m}`:""}${d?" --publish":""}`;await de(qo(m,d),{display:y,quietUser:!0});return}case"catch-up":{if(!Q(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{publish:d}=er(h),m=yt(wt(e));u({kind:"note",tone:"info",text:m?`Catching up on ${m} (+ checking Document360 for drift)\u2026`:"No local queue \u2014 checking for code changes and drift\u2026"}),await de(tr({publish:d}),{display:`/catch-up${d?" --publish":""}`,quietUser:!0});return}case"update":{u({kind:"note",tone:"info",text:`Updating ${Ve}\u2026`}),(await Tn()).ok?(u({kind:"note",tone:"ok",text:"\u2713 Updated \u2014 /exit and relaunch to apply (press 1)."}),G(m=>m.includes("/exit")?m:[...m,"/exit"])):u({kind:"note",tone:"warn",text:`Update failed \u2014 run manually: ${Pn}`});return}case"screenshot":{let d=zo(h);if(d.mode==="list"){if(!Q(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}u({kind:"note",tone:"info",text:Ko(Xo(e,d.scope),d.scope).join(`
263
+ `)});return}if(d.mode==="single"){let $=await Cn(h);$.kind==="forward-to-agent"&&$.prompt&&await de($.prompt,{display:$.display,quietUser:!0});return}if(!Q(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let m=d.mode==="scope"?d.scope:void 0,y=Vd(e,{scope:m}).map($=>$.id);if(y.length===0){u({kind:"note",tone:"info",text:m?`No screenshot placeholders under ${m}.`:"No screenshot placeholders found in the docs."});return}let T=6,E=Gd(y,3),I=Ln(e,"standard");Ue.current=Date.now(),Xe(0),_(!0);let re=new AbortController;we.current=re,X({verb:"Authoring",unit:"spec",total:E.length,done:0,active:[],rows:Mt(E),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 ${E.length} partition${E.length===1?"":"s"} (\u2264${T} agents at once) on ${I.model}${I.forced?" (forced)":""}\u2026 (esc to stop)`});let V=!1;try{for await(let $ of Mn({cwd:e,partitions:E,promptFor:Ci,concurrency:T,profileName:s,allowProdWrites:Ze.current,model:I.model,signal:re.signal}))if($.type==="partition_status")$.status==="running"?X(P=>P&&{...P,active:[...P.active,$.label],rows:Te(P.rows,$.label,"active"),lastAt:Date.now()}):X(P=>P&&{...P,active:P.active.filter(O=>O!==$.label),done:P.done+1,rows:Te(P.rows,$.label,$.status==="done"?"done":"failed"),lastAt:Date.now()});else if($.type==="partition_event")X(P=>{if(!P)return P;let O={...P,lastAt:Date.now()};return $.event.type==="tool"?O.tools=P.tools+1:$.event.type==="text"&&(O.chars=P.chars+$.event.delta.length),O});else if($.type==="run_done"){lt(U=>U+$.totalCostUsd),at(U=>U+$.results.reduce((Y,ae)=>Y+ae.outputTokens,0));let P=$.results.filter(U=>U.ok).length,O=$.aborted?"Stopped. ":"";V=!$.aborted&&d.setup;let Z=$.aborted?"":V?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";u({kind:"note",tone:$.aborted?"warn":$.ok?"ok":"warn",text:`${O}Authored specs \u2014 ${P}/${$.results.length} batches ok.${Z}`})}}catch($){u({kind:"note",tone:"error",text:`Spec authoring failed: ${$.message}`})}finally{we.current=null,X(null),_(!1),oe($=>$+1)}if(V){let $=await Ot();$.kind==="forward-to-agent"&&$.prompt&&await de($.prompt,{display:$.display,quietUser:!0});let P=Jo(e);P&&u({kind:"note",tone:"info",text:P.join(`
264
+ `)})}return}case"reset":{let d=or(e);if(d.length===0){u({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}Hn.current={repoName:Lt(e),targets:d},u({kind:"note",tone:"warn",text:sr(e,d).join(`
265
+ `)}),se(Lt(e));return}default:u({kind:"note",tone:"error",text:`Unknown command: /${a} \u2014 type /help.`})}},[e,r,s,Qt,u,ue,de,Xt,zn,Jn]),Zt=z(l=>{let i=(l??D).trim();if(w(""),qt(null),Ke(0),i.startsWith("/")&&(se(null),ze.current++,Bt(null),G(h=>Qi(h,i))),!i||_r(i)||Nr(i))return;qn.current.push(i);let a=na(i,xr.current);i.startsWith("/")?(u({kind:"user",text:i}),Or(a)):de(a,{display:i})},[D,_r,Nr,Or,de]),Ur=z(l=>{if(l.length>1){if(l.includes("\x1B"))return;let i=Zi(l);if(ea(i)){let a=ta(++Sa.current,i);xr.current.set(a,i),v(a)}else v(i);return}v(l)},[v]),ct=Math.max(10,Be-6),Mr=fr(()=>On(b.text,ct),[b.text,ct]),Qn=z(l=>k(i=>({...i,pos:Math.max(0,Math.min(i.text.length,i.pos+l))})),[]),en=z(l=>k(i=>({...i,pos:ra(On(i.text,ct),i.pos,l)})),[ct]),Ct=z(l=>k(i=>({...i,pos:sa(On(i.text,ct),i.pos,l)})),[ct]),ja=["\x1B[H","\x1B[1~","\x1BOH"],Aa=["\x1B[F","\x1B[4~","\x1BOF"],Lr=z((l,i)=>i.leftArrow?(Qn(-1),!0):i.rightArrow?(Qn(1),!0):l&&ja.includes(l)?(Ct("start"),!0):l&&Aa.includes(l)?(Ct("end"),!0):i.ctrl&&l==="a"?(Ct("start"),!0):i.ctrl&&l==="e"?(Ct("end"),!0):!1,[Qn,Ct]);return bd((l,i)=>{if(i.ctrl&&l==="c"){me.current?.close(),r();return}if(!F){if(N){if(i.escape){if(we.current){we.current.signal.aborted||(u({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),we.current.abort());return}$t.current||($t.current=!0,u({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),me.current?.interrupt());return}if(i.return){let a=D.trim();if(!a)return;qe.current.push(a),Bn([...qe.current]),w("");return}if(Lr(l,i))return;if(i.upArrow){en(-1);return}if(i.downArrow){en(1);return}if(i.backspace||i.delete){C();return}l&&!i.ctrl&&!i.meta&&Ur(l);return}if(Me){if(i.upArrow){Le(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){Le(a=>a&&{...a,cursor:Math.min(xe.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=xe.length){Le(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=xe[Me.cursor];Le(null);let{lines:h,changed:d,effective:m}=Nt(e,a.value??"default");for(let y of h)u({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});d&&(Rr(y=>y+1),me.current?.setModel(m));return}if(l==="s"){let a=xe[Me.cursor];Le(null),me.current?.setModel(a.value??void 0),u({kind:"note",tone:"ok",text:`\u2713 Using ${a.label} for this session only (your saved default is unchanged).`});return}if(i.escape){Le(null);return}return}if(Ce){if(i.upArrow){tt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){tt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=Ce.rows.length){tt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return||l==="s"){let a=Ce.rows[Ce.cursor];tt(null),Kn(a.name,i.return===!0);return}if(i.escape){tt(null);return}return}if(De){if(i.upArrow){nt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){nt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=De.rows.length){nt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=De.rows[De.cursor].name;if(nt(null),!bs(e,a).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 "${a}").`});let d=(()=>{try{let m=Ye(a);return m&&!(_e(m)&&!m.refreshToken)?m:null}catch{return null}})();d?(u({kind:"note",tone:"info",text:`Already signed in to ${a}.`}),(async()=>(await sn(d,a,m=>u({kind:"note",tone:"info",text:m})),await Jn(a)))()):(u({kind:"note",tone:"info",text:`Next: sign in to Document360 (${a}).`}),G(["/login"])),oe(m=>m+1);return}if(i.escape){nt(null);return}return}if(ie){if(i.upArrow){ot(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){ot(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=ie.rows.length){ot(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=ie.rows[ie.cursor],{profile:h,projectId:d}=ie;ot(null),Kt(h,d,a);return}if(i.escape){ot(null);return}return}if(he){if(i.upArrow){rt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){rt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=he.rows.length){rt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=he.rows[he.cursor],{profile:h}=he;rt(null),Ir(h,a);return}if(i.escape){rt(null);return}return}if(ce){if(i.upArrow){it(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){it(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l===" "){it(a=>a&&{...a,rows:a.rows.map((h,d)=>d===a.cursor?{...h,checked:!h.checked}:h)});return}if(i.return){let a=ce.rows.filter(h=>h.checked).map(h=>h.path);if(it(null),a.length===0){u({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}xo(e,a),u({kind:"note",tone:"ok",text:`\u2713 Scoped to ${a.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of a)u({kind:"note",tone:"info",text:` ${h}`});u({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(i.escape){it(null);return}return}if(We){if(i.upArrow){st(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){st(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=We.rows.length){st(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=We.rows[We.cursor];st(null),a.kind==="article"?zn(a.paths[0]):Xt(a.paths);return}if(i.escape){st(null);return}return}if(ge){if(i.escape){Fe(null);return}if(i.upArrow){Fe(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){Fe(a=>a&&{...a,cursor:Math.min(Math.max(0,Yt.length-1),a.cursor+1)});return}if(i.return){let a=Yt[ge.cursor];if(a){Fe(null);try{u({kind:"preview",name:Lt(a),text:ga(ca(Wt(e,a),"utf8"))})}catch(h){u({kind:"note",tone:"error",text:`Could not read ${a}: ${h.message}`})}}return}if(i.backspace||i.delete){Fe(a=>a&&{...a,query:a.query.slice(0,-1),cursor:0});return}if(l&&!i.ctrl&&!i.meta&&l.length===1){Fe(a=>a&&{...a,query:a.query+l,cursor:0});return}return}if(ye){if(i.escape){He(null);return}if(i.upArrow){He(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){He(a=>a&&{...a,cursor:Math.min(Math.max(0,zt.length-1),a.cursor+1)});return}if(i.return){let a=zt[ye.cursor];if(a){He(null),ue(a.uuid),ve.current={uuid:a.uuid,firstPrompt:a.firstPrompt,titleFired:!0},et(a.name),mr(a.uuid),Qe.current=[];let h=cr(e,a.uuid);for(let d of h)u(d);u({kind:"note",tone:"ok",text:h.length?`Resumed "${a.name}" \u2014 restored ${h.length} message(s); continue where you left off.`:`Resumed "${a.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(i.backspace||i.delete){He(a=>a&&{...a,query:a.query.slice(0,-1),cursor:0});return}if(l&&!i.ctrl&&!i.meta&&l.length===1){He(a=>a&&{...a,query:a.query+l,cursor:0});return}return}if(i.tab&&!D&&ke){w(ke),se(null),ze.current++;return}if(!D&&bt.length>0&&l&&/^[1-9]$/.test(l)){let a=bt[Number(l)-1];if(a){w(a);return}}if(!Lr(l,i)){if(Ar){if(i.upArrow){Ke(a=>Math.max(0,a-1));return}if(i.downArrow){Ke(a=>Math.min(vt.length-1,a+1));return}if(i.tab){w("/"+(vt[xt]?.name??"")+" "),Ke(0);return}if(i.return){let a=vt[xt];if(a){let h=D.trim().slice(1).split(/\s+/).slice(1).join(" ");if(ns(a.usage)&&!h){w("/"+a.name+" "),Ke(0);return}Zt("/"+a.name+(h?" "+h:""));return}}}else{if(i.upArrow){if(D!==""&&Je===null){en(-1);return}let a=qn.current;if(!a.length)return;let h=Je===null?a.length-1:Math.max(0,Je-1);qt(h),w(a[h]??"");return}if(i.downArrow){if(D!==""&&Je===null){en(1);return}let a=qn.current;if(Je===null)return;let h=Je+1;h>=a.length?(qt(null),w("")):(qt(h),w(a[h]??""));return}}if(i.return){Zt();return}if(i.backspace||i.delete){C();return}if(i.escape){w(""),Ke(0),se(null),G([]),Bt(null);return}l&&!i.ctrl&&!i.meta&&Ur(l)}}}),Oe(()=>{if(N||F)return;let l=qe.current.shift();l!==void 0&&(Bn([...qe.current]),Zt(l))},[N,F,Zt]),W(K,{flexDirection:"column",width:Be,children:[Yn!==null&&W(K,{marginTop:1,flexDirection:"column",children:[Yn.truncated&&R(S,{dimColor:!0,children:"\u2026"}),R(S,{children:Yn.text})]}),N&&(Pr?R(lp,{p:Pr}):R(ip,{startTime:Ue.current,chars:Ta})),R(K,{borderStyle:"round",borderColor:jr.prod?"yellow":H,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:D?Mr.map((l,i)=>{let a=b.text.slice(l.start,l.end),h=i===Un(Mr,b.pos),d=Math.min(b.pos,l.end)-l.start;return W(S,{children:[R(S,{color:H,children:i===0?"> ":" "}),h?W(wr,{children:[a.slice(0,d),R(Wn,{ch:a[d]??" "}),a.slice(d+1)]}):a||" "]},`${i}-${l.start}`)}):W(S,{children:[R(S,{color:H,children:"> "}),ke&&!N?W(wr,{children:[R(Wn,{ch:ke[0],dim:!0}),R(S,{color:"gray",children:ke.slice(1)}),R(S,{dimColor:!0,children:" (tab)"})]}):Vn.isSetup||!Pa?W(wr,{children:[R(Wn,{ch:Vn.text[0],dim:!0}),R(S,{color:"gray",children:Vn.text.slice(1)})]}):R(Wn,{ch:" "})]})}),Cr.length>0&&R(K,{flexDirection:"column",paddingX:1,children:Cr.map((l,i)=>R(S,{color:"gray",children:`\u29D7 queued: ${l}`},`${i}-${l.slice(0,24)}`))}),Me?W(K,{flexDirection:"column",paddingX:1,children:[R(S,{color:H,bold:!0,children:"Select model"}),R(S,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),xe.map((l,i)=>W(S,{color:i===Me.cursor?H:void 0,children:[i===Me.cursor?"\u276F ":" ",`${i+1}. ${l.label}${i===Me.current?" \u2714":""}`.padEnd(16),R(S,{color:"gray",children:l.desc})]},l.label)),R(S,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):Ce?W(K,{flexDirection:"column",paddingX:1,children:[R(S,{color:H,bold:!0,children:"Switch connection profile"}),Ce.rows.map((l,i)=>W(S,{color:i===Ce.cursor?H:void 0,children:[i===Ce.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===Ce.current?" \u2714":""}`.padEnd(20),R(S,{color:"gray",children:`${l.env} \xB7 ${l.who??"not signed in"}`}),l.prod?R(S,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},l.name)),R(S,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):De?W(K,{flexDirection:"column",paddingX:1,children:[R(S,{color:H,bold:!0,children:"Pick your Document360 environment"}),De.rows.map((l,i)=>W(S,{color:i===De.cursor?H:void 0,children:[i===De.cursor?"\u276F ":" ",`${i+1}. ${l.name}`.padEnd(16),R(S,{color:"gray",children:l.apiUrl})]},l.name)),R(S,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):ie?W(K,{flexDirection:"column",paddingX:1,children:[R(S,{color:H,bold:!0,children:"Switch workspace"}),R(S,{dimColor:!0,children:`environment ${ie.environment} \xB7 project ${ie.projectName??`${ie.projectId.slice(0,8)}\u2026`}`}),ie.rows.map((l,i)=>W(S,{color:i===ie.cursor?H:void 0,children:[i===ie.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===ie.current?" \u2714":""}`.padEnd(30),R(S,{color:"gray",children:l.type??""})]},l.id)),R(S,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):he?W(K,{flexDirection:"column",paddingX:1,children:[R(S,{color:H,bold:!0,children:"Choose project"}),R(S,{dimColor:!0,children:`environment ${he.environment}`}),he.rows.map((l,i)=>W(S,{color:i===he.cursor?H:void 0,children:[i===he.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===he.current?" \u2714":""}`.padEnd(30),R(S,{color:"gray",children:l.sub??""})]},l.id)),R(S,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):ce?(()=>{let i=Math.min(Math.max(0,ce.cursor-Math.floor(7)),Math.max(0,ce.rows.length-14)),a=ce.rows.slice(i,i+14),h=ce.rows.filter(d=>d.checked).length;return W(K,{flexDirection:"column",paddingX:1,children:[R(S,{color:H,bold:!0,children:`Which folders back the user docs? (${h} selected of ${ce.rows.length})`}),i>0?R(S,{dimColor:!0,children:` \u2191 ${i} more`}):null,a.map((d,m)=>{let y=i+m;return W(S,{color:y===ce.cursor?H:void 0,children:[y===ce.cursor?"\u276F ":" ",d.checked?"\u25C9 ":"\u25CB ",d.path.padEnd(Math.min(48,Be-34)),R(S,{color:"gray",children:kn(d)})]},d.path)}),i+14<ce.rows.length?R(S,{dimColor:!0,children:` \u2193 ${ce.rows.length-i-14} more`}):null,R(S,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():We?W(K,{flexDirection:"column",paddingX:1,children:[R(S,{color:H,bold:!0,children:"Publish to Document360 \u2014 pick a category (whole) or an article:"}),We.rows.map((l,i)=>{let a=i===We.cursor,h=l.indent?" ":" ",d=`${i+1}.`.padEnd(3);return W(S,{color:a?H:void 0,bold:l.kind==="category"&&!a,children:[a?"\u276F ":" ",`${h}${d} ${l.label}`.padEnd(Math.min(56,Be-24)),l.note?R(S,{color:"gray",children:l.note}):null]},`${l.kind}:${l.paths.join(",")}`)}),R(S,{dimColor:!0,children:"\u2191\u2193 move \xB7 1-9 jump \xB7 enter publish (draft) \xB7 esc cancel"})]}):ge?W(K,{flexDirection:"column",paddingX:1,children:[R(S,{color:H,bold:!0,children:`Preview article${ge.query?` \u2014 filter: ${ge.query}`:" (type to filter)"}`}),Yt.length===0?R(S,{color:"gray",children:"no articles match"}):Yt.map((l,i)=>W(S,{color:i===ge.cursor?H:void 0,children:[i===ge.cursor?"\u276F ":" ",l]},l)),R(S,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ye?W(K,{flexDirection:"column",paddingX:1,children:[R(S,{color:H,bold:!0,children:`Resume session${ye.query?` \u2014 filter: ${ye.query}`:" (type to filter)"}`}),zt.length===0?R(S,{color:"gray",children:"no sessions match"}):zt.map((l,i)=>W(S,{color:i===ye.cursor?H:void 0,children:[i===ye.cursor?"\u276F ":" ",l.name.slice(0,28).padEnd(30),R(S,{color:i===ye.cursor?H:"gray",children:l.firstPrompt.slice(0,Math.max(10,Be-40))})]},l.uuid)),R(S,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):Ar?R(K,{flexDirection:"column",children:vt.map((l,i)=>W(S,{color:i===xt?H:void 0,children:[i===xt?"\u276F ":" ",l.usage.padEnd(22)," ",R(S,{color:i===xt?H:"gray",children:l.name==="model"&&Gn?`${l.desc} (currently ${Gn})`:l.desc})]},l.name))}):!D&&bt.length>0?W(K,{flexDirection:"column",paddingX:1,children:[vr?R(S,{color:H,children:vr}):null,bt.map((l,i)=>W(S,{children:[R(S,{color:H,children:i+1})," ",l.slice(0,Math.max(20,Be-5))]},l)),R(S,{dimColor:!0,children:`press 1-${bt.length} to fill the command \xB7 esc dismiss`})]}):R(K,{paddingX:1,children:W(S,{color:"gray",children:[jr.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${Gn??"model n/a"}${t.kind==="api"?Sr>0?` \xB7 ${le(Sr)}`:"":Tr>0?` \xB7 ${Ie(Tr)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}L();import{jsx as dp}from"react/jsx-runtime";async function $a(e=process.cwd(),t="auto",n,o="0.0.0"){let r=up(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 ${A("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${A("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=cp(dp(xa,{cwd:e,auth:r,profileName:n,version:o}));await s(),process.stdout.write(`
266
+ `),process.exit(0)}var hp=fp(import.meta.url),va=hp("../package.json"),Se=new pp;function kr(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}Se.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=>{kr(e),await rn({profile:e.profile,manual:e.manual})});Se.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{kr(e),await Jr({profile:e.profile})});Se.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{kr(e),await Kr({profile:e.profile})});var br=Se.command("profile").description("Manage connection profiles for the current repo");br.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>an(process.cwd()));br.command("use <name>").description("Set the default profile for this repo").action(e=>ln(process.cwd(),e));br.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>cn(process.cwd(),e));var Ca=Se.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");Ca.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>ut(process.cwd(),e.profile));Ca.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await qr(process.cwd(),e,t.profile)});Se.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Zr());Se.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(()=>(vn(),gi)),n=await t(process.cwd());for(let o of e(n))console.log(o);process.exitCode=n.some(o=>o.level==="fail")?1:0});Se.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(va.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(mp.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 es(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 Bi(e.cwd,e.auth,e.profile):await $a(e.cwd,e.auth,e.profile,va.version)});Se.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
@@ -26,6 +26,14 @@ export declare function creatorNotesTemplate(): string;
26
26
  (with a human creator-notes template) + the hints inbox, then show ONE copy-run prompt the SOURCE
27
27
  repo's coding agent executes to onboard the docs agent (writes the brief, persists the ongoing rule
28
28
  into CLAUDE.md/AGENTS.md + commits, solicits tribal knowledge) — no manual file edits by the user. */
29
+ /**
30
+ * Scaffold the dev→docs hand-off artifacts (ongoing-hints inbox, architecture brief home + human
31
+ * CREATOR-NOTES, two-way message mailbox, the committed guide, the version trace). Idempotent and
32
+ * silent — it writes files and returns the builder's staleness so the caller can render the hand-off.
33
+ * Shared by `/devhints`, `/init` (which now folds this in so there's no separate onboarding step),
34
+ * and the TUI wizard finale.
35
+ */
36
+ export declare function scaffoldDevhints(cwd: string): DevhintsStaleness;
29
37
  export declare function devhintsCommand(_args: string[], ctx: ReplContext): Promise<SlashCommandResult>;
30
38
  /**
31
39
  * The ONE prompt the user copies and runs in their SOURCE repo's coding agent (Claude Code / Codex /
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document360-writer",
3
- "version": "0.4.67",
3
+ "version": "0.4.68",
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.39",
37
+ "document360-engine": "^0.2.40",
38
38
  "ink": "^5.2.1",
39
39
  "picocolors": "^1.1.1",
40
40
  "react": "^18.3.1",